92bcf805330728a1304678d2af4b4ff24f2993cc
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / test / java / org / openecomp / portalapp / widget / test / service / WidgetCatalogServiceTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.widget.test.service;
39
40
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertNotNull;
43 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
44
45 import java.util.ArrayList;
46 import java.util.HashSet;
47 import java.util.List;
48 import java.util.Set;
49
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;
67
68
69 @RunWith(MockitoJUnitRunner.class)
70 public class WidgetCatalogServiceTest {
71         
72         @Mock
73         private SessionFactory mockedSessionFactory;
74
75         @InjectMocks
76         private WidgetCatalogServiceImpl widgetCatalogService;
77
78         @Before
79         public void setUp() {
80                 MockitoAnnotations.initMocks(this);
81         }
82         
83         @Test
84         public void getWidgetCatalog_NoError() throws Exception{
85                 Session mockedSession = Mockito.mock(Session.class);
86                 Criteria mockedCriteria = Mockito.mock(Criteria.class);
87                 
88                 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
89                 WidgetCatalog widget = new WidgetCatalog();
90                 widget.setId(1);
91                 widget.setName("junit");
92                 list.add(widget);
93                 
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);
98                 
99                 List<WidgetCatalog> result = widgetCatalogService.getWidgetCatalog();
100                 assertNotNull(result);
101                 assertEquals(result, list);
102         }
103         
104         
105         @Test
106         public void saveWidgetCatalog_NoError() throws Exception{
107                 Set<RoleApp> set = new HashSet<RoleApp>();
108                 WidgetCatalog widget = new WidgetCatalog();
109                 widget.setId(1);
110                 widget.setName("junit");
111                 widget.setAllowAllUser("1");
112                 widget.setWidgetRoles(set);
113                 
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);
121         }
122         
123         @Rule public ExpectedException thrown = ExpectedException.none();
124         
125         
126         @Test
127         public void deleteWidgetCatalog_NoError() throws Exception{
128                 long widgetId =1 ;
129                 WidgetCatalog widget = new WidgetCatalog();
130                 widget.setId(1);
131                 widget.setName("junit");
132
133                 Session mockedSession = Mockito.mock(Session.class, RETURNS_DEEP_STUBS);
134                 Transaction mockedTransaction = Mockito.mock(Transaction.class);
135                 
136                 Mockito.when(mockedSessionFactory.getCurrentSession()).thenReturn(mockedSession);
137                 Mockito.when(mockedSession.beginTransaction()).thenReturn(mockedTransaction);
138                 Mockito.when(mockedSession.get(WidgetCatalog.class, widgetId)).thenReturn(widget);
139                 
140                 widgetCatalogService.deleteWidgetCatalog(widgetId);
141         }
142         
143         @Test
144         public void updateWidgetCatalog_NoError() throws Exception{
145                 long widgetId =1 ;
146                 WidgetCatalog widget = new WidgetCatalog();
147                 widget.setId(1);
148                 widget.setName("junit");
149
150                 Session mockedSession = Mockito.mock(Session.class, RETURNS_DEEP_STUBS);
151                 Transaction mockedTransaction = Mockito.mock(Transaction.class);
152                 
153                 Mockito.when(mockedSessionFactory.getCurrentSession()).thenReturn(mockedSession);
154                 Mockito.when(mockedSession.beginTransaction()).thenReturn(mockedTransaction);
155                 Mockito.when(mockedSession.get(WidgetCatalog.class, widgetId)).thenReturn(widget);
156                 
157                 widgetCatalogService.deleteWidgetCatalog(widgetId);
158         }
159         
160         
161 }