Containerization feature of SO
[so.git] / bpmn / so-bpmn-infrastructure-flows / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoDeleteServiceInstanceTest.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 import org.onap.so.bpmn.mock.FileUtil
41 import org.onap.so.bpmn.vcpe.scripts.GroovyTestBase
42
43 import static com.github.tomakehurst.wiremock.client.WireMock.*
44 import static org.mockito.Mockito.*
45
46 @RunWith(MockitoJUnitRunner.class)
47 class DoDeleteServiceInstanceTest {
48
49     @Rule
50     public WireMockRule wireMockRule = new WireMockRule(28090)
51
52     @Captor
53     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
54
55     @Before
56     void init() throws IOException {
57         MockitoAnnotations.initMocks(this);
58     }
59
60     @Test
61     public void preProcessRequestTest() {
62
63         ExecutionEntity mex = setupMock()
64         when(mex.getVariable(GroovyTestBase.DBGFLAG)).thenReturn("true")
65         when(mex.getVariable("serviceInstanceId")).thenReturn("e151059a-d924-4629-845f-264db19e50b4")
66         when(mex.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("/mso/sdncadapter/")
67         when(mex.getVariable("globalSubscriberId")).thenReturn("4993921112123")
68
69         DoDeleteServiceInstance instance = new DoDeleteServiceInstance()
70         instance.preProcessRequest(mex)
71
72         Mockito.verify(mex).setVariable("sdncCallbackUrl", "/mso/sdncadapter/")
73         Mockito.verify(mex).setVariable("siParamsXml", "")
74     }
75
76    
77     @Test
78     public void testPostProcessAAIGET() {
79         ExecutionEntity mockExecution = setupMock()
80         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn('true')
81         when(mockExecution.getVariable("aai.endpoint")).thenReturn('http://localhost:8090')
82         when(mockExecution.getVariable("GENGS_FoundIndicator")).thenReturn(true)
83         when(mockExecution.getVariable("sdnc.si.svc.types")).thenReturn("")
84         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("globalSubscriberId_test")
85         when(mockExecution.getVariable("subscriptionServiceType")).thenReturn("subscriptionServiceType_test")
86
87         String aaiGetResponse = FileUtil.readResourceFile("__files/GenericFlows/aaiGetResponse.xml")
88         when(mockExecution.getVariable("GENGS_service")).thenReturn(aaiGetResponse)
89         when(mockExecution.getVariable("GENGS_siResourceLink")).thenReturn("/aai/v8/business/customers/customer/MSO_1610_dev/service-subscriptions/service-subscription/MSO-dev-service-type/service-instances/service-instance/")
90         when(mockExecution.getVariable("mso.workflow.global.default.aai.version")).thenReturn('8')
91         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn('http://org.openecomp.aai.inventory/')
92
93         mockData()
94         DoDeleteServiceInstance instance = new DoDeleteServiceInstance()
95         instance.postProcessAAIGET(mockExecution)
96
97         Mockito.verify(mockExecution).setVariable("sendToSDNC", true)
98     }
99
100     private static ExecutionEntity setupMock() {
101         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
102         when(mockProcessDefinition.getKey()).thenReturn("DoDeleteServiceInstance")
103         RepositoryService mockRepositoryService = mock(RepositoryService.class)
104         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
105         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoDeleteServiceInstance")
106         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
107         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
108         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
109
110         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
111         // Initialize prerequisite variables
112         when(mockExecution.getId()).thenReturn("100")
113         when(mockExecution.getProcessDefinitionId()).thenReturn("DoDeleteServiceInstance")
114         when(mockExecution.getProcessInstanceId()).thenReturn("DoDeleteServiceInstance")
115         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
116         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
117
118         return mockExecution
119     }
120
121     private void mockData() {
122         stubFor(get(urlMatching(".*/aai/v[0-9]+/business/customers/customer/MSO_1610_dev/service-subscriptions/service-subscription/MSO-dev-service-type/service-instances/service-instance/.*"))
123                 .willReturn(aResponse()
124                 .withStatus(200).withHeader("Content-Type", "text/xml")
125                 .withBodyFile("")))
126     }
127 }