1 package org.openecomp.portalapp.widget.test.service;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertNotNull;
6 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
8 import java.util.ArrayList;
9 import java.util.HashSet;
10 import java.util.List;
13 import org.hibernate.Criteria;
14 import org.hibernate.Session;
15 import org.hibernate.SessionFactory;
16 import org.hibernate.Transaction;
17 import org.junit.Before;
18 import org.junit.Rule;
19 import org.junit.Test;
20 import org.junit.rules.ExpectedException;
21 import org.junit.runner.RunWith;
22 import org.mockito.InjectMocks;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.MockitoAnnotations;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.openecomp.portalapp.widget.domain.RoleApp;
28 import org.openecomp.portalapp.widget.domain.WidgetCatalog;
29 import org.openecomp.portalapp.widget.service.impl.WidgetCatalogServiceImpl;
32 @RunWith(MockitoJUnitRunner.class)
33 public class WidgetCatalogServiceTest {
36 private SessionFactory mockedSessionFactory;
39 private WidgetCatalogServiceImpl widgetCatalogService;
43 MockitoAnnotations.initMocks(this);
47 public void getWidgetCatalog_NoError() throws Exception{
48 Session mockedSession = Mockito.mock(Session.class);
49 Criteria mockedCriteria = Mockito.mock(Criteria.class);
51 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
52 WidgetCatalog widget = new WidgetCatalog();
54 widget.setName("junit");
57 Mockito.when(mockedSessionFactory.getCurrentSession()).thenReturn(mockedSession);
58 Mockito.when(mockedSession.createCriteria(WidgetCatalog.class)).thenReturn(mockedCriteria);
59 Mockito.when(mockedCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)).thenReturn(mockedCriteria);
60 Mockito.when(mockedCriteria.list()).thenReturn(list);
62 List<WidgetCatalog> result = widgetCatalogService.getWidgetCatalog();
63 assertNotNull(result);
64 assertEquals(result, list);
69 public void saveWidgetCatalog_NoError() throws Exception{
70 Set<RoleApp> set = new HashSet<RoleApp>();
71 WidgetCatalog widget = new WidgetCatalog();
73 widget.setName("junit");
74 widget.setAllowAllUser("1");
75 widget.setWidgetRoles(set);
77 Session mockedSession = Mockito.mock(Session.class);
78 Transaction mockedTransaction = Mockito.mock(Transaction.class);
79 Mockito.when(mockedSessionFactory.openSession()).thenReturn(mockedSession);
80 Mockito.when(mockedSession.beginTransaction()).thenReturn(mockedTransaction);
81 long widgetId = widgetCatalogService.saveWidgetCatalog(widget);
82 assertNotNull(widgetId);
83 assertEquals(widgetId, 1);
86 @Rule public ExpectedException thrown = ExpectedException.none();
90 public void deleteWidgetCatalog_NoError() throws Exception{
92 WidgetCatalog widget = new WidgetCatalog();
94 widget.setName("junit");
96 Session mockedSession = Mockito.mock(Session.class, RETURNS_DEEP_STUBS);
97 Transaction mockedTransaction = Mockito.mock(Transaction.class);
99 Mockito.when(mockedSessionFactory.getCurrentSession()).thenReturn(mockedSession);
100 Mockito.when(mockedSession.beginTransaction()).thenReturn(mockedTransaction);
101 Mockito.when(mockedSession.get(WidgetCatalog.class, widgetId)).thenReturn(widget);
103 widgetCatalogService.deleteWidgetCatalog(widgetId);
107 public void updateWidgetCatalog_NoError() throws Exception{
109 WidgetCatalog widget = new WidgetCatalog();
111 widget.setName("junit");
113 Session mockedSession = Mockito.mock(Session.class, RETURNS_DEEP_STUBS);
114 Transaction mockedTransaction = Mockito.mock(Transaction.class);
116 Mockito.when(mockedSessionFactory.getCurrentSession()).thenReturn(mockedSession);
117 Mockito.when(mockedSession.beginTransaction()).thenReturn(mockedTransaction);
118 Mockito.when(mockedSession.get(WidgetCatalog.class, widgetId)).thenReturn(widget);
120 widgetCatalogService.deleteWidgetCatalog(widgetId);