Merge "Replicate demo/boot/portal_vm_init.sh"
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / test / java / org / openecomp / portalapp / widget / test / service / WidgetCatalogServiceTest.java
1 package org.openecomp.portalapp.widget.test.service;
2
3
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertNotNull;
6 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
7
8 import java.util.ArrayList;
9 import java.util.HashSet;
10 import java.util.List;
11 import java.util.Set;
12
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;
30
31
32 @RunWith(MockitoJUnitRunner.class)
33 public class WidgetCatalogServiceTest {
34         
35         @Mock
36         private SessionFactory mockedSessionFactory;
37
38         @InjectMocks
39         private WidgetCatalogServiceImpl widgetCatalogService;
40
41         @Before
42         public void setUp() {
43                 MockitoAnnotations.initMocks(this);
44         }
45         
46         @Test
47         public void getWidgetCatalog_NoError() throws Exception{
48                 Session mockedSession = Mockito.mock(Session.class);
49                 Criteria mockedCriteria = Mockito.mock(Criteria.class);
50                 
51                 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
52                 WidgetCatalog widget = new WidgetCatalog();
53                 widget.setId(1);
54                 widget.setName("junit");
55                 list.add(widget);
56                 
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);
61                 
62                 List<WidgetCatalog> result = widgetCatalogService.getWidgetCatalog();
63                 assertNotNull(result);
64                 assertEquals(result, list);
65         }
66         
67         
68         @Test
69         public void saveWidgetCatalog_NoError() throws Exception{
70                 Set<RoleApp> set = new HashSet<RoleApp>();
71                 WidgetCatalog widget = new WidgetCatalog();
72                 widget.setId(1);
73                 widget.setName("junit");
74                 widget.setAllowAllUser("1");
75                 widget.setWidgetRoles(set);
76                 
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);
84         }
85         
86         @Rule public ExpectedException thrown = ExpectedException.none();
87         
88         
89         @Test
90         public void deleteWidgetCatalog_NoError() throws Exception{
91                 long widgetId =1 ;
92                 WidgetCatalog widget = new WidgetCatalog();
93                 widget.setId(1);
94                 widget.setName("junit");
95
96                 Session mockedSession = Mockito.mock(Session.class, RETURNS_DEEP_STUBS);
97                 Transaction mockedTransaction = Mockito.mock(Transaction.class);
98                 
99                 Mockito.when(mockedSessionFactory.getCurrentSession()).thenReturn(mockedSession);
100                 Mockito.when(mockedSession.beginTransaction()).thenReturn(mockedTransaction);
101                 Mockito.when(mockedSession.get(WidgetCatalog.class, widgetId)).thenReturn(widget);
102                 
103                 widgetCatalogService.deleteWidgetCatalog(widgetId);
104         }
105         
106         @Test
107         public void updateWidgetCatalog_NoError() throws Exception{
108                 long widgetId =1 ;
109                 WidgetCatalog widget = new WidgetCatalog();
110                 widget.setId(1);
111                 widget.setName("junit");
112
113                 Session mockedSession = Mockito.mock(Session.class, RETURNS_DEEP_STUBS);
114                 Transaction mockedTransaction = Mockito.mock(Transaction.class);
115                 
116                 Mockito.when(mockedSessionFactory.getCurrentSession()).thenReturn(mockedSession);
117                 Mockito.when(mockedSession.beginTransaction()).thenReturn(mockedTransaction);
118                 Mockito.when(mockedSession.get(WidgetCatalog.class, widgetId)).thenReturn(widget);
119                 
120                 widgetCatalogService.deleteWidgetCatalog(widgetId);
121         }
122         
123         
124 }