use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.
the class FunctionEvaluator method getPropertyValue.
private static AbstractPropertyValue getPropertyValue(Map<String, AbstractPropertyValue> properties, Map<String, PropertyDefinition> propertyDefinitions, String propertyAccessPath) {
if (properties == null || !properties.containsKey(propertyAccessPath)) {
String propertyName = PropertyUtil.getPropertyNameFromComplexPath(propertyAccessPath);
if (propertyName == null) {
// Non complex
return PropertyUtil.getDefaultFromPropertyDefinitions(propertyAccessPath, propertyDefinitions);
} else {
// Complex
PropertyDefinition propertyDefinition = propertyDefinitions.get(propertyName);
AbstractPropertyValue rawValue;
if (propertyDefinition == null) {
return null;
} else if (ToscaTypes.isSimple(propertyDefinition.getType())) {
// It's a complex path (with '.') but the type in definition is finally simple
return null;
} else if (properties != null && (rawValue = properties.get(propertyName)) != null) {
if (!(rawValue instanceof PropertyValue)) {
throw new NotSupportedException("Only support static value in a get_property");
}
Object value = MapUtil.get(((PropertyValue) rawValue).getValue(), propertyAccessPath.substring(propertyName.length() + 1));
return new ScalarPropertyValue(PropertyUtil.serializePropertyValue(value));
} else {
return null;
}
}
} else {
return properties.get(propertyAccessPath);
}
}
use of org.alien4cloud.tosca.model.definitions.PropertyDefinition 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.definitions.PropertyDefinition in project alien4cloud by alien4cloud.
the class ToscaParserSimpleProfileAlien120Test method testRelationshipType.
@SuppressWarnings("unchecked")
@Test
public void testRelationshipType() throws FileNotFoundException, ParsingException {
RelationshipType mockedResult = Mockito.mock(RelationshipType.class);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq("tosca.relationships.Relationship"), Mockito.any(Set.class))).thenReturn(mockedResult);
Mockito.when(mockedResult.getDerivedFrom()).thenReturn(Lists.newArrayList("tosca.capabilities.Root"));
ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-relationship-type.yml"));
ParserTestUtil.displayErrors(parsingResult);
assertNoBlocker(parsingResult);
ArchiveRoot archiveRoot = parsingResult.getResult();
Assert.assertNotNull(archiveRoot.getArchive());
Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
Assert.assertEquals(1, archiveRoot.getRelationshipTypes().size());
Entry<String, RelationshipType> entry = archiveRoot.getRelationshipTypes().entrySet().iterator().next();
Assert.assertEquals("mycompany.mytypes.myapplication.MyRelationship", entry.getKey());
RelationshipType relationship = entry.getValue();
Assert.assertEquals(Lists.newArrayList("tosca.relationships.Relationship", "tosca.capabilities.Root"), relationship.getDerivedFrom());
Assert.assertEquals("a custom relationship", relationship.getDescription());
// properties
Assert.assertEquals(2, relationship.getProperties().size());
Assert.assertTrue(relationship.getProperties().containsKey("my_feature_setting"));
PropertyDefinition pd = relationship.getProperties().get("my_feature_setting");
Assert.assertEquals("string", pd.getType());
Assert.assertTrue(relationship.getProperties().containsKey("my_feature_value"));
pd = relationship.getProperties().get("my_feature_value");
Assert.assertEquals("integer", pd.getType());
// valid targets
Assert.assertEquals(2, relationship.getValidTargets().length);
Assert.assertEquals("tosca.capabilities.Feature1", relationship.getValidTargets()[0]);
Assert.assertEquals("tosca.capabilities.Feature2", relationship.getValidTargets()[1]);
}
use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.
the class PropertyDefinitionConstraintsTest method testCheckIfCompatibleOrFailConstraintSatisfiedAllNull.
@Test
public void testCheckIfCompatibleOrFailConstraintSatisfiedAllNull() throws ConstraintViolationException, IncompatiblePropertyDefinitionException {
PropertyDefinition propDef1 = new PropertyDefinition();
PropertyDefinition propDef2 = new PropertyDefinition();
propDef1.setType(ToscaTypes.STRING);
propDef2.setType(ToscaTypes.STRING);
propDef1.checkIfCompatibleOrFail(propDef2);
}
use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.
the class PropertyDefinitionConstraintsTest method testCheckIfCompatibleOrFailConstraintNotSatisfiedSetOfScamblePropertyConstraintWithDifferentType.
@Test(expected = IncompatiblePropertyDefinitionException.class)
public void testCheckIfCompatibleOrFailConstraintNotSatisfiedSetOfScamblePropertyConstraintWithDifferentType() throws ConstraintViolationException, IncompatiblePropertyDefinitionException {
PropertyDefinition propDef1 = new PropertyDefinition();
PropertyDefinition propDef2 = new PropertyDefinition();
List<PropertyConstraint> constraintsProp1 = Lists.newArrayList();
List<PropertyConstraint> constraintsProp2 = Lists.newArrayList();
propDef1.setType(ToscaTypes.STRING);
propDef2.setType(ToscaTypes.INTEGER);
EqualConstraint constraint1 = new EqualConstraint();
constraint1.setEqual("test");
InRangeConstraint constraint1Bis = new InRangeConstraint();
constraint1Bis.setRangeMaxValue("4");
LessThanConstraint constraint2 = new LessThanConstraint();
constraint2.setLessThan("5");
constraintsProp1.add(constraint1);
constraintsProp1.add(constraint2);
constraintsProp2.add(constraint2);
constraintsProp2.add(constraint1Bis);
propDef1.setConstraints(constraintsProp1);
propDef2.setConstraints(constraintsProp2);
propDef1.checkIfCompatibleOrFail(propDef2);
}
Aggregations