Fix sonar issues
[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.util.ArrayList;
43 import java.util.List;
44
45 import org.hibernate.Criteria;
46 import org.hibernate.Session;
47 import org.hibernate.SessionFactory;
48 import org.hibernate.Transaction;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.MockitoAnnotations;
54 import org.onap.portalapp.widget.domain.WidgetCatalog;
55 import org.onap.portalapp.widget.domain.WidgetFile;
56 import org.onap.portalapp.widget.excetpion.StorageException;
57 import org.onap.portalapp.widget.service.WidgetCatalogService;
58 import org.springframework.mock.web.MockMultipartFile;
59 import org.springframework.web.multipart.MultipartFile;
60
61 public class StorageServiceImplTest {
62         
63         @InjectMocks
64         StorageServiceImpl storageServiceImpl;
65
66
67         @Mock
68         WidgetCatalogService widgetCatalogService;
69         
70         @Mock
71         Session session;
72         @Mock
73         SessionFactory sessionFactory;
74         
75         @Mock
76         Session currentSession;
77         @Mock
78         Transaction transaction;
79         @Mock
80         Criteria criteria;
81         
82         @Mock
83         Criteria widgetCriteria;
84         @Mock
85         MultipartFile file;
86         
87         
88         @Before
89         public void init() {
90             MockitoAnnotations.initMocks(this);
91         }
92         
93         @Test
94         public void testDeleteWidgetFile() {
95                 List<WidgetFile> widgetFiles=new ArrayList<>();
96                 WidgetFile file=new WidgetFile();
97                 widgetFiles.add(file);
98                 when(sessionFactory.openSession()).thenReturn(session);
99                 when(session.createCriteria(WidgetFile.class)).thenReturn(criteria);
100                 when(criteria.list()).thenReturn(widgetFiles);
101                 when(sessionFactory.getCurrentSession()).thenReturn(currentSession);
102                 when(currentSession.beginTransaction()).thenReturn(transaction);
103                 storageServiceImpl.deleteWidgetFile(2l);
104         }
105         
106         @Test
107         public void testGetWidgetFile() {
108                 List<WidgetFile> widgetFiles=new ArrayList<>();
109                 WidgetFile file=new WidgetFile();
110                 widgetFiles.add(file);
111                 when(sessionFactory.openSession()).thenReturn(session);
112                 when(session.createCriteria(WidgetFile.class)).thenReturn(criteria);
113                 when(criteria.list()).thenReturn(widgetFiles);
114                 storageServiceImpl.getWidgetFile(2l);
115                 
116         }
117         
118         @Test
119 public void testcheckZipFileInvalid() {
120                 MockMultipartFile mockMultipartFile = new MockMultipartFile(
121                             "fileData",
122                             "test.zip",
123                             "text/plain",
124                             "test".getBytes());
125         
126                 storageServiceImpl.checkZipFile(mockMultipartFile);
127         }
128         
129         @Test(expected=StorageException.class)
130         public void testcheckZipEmptyFile() {
131                 when(file.getOriginalFilename()).thenReturn("test.zip");
132                 when(file.isEmpty()).thenReturn(true);
133                 storageServiceImpl.checkZipFile(file);  
134                 
135         }
136         
137         @Test(expected=NullPointerException.class)
138         public void testUpdate() {
139                 
140                 MockMultipartFile mockMultipartFile = new MockMultipartFile(
141                             "fileData",
142                             "test.zip",
143                             "text/plain",
144                             "test".getBytes());
145                 WidgetCatalog catalog=new WidgetCatalog();
146                 catalog.setServiceId(2l);
147                 List<WidgetFile> widgetFiles=new ArrayList<>();
148                 WidgetFile file=new WidgetFile();
149                 widgetFiles.add(file);
150                 when(sessionFactory.openSession()).thenReturn(session);
151                 when(session.createCriteria(WidgetFile.class)).thenReturn(criteria);
152                 when(criteria.list()).thenReturn(widgetFiles);
153                 storageServiceImpl.update(mockMultipartFile, catalog, 2l);
154                 
155         
156                 
157         }
158         
159         @Test
160         public void testGetWidgetCatalogContent()throws Exception {
161                 WidgetCatalog catalog=new WidgetCatalog();
162                 catalog.setServiceId(2l);
163                 catalog.setName("test");
164                 List<WidgetFile> widgetFiles=new ArrayList<>();
165                 WidgetFile file=new WidgetFile();
166                 file.setCss("test".getBytes());
167                 file.setController("test function() Test".getBytes());
168                 file.setMarkup("Test".getBytes());
169                 widgetFiles.add(file);
170         
171                 when(sessionFactory.getCurrentSession()).thenReturn(currentSession);
172                 when(currentSession.beginTransaction()).thenReturn(transaction);
173                 when(currentSession.createCriteria(WidgetFile.class)).thenReturn(criteria,criteria,criteria);
174                 when(criteria.list()).thenReturn(widgetFiles);
175         //      when(currentSession.createCriteria(WidgetFile.class)).thenReturn(criteria);
176         //      when(criteria.list()).thenReturn(widgetFiles);
177                 when(widgetCatalogService.getWidgetCatalog(2l)).thenReturn(catalog);
178                 storageServiceImpl.getWidgetCatalogContent(2l);
179                 
180         }
181         
182         
183 }