From f3480d3be1fa672967a63ca10fecde37663efea9 Mon Sep 17 00:00:00 2001 From: shygnome Date: Mon, 5 Dec 2022 10:49:09 +0700 Subject: [PATCH] Add delete test --- .../id/worx/worx/common/FormConstants.java | 12 +++ .../common/model/forms/field/PhotoField.java | 5 +- .../controller/FormTemplateController.java | 13 ++- .../worx/web/controller/AbstractWebTest.java | 8 ++ .../FormTemplateControllerTest.java | 102 ++++++++++++++++++ 5 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 application/src/main/java/id/worx/worx/common/FormConstants.java diff --git a/application/src/main/java/id/worx/worx/common/FormConstants.java b/application/src/main/java/id/worx/worx/common/FormConstants.java new file mode 100644 index 00000000..7c546148 --- /dev/null +++ b/application/src/main/java/id/worx/worx/common/FormConstants.java @@ -0,0 +1,12 @@ +package id.worx.worx.common; + +public class FormConstants { + + @Generated + private FormConstants() { + throw new IllegalStateException("Utility class"); + } + + public static final int PHOTO_FIELD_MAXIMUM_ALLOWED_MAX_FILES = 6; + +} diff --git a/application/src/main/java/id/worx/worx/common/model/forms/field/PhotoField.java b/application/src/main/java/id/worx/worx/common/model/forms/field/PhotoField.java index db11f7a2..7bae303a 100644 --- a/application/src/main/java/id/worx/worx/common/model/forms/field/PhotoField.java +++ b/application/src/main/java/id/worx/worx/common/model/forms/field/PhotoField.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; +import id.worx.worx.common.FormConstants; import id.worx.worx.common.exception.FormValidationErrorDetail; import id.worx.worx.common.exception.FormValidationReason; import id.worx.worx.common.exception.InvalidParameterException; @@ -20,8 +21,6 @@ public class PhotoField extends Field { private static final long serialVersionUID = 7050599105120337475L; - private static final int MAXIMUM_ALLOWED_MAX_FILES = 6; - @JsonProperty("max_files") private Integer maxFiles; @JsonProperty("allow_gallery_upload") @@ -32,7 +31,7 @@ public PhotoField(String id, String label, String description, Boolean required, Boolean allowGalleryUpload) { super(id, label, description, FieldType.PHOTO, required); - if (maxFiles > MAXIMUM_ALLOWED_MAX_FILES) { + if (maxFiles > FormConstants.PHOTO_FIELD_MAXIMUM_ALLOWED_MAX_FILES) { throw new InvalidParameterException("Maximum number of photos to attach is up to 6"); } diff --git a/application/src/main/java/id/worx/worx/web/controller/FormTemplateController.java b/application/src/main/java/id/worx/worx/web/controller/FormTemplateController.java index dd945624..e7ed9308 100644 --- a/application/src/main/java/id/worx/worx/web/controller/FormTemplateController.java +++ b/application/src/main/java/id/worx/worx/web/controller/FormTemplateController.java @@ -72,7 +72,7 @@ public ResponseEntity> search( @Operation(summary = "Create a new Form Template") @ApiResponses(value = { - @ApiResponse(responseCode = "201", description = "Form Template is created."), + @ApiResponse(responseCode = "201", description = "Form Template is created"), @ApiResponse(responseCode = "400", description = "Invalid FormTemplateRequest") }) @PostMapping @@ -100,6 +100,10 @@ public ResponseEntity> list() { .body(response); } + @Operation(summary = "Delete Form Template(s) by id(s)") + @ApiResponses(value = { + @ApiResponse(responseCode = "204", description = "Form Template(s) is/are deleted") + }) @DeleteMapping public ResponseEntity delete(@RequestBody @Valid MultipleDeleteRequest request) { templateService.delete(request.getIds()); @@ -131,7 +135,7 @@ public ResponseEntity> read(@RequestParam Str @Operation(summary = "Update a Form Template by id") @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "Form Template update is success."), + @ApiResponse(responseCode = "200", description = "Form Template update is success"), @ApiResponse(responseCode = "404", description = "Form Template not found") }) @PutMapping("{id}") @@ -147,8 +151,10 @@ public ResponseEntity> update( .body(response); } + @Operation(summary = "Delete a Form Template by id") @ApiResponses(value = { - @ApiResponse(responseCode = "204", description = "Form Template is deleted.") + @ApiResponse(responseCode = "204", description = "Form Template is deleted"), + @ApiResponse(responseCode = "404", description = "Form Template not found") }) @DeleteMapping("{id}") public ResponseEntity delete(@PathVariable("id") Long id) { @@ -173,7 +179,6 @@ public ResponseEntity> assignGroup(@PathVaria @PostMapping("{id}/share") public ResponseEntity shareFormToEmail(@PathVariable("id") Long id, @RequestBody @Valid FormShareRequest request) { - FormTemplate template = templateService.read(id); templateService.share(template, request.getRecipients()); return ResponseEntity.status(HttpStatus.OK) diff --git a/application/src/test/java/id/worx/worx/web/controller/AbstractWebTest.java b/application/src/test/java/id/worx/worx/web/controller/AbstractWebTest.java index b3886e76..390feb0b 100644 --- a/application/src/test/java/id/worx/worx/web/controller/AbstractWebTest.java +++ b/application/src/test/java/id/worx/worx/web/controller/AbstractWebTest.java @@ -250,6 +250,14 @@ protected ResultActions doPut(String urlTemplate, T content, String... param return mockMvc.perform(postRequest); } + protected ResultActions doDelete(String urlTemplate, T content, String... params) throws Exception { + MockHttpServletRequestBuilder deleteRequest = delete(urlTemplate, (Object[]) params); + setJwtToken(deleteRequest); + String json = json(content); + deleteRequest.contentType(contentType).content(json); + return mockMvc.perform(deleteRequest); + } + protected ResultActions doDelete(String urlTemplate, String... params) throws Exception { MockHttpServletRequestBuilder deleteRequest = delete(urlTemplate); setJwtToken(deleteRequest); diff --git a/application/src/test/java/id/worx/worx/web/controller/FormTemplateControllerTest.java b/application/src/test/java/id/worx/worx/web/controller/FormTemplateControllerTest.java index d8147681..027c457a 100644 --- a/application/src/test/java/id/worx/worx/web/controller/FormTemplateControllerTest.java +++ b/application/src/test/java/id/worx/worx/web/controller/FormTemplateControllerTest.java @@ -1,5 +1,6 @@ package id.worx.worx.web.controller; +import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -11,14 +12,24 @@ import com.fasterxml.jackson.core.type.TypeReference; +import id.worx.worx.common.FormConstants; import id.worx.worx.common.model.dto.FormTemplateDTO; import id.worx.worx.common.model.forms.field.CheckboxGroupField; +import id.worx.worx.common.model.forms.field.DateField; import id.worx.worx.common.model.forms.field.DropdownField; import id.worx.worx.common.model.forms.field.Field; +import id.worx.worx.common.model.forms.field.FileField; import id.worx.worx.common.model.forms.field.Option; +import id.worx.worx.common.model.forms.field.PhotoField; +import id.worx.worx.common.model.forms.field.RadioGroupField; +import id.worx.worx.common.model.forms.field.RatingField; +import id.worx.worx.common.model.forms.field.SignatureField; import id.worx.worx.common.model.forms.field.TextField; import id.worx.worx.common.model.request.FormTemplateRequest; +import id.worx.worx.common.model.request.MultipleDeleteRequest; +import id.worx.worx.common.model.response.BaseListResponse; import id.worx.worx.common.model.response.BaseValueResponse; +import id.worx.worx.exception.WorxErrorCode; @ContextConfiguration(classes = { FormTemplateControllerTest.Config.class }) class FormTemplateControllerTest extends AbstractControllerTest { @@ -94,4 +105,95 @@ void givenFormTemplateRequest_whenUpdate_thenReturn() throws Exception { } + @Test + void givenId_whenDelete_thenReturn() throws Exception { + String label = "Monthly Form #3"; + String description = "Monthly Form Number 3"; + List fields = new ArrayList<>(); + fields.add(new RadioGroupField("radiogroup-field-id", "Radio Group Field 1", "description", false, + List.of(new Option("Option 1"), new Option("Option 2")))); + fields.add(new DateField("date-field-id", "Date Field 1", "description", false, true, true)); + Boolean submitInZone = false; + Boolean isDefaultForm = false; + FormTemplateRequest request = new FormTemplateRequest(label, description, fields, submitInZone, isDefaultForm); + BaseValueResponse response = doPostWithTypedResponse( + "/form/template", + request, + new TypeReference>() { + }, + status().isCreated()); + FormTemplateDTO template = response.getValue(); + assertEquals(label, template.getLabel()); + assertEquals(description, template.getDescription()); + assertEquals(fields.size(), template.getFields().size()); + + Long templateId = template.getId(); + doDelete("/form/template/" + templateId) + .andExpect(status().isNoContent()); + + doGet("/form/template/" + templateId) + .andExpect(status().isNotFound()) + .andExpect(statusReason(containsString(WorxErrorCode.ENTITY_NOT_FOUND_ERROR.getReasonPhrase()))); + } + + @Test + void givenInvalidId_whenDelete_thenThrowWorxException() throws Exception { + BaseListResponse response = doGetTyped("/form/template", + new TypeReference>() { + }); + List list = response.getList(); + + Long invalidId = 1L; + if (!list.isEmpty()) { + invalidId = list.stream().map(FormTemplateDTO::getId).max((o1, o2) -> Long.compare(o1, o2)).get() + 1L; + } + + doDelete("/form/template/" + invalidId) + .andExpect(status().isNotFound()) + .andExpect(statusReason(containsString(WorxErrorCode.ENTITY_NOT_FOUND_ERROR.getReasonPhrase()))); + } + + @Test + void givenIds_whenDelete_thenReturn() throws Exception { + String label = "Monthly Form #4"; + String description = "Monthly Form Number 4"; + List fields = new ArrayList<>(); + fields.add(new RatingField("rating-field-id", "Rating Field 1", "description", false, 10)); + fields.add(new FileField("file-field-id", "File Field 1", "description", false, 2, 20, 10, List.of())); + Boolean submitInZone = false; + Boolean isDefaultForm = false; + FormTemplateRequest request = new FormTemplateRequest(label, description, fields, submitInZone, isDefaultForm); + BaseValueResponse response = doPostWithTypedResponse( + "/form/template", + request, + new TypeReference>() { + }, + status().isCreated()); + FormTemplateDTO template = response.getValue(); + Long firstTemplateId = template.getId(); + + label = "Monthly Form #5"; + description = "Monthly Form Number 5"; + fields = new ArrayList<>(); + fields.add(new PhotoField("photo-field-id", "Photo Field 1", "description", false, + FormConstants.PHOTO_FIELD_MAXIMUM_ALLOWED_MAX_FILES, true)); + fields.add(new SignatureField("signature-field-id", "Signature Field 1", "description", false)); + submitInZone = false; + isDefaultForm = false; + request = new FormTemplateRequest(label, description, fields, submitInZone, isDefaultForm); + response = doPostWithTypedResponse( + "/form/template", + request, + new TypeReference>() { + }, + status().isCreated()); + template = response.getValue(); + Long secondTemplateId = template.getId(); + + MultipleDeleteRequest deleteRequest = new MultipleDeleteRequest(List.of(firstTemplateId, secondTemplateId)); + doDelete("/form/template", deleteRequest) + .andExpect(status().isNoContent()); + + } + }