aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/dcaegen2/services/sonhms/child/TestPnfUtils.java
blob: 607f0c0442b15e6a1d0ba40ad5ad3d89f5e2e5c7 (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
/*******************************************************************************
 * ============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.child;

import static org.junit.Assert.assertEquals;

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

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.Optional;

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.Configuration;
import org.onap.dcaegen2.services.sonhms.dao.CellInfoRepository;
import org.onap.dcaegen2.services.sonhms.entity.CellInfo;
import org.onap.dcaegen2.services.sonhms.exceptions.ConfigDbNotFoundException;
import org.onap.dcaegen2.services.sonhms.exceptions.CpsNotFoundException;
import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
import org.onap.dcaegen2.services.sonhms.restclient.AnrSolutions;
import org.onap.dcaegen2.services.sonhms.restclient.ConfigurationClient;
import org.onap.dcaegen2.services.sonhms.restclient.SdnrRestClient;
import org.onap.dcaegen2.services.sonhms.restclient.Solutions;
import org.onap.dcaegen2.services.sonhms.utils.BeanUtil;
import org.onap.dcaegen2.services.sonhms.utils.ClusterUtilsTest;
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.slf4j.Logger;
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({BeanUtil.class, SdnrRestClient.class, ConfigurationClient.class })
@SpringBootTest(classes = PnfUtils.class)
public class TestPnfUtils {

    @Mock
    private CellInfoRepository cellInfoRepositoryMock;
    
    private static final Logger log = org.slf4j.LoggerFactory.getLogger(TestPnfUtils.class);
    private static Solutions solutions = new Solutions();
    private static List<AnrSolutions> anrSolutions = new ArrayList<>();
    private static Optional<CellInfo> cellInfo;
    private static Optional<CellInfo> cellInfoNull;

    
    @InjectMocks
    PnfUtils pnfUtils;
    
     @BeforeClass
     public static void setup() {

         Configuration config = Configuration.getInstance();
         config.setConfigClientType("ConfigDB");
         String solutionsString=readFromFile("/solutions.json");
         String anrSolutionsString = readFromFile("/anrSolutions.json");
         ObjectMapper mapper = new ObjectMapper();
            
            try {
                solutions=mapper.readValue(solutionsString, Solutions.class);
                anrSolutions = mapper.readValue(anrSolutionsString, new TypeReference<ArrayList<AnrSolutions>>() {
                });
            } catch (IOException e) {
                log.debug("Exception in StateOof Test "+e);
                e.printStackTrace();
            }
         
     }
     @Before
     public void setupTest() {
            cellInfo = Optional.of(new CellInfo("EXP001","ncserver1"));
            cellInfoNull = Optional.ofNullable(null);
            pnfUtils = new PnfUtils();
            MockitoAnnotations.initMocks(this);
        } 
     @Test
     public void getPnfsTest() {
         Map<String, List<CellPciPair>> pnfs = new HashMap<>();
         List<CellPciPair> cellpciPairList1=new ArrayList<>();
         cellpciPairList1.add(new CellPciPair("EXP001",101));
         List<CellPciPair> cellpciPairList2=new ArrayList<>();
         cellpciPairList2.add(new CellPciPair("EXP002",102));
         String pnfName="ncserver2";
         String cellId="EXP002";
         PowerMockito.mockStatic(BeanUtil.class);
         PowerMockito.mockStatic(SdnrRestClient.class);

         PowerMockito.mockStatic(ConfigurationClient.class);

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

         PowerMockito.when(BeanUtil.getBean(CellInfoRepository.class))
                 .thenReturn(cellInfoRepositoryMock);
         
        Mockito.when(cellInfoRepositoryMock.findById("EXP001"))
         .thenReturn(cellInfo);
        Mockito.when(cellInfoRepositoryMock.findById(cellId))
        .thenReturn(cellInfoNull);
        try {
            PowerMockito.whenNew(SdnrRestClient.class).withAnyArguments().thenReturn(sdnr);
            PowerMockito.when(config.getConfigurationClient())
                    .thenReturn(sdnr);
            PowerMockito.doReturn(pnfName).when(sdnr, "getPnfName", Mockito.anyString());
            PowerMockito.when(cellInfoRepositoryMock.save(new CellInfo(cellId, pnfName))).thenReturn(new CellInfo());
        } catch (ConfigDbNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
         pnfs.put(pnfName, cellpciPairList2);
        pnfs.put("ncserver1", cellpciPairList1);
        System.out.println(solutions);
        try {
            assertEquals(pnfs,pnfUtils.getPnfs(solutions));
        } catch (ConfigDbNotFoundException | CpsNotFoundException e) {
            log.debug("exception in stateOof test {}", e);
            e.printStackTrace();
        }
     }
     
     @Test
     public void testGetPnfsForAnrSolutions() {
         Map<String, List<Map<String,List<String>>>> actual = null ;
         Map<String, List<Map<String,List<String>>>> expected = new HashMap<>();

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

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

         try {
             PowerMockito.whenNew(SdnrRestClient.class).withAnyArguments().thenReturn(sdnr);
             PowerMockito.when(config.getConfigurationClient()).thenReturn(sdnr);
             PowerMockito.doReturn("ncServer1").when(sdnr, "getPnfName", Mockito.anyString());
            actual = pnfUtils.getPnfsForAnrSolutions(anrSolutions);
        } catch (ConfigDbNotFoundException e) {
            e.printStackTrace();
        } catch (CpsNotFoundException e) {
             e.printStackTrace();
         } catch (Exception e) {
             e.printStackTrace();
         }
         List<String> remNeighbors1 = new ArrayList<>();
        List<String> remNeighbors2 = new ArrayList<>();
        remNeighbors1.add("cell2");
        remNeighbors1.add("cell3");
        remNeighbors2.add("cell9");
        Map<String,List<String>> cellRemNeighborsPair1 = new HashMap<>();
        Map<String,List<String>> cellRemNeighborsPair2 = new HashMap<>();
        cellRemNeighborsPair1.put("cell1", remNeighbors1);
        cellRemNeighborsPair2.put("cell8", remNeighbors2);
        List<Map<String,List<String>>> list = new ArrayList<>();
        list.add(cellRemNeighborsPair1);
        list.add(cellRemNeighborsPair2);
        expected.put("ncServer1", list);
        assertEquals(expected, actual);
     }
     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;
        }
}