public void GetCoordinates_required_parameters_not_supplied_throws_CoordinateNotFoundException(string type, string name, string version)
        {
            // Arrange
            var classUnderTest       = new CoordinatesService();
            var coordinatePartsModel = new CoordinatePartsModel()
            {
                Type    = type,
                Name    = name,
                Version = version
            };

            // Act
            // Assert
            Assert.Throws <CoordinateNotFoundException>(() => classUnderTest.GetCoordinates(coordinatePartsModel));
        }
        private void Validate(CoordinatePartsModel coordinatePartsModel)
        {
            if (string.IsNullOrEmpty(coordinatePartsModel.Type))
            {
                ThrowCoordinateNotFoundException("Type");
            }

            if (string.IsNullOrEmpty(coordinatePartsModel.Name))
            {
                ThrowCoordinateNotFoundException("Name");
            }

            if (string.IsNullOrEmpty(coordinatePartsModel.Version))
            {
                ThrowCoordinateNotFoundException("Version");
            }
        }
        public void GetCoordinates_only_required_parameters_supplied_returns_valid_package_url()
        {
            // Arrange
            var classUnderTest       = new CoordinatesService();
            var expected             = "pkg:nuget/[email protected]";
            var coordinatePartsModel = new CoordinatePartsModel()
            {
                Type    = "nuget",
                Name    = "System.Net.Http",
                Version = "4.3.1"
            };

            // Act
            var actual = classUnderTest.GetCoordinates(coordinatePartsModel);

            // Assert
            Assert.AreEqual(expected, actual);
        }
        public string GetCoordinates(CoordinatePartsModel coordinatePartsModel)
        {
            Validate(coordinatePartsModel);

            return($"pkg:{coordinatePartsModel.Type}/{GetOptionNameSpace(coordinatePartsModel.Namespace)}{coordinatePartsModel.Name}@{coordinatePartsModel.Version}");
        }
        public void CreateInitialReportShell(int reportId, CoordinatePartsModel coordinatePart)
        {
            // check dbo.component.name on coordinatePart.Name
            // if it exists, return the id
            // else create, return the id
            var component   = _componentRepository.SelectByName(coordinatePart.Name.Trim());
            var componentId = component.Id;

            if (componentId == 0)
            {
                componentId = _componentRepository.Insert(new ComponentModel()
                {
                    Name = coordinatePart.Name.Trim()
                });
            }

            // check dbo.oss_index.component_id
            // if it exists, check `expire_date`
            //    - if older than 30 days, call remote api
            //    - insert results if any to dbo.oss_index_vulnerabilities
            //    - update `expire_date` = NOW
            // else
            //    - create with `expire_date` = NOW
            //    - call remote api
            //    - insert results if any to dbo.oss_index_vulnerabilities
            var ossIndex   = _ossIndexRepository.SelectByComponentId(componentId);
            var ossIndexId = ossIndex.Id;

            if (ossIndexId == 0)
            {
                ossIndex = new OssIndexModel()
                {
                    ComponentId    = componentId,
                    ExpireDate     = DateTime.Now.AddMonths(1),
                    HttpStatus     = (int)HttpStatusCode.Processing,
                    HttpStatusDate = DateTime.Now
                };

                ossIndexId = _ossIndexRepository.Insert(ossIndex);
                ossIndex   = _ossIndexRepository.Select(ossIndexId);
            }

            /* TODO
             *
             * 1. this is always zero as we cannot pass things like `1.4.0` as a decimal, consider deprecating `[vulnuscloud].[dbo].[oss_index].[version]` as this data is already in `[vulnuscloud].[dbo].[oss_index].[coordinates]`
             * 2. [vulnuscloud].[dbo].[oss_index].[coordinates] should be normalized:
             *      `pkg:Nuget/[email protected]`
             *      > pkg: is known, comes from `_coordinatesService`
             *      > Nuget/ should rather be stored as `[vulnuscloud].[dbo].[oss_index].[package_type_id]` - then this links to PackageTypeRepository
             *      > BeITMemcached@ can be read from [vulnuscloud].[dbo].[component].[id] = [vulnuscloud].[dbo].[oss_index].[component_id]
             *      > 1.4.0 could then be stored as [vulnuscloud].[dbo].[oss_index].[version]
             *
             *      [vulnuscloud].[dbo].[oss_index].[coordinates] could then be generated when needed.
             */

            if (decimal.TryParse(coordinatePart.Version, out decimal coordinatePartVersion))
            {
                ossIndex.Version = coordinatePartVersion;
            }

            ossIndex.Coordinates = _coordinatesService.GetCoordinates(coordinatePart);
            _ossIndexRepository.Update(ossIndex);

            _reportLinesRepository.Insert(new ReportLinesModel()
            {
                OssIndexId = ossIndexId,
                ReportId   = reportId
            });
        }