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.dto.PortalDesignConfig;
37 import org.onap.datalake.feeder.repository.DesignTypeRepository;
38 import org.onap.datalake.feeder.repository.PortalDesignRepository;
39 import org.onap.datalake.feeder.service.PortalDesignService;
40 import org.onap.datalake.feeder.service.TopicService;
41 import org.springframework.validation.BindingResult;
43 import javax.servlet.http.HttpServletResponse;
45 import java.io.IOException;
46 import java.lang.reflect.Field;
47 import java.util.ArrayList;
48 import java.util.List;
49 import java.util.Optional;
51 import static org.junit.Assert.*;
52 import static org.mockito.Mockito.when;
54 @RunWith(MockitoJUnitRunner.class)
55 public class PortalDesignControllerTest {
57 //static String Kibana_Dashboard_Import_Api = "/api/kibana/dashboards/import?exclude=index-pattern";
60 private HttpServletResponse httpServletResponse;
63 private BindingResult mockBindingResult;
66 private ApplicationConfiguration applicationConfiguration;
69 private PortalDesignRepository portalDesignRepository;
72 private TopicService topicService;
75 private DesignTypeRepository designTypeRepository;
78 private PortalDesignService portalDesignService;
82 public void setupTest() {
83 MockitoAnnotations.initMocks(this);
84 when(mockBindingResult.hasErrors()).thenReturn(false);
88 public void testCreatePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
90 PortalDesignController testPortalDesignController = new PortalDesignController();
91 setAccessPrivateFields(testPortalDesignController);
92 PortalDesign testPortalDesign = fillDomain();
93 when(topicService.getTopic("unauthenticated.SEC_FAULT_OUTPUT")).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));
94 when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testPortalDesign.getDesignType()));
95 PostReturnBody<PortalDesignConfig> postPortal = testPortalDesignController.createPortalDesign(testPortalDesign.getPortalDesignConfig(), mockBindingResult, httpServletResponse);
96 assertEquals(postPortal.getStatusCode(), 200);
100 public void testUpdatePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
102 PortalDesignController testPortalDesignController = new PortalDesignController();
103 setAccessPrivateFields(testPortalDesignController);
104 PortalDesign testPortalDesign = fillDomain();
106 when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
107 when(topicService.getTopic("unauthenticated.SEC_FAULT_OUTPUT")).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));
108 when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testPortalDesign.getDesignType()));
109 PostReturnBody<PortalDesignConfig> postPortal = testPortalDesignController.updatePortalDesign(testPortalDesign.getPortalDesignConfig(), mockBindingResult, id, httpServletResponse);
110 assertEquals(postPortal.getStatusCode(), 200);
114 public void testDeletePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
116 PortalDesignController testPortalDesignController = new PortalDesignController();
117 setAccessPrivateFields(testPortalDesignController);
118 PortalDesign testPortalDesign = fillDomain();
120 testPortalDesign.setId(1);
121 when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
122 testPortalDesignController.deletePortalDesign(id, httpServletResponse);
126 public void testQueryAllPortalDesign() throws NoSuchFieldException, IllegalAccessException {
128 PortalDesignController testPortalDesignController = new PortalDesignController();
129 setAccessPrivateFields(testPortalDesignController);
130 PortalDesign testPortalDesign = fillDomain();
131 List<PortalDesign> portalDesignList = new ArrayList<>();
132 portalDesignList.add(testPortalDesign);
133 when(portalDesignRepository.findAll()).thenReturn(portalDesignList);
134 assertEquals(1, testPortalDesignController.queryAllPortalDesign().size());
138 public void testDeployPortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
140 PortalDesignController testPortalDesignController = new PortalDesignController();
141 setAccessPrivateFields(testPortalDesignController);
142 PortalDesign testPortalDesign = fillDomain();
144 testPortalDesign.setId(1);
145 //when(applicationConfiguration.getKibanaDashboardImportApi()).thenReturn(Kibana_Dashboard_Import_Api);
146 when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
147 testPortalDesignController.deployPortalDesign(id, httpServletResponse);
150 public void setAccessPrivateFields(PortalDesignController portalDesignController) throws NoSuchFieldException, IllegalAccessException {
152 Field testPortalDesignService = portalDesignController.getClass().getDeclaredField("portalDesignService");
153 testPortalDesignService.setAccessible(true);
154 testPortalDesignService.set(portalDesignController, portalDesignService);
155 Field testPortalDesignRepository = portalDesignController.getClass().getDeclaredField("portalDesignRepository");
156 testPortalDesignRepository.setAccessible(true);
157 testPortalDesignRepository.set(portalDesignController, portalDesignRepository);
161 public PortalDesign fillDomain(){
162 PortalDesign portalDesign = new PortalDesign();
163 portalDesign.setName("Kibana");
164 portalDesign.setBody("jsonString");
165 portalDesign.setSubmitted(false);
166 portalDesign.setNote("test");
167 DesignType designType = new DesignType();
168 designType.setName("Kibana Dashboard");
169 Portal portal = new Portal();
170 portal.setName("Kibana");
171 portal.setHost("127.0.0.1");
172 portal.setPort(5601);
173 designType.setPortal(portal);
174 portalDesign.setDesignType(designType);
175 portalDesign.setTopic(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));