Dynamic Cloud Owner Support
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / MsoGroovyTest.groovy
index 01e37f0..3a2673c 100644 (file)
@@ -26,9 +26,40 @@ import org.camunda.bpm.engine.ProcessEngineServices
 import org.camunda.bpm.engine.RepositoryService
 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
 import org.camunda.bpm.engine.repository.ProcessDefinition
+import org.junit.Rule
+import org.junit.rules.ExpectedException
+import org.junit.runner.RunWith
+import org.mockito.runners.MockitoJUnitRunner
+import org.onap.aai.domain.yang.GenericVnf
+import org.onap.so.bpmn.mock.FileUtil
+import org.onap.so.client.aai.AAIObjectPlurals
+import org.onap.so.client.aai.AAIObjectType
+import org.onap.so.client.aai.AAIResourcesClient
+import org.onap.so.client.aai.entities.AAIResultWrapper
+import org.onap.so.client.aai.entities.uri.AAIResourceUri
+import org.onap.so.client.aai.entities.uri.AAIUriFactory
+import org.onap.so.client.graphinventory.entities.uri.Depth
+import org.onap.so.constants.Defaults
 
+@RunWith(MockitoJUnitRunner.class)
 abstract class MsoGroovyTest {
        
+    @Rule
+    public ExpectedException thrown = ExpectedException.none()
+
+       protected ExecutionEntity mockExecution
+       protected AAIResourcesClient client
+    protected AllottedResourceUtils allottedResourceUtils_MOCK
+       protected final String SEARCH_RESULT_AAI_WITH_RESULTDATA =
+                       FileUtil.readResourceFile("__files/aai/searchResults.json")
+       protected static final String CLOUD_OWNER = Defaults.CLOUD_OWNER.toString();
+
+       protected void init(String procName){
+               mockExecution = setupMock(procName)
+               when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
+               client = mock(AAIResourcesClient.class)
+       }
+
        protected ExecutionEntity setupMock(String procName) {
                ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
                when(mockProcessDefinition.getKey()).thenReturn(procName)
@@ -64,5 +95,59 @@ abstract class MsoGroovyTest {
                return mockExecution
        }
        
+    protected <T> Optional<T> getAAIObjectFromJson(Class<T> clazz , String file){
+        String json = FileUtil.readResourceFile(file)
+        AAIResultWrapper resultWrapper = new AAIResultWrapper(json)
+        return resultWrapper.asBean(clazz)
+    }
+
+    protected Optional<GenericVnf> mockAAIGenericVnf(String vnfId){
+        return mockAAIGenericVnf(vnfId,"__files/aai/GenericVnf.json")
+    }
        
+    protected Optional<GenericVnf> mockAAIGenericVnf(String vnfId,String file){
+        AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
+        AAIResourceUri resourceUriDepthOne = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE)
+        Optional<GenericVnf> genericVnf = getAAIObjectFromJson(GenericVnf.class,file);
+        when(client.get(GenericVnf.class, resourceUri)).thenReturn(genericVnf)
+        when(client.get(GenericVnf.class, resourceUriDepthOne)).thenReturn(genericVnf)
+        return genericVnf
+    }
+
+    protected Optional<GenericVnf> mockAAIGenericVnfByName(String vnfName){
+        AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName)
+        AAIResourceUri resourceUriDepthOne = AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName).depth(Depth.ONE)
+        Optional<GenericVnf> genericVnf = getAAIObjectFromJson(GenericVnf.class,"__files/aai/GenericVnf.json");
+        when(client.get(GenericVnf.class, resourceUri)).thenReturn(genericVnf)
+        when(client.get(GenericVnf.class, resourceUriDepthOne)).thenReturn(genericVnf)
+        return genericVnf
+    }
+
+    protected void mockAAIGenericVnfNotFound(String vnfId){
+        AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE)
+        AAIResourceUri resourceUriDepthOne = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE)
+        when(client.get(GenericVnf.class, resourceUri)).thenReturn(Optional.empty())
+        when(client.get(GenericVnf.class, resourceUriDepthOne)).thenReturn(Optional.empty())
+    }
+
+    protected void mockAAIGenericVnfByNameNotFound(String vnfName){
+        AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName)
+        AAIResourceUri resourceUriDepthOne = AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName).depth(Depth.ONE)
+        when(client.get(GenericVnf.class, resourceUri)).thenReturn(Optional.empty())
+        when(client.get(GenericVnf.class, resourceUriDepthOne)).thenReturn(Optional.empty())
+    }
+
+    protected AAIResultWrapper mockVolumeGroupWrapper(String region, String volumeGroupId, String file){
+        AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, region,volumeGroupId)
+        String json = FileUtil.readResourceFile(file)
+        AAIResultWrapper resultWrapper = new AAIResultWrapper(json)
+        when(client.get(resourceUri)).thenReturn(resultWrapper)
+        return resultWrapper
+    }
+
+    void initAR(String procName){
+        init(procName)
+        allottedResourceUtils_MOCK = spy(new AllottedResourceUtils(mock(AbstractServiceTaskProcessor.class)))
+        when(allottedResourceUtils_MOCK.getAAIClient()).thenReturn(client)
+    }
 }