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.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.when;
29 import fj.data.Either;
31 import java.io.BufferedReader;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mock;
40 import org.onap.dcaegen2.services.sonhms.dao.DmaapNotificationsRepository;
41 import org.onap.dcaegen2.services.sonhms.dao.PerformanceNotificationsRepository;
42 import org.onap.dcaegen2.services.sonhms.model.Notification;
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 = DmaapNotificationsComponentTest.class)
57 public class DmaapNotificationsComponentTest {
60 DmaapNotificationsRepository dmaapNotificationsRepositoryMock;
63 PerformanceNotificationsRepository performanceNotificationsRepositoryMock;
66 DmaapNotificationsComponent component;
68 static String notificationString;
69 static String pmNotificationString;
73 public static void setupTest() {
75 notificationString = readFromFile("/notification1.json");
76 pmNotificationString=readFromFile("/pmNotification.json");
81 public void getDmaapNotificationsTestforLeft() {
82 PowerMockito.mockStatic(BeanUtil.class);
83 PowerMockito.when(BeanUtil.getBean(DmaapNotificationsRepository.class))
84 .thenReturn(dmaapNotificationsRepositoryMock);
85 when(dmaapNotificationsRepositoryMock.getNotificationFromQueue()).thenReturn(notificationString);
88 Either<Notification, Integer> result = component.getSdnrNotifications();
89 //assertTrue(result.isLeft());
90 assertNotNull(result.left().value());
92 when(dmaapNotificationsRepositoryMock.getNotificationFromQueue()).thenReturn("notification");
94 result = component.getSdnrNotifications();
95 int resultRight = result.right().value();
96 assertEquals(400, resultRight);
101 public void getPmNotificationsTest() {
102 PowerMockito.mockStatic(BeanUtil.class);
103 PowerMockito.when(BeanUtil.getBean(PerformanceNotificationsRepository.class))
104 .thenReturn(performanceNotificationsRepositoryMock);
105 when(performanceNotificationsRepositoryMock.getPerformanceNotificationFromQueue()).thenReturn(pmNotificationString);
107 Either<PMNotification,Integer> result = component.getPmNotifications();
108 assertTrue(result.isLeft());
109 assertNotNull(result.left().value());
111 when(performanceNotificationsRepositoryMock.getPerformanceNotificationFromQueue()).thenReturn("pmNotification");
112 result = component.getPmNotifications();
113 int res= result.right().value();
114 assertEquals(400,res);
118 private static String readFromFile(String file) {
119 String content = new String();
122 InputStream is = DmaapNotificationsComponentTest.class.getResourceAsStream(file);
123 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
124 content = bufferedReader.readLine();
126 while ((temp = bufferedReader.readLine()) != null) {
127 content = content.concat(temp);
129 content = content.trim();
130 bufferedReader.close();
131 } catch (Exception e) {