1 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
4 import org.junit.Before;
5 import org.junit.BeforeClass;
8 import org.junit.rules.TestName;
9 import org.junit.runner.RunWith;
10 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
11 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
12 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
13 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
14 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
15 import org.openecomp.sdc.be.model.ComponentParametersView;
16 import org.openecomp.sdc.be.model.LifecycleStateEnum;
17 import org.openecomp.sdc.be.model.ModelTestBase;
18 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
19 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
20 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.GraphTestUtils;
21 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
22 import org.springframework.test.context.ContextConfiguration;
23 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertTrue;
31 * Created by chaya on 6/12/2017.
34 @RunWith(SpringJUnit4ClassRunner.class)
35 @ContextConfiguration("classpath:application-context-test.xml")
36 public class ToscaElementOperationTest extends ModelTestBase{
38 private List<GraphVertex> allVertices = new ArrayList<>();
39 private boolean isInitialized = false;
41 @javax.annotation.Resource
42 private ToscaElementOperationTestImpl toscaElementOperation;
44 @javax.annotation.Resource
45 private JanusGraphDao janusGraphDao;
48 public static void initTest(){
54 public TestName testName = new TestName();
57 public void beforeTest() {
59 GraphTestUtils.clearGraph(janusGraphDao);
60 //exportGraphMl(janusGraphDao.getGraph().left().value(),"");
68 public void testGetAllHighestResourcesNoFilter() {
70 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, null, true);
71 assertTrue(highestResourcesRes.isLeft());
72 List<ToscaElement> highestResources = highestResourcesRes.left().value();
73 // calculate expected count value
74 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
76 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
77 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
80 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
87 public void testGetAllResourcesCertifiedNoFilter() {
88 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, null, false);
89 assertTrue(highestResourcesRes.isLeft());
90 List<ToscaElement> highestResources = highestResourcesRes.left().value();
91 // calculate expected count value
92 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
94 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
95 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
98 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
100 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
101 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
103 }, new HashMap<GraphPropertyEnum, Object>() {
105 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
108 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
112 public void testGetHighestResourcesExclude() {
115 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
116 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
119 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
120 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
122 // exclude CP & VL & VF & VFC
123 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
124 assertTrue(genericTestGetResourcesWithExcludeList(excludeList));
128 public void testGetAllResourcesCertifiedExclude() {
130 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT);
131 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
134 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP);
135 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
137 // exclude CP & VL & VF & VFC
138 excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC);
139 assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList));
143 public void testGetAllHighestServicesNoFilter() {
144 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, true);
145 assertTrue(highestResourcesRes.isLeft());
146 List<ToscaElement> highestResources = highestResourcesRes.left().value();
147 // calculate expected count value
148 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
150 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
151 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
154 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
158 public void testGetAllCertifiedServicesNoFilter() {
159 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, false);
160 assertTrue(highestResourcesRes.isLeft());
161 List<ToscaElement> highestResources = highestResourcesRes.left().value();
162 // calculate expected count value
163 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
165 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
166 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
169 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
171 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
172 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
174 }, new HashMap<GraphPropertyEnum, Object>() {
176 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
179 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
183 public void testGetServicesExcludeList() {
184 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VF, ResourceTypeEnum.VFCMT);
185 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, true);
186 assertTrue(highestResourcesRes.isLeft());
187 List<ToscaElement> highestResources = highestResourcesRes.left().value();
188 // calculate expected count value
189 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
191 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
192 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
195 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
199 public void testGetCertifiedServicesExcludeList() {
200 List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VL);
201 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, false);
202 assertTrue(highestResourcesRes.isLeft());
203 List<ToscaElement> highestResources = highestResourcesRes.left().value();
204 // calculate expected count value
205 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
207 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
208 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
211 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
213 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
214 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
216 }, new HashMap<GraphPropertyEnum, Object>() {
218 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
221 assertEquals(highestResources.stream().count(), highestResourcesExpectedCount);
225 public void testUpdateToscaElement_NotFound() {
226 Either<TopologyTemplate, StorageOperationStatus> result;
227 TopologyTemplate topologyTemplate = new TopologyTemplate();
228 String userID = "userID";
229 topologyTemplate.setLastUpdaterUserId(userID);
230 GraphVertex graphVertex = new GraphVertex();
231 ComponentParametersView componentParametersView = new ComponentParametersView();
232 result = toscaElementOperation.updateToscaElement(topologyTemplate, graphVertex, componentParametersView);
233 assertEquals(null, result);
236 private boolean genericTestGetResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
237 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE,excludeList, true);
238 assertTrue(highestResourcesRes.isLeft());
239 List<ToscaElement> highestResources = highestResourcesRes.left().value();
240 // calculate expected count value
241 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
243 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
244 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
246 }, new HashMap<GraphPropertyEnum, Object>() {
248 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
251 return highestResources.stream().count() == (highestResourcesExpectedCount);
254 private boolean genericTestGetCertifiedResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) {
255 Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, excludeList, false);
256 assertTrue(highestResourcesRes.isLeft());
257 List<ToscaElement> highestResources = highestResourcesRes.left().value();
258 // calculate expected count value
259 long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() {
261 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
262 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
264 }, new HashMap<GraphPropertyEnum, Object>() {
266 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
269 highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() {
271 put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
272 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
274 }, new HashMap<GraphPropertyEnum, Object>() {
276 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
277 put(GraphPropertyEnum.RESOURCE_TYPE, excludeList);
280 return highestResources.stream().count() == highestResourcesExpectedCount;
283 private void initGraphForTest() {
284 GraphTestUtils.createRootCatalogVertex(janusGraphDao);
286 Map<GraphPropertyEnum, Object> highstVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
288 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
292 Map<GraphPropertyEnum, Object> certifiedVerticesProps = new HashMap<GraphPropertyEnum, Object>() {
294 put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
295 put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
299 // add vertices with higestVersion = true
300 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VF));
301 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VFC));
302 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VFCMT));
303 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.VL));
304 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, highstVerticesProps, ResourceTypeEnum.CP));
305 allVertices.add(GraphTestUtils.createServiceVertex(janusGraphDao, highstVerticesProps));
307 // add vertices with non-additional properties
308 for (int i=0 ; i<2 ; i++) {
309 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VF));
310 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VFC));
311 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VFCMT));
312 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VL));
313 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.CP));
314 allVertices.add(GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>()));
317 // add certified vertices
318 for (int i=0; i<3; i++) {
319 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VF));
320 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VFC));
321 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VFCMT));
322 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.VL));
323 allVertices.add(GraphTestUtils.createResourceVertex(janusGraphDao, certifiedVerticesProps, ResourceTypeEnum.CP));
324 allVertices.add(GraphTestUtils.createServiceVertex(janusGraphDao, certifiedVerticesProps));
326 //allVertices.stream().forEach( v -> System.out.println("type: "+v.getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE)));
327 //String result = GraphTestUtils.exportGraphMl(janusGraphDao.getGraph().left().value(), "");
328 //System.out.println("graph is: " + result);
331 private long calculateCount(HashMap<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> doesntHaveProps){
332 return allVertices.stream().
334 Map<GraphPropertyEnum, Object> vertexProps = v.getMetadataProperties();
335 if (hasProps != null) {
336 for (Map.Entry<GraphPropertyEnum, Object> prop: hasProps.entrySet()){
337 Object value = vertexProps.get(prop.getKey());
338 if ( value == null || !value.equals(prop.getValue())) {
344 if (doesntHaveProps != null) {
345 for (Map.Entry<GraphPropertyEnum, Object> prop : doesntHaveProps.entrySet()) {
346 Object value = vertexProps.get(prop.getKey());
347 Object propValue = prop.getValue();
348 if ( value != null && propValue != null && propValue instanceof List ) {
349 for (ResourceTypeEnum propVal : (List<ResourceTypeEnum>)propValue) {
350 if (propVal.name().equals(value)) {
355 else if (value != null && value.equals(propValue)){