Implement Attributes/Outputs BE (part 3)
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / jsonjanusgraph / utils / ModelConverterTest.java
1 /*
2
3  * Copyright (c) 2018 Huawei Intellectual Property.
4
5  *
6
7  * Licensed under the Apache License, Version 2.0 (the "License");
8
9  * you may not use this file except in compliance with the License.
10
11  * You may obtain a copy of the License at
12
13  *
14
15  *     http://www.apache.org/licenses/LICENSE-2.0
16
17  *
18
19  * Unless required by applicable law or agreed to in writing, software
20
21  * distributed under the License is distributed on an "AS IS" BASIS,
22
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
25  * See the License for the specific language governing permissions and
26
27  * limitations under the License.
28
29  */
30 package org.openecomp.sdc.be.model.jsonjanusgraph.utils;
31
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.List;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.InjectMocks;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
41 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
42 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
43 import org.openecomp.sdc.be.datatypes.elements.MapAttributesDataDefinition;
44 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
45 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
46 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
47 import org.openecomp.sdc.be.model.AttributeDefinition;
48 import org.openecomp.sdc.be.model.ComponentInstance;
49 import org.openecomp.sdc.be.model.ComponentInstanceOutput;
50 import org.openecomp.sdc.be.model.OutputDefinition;
51 import org.openecomp.sdc.be.model.Resource;
52 import org.openecomp.sdc.be.model.Service;
53 import org.openecomp.sdc.be.model.Component;
54 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
55 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.NodeType;
56 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
57
58 import java.util.Map;
59
60 import static org.assertj.core.api.Assertions.assertThat;
61 import static org.junit.Assert.assertFalse;
62 import static org.junit.Assert.assertNotNull;
63 import static org.junit.Assert.assertSame;
64 import static org.junit.Assert.assertTrue;
65
66 @RunWith(MockitoJUnitRunner.class)
67 public class ModelConverterTest {
68     @InjectMocks
69     private ModelConverter test;
70
71     @Test
72     public void testConvertToToscaElementService()
73     {
74         Service service = new Service();
75         service.setComponentType(ComponentTypeEnum.SERVICE);
76         TopologyTemplate template = test.convertToToscaElement(service);
77         assertThat(template.getToscaType()).isEqualTo(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE);
78     }
79
80     @Test
81     public void testConvertToToscaElementResource()
82     {
83         Resource resource = new Resource();
84         resource.setComponentType(ComponentTypeEnum.RESOURCE);
85         NodeType nodeType = test.convertToToscaElement(resource);
86         assertThat(nodeType.getToscaType()).isEqualTo(ToscaElementTypeEnum.NODE_TYPE);
87     }
88
89     @Test
90     public void testConvertFromToscaElementService()
91     {
92         TopologyTemplate topologyTemplate = new TopologyTemplate();
93         topologyTemplate.setComponentType(ComponentTypeEnum.SERVICE);
94         Component component = test.convertFromToscaElement(topologyTemplate);
95         assertThat(component.getToscaType()).isEqualTo(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
96     }
97
98     @Test
99     public void testConvertFromToscaElementServiceWithSelfCapabilities()
100     {
101         TopologyTemplate topologyTemplate = new TopologyTemplate();
102
103         Map<String, MapPropertiesDataDefinition> capabilitiesProperties = CapabilityTestUtils
104                 .createCapPropsForTopologyTemplate(topologyTemplate);
105
106         topologyTemplate.setCapabilitiesProperties(capabilitiesProperties);
107
108         topologyTemplate.setComponentType(ComponentTypeEnum.SERVICE);
109         Component component = test.convertFromToscaElement(topologyTemplate);
110         assertThat(component.getToscaType()).isEqualTo(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
111     }
112
113     @Test
114     public void testConvertFromToscaElementResource()
115     {
116         TopologyTemplate topologyTemplate = new TopologyTemplate();
117         topologyTemplate.setComponentType(ComponentTypeEnum.RESOURCE);
118         Component component = test.convertFromToscaElement(topologyTemplate);
119         assertThat(component.getToscaType()).isEqualTo(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
120     }
121
122     @Test
123     public void testConvertFromToscaElementResourceType() {
124         TopologyTemplate topologyTemplate = new TopologyTemplate();
125         topologyTemplate.setComponentType(ComponentTypeEnum.RESOURCE);
126         topologyTemplate.setResourceType(ResourceTypeEnum.PNF);
127         Resource resource = test.convertFromToscaElement(topologyTemplate);
128         assertSame(ResourceTypeEnum.PNF, resource.getResourceType());
129     }
130
131     @Test
132     public void testConvertFromToscaElementResourceOutputs() {
133         final TopologyTemplate topologyTemplate = new TopologyTemplate();
134         topologyTemplate.setComponentType(ComponentTypeEnum.RESOURCE);
135         final OutputDefinition outputDefinition = new OutputDefinition();
136         final Map<String, AttributeDataDefinition> map = new HashMap<>();
137         map.put("mock", outputDefinition);
138         topologyTemplate.setOutputs(map);
139         final Resource resource = test.convertFromToscaElement(topologyTemplate);
140         assertNotNull(resource.getOutputs());
141         assertFalse(resource.getOutputs().isEmpty());
142     }
143
144     @Test
145     public void testConvertFromToscaElementResourceComponentInstancesOutputs() {
146         final TopologyTemplate topologyTemplate = new TopologyTemplate();
147         topologyTemplate.setComponentType(ComponentTypeEnum.RESOURCE);
148
149         final Map<String, MapAttributesDataDefinition> instOutputs = new HashMap<>();
150         final MapAttributesDataDefinition mapAttributesDataDefinition = new MapAttributesDataDefinition();
151         final AttributeDefinition attributeDefinition = new AttributeDefinition();
152         final Map<String, AttributeDataDefinition> mapToscaDataDefinition = new HashMap<>();
153         mapToscaDataDefinition.put("mock", attributeDefinition);
154         mapAttributesDataDefinition.setMapToscaDataDefinition(mapToscaDataDefinition);
155         instOutputs.put("mock", mapAttributesDataDefinition);
156         topologyTemplate.setInstOutputs(instOutputs);
157
158         final Map<String, ComponentInstanceDataDefinition> componentInstanceDataDefinitionMap = new HashMap<>();
159         componentInstanceDataDefinitionMap.put("mock", new ComponentInstance());
160         topologyTemplate.setComponentInstances(componentInstanceDataDefinitionMap);
161
162         final Resource resource = test.convertFromToscaElement(topologyTemplate);
163         assertNotNull(resource);
164         assertNotNull(resource.getComponentInstancesOutputs());
165         assertFalse(resource.getComponentInstancesOutputs().isEmpty());
166     }
167
168     @Test
169     public void testIsAtomicComponent() {
170         Resource component = new Resource();
171         component.setComponentType(ComponentTypeEnum.RESOURCE);
172         boolean result = test.isAtomicComponent(component);
173         assertTrue(result);
174     }
175
176     @Test
177     public void testGetVertexType()
178     {
179         VertexTypeEnum result;
180         Resource component = new Resource();
181         component.setComponentType(ComponentTypeEnum.RESOURCE);
182         result = test.getVertexType(component);
183         assertThat(result.getName()).isEqualTo("node_type");
184     }
185 }