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.DesignType;
 
  33 import org.onap.datalake.feeder.domain.Portal;
 
  34 import org.onap.datalake.feeder.domain.PortalDesign;
 
  35 import org.onap.datalake.feeder.domain.Topic;
 
  36 import org.onap.datalake.feeder.domain.TopicName;
 
  37 import org.onap.datalake.feeder.dto.PortalDesignConfig;
 
  38 import org.onap.datalake.feeder.repository.DesignTypeRepository;
 
  39 import org.onap.datalake.feeder.repository.PortalDesignRepository;
 
  40 import org.onap.datalake.feeder.service.PortalDesignService;
 
  41 import org.onap.datalake.feeder.service.TopicService;
 
  42 import org.springframework.validation.BindingResult;
 
  44 import javax.servlet.http.HttpServletResponse;
 
  46 import java.io.IOException;
 
  47 import java.lang.reflect.Field;
 
  48 import java.util.ArrayList;
 
  49 import java.util.List;
 
  50 import java.util.Optional;
 
  52 import static org.junit.Assert.*;
 
  53 import static org.mockito.Mockito.when;
 
  55 @RunWith(MockitoJUnitRunner.class)
 
  56 public class PortalDesignControllerTest {
 
  58     //static String Kibana_Dashboard_Import_Api = "/api/kibana/dashboards/import?exclude=index-pattern";
 
  61     private HttpServletResponse httpServletResponse;
 
  64     private BindingResult mockBindingResult;
 
  67     private ApplicationConfiguration applicationConfiguration;
 
  70     private PortalDesignRepository portalDesignRepository;
 
  73     private TopicService topicService;
 
  76     private DesignTypeRepository designTypeRepository;
 
  79     private PortalDesignService portalDesignService;
 
  83     public void setupTest() {
 
  84         MockitoAnnotations.initMocks(this);
 
  85         when(mockBindingResult.hasErrors()).thenReturn(false);
 
  89     public void testCreatePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
 
  91         PortalDesignController testPortalDesignController = new PortalDesignController();
 
  92         setAccessPrivateFields(testPortalDesignController);
 
  93         PortalDesign testPortalDesign = fillDomain();
 
  94         when(topicService.getTopic("unauthenticated.SEC_FAULT_OUTPUT")).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));
 
  95 //        when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testPortalDesign.getDesignType()));
 
  96         PostReturnBody<PortalDesignConfig> postPortal = testPortalDesignController.createPortalDesign(testPortalDesign.getPortalDesignConfig(), mockBindingResult, httpServletResponse);
 
  97         //assertEquals(postPortal.getStatusCode(), 200);
 
  98         assertNull(postPortal);
 
 102     public void testUpdatePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
 
 104         PortalDesignController testPortalDesignController = new PortalDesignController();
 
 105         setAccessPrivateFields(testPortalDesignController);
 
 106         PortalDesign testPortalDesign = fillDomain();
 
 108         when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
 
 109         when(topicService.getTopic("unauthenticated.SEC_FAULT_OUTPUT")).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));
 
 110  //       when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testPortalDesign.getDesignType()));
 
 111         PostReturnBody<PortalDesignConfig> postPortal = testPortalDesignController.updatePortalDesign(testPortalDesign.getPortalDesignConfig(), mockBindingResult, id, httpServletResponse);
 
 112         //assertEquals(postPortal.getStatusCode(), 200);
 
 113         assertNull(postPortal);
 
 117     public void testDeletePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
 
 119         PortalDesignController testPortalDesignController = new PortalDesignController();
 
 120         setAccessPrivateFields(testPortalDesignController);
 
 121         PortalDesign testPortalDesign = fillDomain();
 
 123         testPortalDesign.setId(1);
 
 124         when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
 
 125         testPortalDesignController.deletePortalDesign(id, httpServletResponse);
 
 129     public void testQueryAllPortalDesign() throws NoSuchFieldException, IllegalAccessException {
 
 131         PortalDesignController testPortalDesignController = new PortalDesignController();
 
 132         setAccessPrivateFields(testPortalDesignController);
 
 133         PortalDesign testPortalDesign = fillDomain();
 
 134         List<PortalDesign> portalDesignList = new ArrayList<>();
 
 135         portalDesignList.add(testPortalDesign);
 
 136         when(portalDesignRepository.findAll()).thenReturn(portalDesignList);
 
 137         assertEquals(1, testPortalDesignController.queryAllPortalDesign().size());
 
 141     public void testDeployPortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
 
 143         PortalDesignController testPortalDesignController = new PortalDesignController();
 
 144         setAccessPrivateFields(testPortalDesignController);
 
 145         PortalDesign testPortalDesign = fillDomain();
 
 147         testPortalDesign.setId(1);
 
 148         //when(applicationConfiguration.getKibanaDashboardImportApi()).thenReturn(Kibana_Dashboard_Import_Api);
 
 149         when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
 
 150         testPortalDesignController.deployPortalDesign(id, httpServletResponse);
 
 153     public void setAccessPrivateFields(PortalDesignController portalDesignController) throws NoSuchFieldException, IllegalAccessException {
 
 155         Field testPortalDesignService = portalDesignController.getClass().getDeclaredField("portalDesignService");
 
 156         testPortalDesignService.setAccessible(true);
 
 157         testPortalDesignService.set(portalDesignController, portalDesignService);
 
 158         Field testPortalDesignRepository = portalDesignController.getClass().getDeclaredField("portalDesignRepository");
 
 159         testPortalDesignRepository.setAccessible(true);
 
 160         testPortalDesignRepository.set(portalDesignController, portalDesignRepository);
 
 164     public PortalDesign fillDomain(){
 
 165         PortalDesign portalDesign = new PortalDesign();
 
 166         portalDesign.setName("Kibana");
 
 167         portalDesign.setBody("jsonString");
 
 168         portalDesign.setSubmitted(false);
 
 169         portalDesign.setNote("test");
 
 170         DesignType designType = new DesignType();
 
 171         designType.setName("Kibana Dashboard");
 
 172         Portal portal = new Portal();
 
 173         portal.setName("Kibana");
 
 174         portal.setHost("127.0.0.1");
 
 175         portal.setPort(5601);
 
 176         designType.setPortal(portal);
 
 177         portalDesign.setDesignType(designType);
 
 178         portalDesign.setTopicName(new TopicName("unauthenticated.SEC_FAULT_OUTPUT"));