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