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