2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.openecomp.portalapp.widget.test.service;
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertNotNull;
43 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
45 import java.util.ArrayList;
46 import java.util.HashSet;
47 import java.util.List;
50 import org.hibernate.Criteria;
51 import org.hibernate.Session;
52 import org.hibernate.SessionFactory;
53 import org.hibernate.Transaction;
54 import org.junit.Before;
55 import org.junit.Rule;
56 import org.junit.Test;
57 import org.junit.rules.ExpectedException;
58 import org.junit.runner.RunWith;
59 import org.mockito.InjectMocks;
60 import org.mockito.Mock;
61 import org.mockito.Mockito;
62 import org.mockito.MockitoAnnotations;
63 import org.mockito.runners.MockitoJUnitRunner;
64 import org.openecomp.portalapp.widget.domain.RoleApp;
65 import org.openecomp.portalapp.widget.domain.WidgetCatalog;
66 import org.openecomp.portalapp.widget.service.impl.WidgetCatalogServiceImpl;
69 @RunWith(MockitoJUnitRunner.class)
70 public class WidgetCatalogServiceTest {
73 private SessionFactory mockedSessionFactory;
76 private WidgetCatalogServiceImpl widgetCatalogService;
80 MockitoAnnotations.initMocks(this);
84 public void getWidgetCatalog_NoError() throws Exception{
85 Session mockedSession = Mockito.mock(Session.class);
86 Criteria mockedCriteria = Mockito.mock(Criteria.class);
88 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
89 WidgetCatalog widget = new WidgetCatalog();
91 widget.setName("junit");
94 Mockito.when(mockedSessionFactory.getCurrentSession()).thenReturn(mockedSession);
95 Mockito.when(mockedSession.createCriteria(WidgetCatalog.class)).thenReturn(mockedCriteria);
96 Mockito.when(mockedCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)).thenReturn(mockedCriteria);
97 Mockito.when(mockedCriteria.list()).thenReturn(list);
99 List<WidgetCatalog> result = widgetCatalogService.getWidgetCatalog();
100 assertNotNull(result);
101 assertEquals(result, list);
106 public void saveWidgetCatalog_NoError() throws Exception{
107 Set<RoleApp> set = new HashSet<RoleApp>();
108 WidgetCatalog widget = new WidgetCatalog();
110 widget.setName("junit");
111 widget.setAllowAllUser("1");
112 widget.setWidgetRoles(set);
114 Session mockedSession = Mockito.mock(Session.class);
115 Transaction mockedTransaction = Mockito.mock(Transaction.class);
116 Mockito.when(mockedSessionFactory.openSession()).thenReturn(mockedSession);
117 Mockito.when(mockedSession.beginTransaction()).thenReturn(mockedTransaction);
118 long widgetId = widgetCatalogService.saveWidgetCatalog(widget);
119 assertNotNull(widgetId);
120 assertEquals(widgetId, 1);
123 @Rule public ExpectedException thrown = ExpectedException.none();
127 public void deleteWidgetCatalog_NoError() throws Exception{
129 WidgetCatalog widget = new WidgetCatalog();
131 widget.setName("junit");
133 Session mockedSession = Mockito.mock(Session.class, RETURNS_DEEP_STUBS);
134 Transaction mockedTransaction = Mockito.mock(Transaction.class);
136 Mockito.when(mockedSessionFactory.getCurrentSession()).thenReturn(mockedSession);
137 Mockito.when(mockedSession.beginTransaction()).thenReturn(mockedTransaction);
138 Mockito.when(mockedSession.get(WidgetCatalog.class, widgetId)).thenReturn(widget);
140 widgetCatalogService.deleteWidgetCatalog(widgetId);
144 public void updateWidgetCatalog_NoError() throws Exception{
146 WidgetCatalog widget = new WidgetCatalog();
148 widget.setName("junit");
150 Session mockedSession = Mockito.mock(Session.class, RETURNS_DEEP_STUBS);
151 Transaction mockedTransaction = Mockito.mock(Transaction.class);
153 Mockito.when(mockedSessionFactory.getCurrentSession()).thenReturn(mockedSession);
154 Mockito.when(mockedSession.beginTransaction()).thenReturn(mockedTransaction);
155 Mockito.when(mockedSession.get(WidgetCatalog.class, widgetId)).thenReturn(widget);
157 widgetCatalogService.deleteWidgetCatalog(widgetId);