Merge "removed extra argument from extractByKey method"
[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) 2018 Nokia.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.common.scripts
24
25 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake
26 import org.json.JSONArray
27 import org.json.JSONObject
28 import org.junit.Before
29 import org.junit.Test
30 import org.onap.logging.ref.slf4j.ONAPLogConstants
31 import org.onap.so.bpmn.core.UrnPropertiesReader
32 import org.onap.so.bpmn.core.json.JsonUtils
33 import org.onap.so.client.HttpClient
34 import org.onap.so.client.HttpClientFactory
35 import org.onap.so.utils.TargetEntity
36 import org.skyscreamer.jsonassert.JSONAssert
37 import org.skyscreamer.jsonassert.JSONCompareMode
38 import org.springframework.core.env.Environment
39
40 import javax.ws.rs.core.MediaType
41 import javax.ws.rs.core.Response
42
43 import static org.assertj.core.api.Assertions.assertThat
44 import static org.mockito.ArgumentMatchers.anyString
45 import static org.mockito.Mockito.eq
46 import static org.mockito.Mockito.mock
47 import static org.mockito.Mockito.when
48 import static org.mockito.Mockito.verify
49
50 class CatalogDbUtilsTest {
51
52     private static final String AUTHORIZATION_HEADER = "AuthHeaderTest"
53     private static final String RESPONSE_FROM_CATALOG_DB = "{\"serviceVnfs\": [{\"name\": \"service1\"," +
54             "\"vfModules\": [{\"name\": \"module1\", \"isBase\":true, \"initialCount\":1}]}]}"
55     private HttpClientFactory httpClientFactoryMock
56     private JsonUtils jsonUtilsMock
57     private HttpClient httpClientMock
58     private DelegateExecutionFake executionFake
59     private CatalogDbUtils testedObject
60
61
62     @Before
63     void setUp() {
64         httpClientFactoryMock = mock(HttpClientFactory.class)
65         jsonUtilsMock = mock(JsonUtils.class)
66         httpClientMock = mock(HttpClient.class)
67         executionFake = new DelegateExecutionFake()
68         testedObject = new CatalogDbUtils(httpClientFactoryMock, jsonUtilsMock)
69     }
70
71     @Test
72     void getAllVnfsByVnfModelCustomizationUuid_CatVer1_success() {
73         // given
74         executionFake.setVariable("BasicAuthHeaderValueDB", AUTHORIZATION_HEADER)
75         mockGetResponseFromCatalogDb("http://testUrl/v2/serviceVnfs?vnfModelCustomizationUuid=testModel")
76         //when
77         JSONArray vnfsListResult = testedObject.getAllVnfsByVnfModelCustomizationUuid(executionFake, "testModel", "v1")
78         //then
79         verifyHeadersInHttpClient()
80         JSONAssert.assertEquals("[{\"vfModules\":[{\"initialCount\":1,\"modelInfo\":{\"modelType\":\"vfModule\"},\"isBase\":true}],\"modelInfo\":{\"modelType\":\"vnf\"}}]", vnfsListResult, JSONCompareMode.LENIENT)
81     }
82
83     @Test
84     void getAllVnfsByVnfModelCustomizationUuid_CatVer2_success() {
85         // given
86         executionFake.setVariable("BasicAuthHeaderValueDB", AUTHORIZATION_HEADER)
87         mockGetResponseFromCatalogDb("http://testUrl/v2/serviceVnfs?vnfModelCustomizationUuid=testModel")
88         // when
89         JSONArray vnfsListResult = testedObject.getAllVnfsByVnfModelCustomizationUuid(executionFake, "testModel", "v2")
90         // then
91         verifyHeadersInHttpClient()
92         JSONAssert.assertEquals("[{\"vfModules\":[{\"initialCount\":1,\"name\":\"module1\",\"isBase\":true}],\"name\":\"service1\"}]", vnfsListResult, JSONCompareMode.LENIENT)
93     }
94
95     @Test
96     void getServiceResourcesByServiceModelUuid_success() {
97         // given
98         executionFake.setVariable("BasicAuthHeaderValueDB", AUTHORIZATION_HEADER)
99         mockGetResponseFromCatalogDb("http://testUrl/v2/serviceResources?serviceModelUuid=testModel")
100         // when
101         JSONObject result = testedObject.getServiceResourcesByServiceModelUuid(executionFake, "testModel", "v2")
102         // then
103         verifyHeadersInHttpClient()
104         JSONAssert.assertEquals("{\"serviceVnfs\": [{\"name\": \"service1\",\"vfModules\": [{\"name\": \"module1\", \"isBase\":true, \"initialCount\":1}]}]}", result, JSONCompareMode.LENIENT)
105     }
106
107
108     @Test
109     void getServiceResourcesByServiceModelInvariantUuidString_success() {
110         // given
111         executionFake.setVariable("BasicAuthHeaderValueDB", AUTHORIZATION_HEADER)
112         mockGetResponseFromCatalogDb("http://testUrl/v2/serviceResources?serviceModelInvariantUuid=testModel")
113         // when
114         String result = testedObject.getServiceResourcesByServiceModelInvariantUuidString(executionFake, "testModel")
115         // then
116         verifyHeadersInHttpClient()
117         assertThat(result).isEqualTo(RESPONSE_FROM_CATALOG_DB)
118     }
119
120     private Environment createEnvironmentMock() {
121         Environment mockEnvironment = mock(Environment.class)
122         UrnPropertiesReader urnPropertiesReader = new UrnPropertiesReader()
123         urnPropertiesReader.setEnvironment(mockEnvironment)
124         return mockEnvironment
125     }
126
127     private void mockGetResponseFromCatalogDb(String queryEndpoint) {
128         Environment environmentMock = createEnvironmentMock()
129         when(environmentMock.getProperty("mso.catalog.db.endpoint")).thenReturn("http://testUrl")
130         when(httpClientFactoryMock.newJsonClient(new URL(queryEndpoint), TargetEntity.CATALOG_DB)).thenReturn(httpClientMock)
131
132         Response responseMock = mock(Response.class)
133         when(httpClientMock.get()).thenReturn(responseMock)
134         when(responseMock.readEntity(String.class)) thenReturn(RESPONSE_FROM_CATALOG_DB)
135         when(responseMock.getStatus()).thenReturn(200)
136     }
137
138     private void verifyHeadersInHttpClient() {
139         verify(httpClientMock).addAdditionalHeader(eq(ONAPLogConstants.Headers.REQUEST_ID), anyString())
140         verify(httpClientMock).addAdditionalHeader("X-FromAppId", "BPMN")
141         verify(httpClientMock).addAdditionalHeader("Accept", MediaType.APPLICATION_JSON)
142         verify(httpClientMock).addAdditionalHeader("Authorization", AUTHORIZATION_HEADER)
143     }
144
145 }