public async Task CreateShouldSucceed()
        {
            // Arrange
            var name       = "TName";
            var code       = "TCode";
            var source     = "TSource";
            var apiVersion = "1.0";
            var body       = new CreateProductionSiteFromBody()
            {
                Source = source,
                Code   = code,
                Name   = name
            };
            var jsonBody = JsonConvert.SerializeObject(body);

            // Act
            var response = await _client.PostAsync(string.Format("api/productionsites?api-version={0}", apiVersion), new StringContent(jsonBody, Encoding.UTF8, "application/json"));

            var responseBody = await response.Content.ReadAsStringAsync();

            // Assert
            response.StatusCode.Should().Be(HttpStatusCode.Created);
            response.Headers.Location.Should().NotBeNull();
            response.Headers.ETag.Should().NotBeNull();
            responseBody.Should().BeNullOrEmpty();
        }
        public async Task <IActionResult> Create([FromQuery] CreateProductionSiteFromQuery query,
                                                 [FromBody] CreateProductionSiteFromBody body)
        {
            var request = new CreateProductionSiteRequest(query, body);

            var result = await _apiResult.Produce(request);

            return(result);
        }
        public async Task CreateShouldFailWhenCodeAndNameAndSourceAlreadyExists()
        {
            // Arrange
            string name       = "TName";
            string code       = "TCode";
            string source     = "TSource";
            var    apiVersion = "1.0";
            await ControllerHelper.CreateProductionSite(code, name, source);

            var body = new CreateProductionSiteFromBody()
            {
                Source = source,
                Code   = code,
                Name   = name
            };

            var jsonBody = JsonConvert.SerializeObject(body);

            var expectedError = new ResponseError
            {
                Error = new ResponseErrorBody
                {
                    Code    = ServiceError.ResourceAlreadyExists.Code,
                    Message = ServiceError.ResourceAlreadyExists.Message,
                    Details = new List <ResponseErrorField>
                    {
                        new ResponseErrorField
                        {
                            Code    = "conflict",
                            Message = HandlerFailures.CodeSourceConflict,
                            Target  = "code-source"
                        }
                    }
                }
            };

            // Act
            var response = await _client.PostAsync(string.Format("api/productionsites?api-version={0}", apiVersion), new StringContent(jsonBody, Encoding.UTF8, "application/json"));

            var responseBody = await response.Content.ReadAsStringAsync();

            var error = JsonConvert.DeserializeObject <ResponseError>(responseBody);

            // Assert
            response.StatusCode.Should().Be(HttpStatusCode.Conflict);
            responseBody.Should().NotBeNull();
            error.Should().Be(expectedError);
        }
        public async Task CreateShouldFailWhenNameIsNull()
        {
            // Arrange
            string name       = null;
            string code       = "TCode";
            string source     = "TSource";
            var    apiVersion = "1.0";
            var    body       = new CreateProductionSiteFromBody()
            {
                Source = source,
                Code   = code,
                Name   = name
            };
            var jsonBody = JsonConvert.SerializeObject(body);

            var expectedError = new ResponseError
            {
                Error = new ResponseErrorBody
                {
                    Code    = ServiceError.InvalidInput.Code,
                    Message = ServiceError.InvalidInput.Message,
                    Details = new List <ResponseErrorField>
                    {
                        new ResponseErrorField
                        {
                            Code    = "invalid-input",
                            Message = ValidationFailures.ProductionSiteNameMandatory,
                            Target  = "name"
                        }
                    }
                }
            };

            // Act
            var response = await _client.PostAsync(string.Format("api/productionsites?api-version={0}", apiVersion), new StringContent(jsonBody, Encoding.UTF8, "application/json"));

            var responseBody = await response.Content.ReadAsStringAsync();

            var error = JsonConvert.DeserializeObject <ResponseError>(responseBody);

            // Assert
            response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
            responseBody.Should().NotBeNull();
            error.Should().Be(expectedError);
        }
        public async Task CreateShouldFailWhenQueryApiVersionIsInvalid()
        {
            // Arrange
            var name       = "TName";
            var code       = "TCode";
            var source     = "TSource";
            var apiVersion = "4.0";
            var body       = new CreateProductionSiteFromBody()
            {
                Source = source,
                Code   = code,
                Name   = name
            };
            var jsonBody = JsonConvert.SerializeObject(body);

            var expectedError = new ResponseError
            {
                Error = new ResponseErrorBody
                {
                    Code    = ServiceError.InvalidQueryParameterValue.Code,
                    Message = ServiceError.InvalidQueryParameterValue.Message,
                    Details = new List <ResponseErrorField>
                    {
                        new ResponseErrorField
                        {
                            Code    = Consts.Failure.Detail.Code.Invalid,
                            Message = string.Format(RequestFailures.QueryParameterInvalidValue, "api-version"),
                            Target  = Consts.Failure.Detail.Target.ApiVersion
                        }
                    }
                }
            };

            // Act
            var response = await _client.PostAsync(string.Format("api/productionsites?api-version={0}", apiVersion), new StringContent(jsonBody, Encoding.UTF8, "application/json"));

            var responseBody = await response.Content.ReadAsStringAsync();

            var error = JsonConvert.DeserializeObject <ResponseError>(responseBody);

            // Assert
            response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
            responseBody.Should().NotBeNull();
            error.Should().Be(expectedError);
        }
        public static async Task <CreatedRecordResult> CreateProductionSite(string code, string name, string source)
        {
            var apiVersion = "1.0";
            var body       = new CreateProductionSiteFromBody
            {
                Source = source,
                Code   = code,
                Name   = name
            };
            var jsonBody = JsonConvert.SerializeObject(body);

            var response = await _client.PostAsync(string.Format("api/productionsites?api-version={0}", apiVersion), new StringContent(jsonBody, Encoding.UTF8, "application/json"));

            var id     = response.Headers.Location.GetId();
            var eTag   = response.Headers.ETag.Tag;
            var result = new CreatedRecordResult {
                Id = id, ETag = eTag
            };

            return(result);
        }
        public async Task CreateShouldFailWhenBodyIsNonJsonContentType()
        {
            // Arrange
            var name       = "TName";
            var code       = "TCode";
            var source     = "TSource";
            var apiVersion = "1.0";
            var body       = new CreateProductionSiteFromBody()
            {
                Source = source,
                Code   = code,
                Name   = name
            };
            var jsonBody = JsonConvert.SerializeObject(body);

            // Act
            var response = await _client.PostAsync(string.Format("api/productionsites?api-version={0}", apiVersion), new StringContent(jsonBody, Encoding.UTF8, "text/plain"));

            var responseBody = await response.Content.ReadAsStringAsync();

            // Assert
            response.StatusCode.Should().Be(HttpStatusCode.UnsupportedMediaType);
            responseBody.Should().BeNullOrEmpty();
        }
 public CreateProductionSiteRequest(CreateProductionSiteFromQuery query, CreateProductionSiteFromBody body)
 {
     _query = query;
     _body  = body;
 }