Fix bugs and formatting issues
[dcaegen2/services/son-handler.git] / src / test / java / org / onap / dcaegen2 / services / sonhms / child / TestPnfUtils.java
1 /*******************************************************************************
2  * ============LICENSE_START=======================================================
3  * son-handler
4  *  ================================================================================
5  *  Copyright (C) 2019 Wipro 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 package org.onap.dcaegen2.services.sonhms.child;
22
23 import static org.junit.Assert.assertEquals;
24
25 import com.fasterxml.jackson.core.type.TypeReference;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Optional;
37
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.dcaegen2.services.sonhms.dao.CellInfoRepository;
47 import org.onap.dcaegen2.services.sonhms.entity.CellInfo;
48 import org.onap.dcaegen2.services.sonhms.exceptions.ConfigDbNotFoundException;
49 import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
50 import org.onap.dcaegen2.services.sonhms.restclient.AnrSolutions;
51 import org.onap.dcaegen2.services.sonhms.restclient.SdnrRestClient;
52 import org.onap.dcaegen2.services.sonhms.restclient.Solutions;
53 import org.onap.dcaegen2.services.sonhms.utils.BeanUtil;
54 import org.onap.dcaegen2.services.sonhms.utils.ClusterUtilsTest;
55 import org.powermock.api.mockito.PowerMockito;
56 import org.powermock.core.classloader.annotations.PrepareForTest;
57 import org.powermock.modules.junit4.PowerMockRunner;
58 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
59 import org.slf4j.Logger;
60 import org.springframework.boot.test.context.SpringBootTest;
61 import org.springframework.test.context.junit4.SpringRunner;
62
63 @RunWith(PowerMockRunner.class)
64 @PowerMockRunnerDelegate(SpringRunner.class)
65 @PrepareForTest({BeanUtil.class, SdnrRestClient.class })
66 @SpringBootTest(classes = PnfUtils.class)
67 public class TestPnfUtils {
68
69     @Mock
70     private CellInfoRepository cellInfoRepositoryMock;
71     
72     private static final Logger log = org.slf4j.LoggerFactory.getLogger(TestPnfUtils.class);
73     private static Solutions solutions = new Solutions();
74     private static List<AnrSolutions> anrSolutions = new ArrayList<>();
75     private static Optional<CellInfo> cellInfo;
76     private static Optional<CellInfo> cellInfoNull;
77
78     
79     @InjectMocks
80     PnfUtils pnfUtils;
81     
82      @BeforeClass
83      public static void setup() {
84          
85          
86          String solutionsString=readFromFile("/solutions.json");
87          String anrSolutionsString = readFromFile("/anrSolutions.json");
88          ObjectMapper mapper = new ObjectMapper();
89             
90             try {
91                 solutions=mapper.readValue(solutionsString, Solutions.class);
92                 anrSolutions = mapper.readValue(anrSolutionsString, new TypeReference<ArrayList<AnrSolutions>>() {
93                 });
94             } catch (IOException e) {
95                 log.debug("Exception in StateOof Test "+e);
96                 e.printStackTrace();
97             }
98          
99      }
100      @Before
101      public void setupTest() {
102             cellInfo = Optional.of(new CellInfo("EXP001","ncserver1"));
103             cellInfoNull = Optional.ofNullable(null);
104             pnfUtils = new PnfUtils();
105             MockitoAnnotations.initMocks(this);
106         } 
107      @Test
108      public void getPnfsTest() {
109          Map<String, List<CellPciPair>> pnfs = new HashMap<>();
110          List<CellPciPair> cellpciPairList1=new ArrayList<>();
111          cellpciPairList1.add(new CellPciPair("EXP001",101));
112          List<CellPciPair> cellpciPairList2=new ArrayList<>();
113          cellpciPairList2.add(new CellPciPair("EXP002",102));
114          String pnfName="ncserver2";
115          String cellId="EXP002";
116          PowerMockito.mockStatic(BeanUtil.class);
117          PowerMockito.mockStatic(SdnrRestClient.class);
118
119          PowerMockito.when(BeanUtil.getBean(CellInfoRepository.class))
120                  .thenReturn(cellInfoRepositoryMock);
121          
122         Mockito.when(cellInfoRepositoryMock.findById("EXP001"))
123          .thenReturn(cellInfo);
124         Mockito.when(cellInfoRepositoryMock.findById(cellId))
125         .thenReturn(cellInfoNull);
126         try {
127             PowerMockito.when(SdnrRestClient.getPnfName(cellId))
128             .thenReturn(pnfName);
129             PowerMockito.when(cellInfoRepositoryMock.save(new CellInfo(cellId, pnfName))).thenReturn(new CellInfo());
130         } catch (ConfigDbNotFoundException e) {
131             e.printStackTrace();
132         }
133         pnfs.put(pnfName, cellpciPairList2);
134         pnfs.put("ncserver1", cellpciPairList1);
135         System.out.println(solutions);
136         try {
137             assertEquals(pnfs,pnfUtils.getPnfs(solutions));
138         } catch (ConfigDbNotFoundException e) {
139             log.debug("exception in stateOof test {}", e);
140             e.printStackTrace();
141         }
142      }
143      
144      @Test
145      public void testGetPnfsForAnrSolutions() {
146          Map<String, List<Map<String,List<String>>>> actual = null ;
147          Map<String, List<Map<String,List<String>>>> expected = new HashMap<>();
148          try {
149              PowerMockito.mockStatic(SdnrRestClient.class);
150             PowerMockito.when(SdnrRestClient.getPnfName(Mockito.anyString())).thenReturn("ncServer1");
151             actual = pnfUtils.getPnfsForAnrSolutions(anrSolutions);
152         } catch (ConfigDbNotFoundException e) {
153             e.printStackTrace();
154         }
155         List<String> remNeighbors1 = new ArrayList<>();
156         List<String> remNeighbors2 = new ArrayList<>();
157         remNeighbors1.add("cell2");
158         remNeighbors1.add("cell3");
159         remNeighbors2.add("cell9");
160         Map<String,List<String>> cellRemNeighborsPair1 = new HashMap<>();
161         Map<String,List<String>> cellRemNeighborsPair2 = new HashMap<>();
162         cellRemNeighborsPair1.put("cell1", remNeighbors1);
163         cellRemNeighborsPair2.put("cell8", remNeighbors2);
164         List<Map<String,List<String>>> list = new ArrayList<>();
165         list.add(cellRemNeighborsPair1);
166         list.add(cellRemNeighborsPair2);
167         expected.put("ncServer1", list);
168         assertEquals(expected, actual);
169      }
170      private static String readFromFile(String file) {
171             String content  = new String();
172             try {
173                 
174                 InputStream is = ClusterUtilsTest.class.getResourceAsStream(file);
175                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
176                 content = bufferedReader.readLine();
177                 String temp;
178                 while((temp = bufferedReader.readLine()) != null) {
179                     content = content.concat(temp);
180                 }
181                 content = content.trim();
182                 bufferedReader.close();
183             }
184             catch(Exception e) {
185                 content  = null;
186             }
187             return content;
188         }
189 }