c5b8fab9eb15bf7d89bb048b3dc5ef09131a6db3
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / PrepareUpdateAAIVfModuleTest.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.common.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.Before
29 import org.junit.Ignore
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.MockitoAnnotations
36 import org.mockito.runners.MockitoJUnitRunner
37 import org.onap.so.bpmn.mock.StubResponseAAI
38
39 import static org.mockito.Mockito.*
40
41 @RunWith(MockitoJUnitRunner.class)
42 @Ignore
43 class PrepareUpdateAAIVfModuleTest {
44     @Rule
45     public WireMockRule wireMockRule = new WireMockRule(28090)
46
47     @Captor
48     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
49
50     @Before
51     void init() throws IOException {
52         MockitoAnnotations.initMocks(this);
53     }
54
55     @Test
56     void testGetGenericVnf() {
57         ExecutionEntity mockExecution = setupMock()
58         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
59         when(mockExecution.getVariable("PUAAIVfMod_vnfId")).thenReturn("skask")
60         when(mockExecution.getVariable("mso.workflow.default.aai.v8.generic-vnf.uri")).thenReturn("/aai/v8/network/generic-vnfs/generic-vnf")
61         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
62         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
63         when(mockExecution.getVariable("mso.workflow.custom.PrepareUpdateAAIVfModule.aai.version")).thenReturn('8')
64
65         StubResponseAAI.MockAAIVfModule()
66
67         PrepareUpdateAAIVfModule obj = new PrepareUpdateAAIVfModule()
68         obj.getGenericVnf(mockExecution)
69
70         verify(mockExecution).setVariable("PUAAIVfMod_getVnfResponseCode", 200)
71     }
72
73     @Test
74     void testGetGenericVnfEndpointNull() {
75         ExecutionEntity mockExecution = setupMock()
76         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
77         when(mockExecution.getVariable("PUAAIVfMod_vnfId")).thenReturn("skask")
78         when(mockExecution.getVariable("mso.workflow.default.aai.v8.generic-vnf.uri")).thenReturn("/aai/v8/network/generic-vnfs/generic-vnf")
79         when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
80         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
81         when(mockExecution.getVariable("mso.workflow.custom.PrepareUpdateAAIVfModule.aai.version")).thenReturn('8')
82
83         StubResponseAAI.MockAAIVfModule()
84         try {
85             PrepareUpdateAAIVfModule obj = new PrepareUpdateAAIVfModule()
86             obj.getGenericVnf(mockExecution)
87         } catch (Exception ex) {
88             println " Test End - Handle catch-throw BpmnError()! "
89         }
90
91         verify(mockExecution).setVariable("PUAAIVfMod_getVnfResponseCode", 500)
92         verify(mockExecution).setVariable("PUAAIVfMod_getVnfResponse", "AAI GET Failed:org.apache.http.client.ClientProtocolException")
93     }
94
95     @Test
96     void testUpdateVfModule() {
97         ExecutionEntity mockExecution = setupMock()
98         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
99         when(mockExecution.getVariable("PUAAIVfMod_vnfId")).thenReturn("skask")
100         when(mockExecution.getVariable("PUAAIVfMod_vfModuleId")).thenReturn("supercool")
101
102         def node = new Node(null, 'vfModule')
103         new Node(node, 'vf-module-name', "abc")
104         VfModule vfModule = new VfModule(node, true)
105
106         when(mockExecution.getVariable("PUAAIVfMod_vfModule")).thenReturn(vfModule)
107         when(mockExecution.getVariable("PUAAIVfMod_orchestrationStatus")).thenReturn("created")
108         when(mockExecution.getVariable("mso.workflow.default.aai.v8.generic-vnf.uri")).thenReturn("/aai/v8/network/generic-vnfs/generic-vnf")
109         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
110         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
111         when(mockExecution.getVariable("mso.workflow.custom.PrepareUpdateAAIVfModule.aai.version")).thenReturn('8')
112
113         StubResponseAAI.MockAAIVfModule()
114         PrepareUpdateAAIVfModule obj = new PrepareUpdateAAIVfModule()
115         obj.updateVfModule(mockExecution)
116
117         verify(mockExecution).setVariable("PUAAIVfMod_updateVfModuleResponseCode", 200)
118         verify(mockExecution).setVariable("PUAAIVfMod_updateVfModuleResponse", "")
119     }
120
121     @Test
122     void testUpdateVfModuleEndpointNull() {
123         ExecutionEntity mockExecution = setupMock()
124         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
125         when(mockExecution.getVariable("PUAAIVfMod_vnfId")).thenReturn("skask")
126         when(mockExecution.getVariable("PUAAIVfMod_vfModuleId")).thenReturn("supercool")
127
128         def node = new Node(null, 'vfModule')
129         new Node(node, 'vf-module-name', "abc")
130         VfModule vfModule = new VfModule(node, true)
131
132         when(mockExecution.getVariable("PUAAIVfMod_vfModule")).thenReturn(vfModule)
133         when(mockExecution.getVariable("PUAAIVfMod_orchestrationStatus")).thenReturn("created")
134         when(mockExecution.getVariable("mso.workflow.default.aai.v8.generic-vnf.uri")).thenReturn("/aai/v8/network/generic-vnfs/generic-vnf")
135         when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
136         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
137         when(mockExecution.getVariable("mso.workflow.custom.PrepareUpdateAAIVfModule.aai.version")).thenReturn('8')
138
139         StubResponseAAI.MockAAIVfModule()
140         try {
141             PrepareUpdateAAIVfModule obj = new PrepareUpdateAAIVfModule()
142             obj.updateVfModule(mockExecution)
143         } catch (Exception ex) {
144             println " Test End - Handle catch-throw BpmnError()! "
145         }
146
147         verify(mockExecution).setVariable("PUAAIVfMod_updateVfModuleResponseCode", 500)
148         verify(mockExecution).setVariable("PUAAIVfMod_updateVfModuleResponse", "AAI PATCH Failed:org.apache.http.client.ClientProtocolException")
149     }
150
151     private static ExecutionEntity setupMock() {
152         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
153         when(mockProcessDefinition.getKey()).thenReturn("PrepareUpdateAAIVfModule")
154         RepositoryService mockRepositoryService = mock(RepositoryService.class)
155         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
156         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("PrepareUpdateAAIVfModule")
157         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
158         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
159         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
160
161         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
162         // Initialize prerequisite variables
163         when(mockExecution.getId()).thenReturn("100")
164         when(mockExecution.getProcessDefinitionId()).thenReturn("PrepareUpdateAAIVfModule")
165         when(mockExecution.getProcessInstanceId()).thenReturn("PrepareUpdateAAIVfModule")
166         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
167         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
168
169         return mockExecution
170     }
171 }