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