Shubham, Ankit | Refactor. Reduce duplicacy
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
package com.DeploymentPortal.api.Manifest;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
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
|
||||
)
|
||||
@@ -19,6 +20,7 @@ class Manifest {
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
Long id;
|
||||
|
||||
@Type(type="jsonb")
|
||||
@Column(columnDefinition = "jsonb")
|
||||
private JsonNode data;
|
||||
|
||||
@@ -26,15 +28,9 @@ class Manifest {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
public JsonNode getData() {
|
||||
return this.data;
|
||||
}
|
||||
public void setData(JsonNode data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public Manifest() {
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ class ManifestController {
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
Optional<Manifest> fetch(@PathVariable Long id){
|
||||
Optional<Manifest> fetch(@PathVariable Long id) {
|
||||
Optional<Manifest> manifest = manifestService.fetch(id);
|
||||
return manifest;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ class ValidationUtils {
|
||||
}
|
||||
|
||||
ProcessingReport isJsonValid(JsonSchema jsonSchemaNode, JsonNode jsonNode) throws ProcessingException {
|
||||
return jsonSchemaNode.validate(jsonNode, true) ;
|
||||
return jsonSchemaNode.validate(jsonNode, true);
|
||||
}
|
||||
|
||||
boolean isJsonValid(File schemaFile, File jsonFile) throws ProcessingException, IOException {
|
||||
@@ -49,6 +49,6 @@ class ValidationUtils {
|
||||
public ProcessingReport getReport(JsonNode data) throws IOException, ProcessingException {
|
||||
File schemaFile = new File("src/main/java/com/DeploymentPortal/api/Manifest/schema/manifest.json");
|
||||
final JsonSchema schemaNode = getSchemaNode(schemaFile);
|
||||
return isJsonValid(schemaNode,data);
|
||||
return isJsonValid(schemaNode, data);
|
||||
}
|
||||
}
|
||||
@@ -19,35 +19,39 @@ class ManifestServiceTest {
|
||||
@Autowired
|
||||
private ManifestService manifestService;
|
||||
String testData = "src/test/java/com/DeploymentPortal/api/Manifest/schema/TestData/";
|
||||
|
||||
@Test
|
||||
void expectsToGetSavedInformationBackFromRepository() throws IOException, ProcessingException, ManifestDataInvalid {
|
||||
String jsonData = "{\"version\":\"v1\",\"team\":{\"name\":\"accounting\"}}";
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode data = mapper.readTree(jsonData);
|
||||
Manifest manifest = new Manifest(data);
|
||||
Manifest savedManifest = (Manifest) manifestService.add(manifest);
|
||||
JsonNode jsonNode = mapper.readTree(jsonData);
|
||||
Manifest savedManifest = getManifest(jsonNode);
|
||||
|
||||
assertEquals(data, savedManifest.getData());
|
||||
assertEquals(jsonNode, savedManifest.getData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectToSaveManifestDataWhenValid() throws IOException, ProcessingException, ManifestDataInvalid {
|
||||
File jsonFile = new File(testData + "validManifestData.json");
|
||||
final JsonNode jsonNode = JsonLoader.fromFile(jsonFile);
|
||||
Manifest manifest = new Manifest(jsonNode);
|
||||
Manifest savedManifest = (Manifest) manifestService.add(manifest);
|
||||
JsonNode jsonNode = getJsonNode();
|
||||
Manifest savedManifest = getManifest(jsonNode);
|
||||
|
||||
assertEquals(jsonNode, savedManifest.getData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectToGetSavedManifestDataById() throws IOException, ProcessingException, ManifestDataInvalid {
|
||||
File jsonFile = new File(testData + "validManifestData.json");
|
||||
final JsonNode jsonNode = JsonLoader.fromFile(jsonFile);
|
||||
Manifest manifest = new Manifest(jsonNode);
|
||||
Manifest savedManifest = (Manifest) manifestService.add(manifest);
|
||||
JsonNode jsonNode = getJsonNode();
|
||||
Manifest savedManifest = getManifest(jsonNode);
|
||||
Long id = savedManifest.getId();
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
assertEquals(jsonNode.getNodeType(), manifestService.fetch(id).get().getData().getNodeType());
|
||||
}
|
||||
|
||||
private JsonNode getJsonNode() throws IOException {
|
||||
return JsonLoader.fromFile(new File(testData + "validManifestData.json"));
|
||||
}
|
||||
|
||||
private Manifest getManifest(JsonNode jsonNode) throws IOException, ProcessingException, ManifestDataInvalid {
|
||||
return (Manifest) manifestService.add(new Manifest(jsonNode));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
package com.DeploymentPortal.api.Manifest;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ManifestTest {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void expectsManifestToSetJsonDataWithoutDeserialization() throws JsonProcessingException {
|
||||
void expectsManifestToSetJsonDataWithoutDeserialization() throws IOException {
|
||||
String jsonData = "{\"version\":\"v1\",\"team\":{\"name\":\"accounting\"}}";
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode data = mapper.readTree(jsonData);
|
||||
Manifest manifest = new Manifest(data);
|
||||
assertEquals(data,manifest.getData());
|
||||
Manifest manifest = new Manifest(mapper.readTree(jsonData));
|
||||
|
||||
assertEquals(mapper.readTree(jsonData), manifest.getData());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,177 +9,165 @@ import java.io.IOException;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class SchemaTest {
|
||||
|
||||
String schemaPath = "src/main/java/com/DeploymentPortal/api/Manifest/schema/";
|
||||
String testData = "src/test/java/com/DeploymentPortal/api/Manifest/schema/TestData/";
|
||||
String testDataPath = "src/test/java/com/DeploymentPortal/api/Manifest/schema/TestData/";
|
||||
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
|
||||
@Test
|
||||
void expectToGetTrueWhenInstanceDataIsValid() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "instance.json");
|
||||
File jsonFile = new File(testData + "validInstance.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "instance.json");
|
||||
File jsonFile = getFile(testDataPath, "validInstance.json");
|
||||
|
||||
assertTrue(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectFalseWhenCountIsNotPresentInInstance() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "instance.json");
|
||||
File jsonFile = new File(testData + "invalidInstance.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "instance.json");
|
||||
File jsonFile = getFile(testDataPath, "invalidInstance.json");
|
||||
|
||||
assertFalse(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectTrueWhenAllThePropertiesOfEnvironmentVariableAreValid() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "environmentVariable.json");
|
||||
File jsonFile = new File(testData + "validEnvironmentVariable.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "environmentVariable.json");
|
||||
File jsonFile = getFile(testDataPath, "validEnvironmentVariable.json");
|
||||
|
||||
assertTrue(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectFalseWhenSomePropertiesOfEnvironmentVariableAreMissing() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "environmentVariable.json");
|
||||
File jsonFile = new File(testData + "invalidEnvironmentVariable.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "environmentVariable.json");
|
||||
File jsonFile = getFile(testDataPath, "invalidEnvironmentVariable.json");
|
||||
|
||||
assertFalse(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectTrueWhenAllThePropertiesOfExposedPortsAreValid() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "exposedPorts.json");
|
||||
File jsonFile = new File(testData + "validExposedPorts.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "exposedPorts.json");
|
||||
File jsonFile = getFile(testDataPath, "validExposedPorts.json");
|
||||
|
||||
assertTrue(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectFalseWhenSomePropertiesOfExposedPortsAreMissing() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "environmentVariable.json");
|
||||
File jsonFile = new File(testData + "invalidExposedPort.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "environmentVariable.json");
|
||||
File jsonFile = getFile(testDataPath, "invalidExposedPort.json");
|
||||
|
||||
assertFalse(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectTrueWhenAllThePropertiesOfLoadBalancerAreValid() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "loadBalancer.json");
|
||||
File jsonFile = new File(testData + "validLoadBalancer.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "loadBalancer.json");
|
||||
File jsonFile = getFile(testDataPath, "validLoadBalancer.json");
|
||||
|
||||
assertTrue(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectFalseWhenSomePropertiesOfLoadBalancerAreMissing() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "loadBalancer.json");
|
||||
File jsonFile = new File(testData + "invalidLoadBalancer.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "loadBalancer.json");
|
||||
File jsonFile = getFile(testDataPath, "invalidLoadBalancer.json");
|
||||
|
||||
assertFalse(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectTrueWhenAllThePropertiesOfHealthChecksAreValid() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "healthChecks.json");
|
||||
File jsonFile = new File(testData + "validHealthChecks.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "healthChecks.json");
|
||||
File jsonFile = getFile(testDataPath, "validHealthChecks.json");
|
||||
|
||||
assertTrue(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectFalseWhenFewPropertiesOfHealthChecksAreMissing() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "healthChecks.json");
|
||||
File jsonFile = new File(testData + "invalidHealthChecks.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "healthChecks.json");
|
||||
File jsonFile = getFile(testDataPath, "invalidHealthChecks.json");
|
||||
|
||||
assertFalse(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectTrueWhenLabelIsValid() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "labels.json");
|
||||
File jsonFile = new File(testData + "validLabel.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "labels.json");
|
||||
File jsonFile = getFile(testDataPath, "validLabel.json");
|
||||
|
||||
assertTrue(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectFalseWhenLabelPropertyIsMissing() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "labels.json");
|
||||
File jsonFile = new File(testData + "invalidLabel.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "labels.json");
|
||||
File jsonFile = getFile(testDataPath, "invalidLabel.json");
|
||||
|
||||
assertFalse(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectTrueWhenPropertyOfTeamIsValid() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "team.json");
|
||||
File jsonFile = new File(testData + "validTeam.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "team.json");
|
||||
File jsonFile = getFile(testDataPath, "validTeam.json");
|
||||
|
||||
assertTrue(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectFalseWhenFewPropertyOfTeamIsMissing() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "team.json");
|
||||
File jsonFile = new File(testData + "invalidTeam.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "team.json");
|
||||
File jsonFile = getFile(testDataPath, "invalidTeam.json");
|
||||
|
||||
assertFalse(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectTrueWhenAllThePropertiesOfDeploymentAreValid() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "deployment.json");
|
||||
File jsonFile = new File(testData + "validDeployment.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "deployment.json");
|
||||
File jsonFile = getFile(testDataPath, "validDeployment.json");
|
||||
|
||||
assertTrue(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectFalseWhenFewPropertiesOfDeploymentAreMissing() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "deployment.json");
|
||||
File jsonFile = new File(testData + "invalidDeployment.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "deployment.json");
|
||||
File jsonFile = getFile(testDataPath, "invalidDeployment.json");
|
||||
|
||||
assertFalse(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectTrueWhenAllThePropertiesOfDeploymentManifestAreValid() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "manifest.json");
|
||||
File jsonFile = new File(testData + "validManifestData.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "manifest.json");
|
||||
File jsonFile = getFile(testDataPath, "validManifestData.json");
|
||||
|
||||
assertTrue(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectFalseWhenVersionFromDeploymentManifestIsMissing() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "manifest.json");
|
||||
File jsonFile = new File(testData + "invalidManifestData.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "manifest.json");
|
||||
File jsonFile = getFile(testDataPath, "invalidManifestData.json");
|
||||
|
||||
assertFalse(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectFalseWhenFewPropertiesFromDeploymentManifestAreMissing() throws IOException, ProcessingException {
|
||||
File schemaFile = new File(schemaPath + "manifest.json");
|
||||
File jsonFile = new File(testData + "manifestDataWithMissingTeam.json");
|
||||
ValidationUtils validationUtils = new ValidationUtils();
|
||||
File schemaFile = getFile(schemaPath, "manifest.json");
|
||||
File jsonFile = getFile(testDataPath, "manifestDataWithMissingTeam.json");
|
||||
|
||||
assertFalse(validationUtils.isJsonValid(schemaFile, jsonFile));
|
||||
}
|
||||
|
||||
private File getFile(String path, String fileName) {
|
||||
return new File(path + fileName);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user