Skip to content

Commit

Permalink
Issue #1368: Add validation field to pipeline configuration structure
Browse files Browse the repository at this point in the history
  • Loading branch information
mzueva committed Sep 8, 2020
1 parent 638d19c commit f8a9088
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@

package com.epam.pipeline.entity;

import com.epam.pipeline.entity.configuration.PipeConfValueVO;
import com.epam.pipeline.entity.configuration.PipelineConfiguration;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.epam.pipeline.config.JsonMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.*;

public class PipelineConfigurationTest {

private static final String WITH_TYPE_OF_PARAMS_JSON =
Expand All @@ -45,7 +47,8 @@ public class PipelineConfigurationTest {
"\"enum\" : [\"v1\", \"v2\"]" +
"}," +
"\"instance_size\" : {" +
"\"type\" : \"string\"" +
"\"type\" : \"string\"," +
"\"validation\": [{\"throw\":\"a == a\", \"message\": \"error\"}]" +
"}," +
"\"instance_disk\" : \"200\"" +
"}" +
Expand All @@ -67,32 +70,35 @@ public void setup() {

@Test
public void parsingConfigurationJsonTest() throws IOException {
PipelineConfiguration pipelineConfiguration = mapper.readValue(
final PipelineConfiguration pipelineConfiguration = mapper.readValue(
WITH_TYPE_OF_PARAMS_JSON, PipelineConfiguration.class
);

Assert.assertTrue(
assertTrue(
pipelineConfiguration
.getParameters()
.values()
.stream()
.allMatch(v -> v.getType() != null)
);

Assert.assertEquals(STRING_TYPE, pipelineConfiguration.getParameters().get("main_file").getType());
Assert.assertEquals(true, pipelineConfiguration.getParameters().get("main_file").isRequired());
final PipeConfValueVO mainFile = pipelineConfiguration.getParameters().get("main_file");
assertEquals(STRING_TYPE, mainFile.getType());
assertTrue(mainFile.isRequired());

Assert.assertEquals(CLASS_TYPE, pipelineConfiguration.getParameters().get("main_class").getType());
Assert.assertEquals(false, pipelineConfiguration.getParameters().get("main_class").isRequired());
final PipeConfValueVO mainClass = pipelineConfiguration.getParameters().get("main_class");
assertEquals(CLASS_TYPE, mainClass.getType());
assertFalse(mainClass.isRequired());

Assert.assertEquals(STRING_TYPE, pipelineConfiguration.getParameters().get("instance_size").getType());
Assert.assertEquals(false, pipelineConfiguration.getParameters().get("instance_size").isRequired());
Assert.assertEquals(EMPTY, pipelineConfiguration.getParameters().get("instance_size").getValue());

Assert.assertEquals(STRING_TYPE, pipelineConfiguration.getParameters().get("instance_disk").getType());
Assert.assertEquals(false, pipelineConfiguration.getParameters().get("instance_disk").isRequired());
Assert.assertEquals(EXP_INSTANCE_DISK, pipelineConfiguration.getParameters().get("instance_disk").getValue());
final PipeConfValueVO instanceSize = pipelineConfiguration.getParameters().get("instance_size");
assertEquals(STRING_TYPE, instanceSize.getType());
assertFalse(instanceSize.isRequired());
assertEquals(EMPTY, instanceSize.getValue());
assertEquals(1, instanceSize.getValidation().size());

final PipeConfValueVO instanceDisk = pipelineConfiguration.getParameters().get("instance_disk");
assertEquals(STRING_TYPE, instanceDisk.getType());
assertFalse(instanceDisk.isRequired());
assertEquals(EXP_INSTANCE_DISK, instanceDisk.getValue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Setter
@Getter
Expand All @@ -45,6 +46,8 @@ public class PipeConfValueVO {
@JsonProperty(value = "enum")
private List<Object> availableValues;

private List<Map<String, String>> validation;

/**
* String expression to determine visibility of a param
* User for GUI client only
Expand Down Expand Up @@ -74,6 +77,7 @@ public PipeConfValueVO(String value, String type, boolean required, List<String>
this.type = type;
this.required = required;
this.availableValues = Collections.unmodifiableList(availableValues);
this.validation = new ArrayList<>();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class PipelineConfValuesMapDeserializer extends JsonDeserializer<Map<Stri
private static final String ENUM_FIELD = "enum";
private static final String DESCRIPTION_FIELD = "description";
private static final String VISIBLE_FIELD = "visible";
private static final String VALIDATION_FIELD = "validation";
private static final NullNode NULL_NODE = NullNode.getInstance();
private final ObjectMapper mapper;

Expand Down Expand Up @@ -86,6 +87,11 @@ public Map<String, PipeConfValueVO> deserialize(JsonParser p, DeserializationCon
if (hasValue(visible)) {
parameter.setVisible(visible.asText());
}
final JsonNode validation = child.get(VALIDATION_FIELD);
if (hasValue(validation) && validation.isArray()) {
parameter.setValidation(mapper.readValue(validation.traverse(),
new TypeReference<List<Map<String, String>>>(){}));
}
}
parameters.put(name, parameter);
}
Expand All @@ -100,7 +106,7 @@ public void parseEnumValues(DeserializationContext ctxt, JsonNode child, PipeCon
if (arrayItem.isObject()) {
try {
availableValues.add(mapper.readValue(arrayItem.traverse(),
new TypeReference<Map<String,String>>(){}));
new TypeReference<Map<String, String>>(){}));
} catch (IOException e) {
log.error(e.getMessage(), e);
}
Expand Down

0 comments on commit f8a9088

Please sign in to comment.