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.Vector;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.mockito.junit.jupiter.MockitoExtension;
35 import org.oransc.policyagent.configuration.ApplicationConfig.RicConfigUpdate;
36 import org.oransc.policyagent.configuration.ApplicationConfigParser.ConfigParserResult;
37 import org.oransc.policyagent.exceptions.ServiceException;
39 @ExtendWith(MockitoExtension.class)
40 class ApplicationConfigTest {
42 private static final ImmutableRicConfig RIC_CONFIG_1 = ImmutableRicConfig.builder() //
44 .baseUrl("ric1_url") //
45 .managedElementIds(new Vector<>()) //
46 .controllerName("") //
49 ConfigParserResult configParserResult(RicConfig... rics) {
50 return ImmutableConfigParserResult.builder() //
51 .ricConfigs(Arrays.asList(rics)) //
52 .dmaapConsumerTopicUrl("dmaapConsumerTopicUrl") //
53 .dmaapProducerTopicUrl("dmaapProducerTopicUrl") //
54 .controllerConfigs(new HashMap<>()) //
59 void gettingNotAddedRicShouldThrowException() {
60 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
62 appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1));
64 Exception exception = assertThrows(ServiceException.class, () -> {
65 appConfigUnderTest.getRic("name");
68 assertEquals("Could not find ric configuration: name", exception.getMessage());
72 void addRicShouldNotifyAllObserversOfRicAdded() throws Exception {
73 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
75 RicConfigUpdate update = appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1)).blockFirst();
76 assertEquals(RicConfigUpdate.Type.ADDED, update.getType());
77 assertTrue(appConfigUnderTest.getRicConfigs().contains(RIC_CONFIG_1), "Ric not added to configurations.");
79 assertEquals(RIC_CONFIG_1, appConfigUnderTest.getRic(RIC_CONFIG_1.name()),
80 "Not correct Ric retrieved from configurations.");
82 update = appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1)).blockFirst();
83 assertNull(update, "Nothing should be updated");
84 assertTrue(appConfigUnderTest.getRicConfigs().contains(RIC_CONFIG_1), "Ric should remain.");
89 void changedRicShouldNotifyAllObserversOfRicChanged() throws Exception {
90 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
92 appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1));
94 ImmutableRicConfig changedRicConfig = ImmutableRicConfig.builder() //
96 .baseUrl("changed_ric1_url") //
97 .managedElementIds(new Vector<>()) //
98 .controllerName("") //
101 RicConfigUpdate update = appConfigUnderTest.setConfiguration(configParserResult(changedRicConfig)).blockFirst();
103 assertEquals(RicConfigUpdate.Type.CHANGED, update.getType());
104 assertEquals(changedRicConfig, appConfigUnderTest.getRic(RIC_CONFIG_1.name()),
105 "Changed Ric not retrieved from configurations.");
109 void removedRicShouldNotifyAllObserversOfRicRemoved() {
110 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
112 ImmutableRicConfig ricConfig2 = ImmutableRicConfig.builder() //
114 .baseUrl("ric2_url") //
115 .managedElementIds(new Vector<>()) //
116 .controllerName("") //
119 appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1, ricConfig2));
121 RicConfigUpdate update = appConfigUnderTest.setConfiguration(configParserResult(ricConfig2)).blockFirst();
123 assertEquals(RicConfigUpdate.Type.REMOVED, update.getType());
124 assertEquals(1, appConfigUnderTest.getRicConfigs().size(), "Ric not deleted from configurations.");