public void When_NoDefaultCredentials_Then_Cancel_Uses_VendorCredentialsFromConfig()
        {
            // set up
            var subject = new OfferingSaleCanceller(_configuration.Object, _serializer.Object, _deserializer.Object, _httpClientCreator.Object, _authTokenGenerator.Object);

            // execute
            subject.Cancel(_offeringId);

            // verify
            _authTokenGenerator.Verify(x => x.Generate(
                                           Guid.Parse(ConfigurationManager.AppSettings[VendorCredentialsFromConfig.IdKey]),
                                           Guid.Parse(ConfigurationManager.AppSettings[VendorCredentialsFromConfig.SharedSecretKey])), Times.Once);
        }
        public void When_NoDefaultCredentials_Then_Cancel_Throws_InvalidOperationException()
        {
            // set up
            var subject = new OfferingSaleCanceller(_configuration.Object, _serializer.Object, _deserializer.Object, _httpClientCreator.Object, _authTokenGenerator.Object);
            var id      = Guid.NewGuid();

            // execute
            var ex = Assert.Throws <AggregateException>(() => subject.Cancel(id));

            // verify
            var baseEx = (InvalidOperationException)ex.GetBaseException();

            Assert.Equal("No default credentials defined", baseEx.Message);
        }
        public OfferingSaleCancellerFixture()
        {
            _configuration      = new Mock <IOfferingSaleCancellerConfiguration>();
            _serializer         = new Mock <ISerializer>();
            _deserializer       = new Mock <IDeserializer>();
            _httpClientCreator  = new Mock <IHttpClientCreator>();
            _authTokenGenerator = new Mock <IAuthTokenGenerator>();
            _vendorCredentials  = new Mock <IVendorCredentials>();

            _serviceUrl  = new Uri("https://test.com");
            _throwErrors = true;

            _configuration.Setup(x => x.ServiceUrl).Returns(_serviceUrl);
            _configuration.Setup(x => x.ThrowExceptions).Returns(_throwErrors);

            _id     = Guid.NewGuid();
            _secret = Guid.NewGuid();

            _vendorCredentials.Setup(x => x.Id).Returns(_id);
            _vendorCredentials.Setup(x => x.SharedSecret).Returns((_secret));

            _offeringId = Guid.Parse("46017496-FB9D-4B5F-95A8-209D71390A26");

            _offeringIdError = Guid.Parse("287AF95F-92B9-4A01-B98F-3D4312D2BAE6");

            _offeringIdErrorWithNoBody = Guid.Parse("6D51256A-A220-4004-831C-A9924B822890");

            _offeringIdInvalid = Guid.Parse("E0CF4C13-4E6E-43F3-B4D6-B1C5F59C79D0");

            _authToken = "token";

            _okContent                   = "okContent";
            _okResponseBody              = "okResponse";
            _errorContent                = "errorContent";
            _errorResponseBody           = "errorResponse";
            _errorContentWithNoBody      = "errorContentWithNoBody";
            _errorResponseBodyWithNoBody = string.Empty;
            _invalidContent              = "invalidContent";
            _invalidResponseBody         = "InvalidResponse";

            _serializer.Setup(x => x.Serialize(It.Is <OfferingSaleCancellationRequest>(y => y.OfferingId.Equals(_offeringId)))).Returns(_okContent);
            _serializer.Setup(x => x.Serialize(It.Is <OfferingSaleCancellationRequest>(y => y.OfferingId.Equals(_offeringIdError)))).Returns(_errorContent);
            _serializer.Setup(x => x.Serialize(It.Is <OfferingSaleCancellationRequest>(y => y.OfferingId.Equals(_offeringIdErrorWithNoBody)))).Returns(_errorContentWithNoBody);
            _serializer.Setup(x => x.Serialize(It.Is <OfferingSaleCancellationRequest>(y => y.OfferingId.Equals(_offeringIdInvalid)))).Returns(_invalidContent);

            _mockHttpHandler = new MockHttpMessageHandler();

            _mockHttpHandler.When(HttpMethod.Post, _serviceUrl.AbsoluteUri)
            .WithContent(_okContent)
            .WithHeaders("X-InsuranceHub-AuthToken", _authToken)
            .Respond("application/json", _okResponseBody);

            _mockHttpHandler.When(HttpMethod.Post, _serviceUrl.AbsoluteUri)
            .WithContent(_errorContent)
            .WithHeaders("X-InsuranceHub-AuthToken", _authToken)
            .Respond(HttpStatusCode.InternalServerError, "application/json", _errorResponseBody);

            _mockHttpHandler.When(HttpMethod.Post, _serviceUrl.AbsoluteUri)
            .WithContent(_errorContentWithNoBody)
            .WithHeaders("X-InsuranceHub-AuthToken", _authToken)
            .Respond(HttpStatusCode.InternalServerError, "application/json", _errorResponseBodyWithNoBody);

            _mockHttpHandler.When(HttpMethod.Post, _serviceUrl.AbsoluteUri)
            .WithContent(_invalidContent)
            .WithHeaders("X-InsuranceHub-AuthToken", _authToken)
            .Respond(HttpStatusCode.BadRequest, "application/json", _invalidResponseBody);

            _client = new HttpClient(_mockHttpHandler);

            _errorResponse = new ErrorResponse
            {
                ValidationMessages = new List <string>(),
                Message            = "errorMessage"
            };

            _invalidResponse = new ErrorResponse
            {
                ValidationMessages = new List <string> {
                    "validationMessage1", "validationMessage2"
                },
                Message = "invalidRequest"
            };

            _deserializer.Setup(x => x.Deserialize <ErrorResponse>(_errorResponseBody)).Returns(_errorResponse);
            _deserializer.Setup(x => x.Deserialize <ErrorResponse>(_invalidResponseBody)).Returns(_invalidResponse);

            _httpClientCreator.Setup(x => x.Create(It.IsAny <IVendorCredentials>())).Returns(_client);

            _authTokenGenerator.Setup(x => x.Generate(It.IsAny <Guid>(), It.IsAny <Guid>())).Returns(_authToken);

            _subject = new OfferingSaleCanceller(_configuration.Object, _serializer.Object, _deserializer.Object, _httpClientCreator.Object, _authTokenGenerator.Object, _vendorCredentials.Object);
        }