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