Add second control loop name, Change restclient to support SSL
[dcaegen2/services/son-handler.git] / src / test / java / org / onap / dcaegen2 / services / sonhms / child / TestChildThreadUtils.java
index ca5f5fd..f8a6ae2 100644 (file)
 
 package org.onap.dcaegen2.services.sonhms.child;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.when;
+
 import com.fasterxml.jackson.databind.ObjectMapper;
 
+import fj.data.Either;
+
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
@@ -35,77 +42,203 @@ import java.util.Map;
 import org.junit.Assert;
 import org.junit.Before;
 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.ConfigPolicy;
+import org.onap.dcaegen2.services.sonhms.Configuration;
+import org.onap.dcaegen2.services.sonhms.HoMetricsComponent;
+import org.onap.dcaegen2.services.sonhms.dao.HandOverMetricsRepository;
+import org.onap.dcaegen2.services.sonhms.dao.SonRequestsRepository;
+import org.onap.dcaegen2.services.sonhms.dmaap.PolicyDmaapClient;
+import org.onap.dcaegen2.services.sonhms.entity.SonRequests;
+import org.onap.dcaegen2.services.sonhms.exceptions.ConfigDbNotFoundException;
 import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
+import org.onap.dcaegen2.services.sonhms.model.HoDetails;
 import org.onap.dcaegen2.services.sonhms.model.PolicyNotification;
