summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/service/impl/WidgetCatalogServiceImplTest.java
blob: 933710d2c1cfc9d9791aa6a570b84d9fbeb81bee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*-
 * ============LICENSE_START==========================================
 * ONAP Portal
 * ===================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the "License");
 * you may not use this software except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END============================================
 *
 * 
 */
package org.onap.portalapp.widget.service.impl;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.onap.portalapp.widget.domain.App;
import org.onap.portalapp.widget.domain.RoleApp;
import org.onap.portalapp.widget.domain.WidgetCatalog;

public class WidgetCatalogServiceImplTest {

	@InjectMocks
	WidgetCatalogServiceImpl widgetCatalogServiceImpl;

	@Mock
	Session session;
	@Mock
	SessionFactory sessionFactory;
	@Mock
	Transaction transaction;
	@Mock
	Criteria criteria;

	@Mock
	SQLQuery query;

	@Before
	public void init() {
		MockitoAnnotations.initMocks(this);
	}

	@Test
	public void testSaveMicroserivce() {
		List<WidgetCatalog> list = buildWidgetCatalog();
		when(sessionFactory.getCurrentSession()).thenReturn(session);
		// when(session.beginTransaction()).thenReturn(transaction);
		when(session.createCriteria(WidgetCatalog.class)).thenReturn(criteria);
		when(criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)).thenReturn(criteria);
		when(criteria.list()).thenReturn(list);

