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