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