		List<WidgetCatalog> catalogList = widgetCatalogServiceImpl.getWidgetCatalog();
		assertEquals("junit", catalogList.get(0).getName());

	}

	@Test
	public void tesGetUserWidgetCatalog() {
		when(sessionFactory.getCurrentSession()).thenReturn(session);
		StringBuilder sb = widgetUserCatalog("test");
		when(session.createSQLQuery(sb.toString())).thenReturn(query);
		when(query.list()).thenReturn(buildWidgetCatalog());
		List<WidgetCatalog> catalogList = widgetCatalogServiceImpl.getUserWidgetCatalog("test");
		assertEquals("junit", catalogList.get(0).getName());

	}

	@Test
	public void getWidgetCatalog() {

		when(sessionFactory.getCurrentSession()).thenReturn(session);
		when(session.beginTransaction()).thenReturn(transaction);
		when(session.get(WidgetCatalog.class, 1l)).thenReturn(buildWidgetCatalog().get(0));
		WidgetCatalog catalog = widgetCatalogServiceImpl.getWidgetCatalog(1l);
		assertEquals("junit", catalog.getName());
	}

	@Test
	public void deleteWidgetCatalog() {
		Long widgetCatalogId = 1l;
		when(sessionFactory.getCurrentSession()).thenReturn(session, session);
		when(session.beginTransaction()).thenReturn(transaction, transaction);
		when(session.get(WidgetCatalog.class, widgetCatalogId)).thenReturn(buildWidgetCatalog().get(0));
		String queryString = "delete from ep_pers_user_widget_sel where widget_id = :widgetId ";
		when(session.createSQLQuery(queryString)).thenReturn(query);

		String deleteEpUserWidget = "delete from ep_pers_user_widget_placement where widget_id = :widgetId ";
		String deleteEpUserWidgetCatalog = "delete from ep_widget_catalog_files where widget_id = :widgetId ";
		String deleteUserWidgetCatalog = "delete from ep_widget_catalog_parameter where widget_id = :widgetId ";
		when(session.createSQLQuery(deleteEpUserWidget)).thenReturn(query);
		when(session.createSQLQuery(deleteEpUserWidgetCatalog)).thenReturn(query);
		when(session.createSQLQuery(deleteUserWidgetCatalog)).thenReturn(query);
		when(query.setParameter("widgetId", widgetCatalogId)).thenReturn(query, query, query, query);
		widgetCatalogServiceImpl.deleteWidgetCatalog(widgetCatalogId);

	}

	@Test
	public void deleteWidgetCatalogEmpty() {
		Long widgetCatalogId = 1l;
		when(sessionFactory.getCurrentSession()).thenReturn(session, session);
		when(session.beginTransaction()).thenReturn(transaction, transaction);
		when(session.get(WidgetCatalog.class, widgetCatalogId)).thenReturn(null);
		widgetCatalogServiceImpl.deleteWidgetCatalog(widgetCatalogId);
	}

	@Test
	public void saveWidgetCatalog() {
		WidgetCatalog widgetCatalog = buildWidgetCatalog().get(0);
		widgetCatalog.setAllowAllUser("1");
		App app = new App();
		app.setAppId(1l);
		RoleApp roleApp = new RoleApp();
		roleApp.setRoleId(1l);
		roleApp.setApp(app);
		Set<RoleApp> roles = new HashSet<>();
		roles.add(roleApp);
		widgetCatalog.setWidgetRoles(roles);
		String sql = "UPDATE ep_widget_catalog_role SET app_id = " + roleApp.getApp().getAppId() + " WHERE widget_id = "
				+ widgetCatalog.getId() + " AND ROLE_ID = " + roleApp.getRoleId();
		when(sessionFactory.openSession()).thenReturn(session, session);
		when(session.beginTransaction()).thenReturn(transaction);
		when(session.createSQLQuery(sql)).thenReturn(query);
		widgetCatalogServiceImpl.saveWidgetCatalog(widgetCatalog);

	}

	@Test
	public void updateWidgetCatalog() {
		Long widgetCatalogId = 1l;
		WidgetCatalog widgetCatalog = buildWidgetCatalog().get(0);
		widgetCatalog.setServiceId(widgetCatalogId);
		widgetCatalog.setAllowAllUser("1");
		App app = new App();
		app.setAppId(1l);
		RoleApp roleApp = new RoleApp();
		roleApp.setRoleId(1l);
		roleApp.setApp(app);
		Set<RoleApp> roles = new HashSet<>();
		roles.add(roleApp);
		widgetCatalog.setWidgetRoles(roles);
		String sql = "UPDATE ep_widget_catalog_role SET app_id = " + roleApp.getApp().getAppId() + " WHERE widget_id = "
				+ widgetCatalog.getId() + " AND ROLE_ID = " + roleApp.getRoleId();
		when(sessionFactory.getCurrentSession()).thenReturn(session);
		when(session.beginTransaction()).thenReturn(transaction, transaction);
		when(session.get(WidgetCatalog.class, widgetCatalogId)).thenReturn(buildWidgetCatalog().get(0));
		when(sessionFactory.openSession()).thenReturn(session, session);
		when(session.createSQLQuery(sql)).thenReturn(query);

		widgetCatalogServiceImpl.updateWidgetCatalog(widgetCatalogId, widgetCatalog);

	}

	@Test
	public void getServiceIdByWidget() {
		Long widgetCatalogId = 1l;
		when(sessionFactory.getCurrentSession()).thenReturn(session, session);
		when(session.beginTransaction()).thenReturn(transaction, transaction);
		when(session.get(WidgetCatalog.class, widgetCatalogId)).thenReturn(buildWidgetCatalog().get(0));
		Long serviceId = widgetCatalogServiceImpl.getServiceIdByWidget(widgetCatalogId);
		assertEquals(widgetCatalogId, serviceId);

	}
	
	@Test(expected=NullPointerException.class)
	public void getWidgetsByServiceId() {
		Long serviceId=1l;
		List<WidgetCatalog> list = buildWidgetCatalog();
		when(sessionFactory.getCurrentSession()).thenReturn(session,session);
		// when(session.beginTransaction()).thenReturn(transaction);
		when(session.createCriteria(WidgetCatalog.class)).thenReturn(criteria);
		when(criteria.add(Restrictions.eq("serviceId", serviceId))).thenReturn(criteria);
		when(criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)).thenReturn(criteria);
		when(criteria.list()).thenReturn(list);

		List<WidgetCatalog> catalogList = widgetCatalogServiceImpl.getWidgetsByServiceId(serviceId);
		assertEquals("junit", catalogList.get(0).getName());
	}

	private List<WidgetCatalog> buildWidgetCatalog() {
		List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
		WidgetCatalog widget = new WidgetCatalog();
		widget.setId(1);
		widget.setName("junit");
		widget.setServiceId(1l);
		list.add(widget);
		return list;
	}

	private StringBuilder widgetUserCatalog(String loginName) {
		StringBuilder sql = new StringBuilder()

				.append("  select userWidgets.widget_id, userWidgets.wdg_name, userWidgets.wdg_desc, b.x, b.status_cd, b.y, b.height, b.width  from      		                   ")
				.append("  (  			                                                                                                                                           ")
				.append("  select distinct w.widget_id, w.wdg_name, w.wdg_desc from                                 														  	   ")
				.append("  ep_widget_catalog w,                                                                                                                            		   ")
				.append("  ep_widget_catalog_role wr,                                                                                                             				   ")
				.append("  fn_user_role ur,																																		   ")
				.append("  fn_app app,																																		   ")
				.append("  fn_user u                                                                                                                          					   ")
				.append("  where                                                                                                                                            	   ")
				.append("  w.widget_id = wr.WIDGET_ID and 																														   ")
				.append("  ur.app_id = app.app_id and 																														   ")
				.append("  app.enabled = 'Y' and																														   ")
				.append("  wr.role_id = ur.role_id and      																													   ")
				.append("  ur.user_id = u.user_id and         																													   ")
				.append("  u.login_id = '" + loginName
						+ "' and (w.all_user_flag = 'N' or w.all_user_flag is null)                                                          	   ")
				.append(" 	                                                                                                                                                       ")
				.append(" 	union all                                                                                                                                              ")
				.append(" 	                                                                                                                                                       ")
				.append(" 	                                                                                                                                                       ")
				.append("  select distinct w.widget_id, w.wdg_name, w.wdg_desc from                                                                  							   ")
				.append(" 	ep_widget_catalog w                                                                                                                                    ")
				.append(" 	where w.all_user_flag = 'Y'                                                                                                                            ")
				.append(" 	                                                                                                                                                       ")
				.append(" 	 ) userWidgets                                                                                                                                         ")
				.append(" 	                                                                                                                                                       ")
				.append("  left join                                                                                                                                               ")
				.append(" 	                                                                                                                                                       ")
				.append("  (                                                                                                                                                       ")
				.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                    ")
				.append(" 			pers.widget_id else sel.widget_id end as  'widget_id', pers.x, sel.status_cd, pers.y, pers.height, pers.width                                  ")
				.append(" 				from (select * from ep_pers_user_widget_placement where user_id = (select user_id from fn_user where login_id = '"
						+ loginName + "')) pers ")
				.append(" 					left outer join                                                                                                                        ")
				.append(" 				(select * from ep_pers_user_widget_sel where user_id = (select user_id from fn_user where login_id = '"
						+ loginName + "')) sel             ")
				.append(" 		on (pers.user_id = sel.user_id and pers.widget_id = sel.widget_id)                                                                                 ")
				.append(" 			                                                                                                                                               ")
				.append(" 		union                                                                                                                                              ")
				.append(" 			                                                                                                                                               ")
				.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                       ")
				.append(" 			then pers.widget_id else sel.widget_id end as  'widget_id', pers.x, sel.status_cd, pers.y, pers.height, pers.width                             ")
				.append(" 				from (select * from ep_pers_user_widget_placement where user_id = (select user_id from fn_user where login_id = '"
						+ loginName + "')) pers ")
				.append(" 					right outer join                                                                                                                       ")
				.append(" 				(select * from ep_pers_user_widget_sel where user_id = (select user_id from fn_user where login_id = '"
						+ loginName + "')) sel             ")
				.append(" 		on (pers.user_id = sel.user_id and pers.widget_id = sel.widget_id)                                                                                 ")
				.append(" 		                                                                                                                                                   ")
				.append(" 		 order by user_id, widget_id                                                                                                                       ")
				.append(" )b                                                                                                                                                       ")
				.append("  on                                                                                                                                                      ")
				.append("  (userWidgets.widget_id = b.widget_id) order by b.x;                                                                                                     ");

		return sql;

	}

}