aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/dcaegen2/services/sonhms/utils/ClusterUtilsTest.java
blob: 104ec886f92dbc45ef2d257a3654ce4efe48a24d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*******************************************************************************
 *  ============LICENSE_START=======================================================
 *  son-handler
 *  ================================================================================
 *   Copyright (C) 2019-2021 Wipro Limited.
 *   ==============================================================================
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *  
 *          http://www.apache.org/licenses/LICENSE-2.0
 *  
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 *     ============LICENSE_END=========================================================
 *  
 *******************************************************************************/

package org.onap.dcaegen2.services.sonhms.utils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import fj.data.Either;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.onap.dcaegen2.services.sonhms.NotificationToClusterMapping;
import org.onap.dcaegen2.services.sonhms.child.Graph;
import org.onap.dcaegen2.services.sonhms.dao.ClusterDetailsRepository;
import org.onap.dcaegen2.services.sonhms.entity.ClusterDetails;
import org.onap.dcaegen2.services.sonhms.exceptions.ConfigDbNotFoundException;
import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
import org.onap.dcaegen2.services.sonhms.model.FapServiceList;
import org.onap.dcaegen2.services.sonhms.model.Notification;
import org.onap.dcaegen2.services.sonhms.Configuration;
import org.onap.dcaegen2.services.sonhms.exceptions.CpsNotFoundException;
import org.onap.dcaegen2.services.sonhms.restclient.ConfigInterface;
import org.onap.dcaegen2.services.sonhms.restclient.ConfigurationClient;
import org.onap.dcaegen2.services.sonhms.restclient.SdnrRestClient;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest({ SdnrRestClient.class, BeanUtil.class, ConfigurationClient.class })
@SpringBootTest(classes = ClusterUtils.class)
public class ClusterUtilsTest {

    @Mock
    private ClusterDetailsRepository clusterDetailsRepositoryMock;

    @InjectMocks
    ClusterUtils clusterUtils;

    private static Notification notification1;
    private static Notification notification2;
    private static List<ClusterDetails> clusterDetailsForGetClusterDetailsFromClusterIdTest;
    private static Graph cluster;
    private static List<ClusterDetails> clusterDetails = new ArrayList<>();

