Config fetch for VESCollector through DCAE-SDK (CBS Client)
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / configuration / cbs / CbsConfigResolverTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dcaegen2.collectors.ves
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. 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 package org.onap.dcae.configuration.cbs;
21
22 import com.github.tomakehurst.wiremock.junit.WireMockRule;
23 import org.json.JSONObject;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration;
29 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableCbsClientConfiguration;
30
31 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
32 import static com.github.tomakehurst.wiremock.client.WireMock.get;
33 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
34 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
35 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
36 import static org.assertj.core.api.Assertions.assertThat;
37
38 @RunWith(MockitoJUnitRunner.Silent.class)
39 public class CbsConfigResolverTest {
40
41     private static final String VES_CONFIG = "{\"collector.port\": 8081}";
42     private static final String HOSTNAME = "localhost";
43     private static final String PROTOCOL = "http";
44     private static final String APP_NAME = "VESCollector";
45
46     @Rule
47     public final WireMockRule wireMockRule = new WireMockRule(
48             wireMockConfig().dynamicPort().dynamicPort());
49
50     @Test
51     public void shouldFetchConfigurationFromCBS() {
52         // given
53         final int PORT = wireMockRule.port();
54         stubCBSToReturnAppConfig();
55
56         // when
57         CbsClientConfiguration cbsClientConfiguration = ImmutableCbsClientConfiguration.builder()
58                 .protocol(PROTOCOL)
59                 .hostname(HOSTNAME)
60                 .port(PORT)
61                 .appName(APP_NAME)
62                 .build();
63         JSONObject appConfig = new CbsConfigResolver(cbsClientConfiguration).getAppConfig().get();
64
65         // then
66         assertThat(appConfig).isNotNull();
67         assertThat(appConfig.toString()).isEqualTo(new JSONObject(VES_CONFIG).toString());
68     }
69
70     private void stubCBSToReturnAppConfig() {
71         stubFor(get(urlEqualTo("/service_component/VESCollector"))
72                 .willReturn(aResponse().withBody(CbsConfigResolverTest.VES_CONFIG)));
73     }
74 }