3 * ============LICENSE_START=======================================================
4 * Copyright (C) 2022 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdc.be.components.impl;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import java.util.HashMap;
27 import java.util.List;
29 import org.junit.jupiter.api.Test;
30 import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction;
31 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
32 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
33 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
34 import org.openecomp.sdc.be.model.AttributeDefinition;
35 import org.openecomp.sdc.be.model.Component;
36 import org.openecomp.sdc.be.model.ComponentInstance;
37 import org.openecomp.sdc.be.model.ComponentInstanceAttribute;
38 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
39 import org.openecomp.sdc.be.model.InputDefinition;
40 import org.openecomp.sdc.be.model.PropertyDefinition;
41 import org.openecomp.sdc.be.model.Service;
43 class ToscaFunctionServiceTest {
45 private final ToscaFunctionService toscaFunctionService = new ToscaFunctionService();
49 void updateFunctionWithDataFromSelfComponentTest() {
50 //given a component with one property, one attribute, one instance. The instance have one property and one attribute.
51 final Component component = new Service();
52 component.setUniqueId("componentId");
53 component.setName("componentName");
54 final var componentInput1 = new InputDefinition();
55 componentInput1.setUniqueId("input1Id");
56 componentInput1.setName("input1Name");
57 component.setInputs(List.of(componentInput1));
59 final var componentAttribute1 = new AttributeDefinition();
60 componentAttribute1.setUniqueId("componentAttribute1Id");
61 componentAttribute1.setName("componentAttribute1Name");
62 component.setAttributes(List.of(componentAttribute1));
64 final var componentProperty1 = new PropertyDefinition();
65 componentProperty1.setUniqueId("componentProperty1Id");
66 componentProperty1.setName("componentProperty1Name");
67 component.setProperties(List.of(componentProperty1));
69 final var componentInstance1 = new ComponentInstance();
70 componentInstance1.setName("componentInstance1Name");
71 componentInstance1.setUniqueId("componentInstance1Id");
72 component.setComponentInstances(List.of(componentInstance1));
74 final Map<String, List<ComponentInstanceProperty>> instancePropertyMap = new HashMap<>();
75 final var componentInstanceProperty = new ComponentInstanceProperty();
76 final String instancePropertyId1 = "instancePropertyId1";
77 componentInstanceProperty.setUniqueId(instancePropertyId1);
78 final String instancePropertyName1 = "instancePropertyName1";
79 componentInstanceProperty.setName(instancePropertyName1);
80 instancePropertyMap.put(componentInstance1.getUniqueId(), List.of(componentInstanceProperty));
82 final Map<String, List<AttributeDefinition>> instanceAttributeMap = new HashMap<>();
83 final AttributeDefinition instanceAttribute1 = new ComponentInstanceAttribute();
84 instanceAttribute1.setUniqueId("instanceAttribute1Id");
85 instanceAttribute1.setName("instanceAttribute1Name");
86 instanceAttributeMap.put(componentInstance1.getUniqueId(), List.of(instanceAttribute1));
88 final ToscaConcatFunction toscaConcatFunction = new ToscaConcatFunction();
90 final ToscaGetFunctionDataDefinition toscaGetInput = new ToscaGetFunctionDataDefinition();
91 toscaGetInput.setFunctionType(ToscaGetFunctionType.GET_INPUT);
92 toscaGetInput.setPropertyName(componentInput1.getName());
93 toscaGetInput.setPropertySource(PropertySource.SELF);
94 toscaConcatFunction.setParameters(List.of(toscaGetInput));
96 final ToscaGetFunctionDataDefinition toscaGetPropertyFromInstance = new ToscaGetFunctionDataDefinition();
97 toscaGetPropertyFromInstance.setFunctionType(ToscaGetFunctionType.GET_PROPERTY);
98 toscaGetPropertyFromInstance.setPropertyName(instancePropertyName1);
99 toscaGetPropertyFromInstance.setSourceName(componentInstance1.getName());
100 toscaGetPropertyFromInstance.setPropertySource(PropertySource.INSTANCE);
101 toscaGetPropertyFromInstance.setPropertyPathFromSource(List.of(instancePropertyName1));
103 final ToscaGetFunctionDataDefinition toscaGetPropertyFromSelf = new ToscaGetFunctionDataDefinition();
104 toscaGetPropertyFromSelf.setFunctionType(ToscaGetFunctionType.GET_PROPERTY);
105 toscaGetPropertyFromSelf.setPropertyName(componentProperty1.getName());
106 toscaGetPropertyFromSelf.setPropertySource(PropertySource.SELF);
107 toscaGetPropertyFromSelf.setPropertyPathFromSource(List.of(componentProperty1.getName()));
109 final ToscaGetFunctionDataDefinition toscaGetAttributeFromInstance = new ToscaGetFunctionDataDefinition();
110 toscaGetAttributeFromInstance.setFunctionType(ToscaGetFunctionType.GET_ATTRIBUTE);
111 toscaGetAttributeFromInstance.setPropertyName(instanceAttribute1.getUniqueId());
112 toscaGetAttributeFromInstance.setSourceName(componentInstance1.getName());
113 toscaGetAttributeFromInstance.setPropertySource(PropertySource.INSTANCE);
114 toscaGetAttributeFromInstance.setPropertyPathFromSource(List.of(instanceAttribute1.getName()));
116 final ToscaGetFunctionDataDefinition toscaGetAttributeFromSelf = new ToscaGetFunctionDataDefinition();
117 toscaGetAttributeFromSelf.setFunctionType(ToscaGetFunctionType.GET_ATTRIBUTE);
118 toscaGetAttributeFromSelf.setPropertyName(componentAttribute1.getName());
119 toscaGetAttributeFromSelf.setPropertySource(PropertySource.SELF);
120 toscaGetAttributeFromSelf.setPropertyPathFromSource(List.of(componentAttribute1.getName()));
122 toscaConcatFunction.setParameters(
123 List.of(toscaGetInput, toscaGetPropertyFromSelf, toscaGetPropertyFromInstance, toscaGetAttributeFromSelf, toscaGetAttributeFromInstance)
127 toscaFunctionService.updateFunctionWithDataFromSelfComponent(toscaConcatFunction, component, instancePropertyMap, instanceAttributeMap);
130 assertEquals(componentInput1.getUniqueId(), toscaGetInput.getPropertyUniqueId());
131 assertEquals(component.getUniqueId(), toscaGetInput.getSourceUniqueId());
132 assertEquals(component.getName(), toscaGetInput.getSourceName());
134 assertEquals(instancePropertyId1, toscaGetPropertyFromInstance.getPropertyUniqueId());
135 assertEquals(componentInstance1.getUniqueId(), toscaGetPropertyFromInstance.getSourceUniqueId());
137 assertEquals(instanceAttribute1.getUniqueId(), toscaGetAttributeFromInstance.getPropertyUniqueId());
138 assertEquals(componentInstance1.getUniqueId(), toscaGetAttributeFromInstance.getSourceUniqueId());
140 assertEquals(componentAttribute1.getUniqueId(), toscaGetAttributeFromSelf.getPropertyUniqueId());
141 assertEquals(component.getUniqueId(), toscaGetAttributeFromSelf.getSourceUniqueId());
142 assertEquals(component.getName(), toscaGetAttributeFromSelf.getSourceName());
144 assertEquals(componentProperty1.getUniqueId(), toscaGetPropertyFromSelf.getPropertyUniqueId());
145 assertEquals(component.getUniqueId(), toscaGetPropertyFromSelf.getSourceUniqueId());
146 assertEquals(component.getName(), toscaGetPropertyFromSelf.getSourceName());