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