public void AddressesGatewatShouldThrowAnErrorWhenAPICallIsConsideredInvalid()
        {
            // arrange
            var contextResponse = Randomm.AddressesAPIContextResponse(400);

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

            // act
            Action gatewayCall = () => _classUnderTest.GetPostcodeCoordinates(It.IsAny <string>());

            // assert
            gatewayCall.Should().Throw <BadAPICallException>().And.Message.Should().NotBeEmpty();
        }
        public void AddressGatewayShouldReturnNullCoordinatesWhenNoAddressesForAGivenPostcodeWereFound()
        {
            // arrange
            var contextResponse = Randomm.AddressesAPIContextResponse(200);

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

            // act
            var gatewayResponse = _classUnderTest.GetPostcodeCoordinates(It.IsAny <string>());

            // assert
            gatewayResponse.Should().Be(null);
        }
        [TestCase(TestName = "Given Addresses API context's GetAddressesRequest method is called, When internal server error response is returned, Then the AddressesGateway throws APICallInternalException.")] // TODO: test exception message
        public void AddressesGatewayShouldThrowAnErrorWhenAPICallReturnInternalFailure()
        {
            // arrange
            var contextResponse = Randomm.AddressesAPIContextResponse(500);

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

            // act
            Action gatewayCall = () => _classUnderTest.GetPostcodeCoordinates(It.IsAny <string>());

            //assert
            gatewayCall.Should().Throw <APICallInternalException>();
        }
        public void Success()
        {
            // arrange
            var expectedCoordinates = Randomm.Coordinates();
            var contextResponse     = Randomm.AddressesAPIContextResponse(200, expectedCoordinates);

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

            // act
            var gatewayResponse = _classUnderTest.GetPostcodeCoordinates(It.IsAny <string>());

            // assert
            gatewayResponse.Should().BeEquivalentTo(expectedCoordinates);
        }
        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);
        }
        public void AddressesGatewayShouldThrowAnErrorWhenAPICallReturnsUnexpectedSchema(int statusCode) // if anything about the Addresses API changes, ResponseSchemaNotRecognisedException //TODO: should contain api name
        {
            // arrange
            var contextResponse = Randomm.AddressesAPIContextResponse(statusCode == 9000 ? 400 : statusCode);

            switch (statusCode)
            {
            case 200:
                contextResponse.JsonContent =
                    contextResponse.JsonContent.Replace("Data", Randomm.Word()
                                                        , StringComparison.OrdinalIgnoreCase);
                break;

            case 400:
                contextResponse.JsonContent =
                    contextResponse.JsonContent.Replace("validationErrors", Randomm.Word()
                                                        , StringComparison.OrdinalIgnoreCase);
                break;

            case 500:
                contextResponse.JsonContent =
                    contextResponse.JsonContent.Replace("Errors", Randomm.Word()
                                                        , StringComparison.OrdinalIgnoreCase);
                break;

            default:
                contextResponse.JsonContent =
                    contextResponse.JsonContent.Replace("error", Randomm.Word()
                                                        , StringComparison.OrdinalIgnoreCase);
                break;
            }

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

            // act
            Action gatewayCall = () => _classUnderTest.GetPostcodeCoordinates(It.IsAny <string>());

            // assert
            gatewayCall.Should().Throw <ResponseSchemaNotRecognisedException>();
        }