2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.widget.service.impl;
40 import static org.junit.Assert.assertEquals;
41 import static org.mockito.Mockito.when;
43 import java.util.ArrayList;
44 import java.util.HashSet;
45 import java.util.List;
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;
63 public class WidgetCatalogServiceImplTest {
66 WidgetCatalogServiceImpl widgetCatalogServiceImpl;
71 SessionFactory sessionFactory;
73 Transaction transaction;
82 MockitoAnnotations.initMocks(this);
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);
94 List<WidgetCatalog> catalogList = widgetCatalogServiceImpl.getWidgetCatalog();
95 assertEquals("junit", catalogList.get(0).getName());
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());
111 public void getWidgetCatalog() {
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());
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);
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);
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);
150 public void saveWidgetCatalog() {
151 WidgetCatalog widgetCatalog = buildWidgetCatalog().get(0);
152 widgetCatalog.setAllowAllUser("1");
155 RoleApp roleApp = new RoleApp();
156 roleApp.setRoleId(1l);
158 Set<RoleApp> roles = new HashSet<>();
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);
171 public void updateWidgetCatalog() {
172 Long widgetCatalogId = 1l;
173 WidgetCatalog widgetCatalog = buildWidgetCatalog().get(0);
174 widgetCatalog.setServiceId(widgetCatalogId);
175 widgetCatalog.setAllowAllUser("1");
178 RoleApp roleApp = new RoleApp();
179 roleApp.setRoleId(1l);
181 Set<RoleApp> roles = new HashSet<>();
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);
192 widgetCatalogServiceImpl.updateWidgetCatalog(widgetCatalogId, widgetCatalog);
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);
207 @Test(expected=NullPointerException.class)
208 public void getWidgetsByServiceId() {
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);
218 List<WidgetCatalog> catalogList = widgetCatalogServiceImpl.getWidgetsByServiceId(serviceId);
219 assertEquals("junit", catalogList.get(0).getName());
222 private List<WidgetCatalog> buildWidgetCatalog() {
223 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
224 WidgetCatalog widget = new WidgetCatalog();
226 widget.setName("junit");
227 widget.setServiceId(1l);
232 private StringBuilder widgetUserCatalog(String loginName) {
233 StringBuilder sql = new StringBuilder()
235 .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(" 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 ")
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) ")
252 .append(" union all ")
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' ")
259 .append(" ) userWidgets ")
261 .append(" left join ")
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) ")
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) ")
284 .append(" order by user_id, widget_id ")
287 .append(" (userWidgets.widget_id = b.widget_id) order by b.x; ");