replace all fixed wiremock ports
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / SniroHomingV1Test.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.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 import org.onap.so.bpmn.core.domain.AllottedResource
41 import org.onap.so.bpmn.core.domain.ModelInfo
42 import org.onap.so.bpmn.core.domain.ServiceDecomposition
43 import org.onap.so.bpmn.core.domain.VnfResource
44
45 import static com.github.tomakehurst.wiremock.client.WireMock.*
46 import static org.mockito.Mockito.*
47
48 @RunWith(MockitoJUnitRunner.class)
49 @Ignore
50 class SniroHomingV1Test {
51     String subsInfo = "{\"globalSubscriberId\": \"SUB12_0322_DS_1201\",\"subscriberCommonSiteId\": \"DALTX0101\",\"subscriberName\": \"SUB_12_0322_DS_1201\"}"
52     ServiceDecomposition serviceDecomp
53
54     {
55         serviceDecomp = new ServiceDecomposition("{\"serviceResources\":{}}", "123")
56         ModelInfo modelInfo = new ModelInfo()
57         serviceDecomp.modelInfo = modelInfo
58
59         AllottedResource allottedResource = new AllottedResource()
60         allottedResource.setModelInfo(modelInfo)
61         List allottedResourceList = new ArrayList()
62         allottedResourceList.add(allottedResource)
63
64         VnfResource vnfResource = new VnfResource()
65         vnfResource.setModelInfo(modelInfo)
66         List vnfResourceList = new ArrayList()
67         vnfResourceList.add(vnfResource)
68
69         serviceDecomp.serviceAllottedResources = allottedResourceList
70         serviceDecomp.setVnfResources(vnfResourceList)
71     }
72
73     @Captor
74     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
75
76
77     @Before
78     public void init() {
79         MockitoAnnotations.initMocks(this)
80     }
81
82     @Test
83     public void testCallSniro() {
84         ExecutionEntity mockExecution = setupMock()
85         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
86         when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
87         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
88         when(mockExecution.getVariable("subscriberInfo")).thenReturn(subsInfo)
89         when(mockExecution.getVariable("serviceDecomposition")).thenReturn(serviceDecomp)
90         when(mockExecution.getVariable("mso.sniro.auth")).thenReturn("3141634BF7E070AA289CF2892C986C0B")
91         when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7")
92         when(mockExecution.getVariable("mso.adapters.workflow.message.endpoint")).thenReturn('http://localhost:18080/workflows/messages/message/')
93         when(mockExecution.getVariable("mso.service.agnostic.sniro.endpoint")).thenReturn("/sniro")
94         when(mockExecution.getVariable("mso.service.agnostic.sniro.host")).thenReturn("http://localhost:28090")
95         when(mockExecution.getVariable("mso.workflow.GenericPutVnf.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
96         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
97
98         mockData()
99
100         SniroHomingV1 obj = new SniroHomingV1()
101         obj.callSniro(mockExecution)
102
103         Mockito.verify(mockExecution, times(10)).setVariable(captor.capture(), captor.capture())
104         Assert.assertEquals(200, captor.getAllValues().get(17))
105
106     }
107
108     @Test
109     public void testCallSniroMissingAuth() {
110         ExecutionEntity mockExecution = setupMock()
111         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
112         when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
113         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
114         when(mockExecution.getVariable("subscriberInfo")).thenReturn(subsInfo)
115         when(mockExecution.getVariable("serviceDecomposition")).thenReturn(serviceDecomp)
116         when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7")
117         when(mockExecution.getVariable("mso.adapters.workflow.message.endpoint")).thenReturn('http://localhost:18080/workflows/messages/message/')
118         when(mockExecution.getVariable("mso.service.agnostic.sniro.endpoint")).thenReturn("/sniro")
119         when(mockExecution.getVariable("mso.service.agnostic.sniro.host")).thenReturn("http://localhost:28090")
120         when(mockExecution.getVariable("mso.workflow.GenericPutVnf.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
121         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
122
123         mockData()
124
125         try {
126             SniroHomingV1 obj = new SniroHomingV1()
127             obj.callSniro(mockExecution)
128         } catch (Exception ex) {
129             println " Test End - Handle catch-throw BpmnError()! "
130         }
131         Mockito.verify(mockExecution, times(4)).setVariable(captor.capture(), captor.capture())
132         WorkflowException workflowException = captor.getValue()
133         Assert.assertEquals(401, workflowException.getErrorCode())
134         Assert.assertEquals("Internal Error - BasicAuth value null", workflowException.getErrorMessage())
135     }
136
137     @Test
138     public void testCallSniroHostNull() {
139         ExecutionEntity mockExecution = setupMock()
140         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
141         when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
142         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
143         when(mockExecution.getVariable("subscriberInfo")).thenReturn(subsInfo)
144         when(mockExecution.getVariable("serviceDecomposition")).thenReturn(serviceDecomp)
145         when(mockExecution.getVariable("mso.sniro.auth")).thenReturn("3141634BF7E070AA289CF2892C986C0B")
146         when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7")
147         when(mockExecution.getVariable("mso.adapters.workflow.message.endpoint")).thenReturn('http://localhost:18080/workflows/messages/message/')
148         when(mockExecution.getVariable("mso.service.agnostic.sniro.endpoint")).thenReturn("/sniro")
149         when(mockExecution.getVariable("mso.service.agnostic.sniro.host")).thenReturn(null)
150         when(mockExecution.getVariable("mso.workflow.GenericPutVnf.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
151         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
152
153         mockData()
154         try {
155             SniroHomingV1 obj = new SniroHomingV1()
156             obj.callSniro(mockExecution)
157         } catch (Exception ex) {
158             println " Test End - Handle catch-throw BpmnError()! "
159         }
160         Mockito.verify(mockExecution, times(9)).setVariable(captor.capture(), captor.capture())
161         WorkflowException workflowException = captor.getValue()
162         Assert.assertEquals(2500, workflowException.getErrorCode())
163         Assert.assertEquals("Internal Error - Occured in Homing CallSniro: org.apache.http.client.ClientProtocolException", workflowException.getErrorMessage())
164     }
165
166     private static ExecutionEntity setupMock() {
167         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
168         when(mockProcessDefinition.getKey()).thenReturn("Homing")
169         RepositoryService mockRepositoryService = mock(RepositoryService.class)
170         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
171         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("Homing")
172         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
173         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
174         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
175
176         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
177         // Initialize prerequisite variables
178         when(mockExecution.getId()).thenReturn("100")
179         when(mockExecution.getProcessDefinitionId()).thenReturn("Homing")
180         when(mockExecution.getProcessInstanceId()).thenReturn("Homing")
181         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
182         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
183
184         return mockExecution
185     }
186
187     private static void mockData() {
188         stubFor(post(urlMatching(".*/sniro"))
189                 .willReturn(aResponse()
190                 .withStatus(200)
191                 .withBody("")))
192     }
193 }