587abbee52d09bcf125d1b762e82e515b1feed70
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateServiceInstanceTest.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.Rule
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 import org.mockito.ArgumentCaptor
34 import org.mockito.Captor
35 import org.mockito.Mockito
36 import org.mockito.MockitoAnnotations
37 import org.mockito.runners.MockitoJUnitRunner
38 import org.onap.so.bpmn.core.WorkflowException
39 import org.onap.so.bpmn.core.domain.ModelInfo
40 import org.onap.so.bpmn.core.domain.ServiceDecomposition
41 import org.onap.so.bpmn.core.domain.ServiceInstance
42 import org.onap.so.bpmn.mock.StubResponseAAI
43
44 import static org.mockito.Mockito.*
45
46 @RunWith(MockitoJUnitRunner.class)
47 class DoCreateServiceInstanceTest {
48     def prefix = "DCRESI_"
49
50     @Rule
51     public WireMockRule wireMockRule = new WireMockRule(28090)
52
53     @Captor
54     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
55
56     @Before
57     void init() throws IOException {
58         MockitoAnnotations.initMocks(this);
59     }
60
61     @Test
62     void testPreProcessRequest() {
63         ExecutionEntity mockExecution = setupMock()
64         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
65         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
66         when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
67         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("MIS/1604/0026/SW_INTERNET")
68         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("MDTWNJ21")
69         when(mockExecution.getVariable("mso-request-id")).thenReturn("testRequestId-1503410089303")
70         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:28080/mso/SDNCAdapterCallbackService")
71         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("MSO_dev")
72         when(mockExecution.getVariable("subscriptionServiceType")).thenReturn("MSO-dev-service-type")
73         when(mockExecution.getVariable("productFamilyId")).thenReturn("RDM2WAGPLCP")
74         when(mockExecution.getVariable("sdnc.si.svc.types")).thenReturn("PORT-MIRROR,PPROBES")
75
76         ServiceDecomposition decomposition = new ServiceDecomposition()
77         ModelInfo modelInfo = new ModelInfo()
78         ServiceInstance instance = new ServiceInstance()
79         instance.instanceId = "12345"
80         decomposition.modelInfo = modelInfo
81         decomposition.serviceInstance = instance
82
83         when(mockExecution.getVariable("serviceDecomposition")).thenReturn(decomposition)
84         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
85         when(mockExecution.getVariable("mso.workflow.default.aai.customer.version")).thenReturn("8")
86
87
88         DoCreateServiceInstance obj = new DoCreateServiceInstance()
89         obj.preProcessRequest(mockExecution)
90
91         verify(mockExecution).setVariable("prefix", prefix)
92         verify(mockExecution).setVariable("sdncCallbackUrl", "http://localhost:28080/mso/SDNCAdapterCallbackService")
93     }
94
95
96
97     @Test
98     void testGetAAICustomerById() {
99         ExecutionEntity mockExecution = setupMock()
100         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
101         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
102         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("12345")
103         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
104         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
105         when(mockExecution.getVariable("mso.workflow.custom.DoCreateServiceInstance.aai.version")).thenReturn('8')
106         StubResponseAAI.MockGetCustomer("12345", "")
107         DoCreateServiceInstance obj = new DoCreateServiceInstance()
108         obj.getAAICustomerById(mockExecution)
109
110         verify(mockExecution, times(1)).getVariable("aai.endpoint")
111     }
112
113     private static ExecutionEntity setupMock() {
114         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
115         when(mockProcessDefinition.getKey()).thenReturn("DoCreateServiceInstance")
116         RepositoryService mockRepositoryService = mock(RepositoryService.class)
117         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
118         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoCreateServiceInstance")
119         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
120         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
121         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
122
123         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
124         // Initialize prerequisite variables
125         when(mockExecution.getId()).thenReturn("100")
126         when(mockExecution.getProcessDefinitionId()).thenReturn("DoCreateServiceInstance")
127         when(mockExecution.getProcessInstanceId()).thenReturn("DoCreateServiceInstance")
128         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
129         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
130
131         return mockExecution
132     }
133 }