3adfb3164c3c8c1ce9a9b12acefb491b422842e8
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2022 Nordix Foundation. All rights reserved.
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.onap.ccsdk.oran.a1policymanagementservice.tasks;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.spy;
25
26 import io.micrometer.prometheus.PrometheusConfig;
27 import io.micrometer.prometheus.PrometheusMeterRegistry;
28 import java.time.Instant;
29 import java.util.Arrays;
30 import java.util.Vector;
31 import org.junit.jupiter.api.BeforeEach;
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.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
36 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
37 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
38 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
39 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
40 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
41 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
42 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
43
44 @ExtendWith(MockitoExtension.class)
45 public class RefreshCounterTaskTest {
46
47     private static final String POLICY_TYPE_1_NAME = "type1";
48     private static final PolicyType POLICY_TYPE_1 = PolicyType.builder().id(POLICY_TYPE_1_NAME).schema("").build();
49
50     private static final Ric RIC_1 = new Ric(RicConfig.builder().ricId("ric_1").baseUrl("baseUrl1")
51             .managedElementIds(new Vector<String>(Arrays.asList("kista_1", "kista_2"))).controllerName("controllerName")
52             .build());
53
54     private static final String POLICY_1_ID = "policyId1";
55     private static final Policy POLICY_1 = Policy.builder().id(POLICY_1_ID).json("").ownerServiceId("service")
56             .ric(RIC_1).type(POLICY_TYPE_1).lastModified(Instant.now()).isTransient(false)
57             .statusNotificationUri("statusNotificationUri").build();
58
59     private PrometheusMeterRegistry prometheusMeterRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
60     private final ApplicationConfig appConfig = new ApplicationConfig();
61
62     private PolicyTypes types;
63     private Policies policies;
64     private Rics rics = new Rics();
65
66     @BeforeEach
67     void init() {
68         types = new PolicyTypes(appConfig);
69         policies = new Policies(appConfig);
70
71         rics.clear();
72         RIC_1.setState(Ric.RicState.AVAILABLE);
73         RIC_1.clearSupportedPolicyTypes();
74     }
75
76     @Test
77     void testCounters_whenNeitherChangedPoliciesNorPolicyTypes() {
78         RIC_1.setState(Ric.RicState.AVAILABLE);
79         RIC_1.addSupportedPolicyType(POLICY_TYPE_1);
80         rics.put(RIC_1);
81
82         types.put(POLICY_TYPE_1);
83
84         policies.put(POLICY_1);
85
86         RefreshCounterTask refreshCounterTaskUnderTest = spy(createRefreshCounterTask());
87         refreshCounterTaskUnderTest.checkAllCounters();
88
89         assertThat(prometheusMeterRegistry.get("total_ric_count").gauge().value()).isEqualTo(1);
90         assertThat(prometheusMeterRegistry.get("total_policy_type_count").gauge().value()).isEqualTo(1);
91         assertThat(prometheusMeterRegistry.get("total_policy_count").gauge().value()).isEqualTo(1);
92     }
93
94     @Test
95     void testCounters_whenChangedPoliciesAndNoChangedPolicyTypes() {
96         RIC_1.setState(Ric.RicState.AVAILABLE);
97         RIC_1.addSupportedPolicyType(POLICY_TYPE_1);
98         rics.put(RIC_1);
99
100         types.put(POLICY_TYPE_1);
101
102         policies.put(POLICY_1);
103
104         String POLICY_2_ID = "policyId2";
105         Policy POLICY_2 = Policy.builder() //
106                 .id(POLICY_2_ID) //
107                 .json("") //
108                 .ownerServiceId("service") //
109                 .ric(RIC_1) //
110                 .type(POLICY_TYPE_1) //
111                 .lastModified(Instant.now()) //
112                 .isTransient(false) //
113                 .statusNotificationUri("statusNotificationUri") //
114                 .build();
115
116         policies.put(POLICY_2);
117
118         RefreshCounterTask refreshCounterTaskUnderTest = spy(createRefreshCounterTask());
119         refreshCounterTaskUnderTest.checkAllCounters();
120
121         assertThat(prometheusMeterRegistry.get("total_ric_count").gauge().value()).isEqualTo(1);
122         assertThat(prometheusMeterRegistry.get("total_policy_type_count").gauge().value()).isEqualTo(1);
123         assertThat(prometheusMeterRegistry.get("total_policy_count").gauge().value()).isEqualTo(2);
124     }
125
126     @Test
127     void testCounters_whenNoChangedPoliciesAndChangedPolicyTypes() {
128         RIC_1.setState(Ric.RicState.AVAILABLE);
129
130         String POLICY_TYPE_2_NAME = "type2";
131         PolicyType POLICY_TYPE_2 = PolicyType.builder() //
132                 .id(POLICY_TYPE_2_NAME) //
133                 .schema("") //
134                 .build();
135
136         RIC_1.addSupportedPolicyType(POLICY_TYPE_1);
137         RIC_1.addSupportedPolicyType(POLICY_TYPE_2);
138         rics.put(RIC_1);
139
140         types.put(POLICY_TYPE_1);
141         types.put(POLICY_TYPE_2);
142
143         policies.put(POLICY_1);
144
145         RefreshCounterTask refreshCounterTaskUnderTest = spy(createRefreshCounterTask());
146         refreshCounterTaskUnderTest.checkAllCounters();
147
148         assertThat(prometheusMeterRegistry.get("total_ric_count").gauge().value()).isEqualTo(1);
149         assertThat(prometheusMeterRegistry.get("total_policy_type_count").gauge().value()).isEqualTo(2);
150         assertThat(prometheusMeterRegistry.get("total_policy_count").gauge().value()).isEqualTo(1);
151     }
152
153     private RefreshCounterTask createRefreshCounterTask() {
154         return new RefreshCounterTask(rics, types, policies, prometheusMeterRegistry);
155     }
156 }