Springboot 2.0 upgrade
[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.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.so.client.aai.AAIObjectType
43 import org.onap.so.client.aai.entities.uri.AAIResourceUri
44 import org.onap.so.client.aai.entities.uri.AAIUriFactory
45 import org.onap.so.client.graphinventory.entities.uri.Depth
46 import org.onap.so.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     @Rule
54     public WireMockRule wireMockRule = new WireMockRule(28090);
55
56     @Captor
57     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
58
59     @Spy
60     DoDeleteVnfAndModules doDeleteVnfAndModules
61
62     String cloudConfiguration = "{ " +
63             "\"lcpCloudRegionId\": \"mdt1\"," +
64             "\"tenantId\": \"88a6ca3ee0394ade9403f075db23167e\"" + "}";
65
66     @Before
67     public void init() throws IOException {
68         super.init("DoDeleteVnfAndModules")
69         MockitoAnnotations.initMocks(this);
70         when(doDeleteVnfAndModules.getAAIClient()).thenReturn(client)
71     }
72
73     @Test
74     public void testPreProcessRequestTest() {
75         ExecutionEntity mockExecution = setupMock()
76         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
77         when(mockExecution.getVariable("cloudConfiguration")).thenReturn(cloudConfiguration)
78         when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
79         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("serviceInstanceId")
80         when(mockExecution.getVariable("sdncVersion")).thenReturn("1702")
81         when(mockExecution.getVariable("productFamilyId")).thenReturn("productFamilyId")
82         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("mdt1")
83         when(mockExecution.getVariable("tenantId")).thenReturn("19123c2924c648eb8e42a3c1f14b7682")
84         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("MSO_test")
85         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:8090/SDNCAdapterCallback")
86
87         DoDeleteVnfAndModules obj = new DoDeleteVnfAndModules()
88         obj.preProcessRequest(mockExecution)
89
90         Mockito.verify(mockExecution).setVariable("prefix", "DDVAM_")
91         Mockito.verify(mockExecution).setVariable("requestId", "12345")
92         Mockito.verify(mockExecution).setVariable("mso-request-id", "12345")
93         Mockito.verify(mockExecution).setVariable("DDVAM_source", "VID")
94         Mockito.verify(mockExecution).setVariable("DDVAM_isVidRequest", "true")
95         Mockito.verify(mockExecution).setVariable("DDVAM_sdncVersion", "1702")
96         Mockito.verify(mockExecution).setVariable("DDVAM_isVidRequest", "true")
97         Mockito.verify(mockExecution).setVariable("sdncCallbackUrl", "http://localhost:8090/SDNCAdapterCallback")
98     }
99
100   
101
102     @Test
103     public void testQueryAAIVfModuleNullEndPoint() {
104         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
105         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "12345").depth(Depth.ONE)
106         doThrow(new GraphInventoryUriComputationException("Error in AAI")).when(client).get(GenericVnf.class,uri)
107         try {
108             doDeleteVnfAndModules.queryAAIVfModule(mockExecution)
109         } catch (Exception ex) {
110             println " Test End - Handle catch-throw BpmnError()! "
111         }
112         Mockito.verify(mockExecution,atLeastOnce()).setVariable(captor.capture(),captor.capture())
113         WorkflowException workflowException = captor.getValue()
114         Assert.assertEquals("AAI GET Failed:Error in AAI", workflowException.getErrorMessage())
115         Assert.assertEquals(1002, workflowException.getErrorCode())
116     }
117
118     @Test
119     public void testQueryAAIVfModule() {
120         ExecutionEntity mockExecution = setupMock()
121         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
122         mockAAIGenericVnf("12345","__files/AAI/GenericVnfVfModule.json")
123         doDeleteVnfAndModules.queryAAIVfModule(mockExecution)
124         Mockito.verify(mockExecution).setVariable("DCVFM_queryAAIVfModuleResponseCode", 200)
125     }
126
127     private ExecutionEntity setupMock() {
128
129         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
130         when(mockProcessDefinition.getKey()).thenReturn("DoDeleteVnfAndModules")
131         RepositoryService mockRepositoryService = mock(RepositoryService.class)
132         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
133         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoDeleteVnfAndModules")
134         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
135         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
136         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
137
138         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
139         // Initialize prerequisite variables
140
141         when(mockExecution.getId()).thenReturn("100")
142         when(mockExecution.getProcessDefinitionId()).thenReturn("DoDeleteVnfAndModules")
143         when(mockExecution.getProcessInstanceId()).thenReturn("DoDeleteVnfAndModules")
144         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
145         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
146
147         return mockExecution
148
149     }
150
151     private void mockData() {
152
153         stubFor(get(urlMatching("/aai/v[0-9]+/network/generic-vnfs/generic-vnf/12345[?]depth=1"))
154                 .willReturn(aResponse()
155                 .withStatus(200)
156                 .withHeader("Content-Type", "text/xml")
157                 .withBodyFile("GenericFlows/getGenericVnfResponse_hasRelationships.xml")));
158     }
159 }