use of org.alien4cloud.tosca.model.types.DataType in project alien4cloud by alien4cloud.
the class InputsMappingFileVariableResolverTest method check_inputs_mapping_can_be_parsed_when_variable.
@Ignore("Update when ToscaTypeConverter behavior is clearly defined")
@Test
public // Bad test
void check_inputs_mapping_can_be_parsed_when_variable() throws Exception {
inputsMappingFileVariableResolverConfigured.customConverter(new ToscaTypeConverter((concreteType, id) -> {
if (id.equals("datatype.complex_input_entry.sub1")) {
DataType dataType = new DataType();
dataType.setDeriveFromSimpleType(false);
dataType.setProperties(//
ImmutableMap.of(//
"complex", //
buildPropDef(ToscaTypes.MAP, ToscaTypes.STRING)));
return dataType;
}
if (id.equals("datatype.complex_input_entry")) {
DataType dataType = new DataType();
dataType.setDeriveFromSimpleType(false);
dataType.setProperties(//
ImmutableMap.of(//
"sub1", //
buildPropDef("datatype.complex_input_entry.sub1"), //
"sub2", //
buildPropDef(ToscaTypes.MAP, ToscaTypes.STRING), //
"field01", //
buildPropDef(ToscaTypes.STRING)));
return dataType;
}
return null;
}));
Map<String, PropertyValue> inputsMappingFileResolved = resolve("src/test/resources/alien/variables/inputs_mapping_with_variables.yml");
assertThat(inputsMappingFileResolved.get("int_input")).isEqualTo(new ScalarPropertyValue("1"));
assertThat(inputsMappingFileResolved.get("float_input")).isEqualTo(new ScalarPropertyValue("3.14"));
assertThat(inputsMappingFileResolved.get("string_input")).isEqualTo(new ScalarPropertyValue("text_3.14"));
//
assertThat(inputsMappingFileResolved.get("complex_input")).isEqualTo(new ComplexPropertyValue(//
ImmutableMap.of("sub1", new ComplexPropertyValue(//
ImmutableMap.of("complex", //
new ComplexPropertyValue(ImmutableMap.of("subfield", new ScalarPropertyValue("text"))))), "sub2", new ComplexPropertyValue(//
ImmutableMap.of("subfield21", //
new ScalarPropertyValue("1"))), "field01", //
new ScalarPropertyValue("text"))));
}
use of org.alien4cloud.tosca.model.types.DataType in project alien4cloud by alien4cloud.
the class ConstraintPropertyServiceTest method testMapPropertyInComplex.
// ///////////////////////////////////////////////////////////////
// Tests on complex properties validation
// ///////////////////////////////////////////////////////////////
@Test
public void testMapPropertyInComplex() throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException {
// given
PropertyDefinition propertyDefinition = new PropertyDefinition();
propertyDefinition.setType("alien.test.ComplexStruct");
PropertyDefinition subPropertyDefinition = new PropertyDefinition();
subPropertyDefinition.setType(ToscaTypes.MAP);
PropertyDefinition entrySchema = new PropertyDefinition();
entrySchema.setType(ToscaTypes.STRING);
subPropertyDefinition.setEntrySchema(entrySchema);
DataType dataType = new DataType();
dataType.setProperties(Maps.newHashMap());
dataType.getProperties().put("myMap", subPropertyDefinition);
dataType.setElementId("alien.test.ComplexStruct");
ICSARRepositorySearchService originalCsarRepositorySearchService = ToscaContext.getCsarRepositorySearchService();
ToscaContext.init(new HashSet<>());
ICSARRepositorySearchService mockSearchService = Mockito.mock(ICSARRepositorySearchService.class);
Mockito.when(mockSearchService.getRequiredElementInDependencies(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(dataType);
Mockito.when(mockSearchService.getElementInDependencies(Mockito.any(), Mockito.any(), Mockito.anySet())).thenReturn(dataType);
try {
ToscaContext.setCsarRepositorySearchService(mockSearchService);
// when
Object subPropertyValue = ImmutableMap.builder().put("aa", "bb").build();
Object propertyValue = ImmutableMap.builder().put("myMap", subPropertyValue).build();
// then
ConstraintPropertyService.checkPropertyConstraint("test", propertyValue, propertyDefinition);
} finally {
ToscaContext.setCsarRepositorySearchService(originalCsarRepositorySearchService);
ToscaContext.destroy();
}
}
use of org.alien4cloud.tosca.model.types.DataType in project alien4cloud by alien4cloud.
the class ConstraintPropertyService method checkDataTypePropertyConstraint.
private static void checkDataTypePropertyConstraint(String propertyName, Map<String, Object> complexPropertyValue, PropertyDefinition propertyDefinition, Consumer<String> missingPropertyConsumer) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
DataType dataType = ToscaContext.get(DataType.class, propertyDefinition.getType());
if (dataType == null) {
throw new ConstraintViolationException("Complex type " + propertyDefinition.getType() + " is not complex or it cannot be found in the archive nor in Alien");
}
for (Map.Entry<String, Object> complexPropertyValueEntry : complexPropertyValue.entrySet()) {
if (!safe(dataType.getProperties()).containsKey(complexPropertyValueEntry.getKey())) {
throw new ConstraintViolationException("Complex type <" + propertyDefinition.getType() + "> do not have nested property with name <" + complexPropertyValueEntry.getKey() + "> for property <" + propertyName + ">");
} else {
Object nestedPropertyValue = complexPropertyValueEntry.getValue();
PropertyDefinition nestedPropertyDefinition = dataType.getProperties().get(complexPropertyValueEntry.getKey());
checkPropertyConstraint(propertyName + "." + complexPropertyValueEntry.getKey(), nestedPropertyValue, nestedPropertyDefinition, missingPropertyConsumer);
}
}
// check if the data type has required missing properties
if (missingPropertyConsumer != null) {
for (Map.Entry<String, PropertyDefinition> dataTypeDefinition : safe(dataType.getProperties()).entrySet()) {
if (dataTypeDefinition.getValue().isRequired() && !complexPropertyValue.containsKey(dataTypeDefinition.getKey())) {
// A required property is missing
String missingPropertyName = propertyName + "." + dataTypeDefinition.getKey();
missingPropertyConsumer.accept(missingPropertyName);
}
}
}
}
use of org.alien4cloud.tosca.model.types.DataType in project alien4cloud by alien4cloud.
the class ConstraintPropertyService method checkPropertyConstraint.
/**
* Check the constraints on an unwrapped property value (basically a string, map or list) and get events through the given consumer parameter when missing
* properties on complex data type are found.
* Note that the property value cannot be null and the required characteristic of the initial property definition will NOT be checked.
*
* @param propertyName The name of the property.
* @param propertyValue The value of the property to check.
* @param propertyDefinition The property definition that defines the property to check.
* @param missingPropertyConsumer A consumer to receive events when a required property is not defined on a complex type sub-field.
* @throws ConstraintValueDoNotMatchPropertyTypeException In case the value type doesn't match the type of the property as defined.
* @throws ConstraintViolationException In case the value doesn't match one of the constraints defined on the property.
*/
public static void checkPropertyConstraint(String propertyName, Object propertyValue, PropertyDefinition propertyDefinition, Consumer<String> missingPropertyConsumer) throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException {
Object value = propertyValue;
if (propertyValue instanceof PropertyValue) {
value = ((PropertyValue) propertyValue).getValue();
}
boolean isTypeDerivedFromPrimitive = false;
DataType dataType = null;
String typeName = propertyDefinition.getType();
if (!ToscaTypes.isPrimitive(typeName)) {
dataType = ToscaContext.get(DataType.class, typeName);
if (dataType instanceof PrimitiveDataType) {
// the type is derived from a primitive type
isTypeDerivedFromPrimitive = true;
}
}
if (value instanceof String) {
if (ToscaTypes.isSimple(typeName)) {
checkSimplePropertyConstraint(propertyName, (String) value, propertyDefinition);
} else if (isTypeDerivedFromPrimitive) {
checkComplexPropertyDerivedFromPrimitiveTypeConstraints(propertyName, (String) value, propertyDefinition, dataType);
} else {
throwConstraintValueDoNotMatchPropertyTypeException("Property value is a String while the expected data type is the complex type " + propertyDefinition.getType(), propertyName, propertyDefinition.getType(), value);
}
} else if (value instanceof Map) {
if (ToscaTypes.MAP.equals(typeName)) {
checkMapPropertyConstraint(propertyName, (Map<String, Object>) value, propertyDefinition, missingPropertyConsumer);
} else {
checkDataTypePropertyConstraint(propertyName, (Map<String, Object>) value, propertyDefinition, missingPropertyConsumer);
}
} else if (value instanceof List) {
// Range type is a specific primitive type that is actually wrapped
if (ToscaTypes.RANGE.equals(typeName)) {
checkRangePropertyConstraint(propertyName, (List<Object>) value, propertyDefinition);
} else {
checkListPropertyConstraint(propertyName, (List<Object>) value, propertyDefinition, missingPropertyConsumer);
}
} else {
throw new InvalidArgumentException("Not expecting to receive constraint validation for other types than String, Map or List, but got " + value.getClass().getName());
}
}
use of org.alien4cloud.tosca.model.types.DataType in project alien4cloud by alien4cloud.
the class ParserTestUtil method mockNormativeTypes.
/**
* Utility method to mock the acess to some normative types nodes and capabilities.
*
* @param repositorySearchService The repositorySearchService to mock.
*/
public static void mockNormativeTypes(ICSARRepositorySearchService repositorySearchService) {
Csar csar = new Csar("tosca-normative-types", "1.0.0-ALIEN12");
Mockito.when(repositorySearchService.getArchive(csar.getName(), csar.getVersion())).thenReturn(csar);
NodeType mockedNodeRoot = new NodeType();
Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq(NormativeTypesConstant.ROOT_NODE_TYPE), Mockito.any(Set.class))).thenReturn(mockedNodeRoot);
RelationshipType mockedRelationshipRoot = new RelationshipType();
Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq(NormativeTypesConstant.ROOT_RELATIONSHIP_TYPE), Mockito.any(Set.class))).thenReturn(mockedRelationshipRoot);
CapabilityType mockedCapabilityType = new CapabilityType();
Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq(NormativeCapabilityTypes.ROOT), Mockito.any(Set.class))).thenReturn(mockedCapabilityType);
DataType mockedDataType = new DataType();
Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(DataType.class), Mockito.eq(NormativeTypesConstant.ROOT_DATA_TYPE), Mockito.any(Set.class))).thenReturn(mockedDataType);
ArtifactType mockedArtifactType = new ArtifactType();
Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(ArtifactType.class), Mockito.eq(NormativeTypesConstant.ROOT_ARTIFACT_TYPE), Mockito.any(Set.class))).thenReturn(mockedArtifactType);
}
Aggregations