Implement Enhancements to the Microservice
[dcaegen2/services/son-handler.git] / src / test / java / org / onap / dcaegen2 / services / sonhms / DmaapNotificationsComponentTest.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.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.when;
28
29 import fj.data.Either;
30
31 import java.io.BufferedReader;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34
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;
51
52 @RunWith(PowerMockRunner.class)
53 @PowerMockRunnerDelegate(SpringRunner.class)
54 @PrepareForTest({ BeanUtil.class })
55 @SpringBootTest(classes = DmaapNotificationsComponentTest.class)
56
57 public class DmaapNotificationsComponentTest {
58
59         @Mock
60         DmaapNotificationsRepository dmaapNotificationsRepositoryMock;
61         
62         @Mock
63         PerformanceNotificationsRepository performanceNotificationsRepositoryMock;
64
65         @InjectMocks
66         DmaapNotificationsComponent component;
67
68         static String notificationString;
69         static String pmNotificationString;
70
71
72         @BeforeClass
73         public static void setupTest() {
74
75                 notificationString = readFromFile("/notification1.json");
76                 pmNotificationString=readFromFile("/pmNotification.json");
77
78         }
79
80         @Test
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);
86
87
88                 Either<Notification, Integer> result = component.getSdnrNotifications();
89                 //assertTrue(result.isLeft());
90                 assertNotNull(result.left().value());
91                 
92                 when(dmaapNotificationsRepositoryMock.getNotificationFromQueue()).thenReturn("notification");
93
94         result = component.getSdnrNotifications();
95         int resultRight = result.right().value();
96         assertEquals(400, resultRight);
97                 
98         }
99         
100         @Test
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);
106         
107         Either<PMNotification,Integer> result = component.getPmNotifications();
108         assertTrue(result.isLeft());
109         assertNotNull(result.left().value());
110         
111         when(performanceNotificationsRepositoryMock.getPerformanceNotificationFromQueue()).thenReturn("pmNotification");
112         result = component.getPmNotifications();
113         int res= result.right().value();
114         assertEquals(400,res);
115         
116         }
117
118         private static String readFromFile(String file) { 
119                 String content = new String();
120                 try {
121
122                         InputStream is = DmaapNotificationsComponentTest.class.getResourceAsStream(file);
123                         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
124                         content = bufferedReader.readLine();
125                         String temp;
126                         while ((temp = bufferedReader.readLine()) != null) {
127                                 content = content.concat(temp);
128                         }
129                         content = content.trim();
130                         bufferedReader.close();
131                 } catch (Exception e) {
132                         content = null;
133                 }
134                 return content;
135         }
136
137 }