2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent.configuration;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.Properties;
31 import java.util.Vector;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.mockito.junit.jupiter.MockitoExtension;
36 import org.oransc.policyagent.configuration.ApplicationConfig.RicConfigUpdate;
37 import org.oransc.policyagent.configuration.ApplicationConfigParser.ConfigParserResult;
38 import org.oransc.policyagent.exceptions.ServiceException;
40 @ExtendWith(MockitoExtension.class)
41 public class ApplicationConfigTest {
43 private static final ImmutableRicConfig RIC_CONFIG_1 = ImmutableRicConfig.builder() //
45 .baseUrl("ric1_url") //
46 .managedElementIds(new Vector<>()) //
47 .controllerName("") //
50 ConfigParserResult configParserResult(RicConfig... rics) {
51 return ImmutableConfigParserResult.builder() //
52 .ricConfigs(Arrays.asList(rics)) //
53 .dmaapConsumerConfig(new Properties()) //
54 .dmaapPublisherConfig(new Properties()) //
55 .controllerConfigs(new HashMap<>()) //
60 public void gettingNotAddedRicShouldThrowException() {
61 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
63 appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1));
65 Exception exception = assertThrows(ServiceException.class, () -> {
66 appConfigUnderTest.getRic("name");
69 assertEquals("Could not find ric configuration: name", exception.getMessage());
73 public void addRicShouldNotifyAllObserversOfRicAdded() throws Exception {
74 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
76 RicConfigUpdate update = appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1)).blockFirst();
77 assertEquals(RicConfigUpdate.Type.ADDED, update.getType());
78 assertTrue(appConfigUnderTest.getRicConfigs().contains(RIC_CONFIG_1), "Ric not added to configurations.");
80 assertEquals(RIC_CONFIG_1, appConfigUnderTest.getRic(RIC_CONFIG_1.name()),
81 "Not correct Ric retrieved from configurations.");
83 update = appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1)).blockFirst();
84 assertNull(update, "Nothing should be updated");
85 assertTrue(appConfigUnderTest.getRicConfigs().contains(RIC_CONFIG_1), "Ric should remain.");
90 public void changedRicShouldNotifyAllObserversOfRicChanged() throws Exception {
91 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
93 appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1));
95 ImmutableRicConfig changedRicConfig = ImmutableRicConfig.builder() //
97 .baseUrl("changed_ric1_url") //
98 .managedElementIds(new Vector<>()) //
99 .controllerName("") //
102 RicConfigUpdate update = appConfigUnderTest.setConfiguration(configParserResult(changedRicConfig)).blockFirst();
104 assertEquals(RicConfigUpdate.Type.CHANGED, update.getType());
105 assertEquals(changedRicConfig, appConfigUnderTest.getRic(RIC_CONFIG_1.name()),
106 "Changed Ric not retrieved from configurations.");
110 public void removedRicShouldNotifyAllObserversOfRicRemoved() {
111 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
113 ImmutableRicConfig ricConfig2 = ImmutableRicConfig.builder() //
115 .baseUrl("ric2_url") //
116 .managedElementIds(new Vector<>()) //
117 .controllerName("") //
120 appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1, ricConfig2));
122 RicConfigUpdate update = appConfigUnderTest.setConfiguration(configParserResult(ricConfig2)).blockFirst();
124 assertEquals(RicConfigUpdate.Type.REMOVED, update.getType());
125 assertEquals(1, appConfigUnderTest.getRicConfigs().size(), "Ric not deleted from configurations.");