Support for TOSCA functions for Service Import
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / ToscaFunctionServiceTest.java
1 /*
2  * -
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
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.be.components.impl;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
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;
42
43 class ToscaFunctionServiceTest {
44
45     private final ToscaFunctionService toscaFunctionService = new ToscaFunctionService();
46
47
48     @Test
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));
58
59         final var componentAttribute1 = new AttributeDefinition();
60         componentAttribute1.setUniqueId("componentAttribute1Id");
61         componentAttribute1.setName("componentAttribute1Name");
62         component.setAttributes(List.of(componentAttribute1));
63
64         final var componentProperty1 = new PropertyDefinition();
65         componentProperty1.setUniqueId("componentProperty1Id");
66         componentProperty1.setName("componentProperty1Name");
67         component.setProperties(List.of(componentProperty1));
68
69         final var componentInstance1 = new ComponentInstance();
70         componentInstance1.setName("componentInstance1Name");
71         componentInstance1.setUniqueId("componentInstance1Id");
72         component.setComponentInstances(List.of(componentInstance1));
73
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));
81
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));
87
88         final ToscaConcatFunction toscaConcatFunction = new ToscaConcatFunction();
89
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));
95
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));
102
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()));
108
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()));
115
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()));
121
122         toscaConcatFunction.setParameters(
123             List.of(toscaGetInput, toscaGetPropertyFromSelf, toscaGetPropertyFromInstance, toscaGetAttributeFromSelf, toscaGetAttributeFromInstance)
124         );
125
126         //when
127         toscaFunctionService.updateFunctionWithDataFromSelfComponent(toscaConcatFunction, component, instancePropertyMap, instanceAttributeMap);
128
129         //then
130         assertEquals(componentInput1.getUniqueId(), toscaGetInput.getPropertyUniqueId());
131         assertEquals(component.getUniqueId(), toscaGetInput.getSourceUniqueId());
132         assertEquals(component.getName(), toscaGetInput.getSourceName());
133
134         assertEquals(instancePropertyId1, toscaGetPropertyFromInstance.getPropertyUniqueId());
135         assertEquals(componentInstance1.getUniqueId(), toscaGetPropertyFromInstance.getSourceUniqueId());
136
137         assertEquals(instanceAttribute1.getUniqueId(), toscaGetAttributeFromInstance.getPropertyUniqueId());
138         assertEquals(componentInstance1.getUniqueId(), toscaGetAttributeFromInstance.getSourceUniqueId());
139
140         assertEquals(componentAttribute1.getUniqueId(), toscaGetAttributeFromSelf.getPropertyUniqueId());
141         assertEquals(component.getUniqueId(), toscaGetAttributeFromSelf.getSourceUniqueId());
142         assertEquals(component.getName(), toscaGetAttributeFromSelf.getSourceName());
143
144         assertEquals(componentProperty1.getUniqueId(), toscaGetPropertyFromSelf.getPropertyUniqueId());
145         assertEquals(component.getUniqueId(), toscaGetPropertyFromSelf.getSourceUniqueId());
146         assertEquals(component.getName(), toscaGetPropertyFromSelf.getSourceName());
147     }
148 }