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
41 import static com.github.tomakehurst.wiremock.client.WireMock.*
42 import static org.mockito.Mockito.mock
43 import static org.mockito.Mockito.times
44 import static org.mockito.Mockito.when
46 @RunWith(MockitoJUnitRunner.class)
48 class GenericGetVnfTest {
53 public WireMockRule wireMockRule = new WireMockRule(28090)
56 static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
62 MockitoAnnotations.initMocks(this)
66 public void testGetVnfByName() {
68 ExecutionEntity mockExecution = setupMock()
69 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
70 when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
71 when(mockExecution.getVariable(prefix+"vnfName")).thenReturn("genricVnf_test")
72 when(mockExecution.getVariable(prefix+"type")).thenReturn("generic-vnf")
73 when(mockExecution.getVariable("mso.workflow.DoCreateVfModule.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
74 when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
77 GenericGetVnf obj = new GenericGetVnf()
78 obj.getVnfByName(mockExecution)
80 Mockito.verify(mockExecution).setVariable("prefix","GENGV_")
81 Mockito.verify(mockExecution).setVariable(prefix+"getVnfPath","http://localhost:28090/aai/v9/network/generic-vnfs/generic-vnf?vnf-name=genricVnf_test&depth=1")
85 public void testGetVnfByNameEndpointNull() {
87 ExecutionEntity mockExecution = setupMock()
88 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
89 when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
90 when(mockExecution.getVariable(prefix+"vnfName")).thenReturn("genricVnf_test")
91 when(mockExecution.getVariable(prefix+"type")).thenReturn("generic-vnf")
92 when(mockExecution.getVariable("mso.workflow.DoCreateVfModule.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
93 when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
97 GenericGetVnf obj = new GenericGetVnf()
98 obj.getVnfByName(mockExecution)
99 }catch (Exception ex) {
100 println " Test End - Handle catch-throw BpmnError()! "
103 Mockito.verify(mockExecution, times(3)).setVariable(captor.capture(), captor.capture())
104 WorkflowException workflowException = captor.getValue()
105 Assert.assertEquals(9999, workflowException.getErrorCode())
106 Assert.assertEquals("org.apache.http.client.ClientProtocolException", workflowException.getErrorMessage())
110 public void testGetVnfById() {
112 ExecutionEntity mockExecution = setupMock()
113 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
114 when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
115 when(mockExecution.getVariable(prefix+"vnfId")).thenReturn("genricVnf_test")
116 when(mockExecution.getVariable(prefix+"type")).thenReturn("generic-vnf")
117 when(mockExecution.getVariable("mso.workflow.DoCreateVfModule.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
118 when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
121 GenericGetVnf obj = new GenericGetVnf()
122 obj.getVnfById(mockExecution)
124 Mockito.verify(mockExecution).setVariable("prefix","GENGV_")
125 Mockito.verify(mockExecution).setVariable(prefix+"getVnfPath","http://localhost:28090/aai/v9/network/generic-vnfs/generic-vnf/genricVnf_test?depth=1")
129 public void testGetVnfByIdEndpointNull() {
131 ExecutionEntity mockExecution = setupMock()
132 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
133 when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
134 when(mockExecution.getVariable(prefix+"vnfId")).thenReturn("genricVnf_test")
135 when(mockExecution.getVariable(prefix+"type")).thenReturn("generic-vnf")
136 when(mockExecution.getVariable("mso.workflow.DoCreateVfModule.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
137 when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
141 GenericGetVnf obj = new GenericGetVnf()
142 obj.getVnfById(mockExecution)
143 }catch (Exception ex) {
144 println " Test End - Handle catch-throw BpmnError()! "
147 Mockito.verify(mockExecution, times(3)).setVariable(captor.capture(), captor.capture())
148 WorkflowException workflowException = captor.getValue()
149 Assert.assertEquals(9999, workflowException.getErrorCode())
150 Assert.assertEquals("org.apache.http.client.ClientProtocolException", workflowException.getErrorMessage())
153 private static ExecutionEntity setupMock() {
154 ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
155 when(mockProcessDefinition.getKey()).thenReturn("DoCreateVfModule")
156 RepositoryService mockRepositoryService = mock(RepositoryService.class)
157 when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
158 when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoCreateVfModule")
159 when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
160 ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
161 when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
163 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
164 // Initialize prerequisite variables
165 when(mockExecution.getId()).thenReturn("100")
166 when(mockExecution.getProcessDefinitionId()).thenReturn("DoCreateVfModule")
167 when(mockExecution.getProcessInstanceId()).thenReturn("DoCreateVfModule")
168 when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
169 when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
174 private static void mockData() {
175 stubFor(get(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf.*"))
176 .willReturn(aResponse()
177 .withStatus(200).withHeader("Content-Type", "text/xml")
178 .withBodyFile("GenericFlows/getGenericVnfResponse.xml")))