public async Task WhenTheMockupAPIIsCalled()
        {
            var mockup = new Mockup
            {
                design  = design,
                product = new MockupProduct
                {
                    id    = AvailableProducts.gildan_cotton_t_shirt,
                    color = "black"
                },
                template = new Template
                {
                    name       = $"Template created at {DateTime.Now}",
                    background = AvailableBackgrounds.brick
                },
                output = new MockupOutput
                {
                    width   = 11,
                    height  = 8.5f,
                    padding = 8,
                    format  = MockupOutputFormats.jpg
                }
            };

            response = await apiClient.MockupAPI.CreateMockupAsync(mockup).ConfigureAwait(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a new mockup response to the database.
        /// </summary>
        /// <param name="response">The new response.</param>
        /// <param name="transactionId">The identifier for the current transaction. For logging purpose.</param>
        /// <returns>The new mockup response with the matching id.</returns>
        /// <exception cref="WebApiMockException">Thrown with error code #3 if the id of the given response is not 0 (zero).</exception>
        /// <exception cref="WebApiMockException">Thrown with error code #4 if a response with the same values already exists.</exception>
        public static MockupResponse AddResponse(MockupResponse response, Guid?transactionId = null)
        {
            var logId = Guid.Empty;

            if (transactionId.HasValue)
            {
                logId = transactionId.Value;
            }
            if (logId.Equals(Guid.Empty))
            {
                logId = Guid.NewGuid();
            }
            if (response.Id != 0)
            {
                Program.Logger.Error($"[{logId}] The id for a new response must be 0.");
                throw new WebApiMockException("The id for a new response must be 0.", 3);
            }
            if (ResponseExists(response.StatusCode, response.Response, response.MimeType, logId))
            {
                Program.Logger.Error($"[{logId}] The response already exists.");
                throw new WebApiMockException("The response already exists.", 4);
            }
            var newData = new ResponseDefinition {
                StatusCode = response.StatusCode,
                Response   = response.Response,
                MimeType   = response.MimeType
            };

            using var ctx = new DataContext();
            ctx.Responses.Add(newData);
            ctx.SaveChanges(true);
            Program.Logger.Info($"[{logId}] Successfully created response (HTTP {response.StatusCode}) with id #{newData.Id}.");
            return(newData.ToMockupResponse());
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <MockupResponse> > Put([FromBody] MockupResponse response)
        {
            Program.Logger.Info($"Executing ResponseController.Put({response})");
            if (response.Response == null)
            {
                response.Response = "";
            }
            if (response.MimeType == null)
            {
                response.MimeType = "";
            }
            if (response.Id > 0)
            {
                Program.Logger.Error("The new response definition does not have an empty id.");
                return(StatusCode(409, "The new response definition does not have an empty id."));
            }
            if (DataService.ResponseExists(response.StatusCode, response.Response, response.MimeType))
            {
                Program.Logger.Error("A response with the given values already exists.");
                return(BadRequest("A response with the given values already exists."));
            }
            var retVal = DataService.AddResponse(response);

            Program.Logger.Info($"Successfully created new reponse with id #{retVal.Id}.");
            return(Ok(retVal));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Converts a <see cref="MockupResponse"/> to a <see cref="ResponseDefinition"/>.
        /// </summary>
        /// <param name="response">The response to convert.</param>
        /// <returns>The converted instance.</returns>
        public static ResponseDefinition ToResponseDefinition(this MockupResponse response)
        {
            if (response.Id == 0)
            {
                return(new ResponseDefinition {
                    Id = 0,
                    StatusCode = response.StatusCode,
                    MimeType = response.MimeType,
                    Response = response.Response
                });
            }
            using var ctx = new DataContext();
            if (!ctx.Responses.Any(r => r.Id.Equals(response.Id)))
            {
                return(null);
            }
            var retVal = ctx.Responses.Single(r => r.Id.Equals(response.Id));

            return(retVal);
        }
Ejemplo n.º 5
0
        public async Task <ActionResult <MockupResponse> > Patch(int id, [FromBody] MockupResponse response)
        {
            MockupResponse existingResponse;

            Program.Logger.Info($"Executing ResponseController.Patch({id}, {response})");
            if (response.Id < 1)
            {
                Program.Logger.Error("The response definition has an empty id.");
                return(StatusCode(409, "The response definition has an empty id."));
            }
            if (!response.Id.Equals(id))
            {
                Program.Logger.Error("Ids mismatch.");
                return(StatusCode(406, "Ids mismatch."));
            }
            if (!DataService.ResponseExistsForId(response.Id))
            {
                Program.Logger.Error($"No response definition found with id #{id}.");
                return(NotFound($"No response definition found with id #{id}."));
            }
            existingResponse = DataService.GetResponseById(id);
            if (existingResponse.StatusCode != response.StatusCode)
            {
                Program.Logger.Debug("Updating HTTP status code.");
                DataService.SetResponseStatusCode(id, response.StatusCode);
            }
            if (!existingResponse.Response.Equals(response.Response))
            {
                Program.Logger.Debug("Updating response content.");
                DataService.SetResponseResponse(id, response.Response);
            }
            if (!existingResponse.MimeType.Equals(response.MimeType))
            {
                Program.Logger.Debug("Updating MIME type.");
                DataService.SetResponseMimeType(id, response.MimeType);
            }
            Program.Logger.Info($"Successfully updated response #{id}.");
            return(Ok(response));
        }