re base code
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / jsontitan / operations / ExternalReferencesOperationTest.java
index efa4020..e580fd5 100644 (file)
@@ -1,14 +1,6 @@
 package org.openecomp.sdc.be.model.jsontitan.operations;
 
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.when;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.annotation.Resource;
-
+import fj.data.Either;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -24,10 +16,15 @@ import org.openecomp.sdc.be.datatypes.elements.MapComponentInstanceExternalRefs;
 import org.openecomp.sdc.be.model.ModelTestBase;
 import org.openecomp.sdc.be.model.jsontitan.utils.GraphTestUtils;
 import org.openecomp.sdc.be.model.jsontitan.utils.IdMapper;
+import org.openecomp.sdc.be.model.operations.StorageException;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
-import fj.data.Either;
+import javax.annotation.Resource;
+import java.util.*;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.when;
 
 /**
  * Created by yavivi on 26/01/2018.
@@ -36,6 +33,18 @@ import fj.data.Either;
 @ContextConfiguration("classpath:application-context-test.xml")
 public class ExternalReferencesOperationTest extends ModelTestBase {
 
+    private static final String COMPONENT_ID = "ci-MyComponentName";
+    private static final String COMPONENT2_ID = "ci-MyComponentName2";
+    private static final String MONITORING_OBJECT_TYPE = "monitoring";
+    private static final String WORKFLOW_OBJECT_TYPE = "workflow";
+    private static final String REF_1 = "ref1";
+    private static final String REF_2 = "ref2";
+    private static final String REF_3 = "ref3";
+    private static final String REF_4 = "ref4";
+    private static final String REF_5 = "ref5";
+    //workflow
+    private static final String REF_6 = "ref6";
+
     @Resource
     private ExternalReferencesOperation externalReferenceOperation;
 
@@ -45,20 +54,12 @@ public class ExternalReferencesOperationTest extends ModelTestBase {
     private boolean isInitialized;
 
     private GraphVertex serviceVertex;
+    private GraphVertex serviceVertex2;
+    private GraphVertex serviceVertex3;
 
     private String serviceVertexUuid;
-    private static final String COMPONENT_ID = "ci-MyComponentName";
-
-    private static final String MONITORING_OBJECT_TYPE = "monitoring";
-    private static final String WORKFLOW_OBJECT_TYPE = "workflow";
-    private static final String REF_1 = "ref1";
-    private static final String REF_2 = "ref2";
-    private static final String REF_3 = "ref3";
-    private static final String REF_4 = "ref4";
-    private static final String REF_5 = "ref5";
-
-    //workflow
-    private static final String REF_6 = "ref6";
+    private String serviceVertex2Uuid;
+    private String serviceVertex3Uuid;
 
     private IdMapper idMapper;
 
@@ -91,6 +92,44 @@ public class ExternalReferencesOperationTest extends ModelTestBase {
         assertThat(getServiceExternalRefs()).contains(REF_1, REF_2, REF_3, REF_4);
     }
 
+    @Test
+    public void testAddExternalReferences_success() {
+        Map<String, List<String>> refsMap = Collections.singletonMap(MONITORING_OBJECT_TYPE, Arrays.asList(REF_1, REF_2));
+        externalReferenceOperation.addAllExternalReferences(serviceVertex3Uuid, COMPONENT_ID, refsMap);
+        Map<String, List<String>> allExternalReferences = externalReferenceOperation.getAllExternalReferences(serviceVertex3Uuid, COMPONENT_ID);
+        assertThat(allExternalReferences.size()).isEqualTo(1);
+        assertThat(allExternalReferences).flatExtracting(MONITORING_OBJECT_TYPE).containsExactly(REF_1, REF_2);
+        externalReferenceOperation.addAllExternalReferences(serviceVertex3Uuid, COMPONENT2_ID, refsMap);
+        Map<String, List<String>> allExternalReferences2 = externalReferenceOperation.getAllExternalReferences(serviceVertex3Uuid, COMPONENT2_ID);
+        assertThat(allExternalReferences2.size()).isEqualTo(1);
+        assertThat(allExternalReferences2).flatExtracting(MONITORING_OBJECT_TYPE).containsExactly(REF_1, REF_2);
+    }
+
+    @Test
+    public void testGetAllCIExternalRefs_success() {
+        Map<String, List<String>> allExternalReferences = externalReferenceOperation.getAllExternalReferences(serviceVertexUuid, COMPONENT_ID);
+        assertThat(allExternalReferences.size()).isEqualTo(2);
+        assertThat(allExternalReferences).flatExtracting(WORKFLOW_OBJECT_TYPE).containsExactly(REF_6);
+        assertThat(allExternalReferences).flatExtracting(MONITORING_OBJECT_TYPE).containsExactly(REF_1, REF_2, REF_3, REF_5);
+    }
+
+    @Test
+    public void testGetAllCIExternalRefs_noRefsExist() {
+        Map<String, List<String>> allExternalReferences = externalReferenceOperation.getAllExternalReferences(serviceVertex2Uuid, COMPONENT_ID);
+        assertThat(allExternalReferences.size()).isZero();
+    }
+
+    @Test
+    public void testGetAllCIExternalRefs_noSuchComponentInstance() {
+        Map<String, List<String>> allExternalReferences = externalReferenceOperation.getAllExternalReferences(serviceVertex2Uuid, "FAKE");
+        assertThat(allExternalReferences.size()).isZero();
+    }
+
+    @Test(expected=StorageException.class)
+    public void testGetAllCIExternalRefs_nonExitingService_throwsException() {
+        externalReferenceOperation.getAllExternalReferences("FAKE", COMPONENT_ID);
+    }
+
     @Test
     public void testGetComponentInstanceExternalRef(){
         assertThat(externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE).left().value()).contains(REF_1, REF_2, REF_3, REF_5);
@@ -155,13 +194,12 @@ public class ExternalReferencesOperationTest extends ModelTestBase {
 
         //Get List of references
         //final List<String> externalRefsByObjectType = mapComponentInstanceExternalRefs.externalRefsByObjectType(objectType);
-        final List<String> externalRefsByObjectType = mapComponentInstanceExternalRefs.getExternalRefsByObjectType(MONITORING_OBJECT_TYPE);
 
-        return externalRefsByObjectType;
+        return mapComponentInstanceExternalRefs.getExternalRefsByObjectType(MONITORING_OBJECT_TYPE);
     }
 
     private void initGraphForTest() {
-        //create a service and add 1 ref
+        //create a service
         this.serviceVertex = GraphTestUtils.createServiceVertex(titanDao, new HashMap<>());
         this.serviceVertexUuid = this.serviceVertex.getUniqueId();
 
@@ -174,6 +212,14 @@ public class ExternalReferencesOperationTest extends ModelTestBase {
         //workflow references
         externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6);
 
+        //create a service without refs
+        serviceVertex2 = GraphTestUtils.createServiceVertex(titanDao, new HashMap<>());
+        serviceVertex2Uuid = serviceVertex2.getUniqueId();
+
+        //create a service for adding all references
+        serviceVertex3 = GraphTestUtils.createServiceVertex(titanDao, new HashMap<>());
+        serviceVertex3Uuid = serviceVertex3.getUniqueId();
+
         final TitanOperationStatus commit = this.titanDao.commit();
         assertThat(commit).isEqualTo(TitanOperationStatus.OK);
     }