Add test for ApplicationConfig
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / configuration / ApplicationConfigTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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.oransc.policyagent.configuration;
22
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;
27
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;
36
37 @ExtendWith(MockitoExtension.class)
38 public class ApplicationConfigTest {
39     @Mock
40     Observer observerMock1;
41
42     @Mock
43     Observer observerMock2;
44
45     private static final ImmutableRicConfig RIC_CONFIG_1 = ImmutableRicConfig.builder() //
46         .name("ric1") //
47         .baseUrl("ric1_url") //
48         .managedElementIds(new Vector<>()) //
49         .build();
50
51     @Test
52     public void addRicShouldNotifyAllObserversOfRicAdded() throws Exception {
53         ApplicationConfig appConfigUnderTest = new ApplicationConfig();
54
55         appConfigUnderTest.addObserver(observerMock1);
56         appConfigUnderTest.addObserver(observerMock2);
57
58         appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1), null, null);
59
60         verify(observerMock1).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.ADDED);
61         verify(observerMock2).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.ADDED);
62
63         assertTrue(appConfigUnderTest.getRicConfigs().contains(RIC_CONFIG_1), "Ric not added to configuraions.");
64
65         assertEquals(RIC_CONFIG_1, appConfigUnderTest.getRic(RIC_CONFIG_1.name()),
66             "Not correct Ric retrieved from configurations.");
67     }
68
69     @Test
70     public void changedRicShouldNotifyAllObserversOfRicChanged() throws Exception {
71         ApplicationConfig appConfigUnderTest = new ApplicationConfig();
72
73         appConfigUnderTest.addObserver(observerMock1);
74
75         appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1), null, null);
76
77         ImmutableRicConfig changedRicConfig = ImmutableRicConfig.builder() //
78             .name("ric1") //
79             .baseUrl("changed_ric1_url") //
80             .managedElementIds(new Vector<>()) //
81             .build();
82
83         appConfigUnderTest.setConfiguration(Arrays.asList(changedRicConfig), null, null);
84
85         verify(observerMock1).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.ADDED);
86         verify(observerMock1).onRicConfigUpdate(changedRicConfig, ApplicationConfig.RicConfigUpdate.CHANGED);
87
88         assertEquals(changedRicConfig, appConfigUnderTest.getRic(RIC_CONFIG_1.name()),
89             "Changed Ric not retrieved from configurations.");
90     }
91
92     @Test
93     public void removedRicShouldNotifyAllObserversOfRicRemoved() {
94         ApplicationConfig appConfigUnderTest = new ApplicationConfig();
95
96         appConfigUnderTest.addObserver(observerMock1);
97
98         ImmutableRicConfig ricConfig2 = ImmutableRicConfig.builder() //
99             .name("ric2") //
100             .baseUrl("ric2_url") //
101             .managedElementIds(new Vector<>()) //
102             .build();
103
104         appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1, ricConfig2), null, null);
105
106         appConfigUnderTest.setConfiguration(Arrays.asList(ricConfig2), null, null);
107
108         verify(observerMock1).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.REMOVED);
109
110         assertEquals(1, appConfigUnderTest.getRicConfigs().size(), "Ric not deleted from configurations.");
111     }
112
113     @Test
114     public void gettingNotAddedRicShouldThrowException() {
115         ApplicationConfig appConfigUnderTest = new ApplicationConfig();
116
117         appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1), null, null);
118
119         Exception exception = assertThrows(ServiceException.class, () -> {
120             appConfigUnderTest.getRic("name");
121         });
122
123         assertEquals("Could not find ric: name", exception.getMessage());
124     }
125 }