Bugfix, RIC configs would disappear after one minute
[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.assertNull;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.Properties;
31 import java.util.Vector;
32
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.mockito.junit.jupiter.MockitoExtension;
36 import org.oransc.policyagent.configuration.ApplicationConfig.RicConfigUpdate;
37 import org.oransc.policyagent.configuration.ApplicationConfigParser.ConfigParserResult;
38 import org.oransc.policyagent.exceptions.ServiceException;
39
40 @ExtendWith(MockitoExtension.class)
41 public class ApplicationConfigTest {
42
43     private static final ImmutableRicConfig RIC_CONFIG_1 = ImmutableRicConfig.builder() //
44         .name("ric1") //
45         .baseUrl("ric1_url") //
46         .managedElementIds(new Vector<>()) //
47         .controllerName("") //
48         .build();
49
50     ConfigParserResult configParserResult(RicConfig... rics) {
51         return ImmutableConfigParserResult.builder() //
52             .ricConfigs(Arrays.asList(rics)) //
53             .dmaapConsumerConfig(new Properties()) //
54             .dmaapPublisherConfig(new Properties()) //
55             .controllerConfigs(new HashMap<>()) //
56             .build();
57     }
58
59     @Test
60     public void gettingNotAddedRicShouldThrowException() {
61         ApplicationConfig appConfigUnderTest = new ApplicationConfig();
62
63         appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1));
64
65         Exception exception = assertThrows(ServiceException.class, () -> {
66             appConfigUnderTest.getRic("name");
67         });
68
69         assertEquals("Could not find ric configuration: name", exception.getMessage());
70     }
71
72     @Test
73     public void addRicShouldNotifyAllObserversOfRicAdded() throws Exception {
74         ApplicationConfig appConfigUnderTest = new ApplicationConfig();
75
76         RicConfigUpdate update = appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1)).blockFirst();
77         assertEquals(RicConfigUpdate.Type.ADDED, update.getType());
78         assertTrue(appConfigUnderTest.getRicConfigs().contains(RIC_CONFIG_1), "Ric not added to configurations.");
79
80         assertEquals(RIC_CONFIG_1, appConfigUnderTest.getRic(RIC_CONFIG_1.name()),
81             "Not correct Ric retrieved from configurations.");
82
83         update = appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1)).blockFirst();
84         assertNull(update, "Nothing should be updated");
85         assertTrue(appConfigUnderTest.getRicConfigs().contains(RIC_CONFIG_1), "Ric should remain.");
86
87     }
88
89     @Test
90     public void changedRicShouldNotifyAllObserversOfRicChanged() throws Exception {
91         ApplicationConfig appConfigUnderTest = new ApplicationConfig();
92
93         appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1));
94
95         ImmutableRicConfig changedRicConfig = ImmutableRicConfig.builder() //
96             .name("ric1") //
97             .baseUrl("changed_ric1_url") //
98             .managedElementIds(new Vector<>()) //
99             .controllerName("") //
100             .build();
101
102         RicConfigUpdate update = appConfigUnderTest.setConfiguration(configParserResult(changedRicConfig)).blockFirst();
103
104         assertEquals(RicConfigUpdate.Type.CHANGED, update.getType());
105         assertEquals(changedRicConfig, appConfigUnderTest.getRic(RIC_CONFIG_1.name()),
106             "Changed Ric not retrieved from configurations.");
107     }
108
109     @Test
110     public void removedRicShouldNotifyAllObserversOfRicRemoved() {
111         ApplicationConfig appConfigUnderTest = new ApplicationConfig();
112
113         ImmutableRicConfig ricConfig2 = ImmutableRicConfig.builder() //
114             .name("ric2") //
115             .baseUrl("ric2_url") //
116             .managedElementIds(new Vector<>()) //
117             .controllerName("") //
118             .build();
119
120         appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1, ricConfig2));
121
122         RicConfigUpdate update = appConfigUnderTest.setConfiguration(configParserResult(ricConfig2)).blockFirst();
123
124         assertEquals(RicConfigUpdate.Type.REMOVED, update.getType());
125         assertEquals(1, appConfigUnderTest.getRicConfigs().size(), "Ric not deleted from configurations.");
126     }
127
128 }