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.Before
29 import org.junit.Ignore
32 import org.junit.runner.RunWith
33 import org.mockito.ArgumentCaptor
34 import org.mockito.Captor
35 import org.mockito.Mockito
36 import org.mockito.MockitoAnnotations
37 import org.mockito.runners.MockitoJUnitRunner
38 import org.onap.so.bpmn.core.WorkflowException
39 import org.onap.so.bpmn.mock.FileUtil
41 import static com.github.tomakehurst.wiremock.client.WireMock.*
42 import static org.mockito.ArgumentMatchers.any
43 import static org.mockito.ArgumentMatchers.refEq
44 import static org.mockito.Mockito.*
46 @RunWith(MockitoJUnitRunner.class)
48 class UpdateAAIGenericVnfTest {
50 String getVfModuleResponse = FileUtil.readResourceFile("__files/VfModularity/GenericVnf.xml")
53 static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
56 void init() throws IOException {
57 MockitoAnnotations.initMocks(this);
61 void testGetGenericVnf() {
62 ExecutionEntity mockExecution = setupMock()
63 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
64 when(mockExecution.getVariable("UAAIGenVnf_vnfId")).thenReturn("skask")
66 when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
67 when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
68 when(mockExecution.getVariable("mso.workflow.custom.UpdateAAIGenericVnf.aai.version")).thenReturn('8')
70 StubResponseAAI.MockAAIVfModule()
71 UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
72 obj.getGenericVnf(mockExecution)
74 Mockito.verify(mockExecution).setVariable("UAAIGenVnf_getGenericVnfResponseCode", 200)
78 void testGetGenericVnfEndpointNull() {
79 ExecutionEntity mockExecution = setupMock()
80 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
81 when(mockExecution.getVariable("UAAIGenVnf_vnfId")).thenReturn("skask")
83 when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
84 when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
85 when(mockExecution.getVariable("mso.workflow.custom.UpdateAAIGenericVnf.aai.version")).thenReturn('8')
87 StubResponseAAI.MockAAIVfModule()
89 UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
90 obj.getGenericVnf(mockExecution)
91 } catch (Exception ex) {
92 println " Test End - Handle catch-throw BpmnError()! "
95 WorkflowException exception = new WorkflowException("UpdateAAIGenericVnf", 9999, "org.apache.http.client.ClientProtocolException")
96 Mockito.verify(mockExecution).setVariable("WorkflowException", refEq(exception, any(WorkflowException.class)))
97 Mockito.verify(mockExecution).setVariable("UAAIGenVnf_getGenericVnfResponseCode", 500)
98 Mockito.verify(mockExecution).setVariable("UAAIGenVnf_getGenericVnfResponse", "AAI GET Failed:null")
102 void testUpdateGenericVnf() {
103 ExecutionEntity mockExecution = setupMock()
104 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
105 when(mockExecution.getVariable("UAAIGenVnf_vnfId")).thenReturn("skask")
106 when(mockExecution.getVariable("UAAIGenVnf_getGenericVnfResponse")).thenReturn(getVfModuleResponse)
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.UpdateAAIGenericVnf.aai.version")).thenReturn('8')
112 stubFor(patch(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/.*"))
113 .willReturn(aResponse()
115 UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
116 obj.updateGenericVnf(mockExecution)
118 Mockito.verify(mockExecution).setVariable("UAAIGenVnf_updateGenericVnfResponseCode", 200)
119 Mockito.verify(mockExecution).setVariable("UAAIGenVnf_updateGenericVnfResponse", "")
123 void testUpdateGenericVnfEndpointNull() {
124 ExecutionEntity mockExecution = setupMock()
125 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
126 when(mockExecution.getVariable("UAAIGenVnf_vnfId")).thenReturn("skask")
127 when(mockExecution.getVariable("UAAIGenVnf_getGenericVnfResponse")).thenReturn(getVfModuleResponse)
129 when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
130 when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
131 when(mockExecution.getVariable("mso.workflow.custom.UpdateAAIGenericVnf.aai.version")).thenReturn('8')
133 stubFor(patch(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/.*"))
134 .willReturn(aResponse()
138 UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
139 obj.updateGenericVnf(mockExecution)
140 } catch (Exception ex) {
141 println " Test End - Handle catch-throw BpmnError()! "
143 WorkflowException exception = new WorkflowException("UpdateAAIGenericVnf", 9999, "org.apache.http.client.ClientProtocolException")
144 Mockito.verify(mockExecution).setVariable("WorkflowException", refEq(exception, any(WorkflowException.class)))
145 Mockito.verify(mockExecution).setVariable("UAAIGenVnf_updateGenericVnfResponseCode", 500)
146 Mockito.verify(mockExecution).setVariable("UAAIGenVnf_updateGenericVnfResponse", "AAI PATCH Failed:null")
149 private static ExecutionEntity setupMock() {
150 ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
151 when(mockProcessDefinition.getKey()).thenReturn("UpdateAAIGenericVnf")
152 RepositoryService mockRepositoryService = mock(RepositoryService.class)
153 when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
154 when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("UpdateAAIGenericVnf")
155 when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
156 ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
157 when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
159 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
160 // Initialize prerequisite variables
161 when(mockExecution.getId()).thenReturn("100")
162 when(mockExecution.getProcessDefinitionId()).thenReturn("UpdateAAIGenericVnf")
163 when(mockExecution.getProcessInstanceId()).thenReturn("UpdateAAIGenericVnf")
164 when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
165 when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)