ec277c5be68fcf3718eabbfe53cada0c1727a485
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / tasks / StartupServiceTest.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.tasks;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.ArgumentMatchers.anyString;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.verifyNoMoreInteractions;
29 import static org.mockito.Mockito.when;
30 import static org.oransc.policyagent.repository.Ric.RicState.ACTIVE;
31
32 import java.util.Vector;
33
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.mockito.junit.jupiter.MockitoExtension;
40 import org.oransc.policyagent.clients.RicClient;
41 import org.oransc.policyagent.configuration.ApplicationConfig;
42 import org.oransc.policyagent.configuration.ImmutableRicConfig;
43 import org.oransc.policyagent.configuration.RicConfig;
44 import org.oransc.policyagent.exceptions.ServiceException;
45 import org.oransc.policyagent.repository.ImmutablePolicyType;
46 import org.oransc.policyagent.repository.PolicyType;
47 import org.oransc.policyagent.repository.PolicyTypes;
48 import org.oransc.policyagent.repository.Ric;
49 import org.oransc.policyagent.repository.Rics;
50
51 @ExtendWith(MockitoExtension.class)
52 @RunWith(MockitoJUnitRunner.class)
53 public class StartupServiceTest {
54     private static final String FIRST_RIC_NAME = "first";
55     private static final String FIRST_RIC_URL = "firstUrl";
56     private static final String SECOND_RIC_NAME = "second";
57     private static final String SECOND_RIC_URL = "secondUrl";
58     private static final String MANAGED_NODE_A = "nodeA";
59     private static final String MANAGED_NODE_B = "nodeB";
60     private static final String MANAGED_NODE_C = "nodeC";
61
62     private static final String POLICY_TYPE_1_NAME = "type1";
63     private static final String POLICY_TYPE_2_NAME = "type2";
64
65     @Mock
66     ApplicationConfig appConfigMock;
67
68     @Mock
69     RicClient ricClientMock;
70
71     @Test
72     public void startup_allOk() throws ServiceException {
73         Vector<RicConfig> ricConfigs = new Vector<>(2);
74         ricConfigs.add(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A));
75         ricConfigs.add(getRicConfig(SECOND_RIC_NAME, SECOND_RIC_URL, MANAGED_NODE_B, MANAGED_NODE_C));
76         when(appConfigMock.getRicConfigs()).thenReturn(ricConfigs);
77
78         Vector<PolicyType> firstTypes = new Vector<>();
79         PolicyType type1 = ImmutablePolicyType.builder().name(POLICY_TYPE_1_NAME).jsonSchema("{}").build();
80         firstTypes.add(type1);
81         Vector<PolicyType> secondTypes = new Vector<>();
82         secondTypes.add(type1);
83         PolicyType type2 = ImmutablePolicyType.builder().name(POLICY_TYPE_2_NAME).jsonSchema("{}").build();
84         secondTypes.add(type2);
85         when(ricClientMock.getPolicyTypes(anyString())).thenReturn(firstTypes, secondTypes);
86
87         Rics rics = new Rics();
88         PolicyTypes policyTypes = new PolicyTypes();
89         StartupService serviceUnderTest = new StartupService(appConfigMock, rics, policyTypes, ricClientMock);
90
91         serviceUnderTest.startup();
92
93         verify(ricClientMock).deleteAllPolicies(FIRST_RIC_URL);
94         verify(ricClientMock).getPolicyTypes(FIRST_RIC_URL);
95         verify(ricClientMock).deleteAllPolicies(SECOND_RIC_URL);
96         verify(ricClientMock).getPolicyTypes(SECOND_RIC_URL);
97         verifyNoMoreInteractions(ricClientMock);
98
99         assertEquals(2, policyTypes.size(), "Not correct number of policy types added.");
100         assertEquals(type1, policyTypes.getType(POLICY_TYPE_1_NAME), "Not correct type added.");
101         assertEquals(type2, policyTypes.getType(POLICY_TYPE_2_NAME), "Not correct type added.");
102         assertEquals(2, rics.size(), "Correct nymber of Rics not added to Rics");
103
104         Ric firstRic = rics.getRic(FIRST_RIC_NAME);
105         assertNotNull(firstRic, "Ric \"" + FIRST_RIC_NAME + "\" not added to repositpry");
106         assertEquals(FIRST_RIC_NAME, firstRic.name(), "Not correct Ric \"" + FIRST_RIC_NAME + "\" added to Rics");
107         assertEquals(ACTIVE, firstRic.state(), "Not correct state for \"" + FIRST_RIC_NAME + "\"");
108         assertEquals(1, firstRic.getSupportedPolicyTypes().size(), "Not correct no of types supported");
109         assertTrue(firstRic.isSupportingType(type1.name()), "Not correct type supported");
110         assertEquals(1, firstRic.getManagedNodes().size(), "Not correct no of managed nodes");
111         assertTrue(firstRic.isManaging(MANAGED_NODE_A), "Not managed by node");
112
113         Ric secondRic = rics.getRic(SECOND_RIC_NAME);
114         assertNotNull(secondRic, "Ric \"" + SECOND_RIC_NAME + "\" not added to repositpry");
115         assertEquals(SECOND_RIC_NAME, secondRic.name(), "Not correct Ric \"" + SECOND_RIC_NAME + "\" added to Rics");
116         assertEquals(ACTIVE, secondRic.state(), "Not correct state for \"" + SECOND_RIC_NAME + "\"");
117         assertEquals(2, secondRic.getSupportedPolicyTypes().size(), "Not correct no of types supported");
118         assertTrue(secondRic.isSupportingType(type1.name()), "Not correct type supported");
119         assertTrue(secondRic.isSupportingType(type2.name()), "Not correct type supported");
120         assertEquals(2, secondRic.getManagedNodes().size(), "Not correct no of managed nodes");
121         assertTrue(secondRic.isManaging(MANAGED_NODE_B), "Not correct managed node");
122         assertTrue(secondRic.isManaging(MANAGED_NODE_C), "Not correct managed node");
123     }
124
125     private RicConfig getRicConfig(String name, String baseUrl, String... nodeNames) {
126         Vector<String> managedNodes = new Vector<String>(1);
127         for (String nodeName : nodeNames) {
128             managedNodes.add(nodeName);
129         }
130         ImmutableRicConfig ricConfig = ImmutableRicConfig.builder() //
131             .name(name) //
132             .managedElementIds(managedNodes) //
133             .baseUrl(baseUrl) //
134             .build();
135         return ricConfig;
136     }
137 }