2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
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;
33 import java.util.Collections;
34 import java.util.HashMap;
37 import static org.junit.Assert.*;
38 import static org.mockito.Mockito.*;
40 public class ArtifactsOperationsTest {
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);
47 public void getInstanceArtifacts_collectAllInstanceArtifacts() throws Exception {
48 Map<String, ToscaDataDefinition> instanceArtifacts = Collections.singletonMap(INSTANCE_ID, getArtifactsByInstance("name1"));
50 Map<String, ToscaDataDefinition> instanceDeploymentArtifacts = new HashMap<>();
51 instanceDeploymentArtifacts.put(INSTANCE_ID, getArtifactsByInstance("name2", "name3"));
52 instanceDeploymentArtifacts.put("instanceId2", getArtifactsByInstance("name4"));
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);
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
67 public void getInstanceArtifacts_noArtifactsForInstance() throws Exception {
68 Map<String, ToscaDataDefinition> instanceArtifacts = Collections.singletonMap(INSTANCE_ID, getArtifactsByInstance("name1"));
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");
74 assertTrue(allInstArtifacts.isLeft());
75 assertTrue(allInstArtifacts.left().value().isEmpty());
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());
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());
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());
100 artifactsByInstance.setMapToscaDataDefinition(artifactsByName);
101 return artifactsByInstance;