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
40 import org.onap.so.bpmn.mock.StubResponseAAI
42 import static com.github.tomakehurst.wiremock.client.WireMock.*
43 import static org.mockito.Matchers.any
44 import static org.mockito.Matchers.refEq
45 import static org.mockito.Mockito.*
47 @RunWith(MockitoJUnitRunner.class)
49 class UpdateAAIGenericVnfTest {
51 String getVfModuleResponse = FileUtil.readResourceFile("__files/VfModularity/GenericVnf.xml")
53 public WireMockRule wireMockRule = new WireMockRule(28090)
56 static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
59 void init() throws IOException {
60 MockitoAnnotations.initMocks(this);
64 void testGetGenericVnf() {
65 ExecutionEntity mockExecution = setupMock()
66 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
67 when(mockExecution.getVariable("UAAIGenVnf_vnfId")).thenReturn("skask")
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.UpdateAAIGenericVnf.aai.version")).thenReturn('8')
73 StubResponseAAI.MockAAIVfModule()
74 UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
75 obj.getGenericVnf(mockExecution)
77 Mockito.verify(mockExecution).setVariable("UAAIGenVnf_getGenericVnfResponseCode", 200)
81 void testGetGenericVnfEndpointNull() {
82 ExecutionEntity mockExecution = setupMock()
83 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
84 when(mockExecution.getVariable("UAAIGenVnf_vnfId")).thenReturn("skask")
86 when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
87 when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
88 when(mockExecution.getVariable("mso.workflow.custom.UpdateAAIGenericVnf.aai.version")).thenReturn('8')
90 StubResponseAAI.MockAAIVfModule()
92 UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
93 obj.getGenericVnf(mockExecution)
94 } catch (Exception ex) {
95 println " Test End - Handle catch-throw BpmnError()! "
98 WorkflowException exception = new WorkflowException("UpdateAAIGenericVnf", 9999, "org.apache.http.client.ClientProtocolException")
99 Mockito.verify(mockExecution).setVariable("WorkflowException", refEq(exception, any(WorkflowException.class)))
100 Mockito.verify(mockExecution).setVariable("UAAIGenVnf_getGenericVnfResponseCode", 500)
101 Mockito.verify(mockExecution).setVariable("UAAIGenVnf_getGenericVnfResponse", "AAI GET Failed:null")
105 void testUpdateGenericVnf() {
106 ExecutionEntity mockExecution = setupMock()
107 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
108 when(mockExecution.getVariable("UAAIGenVnf_vnfId")).thenReturn("skask")
109 when(mockExecution.getVariable("UAAIGenVnf_getGenericVnfResponse")).thenReturn(getVfModuleResponse)
111 when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
112 when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
113 when(mockExecution.getVariable("mso.workflow.custom.UpdateAAIGenericVnf.aai.version")).thenReturn('8')
115 stubFor(patch(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/.*"))
116 .willReturn(aResponse()
118 UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
119 obj.updateGenericVnf(mockExecution)
121 Mockito.verify(mockExecution).setVariable("UAAIGenVnf_updateGenericVnfResponseCode", 200)
122 Mockito.verify(mockExecution).setVariable("UAAIGenVnf_updateGenericVnfResponse", "")
126 void testUpdateGenericVnfEndpointNull() {
127 ExecutionEntity mockExecution = setupMock()
128 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
129 when(mockExecution.getVariable("UAAIGenVnf_vnfId")).thenReturn("skask")
130 when(mockExecution.getVariable("UAAIGenVnf_getGenericVnfResponse")).thenReturn(getVfModuleResponse)
132 when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
133 when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
134 when(mockExecution.getVariable("mso.workflow.custom.UpdateAAIGenericVnf.aai.version")).thenReturn('8')
136 stubFor(patch(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/.*"))
137 .willReturn(aResponse()
141 UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
142 obj.updateGenericVnf(mockExecution)
143 } catch (Exception ex) {
144 println " Test End - Handle catch-throw BpmnError()! "
146 WorkflowException exception = new WorkflowException("UpdateAAIGenericVnf", 9999, "org.apache.http.client.ClientProtocolException")
147 Mockito.verify(mockExecution).setVariable("WorkflowException", refEq(exception, any(WorkflowException.class)))
148 Mockito.verify(mockExecution).setVariable("UAAIGenVnf_updateGenericVnfResponseCode", 500)
149 Mockito.verify(mockExecution).setVariable("UAAIGenVnf_updateGenericVnfResponse", "AAI PATCH Failed:null")
152 private static ExecutionEntity setupMock() {
153 ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
154 when(mockProcessDefinition.getKey()).thenReturn("UpdateAAIGenericVnf")
155 RepositoryService mockRepositoryService = mock(RepositoryService.class)
156 when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
157 when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("UpdateAAIGenericVnf")
158 when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
159 ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
160 when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
162 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
163 // Initialize prerequisite variables
164 when(mockExecution.getId()).thenReturn("100")
165 when(mockExecution.getProcessDefinitionId()).thenReturn("UpdateAAIGenericVnf")
166 when(mockExecution.getProcessInstanceId()).thenReturn("UpdateAAIGenericVnf")
167 when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
168 when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)