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