Fix bugs and formatting issues
[dcaegen2/services/son-handler.git] / src / test / java / org / onap / dcaegen2 / services / sonhms / PMNotificationHandlerTest.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
22 package org.onap.dcaegen2.services.sonhms;
23
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.when;
27
28 import com.fasterxml.jackson.databind.ObjectMapper;
29
30 import java.io.BufferedReader;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.onap.dcaegen2.services.sonhms.dao.HandOverMetricsRepository;
40 import org.onap.dcaegen2.services.sonhms.dmaap.PolicyDmaapClient;
41 import org.onap.dcaegen2.services.sonhms.entity.HandOverMetrics;
42 import org.onap.dcaegen2.services.sonhms.model.Flag;
43 import org.onap.dcaegen2.services.sonhms.model.PMNotification;
44 import org.onap.dcaegen2.services.sonhms.utils.BeanUtil;
45 import org.powermock.api.mockito.PowerMockito;
46 import org.powermock.core.classloader.annotations.PrepareForTest;
47 import org.powermock.modules.junit4.PowerMockRunner;
48 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
49 import org.springframework.boot.test.context.SpringBootTest;
50 import org.springframework.test.context.junit4.SpringRunner;
51
52 @RunWith(PowerMockRunner.class)
53 @PowerMockRunnerDelegate(SpringRunner.class)
54 @PrepareForTest({ BeanUtil.class })
55 @SpringBootTest(classes = PMNotificationHandlerTest.class)
56 public class PMNotificationHandlerTest {
57
58     @Mock
59     HandOverMetricsRepository handOverMetricsRepositoryMock;
60     
61     @Mock
62     Flag flagMock;
63     
64     @InjectMocks
65     PmNotificationHandler pmNotificationHandler;
66     
67     @Mock
68     PolicyDmaapClient policyDmaapClient;
69     
70     private static String pmNotificationsString ;
71     private static String pmNotificationsString1 ;
72     private static PMNotification pmNotification;
73     private static PMNotification pmNotification1;
74
75     @BeforeClass
76     public static void setup() {
77         pmNotificationsString=readFromFile("/pmNotification.json");
78         pmNotificationsString1=readFromFile("/pmNotification1.json");
79         ObjectMapper mapper = new ObjectMapper();        
80         pmNotification = new PMNotification();
81         pmNotification1 = new PMNotification();
82         
83         try {
84             pmNotification = mapper.readValue(pmNotificationsString, PMNotification.class);
85             pmNotification1 = mapper.readValue(pmNotificationsString1, PMNotification.class);
86         } catch(Exception e) {
87             e.printStackTrace();      
88             }
89     }
90     
91     @Test
92     public void handlePmNotificationsTest() {
93         PowerMockito.mockStatic(BeanUtil.class);
94         PowerMockito.when(BeanUtil
95                 .getBean(HandOverMetricsRepository.class)).thenReturn(handOverMetricsRepositoryMock);
96         PowerMockito.when(BeanUtil
97                 .getBean(Flag.class)).thenReturn(flagMock);
98         when(handOverMetricsRepositoryMock.save(new HandOverMetrics())).thenReturn(null);
99         when(flagMock.getHolder()).thenReturn("NONE");
100         when(policyDmaapClient.sendNotificationToPolicy(Mockito.anyString())).thenReturn(true);
101         assertTrue(pmNotificationHandler.handlePmNotifications(pmNotification, 50));
102         assertFalse(pmNotificationHandler.handlePmNotifications(null, 0));
103         assertTrue(pmNotificationHandler.handlePmNotifications(pmNotification1, 50));
104     }
105
106     private static String readFromFile(String file) { 
107         String content = new String();
108         try {
109
110             InputStream is = HoMetricsComponentTest.class.getResourceAsStream(file);
111             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
112             content = bufferedReader.readLine();
113             String temp;
114             while ((temp = bufferedReader.readLine()) != null) {
115                 content = content.concat(temp);
116             }
117             content = content.trim();
118             bufferedReader.close();
119         } catch (Exception e) {
120             content = null;
121         }
122         return content;
123     }
124     
125 }