replace all fixed wiremock ports
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / UpdateAAIGenericVnfTest.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.Before
29 import org.junit.Ignore
30 import org.junit.Rule
31 import org.junit.Test
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
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.*
45
46 @RunWith(MockitoJUnitRunner.class)
47 @Ignore
48 class UpdateAAIGenericVnfTest {
49
50     String getVfModuleResponse = FileUtil.readResourceFile("__files/VfModularity/GenericVnf.xml")
51
52     @Captor
53     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
54
55     @Before
56     void init() throws IOException {
57         MockitoAnnotations.initMocks(this);
58     }
59
60     @Test
61     void testGetGenericVnf() {
62         ExecutionEntity mockExecution = setupMock()
63         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
64         when(mockExecution.getVariable("UAAIGenVnf_vnfId")).thenReturn("skask")
65
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')
69
70         StubResponseAAI.MockAAIVfModule()
71         UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
72         obj.getGenericVnf(mockExecution)
73
74         Mockito.verify(mockExecution).setVariable("UAAIGenVnf_getGenericVnfResponseCode", 200)
75     }
76
77     @Test
78     void testGetGenericVnfEndpointNull() {
79         ExecutionEntity mockExecution = setupMock()
80         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
81         when(mockExecution.getVariable("UAAIGenVnf_vnfId")).thenReturn("skask")
82
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')
86
87         StubResponseAAI.MockAAIVfModule()
88         try {
89             UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
90             obj.getGenericVnf(mockExecution)
91         } catch (Exception ex) {
92             println " Test End - Handle catch-throw BpmnError()! "
93         }
94
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")
99     }
100
101     @Test
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)
107
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')
111
112         stubFor(patch(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/.*"))
113                 .willReturn(aResponse()
114                 .withStatus(200)))
115         UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
116         obj.updateGenericVnf(mockExecution)
117
118         Mockito.verify(mockExecution).setVariable("UAAIGenVnf_updateGenericVnfResponseCode", 200)
119         Mockito.verify(mockExecution).setVariable("UAAIGenVnf_updateGenericVnfResponse", "")
120     }
121
122     @Test
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)
128
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')
132
133         stubFor(patch(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/.*"))
134                 .willReturn(aResponse()
135                 .withStatus(200)))
136
137         try {
138             UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
139             obj.updateGenericVnf(mockExecution)
140         } catch (Exception ex) {
141             println " Test End - Handle catch-throw BpmnError()! "
142         }
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")
147     }
148
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)
158
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)
166
167         return mockExecution
168     }
169 }