7390b65ba95f40928f32bcac054f7e8fb87a5ed6
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP POLICY-CLAMP
4  * ================================================================================
5  * Copyright (C) 2017, 2021 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  * Modifications copyright (c) 2018 Nokia
20  * ================================================================================
21  *
22  */
23
24 package org.onap.policy.clamp.clds.config.sdc;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
31
32 import com.google.gson.JsonObject;
33 import java.io.IOException;
34 import java.io.InputStreamReader;
35 import java.nio.charset.StandardCharsets;
36 import org.junit.Test;
37 import org.onap.policy.clamp.clds.exception.sdc.controller.SdcParametersException;
38 import org.onap.policy.clamp.clds.util.JsonUtils;
39 import org.onap.policy.clamp.clds.util.ResourceFileUtils;
40
41 /**
42  * This class tests the SDC Controller config.
43  */
44 public class SdcSingleControllerConfigurationTest {
45
46     /**
47      * This method loads the SDC controller configuration from a file located in the resource folder.
48      *
49      * @param fileName          file for sdc controller configuration.
50      * @param sdcControllerName sdc controller name.
51      * @return instance of SdcSingleControllerConfiguration.
52      */
53     public static SdcSingleControllerConfiguration loadControllerConfiguration(String fileName,
54                                                                                String sdcControllerName) {
55
56         InputStreamReader streamReader = new InputStreamReader(ResourceFileUtils.getResourceAsStream(fileName),
57                 StandardCharsets.UTF_8);
58         JsonObject jsonNode = JsonUtils.GSON.fromJson(streamReader, JsonObject.class);
59
60         return new SdcSingleControllerConfiguration(jsonNode, sdcControllerName,
61                 "classpath:clds/aaf/org.onap.clamp.keyfile");
62     }
63
64     @Test
65     public final void testTheInit() throws SdcParametersException, IOException {
66         SdcSingleControllerConfiguration sdcConfig = loadControllerConfiguration("clds/sdc-controller-config-TLS.json",
67                 "sdc-controller1");
68         assertEquals("User", sdcConfig.getUser());
69         assertEquals("ThePassword", sdcConfig.getPassword());
70         assertEquals("consumerGroup", sdcConfig.getConsumerGroup());
71         assertEquals("consumerId", sdcConfig.getConsumerID());
72         assertEquals("environmentName", sdcConfig.getEnvironmentName());
73         assertEquals("hostname:8080", sdcConfig.getAsdcAddress());
74         assertEquals(10, sdcConfig.getPollingInterval());
75         assertEquals(30, sdcConfig.getPollingTimeout());
76
77         assertThat(SdcSingleControllerConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST)
78                 .hasSameSizeAs(sdcConfig.getRelevantArtifactTypes());
79         assertEquals("ThePassword", sdcConfig.getKeyStorePassword());
80         assertTrue(sdcConfig.activateServerTLSAuth());
81         assertThat(sdcConfig.getMsgBusAddress()).contains("localhost");
82     }
83
84     @Test(expected = SdcParametersException.class)
85     public final void testAllRequiredParameters() throws IOException {
86         SdcSingleControllerConfiguration sdcConfig = loadControllerConfiguration("clds/sdc-controller-config-TLS.json",
87                 "sdc-controller1");
88         // No exception should be raised
89         sdcConfig.testAllRequiredParameters();
90         sdcConfig = loadControllerConfiguration("clds/sdc-controller-config-bad.json", "sdc-controller1");
91         fail("Should have raised an exception");
92     }
93
94     @Test
95     public final void testAllRequiredParametersEmptyEncrypted() throws IOException {
96         SdcSingleControllerConfiguration sdcConfig = loadControllerConfiguration(
97                 "clds/sdc-controller-config-empty-encrypted.json", "sdc-controller1");
98         sdcConfig.testAllRequiredParameters();
99         assertNull(sdcConfig.getKeyStorePassword());
100     }
101
102     @Test
103     public final void testConsumerGroupWithNull() throws IOException {
104         SdcSingleControllerConfiguration sdcConfig = loadControllerConfiguration("clds/sdc-controller-config-NULL.json",
105                 "sdc-controller1");
106         assertTrue(sdcConfig.getConsumerGroup() == null);
107     }
108 }