use of org.alien4cloud.tosca.model.types.DataType in project alien4cloud by alien4cloud.
the class ConstraintPropertyServiceTest method testInvalidMapPropertyInComplex.
@Test(expected = ConstraintValueDoNotMatchPropertyTypeException.class)
public void testInvalidMapPropertyInComplex() 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 propertyValue = ImmutableMap.builder().put("myMap", "aa").build();
// then -> ConstraintViolationException
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 ToscaParserAlien140Test method testCapabilitiesComplexProperty.
@Test
public void testCapabilitiesComplexProperty() throws ParsingException {
Mockito.reset(csarRepositorySearchService);
Csar csar = new Csar("tosca-normative-types", "1.0.0-ALIEN14");
Mockito.when(csarRepositorySearchService.getArchive(csar.getName(), csar.getVersion())).thenReturn(csar);
NodeType mockedResult = Mockito.mock(NodeType.class);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockedResult);
CapabilityType mockedCapabilityResult = Mockito.mock(CapabilityType.class);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Root"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
DataType mockedDataType = new DataType();
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(DataType.class), Mockito.eq("tosca.datatypes.Root"), Mockito.any(Set.class))).thenReturn(mockedDataType);
ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "capa_complex_props.yml"));
Assert.assertEquals(0, parsingResult.getContext().getParsingErrors().size());
ArchiveRoot archiveRoot = parsingResult.getResult();
// check the capabilityType
// ////////////
CapabilityType capaType = archiveRoot.getCapabilityTypes().values().stream().findFirst().get();
assertNotNull(capaType.getProperties());
Assert.assertEquals(3, capaType.getProperties().size());
// map property
String map = "map";
PropertyDefinition propertyDefinition = capaType.getProperties().get(map);
assertNotNull(propertyDefinition.getDefault());
assertTrue(propertyDefinition.getDefault() instanceof ComplexPropertyValue);
Map<String, Object> propertyMapValue = ((ComplexPropertyValue) propertyDefinition.getDefault()).getValue();
assertNotNull(propertyMapValue);
Assert.assertEquals(2, propertyMapValue.size());
Assert.assertEquals("toto_value", propertyMapValue.get("toto"));
Assert.assertEquals("tata_value", propertyMapValue.get("tata"));
// custom property
String custom = "custom";
propertyDefinition = capaType.getProperties().get(custom);
assertEquals("alien.test.datatypes.Custom", propertyDefinition.getType());
assertNull(propertyDefinition.getDefault());
// custom_with_default property
String custom_with_default = "custom_with_default";
propertyDefinition = capaType.getProperties().get(custom_with_default);
assertNotNull(propertyDefinition.getDefault());
assertTrue(propertyDefinition.getDefault() instanceof ComplexPropertyValue);
propertyMapValue = ((ComplexPropertyValue) propertyDefinition.getDefault()).getValue();
assertNotNull(propertyMapValue);
assertEquals(2, propertyMapValue.size());
assertEquals("defaultName", propertyMapValue.get("name"));
Object list = propertyMapValue.get("groups");
assertTrue(list instanceof List);
assertEquals(2, ((List) list).size());
assertTrue(CollectionUtils.containsAll((List) list, Lists.newArrayList("alien", "fastconnect")));
// check the node template capability
// ////////////
NodeTemplate nodeTemplate = archiveRoot.getTopology().getNodeTemplates().values().stream().findFirst().get();
Capability capability = nodeTemplate.getCapabilities().values().stream().findFirst().get();
assertNotNull(capability);
Assert.assertEquals(3, capability.getProperties().size());
// map property
AbstractPropertyValue propertyValue = capability.getProperties().get(map);
assertNotNull(propertyValue);
assertTrue(propertyValue instanceof ComplexPropertyValue);
propertyMapValue = ((ComplexPropertyValue) propertyValue).getValue();
assertNotNull(propertyMapValue);
Assert.assertEquals(2, propertyMapValue.size());
Assert.assertEquals("toto_value", propertyMapValue.get("toto"));
Assert.assertEquals("tata_value", propertyMapValue.get("tata"));
// custom property
propertyValue = capability.getProperties().get(custom);
assertNotNull(propertyValue);
assertTrue(propertyValue instanceof ComplexPropertyValue);
propertyMapValue = ((ComplexPropertyValue) propertyValue).getValue();
assertNotNull(propertyMapValue);
assertEquals(2, propertyMapValue.size());
assertEquals("manual", propertyMapValue.get("name"));
list = propertyMapValue.get("groups");
assertTrue(list instanceof List);
assertEquals(2, ((List) list).size());
assertTrue(CollectionUtils.containsAll((List) list, Lists.newArrayList("manual_alien", "manual_fastconnect")));
// custom_with_default property
propertyValue = capability.getProperties().get(custom_with_default);
assertNotNull(propertyValue);
assertTrue(propertyValue instanceof ComplexPropertyValue);
propertyMapValue = ((ComplexPropertyValue) propertyValue).getValue();
assertNotNull(propertyMapValue);
assertEquals(2, propertyMapValue.size());
assertEquals("defaultName", propertyMapValue.get("name"));
list = propertyMapValue.get("groups");
assertTrue(list instanceof List);
assertEquals(2, ((List) list).size());
assertTrue(CollectionUtils.containsAll((List) list, Lists.newArrayList("alien", "fastconnect")));
}
use of org.alien4cloud.tosca.model.types.DataType in project alien4cloud by alien4cloud.
the class PropertyDefinitionPostProcessor method validateType.
private void validateType(PropertyDefinition propertyDefinition) {
String propertyType = propertyDefinition.getType();
if (propertyType == null) {
Node node = ParsingContextExecution.getObjectToNodeMap().get(propertyType);
if (node == null) {
node = ParsingContextExecution.getObjectToNodeMap().get(propertyDefinition);
}
ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.VALIDATION_ERROR, "ToscaPropertyType", node.getStartMark(), "Property type must be defined", node.getEndMark(), "type"));
} else if (!ToscaTypes.isSimple(propertyType)) {
if (ToscaTypes.LIST.equals(propertyType) || ToscaTypes.MAP.equals(propertyType)) {
PropertyDefinition entrySchema = propertyDefinition.getEntrySchema();
if (entrySchema == null) {
Node node = ParsingContextExecution.getObjectToNodeMap().get(propertyDefinition);
ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.VALIDATION_ERROR, "ToscaPropertyType", node.getStartMark(), "Type " + propertyType + " must define entry schema", node.getEndMark(), "type"));
} else {
validateType(entrySchema);
}
} else {
// It's data type
ArchiveRoot archiveRoot = ParsingContextExecution.getRootObj();
if (!archiveRoot.getDataTypes().containsKey(propertyType)) {
DataType dataType = ToscaContext.get(DataType.class, propertyType);
if (dataType == null) {
Node node = ParsingContextExecution.getObjectToNodeMap().get(propertyType);
ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.TYPE_NOT_FOUND, "ToscaPropertyType", node.getStartMark(), "Type " + propertyType + " is not found.", node.getEndMark(), "type"));
}
}
}
}
}
use of org.alien4cloud.tosca.model.types.DataType in project alien4cloud by alien4cloud.
the class ArchiveRootPostProcessor method processRepositoriesDefinitions.
private void processRepositoriesDefinitions(Map<String, RepositoryDefinition> repositories) {
if (MapUtils.isNotEmpty(repositories)) {
DataType credentialType = ToscaContext.get(DataType.class, NormativeCredentialConstant.DATA_TYPE);
repositories.values().forEach(repositoryDefinition -> {
if (repositoryDefinition.getCredential() != null) {
credentialType.getProperties().forEach((propertyName, propertyDefinition) -> {
// Fill with default value
if (!repositoryDefinition.getCredential().getValue().containsKey(propertyName)) {
AbstractPropertyValue defaultValue = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(propertyDefinition);
if (defaultValue instanceof PropertyValue) {
repositoryDefinition.getCredential().getValue().put(propertyName, ((PropertyValue) defaultValue).getValue());
}
}
});
Node credentialNode = ParsingContextExecution.getObjectToNodeMap().get(repositoryDefinition.getCredential());
PropertyDefinition propertyDefinition = new PropertyDefinition();
propertyDefinition.setType(NormativeCredentialConstant.DATA_TYPE);
propertyValueChecker.checkProperty("credential", credentialNode, repositoryDefinition.getCredential(), propertyDefinition, repositoryDefinition.getId());
}
});
}
}
use of org.alien4cloud.tosca.model.types.DataType in project alien4cloud by alien4cloud.
the class ToscaPropertyFormDescriptorGenerator method doGenerateDescriptor.
private Map<String, Object> doGenerateDescriptor(Set<String> processedDataTypes, PropertyDefinition propertyDefinition, Set<CSARDependency> dependencies) {
Map<String, Object> dataTypeDescriptors;
if (ToscaTypes.isSimple(propertyDefinition.getType())) {
dataTypeDescriptors = generateDescriptorForSimpleType(propertyDefinition);
} else if (ToscaTypes.LIST.equals(propertyDefinition.getType())) {
PropertyDefinition entryDefinition = propertyDefinition.getEntrySchema();
if (entryDefinition == null) {
throw new InvalidArgumentException("List type without entry schema");
}
return generateDescriptorForListType(processedDataTypes, entryDefinition, dependencies);
} else if (ToscaTypes.MAP.equals(propertyDefinition.getType())) {
PropertyDefinition entryDefinition = propertyDefinition.getEntrySchema();
if (entryDefinition == null) {
throw new InvalidArgumentException("Map type without entry schema");
}
dataTypeDescriptors = generateDescriptorForMapType(processedDataTypes, entryDefinition, dependencies);
} else {
DataType dataType = csarRepositorySearchService.getElementInDependencies(DataType.class, propertyDefinition.getType(), dependencies);
if (dataType == null) {
throw new InvalidArgumentException("Data type <" + propertyDefinition.getType() + "> do not exist in dependencies " + dependencies);
}
if (processedDataTypes.add(dataType.getElementId())) {
dataTypeDescriptors = generateDescriptorForDataType(processedDataTypes, dataType, dependencies);
} else {
dataTypeDescriptors = generateDescriptorForSimpleType(propertyDefinition);
}
}
if (propertyDefinition.isRequired()) {
dataTypeDescriptors.put(NOT_NULL_KEY, true);
}
return dataTypeDescriptors;
}
Aggregations