replace all fixed wiremock ports
[so.git] / bpmn / so-bpmn-infrastructure-common / 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     @Captor
50     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
51
52     @Before
53     void init() throws IOException {
54         MockitoAnnotations.initMocks(this);
55     }
56
57     @Test
58     public void preProcessRequestTest() {
59
60         ExecutionEntity mex = setupMock()
61         when(mex.getVariable(GroovyTestBase.DBGFLAG)).thenReturn("true")
62         when(mex.getVariable("serviceInstanceId")).thenReturn("e151059a-d924-4629-845f-264db19e50b4")
63         when(mex.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("/mso/sdncadapter/")
64         when(mex.getVariable("globalSubscriberId")).thenReturn("4993921112123")
65
66         DoDeleteServiceInstance instance = new DoDeleteServiceInstance()
67         instance.preProcessRequest(mex)
68
69         Mockito.verify(mex).setVariable("sdncCallbackUrl", "/mso/sdncadapter/")
70         Mockito.verify(mex).setVariable("siParamsXml", "")
71     }
72
73    
74     @Test
75     public void testPostProcessAAIGET() {
76         ExecutionEntity mockExecution = setupMock()
77         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn('true')
78         when(mockExecution.getVariable("aai.endpoint")).thenReturn('http://localhost:8090')
79         when(mockExecution.getVariable("GENGS_FoundIndicator")).thenReturn(true)
80         when(mockExecution.getVariable("sdnc.si.svc.types")).thenReturn("")
81         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("globalSubscriberId_test")
82         when(mockExecution.getVariable("subscriptionServiceType")).thenReturn("subscriptionServiceType_test")
83
84         String aaiGetResponse = FileUtil.readResourceFile("__files/GenericFlows/aaiGetResponse.xml")
85         when(mockExecution.getVariable("GENGS_service")).thenReturn(aaiGetResponse)
86         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/")
87         when(mockExecution.getVariable("mso.workflow.global.default.aai.version")).thenReturn('8')
88         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn('http://org.openecomp.aai.inventory/')
89
90         mockData()
91         DoDeleteServiceInstance instance = new DoDeleteServiceInstance()
92         instance.postProcessAAIGET(mockExecution)
93
94         Mockito.verify(mockExecution).setVariable("sendToSDNC", true)
95     }
96
97     private static ExecutionEntity setupMock() {
98         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
99         when(mockProcessDefinition.getKey()).thenReturn("DoDeleteServiceInstance")
100         RepositoryService mockRepositoryService = mock(RepositoryService.class)
101         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
102         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoDeleteServiceInstance")
103         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
104         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
105         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
106
107         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
108         // Initialize prerequisite variables
109         when(mockExecution.getId()).thenReturn("100")
110         when(mockExecution.getProcessDefinitionId()).thenReturn("DoDeleteServiceInstance")
111         when(mockExecution.getProcessInstanceId()).thenReturn("DoDeleteServiceInstance")
112         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
113         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
114
115         return mockExecution
116     }
117
118     private void mockData() {
119         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/.*"))
120                 .willReturn(aResponse()
121                 .withStatus(200).withHeader("Content-Type", "text/xml")
122                 .withBodyFile("")))
123     }
124 }