Implement Enhancements to the Microservice
[dcaegen2/services/son-handler.git] / src / test / java / org / onap / dcaegen2 / services / sonhms / BufferNotificationComponentTest.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.mockito.Mockito.when;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.dcaegen2.services.sonhms.dao.BufferedNotificationsRepository;
37 import org.onap.dcaegen2.services.sonhms.entity.BufferedNotifications;
38 import org.onap.dcaegen2.services.sonhms.utils.BeanUtil;
39 import org.powermock.api.mockito.PowerMockito;
40 import org.powermock.core.classloader.annotations.PrepareForTest;
41 import org.powermock.modules.junit4.PowerMockRunner;
42 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.test.context.junit4.SpringRunner;
45
46 @RunWith(PowerMockRunner.class)
47 @PowerMockRunnerDelegate(SpringRunner.class)
48 @PrepareForTest({ BeanUtil.class })
49 @SpringBootTest(classes = BufferNotificationComponent.class)
50 public class BufferNotificationComponentTest { 
51
52     @Mock
53     private BufferedNotificationsRepository bufferedNotificationsRepositoryMock;
54
55     @InjectMocks
56     private BufferNotificationComponent bufferNotificationComponent;
57
58     @Before
59     public void setup() {
60         MockitoAnnotations.initMocks(this);
61     }
62
63     @Test 
64     public void bufferNotificationTest() {
65         BufferedNotifications bufferedNotifications = new BufferedNotifications();
66         bufferedNotifications.setClusterId("clusterId");
67         bufferedNotifications.setNotification("notification");
68         PowerMockito.mockStatic(BeanUtil.class);
69         PowerMockito.when(BeanUtil.getBean(BufferedNotificationsRepository.class))
70                 .thenReturn(bufferedNotificationsRepositoryMock);
71         when(bufferedNotificationsRepositoryMock.save(bufferedNotifications)).thenReturn(bufferedNotifications);
72         bufferNotificationComponent.bufferNotification("notification", "clusterId");
73         assertEquals(bufferedNotifications, bufferedNotificationsRepositoryMock.save(bufferedNotifications)); 
74
75     }
76     
77     @Test
78     public void getBufferedNotificationTest() {
79         List<String> notificationsList = new ArrayList<String>();
80         notificationsList.add("NOTIF1");
81         notificationsList.add("NOTIF2");
82         String clusterId = "clusterId";
83         PowerMockito.mockStatic(BeanUtil.class);
84         PowerMockito.when(BeanUtil.getBean(BufferedNotificationsRepository.class))
85                 .thenReturn(bufferedNotificationsRepositoryMock);
86         when(bufferedNotificationsRepositoryMock.getNotificationsFromQueue(clusterId)).thenReturn((notificationsList));
87
88         List<String> testResult = new ArrayList<String>();
89         testResult = bufferNotificationComponent.getBufferedNotification(clusterId);
90         for (int i = 1; i <= testResult.size(); i++) {
91             assertEquals("NOTIF" + i, testResult.get(i - 1));
92         }
93     }
94     
95     @Test
96     public void getClusterIdTest() {
97         String notification = "NOTIF1";
98         PowerMockito.mockStatic(BeanUtil.class);
99         PowerMockito.when(BeanUtil.getBean(BufferedNotificationsRepository.class))
100                 .thenReturn(bufferedNotificationsRepositoryMock);
101         when(bufferedNotificationsRepositoryMock.getClusterIdForNotification(notification)).thenReturn(("clusterId"));
102         String clusterId = bufferNotificationComponent.getClusterId(notification);
103         assertEquals("clusterId", clusterId);
104     }
105
106 }