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;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.mockito.Mock;
33 import org.mockito.junit.jupiter.MockitoExtension;
34 import org.oransc.policyagent.configuration.ApplicationConfig.Observer;
35 import org.oransc.policyagent.exceptions.ServiceException;
37 @ExtendWith(MockitoExtension.class)
38 public class ApplicationConfigTest {
40 Observer observerMock1;
43 Observer observerMock2;
45 private static final ImmutableRicConfig RIC_CONFIG_1 = ImmutableRicConfig.builder() //
47 .baseUrl("ric1_url") //
48 .managedElementIds(new Vector<>()) //
52 public void addRicShouldNotifyAllObserversOfRicAdded() throws Exception {
53 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
55 appConfigUnderTest.addObserver(observerMock1);
56 appConfigUnderTest.addObserver(observerMock2);
58 appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1), null, null);
60 verify(observerMock1).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.ADDED);
61 verify(observerMock2).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.ADDED);
63 assertTrue(appConfigUnderTest.getRicConfigs().contains(RIC_CONFIG_1), "Ric not added to configuraions.");
65 assertEquals(RIC_CONFIG_1, appConfigUnderTest.getRic(RIC_CONFIG_1.name()),
66 "Not correct Ric retrieved from configurations.");
70 public void changedRicShouldNotifyAllObserversOfRicChanged() throws Exception {
71 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
73 appConfigUnderTest.addObserver(observerMock1);
75 appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1), null, null);
77 ImmutableRicConfig changedRicConfig = ImmutableRicConfig.builder() //
79 .baseUrl("changed_ric1_url") //
80 .managedElementIds(new Vector<>()) //
83 appConfigUnderTest.setConfiguration(Arrays.asList(changedRicConfig), null, null);
85 verify(observerMock1).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.ADDED);
86 verify(observerMock1).onRicConfigUpdate(changedRicConfig, ApplicationConfig.RicConfigUpdate.CHANGED);
88 assertEquals(changedRicConfig, appConfigUnderTest.getRic(RIC_CONFIG_1.name()),
89 "Changed Ric not retrieved from configurations.");
93 public void removedRicShouldNotifyAllObserversOfRicRemoved() {
94 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
96 appConfigUnderTest.addObserver(observerMock1);
98 ImmutableRicConfig ricConfig2 = ImmutableRicConfig.builder() //
100 .baseUrl("ric2_url") //
101 .managedElementIds(new Vector<>()) //
104 appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1, ricConfig2), null, null);
106 appConfigUnderTest.setConfiguration(Arrays.asList(ricConfig2), null, null);
108 verify(observerMock1).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.REMOVED);
110 assertEquals(1, appConfigUnderTest.getRicConfigs().size(), "Ric not deleted from configurations.");
114 public void gettingNotAddedRicShouldThrowException() {
115 ApplicationConfig appConfigUnderTest = new ApplicationConfig();
117 appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1), null, null);
119 Exception exception = assertThrows(ServiceException.class, () -> {
120 appConfigUnderTest.getRic("name");
123 assertEquals("Could not find ric: name", exception.getMessage());