Exemplo n.º 1
0
        public void WhenAddressesGatewayReturnsNullThenGetMultipleUsecasePopulatesDistanceAndMetadataFields()
        {
            // arrange
            var request = Randomm.Create <SearchServicesRequest>();

            request.PostCode = Randomm.Postcode();

            var gatewayResponse = Randomm.SSGatewayResult();

            _mockServicesGateway.Setup(g => g.SearchServices(It.IsAny <SearchServicesRequest>())).Returns(gatewayResponse);

            var expectedPostcodeCoords = Randomm.Coordinates();

            _mockAddressesGateway.Setup(g => g.GetPostcodeCoordinates(It.IsAny <string>())).Returns(expectedPostcodeCoords);

            // act
            var usecaseResponse = _classUnderTest.ExecuteGet(request);

            // assert
            usecaseResponse.Metadata.PostCode.Should().Be(request.PostCode);
            usecaseResponse.Metadata.PostCodeLatitude.Should().Be(expectedPostcodeCoords.Latitude);
            usecaseResponse.Metadata.PostCodeLongitude.Should().Be(expectedPostcodeCoords.Longitude);
            usecaseResponse.Metadata.Error.Should().BeNull();
            usecaseResponse.Services.Should().OnlyContain(s => s.Locations.All(l => l.Latitude.HasValue && l.Longitude.HasValue ? l.Distance != null : l.Distance == null));
        }
Exemplo n.º 2
0
        public void WhenAddressesGatewayReturnsNullThenUsecasePopulatesDistanceAndMetadataFields()
        {
            // arrange
            var request = Randomm.Create <GetServiceByIdRequest>();

            request.PostCode = Randomm.Postcode();

            var expectedService = EntityHelpers.CreateService().ToDomain();

            _mockServicesGateway.Setup(g => g.GetService(It.IsAny <int>())).Returns(expectedService);

            var expectedPostcodeCoords = Randomm.Coordinates();

            _mockAddressesGateway.Setup(g => g.GetPostcodeCoordinates(It.IsAny <string>())).Returns(expectedPostcodeCoords);

            // act
            var usecaseResponse = _classUnderTest.ExecuteGet(request);

            // assert
            usecaseResponse.Metadata.PostCode.Should().Be(request.PostCode);
            usecaseResponse.Metadata.PostCodeLatitude.Should().Be(expectedPostcodeCoords.Latitude);
            usecaseResponse.Metadata.PostCodeLongitude.Should().Be(expectedPostcodeCoords.Longitude);
            usecaseResponse.Metadata.Error.Should().BeNull();
            usecaseResponse.Service.Locations.Should().OnlyContain(l => l.Latitude.HasValue && l.Longitude.HasValue ? l.Distance != null : l.Distance == null);
        }
Exemplo n.º 3
0
        public void WhenAddressesGatewayThrowsAnExceptionThenUsecasePopulatesTheMetadataErrorField()
        {
            // arrange
            var request = Randomm.Create <GetServiceByIdRequest>();

            request.PostCode = Randomm.Postcode();

            var expectedService = EntityHelpers.CreateService().ToDomain();

            _mockServicesGateway.Setup(g => g.GetService(It.IsAny <int>())).Returns(expectedService);

            var expectedException = new Exception(Randomm.Text());

            _mockAddressesGateway.Setup(g => g.GetPostcodeCoordinates(It.IsAny <string>())).Throws(expectedException);

            // act
            var usecaseResponse = _classUnderTest.ExecuteGet(request);

            // assert
            usecaseResponse.Service.Locations.Should().OnlyContain(l => l.Distance == null);
            usecaseResponse.Metadata.PostCode.Should().Be(request.PostCode);
            usecaseResponse.Metadata.PostCodeLatitude.Should().Be(null);
            usecaseResponse.Metadata.PostCodeLongitude.Should().Be(null);
            usecaseResponse.Metadata.Error.Should().Be(expectedException.Message);
        }
