Returns_400_If_Request_Shipment_But_Invalid()
            {
                // Arrange
                Guid[] keys = new[] {
                    Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid()
                };
                var requestKey = Guid.NewGuid();
                var config     = IntegrationTestHelper
                                 .GetInitialIntegrationTestConfig(GetContainer(keys));

                var shipmentRequestModel = new ShipmentUpdateRequestModel {
                    ReceiverName      = "ANameWhichIsMoreThan50CharsANameWhichIsMoreThan50Chars",
                    ReceiverSurname   = "ASurnameWhichIsMoreThan50CharsASurnameWhichIsMoreThan50Chars",
                    ReceiverAddress   = "AnAddressWhichIsMoreThan50CharsAnAddressWhichIsMoreThan50Chars",
                    ReceiverCity      = "ACityWhichIsMoreThan50CharsACityWhichIsMoreThan50Chars",
                    ReceiverTelephone = "ATelephoneWhichIsMoreThan50CharsATelephoneWhichIsMoreThan50Chars",
                    ReceiverEmail     = "fooexample.com"
                };

                var request = HttpRequestMessageHelper
                              .ConstructRequest(
                    httpMethod: HttpMethod.Put,
                    uri: string.Format(
                        "https://localhost/{0}/{1}",
                        ApiBaseRequestPath, requestKey),
                    mediaType: "application/json",
                    username: Constants.ValidAdminUserName,
                    password: Constants.ValidAdminPassword);

                request.Content = new ObjectContent <ShipmentUpdateRequestModel>(
                    shipmentRequestModel, new JsonMediaTypeFormatter());

                var httpError = await IntegrationTestHelper.
                                GetResponseMessageBodyAsync <HttpError>(
                    config, request, HttpStatusCode.BadRequest);

                var modelState             = (HttpError)httpError["ModelState"];
                var priceError             = modelState["requestModel.Price"] as string[];
                var receiverNameError      = modelState["requestModel.ReceiverName"] as string[];
                var receiverSurnameError   = modelState["requestModel.ReceiverSurname"] as string[];
                var receiverAddressError   = modelState["requestModel.ReceiverAddress"] as string[];
                var receiverCityError      = modelState["requestModel.ReceiverCity"] as string[];
                var receiverCountryError   = modelState["requestModel.ReceiverCountry"] as string[];
                var receiverTelephoneError = modelState["requestModel.ReceiverTelephone"] as string[];
                var receiverEmailError     = modelState["requestModel.ReceiverEmail"] as string[];
                var receiverZipCodeError   = modelState["requestModel.ReceiverZipCode"] as string[];

                // Assert
                Assert.NotNull(priceError);
                Assert.NotNull(receiverNameError);
                Assert.NotNull(receiverSurnameError);
                Assert.NotNull(receiverAddressError);
                Assert.NotNull(receiverCityError);
                Assert.NotNull(receiverCountryError);
                Assert.NotNull(receiverTelephoneError);
                Assert.NotNull(receiverEmailError);
                Assert.NotNull(receiverZipCodeError);
            }
            Returns_200_And_Updated_Shipment_If_Request_Authorized_And_Request_Is_Valid()
            {
                // Arrange
                Guid[] keys = new[] {
                    Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid()
                };
                var requestKey      = keys[1];
                var shipments       = GetDummyShipments(keys);
                var shipmentSrvMock = GetShipmentSrvMock(shipments);
                var config          = IntegrationTestHelper
                                      .GetInitialIntegrationTestConfig(
                    GetContainerThroughMock(shipmentSrvMock));

                var shipmentRequestModel = new ShipmentUpdateRequestModel {
                    Price             = 12.23M,
                    ReceiverName      = "Receiver 1 Name",
                    ReceiverSurname   = "Receiver 1 Surname",
                    ReceiverAddress   = "Receiver 1 Address",
                    ReceiverCity      = "Receiver 1 City",
                    ReceiverCountry   = "Receiver 1 Country",
                    ReceiverTelephone = "Receiver 1 Country",
                    ReceiverZipCode   = "12345",
                    ReceiverEmail     = "*****@*****.**"
                };

                var request = HttpRequestMessageHelper
                              .ConstructRequest(
                    httpMethod: HttpMethod.Put,
                    uri: string.Format(
                        "https://localhost/{0}/{1}",
                        ApiBaseRequestPath, requestKey),
                    mediaType: "application/json",
                    username: Constants.ValidAdminUserName,
                    password: Constants.ValidAdminPassword);

                request.Content = new ObjectContent <ShipmentUpdateRequestModel>(
                    shipmentRequestModel, new JsonMediaTypeFormatter());

                var shipmentDto = await IntegrationTestHelper.
                                  GetResponseMessageBodyAsync <ShipmentDto>(
                    config, request, HttpStatusCode.OK);

                // Assert
                shipmentSrvMock.Verify(
                    ss => ss.UpdateShipment(
                        It.IsAny <Shipment>()), Times.Once()
                    );

                Assert.Equal(requestKey, shipmentDto.Key);
                Assert.Equal(shipmentRequestModel.Price, shipmentDto.Price);
                Assert.Equal(shipmentRequestModel.ReceiverEmail, shipmentDto.ReceiverEmail);
            }
        public ShipmentDto PutShipment(Guid key, ShipmentUpdateRequestModel requestModel) {

            var shipment = _shipmentService.GetShipment(key);
            if (shipment == null) {

                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var updatedShipment = _shipmentService.UpdateShipment(
                requestModel.ToShipment(shipment));

            return updatedShipment.ToShipmentDto();
        }
        public ShipmentDto PutShipment(Guid key, ShipmentUpdateRequestModel requestModel)
        {
            var shipment = _shipmentService.GetShipment(key);

            if (shipment == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var updatedShipment = _shipmentService.UpdateShipment(
                requestModel.ToShipment(shipment));

            return(updatedShipment.ToShipmentDto());
        }
        internal static Shipment ToShipment(
            this ShipmentUpdateRequestModel requestModel, Shipment existingShipment)
        {
            existingShipment.Price             = requestModel.Price.Value;
            existingShipment.ReceiverName      = requestModel.ReceiverName;
            existingShipment.ReceiverSurname   = requestModel.ReceiverSurname;
            existingShipment.ReceiverAddress   = requestModel.ReceiverAddress;
            existingShipment.ReceiverZipCode   = requestModel.ReceiverZipCode;
            existingShipment.ReceiverCity      = requestModel.ReceiverCity;
            existingShipment.ReceiverCountry   = requestModel.ReceiverCountry;
            existingShipment.ReceiverTelephone = requestModel.ReceiverTelephone;
            existingShipment.ReceiverEmail     = requestModel.ReceiverEmail;

            return(existingShipment);
        }
            Returns_404_If_Request_Authorized_But_Shipment_Does_Not_Exist()
            {
                // Arrange
                Guid[] keys = new[] {
                    Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid()
                };
                var requestKey = Guid.NewGuid();
                var config     = IntegrationTestHelper
                                 .GetInitialIntegrationTestConfig(GetContainer(keys));

                var shipmentRequestModel = new ShipmentUpdateRequestModel {
                    Price             = 12.23M,
                    ReceiverName      = "Receiver 1 Name",
                    ReceiverSurname   = "Receiver 1 Surname",
                    ReceiverAddress   = "Receiver 1 Address",
                    ReceiverCity      = "Receiver 1 City",
                    ReceiverCountry   = "Receiver 1 Country",
                    ReceiverTelephone = "Receiver 1 Country",
                    ReceiverZipCode   = "12345",
                    ReceiverEmail     = "*****@*****.**"
                };

                var request = HttpRequestMessageHelper
                              .ConstructRequest(
                    httpMethod: HttpMethod.Put,
                    uri: string.Format(
                        "https://localhost/{0}/{1}",
                        ApiBaseRequestPath, requestKey),
                    mediaType: "application/json",
                    username: Constants.ValidAdminUserName,
                    password: Constants.ValidAdminPassword);

                request.Content = new ObjectContent <ShipmentUpdateRequestModel>(
                    shipmentRequestModel, new JsonMediaTypeFormatter());

                // Act
                var response = await IntegrationTestHelper
                               .GetResponseAsync(config, request);

                // Assert
                Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
            }
                Returns_200_And_Updated_Shipment_If_Request_Authorized_And_Request_Is_Valid()
            {

                // Arrange
                Guid[] keys = new[] { 
                    Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid()
                };
                var requestKey = keys[1];
                var shipments = GetDummyShipments(keys);
                var shipmentSrvMock = GetShipmentSrvMock(shipments);
                var config = IntegrationTestHelper
                    .GetInitialIntegrationTestConfig(
                        GetContainerThroughMock(shipmentSrvMock));

                var shipmentRequestModel = new ShipmentUpdateRequestModel
                {
                    Price = 12.23M,
                    ReceiverName = "Receiver 1 Name",
                    ReceiverSurname = "Receiver 1 Surname",
                    ReceiverAddress = "Receiver 1 Address",
                    ReceiverCity = "Receiver 1 City",
                    ReceiverCountry = "Receiver 1 Country",
                    ReceiverTelephone = "Receiver 1 Country",
                    ReceiverZipCode = "12345",
                    ReceiverEmail = "*****@*****.**"
                };

                var request = HttpRequestMessageHelper
                    .ConstructRequest(
                        httpMethod: HttpMethod.Put,
                        uri: string.Format(
                            "https://localhost/{0}/{1}",
                            ApiBaseRequestPath, requestKey),
                        mediaType: "application/json",
                        username: Constants.ValidAdminUserName,
                        password: Constants.ValidAdminPassword);

                request.Content = new ObjectContent<ShipmentUpdateRequestModel>(
                    shipmentRequestModel, new JsonMediaTypeFormatter());

                var shipmentDto = await IntegrationTestHelper.
                    GetResponseMessageBodyAsync<ShipmentDto>(
                        config, request, HttpStatusCode.OK);

                // Assert
                shipmentSrvMock.Verify(
                    ss => ss.UpdateShipment(
                        It.IsAny<Shipment>()), Times.Once()
                );

                Assert.Equal(requestKey, shipmentDto.Key);
                Assert.Equal(shipmentRequestModel.Price, shipmentDto.Price);
                Assert.Equal(shipmentRequestModel.ReceiverEmail, shipmentDto.ReceiverEmail);
            }
                Returns_400_If_Request_Shipment_But_Invalid()
            {

                // Arrange
                Guid[] keys = new[] { 
                    Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid()
                };
                var requestKey = Guid.NewGuid();
                var config = IntegrationTestHelper
                    .GetInitialIntegrationTestConfig(GetContainer(keys));

                var shipmentRequestModel = new ShipmentUpdateRequestModel
                {
                    ReceiverName = "ANameWhichIsMoreThan50CharsANameWhichIsMoreThan50Chars",
                    ReceiverSurname = "ASurnameWhichIsMoreThan50CharsASurnameWhichIsMoreThan50Chars",
                    ReceiverAddress = "AnAddressWhichIsMoreThan50CharsAnAddressWhichIsMoreThan50Chars",
                    ReceiverCity = "ACityWhichIsMoreThan50CharsACityWhichIsMoreThan50Chars",
                    ReceiverTelephone = "ATelephoneWhichIsMoreThan50CharsATelephoneWhichIsMoreThan50Chars",
                    ReceiverEmail = "fooexample.com"
                };

                var request = HttpRequestMessageHelper
                    .ConstructRequest(
                        httpMethod: HttpMethod.Put,
                        uri: string.Format(
                            "https://localhost/{0}/{1}",
                            ApiBaseRequestPath, requestKey),
                        mediaType: "application/json",
                        username: Constants.ValidAdminUserName,
                        password: Constants.ValidAdminPassword);

                request.Content = new ObjectContent<ShipmentUpdateRequestModel>(
                    shipmentRequestModel, new JsonMediaTypeFormatter());

                var httpError = await IntegrationTestHelper.
                    GetResponseMessageBodyAsync<HttpError>(
                        config, request, HttpStatusCode.BadRequest);

                var modelState = (HttpError)httpError["ModelState"];
                var priceError = modelState["requestModel.Price"] as string[];
                var receiverNameError = modelState["requestModel.ReceiverName"] as string[];
                var receiverSurnameError = modelState["requestModel.ReceiverSurname"] as string[];
                var receiverAddressError = modelState["requestModel.ReceiverAddress"] as string[];
                var receiverCityError = modelState["requestModel.ReceiverCity"] as string[];
                var receiverCountryError = modelState["requestModel.ReceiverCountry"] as string[];
                var receiverTelephoneError = modelState["requestModel.ReceiverTelephone"] as string[];
                var receiverEmailError = modelState["requestModel.ReceiverEmail"] as string[];
                var receiverZipCodeError = modelState["requestModel.ReceiverZipCode"] as string[];

                // Assert
                Assert.NotNull(priceError);
                Assert.NotNull(receiverNameError);
                Assert.NotNull(receiverSurnameError);
                Assert.NotNull(receiverAddressError);
                Assert.NotNull(receiverCityError);
                Assert.NotNull(receiverCountryError);
                Assert.NotNull(receiverTelephoneError);
                Assert.NotNull(receiverEmailError);
                Assert.NotNull(receiverZipCodeError);
            }
                Returns_404_If_Request_Authorized_But_Shipment_Does_Not_Exist()
            {

                // Arrange
                Guid[] keys = new[] { 
                    Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid()
                };
                var requestKey = Guid.NewGuid();
                var config = IntegrationTestHelper
                    .GetInitialIntegrationTestConfig(GetContainer(keys));

                var shipmentRequestModel = new ShipmentUpdateRequestModel
                {
                    Price = 12.23M,
                    ReceiverName = "Receiver 1 Name",
                    ReceiverSurname = "Receiver 1 Surname",
                    ReceiverAddress = "Receiver 1 Address",
                    ReceiverCity = "Receiver 1 City",
                    ReceiverCountry = "Receiver 1 Country",
                    ReceiverTelephone = "Receiver 1 Country",
                    ReceiverZipCode = "12345",
                    ReceiverEmail = "*****@*****.**"
                };

                var request = HttpRequestMessageHelper
                    .ConstructRequest(
                        httpMethod: HttpMethod.Put,
                        uri: string.Format(
                            "https://localhost/{0}/{1}",
                            ApiBaseRequestPath, requestKey),
                        mediaType: "application/json",
                        username: Constants.ValidAdminUserName,
                        password: Constants.ValidAdminPassword);

                request.Content = new ObjectContent<ShipmentUpdateRequestModel>(
                    shipmentRequestModel, new JsonMediaTypeFormatter());

                // Act
                var response = await IntegrationTestHelper
                    .GetResponseAsync(config, request);

                // Assert
                Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
            }