cf4987fa87f9537bf858d61a182252363db9b671
[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  */
20
21 package org.onap.policy.clamp.clds.it.config;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26
27 import com.google.gson.JsonSyntaxException;
28 import java.io.IOException;
29 import java.util.Map;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.onap.policy.clamp.clds.config.sdc.SdcControllersConfiguration;
33 import org.onap.policy.clamp.clds.config.sdc.SdcSingleControllerConfiguration;
34 import org.onap.policy.clamp.clds.exception.sdc.controller.SdcParametersException;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.boot.test.context.SpringBootTest;
37 import org.springframework.test.context.ActiveProfiles;
38 import org.springframework.test.context.junit4.SpringRunner;
39 import org.springframework.test.util.ReflectionTestUtils;
40
41 /**
42  * This class tests the SDC Controller config.
43  */
44 @RunWith(SpringRunner.class)
45 @SpringBootTest
46 @ActiveProfiles({"clamp-default", "clamp-default-user", "clamp-sdc-controller"})
47 public class SdcControllersConfigurationItTestCase {
48
49     @Autowired
50     private SdcControllersConfiguration sdcControllersConfiguration;
51
52     public final void loadFile(String fileName) throws IOException {
53         ReflectionTestUtils.setField(sdcControllersConfiguration, "sdcControllerFile", fileName);
54         sdcControllersConfiguration.loadConfiguration();
55     }
56
57     @Test
58     public void testGetAllDefinedControllers() throws IOException {
59         loadFile("classpath:clds/sdc-controllers-config.json");
60         Map<String, SdcSingleControllerConfiguration> mapResult = sdcControllersConfiguration
61                 .getAllDefinedControllers();
62         assertThat(mapResult).hasSize(2);
63         assertEquals("sdc-controller1", mapResult.get("sdc-controller1").getSdcControllerName());
64         assertEquals("sdc-controller2", mapResult.get("sdc-controller2").getSdcControllerName());
65     }
66
67     @Test
68     public void testGetSdcSingleControllerConfiguration() throws IOException {
69         loadFile("classpath:clds/sdc-controllers-config.json");
70         assertEquals("sdc-controller1", sdcControllersConfiguration
71                 .getSdcSingleControllerConfiguration("sdc-controller1").getSdcControllerName());
72         assertEquals("sdc-controller2", sdcControllersConfiguration
73                 .getSdcSingleControllerConfiguration("sdc-controller2").getSdcControllerName());
74     }
75
76     @Test(expected = JsonSyntaxException.class)
77     public void testBadJsonLoading() throws IOException {
78         loadFile("classpath:clds/sdc-controllers-config-bad.json");
79         fail("Should have raised an exception");
80     }
81
82     @Test(expected = SdcParametersException.class)
83     public void testMissingParamInJsonLoading() throws IOException {
84         loadFile("classpath:clds/sdc-controllers-config-missing-param.json");
85         sdcControllersConfiguration.getAllDefinedControllers();
86         fail("Should have raised an exception");
87     }
88 }