7bbb5a12634b2249b7a93b97a6e2b73e7c248a61
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / ArtifactsOperationsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
22
23 import fj.data.Either;
24 import org.junit.Test;
25 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
26 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
27 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
28 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
29 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
30 import org.openecomp.sdc.be.model.ArtifactDefinition;
31 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
32
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.Map;
36
37 import static org.junit.Assert.*;
38 import static org.mockito.Mockito.*;
39
40 public class ArtifactsOperationsTest {
41
42     private static final String SERVICE_ID = "serviceId";
43     private static final String INSTANCE_ID = "instanceId";
44     private ArtifactsOperations testInstance = mock(ArtifactsOperations.class, CALLS_REAL_METHODS);
45
46     @Test
47     public void getInstanceArtifacts_collectAllInstanceArtifacts() throws Exception {
48         Map<String, ToscaDataDefinition> instanceArtifacts = Collections.singletonMap(INSTANCE_ID, getArtifactsByInstance("name1"));
49
50         Map<String, ToscaDataDefinition> instanceDeploymentArtifacts = new HashMap<>();
51         instanceDeploymentArtifacts.put(INSTANCE_ID, getArtifactsByInstance("name2", "name3"));
52         instanceDeploymentArtifacts.put("instanceId2", getArtifactsByInstance("name4"));
53
54         doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INSTANCE_ARTIFACTS);
55         doReturn(Either.left(instanceDeploymentArtifacts)).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
56         Either<Map<String, ArtifactDefinition>, StorageOperationStatus> allInstArtifacts = testInstance.getAllInstanceArtifacts(SERVICE_ID, INSTANCE_ID);
57
58         assertTrue(allInstArtifacts.isLeft());
59         assertEquals(allInstArtifacts.left().value().size(), 3);
60         assertTrue(allInstArtifacts.left().value().containsKey("name1"));
61         assertTrue(allInstArtifacts.left().value().containsKey("name2"));
62         assertTrue(allInstArtifacts.left().value().containsKey("name3"));
63         assertFalse(allInstArtifacts.left().value().containsKey("name4"));//this key is of different instance
64     }
65
66     @Test
67     public void getInstanceArtifacts_noArtifactsForInstance() throws Exception {
68         Map<String, ToscaDataDefinition> instanceArtifacts = Collections.singletonMap(INSTANCE_ID, getArtifactsByInstance("name1"));
69
70         doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INSTANCE_ARTIFACTS);
71         doReturn(Either.left(new HashMap<>())).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
72         Either<Map<String, ArtifactDefinition>, StorageOperationStatus> allInstArtifacts = testInstance.getAllInstanceArtifacts(SERVICE_ID, "someOtherInstance");
73
74         assertTrue(allInstArtifacts.isLeft());
75         assertTrue(allInstArtifacts.left().value().isEmpty());
76     }
77
78     @Test
79     public void getInstanceArtifacts_errorGettingInstanceArtifacts() throws Exception {
80         doReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR)).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INSTANCE_ARTIFACTS);
81         Either<Map<String, ArtifactDefinition>, StorageOperationStatus> allInstArtifacts = testInstance.getAllInstanceArtifacts(SERVICE_ID, INSTANCE_ID);
82         verify(testInstance, times(0)).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
83         assertTrue(allInstArtifacts.isRight());
84     }
85
86     @Test
87     public void getAllInstanceArtifacts_errorGettingDeploymentArtifacts() throws Exception {
88         doReturn(Either.left(new HashMap<>())).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INSTANCE_ARTIFACTS);
89         doReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR)).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
90         Either<Map<String, ArtifactDefinition>, StorageOperationStatus> allInstArtifacts = testInstance.getAllInstanceArtifacts(SERVICE_ID, INSTANCE_ID);
91         assertTrue(allInstArtifacts.isRight());
92     }
93
94     private ToscaDataDefinition getArtifactsByInstance(String ... artifactsNames) {
95         MapArtifactDataDefinition artifactsByInstance = new MapArtifactDataDefinition();
96         Map<String, ArtifactDataDefinition> artifactsByName = new HashMap<>();
97         for (String artifactName : artifactsNames) {
98             artifactsByName.put(artifactName, new ArtifactDataDefinition());
99         }
100         artifactsByInstance.setMapToscaDataDefinition(artifactsByName);
101         return artifactsByInstance;
102     }
103 }