407ffa291539e3a6ae20604fff3e9ab567bcedfe
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / UpdateAAIVfModuleTest.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.Assert
29 import org.junit.Before
30 import org.junit.Ignore
31 import org.junit.Rule
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 import org.mockito.ArgumentCaptor
35 import org.mockito.Captor
36 import org.mockito.Mockito
37 import org.mockito.MockitoAnnotations
38 import org.mockito.runners.MockitoJUnitRunner
39 import org.onap.so.bpmn.core.WorkflowException
40 import org.onap.so.bpmn.mock.FileUtil
41 import org.onap.so.bpmn.mock.StubResponseAAI
42
43 import static org.mockito.Mockito.*
44
45 @RunWith(MockitoJUnitRunner.class)
46 @Ignore
47 class UpdateAAIVfModuleTest {
48     def prefix = "UAAIVfMod_"
49
50     @Rule
51     public WireMockRule wireMockRule = new WireMockRule(28090)
52
53     @Captor
54     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
55
56     @Before
57     void init() throws IOException {
58         MockitoAnnotations.initMocks(this);
59     }
60
61     @Test
62     void testGetVfModule() {
63         ExecutionEntity mockExecution = setupMock()
64         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
65         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
66         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
67         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
68
69         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
70         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
71         when(mockExecution.getVariable("mso.workflow.custom.UpdateAAIVfModule.aai.version")).thenReturn('8')
72
73         StubResponseAAI.MockAAIVfModule()
74         UpdateAAIVfModule obj = new UpdateAAIVfModule()
75         obj.getVfModule(mockExecution)
76
77         verify(mockExecution).setVariable(prefix + "getVfModuleResponseCode", 200)
78     }
79
80     @Test
81     void testGetVfModuleEndpointNull() {
82         ExecutionEntity mockExecution = setupMock()
83         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
84         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
85         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
86         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
87
88         when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
89         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
90         when(mockExecution.getVariable("mso.workflow.custom.UpdateAAIVfModule.aai.version")).thenReturn('8')
91
92         StubResponseAAI.MockAAIVfModule()
93         try {
94             UpdateAAIVfModule obj = new UpdateAAIVfModule()
95             obj.getVfModule(mockExecution)
96         } catch (Exception ex) {
97             println " Test End - Handle catch-throw BpmnError()! "
98         }
99
100         Mockito.verify(mockExecution, times(3)).setVariable(captor.capture(), captor.capture())
101         WorkflowException workflowException = captor.getAllValues().get(1)
102         Assert.assertEquals(9999, workflowException.getErrorCode())
103         Assert.assertEquals("org.apache.http.client.ClientProtocolException", workflowException.getErrorMessage())
104     }
105
106     @Test
107     void testUpdateVfModule() {
108         ExecutionEntity mockExecution = setupMock()
109         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
110         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
111         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
112         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
113
114         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
115         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
116         when(mockExecution.getVariable("mso.workflow.custom.UpdateAAIVfModule.aai.version")).thenReturn('8')
117
118         String getVfModuleResponse = FileUtil.readResourceFile("__files/VfModularity/GenericVnf.xml")
119         when(mockExecution.getVariable(prefix + "getVfModuleResponse")).thenReturn(getVfModuleResponse)
120         StubResponseAAI.MockAAIVfModule()
121         UpdateAAIVfModule obj = new UpdateAAIVfModule()
122         obj.updateVfModule(mockExecution)
123
124         verify(mockExecution).setVariable(prefix + "updateVfModuleResponseCode", 200)
125     }
126
127     @Test
128     void testUpdateVfModuleEndpointNull() {
129         ExecutionEntity mockExecution = setupMock()
130         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
131         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
132         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
133         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
134
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.UpdateAAIVfModule.aai.version")).thenReturn('8')
138
139         String getVfModuleResponse = FileUtil.readResourceFile("__files/VfModularity/GenericVnf.xml")
140         when(mockExecution.getVariable(prefix + "getVfModuleResponse")).thenReturn(getVfModuleResponse)
141         StubResponseAAI.MockAAIVfModule()
142         try {
143             UpdateAAIVfModule obj = new UpdateAAIVfModule()
144             obj.updateVfModule(mockExecution)
145         } catch (Exception ex) {
146             println " Test End - Handle catch-throw BpmnError()! "
147         }
148
149         Mockito.verify(mockExecution, times(3)).setVariable(captor.capture(), captor.capture())
150         WorkflowException workflowException = captor.getAllValues().get(1)
151         Assert.assertEquals(9999, workflowException.getErrorCode())
152         Assert.assertEquals("org.apache.http.client.ClientProtocolException", workflowException.getErrorMessage())
153     }
154
155     private static ExecutionEntity setupMock() {
156         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
157         when(mockProcessDefinition.getKey()).thenReturn("UpdateAAIVfModule")
158         RepositoryService mockRepositoryService = mock(RepositoryService.class)
159         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
160         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("UpdateAAIVfModule")
161         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
162         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
163         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
164
165         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
166         // Initialize prerequisite variables
167         when(mockExecution.getId()).thenReturn("100")
168         when(mockExecution.getProcessDefinitionId()).thenReturn("UpdateAAIVfModule")
169         when(mockExecution.getProcessInstanceId()).thenReturn("UpdateAAIVfModule")
170         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
171         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
172
173         return mockExecution
174     }
175 }