b093aaa491cfec710332e7884490ba1ef44153d1
[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     @Rule
74     public WireMockRule wireMockRule = new WireMockRule(28090)
75
76     @Captor
77     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
78
79
80     @Before
81     public void init() {
82         MockitoAnnotations.initMocks(this)
83     }
84
85     @Test
86     public void testCallSniro() {
87         ExecutionEntity mockExecution = setupMock()
88         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
89         when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
90         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
91         when(mockExecution.getVariable("subscriberInfo")).thenReturn(subsInfo)
92         when(mockExecution.getVariable("serviceDecomposition")).thenReturn(serviceDecomp)
93         when(mockExecution.getVariable("mso.sniro.auth")).thenReturn("3141634BF7E070AA289CF2892C986C0B")
94         when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7")
95         when(mockExecution.getVariable("mso.adapters.workflow.message.endpoint")).thenReturn('http://localhost:18080/workflows/messages/message/')
96         when(mockExecution.getVariable("mso.service.agnostic.sniro.endpoint")).thenReturn("/sniro")
97         when(mockExecution.getVariable("mso.service.agnostic.sniro.host")).thenReturn("http://localhost:28090")
98         when(mockExecution.getVariable("mso.workflow.GenericPutVnf.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
99         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
100
101         mockData()
102
103         SniroHomingV1 obj = new SniroHomingV1()
104         obj.callSniro(mockExecution)
105
106         Mockito.verify(mockExecution, times(10)).setVariable(captor.capture(), captor.capture())
107         Assert.assertEquals(200, captor.getAllValues().get(17))
108
109     }
110
111     @Test
112     public void testCallSniroMissingAuth() {
113         ExecutionEntity mockExecution = setupMock()
114         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
115         when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
116         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
117         when(mockExecution.getVariable("subscriberInfo")).thenReturn(subsInfo)
118         when(mockExecution.getVariable("serviceDecomposition")).thenReturn(serviceDecomp)
119         when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7")
120         when(mockExecution.getVariable("mso.adapters.workflow.message.endpoint")).thenReturn('http://localhost:18080/workflows/messages/message/')
121         when(mockExecution.getVariable("mso.service.agnostic.sniro.endpoint")).thenReturn("/sniro")
122         when(mockExecution.getVariable("mso.service.agnostic.sniro.host")).thenReturn("http://localhost:28090")
123         when(mockExecution.getVariable("mso.workflow.GenericPutVnf.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
124         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
125
126         mockData()
127
128         try {
129             SniroHomingV1 obj = new SniroHomingV1()
130             obj.callSniro(mockExecution)
131         } catch (Exception ex) {
132             println " Test End - Handle catch-throw BpmnError()! "
133         }
134         Mockito.verify(mockExecution, times(4)).setVariable(captor.capture(), captor.capture())
135         WorkflowException workflowException = captor.getValue()
136         Assert.assertEquals(401, workflowException.getErrorCode())
137         Assert.assertEquals("Internal Error - BasicAuth value null", workflowException.getErrorMessage())
138     }
139
140     @Test
141     public void testCallSniroHostNull() {
142         ExecutionEntity mockExecution = setupMock()
143         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
144         when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
145         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
146         when(mockExecution.getVariable("subscriberInfo")).thenReturn(subsInfo)
147         when(mockExecution.getVariable("serviceDecomposition")).thenReturn(serviceDecomp)
148         when(mockExecution.getVariable("mso.sniro.auth")).thenReturn("3141634BF7E070AA289CF2892C986C0B")
149         when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7")
150         when(mockExecution.getVariable("mso.adapters.workflow.message.endpoint")).thenReturn('http://localhost:18080/workflows/messages/message/')
151         when(mockExecution.getVariable("mso.service.agnostic.sniro.endpoint")).thenReturn("/sniro")
152         when(mockExecution.getVariable("mso.service.agnostic.sniro.host")).thenReturn(null)
153         when(mockExecution.getVariable("mso.workflow.GenericPutVnf.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
154         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
155
156         mockData()
157         try {
158             SniroHomingV1 obj = new SniroHomingV1()
159             obj.callSniro(mockExecution)
160         } catch (Exception ex) {
161             println " Test End - Handle catch-throw BpmnError()! "
162         }
163         Mockito.verify(mockExecution, times(9)).setVariable(captor.capture(), captor.capture())
164         WorkflowException workflowException = captor.getValue()
165         Assert.assertEquals(2500, workflowException.getErrorCode())
166         Assert.assertEquals("Internal Error - Occured in Homing CallSniro: org.apache.http.client.ClientProtocolException", workflowException.getErrorMessage())
167     }
168
169     private static ExecutionEntity setupMock() {
170         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
171         when(mockProcessDefinition.getKey()).thenReturn("Homing")
172         RepositoryService mockRepositoryService = mock(RepositoryService.class)
173         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
174         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("Homing")
175         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
176         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
177         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
178
179         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
180         // Initialize prerequisite variables
181         when(mockExecution.getId()).thenReturn("100")
182         when(mockExecution.getProcessDefinitionId()).thenReturn("Homing")
183         when(mockExecution.getProcessInstanceId()).thenReturn("Homing")
184         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
185         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
186
187         return mockExecution
188     }
189
190     private static void mockData() {
191         stubFor(post(urlMatching(".*/sniro"))
192                 .willReturn(aResponse()
193                 .withStatus(200)
194                 .withBody("")))
195     }
196 }