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;
 
  42 import java.util.ArrayList;
 
  43 import java.util.List;
 
  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;
 
  61 public class StorageServiceImplTest {
 
  64         StorageServiceImpl storageServiceImpl;
 
  68         WidgetCatalogService widgetCatalogService;
 
  73         SessionFactory sessionFactory;
 
  76         Session currentSession;
 
  78         Transaction transaction;
 
  83         Criteria widgetCriteria;
 
  90             MockitoAnnotations.initMocks(this);
 
  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);
 
 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);
 
 119 public void testcheckZipFileInvalid() {
 
 120                 MockMultipartFile mockMultipartFile = new MockMultipartFile(
 
 126                 storageServiceImpl.checkZipFile(mockMultipartFile);
 
 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);  
 
 137         @Test(expected=NullPointerException.class)
 
 138         public void testUpdate() {
 
 140                 MockMultipartFile mockMultipartFile = new MockMultipartFile(
 
 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);
 
 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);
 
 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);