[SLICEMS] Add bw decrease logics and enhance runtime config feature
[dcaegen2/services.git] / components / slice-analysis-ms / src / test / java / org / onap / slice / analysis / ms / service / ccvpn / CCVPNPmDatastoreTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  slice-analysis-ms
4  *  ================================================================================
5  *   Copyright (C) 2022 Huawei Canada Limited.
6  *   Copyright (C) 2022 Huawei Technologies Co., Ltd.
7  *   ==============================================================================
8  *     Licensed under the Apache License, Version 2.0 (the "License");
9  *     you may not use this file except in compliance with the License.
10  *     You may obtain a copy of the License at
11  *
12  *          http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *     Unless required by applicable law or agreed to in writing, software
15  *     distributed under the License is distributed on an "AS IS" BASIS,
16  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *     See the License for the specific language governing permissions and
18  *     limitations under the License.
19  *     ============LICENSE_END=========================================================
20  *
21  *******************************************************************************/
22
23 package org.onap.slice.analysis.ms.service.ccvpn;
24
25 import java.util.HashSet;
26 import java.util.Set;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mockito;
31 import org.mockito.Spy;
32 import org.springframework.boot.test.context.SpringBootTest;
33 import org.springframework.test.context.junit4.SpringRunner;
34
35 import java.util.Arrays;
36
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertTrue;
39
40
41 @RunWith(SpringRunner.class)
42 @SpringBootTest(classes = CCVPNPmDatastoreTest.class)
43 public class CCVPNPmDatastoreTest {
44
45     @Spy
46     @InjectMocks
47     CCVPNPmDatastore datastore;
48
49     @Test
50     public void getUsedBwOfSvcTest() {
51         datastore.addUsedBwToEndpoint("cll-test", "uni-01", "100");
52         datastore.addUsedBwToEndpoint("cll-test", "uni-01", "100");
53         datastore.addUsedBwToEndpoint("cll-test", "uni-02", "100");
54         datastore.addUsedBwToEndpoint("cll-test2", "uni-01", "100");
55         assertEquals(datastore.getUsedBwOfSvc("cll-test").get(new Endpointkey("cll-test", "uni-01")).size(),
56         2);
57     }
58
59     @Test
60     public void getMaxBwOfSvcTest() {
61         datastore.updateProvBw("cll-test", 100, false);
62         assertEquals(datastore.getProvBwOfSvc("cll-test"), Integer.valueOf(100));
63     }
64
65     @Test
66     public void getStatusOfSvcTest() {
67         datastore.updateSvcState("cll-01", ServiceState.RUNNING);
68         assertEquals(datastore.getStatusOfSvc("cll-01"), ServiceState.RUNNING);
69     }
70
71     @Test
72     public void getSvcStatusMapTest() {
73         datastore.updateSvcState("cll-01", ServiceState.RUNNING);
74         datastore.getSvcStatusMap();
75         Mockito.verify(datastore, Mockito.atLeastOnce()).getSvcStatusMap();
76     }
77
78     @Test
79     public void getUsedBwMapTest() {
80         datastore.updateSvcState("cll-01", ServiceState.RUNNING);
81         datastore.getUsedBwMap();
82         Mockito.verify(datastore, Mockito.atLeastOnce()).getUsedBwMap();
83     }
84
85     @Test
86     public void updateSvcStateTest() {
87         datastore.updateSvcState("cll-01", ServiceState.RUNNING);
88         assertEquals(datastore.getStatusOfSvc("cll-01"), ServiceState.RUNNING);
89     }
90
91     @Test
92     public void readToArrayTest() {
93         for(int i = 0; i < 5; i++){
94             datastore.addUsedBwToEndpoint("cll-01", "uni-n1", "300");
95         }
96         assertTrue(Arrays.stream(datastore.readToArray("cll-01", "uni-n1"))
97                 .mapToInt(o -> (int)o)
98                 .allMatch(n -> n == 1));
99     }
100
101     @Test
102     public void updateMaxBwTest() throws NoSuchFieldException, IllegalAccessException {
103         datastore.updateProvBw("cll-01", "300");
104         Mockito.verify(datastore, Mockito.atLeastOnce()).updateProvBw(Mockito.any(String.class), Mockito.any(String.class));
105     }
106
107     @Test
108     public void updateUpperBoundBwTest() throws NoSuchFieldException, IllegalAccessException {
109         datastore.updateUpperBoundBw("cll-01", 300);
110         Mockito.verify(datastore, Mockito.atLeastOnce()).updateUpperBoundBw(Mockito.any(String.class), Mockito.any(Integer.class));
111     }
112
113     @Test
114     public void addUsedBwToEndpointTest() {
115         datastore.addUsedBwToEndpoint("cll-01", "uni-n1", "300Mb");
116         datastore.addUsedBwToEndpoint("cll-01", "uni-n1", "300mb");
117         datastore.addUsedBwToEndpoint("cll-01", "uni-n1", "300Gb");
118         datastore.addUsedBwToEndpoint("cll-01", "uni-n1", "300kb");
119         assertTrue(datastore.readToArray("cll-01", "uni-n1") == null);
120         datastore.addUsedBwToEndpoint("cll-01", "uni-n1", "300.00");
121         assertTrue(Arrays.stream(datastore.readToArray("cll-01", "uni-n1"))
122                 .mapToInt(o -> (int)o)
123                 .sum() == 300602 );
124     }
125
126     @Test
127     public void updateCllInstancesTest() {
128         datastore.addUsedBwToEndpoint("cll-01", "uni-n1", "300Mb");
129         datastore.updateUpperBoundBw("cll-01", 300);
130         datastore.updateProvBw("cll-01", "300");
131         datastore.updateSvcState("cll-01", ServiceState.RUNNING);
132         datastore.updateOriginalBw("cll-01", 1000);
133         datastore.updateClosedloopStatus("cll-01", true);
134         datastore.addUsedBwToEndpoint("cll-02", "uni-n2", "300Mb");
135         datastore.updateUpperBoundBw("cll-02", 300);
136         datastore.updateProvBw("cll-02", "300");
137         datastore.updateSvcState("cll-02", ServiceState.RUNNING);
138         datastore.updateOriginalBw("cll-02", 1000);
139         datastore.updateClosedloopStatus("cll-02", true);
140         Set<String> cllId = new HashSet<>();
141         cllId.add("cll-01");
142         datastore.updateCllInstances(cllId);
143         assertEquals(datastore.getEndpointToUsedBw().keySet().size(), 1);
144         assertEquals(datastore.getUpperBoundBw().keySet().equals(cllId), true);
145         assertEquals(datastore.getEndpointToProvBw().keySet().equals(cllId), true);
146         assertEquals(datastore.getSvcStatus().keySet().equals(cllId), true);
147         assertEquals(datastore.getEndpointToOriginalBw().keySet().equals(cllId), true);
148     }
149 }