Shubham | Add. Create dynamic fields

This commit is contained in:
Kuamr Shubham
2020-04-14 16:25:37 +05:30
parent 394f6bec5d
commit 87f98d2084
7 changed files with 171 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
package com.DeploymentPortal.api.dynamicField;
import com.fasterxml.jackson.databind.JsonNode;
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import javax.persistence.*;
@Entity
@TypeDef(
name = "jsonb",
typeClass = JsonBinaryType.class,
defaultForType = JsonNode.class
)
public class DynamicField {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private JsonNode fieldInfo;
public DynamicField() {
}
public DynamicField(JsonNode fieldInfo) {
this.fieldInfo = fieldInfo;
}
public Long getId() {
return id;
}
public JsonNode getFieldInfo() {
return fieldInfo;
}
}

View File

@@ -0,0 +1,28 @@
package com.DeploymentPortal.api.dynamicField;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
@RestController
@CrossOrigin
@RequestMapping("/dynamicFields")
public class DynamicFieldController {
@Autowired
private DynamicFieldService dynamicFieldService;
public DynamicFieldController(DynamicFieldService dynamicFieldService) {
this.dynamicFieldService = dynamicFieldService;
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
DynamicField create(@Valid @RequestBody JsonNode jsonNode) {
DynamicField dynamicField = new DynamicField(jsonNode);
return dynamicFieldService.add(dynamicField);
}
}

View File

@@ -0,0 +1,8 @@
package com.DeploymentPortal.api.dynamicField;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface DynamicFieldRepository extends JpaRepository<DynamicField, Long> {
}

View File

@@ -0,0 +1,16 @@
package com.DeploymentPortal.api.dynamicField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DynamicFieldService {
@Autowired
private DynamicFieldRepository dynamicFieldRepository;
public DynamicField add(DynamicField dynamicField) {
DynamicField savedDynamicField = dynamicFieldRepository.save(dynamicField);
return savedDynamicField;
}
}

View File

@@ -0,0 +1,43 @@
package com.DeploymentPortal.api.dynamicField;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import java.io.File;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(DynamicFieldController.class)
@WithMockUser()
public class DynamicFieldControllerTest {
@Autowired
MockMvc mockMvc;
@MockBean private DynamicFieldService dynamicFieldService;
private static final String BASE_PATH = "/dynamicFields";
@Test
public void expectToAddDynamicField() throws Exception {
JsonNode jsonNode = JsonLoader.fromFile(new File("src/test/java/com/DeploymentPortal/api/dynamicField/sampleDynamicField.json"));
System.out.println(jsonNode.toString());
System.out.println(String.valueOf(jsonNode));
mockMvc.perform(post(BASE_PATH)
.content(String.valueOf(jsonNode))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andExpect(content().string(jsonNode.toString() ));
}
}

View File

@@ -0,0 +1,30 @@
package com.DeploymentPortal.api.dynamicField;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.File;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest
public class DynamicFieldServiceTest {
@Autowired
private DynamicFieldService dynamicFieldService;
@Test
public void expectToSaveDynamicField() throws IOException {
JsonNode jsonNode = JsonLoader.fromFile(new File("src/test/java/com/DeploymentPortal/api/dynamicField/sampleDynamicField.json"));
DynamicField dynamicField = new DynamicField(jsonNode);
DynamicField savedDynamicField = dynamicFieldService.add(dynamicField);
assertEquals(jsonNode, savedDynamicField.getFieldInfo());
}
}

View File

@@ -0,0 +1,7 @@
{
"name": "initialValues.new",
"inputType": "DropDown",
"placeholder": "Enter New Field",
"required": true,
"defaultValue": "New Data"
}