2da6eca84e65cb348eba3f5978ad4b7f00475af1
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.onap.so.bpmn.infrastructure.scripts
22
23 import com.github.tomakehurst.wiremock.junit.WireMockRule
24 import org.camunda.bpm.engine.ProcessEngineServices
25 import org.camunda.bpm.engine.RepositoryService
26 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
27 import org.camunda.bpm.engine.repository.ProcessDefinition
28 import org.junit.Assert
29 import org.junit.Before
30 import org.junit.Rule
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 import org.mockito.ArgumentCaptor
34 import org.mockito.Captor
35 import org.mockito.Mockito
36 import org.mockito.MockitoAnnotations
37 import org.mockito.Spy
38 import org.mockito.runners.MockitoJUnitRunner
39 import org.onap.aai.domain.yang.GenericVnf
40 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
41 import org.onap.so.bpmn.core.WorkflowException
42 import org.onap.aaiclient.client.aai.AAIObjectType
43 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
44 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
45 import org.onap.aaiclient.client.graphinventory.entities.uri.Depth
46 import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryUriComputationException
47 import static com.github.tomakehurst.wiremock.client.WireMock.*
48 import static org.mockito.Mockito.*
49
50 @RunWith(MockitoJUnitRunner.class)
51 public class DoDeleteVnfAndModulesTest extends MsoGroovyTest{
52
53     @Captor
54     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
55
56     @Spy
57     DoDeleteVnfAndModules doDeleteVnfAndModules
58
59     String cloudConfiguration = "{ " +
60             "\"lcpCloudRegionId\": \"mdt1\"," +
61             "\"tenantId\": \"88a6ca3ee0394ade9403f075db23167e\"" + "}";
62
63     @Before
64     public void init() throws IOException {
65         super.init("DoDeleteVnfAndModules")
66         MockitoAnnotations.initMocks(this);
67         when(doDeleteVnfAndModules.getAAIClient()).thenReturn(client)
68     }
69
70     @Test
71     public void testPreProcessRequestTest() {
72         ExecutionEntity mockExecution = setupMock()
73         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
74         when(mockExecution.getVariable("cloudConfiguration")).thenReturn(cloudConfiguration)
75         when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
76         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("serviceInstanceId")
77         when(mockExecution.getVariable("sdncVersion")).thenReturn("1702")
78         when(mockExecution.getVariable("productFamilyId")).thenReturn("productFamilyId")
79         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("mdt1")
80         when(mockExecution.getVariable("tenantId")).thenReturn("19123c2924c648eb8e42a3c1f14b7682")
81         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("MSO_test")
82         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:8090/SDNCAdapterCallback")
83
84         DoDeleteVnfAndModules obj = new DoDeleteVnfAndModules()
85         obj.preProcessRequest(mockExecution)
86
87         Mockito.verify(mockExecution).setVariable("prefix", "DDVAM_")
88         Mockito.verify(mockExecution).setVariable("requestId", "12345")
89         Mockito.verify(mockExecution).setVariable("mso-request-id", "12345")
90         Mockito.verify(mockExecution).setVariable("DDVAM_source", "VID")
91         Mockito.verify(mockExecution).setVariable("DDVAM_isVidRequest", "true")
92         Mockito.verify(mockExecution).setVariable("DDVAM_sdncVersion", "1702")
93         Mockito.verify(mockExecution).setVariable("DDVAM_isVidRequest", "true")
94         Mockito.verify(mockExecution).setVariable("sdncCallbackUrl", "http://localhost:8090/SDNCAdapterCallback")
95     }
96
97   
98
99     @Test
100     public void testQueryAAIVfModuleNullEndPoint() {
101         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
102         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "12345").depth(Depth.ONE)
103         doThrow(new GraphInventoryUriComputationException("Error in AAI")).when(client).get(GenericVnf.class,uri)
104         try {
105             doDeleteVnfAndModules.queryAAIVfModule(mockExecution)
106         } catch (Exception ex) {
107             println " Test End - Handle catch-throw BpmnError()! "
108         }
109         Mockito.verify(mockExecution,atLeastOnce()).setVariable(captor.capture(),captor.capture())
110         WorkflowException workflowException = captor.getValue()
111         Assert.assertEquals("AAI GET Failed:Error in AAI", workflowException.getErrorMessage())
112         Assert.assertEquals(1002, workflowException.getErrorCode())
113     }
114
115     @Test
116     public void testQueryAAIVfModule() {
117         ExecutionEntity mockExecution = setupMock()
118         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
119         mockAAIGenericVnf("12345","__files/AAI/GenericVnfVfModule.json")
120         doDeleteVnfAndModules.queryAAIVfModule(mockExecution)
121         Mockito.verify(mockExecution).setVariable("DCVFM_queryAAIVfModuleResponseCode", 200)
122     }
123
124     private ExecutionEntity setupMock() {
125
126         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
127         when(mockProcessDefinition.getKey()).thenReturn("DoDeleteVnfAndModules")
128         RepositoryService mockRepositoryService = mock(RepositoryService.class)
129         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
130         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoDeleteVnfAndModules")
131         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
132         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
133         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
134
135         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
136         // Initialize prerequisite variables
137
138         when(mockExecution.getId()).thenReturn("100")
139         when(mockExecution.getProcessDefinitionId()).thenReturn("DoDeleteVnfAndModules")
140         when(mockExecution.getProcessInstanceId()).thenReturn("DoDeleteVnfAndModules")
141         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
142         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
143
144         return mockExecution
145
146     }
147
148     private void mockData() {
149
150         stubFor(get(urlMatching("/aai/v[0-9]+/network/generic-vnfs/generic-vnf/12345[?]depth=1"))
151                 .willReturn(aResponse()
152                 .withStatus(200)
153                 .withHeader("Content-Type", "text/xml")
154                 .withBodyFile("GenericFlows/getGenericVnfResponse_hasRelationships.xml")));
155     }
156 }