Exemplo n.º 4
0
        [TestCase(TestName = "Given a postcode, When usecase's ExecuteGet method is called, Then it returns a collection of services ordered asc by the closest service location distance.")] // just as above, except this confirms that sorting works fine under normal conditions
        public void GivenAPostcodeReturnedServicesAreOrderedAscByDistance()
        {
            // arrange
            var request = Randomm.Create <SearchServicesRequest>();

            request.PostCode = Randomm.Postcode();

            var addressGatewayResponse = Randomm.Coordinates();

            _mockAddressesGateway.Setup(g => g.GetPostcodeCoordinates(It.IsAny <string>())).Returns(addressGatewayResponse);

            var serviceGatewayResponse = Randomm.SSGatewayResult();

            _mockServicesGateway.Setup(g => g.SearchServices(It.IsAny <SearchServicesRequest>())).Returns(serviceGatewayResponse);

            var fullMLength = serviceGatewayResponse.FullMatchServices.Count;

            // act
            var usecaseResponse             = _classUnderTest.ExecuteGet(request);
            var fullMServicesWithLocations  = usecaseResponse.Services.Take(fullMLength).ToList();
            var splitMServicesWithLocations = usecaseResponse.Services.Skip(fullMLength).ToList();

            // assert
            AssertThatServiceCollectionIsInAscendingDistancesOrder(fullMServicesWithLocations);
            AssertThatServiceCollectionIsInAscendingDistancesOrder(splitMServicesWithLocations);
        }
Exemplo n.º 5
0
        public void GivenAPostcodeReturnedServicesAreOrderedInAWayWhereIfTheyHaveNoChildLocationsTheyAreConsideredTheMostDistant()
        {
            // arrange
            var request = Randomm.Create <SearchServicesRequest>();

            request.PostCode = Randomm.Postcode();

            var addressGatewayResponse = Randomm.Coordinates();

            _mockAddressesGateway.Setup(g => g.GetPostcodeCoordinates(It.IsAny <string>())).Returns(addressGatewayResponse);

            var serviceGatewayResponse = Randomm.SSGatewayResult();

            serviceGatewayResponse.FullMatchServices.FirstOrDefault().ServiceLocations  = new List <ServiceLocation>();
            serviceGatewayResponse.SplitMatchServices.FirstOrDefault().ServiceLocations = new List <ServiceLocation>();
            _mockServicesGateway.Setup(g => g.SearchServices(It.IsAny <SearchServicesRequest>())).Returns(serviceGatewayResponse);

            var fullMLength  = serviceGatewayResponse.FullMatchServices.Count;
            var splitMLength = serviceGatewayResponse.SplitMatchServices.Count;
            var fullMatchServiceNoLocationsName  = serviceGatewayResponse.FullMatchServices.FirstOrDefault().Name; //unique enough due to being generated as hash
            var splitMatchServiceNoLocationsName = serviceGatewayResponse.SplitMatchServices.FirstOrDefault().Name;

            // act
            var usecaseResponse = _classUnderTest.ExecuteGet(request);

            // assert
            usecaseResponse.Services.Take(fullMLength).Last().Name.Should().Be(fullMatchServiceNoLocationsName); // the service with no locations should be last
            usecaseResponse.Services.Last().Name.Should().Be(splitMatchServiceNoLocationsName);

            var fullMServicesWithLocations  = usecaseResponse.Services.Take(fullMLength - 1).ToList();
            var splitMServicesWithLocations = usecaseResponse.Services.Skip(fullMLength).Take(splitMLength - 1).ToList();

            AssertThatServiceCollectionIsInAscendingDistancesOrder(fullMServicesWithLocations); // checking whether sorting works in the context of empty service location existing. essentially a check that sorting doesn't stop half way just because it encountered an exceptional service with no locations
            AssertThatServiceCollectionIsInAscendingDistancesOrder(splitMServicesWithLocations);
        }
