1b03de061f87403c3ec1462b4220adafe13f878b
[dcaegen2/services.git] /
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  slice-analysis-ms
4  *  ================================================================================
5  *   Copyright (C) 2022 Huawei Canada Limited.
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
22 package org.onap.slice.analysis.ms.service.ccvpn;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mockito;
29 import org.mockito.Spy;
30 import org.springframework.boot.test.context.SpringBootTest;
31 import org.springframework.test.context.junit4.SpringRunner;
32
33 import java.lang.reflect.Field;
34 import java.util.Arrays;
35 import java.util.concurrent.ConcurrentMap;
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.updateMaxBw("cll-test", 100, false);
62         assertEquals(datastore.getMaxBwOfSvc("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.updateMaxBw("cll-01", "300");
104         Mockito.verify(datastore, Mockito.atLeastOnce()).updateMaxBw(Mockito.any(String.class), Mockito.any(String.class));
105     }
106
107     @Test
108     public void addUsedBwToEndpointTest() {
109         datastore.addUsedBwToEndpoint("cll-01", "uni-n1", "300Mb");
110         datastore.addUsedBwToEndpoint("cll-01", "uni-n1", "300mb");
111         datastore.addUsedBwToEndpoint("cll-01", "uni-n1", "300Gb");
112         datastore.addUsedBwToEndpoint("cll-01", "uni-n1", "300kb");
113         assertTrue(datastore.readToArray("cll-01", "uni-n1") == null);
114         datastore.addUsedBwToEndpoint("cll-01", "uni-n1", "300.00");
115         assertTrue(Arrays.stream(datastore.readToArray("cll-01", "uni-n1"))
116                 .mapToInt(o -> (int)o)
117                 .sum() == 300602 );
118     }
119 }