Skip to content

Commit

Permalink
Add delete test
Browse files Browse the repository at this point in the history
  • Loading branch information
shygnome committed Dec 5, 2022
1 parent 917cf4a commit f3480d3
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 7 deletions.
12 changes: 12 additions & 0 deletions application/src/main/java/id/worx/worx/common/FormConstants.java
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ResponseEntity<Page<FormTemplateDTO>> 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
Expand Down Expand Up @@ -100,6 +100,10 @@ public ResponseEntity<BaseListResponse<FormTemplateDTO>> 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<BaseResponse> delete(@RequestBody @Valid MultipleDeleteRequest request) {
templateService.delete(request.getIds());
Expand Down Expand Up @@ -131,7 +135,7 @@ public ResponseEntity<BaseValueResponse<FormTemplateDTO>> 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}")
Expand All @@ -147,8 +151,10 @@ public ResponseEntity<BaseValueResponse<FormTemplateDTO>> 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<BaseResponse> delete(@PathVariable("id") Long id) {
Expand All @@ -173,7 +179,6 @@ public ResponseEntity<BaseValueResponse<FormTemplateDTO>> assignGroup(@PathVaria
@PostMapping("{id}/share")
public ResponseEntity<BaseResponse> shareFormToEmail(@PathVariable("id") Long id,
@RequestBody @Valid FormShareRequest request) {

FormTemplate template = templateService.read(id);
templateService.share(template, request.getRecipients());
return ResponseEntity.status(HttpStatus.OK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ protected <T> ResultActions doPut(String urlTemplate, T content, String... param
return mockMvc.perform(postRequest);
}

protected <T> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 {
Expand Down Expand Up @@ -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<Field> 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<FormTemplateDTO> response = doPostWithTypedResponse(
"/form/template",
request,
new TypeReference<BaseValueResponse<FormTemplateDTO>>() {
},
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<FormTemplateDTO> response = doGetTyped("/form/template",
new TypeReference<BaseListResponse<FormTemplateDTO>>() {
});
List<FormTemplateDTO> 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<Field> 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<FormTemplateDTO> response = doPostWithTypedResponse(
"/form/template",
request,
new TypeReference<BaseValueResponse<FormTemplateDTO>>() {
},
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<BaseValueResponse<FormTemplateDTO>>() {
},
status().isCreated());
template = response.getValue();
Long secondTemplateId = template.getId();

MultipleDeleteRequest deleteRequest = new MultipleDeleteRequest(List.of(firstTemplateId, secondTemplateId));
doDelete("/form/template", deleteRequest)
.andExpect(status().isNoContent());

}

}

0 comments on commit f3480d3

Please sign in to comment.