1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 *******************************************************************************/
22 package org.onap.dcaegen2.services.sonhms;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.when;
28 import com.fasterxml.jackson.databind.ObjectMapper;
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;
52 @RunWith(PowerMockRunner.class)
53 @PowerMockRunnerDelegate(SpringRunner.class)
54 @PrepareForTest({ BeanUtil.class })
55 @SpringBootTest(classes = PMNotificationHandlerTest.class)
56 public class PMNotificationHandlerTest {
59 HandOverMetricsRepository handOverMetricsRepositoryMock;
65 PmNotificationHandler pmNotificationHandler;
68 PolicyDmaapClient policyDmaapClient;
70 private static String pmNotificationsString ;
71 private static String pmNotificationsString1 ;
72 private static PMNotification pmNotification;
73 private static PMNotification pmNotification1;
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();
84 pmNotification = mapper.readValue(pmNotificationsString, PMNotification.class);
85 pmNotification1 = mapper.readValue(pmNotificationsString1, PMNotification.class);
86 } catch(Exception e) {
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));
106 private static String readFromFile(String file) {
107 String content = new String();
110 InputStream is = HoMetricsComponentTest.class.getResourceAsStream(file);
111 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
112 content = bufferedReader.readLine();
114 while ((temp = bufferedReader.readLine()) != null) {
115 content = content.concat(temp);
117 content = content.trim();
118 bufferedReader.close();
119 } catch (Exception e) {