3 * Copyright (c) 2018 Huawei Intellectual Property.
7 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
27 * limitations under the License.
30 package org.openecomp.sdc.be.model.jsonjanusgraph.utils;
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;
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;
66 @RunWith(MockitoJUnitRunner.class)
67 public class ModelConverterTest {
69 private ModelConverter test;
72 public void testConvertToToscaElementService()
74 Service service = new Service();
75 service.setComponentType(ComponentTypeEnum.SERVICE);
76 TopologyTemplate template = test.convertToToscaElement(service);
77 assertThat(template.getToscaType()).isEqualTo(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE);
81 public void testConvertToToscaElementResource()
83 Resource resource = new Resource();
84 resource.setComponentType(ComponentTypeEnum.RESOURCE);
85 NodeType nodeType = test.convertToToscaElement(resource);
86 assertThat(nodeType.getToscaType()).isEqualTo(ToscaElementTypeEnum.NODE_TYPE);
90 public void testConvertFromToscaElementService()
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());
99 public void testConvertFromToscaElementServiceWithSelfCapabilities()
101 TopologyTemplate topologyTemplate = new TopologyTemplate();
103 Map<String, MapPropertiesDataDefinition> capabilitiesProperties = CapabilityTestUtils
104 .createCapPropsForTopologyTemplate(topologyTemplate);
106 topologyTemplate.setCapabilitiesProperties(capabilitiesProperties);
108 topologyTemplate.setComponentType(ComponentTypeEnum.SERVICE);
109 Component component = test.convertFromToscaElement(topologyTemplate);
110 assertThat(component.getToscaType()).isEqualTo(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
114 public void testConvertFromToscaElementResource()
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());
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());
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());
145 public void testConvertFromToscaElementResourceComponentInstancesOutputs() {
146 final TopologyTemplate topologyTemplate = new TopologyTemplate();
147 topologyTemplate.setComponentType(ComponentTypeEnum.RESOURCE);
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);
158 final Map<String, ComponentInstanceDataDefinition> componentInstanceDataDefinitionMap = new HashMap<>();
159 componentInstanceDataDefinitionMap.put("mock", new ComponentInstance());
160 topologyTemplate.setComponentInstances(componentInstanceDataDefinitionMap);
162 final Resource resource = test.convertFromToscaElement(topologyTemplate);
163 assertNotNull(resource);
164 assertNotNull(resource.getComponentInstancesOutputs());
165 assertFalse(resource.getComponentInstancesOutputs().isEmpty());
169 public void testIsAtomicComponent() {
170 Resource component = new Resource();
171 component.setComponentType(ComponentTypeEnum.RESOURCE);
172 boolean result = test.isAtomicComponent(component);
177 public void testGetVertexType()
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");