Skip to content

Commit

Permalink
Add create and update template test
Browse files Browse the repository at this point in the history
  • Loading branch information
shygnome committed Dec 1, 2022
1 parent 435899e commit b4b7feb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.List;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

import id.worx.worx.common.exception.detail.ErrorDetail;
import id.worx.worx.common.model.forms.value.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,14 @@ public ResponseEntity<BaseValueResponse<FormTemplateDTO>> read(@RequestParam Str
.body(response);
}

@Operation(summary = "Update a Form Template by id")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Form Template update is success."),
@ApiResponse(responseCode = "404", description = "Form Template not found")
})
@PutMapping("{id}")
public ResponseEntity<BaseValueResponse<FormTemplateDTO>> update(@PathVariable("id") Long id,
public ResponseEntity<BaseValueResponse<FormTemplateDTO>> update(
@PathVariable("id") Long id,
@RequestBody @Valid FormTemplateRequest request) {
FormTemplate template = templateService.update(id, request);
FormTemplateDTO dto = templateService.toDTO(template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

import id.worx.worx.common.model.request.auth.LoginRequest;
import id.worx.worx.common.model.request.users.UserRequest;
Expand All @@ -52,7 +54,7 @@
@Slf4j
public abstract class AbstractWebTest {

protected ObjectMapper mapper = new ObjectMapper();
protected ObjectMapper mapper;

protected static final String ADMIN_USER_EMAIL = "testadmin@worx.id";
protected static final String ADMIN_USER_PASSWORD = "TestAdmin123!";
Expand Down Expand Up @@ -90,6 +92,12 @@ void setConverters(HttpMessageConverter<?>[] converters) {
assertNotNull(this.mappingJackson2HttpMessageConverter, "the JSON message converter must not be null");
}

@Autowired
void setObjectMapper() {
this.mapper = new ObjectMapper();
mapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));
}

@BeforeEach
public void initWebTest() throws Exception {
log.info("Setup web test start");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import com.fasterxml.jackson.core.type.TypeReference;

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.DropdownField;
import id.worx.worx.common.model.forms.field.Field;
import id.worx.worx.common.model.forms.field.Option;
import id.worx.worx.common.model.forms.field.TextField;
import id.worx.worx.common.model.request.FormTemplateRequest;
import id.worx.worx.common.model.response.BaseValueResponse;
Expand All @@ -25,8 +28,8 @@ static class Config {

@Test
void givenFormTemplateRequest_whenCreate_thenReturn() throws Exception {
String label = "Field 2 Group";
String description = "#4287f5";
String label = "Monthly Form #1";
String description = "Monthly Form Number 1";
List<Field> fields = new ArrayList<>();
fields.add(new TextField("text-field-id", "Text Field 1", false, "description", 1, 1024));
Boolean submitInZone = false;
Expand All @@ -41,33 +44,54 @@ void givenFormTemplateRequest_whenCreate_thenReturn() throws Exception {
FormTemplateDTO actualFormTemplate = actualResponse.getValue();
assertEquals(label, actualFormTemplate.getLabel());
assertEquals(description, actualFormTemplate.getDescription());
assertEquals(description, actualFormTemplate.getDescription());
assertEquals(fields.size(), actualFormTemplate.getFields().size());
assertEquals(submitInZone, actualFormTemplate.getSubmitInZone());
assertEquals(isDefaultForm, actualFormTemplate.getIsDefaultForm());
assertEquals(1, actualFormTemplate.getAssignedGroups().size());
}

@Test
void givenInvalidFormTemplateRequest_whenCreate_thenThrowException() throws Exception {
String label = "Field 2 Group";
String description = "#4287f5";
void givenFormTemplateRequest_whenUpdate_thenReturn() throws Exception {
String label = "Monthly Form #2";
String description = "Monthly Form Number 2";
List<Field> fields = new ArrayList<>();
fields.add(new TextField("text-field-id", "Text Field 1", false, "description", 1, 1024));
Boolean submitInZone = false;
Boolean isDefaultForm = false;
FormTemplateRequest request = new FormTemplateRequest(label, description, fields, submitInZone, isDefaultForm);
BaseValueResponse<FormTemplateDTO> actualResponse = doPostWithTypedResponse(
BaseValueResponse<FormTemplateDTO> response = doPostWithTypedResponse(
"/form/template",
request,
new TypeReference<BaseValueResponse<FormTemplateDTO>>() {
},
status().isCreated());
FormTemplateDTO actualFormTemplate = actualResponse.getValue();
assertEquals(label, actualFormTemplate.getLabel());
assertEquals(description, actualFormTemplate.getDescription());
assertEquals(description, actualFormTemplate.getDescription());
assertEquals(submitInZone, actualFormTemplate.getSubmitInZone());
assertEquals(isDefaultForm, actualFormTemplate.getIsDefaultForm());
assertEquals(1, actualFormTemplate.getAssignedGroups().size());
FormTemplateDTO template = response.getValue();
assertEquals(label, template.getLabel());
assertEquals(description, template.getDescription());
assertEquals(fields.size(), template.getFields().size());

Long templateId = template.getId();
String updatedLabel = "Updated Monthly Form #2";
String updatedDescription = "Updated Monthly Form Number 2";
List<Field> updatedFields = new ArrayList<>();
updatedFields.add(new CheckboxGroupField(
"checkbox-field-id", "Checkbox Field 1", "description", true, 1, 2,
List.of(new Option("Option 1"), new Option("Option 2"))));
updatedFields.add(new DropdownField(
"dropdown-field-id", "Dropdown Field 1", "description", true,
List.of(new Option("Option 1"), new Option("Option 2"))));
FormTemplateRequest updateRequest = new FormTemplateRequest(updatedLabel, updatedDescription, updatedFields,
template.getSubmitInZone(), template.getIsDefaultForm());
BaseValueResponse<FormTemplateDTO> actualResponse = doPutWithTypedResponse(
"/form/template/" + templateId,
updateRequest,
new TypeReference<BaseValueResponse<FormTemplateDTO>>() {
});
FormTemplateDTO actualTemplate = actualResponse.getValue();
assertEquals(updatedLabel, actualTemplate.getLabel());
assertEquals(updatedDescription, actualTemplate.getDescription());
assertEquals(updatedFields.size(), actualTemplate.getFields().size());

}

}

0 comments on commit b4b7feb

Please sign in to comment.