Exemplo n.º 6
0
        public void WhenAddressesGatewayThrowsAnExceptionThenGetMultipleUsecasePopulatesTheMetadataErrorField()
        {
            // arrange
            var request = Randomm.Create <SearchServicesRequest>();

            request.PostCode = Randomm.Postcode();

            var gatewayResponse = Randomm.SSGatewayResult();

            _mockServicesGateway.Setup(g => g.SearchServices(It.IsAny <SearchServicesRequest>())).Returns(gatewayResponse);

            var expectedException = new Exception(Randomm.Text());

            _mockAddressesGateway.Setup(g => g.GetPostcodeCoordinates(It.IsAny <string>())).Throws(expectedException);

            // act
            var usecaseResponse = _classUnderTest.ExecuteGet(request);

            // assert
            usecaseResponse.Metadata.PostCode.Should().Be(request.PostCode);
            usecaseResponse.Metadata.PostCodeLatitude.Should().Be(null);
            usecaseResponse.Metadata.PostCodeLongitude.Should().Be(null);
            usecaseResponse.Metadata.Error.Should().Be(expectedException.Message);
            usecaseResponse.Services.Should().OnlyContain(s => s.Locations.All(l => l.Distance == null));
        }
        public void AddressesGatewayShouldCallAddressesContextWithGivenPostcode()
        {
            // rubbish arrange
            var irrelevantResponse = Randomm.AddressesAPIContextResponse(200);

            _mockAddressesApiContext.Setup(c => c.GetAddressesRequest(It.IsAny <string>())).Returns(irrelevantResponse);

            // arrange
            var postcode = Randomm.Postcode();

            // act
            _classUnderTest.GetPostcodeCoordinates(postcode);

            // assert
            _mockAddressesApiContext.Verify(c => c.GetAddressesRequest(It.Is <string>(p => p == postcode)), Times.Once);
        }
Exemplo n.º 8
0
        public void FactoryPopulatesTheMetadataFieldWithWhatIsPassedInAsAnArgument()
        {
            // arrange
            var expectedMetadata = new Metadata
            {
                Error             = Randomm.Text(),
                PostCode          = Randomm.Postcode(),
                PostCodeLatitude  = Randomm.Latitude(),
                PostCodeLongitude = Randomm.Longitude()
            };

            // act
            var factoryResult = ServiceFactory.SearchServiceUsecaseResponse(null, null, expectedMetadata);

            // assert
            factoryResult.Metadata.Error.Should().Be(expectedMetadata.Error);
            factoryResult.Metadata.PostCode.Should().Be(expectedMetadata.PostCode);
            factoryResult.Metadata.PostCodeLatitude.Should().Be(expectedMetadata.PostCodeLatitude);
            factoryResult.Metadata.PostCodeLongitude.Should().Be(expectedMetadata.PostCodeLongitude);
        }
Exemplo n.º 9
0
        public void GivenAPostcodeIfServicesGatewayReturnsAnEmptyCollectionThenUsecaseAlsoReturnsEmptyCollection() // essentially a test to see if the sorting implementation doesn't fall over upon empty collection.
        {
            // arrange
            var request = Randomm.Create <SearchServicesRequest>();

            request.PostCode = Randomm.Postcode();

            var addressGatewayResponse = Randomm.Coordinates();

            _mockAddressesGateway.Setup(g => g.GetPostcodeCoordinates(It.IsAny <string>())).Returns(addressGatewayResponse);

            var serviceGatewayResponse = new SearchServiceGatewayResult(
                fullMatchServices: new List <ServiceEntity>(),
                splitMatchServices: new List <ServiceEntity>()
                );

            _mockServicesGateway.Setup(g => g.SearchServices(It.IsAny <SearchServicesRequest>())).Returns(serviceGatewayResponse);

            // act
            var usecaseResponse = _classUnderTest.ExecuteGet(request);

            // assert
            usecaseResponse.Services.Should().BeEmpty();
        }
Exemplo n.º 10
0
        public async Task ReturnsThatMatchingService()
        {
            // arrange
            DatabaseContext.Database.RollbackTransaction();
            var service = EntityHelpers.CreateService();

            DatabaseContext.Services.Add(service);
            DatabaseContext.SaveChanges();
            var expectedService  = DatabaseContext.Services.FirstOrDefault();
            var searchSearviceId = expectedService.Id;
            // act
            var requestUri     = new Uri($"api/v1/services/{searchSearviceId}?postcode={Randomm.Postcode()}", UriKind.Relative);
            var response       = Client.GetAsync(requestUri).Result;
            var content        = response.Content;
            var stringResponse = await content.ReadAsStringAsync().ConfigureAwait(true);

            var actualService = JsonConvert.DeserializeObject <GetServiceResponse>(stringResponse);

            // assert
            response.StatusCode.Should().Be(200);
            actualService.Service.Id.Should().Be(expectedService.Id);
            actualService.Service.Status.Should().Be(service.Status);
        }