Containerization feature of SO
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / GenericPutServiceTest.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 static org.mockito.Mockito.*
24 import static org.onap.so.bpmn.mock.StubResponseAAI.MockPutServiceInstance;
25
26 import org.camunda.bpm.engine.ProcessEngineServices
27 import org.camunda.bpm.engine.RepositoryService
28 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
29 import org.camunda.bpm.engine.repository.ProcessDefinition
30 import org.junit.Before
31 import org.junit.Ignore
32 import org.junit.Rule
33 import org.junit.Test
34 import org.junit.runner.RunWith
35 import org.mockito.MockitoAnnotations
36 import org.mockito.internal.debugging.MockitoDebuggerImpl
37 import org.mockito.runners.MockitoJUnitRunner
38
39 import com.github.tomakehurst.wiremock.client.WireMock
40 import com.github.tomakehurst.wiremock.junit.WireMockRule
41 import org.apache.commons.lang3.*
42
43 @RunWith(MockitoJUnitRunner.class)
44 @Ignore
45 class GenericPutServiceTest {
46
47         @Rule
48         public WireMockRule wireMockRule = new WireMockRule(8090);
49
50         @Before
51         public void init()
52         {
53                 MockitoAnnotations.initMocks(this)
54
55         }
56
57         @Test
58         public void preProcessRequest() {
59
60
61                 println "************ preProcessRequest ************* "
62
63                 ExecutionEntity mockExecution = setupMock()
64
65                 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
66                 when(mockExecution.getVariable("GENPS_globalSubscriberId")).thenReturn("1604-MVM-26")
67                 when(mockExecution.getVariable("GENPS_serviceInstanceId")).thenReturn("MIS%2F1604%2F0026%2FSW_INTERNET")
68                 when(mockExecution.getVariable("GENPS_serviceType")).thenReturn("SDN-ETHERNET-INTERNET")
69                 when(mockExecution.getVariable("GENPS_serviceInstanceData")).thenReturn("f70e927b-6087-4974-9ef8-c5e4d5847ca4")
70                 when(mockExecution.getVariable("GENPS_type")).thenReturn("service-instance")
71
72                 GenericPutService  putServiceInstance= new GenericPutService()
73                 putServiceInstance.preProcessRequest(mockExecution)
74
75                 // check the sequence of variable invocation
76                 //MockitoDebuggerImpl preDebugger = new MockitoDebuggerImpl()
77                 //preDebugger.printInvocations(mockExecution)
78
79                 verify(mockExecution, atLeast(1)).getVariable("isDebugLogEnabled")
80                 verify(mockExecution).setVariable("prefix", "GENPS_")
81
82         //      execution.getVariable("isDebugLogEnabled")
83
84                 verify(mockExecution).setVariable("GENPS_SuccessIndicator", false)
85         //      verify(mockExecution).setVariable("globalSubscriberId", "1604-MVM-26")
86         //      verify(mockExecution).setVariable("serviceInstanceId", "MIS%2F1604%2F0026%2FSW_INTERNET")
87         //      verify(mockExecution).setVariable("serviceType", "SDN-ETHERNET-INTERNET")
88         //      verify(mockExecution).setVariable("ServiceInstanceData", "f70e927b-6087-4974-9ef8-c5e4d5847ca4")
89
90
91         }
92
93
94         @Test
95         @Ignore
96         public void putServiceInstance() {
97                 println "************ putServiceInstance ************* "
98
99                 WireMock.reset();
100
101                 MockPutServiceInstance("1604-MVM-26", "SDN-ETHERNET-INTERNET", "MIS%252F1604%252F0026%252FSW_INTERNET", "GenericPutServiceInstance/GenericPutServiceInstance_PutServiceInstance_AAIResponse_Success.xml");
102                 ExecutionEntity mockExecution = setupMock()
103
104                 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
105                 when(mockExecution.getVariable("GENPS_globalSubscriberId")).thenReturn("1604-MVM-26")
106                 when(mockExecution.getVariable("GENPS_serviceInstanceId")).thenReturn("MIS%2F1604%2F0026%2FSW_INTERNET")
107                 when(mockExecution.getVariable("GENPS_serviceType")).thenReturn("SDN-ETHERNET-INTERNET")
108                 when(mockExecution.getVariable("GENPS_serviceInstanceData")).thenReturn("f70e927b-6087-4974-9ef8-c5e4d5847ca4")
109                 when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:8090")
110                 when(mockExecution.getVariable("mso.workflow.PutServiceInstance.aai.business.customer.uri")).thenReturn("/aai/v7/business/customers/customer")
111                 when(mockExecution.getVariable("GENPS_serviceInstanceData")).thenReturn("f70e927b-6087-4974-9ef8-c5e4d5847ca4")
112                 when(mockExecution.getVariable("GENPS_type")).thenReturn("service-instance")
113                 when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn('http://org.openecomp.aai.inventory/')
114                 when(mockExecution.getVariable("mso.workflow.global.default.aai.version")).thenReturn("7")
115                 when(mockExecution.getVariable("mso.workflow.default.aai.v7.customer.uri")).thenReturn("/aai/v7/business/customers/customer")
116                 when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7")
117                 when(mockExecution.getVariable("aai.auth")).thenReturn("757A94191D685FD2092AC1490730A4FC")
118
119                 GenericPutService  serviceInstance= new GenericPutService()
120                 serviceInstance.putServiceInstance(mockExecution)
121
122                 // check the sequence of variable invocation
123                 MockitoDebuggerImpl preDebugger = new MockitoDebuggerImpl()
124                 preDebugger.printInvocations(mockExecution)
125
126                 verify(mockExecution, atLeast(1)).getVariable("isDebugLogEnabled")
127                 verify(mockExecution).setVariable("prefix", "GENPS_")
128
129         //      execution.getVariable("isDebugLogEnabled")
130         //      verify(mockExecution).setVariable("GENPSI_serviceInstanceData","f70e927b-6087-4974-9ef8-c5e4d5847ca4")
131
132                 String servicePayload = """<service-instance xmlns="http://org.openecomp.aai.inventory/v7">f70e927b-6087-4974-9ef8-c5e4d5847ca4</service-instance>""" as String
133                 verify(mockExecution).setVariable("GENPS_serviceInstancePayload",servicePayload)
134
135                 String serviceAaiPath = "http://localhost:28090/aai/v7/business/customers/customer/1604-MVM-26/service-subscriptions/service-subscription/SDN-ETHERNET-INTERNET/service-instances/service-instance/MIS%252F1604%252F0026%252FSW_INTERNET"
136                 verify(mockExecution).setVariable("GENPS_putServiceInstanceAaiPath", serviceAaiPath)
137
138                 int responseCode = 200
139                 verify(mockExecution).setVariable("GENPS_putServiceInstanceResponseCode", responseCode)
140
141                 String aaiResponse = """<rest:RESTResponse xmlns:rest="http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd"
142                    statusCode="200">
143    <rest:headers>
144       <rest:header name="Date" value="Thu,10 Mar 2016 00:01:18 GMT"/>
145       <rest:header name="Content-Length" value="0"/>
146       <rest:header name="Expires" value="Thu,01 Jan 1970 00:00:00 UTC"/>
147       <rest:header name="X-AAI-TXID" value="mtcnjv9aaas03-20160310-00:01:18:551-132672"/>
148       <rest:header name="Server" value="Apache-Coyote/1.1"/>
149       <rest:header name="Cache-Control" value="private"/>
150    </rest:headers>
151 </rest:RESTResponse>"""
152
153                 verify(mockExecution).setVariable("GENPS_putServiceInstanceResponse", aaiResponse)
154
155                 verify(mockExecution).setVariable("GENPS_SuccessIndicator", true)
156         }
157
158         @Test
159         @Ignore
160         public void putServiceInstance_404() {
161
162
163                 println "************ putServiceInstance ************* "
164
165                 WireMock.reset();
166
167                 ExecutionEntity mockExecution = setupMock()
168
169                 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
170                 when(mockExecution.getVariable("GENPS_globalSubscriberId")).thenReturn("1604-MVM-26")
171                 when(mockExecution.getVariable("GENPS_serviceInstanceId")).thenReturn("MIS%2F1604%2F0026%2FSW_INTERNET")
172                 when(mockExecution.getVariable("GENPS_serviceType")).thenReturn("SDN-ETHERNET-INTERNET")
173                 when(mockExecution.getVariable("GENPS_ServiceInstanceData")).thenReturn("f70e927b-6087-4974-9ef8-c5e4d5847ca4")
174                 when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:8090")
175                 when(mockExecution.getVariable("mso.workflow.PutServiceInstance.aai.business.customer_uri")).thenReturn("/aai/v7/business/customers/customer")
176                 when(mockExecution.getVariable("GENPS_ServiceInstanceData")).thenReturn("f70e927b-6087-4974-9ef8-c5e4d5847ca4")
177
178                 GenericPutService  serviceInstance= new GenericPutService()
179                 serviceInstance.putServiceInstance(mockExecution)
180
181                 // check the sequence of variable invocation
182                 MockitoDebuggerImpl preDebugger = new MockitoDebuggerImpl()
183                 preDebugger.printInvocations(mockExecution)
184
185                 verify(mockExecution, atLeast(1)).getVariable("isDebugLogEnabled")
186                 verify(mockExecution).setVariable("prefix", "GENPS_")
187
188         //      execution.getVariable("isDebugLogEnabled")
189
190
191                 verify(mockExecution).setVariable("GENPS_serviceInstanceData","f70e927b-6087-4974-9ef8-c5e4d5847ca4")
192
193                 String serviceInstancepayload = """<service-instance xmlns="http://org.onap.so.aai.inventory/v7">f70e927b-6087-4974-9ef8-c5e4d5847ca4
194                         </service-instance>""" as String
195                 verify(mockExecution).setVariable("GENPS_serviceInstancePayload",serviceInstancepayload)
196
197                 String serviceInstanceAaiPath = "http://localhost:8090/aai/v7/business/customers/customer/1604-MVM-26/service-subscriptions/service-subscription/SDN-ETHERNET-INTERNET/service-instances/service-instance/MIS%252F1604%252F0026%252FSW_INTERNET"
198                 verify(mockExecution).setVariable("GENPS_putServiceInstanceAaiPath", serviceInstanceAaiPath)
199
200                 int responseCode = 404
201                 verify(mockExecution).setVariable("GENPS_putServiceInstanceResponseCode", responseCode)
202
203                 String aaiResponse = ""
204                 verify(mockExecution).setVariable("GENPS_putServiceInstanceResponse", aaiResponse)
205
206                 verify(mockExecution).setVariable("GENPS_SuccessIndicator", false)
207
208
209         }
210
211
212         private ExecutionEntity setupMock() {
213
214                 ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
215                 when(mockProcessDefinition.getKey()).thenReturn("PutServiceInstance")
216                 RepositoryService mockRepositoryService = mock(RepositoryService.class)
217                 when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
218                 when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("PutServiceInstance")
219                 when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
220                 ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
221                 when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
222
223                 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
224                 // Initialize prerequisite variables
225
226                 when(mockExecution.getId()).thenReturn("100")
227                 when(mockExecution.getProcessDefinitionId()).thenReturn("PutServiceInstance")
228                 when(mockExecution.getProcessInstanceId()).thenReturn("PutServiceInstance")
229                 when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
230                 when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
231
232                 return mockExecution
233         }
234
235 }