2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2018 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============================================
38 package org.onap.portalapp.widget.service.impl;
40 import static org.mockito.Mockito.when;
43 import java.io.FileOutputStream;
44 import java.util.ArrayList;
45 import java.util.List;
47 import org.hibernate.Criteria;
48 import org.hibernate.Session;
49 import org.hibernate.SessionFactory;
50 import org.hibernate.Transaction;
51 import org.junit.Before;
52 import org.junit.Test;
53 import org.mockito.InjectMocks;
54 import org.mockito.Mock;
55 import org.mockito.MockitoAnnotations;
56 import org.onap.portalapp.widget.domain.WidgetCatalog;
57 import org.onap.portalapp.widget.domain.WidgetFile;
58 import org.onap.portalapp.widget.excetpion.StorageException;
59 import org.onap.portalapp.widget.service.WidgetCatalogService;
60 import org.springframework.mock.web.MockMultipartFile;
61 import org.springframework.web.multipart.MultipartFile;
63 public class StorageServiceImplTest {
66 StorageServiceImpl storageServiceImpl;
70 WidgetCatalogService widgetCatalogService;
75 SessionFactory sessionFactory;
78 Session currentSession;
80 Transaction transaction;
85 Criteria widgetCriteria;
92 MockitoAnnotations.initMocks(this);
96 public void testDeleteWidgetFile() {
97 List<WidgetFile> widgetFiles=new ArrayList<>();
98 WidgetFile file=new WidgetFile();
99 widgetFiles.add(file);
100 when(sessionFactory.openSession()).thenReturn(session);
101 when(session.createCriteria(WidgetFile.class)).thenReturn(criteria);
102 when(criteria.list()).thenReturn(widgetFiles);
103 when(sessionFactory.getCurrentSession()).thenReturn(currentSession);
104 when(currentSession.beginTransaction()).thenReturn(transaction);
105 storageServiceImpl.deleteWidgetFile(2l);
109 public void testGetWidgetFile() {
110 List<WidgetFile> widgetFiles=new ArrayList<>();
111 WidgetFile file=new WidgetFile();
112 widgetFiles.add(file);
113 when(sessionFactory.openSession()).thenReturn(session);
114 when(session.createCriteria(WidgetFile.class)).thenReturn(criteria);
115 when(criteria.list()).thenReturn(widgetFiles);
116 storageServiceImpl.getWidgetFile(2l);
121 public void testcheckZipFileInvalid() {
122 MockMultipartFile mockMultipartFile = new MockMultipartFile(
128 storageServiceImpl.checkZipFile(mockMultipartFile);
131 @Test(expected=StorageException.class)
132 public void testcheckZipEmptyFile() {
133 when(file.getOriginalFilename()).thenReturn("test.zip");
134 when(file.isEmpty()).thenReturn(true);
135 storageServiceImpl.checkZipFile(file);
139 @Test(expected=NullPointerException.class)
140 public void testUpdate() {
142 MockMultipartFile mockMultipartFile = new MockMultipartFile(
147 WidgetCatalog catalog=new WidgetCatalog();
148 catalog.setServiceId(2l);
149 List<WidgetFile> widgetFiles=new ArrayList<>();
150 WidgetFile file=new WidgetFile();
151 widgetFiles.add(file);
152 when(sessionFactory.openSession()).thenReturn(session);
153 when(session.createCriteria(WidgetFile.class)).thenReturn(criteria);
154 when(criteria.list()).thenReturn(widgetFiles);
155 storageServiceImpl.update(mockMultipartFile, catalog, 2l);
162 public void testGetWidgetCatalogContent()throws Exception {
163 WidgetCatalog catalog=new WidgetCatalog();
164 catalog.setServiceId(2l);
165 catalog.setName("test");
166 List<WidgetFile> widgetFiles=new ArrayList<>();
167 WidgetFile file=new WidgetFile();
168 file.setCss("test".getBytes());
169 file.setController("test function() Test".getBytes());
170 file.setMarkup("Test".getBytes());
171 widgetFiles.add(file);
173 when(sessionFactory.getCurrentSession()).thenReturn(currentSession);
174 when(currentSession.beginTransaction()).thenReturn(transaction);
175 when(currentSession.createCriteria(WidgetFile.class)).thenReturn(criteria,criteria,criteria);
176 when(criteria.list()).thenReturn(widgetFiles);
177 // when(currentSession.createCriteria(WidgetFile.class)).thenReturn(criteria);
178 // when(criteria.list()).thenReturn(widgetFiles);
179 when(widgetCatalogService.getWidgetCatalog(2l)).thenReturn(catalog);
180 storageServiceImpl.getWidgetCatalogContent(2l);
184 @Test(expected=IllegalArgumentException.class)
185 public void testSaveMultiPartFile() {
186 MockMultipartFile mockMultipartFile = new MockMultipartFile(
192 WidgetCatalog catalog=new WidgetCatalog();
193 catalog.setServiceId(2l);
194 catalog.setName("test");
196 storageServiceImpl.save(mockMultipartFile, catalog, 2l);
201 @Test(expected=IllegalArgumentException.class)
202 public void testInitSave()throws Exception {
203 MockMultipartFile mockMultipartFile = new MockMultipartFile(
208 File convFile = new File(mockMultipartFile.getOriginalFilename());
209 FileOutputStream fos = new FileOutputStream(convFile);
210 fos.write(mockMultipartFile.getBytes());
212 WidgetCatalog catalog=new WidgetCatalog();
213 catalog.setServiceId(2l);
214 catalog.setName("test");
215 storageServiceImpl.initSave(convFile, catalog, 2l);
222 public void testWidgetFramework()throws Exception {
223 List<WidgetFile> widgetFiles=new ArrayList<>();
224 WidgetFile file=new WidgetFile();
225 file.setCss("test".getBytes());
226 file.setController("test function() Test".getBytes());
227 file.setMarkup("Test".getBytes());
228 file.setFramework("test".getBytes());
229 widgetFiles.add(file);
231 when(sessionFactory.getCurrentSession()).thenReturn(currentSession);
232 when(currentSession.beginTransaction()).thenReturn(transaction);
233 when(currentSession.createCriteria(WidgetFile.class)).thenReturn(criteria);
234 when(criteria.list()).thenReturn(widgetFiles);
235 storageServiceImpl.getWidgetFramework(2l);