    @BeforeClass
    public static void setup() {

        Configuration config = Configuration.getInstance();
        config.setConfigClientType("ConfigDB");
        notification1 = new Notification();
        notification2 = new Notification();
        clusterDetailsForGetClusterDetailsFromClusterIdTest = new ArrayList<ClusterDetails>();

        String notificationString1 = readFromFile("/notification1.json");
        String notificationString2 = readFromFile("/notification2.json");
        String clusterDetailsListString = readFromFile("/ClusterDetailsTest.json");

        String clusterInfo1 = readFromFile("/clusterInfo1.json");
        String clusterInfo2 = readFromFile("/clusterInfo2.json");
        String clusterInfo3 = readFromFile("/clusterInfo3.json");
        String clusterInfo4 = readFromFile("/clusterInfo4.json");
        String clusterInfo = readFromFile("/clusterInfo5.json");
        cluster = new Graph(clusterInfo);

        clusterDetails.add(new ClusterDetails("1", clusterInfo1, 35));
        clusterDetails.add(new ClusterDetails("2", clusterInfo2, 36));
        clusterDetails.add(new ClusterDetails("3", clusterInfo3, 37));
        clusterDetails.add(new ClusterDetails("4", clusterInfo4, 38));

        ObjectMapper mapper = new ObjectMapper();

        try {
            notification1 = mapper.readValue(notificationString1, Notification.class);
            notification2 = mapper.readValue(notificationString2, Notification.class);
            clusterDetailsForGetClusterDetailsFromClusterIdTest = mapper.readValue(clusterDetailsListString,
                    new TypeReference<List<ClusterDetails>>() {
                    });
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Before
    public void setupTest() {
        clusterUtils = new ClusterUtils();
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void getClustersForNotificationTest() {

        NotificationToClusterMapping expected = new NotificationToClusterMapping();
        Map<FapServiceList, String> cellsinCluster = new HashMap<>();
        cellsinCluster.put(notification1.getPayload().getRadioAccess().getFapServiceList().get(0), "2");
        expected.setCellsinCluster(cellsinCluster);
        expected.setNewCells(new ArrayList<FapServiceList>());

        NotificationToClusterMapping result = clusterUtils.getClustersForNotification(notification1, clusterDetails);
        assertEquals(expected, result);

        expected = new NotificationToClusterMapping();
        List<FapServiceList> newCells = new ArrayList<>();
        newCells.add(notification2.getPayload().getRadioAccess().getFapServiceList().get(0));
        expected.setCellsinCluster(new HashMap<>());
        expected.setNewCells(newCells);

        result = clusterUtils.getClustersForNotification(notification2, clusterDetails);
        assertEquals(expected, result);
    }

    @Test
    public void createClusterTest() throws Exception {

        Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = new HashMap<CellPciPair, ArrayList<CellPciPair>>();

        List<CellPciPair> firstNbrList = new ArrayList<>();
        List<CellPciPair> nbrList = new ArrayList<>();

        firstNbrList.add(new CellPciPair("48", 0));
        nbrList.add(new CellPciPair("44", 3));

        PowerMockito.mockStatic(SdnrRestClient.class);
        PowerMockito.mockStatic(ConfigurationClient.class);

        SdnrRestClient sdnr = PowerMockito.spy(new SdnrRestClient());
        Configuration config = Configuration.getInstance();

        PowerMockito.whenNew(SdnrRestClient.class).withAnyArguments().thenReturn(sdnr);
        PowerMockito.when(config.getConfigurationClient()).thenReturn(sdnr);
        PowerMockito.doReturn(nbrList).when(sdnr, "getNbrList", Mockito.anyString());

        clusterMap.put(new CellPciPair("45", 310), (ArrayList<CellPciPair>) firstNbrList);

        assertEquals(cluster, clusterUtils.createCluster(clusterMap));

    }

    @Test
    public void getClusterDetailsFromClusterIdTest() {
        ClusterDetails responseValue = null;
        Integer responseVal = null;
        Integer expectedValue = 404;
        Either<ClusterDetails, Integer> response = clusterUtils.getClusterDetailsFromClusterId("0",
                clusterDetailsForGetClusterDetailsFromClusterIdTest);
        assertTrue(response.isLeft());
        if (response.isLeft()) {
            responseValue = response.left().value();
        }
        assertEquals(clusterDetailsForGetClusterDetailsFromClusterIdTest.get(0), responseValue);
        response = clusterUtils.getClusterDetailsFromClusterId("1",
                clusterDetailsForGetClusterDetailsFromClusterIdTest);
        assertTrue(response.isLeft());
        if (response.isLeft()) {
            responseValue = response.left().value();
        }
        assertEquals(clusterDetailsForGetClusterDetailsFromClusterIdTest.get(1), responseValue);
        response = clusterUtils.getClusterDetailsFromClusterId("9",
                clusterDetailsForGetClusterDetailsFromClusterIdTest);
        assertTrue(response.isRight());
        if (response.isRight()) {
            responseVal = response.right().value();
        }
        assertEquals(expectedValue, responseVal);

    }

    @Test
    public void saveClusterTest() {
        ClusterDetails details = new ClusterDetails();
        details.setClusterId("123e4567-e89b-12d3-a456-426655440000");
        details.setClusterInfo("cellPciNeighbourString");
        details.setChildThreadId(978668);
        PowerMockito.mockStatic(BeanUtil.class);
        PowerMockito.when(BeanUtil.getBean(ClusterDetailsRepository.class)).thenReturn(clusterDetailsRepositoryMock);
        Mockito.when(clusterDetailsRepositoryMock.save(details)).thenReturn(details);
        Long threadId = (long) 978668;
        clusterUtils.saveCluster(cluster, UUID.fromString("123e4567-e89b-12d3-a456-426655440000"), threadId);
        assertEquals(details, clusterDetailsRepositoryMock.save(details));

    }

    @Test
    public void getClusterForCellTest() {
        FapServiceList fapServiceList = notification1.getPayload().getRadioAccess().getFapServiceList().get(0);
        String clusterInfo1 = readFromFile("/clusterInfo1.json");
        String clusterInfo2 = readFromFile("/clusterInfo2.json");
        Graph graph1 = new Graph(clusterInfo1);
        Graph graph2 = new Graph(clusterInfo2);
        List<Graph> newClusters = new ArrayList<Graph>();
        newClusters.add(graph1);
        newClusters.add(graph2);
        Either<Graph, Integer> result = clusterUtils.getClusterForCell(fapServiceList, newClusters);
        assertTrue(result.isLeft());

        newClusters = new ArrayList<>();
        newClusters.add(graph1);
        result = clusterUtils.getClusterForCell(fapServiceList, newClusters);
        assertTrue(result.isRight());
        int resultRight = result.right().value();
        assertEquals(404, resultRight);

        List<Graph> emptyList = new ArrayList<Graph>();

        result = clusterUtils.getClusterForCell(fapServiceList, emptyList);
        assertTrue(result.isRight());
        resultRight = result.right().value();
        assertEquals(404, resultRight);

    }

    @Test
    public void modifyClusterTest() {

        String clusterInfo = readFromFile("/clusterInfo2.json");
        String clusterInfo2 = readFromFile("/clusterInfo6.json");

        Graph cluster = new Graph(clusterInfo);
        Graph expected = new Graph(clusterInfo2);
        Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = new HashMap<CellPciPair, ArrayList<CellPciPair>>();

        ArrayList<CellPciPair> firstNbrList = new ArrayList<>();
        firstNbrList.add(new CellPciPair("48",0));
        clusterMap.put(new CellPciPair("45",310),firstNbrList);
        assertEquals(expected, clusterUtils.modifyCluster(cluster,clusterMap));
    }

    private static String readFromFile(String file) {
        String content = new String();
        try {

            InputStream is = ClusterUtilsTest.class.getResourceAsStream(file);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
            content = bufferedReader.readLine();
            String temp;
            while ((temp = bufferedReader.readLine()) != null) {
                content = content.concat(temp);
            }
            content = content.trim();
            bufferedReader.close();
        } catch (Exception e) {
            content = null;
        }
        return content;
    }
}