5641c5e12f75fc148433246aefb892b6e3a8052a
[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 import org.onap.so.bpmn.mock.StubResponseAAI
41
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.*
46
47 @RunWith(MockitoJUnitRunner.class)
48 @Ignore
49 class UpdateAAIGenericVnfTest {
50
51     String getVfModuleResponse = FileUtil.readResourceFile("__files/VfModularity/GenericVnf.xml")
52     @Rule
53     public WireMockRule wireMockRule = new WireMockRule(28090)
54
55     @Captor
56     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
57
58     @Before
59     void init() throws IOException {
60         MockitoAnnotations.initMocks(this);
61     }
62
63     @Test
64     void testGetGenericVnf() {
65         ExecutionEntity mockExecution = setupMock()
66         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
67         when(mockExecution.getVariable("UAAIGenVnf_vnfId")).thenReturn("skask")
68
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')
72
73         StubResponseAAI.MockAAIVfModule()
74         UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
75         obj.getGenericVnf(mockExecution)
76
77         Mockito.verify(mockExecution).setVariable("UAAIGenVnf_getGenericVnfResponseCode", 200)
78     }
79
80     @Test
81     void testGetGenericVnfEndpointNull() {
82         ExecutionEntity mockExecution = setupMock()
83         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
84         when(mockExecution.getVariable("UAAIGenVnf_vnfId")).thenReturn("skask")
85
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')
89
90         StubResponseAAI.MockAAIVfModule()
91         try {
92             UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
93             obj.getGenericVnf(mockExecution)
94         } catch (Exception ex) {
95             println " Test End - Handle catch-throw BpmnError()! "
96         }
97
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")
102     }
103
104     @Test
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)
110
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')
114
115         stubFor(patch(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/.*"))
116                 .willReturn(aResponse()
117                 .withStatus(200)))
118         UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
119         obj.updateGenericVnf(mockExecution)
120
121         Mockito.verify(mockExecution).setVariable("UAAIGenVnf_updateGenericVnfResponseCode", 200)
122         Mockito.verify(mockExecution).setVariable("UAAIGenVnf_updateGenericVnfResponse", "")
123     }
124
125     @Test
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)
131
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')
135
136         stubFor(patch(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/.*"))
137                 .willReturn(aResponse()
138                 .withStatus(200)))
139
140         try {
141             UpdateAAIGenericVnf obj = new UpdateAAIGenericVnf()
142             obj.updateGenericVnf(mockExecution)
143         } catch (Exception ex) {
144             println " Test End - Handle catch-throw BpmnError()! "
145         }
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")
150     }
151
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)
161
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)
169
170         return mockExecution
171     }
172 }