2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.common.scripts
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
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
43 import static org.mockito.Mockito.*
45 @RunWith(MockitoJUnitRunner.class)
47 class UpdateAAIVfModuleTest {
48 def prefix = "UAAIVfMod_"
51 public WireMockRule wireMockRule = new WireMockRule(28090)
54 static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
57 void init() throws IOException {
58 MockitoAnnotations.initMocks(this);
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 when(mockExecution.getVariable("mso.workflow.default.aai.v8.generic-vnf.uri")).thenReturn("/aai/v8/network/generic-vnfs/generic-vnf")
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')
73 StubResponseAAI.MockAAIVfModule()
74 UpdateAAIVfModule obj = new UpdateAAIVfModule()
75 obj.getVfModule(mockExecution)
77 verify(mockExecution).setVariable(prefix + "getVfModuleResponseCode", 200)
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 when(mockExecution.getVariable("mso.workflow.default.aai.v8.generic-vnf.uri")).thenReturn("/aai/v8/network/generic-vnfs/generic-vnf")
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')
92 StubResponseAAI.MockAAIVfModule()
94 UpdateAAIVfModule obj = new UpdateAAIVfModule()
95 obj.getVfModule(mockExecution)
96 } catch (Exception ex) {
97 println " Test End - Handle catch-throw BpmnError()! "
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())
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 when(mockExecution.getVariable("mso.workflow.default.aai.v8.generic-vnf.uri")).thenReturn("/aai/v8/network/generic-vnfs/generic-vnf")
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')
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)
124 verify(mockExecution).setVariable(prefix + "updateVfModuleResponseCode", 200)
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 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.UpdateAAIVfModule.aai.version")).thenReturn('8')
139 String getVfModuleResponse = FileUtil.readResourceFile("__files/VfModularity/GenericVnf.xml")
140 when(mockExecution.getVariable(prefix + "getVfModuleResponse")).thenReturn(getVfModuleResponse)
141 StubResponseAAI.MockAAIVfModule()
143 UpdateAAIVfModule obj = new UpdateAAIVfModule()
144 obj.updateVfModule(mockExecution)
145 } catch (Exception ex) {
146 println " Test End - Handle catch-throw BpmnError()! "
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())
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)
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)