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;
62 import org.hibernate.query.NativeQuery;
64 public class WidgetCatalogServiceImplTest {
67 WidgetCatalogServiceImpl widgetCatalogServiceImpl;
72 SessionFactory sessionFactory;
74 Transaction transaction;
83 MockitoAnnotations.initMocks(this);
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);
95 List<WidgetCatalog> catalogList = widgetCatalogServiceImpl.getWidgetCatalog();
96 assertEquals("junit", catalogList.get(0).getName());
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());
112 public void getWidgetCatalog() {
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());
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);
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);
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);
151 public void saveWidgetCatalog() {
152 WidgetCatalog widgetCatalog = buildWidgetCatalog().get(0);
153 widgetCatalog.setAllowAllUser("1");
156 RoleApp roleApp = new RoleApp();
157 roleApp.setRoleId(1l);
159 Set<RoleApp> roles = new HashSet<>();
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);
172 public void updateWidgetCatalog() {
173 Long widgetCatalogId = 1l;
174 WidgetCatalog widgetCatalog = buildWidgetCatalog().get(0);
175 widgetCatalog.setServiceId(widgetCatalogId);
176 widgetCatalog.setAllowAllUser("1");
179 RoleApp roleApp = new RoleApp();
180 roleApp.setRoleId(1l);
182 Set<RoleApp> roles = new HashSet<>();
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);
193 widgetCatalogServiceImpl.updateWidgetCatalog(widgetCatalogId, widgetCatalog);
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);
208 @Test(expected=NullPointerException.class)
209 public void getWidgetsByServiceId() {
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);
219 List<WidgetCatalog> catalogList = widgetCatalogServiceImpl.getWidgetsByServiceId(serviceId);
220 assertEquals("junit", catalogList.get(0).getName());
223 private List<WidgetCatalog> buildWidgetCatalog() {
224 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
225 WidgetCatalog widget = new WidgetCatalog();
227 widget.setName("junit");
228 widget.setServiceId(1l);
233 private StringBuilder widgetUserCatalog(String loginName) {
234 StringBuilder sql = new StringBuilder()
236 .append(" select userWidgets.widget_id, userWidgets.wdg_name, userWidgets.wdg_desc, b.x, b.status_cd, b.y, b.height, b.width from ")
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 ")
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) ")
253 .append(" union all ")
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' ")
260 .append(" ) userWidgets ")
262 .append(" left join ")
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) ")
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) ")
285 .append(" order by user_id, widget_id ")
288 .append(" (userWidgets.widget_id = b.widget_id) order by b.x; ");