39064caa288eae78758473c6db75627fa36331c5
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / CatalogDbUtilsTest.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.Rule
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 import org.mockito.Mockito
34 import org.mockito.runners.MockitoJUnitRunner
35 import org.onap.so.bpmn.mock.FileUtil
36
37 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse
38 import static com.github.tomakehurst.wiremock.client.WireMock.get
39 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor
40 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching
41 import static org.mockito.Mockito.mock
42 import static org.mockito.Mockito.mock
43 import static org.mockito.Mockito.mock
44 import static org.mockito.Mockito.mock
45 import static org.mockito.Mockito.when
46 import static org.mockito.Mockito.when
47 import static org.mockito.Mockito.when
48 import static org.mockito.Mockito.when
49 import static org.mockito.Mockito.when
50 import static org.mockito.Mockito.when
51 import static org.mockito.Mockito.when
52
53 @RunWith(MockitoJUnitRunner.class)
54 class CatalogDbUtilsTest {
55
56
57     @Rule
58     public WireMockRule wireMockRule = new WireMockRule(8090)
59
60     @Test
61     public void testGetResponseFromCatalogDb() {
62         ExecutionEntity mockExecution = setupMock()
63         when(mockExecution.getVariable("mso.catalog.db.endpoint")).thenReturn('http://localhost:8090')
64                 when(mockExecution.getVariable("mso.adapters.db.auth")).thenReturn('757A94191D685FD2092AC1490730A4FC')
65                 when(mockExecution.getVariable("mso.msoKey")).thenReturn('07a7159d3bf51a0e53be7a8f89699be7')
66
67         stubFor(get(urlMatching(".*/serviceNetworks[?]serviceModelUuid=12345"))
68                 .willReturn(aResponse()
69                 .withStatus(200)
70                 .withBodyFile("catalogDbFiles/DoCreateServiceInstance_request.json")))
71
72         CatalogDbUtils obj = new CatalogDbUtils()
73         String str = obj.getResponseFromCatalogDb(mockExecution, "/serviceNetworks?serviceModelUuid=12345")
74         String expectedValue =
75                 FileUtil.readResourceFile("__files/catalogDbFiles/DoCreateServiceInstance_request.json");
76         Assert.assertEquals(expectedValue, str)
77
78     }
79
80     private ExecutionEntity setupMock() {
81
82         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
83         when(mockProcessDefinition.getKey()).thenReturn("GenericGetService")
84         RepositoryService mockRepositoryService = mock(RepositoryService.class)
85         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
86         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("GenericGetService")
87         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
88         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
89         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
90         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
91         when(mockExecution.getId()).thenReturn("100")
92         when(mockExecution.getProcessDefinitionId()).thenReturn("GenericGetService")
93         when(mockExecution.getProcessInstanceId()).thenReturn("GenericGetService")
94         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
95         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
96         return mockExecution
97     }
98 }