Dynamic Cloud Owner Support
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoUpdateVnfAndModulesTest.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.infrastructure.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.Assert
29 import org.junit.Before
30 import org.junit.Ignore
31 import org.junit.Rule
32 import org.junit.Test
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
40
41 import static com.github.tomakehurst.wiremock.client.WireMock.*
42 import static org.mockito.Mockito.*
43
44 @RunWith(MockitoJUnitRunner.class)
45 class DoUpdateVnfAndModulesTest {
46
47     @Captor
48     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
49
50     @Rule
51     public WireMockRule wireMockRule = new WireMockRule(28090);
52
53     @Before
54     public void init() {
55         MockitoAnnotations.initMocks(this)
56     }
57
58     @Test
59     void testQueryAAIVfModule() {
60         ExecutionEntity mockExecution = setupMock()
61         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
62         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
63         when(mockExecution.getVariable("mso.workflow.default.aai.generic-vnf.version")).thenReturn("8")
64
65         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
66         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
67
68         mockData()
69         DoUpdateVnfAndModules obj = new DoUpdateVnfAndModules()
70         obj.queryAAIVfModule(mockExecution)
71         Mockito.verify(mockExecution).setVariable("DUVAM_queryAAIVfModuleResponseCode", 200)
72     }
73
74     @Test
75         @Ignore
76     void testQueryAAIVfModuleEndpointNull() {
77         ExecutionEntity mockExecution = setupMock()
78         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
79         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
80         when(mockExecution.getVariable("mso.workflow.default.aai.generic-vnf.version")).thenReturn("8")
81
82         when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
83         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
84
85         mockData()
86         try {
87             DoUpdateVnfAndModules obj = new DoUpdateVnfAndModules()
88             obj.queryAAIVfModule(mockExecution)
89         } catch (Exception ex) {
90             println " Test End - Handle catch-throw BpmnError()! "
91         }
92         Mockito.verify(mockExecution, atLeastOnce()).setVariable(captor.capture(), captor.capture())
93         WorkflowException workflowException = captor.getValue()
94         Assert.assertEquals(1002, workflowException.getErrorCode())
95         Assert.assertEquals("AAI GET Failed:org.apache.http.client.ClientProtocolException", workflowException.getErrorMessage())
96     }
97
98     private static ExecutionEntity setupMock() {
99         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
100         when(mockProcessDefinition.getKey()).thenReturn("DoUpdateVfModule")
101         RepositoryService mockRepositoryService = mock(RepositoryService.class)
102         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
103         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoUpdateVfModule")
104         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
105         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
106         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
107
108         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
109         // Initialize prerequisite variables
110         when(mockExecution.getId()).thenReturn("100")
111         when(mockExecution.getProcessDefinitionId()).thenReturn("DoUpdateVfModule")
112         when(mockExecution.getProcessInstanceId()).thenReturn("DoUpdateVfModule")
113         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
114         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
115
116         return mockExecution
117     }
118
119     private static void mockData() {
120         stubFor(get(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/12345[?]depth=1"))
121                 .willReturn(aResponse()
122                 .withStatus(200).withHeader("Content-Type", "text/xml")
123                 .withBodyFile("DoUpdateVfModule/getGenericVnfResponse.xml")))
124     }
125
126 }