2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 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.assertThrows;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.Mockito.verify;
28 import java.util.Arrays;
29 import java.util.Vector;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockito.Mock;
34 import org.mockito.junit.jupiter.MockitoExtension;
35 import org.oransc.policyagent.configuration.ApplicationConfig.Observer;
36 import org.oransc.policyagent.exceptions.ServiceException;
38 @ExtendWith(MockitoExtension.class)
39 public class ApplicationConfigTest {
41 Observer observerMock1;
44 Observer observerMock2;
46 private static final ImmutableRicConfig RIC_CONFIG_1 = ImmutableRicConfig.builder() //
48 .baseUrl("ric1_url") //
49 .managedElementIds(new Vector<>()) //
53 public void addRicShouldNotifyAllObserversOfRicAdded() throws Exception {
54 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
56 appConfigUnderTest.addObserver(observerMock1);
57 appConfigUnderTest.addObserver(observerMock2);
59 appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1), null, null);
61 verify(observerMock1).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.ADDED);
62 verify(observerMock2).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.ADDED);
64 assertTrue(appConfigUnderTest.getRicConfigs().contains(RIC_CONFIG_1), "Ric not added to configuraions.");
66 assertEquals(RIC_CONFIG_1, appConfigUnderTest.getRic(RIC_CONFIG_1.name()),
67 "Not correct Ric retrieved from configurations.");
71 public void changedRicShouldNotifyAllObserversOfRicChanged() throws Exception {
72 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
74 appConfigUnderTest.addObserver(observerMock1);
76 appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1), null, null);
78 ImmutableRicConfig changedRicConfig = ImmutableRicConfig.builder() //
80 .baseUrl("changed_ric1_url") //
81 .managedElementIds(new Vector<>()) //
84 appConfigUnderTest.setConfiguration(Arrays.asList(changedRicConfig), null, null);
86 verify(observerMock1).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.ADDED);
87 verify(observerMock1).onRicConfigUpdate(changedRicConfig, ApplicationConfig.RicConfigUpdate.CHANGED);
89 assertEquals(changedRicConfig, appConfigUnderTest.getRic(RIC_CONFIG_1.name()),
90 "Changed Ric not retrieved from configurations.");
94 public void removedRicShouldNotifyAllObserversOfRicRemoved() {
95 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
97 appConfigUnderTest.addObserver(observerMock1);
99 ImmutableRicConfig ricConfig2 = ImmutableRicConfig.builder() //
101 .baseUrl("ric2_url") //
102 .managedElementIds(new Vector<>()) //
105 appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1, ricConfig2), null, null);
107 appConfigUnderTest.setConfiguration(Arrays.asList(ricConfig2), null, null);
109 verify(observerMock1).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.REMOVED);
111 assertEquals(1, appConfigUnderTest.getRicConfigs().size(), "Ric not deleted from configurations.");
115 public void gettingNotAddedRicShouldThrowException() {
116 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
118 appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1), null, null);
120 Exception exception = assertThrows(ServiceException.class, () -> {
121 appConfigUnderTest.getRic("name");
124 assertEquals("Could not find ric: name", exception.getMessage());