JUnits for coverage
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / test / java / org / onap / portalapp / widget / service / impl / StorageServiceImplTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2018 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  * 
37  */
38 package org.onap.portalapp.widget.service.impl;
39
40 import static org.mockito.Mockito.when;
41
42 import java.io.File;
43 import java.io.FileOutputStream;
44 import java.util.ArrayList;
45 import java.util.List;
46
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;
62
63 public class StorageServiceImplTest {
64         
65         @InjectMocks
66         StorageServiceImpl storageServiceImpl;
67
68
69         @Mock
70         WidgetCatalogService widgetCatalogService;
71         
72         @Mock
73         Session session;
74         @Mock
75         SessionFactory sessionFactory;
76         
77         @Mock
78         Session currentSession;
79         @Mock
80         Transaction transaction;
81         @Mock
82         Criteria criteria;
83         
84         @Mock
85         Criteria widgetCriteria;
86         @Mock
87         MultipartFile file;
88         
89         
90         @Before
91         public void init() {
92             MockitoAnnotations.initMocks(this);
93         }
94         
95         @Test
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);
106         }
107         
108         @Test
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);
117                 
118         }
119         
120         @Test
121 public void testcheckZipFileInvalid() {
122                 MockMultipartFile mockMultipartFile = new MockMultipartFile(
123                             "fileData",
124                             "test.zip",
125                             "text/plain",
126                             "test".getBytes());
127         
128                 storageServiceImpl.checkZipFile(mockMultipartFile);
129         }
130         
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);  
136                 
137         }
138         
139         @Test(expected=NullPointerException.class)
140         public void testUpdate() {
141                 
142                 MockMultipartFile mockMultipartFile = new MockMultipartFile(
143                             "fileData",
144                             "test.zip",
145                             "text/plain",
146                             "test".getBytes());
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);
156                 
157         
158                 
159         }
160         
161         @Test
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);
172         
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);
181                 
182         }
183         
184         @Test(expected=IllegalArgumentException.class)
185         public void testSaveMultiPartFile() {
186                 MockMultipartFile mockMultipartFile = new MockMultipartFile(
187                             "fileData",
188                             "test.zip",
189                             "text/plain",
190                             "test".getBytes());
191                 
192                 WidgetCatalog catalog=new WidgetCatalog();
193                 catalog.setServiceId(2l);
194                 catalog.setName("test");
195                 
196                 storageServiceImpl.save(mockMultipartFile, catalog, 2l);
197                 
198                 
199         }
200         
201         @Test(expected=IllegalArgumentException.class)
202         public void testInitSave()throws Exception {
203                 MockMultipartFile mockMultipartFile = new MockMultipartFile(
204                             "fileData",
205                             "test.zip",
206                             "text/plain",
207                             "test".getBytes());
208                 File convFile = new File(mockMultipartFile.getOriginalFilename());
209                 FileOutputStream fos = new FileOutputStream(convFile);
210                 fos.write(mockMultipartFile.getBytes());
211                 fos.close();
212                 WidgetCatalog catalog=new WidgetCatalog();
213                 catalog.setServiceId(2l);
214                 catalog.setName("test");
215                 storageServiceImpl.initSave(convFile, catalog, 2l);
216                 convFile.delete();
217                 
218                 
219         }
220         
221         @Test
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);
230         
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);
236                 
237         }
238         
239         
240 }