cleaned up clds service code
[clamp.git] / src / test / java / org / onap / clamp / clds / config / sdc / SdcSingleControllerConfigurationTest.java
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  * Modifications copyright (c) 2018 Nokia
20  * ================================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.config.sdc;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.junit.Assert.assertArrayEquals;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertTrue;
31 import static org.junit.Assert.fail;
32
33 import com.fasterxml.jackson.core.JsonParseException;
34 import com.fasterxml.jackson.databind.JsonMappingException;
35 import com.fasterxml.jackson.databind.JsonNode;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37
38 import java.io.IOException;
39
40 import org.junit.Test;
41 import org.onap.clamp.clds.exception.sdc.controller.SdcParametersException;
42 import org.onap.clamp.clds.util.ResourceFileUtil;
43
44 /**
45  * This class tests the SDC Controller config.
46  */
47 public class SdcSingleControllerConfigurationTest {
48
49     private SdcSingleControllerConfiguration loadControllerConfiguration(String fileName, String sdcControllerName)
50             throws IOException {
51         JsonNode jsonNode = new ObjectMapper().readValue(ResourceFileUtil.getResourceAsStream(fileName),
52                 JsonNode.class);
53         SdcSingleControllerConfiguration sdcSingleControllerConfiguration = new SdcSingleControllerConfiguration(
54                 jsonNode, sdcControllerName);
55         return sdcSingleControllerConfiguration;
56     }
57
58     @Test
59     public final void testTheInit() throws SdcParametersException, IOException {
60         SdcSingleControllerConfiguration sdcConfig = loadControllerConfiguration("clds/sdc-controller-config-TLS.json",
61                 "sdc-controller1");
62         assertEquals("User", sdcConfig.getUser());
63         assertEquals("ThePassword", sdcConfig.getPassword());
64         assertEquals("consumerGroup", sdcConfig.getConsumerGroup());
65         assertEquals("consumerId", sdcConfig.getConsumerID());
66         assertEquals("environmentName", sdcConfig.getEnvironmentName());
67         assertEquals("hostname:8080", sdcConfig.getAsdcAddress());
68         assertEquals(10, sdcConfig.getPollingInterval());
69         assertEquals(30, sdcConfig.getPollingTimeout());
70
71         assertThat(SdcSingleControllerConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST)
72             .hasSameSizeAs(sdcConfig.getRelevantArtifactTypes());
73         assertEquals("ThePassword", sdcConfig.getKeyStorePassword());
74         assertTrue(sdcConfig.activateServerTLSAuth());
75         assertThat(sdcConfig.getMsgBusAddress()).contains("localhost");
76     }
77
78     @Test(expected = SdcParametersException.class)
79     public final void testAllRequiredParameters() throws IOException {
80         SdcSingleControllerConfiguration sdcConfig = loadControllerConfiguration("clds/sdc-controller-config-TLS.json",
81                 "sdc-controller1");
82         // No exception should be raised
83         sdcConfig.testAllRequiredParameters();
84         sdcConfig = loadControllerConfiguration("clds/sdc-controller-config-bad.json", "sdc-controller1");
85         fail("Should have raised an exception");
86     }
87
88     @Test
89     public final void testAllRequiredParametersEmptyEncrypted()
90             throws IOException {
91         SdcSingleControllerConfiguration sdcConfig = loadControllerConfiguration(
92                 "clds/sdc-controller-config-empty-encrypted.json", "sdc-controller1");
93         sdcConfig.testAllRequiredParameters();
94         assertNull(sdcConfig.getKeyStorePassword());
95     }
96
97     @Test
98     public final void testConsumerGroupWithNull() throws IOException {
99         SdcSingleControllerConfiguration sdcConfig = loadControllerConfiguration("clds/sdc-controller-config-NULL.json",
100                 "sdc-controller1");
101         assertTrue(sdcConfig.getConsumerGroup() == null);
102     }
103 }