2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.bpmn.common.scripts
25 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake
26 import org.json.JSONArray
27 import org.json.JSONObject
28 import org.junit.Before
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
40 import javax.ws.rs.core.MediaType
41 import javax.ws.rs.core.Response
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
50 class CatalogDbUtilsTest {
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
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)
72 void getAllVnfsByVnfModelCustomizationUuid_CatVer1_success() {
74 executionFake.setVariable("BasicAuthHeaderValueDB", AUTHORIZATION_HEADER)
75 mockGetResponseFromCatalogDb("http://testUrl/v2/serviceVnfs?vnfModelCustomizationUuid=testModel")
77 JSONArray vnfsListResult = testedObject.getAllVnfsByVnfModelCustomizationUuid(executionFake, "testModel", "v1")
79 verifyHeadersInHttpClient()
80 JSONAssert.assertEquals("[{\"vfModules\":[{\"initialCount\":1,\"modelInfo\":{\"modelType\":\"vfModule\"},\"isBase\":true}],\"modelInfo\":{\"modelType\":\"vnf\"}}]", vnfsListResult, JSONCompareMode.LENIENT)
84 void getAllVnfsByVnfModelCustomizationUuid_CatVer2_success() {
86 executionFake.setVariable("BasicAuthHeaderValueDB", AUTHORIZATION_HEADER)
87 mockGetResponseFromCatalogDb("http://testUrl/v2/serviceVnfs?vnfModelCustomizationUuid=testModel")
89 JSONArray vnfsListResult = testedObject.getAllVnfsByVnfModelCustomizationUuid(executionFake, "testModel", "v2")
91 verifyHeadersInHttpClient()
92 JSONAssert.assertEquals("[{\"vfModules\":[{\"initialCount\":1,\"name\":\"module1\",\"isBase\":true}],\"name\":\"service1\"}]", vnfsListResult, JSONCompareMode.LENIENT)
96 void getServiceResourcesByServiceModelUuid_success() {
98 executionFake.setVariable("BasicAuthHeaderValueDB", AUTHORIZATION_HEADER)
99 mockGetResponseFromCatalogDb("http://testUrl/v2/serviceResources?serviceModelUuid=testModel")
101 JSONObject result = testedObject.getServiceResourcesByServiceModelUuid(executionFake, "testModel", "v2")
103 verifyHeadersInHttpClient()
104 JSONAssert.assertEquals("{\"serviceVnfs\": [{\"name\": \"service1\",\"vfModules\": [{\"name\": \"module1\", \"isBase\":true, \"initialCount\":1}]}]}", result, JSONCompareMode.LENIENT)
109 void getServiceResourcesByServiceModelInvariantUuidString_success() {
111 executionFake.setVariable("BasicAuthHeaderValueDB", AUTHORIZATION_HEADER)
112 mockGetResponseFromCatalogDb("http://testUrl/v2/serviceResources?serviceModelInvariantUuid=testModel")
114 String result = testedObject.getServiceResourcesByServiceModelInvariantUuidString(executionFake, "testModel")
116 verifyHeadersInHttpClient()
117 assertThat(result).isEqualTo(RESPONSE_FROM_CATALOG_DB)
120 private Environment createEnvironmentMock() {
121 Environment mockEnvironment = mock(Environment.class)
122 UrnPropertiesReader urnPropertiesReader = new UrnPropertiesReader()
123 urnPropertiesReader.setEnvironment(mockEnvironment)
124 return mockEnvironment
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)
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)
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)