2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright 2019 China Mobile
 
   6  *=================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *     http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.datalake.feeder.controller;
 
  23 import org.junit.Before;
 
  24 import org.junit.Test;
 
  25 import org.junit.runner.RunWith;
 
  26 import org.mockito.InjectMocks;
 
  27 import org.mockito.Mock;
 
  28 import org.mockito.MockitoAnnotations;
 
  29 import org.mockito.junit.MockitoJUnitRunner;
 
  30 import org.onap.datalake.feeder.config.ApplicationConfiguration;
 
  31 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
 
  32 import org.onap.datalake.feeder.domain.*;
 
  33 import org.onap.datalake.feeder.domain.Design;
 
  34 import org.onap.datalake.feeder.dto.DesignConfig;
 
  35 import org.onap.datalake.feeder.repository.DesignTypeRepository;
 
  36 import org.onap.datalake.feeder.repository.DesignRepository;
 
  37 import org.onap.datalake.feeder.service.DesignService;
 
  38 import org.onap.datalake.feeder.service.TopicService;
 
  39 import org.springframework.validation.BindingResult;
 
  41 import javax.servlet.http.HttpServletResponse;
 
  43 import java.io.IOException;
 
  44 import java.lang.reflect.Field;
 
  45 import java.util.ArrayList;
 
  46 import java.util.List;
 
  47 import java.util.Optional;
 
  49 import static org.junit.Assert.*;
 
  50 import static org.mockito.Mockito.when;
 
  52 @RunWith(MockitoJUnitRunner.class)
 
  53 public class DesignControllerTest {
 
  55     //static String Kibana_Dashboard_Import_Api = "/api/kibana/dashboards/import?exclude=index-pattern";
 
  58     private HttpServletResponse httpServletResponse;
 
  61     private BindingResult mockBindingResult;
 
  64     private ApplicationConfiguration applicationConfiguration;
 
  67     private DesignRepository designRepository;
 
  70     private TopicService topicService;
 
  73     private DesignTypeRepository designTypeRepository;
 
  76     private DesignService designService;
 
  80     public void setupTest() {
 
  81         MockitoAnnotations.initMocks(this);
 
  82         when(mockBindingResult.hasErrors()).thenReturn(false);
 
  86     public void testCreateDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
 
  88         DesignController testDesignController = new DesignController();
 
  89         setAccessPrivateFields(testDesignController);
 
  90         Design testDesign = fillDomain();
 
  91         //when(topicService.getTopic(0)).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));
 
  92 //        when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testDesign.getDesignType()));
 
  93         PostReturnBody<DesignConfig> postPortal = testDesignController.createDesign(testDesign.getDesignConfig(), mockBindingResult, httpServletResponse);
 
  94         //assertEquals(postPortal.getStatusCode(), 200);
 
  95         assertNull(postPortal);
 
  99     public void testUpdateDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
 
 101         DesignController testDesignController = new DesignController();
 
 102         setAccessPrivateFields(testDesignController);
 
 103         Design testDesign = fillDomain();
 
 105         when(designRepository.findById(id)).thenReturn((Optional.of(testDesign)));
 
 106         //when(topicService.getTopic(0)).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));
 
 107  //       when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testDesign.getDesignType()));
 
 108         PostReturnBody<DesignConfig> postPortal = testDesignController.updateDesign(testDesign.getDesignConfig(), mockBindingResult, id, httpServletResponse);
 
 109         //assertEquals(postPortal.getStatusCode(), 200);
 
 110         assertNull(postPortal);
 
 114     public void testDeleteDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
 
 116         DesignController testDesignController = new DesignController();
 
 117         setAccessPrivateFields(testDesignController);
 
 118         Design testDesign = fillDomain();
 
 121         when(designRepository.findById(id)).thenReturn((Optional.of(testDesign)));
 
 122         testDesignController.deleteDesign(id, httpServletResponse);
 
 126     public void testQueryAllDesign() throws NoSuchFieldException, IllegalAccessException {
 
 128         DesignController testDesignController = new DesignController();
 
 129         setAccessPrivateFields(testDesignController);
 
 130         Design testDesign = fillDomain();
 
 131         List<Design> designList = new ArrayList<>();
 
 132         designList.add(testDesign);
 
 133         when(designRepository.findAll()).thenReturn(designList);
 
 134         assertEquals(1, testDesignController.queryAllDesign().size());
 
 137     @Test(expected = NullPointerException.class)
 
 138     public void testDeployDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
 
 140         DesignController testDesignController = new DesignController();
 
 141         setAccessPrivateFields(testDesignController);
 
 142         Design testDesign = fillDomain();
 
 145         //when(applicationConfiguration.getKibanaDashboardImportApi()).thenReturn(Kibana_Dashboard_Import_Api);
 
 146         when(designRepository.findById(id)).thenReturn((Optional.of(testDesign)));
 
 147         testDesignController.deployDesign(id, httpServletResponse);
 
 150     public void setAccessPrivateFields(DesignController designController) throws NoSuchFieldException, IllegalAccessException {
 
 152         Field testPortalDesignService = designController.getClass().getDeclaredField("designService");
 
 153         testPortalDesignService.setAccessible(true);
 
 154         testPortalDesignService.set(designController, designService);
 
 155         Field testPortalDesignRepository = designController.getClass().getDeclaredField("designRepository");
 
 156         testPortalDesignRepository.setAccessible(true);
 
 157         testPortalDesignRepository.set(designController, designRepository);
 
 161     public Design fillDomain(){
 
 162         Design design = new Design();
 
 163         design.setName("Kibana");
 
 164         design.setBody("jsonString");
 
 165         design.setSubmitted(false);
 
 166         design.setNote("test");
 
 167         DesignType designType = new DesignType();
 
 168         designType.setName("Kibana Dashboard");
 
 169         design.setDesignType(designType);
 
 170         design.setTopicName(new TopicName("unauthenticated.SEC_FAULT_OUTPUT"));