+import org.onap.dcaegen2.services.sonhms.restclient.AsyncResponseBody;
+import org.onap.dcaegen2.services.sonhms.restclient.SdnrRestClient;
+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.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)
+@PowerMockRunnerDelegate(SpringRunner.class)
+@PrepareForTest({ BeanUtil.class, SdnrRestClient.class })
+@SpringBootTest(classes = TestChildThreadUtils.class)
 public class TestChildThreadUtils {
-    
-    ChildThreadUtils childThreadUtils;
-    
-    @Before
-    public void setup() {
-        
-        ConfigPolicy configPolicy = ConfigPolicy.getInstance();
-        
-        Map<String, Object> configPolicyMap = new HashMap<>();
-        configPolicyMap.put("PCI_MODCONFIG_POLICY_NAME", "ControlLoop-vPCI-fb41f388-a5f2-11e8-98d0-529269fb1459");
-        configPolicy.setConfig(configPolicyMap);
-        childThreadUtils = new ChildThreadUtils(configPolicy);
-    }
-    
-    @Test
-    public void getNotificationStringTest() {
-        
-        String policy_notif = readFromFile("/policy_notification.json");
-        PolicyNotification expected = new PolicyNotification();
-        ObjectMapper mapper = new ObjectMapper();
-        
+
+       ChildThreadUtils childThreadUtils;
+       @Mock
+       private SonRequestsRepository repository;
+
+       @Mock
+       private PnfUtils pnfUtils;
+       @Mock
+       private PolicyDmaapClient policyDmaapClient;
+       
+       @Mock
+       private HandOverMetricsRepository hoMetricRepository;
+       
+       @Mock
+       private HoMetricsComponent hoMetricsComponent;
+       
+       @InjectMocks
+       private ChildThreadUtils childThreadUtils2;
+
+       @Before
+       public void setup() {
+
+               ConfigPolicy configPolicy = ConfigPolicy.getInstance();
+               Configuration config = Configuration.getInstance();
+               config.setMinCollision(5);
+               config.setMinConfusion(5);
+               Map<String, Object> configPolicyMap = new HashMap<>();
+               configPolicyMap.put("PCI_MODCONFIG_POLICY_NAME", "ControlLoop-vPCI-fb41f388-a5f2-11e8-98d0-529269fb1459");
+               configPolicy.setConfig(configPolicyMap);
+               childThreadUtils = new ChildThreadUtils(configPolicy, pnfUtils,  policyDmaapClient, hoMetricsComponent);
+               MockitoAnnotations.initMocks(this);
+               
+       }
+
+       @Test
+       public void savePciRequestTest() {
+               SonRequests sonRequest = new SonRequests();
+               sonRequest.setTransactionId("transactionId");
+               sonRequest.setChildThreadId(1L);
+               PowerMockito.mockStatic(BeanUtil.class);
+               PowerMockito.when(BeanUtil.getBean(SonRequestsRepository.class))
+                               .thenReturn(repository);
+               when(repository.save(sonRequest)).thenReturn(sonRequest);
+               childThreadUtils2.saveRequest("transactionId",1L);;
+               assertEquals(sonRequest, repository.save(sonRequest));
+       }
+
+       @Test
+       public void triggerOrWaitTest() {
+               Map<String, ArrayList<Integer>> collisionConfusionResult = new HashMap<String, ArrayList<Integer>>();
+               ArrayList<Integer> list = new ArrayList<Integer>();
+               list.add(6);
+               list.add(7);
+               collisionConfusionResult.put("cellId", list);
+
+               Boolean result = childThreadUtils.triggerOrWait(collisionConfusionResult);
+               assertTrue(result);
+               Map<String, ArrayList<Integer>> collisionConfusionResult1 = new HashMap<String, ArrayList<Integer>>();
+
+               ArrayList<Integer> list1 = new ArrayList<Integer>();
+               list1.add(1);
+               list1.add(2);
+               collisionConfusionResult1.put("cell1", list1);
+               result = childThreadUtils.triggerOrWait(collisionConfusionResult1);
+               assertFalse(result);
+
+       }
+
+       @Test
+       public void getNotificationStringTest() {
+
+               String policy_notif = readFromFile("/policy_notification.json");
+               PolicyNotification expected = new PolicyNotification();
+               ObjectMapper mapper = new ObjectMapper();
+
+               try {
+                       expected = mapper.readValue(policy_notif, PolicyNotification.class);
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+
+               String pnfName = "ncserver23";
+               List<CellPciPair> cellPciPairs = new ArrayList<>();
+
+               cellPciPairs.add(new CellPciPair("Chn0330", 6));
+               cellPciPairs.add(new CellPciPair("Chn0331", 7));
+               String requestId = "a4130fd5-2291-4a83-8992-04e4c9f32731";
+               Long alarmStart = Long.parseLong("1542445563201");
+
+               String result = childThreadUtils.getNotificationString(pnfName, requestId, "payloadString", alarmStart, "ModifyConfig");
+               PolicyNotification actual = new PolicyNotification();
+               try {
+                       actual = mapper.readValue(result, PolicyNotification.class);
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+               System.out.println("actual :; "+result+"\nexp "+policy_notif);
+               Assert.assertEquals(expected.hashCode(), actual.hashCode());
+       }
+       
+       @Test
+       public void sendToPolicyTest() throws ConfigDbNotFoundException {
+           
+           PowerMockito.mockStatic(SdnrRestClient.class);
+           PowerMockito.mockStatic(BeanUtil.class);
+
+           String asyncRspBodyString = readFromFile("/AsyncRespBody.json");
+           ObjectMapper mapper = new ObjectMapper();
+           AsyncResponseBody async = new AsyncResponseBody ();
         try {
-            expected = mapper.readValue(policy_notif, PolicyNotification.class);
-        } catch (IOException e) {
-            // TODO Auto-generated catch block
+            async = mapper.readValue(asyncRspBodyString, AsyncResponseBody.class);
+        } catch (Exception e) {
             e.printStackTrace();
         }
+        PowerMockito.when(SdnrRestClient.getPci(Mockito.anyString())).thenReturn(3);
+        PowerMockito.when(SdnrRestClient.getPnfName(Mockito.anyString())).thenReturn("pnfName");
+        when(policyDmaapClient.sendNotificationToPolicy(Mockito.anyString())).thenReturn(true);
+        Map<String,List<CellPciPair>> pnfsMap = new HashMap<String,List<CellPciPair>>();
+        CellPciPair cell1 = new CellPciPair("cell0", 1);
+        CellPciPair cell2 = new CellPciPair("cell1", 2);
+        CellPciPair cell3 = new CellPciPair("cell2", 3);
+        List<CellPciPair> pciPairList = new ArrayList<>();
+        pciPairList.add(cell1);
+        pciPairList.add(cell2);
+        pciPairList.add(cell3);
+        pnfsMap.put("pnf1", pciPairList);
+        when(policyDmaapClient.handlePolicyResponse(Mockito.anyString())).thenReturn(true);
+        when(pnfUtils.getPnfs(async.getSolutions())).thenReturn(pnfsMap);
+        List<String> remNeighbors = new ArrayList<>();
+        remNeighbors.add("EXP006");
+        Map<String,List<String>> cellRemNeighborsPair = new HashMap<>();
+        cellRemNeighborsPair.put("EXP003", remNeighbors);
+        List<Map<String,List<String>>> list = new ArrayList<>();
+        list.add(cellRemNeighborsPair);
+        Map<String, List<Map<String,List<String>>>> expected = new HashMap<>();
+        expected.put("pnfName", list);
+        when(pnfUtils.getPnfsForAnrSolutions(async.getSolutions().getAnrSolutions())).thenReturn(expected);
+        HoDetails hoDetails = new HoDetails();
+        hoDetails.setDstCellId("EXP006");
+        List<HoDetails> hoDetailsList = new ArrayList<>();
+        hoDetailsList.add(hoDetails);
+        Either<List<HoDetails>, Integer> hoMetrics = Either.left(hoDetailsList);
+        when(hoMetricsComponent.getHoMetrics(Mockito.anyString())).thenReturn(hoMetrics);
+        when(hoMetricsComponent.update(Mockito.anyString(), Mockito.anyString())).thenReturn(true);
+        assertTrue(childThreadUtils2.sendToPolicy(async));
         
-        String pnfName = "ncserver23";
-        List<CellPciPair> cellPciPairs = new ArrayList<>();
-        
-        cellPciPairs.add(new CellPciPair("Chn0330", 6));
-        cellPciPairs.add(new CellPciPair("Chn0331", 7));
-        String requestId = "a4130fd5-2291-4a83-8992-04e4c9f32731";
-        Long alarmStart = Long.parseLong("1542445563201");
-        
-        String result = childThreadUtils.getNotificationString(pnfName, cellPciPairs, requestId, alarmStart);
-        PolicyNotification actual = new PolicyNotification();
-        try {
-            actual = mapper.readValue(result, PolicyNotification.class);
-        } catch (IOException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-        Assert.assertEquals(expected.hashCode(), actual.hashCode());
-    }
-    
-    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) {
-            e.printStackTrace();
-            content  = null;
-        }
-        return content; 
-    }
+       }
+
+       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) {
+                       e.printStackTrace();
+                       content = null;
+               }
+               return content;
+       }
 }