ad41d5f69788fbbde2152b4200cfe2b667cdd7e0
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / UserNotificationServiceImplTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.portal.service;
39
40 import static org.junit.Assert.assertEquals;
41
42 import java.util.ArrayList;
43 import java.util.Date;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.mockito.InjectMocks;
51 import org.mockito.Mock;
52 import org.mockito.Mockito;
53 import org.mockito.MockitoAnnotations;
54 import org.onap.portalapp.portal.core.MockEPUser;
55 import org.onap.portalapp.portal.domain.EPUser;
56 import org.onap.portalapp.portal.domain.EPUserNotification;
57 import org.onap.portalapp.portal.domain.EcompAppRole;
58 import org.onap.portalapp.portal.transport.EpNotificationItem;
59 import org.onap.portalapp.portal.transport.EpNotificationItemVO;
60 import org.onap.portalapp.portal.transport.EpRoleNotificationItem;
61 import org.onap.portalsdk.core.service.DataAccessService;
62 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
63
64 public class UserNotificationServiceImplTest {
65         
66         @Mock
67         DataAccessService dataAccessService = new DataAccessServiceImpl();
68         
69         @Before
70         public void setup() {
71                 MockitoAnnotations.initMocks(this);
72         }
73         
74         @InjectMocks
75         UserNotificationServiceImpl userNotificationServiceImpl =  new UserNotificationServiceImpl();
76         
77         MockEPUser mockUser = new MockEPUser();
78         
79         @Test
80         public void getNotificationsTest() {
81                 EPUser user = mockUser.mockEPUser();
82                 Map<String, String> params = new HashMap<>();
83                 params.put("user_id", String.valueOf(user.getId()));
84                 List<EpNotificationItem> mockNotificationList = new ArrayList<>();
85                 Mockito.when(dataAccessService.executeNamedQuery("getNotifications", params, null)).thenReturn(mockNotificationList);
86                 List<EpNotificationItem> notificationList = userNotificationServiceImpl.getNotifications(user.getId());
87                 assertEquals(notificationList, mockNotificationList); 
88         }
89         
90         @Test
91         public void getNotificationHistoryVOTest() {
92                 EPUser user = mockUser.mockEPUser();
93                 Map<String, String> params = new HashMap<String, String>();
94                 params.put("user_id", String.valueOf(user.getId()));
95                 List<EpNotificationItemVO> mockNotificationListVO = new  ArrayList<>();
96                 Mockito.when(dataAccessService.executeNamedQuery("getNotificationHistoryVO",
97                                 params, null)).thenReturn(mockNotificationListVO);
98                 List<EpNotificationItemVO> notificationListVO = userNotificationServiceImpl.getNotificationHistoryVO(user.getId());
99                 assertEquals(notificationListVO,mockNotificationListVO); 
100         }
101         
102         @Test
103         public void getAdminNotificationVOSTest() {
104                 EPUser user = mockUser.mockEPUser();
105                 Map<String, String> params = new HashMap<String, String>();
106                 params.put("user_id", String.valueOf(user.getId()));
107                 List<EpNotificationItemVO> mockAdminNotificationListItemVO = new  ArrayList<>();
108                 Mockito.when(dataAccessService.executeNamedQuery("getAdminNotificationHistoryVO",
109                                 params, null)).thenReturn(mockAdminNotificationListItemVO);
110                 List<EpNotificationItemVO> adminNotificationListItemVO = userNotificationServiceImpl.getAdminNotificationVOS(user.getId());
111                 assertEquals(adminNotificationListItemVO,mockAdminNotificationListItemVO); 
112         }
113         
114         @Test
115         public void getNotificationRolesTest() {
116                 Map<String, String> params = new HashMap<String, String>();
117                 params.put("notificationId", Long.toString(1l));
118                 List<EpRoleNotificationItem> mockRoleNotifList = new ArrayList<>();
119                 Mockito.when(dataAccessService.executeNamedQuery("getNotificationRoles",
120                                 params, null)).thenReturn(mockRoleNotifList);
121                 List<EpRoleNotificationItem> roleNotifList = userNotificationServiceImpl.getNotificationRoles(1l);
122                 assertEquals(roleNotifList,mockRoleNotifList); 
123         }
124         
125         @Test
126         public void getAppRoleListTest() {
127                 List<EcompAppRole> mockAppRoleList = new ArrayList<>();
128                 Mockito.when(dataAccessService
129                                 .executeNamedQuery("getEpNotificationAppRoles", null, null)).thenReturn(mockAppRoleList);
130                 List<EcompAppRole> appRoleList = userNotificationServiceImpl.getAppRoleList();
131                 assertEquals(mockAppRoleList, appRoleList);
132         }
133         
134         @Test
135         public void setNotificationReadTest() {
136                 EPUserNotification mockUserNotification = new EPUserNotification();
137                 mockUserNotification.setNotificationId(1l);
138                 mockUserNotification.setId(1l);
139                 mockUserNotification.setUpdateTime(new Date());
140                 mockUserNotification.setViewed("Y");
141                 mockUserNotification.setUserId((long) 1);
142                 Mockito.doNothing().when(dataAccessService).saveDomainObject(mockUserNotification, null);
143                 userNotificationServiceImpl.setNotificationRead(1l, 1);
144         }
145         
146         @Test
147         public void saveNotificationTest() throws Exception {
148                 EpNotificationItem epNotificationItem = new EpNotificationItem();
149                 List<Long> roleIdList =  new ArrayList<>();
150                 Long roleId = new Long(1l);
151                 Long roleId2 = new Long(16l);
152                 roleIdList.add(roleId);
153                 roleIdList.add(roleId2);
154                 epNotificationItem.setIsForAllRoles("N");
155                 epNotificationItem.setRoleIds(roleIdList);
156                 epNotificationItem.setNotificationId(1l);
157                 Mockito.doNothing().when(dataAccessService).saveDomainObject(epNotificationItem, null);
158                 userNotificationServiceImpl.saveNotification(epNotificationItem);
159         }
160         
161         @Test
162         public void getUsersByOrgIdsTest() {
163                 Map<String, Object> params = new HashMap<String, Object>();
164                 params.put("OrgIds", "test");
165                 List<EPUser> mockUserList = new ArrayList<>();
166                 Mockito.when(dataAccessService.executeNamedQuery("getUsersByOrgIdsNotifications",
167                                 params, null)).thenReturn(mockUserList);
168                 List<String> orgIdsList =  new ArrayList<>();
169                 String orgId = "test";
170                 orgIdsList.add(orgId);
171                 List<EPUser> userList = userNotificationServiceImpl.getUsersByOrgIds(orgIdsList);
172                 assertEquals(userList, mockUserList);
173         }
174         
175         @Test
176         public void getMessageRecipientsTest() {
177                 Map<String, String> params = new HashMap<>();
178                 params.put("notificationId", Long.toString(1l));
179                 List<String> mockActiveUsers =  new ArrayList<>();
180                 Mockito.when(dataAccessService.executeNamedQuery("messageRecipients",
181                                 params, null)).thenReturn(mockActiveUsers);
182                 List<String> activeUsers = userNotificationServiceImpl.getMessageRecipients(1l);
183                 assertEquals(activeUsers, mockActiveUsers);
184         }
185         
186         @Test
187         public void deleteNotificationsFromEpNotificationTableTest() {
188                 Map<String, String> params = new HashMap<String, String>();
189                 Mockito.when(dataAccessService
190                                 .executeNamedQuery("deleteNotificationsFromEpUserNotificationTable", params, null)).thenReturn(null);
191                 userNotificationServiceImpl.deleteNotificationsFromEpNotificationTable();
192         }
193         
194         @Test
195         public void deleteNotificationsFromEpUserNotificationTable() {
196                 Map<String, String> params = new HashMap<String, String>();
197                 Mockito.when(dataAccessService
198                                 .executeNamedQuery("deleteNotificationsFromEpUserNotificationTable", params, null)).thenReturn(null);
199                 userNotificationServiceImpl.deleteNotificationsFromEpUserNotificationTable();
200         }
201         
202         @Test
203         public void deleteNotificationsFromEpRoleNotificationTable() {
204                 Map<String, String> params = new HashMap<String, String>();
205                 Mockito.when(dataAccessService
206                                 .executeNamedQuery("deleteNotificationsFromEpRoleNotificationTable", params, null)).thenReturn(null);
207                 userNotificationServiceImpl.deleteNotificationsFromEpRoleNotificationTable();
208         }
209 }