SB 2.x Migration for portal widget mS
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / test / java / org / onap / portalapp / widget / service / impl / WidgetCatalogServiceImplTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2018 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  * 
37  */
38 package org.onap.portalapp.widget.service.impl;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.mockito.Mockito.when;
42
43 import java.util.ArrayList;
44 import java.util.HashSet;
45 import java.util.List;
46 import java.util.Set;
47
48 import org.hibernate.Criteria;
49 import org.hibernate.SQLQuery;
50 import org.hibernate.Session;
51 import org.hibernate.SessionFactory;
52 import org.hibernate.Transaction;
53 import org.hibernate.criterion.Restrictions;
54 import org.junit.Before;
55 import org.junit.Test;
56 import org.mockito.InjectMocks;
57 import org.mockito.Mock;
58 import org.mockito.MockitoAnnotations;
59 import org.onap.portalapp.widget.domain.App;
60 import org.onap.portalapp.widget.domain.RoleApp;
61 import org.onap.portalapp.widget.domain.WidgetCatalog;
62 import org.hibernate.query.NativeQuery;
63
64 public class WidgetCatalogServiceImplTest {
65
66         @InjectMocks
67         WidgetCatalogServiceImpl widgetCatalogServiceImpl;
68
69         @Mock
70         Session session;
71         @Mock
72         SessionFactory sessionFactory;
73         @Mock
74         Transaction transaction;
75         @Mock
76         Criteria criteria;
77
78         @Mock
79         NativeQuery query;
80
81         @Before
82         public void init() {
83                 MockitoAnnotations.initMocks(this);
84         }
85
86         @Test
87         public void testSaveMicroserivce() {
88                 List<WidgetCatalog> list = buildWidgetCatalog();
89                 when(sessionFactory.getCurrentSession()).thenReturn(session);
90                 // when(session.beginTransaction()).thenReturn(transaction);
91                 when(session.createCriteria(WidgetCatalog.class)).thenReturn(criteria);
92                 when(criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)).thenReturn(criteria);
93                 when(criteria.list()).thenReturn(list);
94
95                 List<WidgetCatalog> catalogList = widgetCatalogServiceImpl.getWidgetCatalog();
96                 assertEquals("junit", catalogList.get(0).getName());
97
98         }
99
100         @Test
101         public void tesGetUserWidgetCatalog() {
102                 when(sessionFactory.getCurrentSession()).thenReturn(session);
103                 StringBuilder sb = widgetUserCatalog("test");
104                 when(session.createSQLQuery(sb.toString())).thenReturn(query);
105                 when(query.list()).thenReturn(buildWidgetCatalog());
106                 List<WidgetCatalog> catalogList = widgetCatalogServiceImpl.getUserWidgetCatalog("test");
107                 assertEquals("junit", catalogList.get(0).getName());
108
109         }
110
111         @Test
112         public void getWidgetCatalog() {
113
114                 when(sessionFactory.getCurrentSession()).thenReturn(session);
115                 when(session.beginTransaction()).thenReturn(transaction);
116                 when(session.get(WidgetCatalog.class, 1l)).thenReturn(buildWidgetCatalog().get(0));
117                 WidgetCatalog catalog = widgetCatalogServiceImpl.getWidgetCatalog(1l);
118                 assertEquals("junit", catalog.getName());
119         }
120
121         @Test
122         public void deleteWidgetCatalog() {
123                 Long widgetCatalogId = 1l;
124                 when(sessionFactory.getCurrentSession()).thenReturn(session, session);
125                 when(session.beginTransaction()).thenReturn(transaction, transaction);
126                 when(session.get(WidgetCatalog.class, widgetCatalogId)).thenReturn(buildWidgetCatalog().get(0));
127                 String queryString = "delete from ep_pers_user_widget_sel where widget_id = :widgetId ";
128                 when(session.createSQLQuery(queryString)).thenReturn(query);
129
130                 String deleteEpUserWidget = "delete from ep_pers_user_widget_placement where widget_id = :widgetId ";
131                 String deleteEpUserWidgetCatalog = "delete from ep_widget_catalog_files where widget_id = :widgetId ";
132                 String deleteUserWidgetCatalog = "delete from ep_widget_catalog_parameter where widget_id = :widgetId ";
133                 when(session.createSQLQuery(deleteEpUserWidget)).thenReturn(query);
134                 when(session.createSQLQuery(deleteEpUserWidgetCatalog)).thenReturn(query);
135                 when(session.createSQLQuery(deleteUserWidgetCatalog)).thenReturn(query);
136                 when(query.setParameter("widgetId", widgetCatalogId)).thenReturn(query, query, query, query);
137                 widgetCatalogServiceImpl.deleteWidgetCatalog(widgetCatalogId);
138
139         }
140
141         @Test
142         public void deleteWidgetCatalogEmpty() {
143                 Long widgetCatalogId = 1l;
144                 when(sessionFactory.getCurrentSession()).thenReturn(session, session);
145                 when(session.beginTransaction()).thenReturn(transaction, transaction);
146                 when(session.get(WidgetCatalog.class, widgetCatalogId)).thenReturn(null);
147                 widgetCatalogServiceImpl.deleteWidgetCatalog(widgetCatalogId);
148         }
149
150         @Test
151         public void saveWidgetCatalog() {
152                 WidgetCatalog widgetCatalog = buildWidgetCatalog().get(0);
153                 widgetCatalog.setAllowAllUser("1");
154                 App app = new App();
155                 app.setAppId(1l);
156                 RoleApp roleApp = new RoleApp();
157                 roleApp.setRoleId(1l);
158                 roleApp.setApp(app);
159                 Set<RoleApp> roles = new HashSet<>();
160                 roles.add(roleApp);
161                 widgetCatalog.setWidgetRoles(roles);
162                 String sql = "UPDATE ep_widget_catalog_role SET app_id = " + roleApp.getApp().getAppId() + " WHERE widget_id = "
163                                 + widgetCatalog.getId() + " AND ROLE_ID = " + roleApp.getRoleId();
164                 when(sessionFactory.openSession()).thenReturn(session, session);
165                 when(session.beginTransaction()).thenReturn(transaction);
166                 when(session.createSQLQuery(sql)).thenReturn(query);
167                 widgetCatalogServiceImpl.saveWidgetCatalog(widgetCatalog);
168
169         }
170
171         @Test
172         public void updateWidgetCatalog() {
173                 Long widgetCatalogId = 1l;
174                 WidgetCatalog widgetCatalog = buildWidgetCatalog().get(0);
175                 widgetCatalog.setServiceId(widgetCatalogId);
176                 widgetCatalog.setAllowAllUser("1");
177                 App app = new App();
178                 app.setAppId(1l);
179                 RoleApp roleApp = new RoleApp();
180                 roleApp.setRoleId(1l);
181                 roleApp.setApp(app);
182                 Set<RoleApp> roles = new HashSet<>();
183                 roles.add(roleApp);
184                 widgetCatalog.setWidgetRoles(roles);
185                 String sql = "UPDATE ep_widget_catalog_role SET app_id = " + roleApp.getApp().getAppId() + " WHERE widget_id = "
186                                 + widgetCatalog.getId() + " AND ROLE_ID = " + roleApp.getRoleId();
187                 when(sessionFactory.getCurrentSession()).thenReturn(session);
188                 when(session.beginTransaction()).thenReturn(transaction, transaction);
189                 when(session.get(WidgetCatalog.class, widgetCatalogId)).thenReturn(buildWidgetCatalog().get(0));
190                 when(sessionFactory.openSession()).thenReturn(session, session);
191                 when(session.createSQLQuery(sql)).thenReturn(query);
192
193                 widgetCatalogServiceImpl.updateWidgetCatalog(widgetCatalogId, widgetCatalog);
194
195         }
196
197         @Test
198         public void getServiceIdByWidget() {
199                 Long widgetCatalogId = 1l;
200                 when(sessionFactory.getCurrentSession()).thenReturn(session, session);
201                 when(session.beginTransaction()).thenReturn(transaction, transaction);
202                 when(session.get(WidgetCatalog.class, widgetCatalogId)).thenReturn(buildWidgetCatalog().get(0));
203                 Long serviceId = widgetCatalogServiceImpl.getServiceIdByWidget(widgetCatalogId);
204                 assertEquals(widgetCatalogId, serviceId);
205
206         }
207         
208         @Test(expected=NullPointerException.class)
209         public void getWidgetsByServiceId() {
210                 Long serviceId=1l;
211                 List<WidgetCatalog> list = buildWidgetCatalog();
212                 when(sessionFactory.getCurrentSession()).thenReturn(session,session);
213                 // when(session.beginTransaction()).thenReturn(transaction);
214                 when(session.createCriteria(WidgetCatalog.class)).thenReturn(criteria);
215                 when(criteria.add(Restrictions.eq("serviceId", serviceId))).thenReturn(criteria);
216                 when(criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)).thenReturn(criteria);
217                 when(criteria.list()).thenReturn(list);
218
219                 List<WidgetCatalog> catalogList = widgetCatalogServiceImpl.getWidgetsByServiceId(serviceId);
220                 assertEquals("junit", catalogList.get(0).getName());
221         }
222
223         private List<WidgetCatalog> buildWidgetCatalog() {
224                 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
225                 WidgetCatalog widget = new WidgetCatalog();
226                 widget.setId(1);
227                 widget.setName("junit");
228                 widget.setServiceId(1l);
229                 list.add(widget);
230                 return list;
231         }
232
233         private StringBuilder widgetUserCatalog(String loginName) {
234                 StringBuilder sql = new StringBuilder()
235
236                                 .append("  select userWidgets.widget_id, userWidgets.wdg_name, userWidgets.wdg_desc, b.x, b.status_cd, b.y, b.height, b.width  from                                        ")
237                                 .append("  (                                                                                                                                                               ")
238                                 .append("  select distinct w.widget_id, w.wdg_name, w.wdg_desc from                                                                                                                                                        ")
239                                 .append("  ep_widget_catalog w,                                                                                                                                            ")
240                                 .append("  ep_widget_catalog_role wr,                                                                                                                                              ")
241                                 .append("  fn_user_role ur,                                                                                                                                                                                                                                                                                ")
242                                 .append("  fn_app app,                                                                                                                                                                                                                                                                             ")
243                                 .append("  fn_user u                                                                                                                                                               ")
244                                 .append("  where                                                                                                                                                   ")
245                                 .append("  w.widget_id = wr.WIDGET_ID and                                                                                                                                                                                                                                                  ")
246                                 .append("  ur.app_id = app.app_id and                                                                                                                                                                                                                                              ")
247                                 .append("  app.enabled = 'Y' and                                                                                                                                                                                                                                                   ")
248                                 .append("  wr.role_id = ur.role_id and                                                                                                                                                                                                                                             ")
249                                 .append("  ur.user_id = u.user_id and                                                                                                                                                                                                                                              ")
250                                 .append("  u.login_id = '" + loginName
251                                                 + "' and (w.all_user_flag = 'N' or w.all_user_flag is null)                                                                ")
252                                 .append("                                                                                                                                                              ")
253                                 .append("       union all                                                                                                                                              ")
254                                 .append("                                                                                                                                                              ")
255                                 .append("                                                                                                                                                              ")
256                                 .append("  select distinct w.widget_id, w.wdg_name, w.wdg_desc from                                                                                                                        ")
257                                 .append("       ep_widget_catalog w                                                                                                                                    ")
258                                 .append("       where w.all_user_flag = 'Y'                                                                                                                            ")
259                                 .append("                                                                                                                                                              ")
260                                 .append("        ) userWidgets                                                                                                                                         ")
261                                 .append("                                                                                                                                                              ")
262                                 .append("  left join                                                                                                                                               ")
263                                 .append("                                                                                                                                                              ")
264                                 .append("  (                                                                                                                                                       ")
265                                 .append("               select case when pers.user_id is null then sel.user_id else pers.user_id end as 'user_id', case when sel.widget_id is null then                    ")
266                                 .append("                       pers.widget_id else sel.widget_id end as  'widget_id', pers.x, sel.status_cd, pers.y, pers.height, pers.width                                  ")
267                                 .append("                               from (select * from ep_pers_user_widget_placement where user_id = (select user_id from fn_user where login_id = '"
268                                                 + loginName + "')) pers ")
269                                 .append("                                       left outer join                                                                                                                        ")
270                                 .append("                               (select * from ep_pers_user_widget_sel where user_id = (select user_id from fn_user where login_id = '"
271                                                 + loginName + "')) sel             ")
272                                 .append("               on (pers.user_id = sel.user_id and pers.widget_id = sel.widget_id)                                                                                 ")
273                                 .append("                                                                                                                                                                      ")
274                                 .append("               union                                                                                                                                              ")
275                                 .append("                                                                                                                                                                      ")
276                                 .append("                select case when pers.user_id is null then sel.user_id else pers.user_id end as 'user_id',  case when sel.widget_id is null                       ")
277                                 .append("                       then pers.widget_id else sel.widget_id end as  'widget_id', pers.x, sel.status_cd, pers.y, pers.height, pers.width                             ")
278                                 .append("                               from (select * from ep_pers_user_widget_placement where user_id = (select user_id from fn_user where login_id = '"
279                                                 + loginName + "')) pers ")
280                                 .append("                                       right outer join                                                                                                                       ")
281                                 .append("                               (select * from ep_pers_user_widget_sel where user_id = (select user_id from fn_user where login_id = '"
282                                                 + loginName + "')) sel             ")
283                                 .append("               on (pers.user_id = sel.user_id and pers.widget_id = sel.widget_id)                                                                                 ")
284                                 .append("                                                                                                                                                                  ")
285                                 .append("                order by user_id, widget_id                                                                                                                       ")
286                                 .append(" )b                                                                                                                                                       ")
287                                 .append("  on                                                                                                                                                      ")
288                                 .append("  (userWidgets.widget_id = b.widget_id) order by b.x;                                                                                                     ");
289
290                 return sql;
291
292         }
293
294 }