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 {
58 private HttpServletResponse httpServletResponse;
61 private BindingResult mockBindingResult;
64 private ApplicationConfiguration applicationConfiguration;
67 private PortalDesignRepository portalDesignRepository;
70 private TopicService topicService;
73 private DesignTypeRepository designTypeRepository;
76 private PortalDesignService portalDesignService;
80 public void setupTest() {
81 MockitoAnnotations.initMocks(this);
82 when(mockBindingResult.hasErrors()).thenReturn(false);
86 public void testCreatePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
88 PortalDesignController testPortalDesignController = new PortalDesignController();
89 setAccessPrivateFields(testPortalDesignController);
90 PortalDesign testPortalDesign = fillDomain();
91 when(topicService.getTopic("unauthenticated.SEC_FAULT_OUTPUT")).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));
92 when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testPortalDesign.getDesignType()));
93 PostReturnBody<PortalDesignConfig> postPortal = testPortalDesignController.createPortalDesign(testPortalDesign.getPortalDesignConfig(), mockBindingResult, httpServletResponse);
94 assertEquals(postPortal.getStatusCode(), 200);
98 public void testUpdatePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
100 PortalDesignController testPortalDesignController = new PortalDesignController();
101 setAccessPrivateFields(testPortalDesignController);
102 PortalDesign testPortalDesign = fillDomain();
104 when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
105 when(topicService.getTopic("unauthenticated.SEC_FAULT_OUTPUT")).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));
106 when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testPortalDesign.getDesignType()));
107 PostReturnBody<PortalDesignConfig> postPortal = testPortalDesignController.updatePortalDesign(testPortalDesign.getPortalDesignConfig(), mockBindingResult, id, httpServletResponse);
108 assertEquals(postPortal.getStatusCode(), 200);
112 public void testDeletePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
114 PortalDesignController testPortalDesignController = new PortalDesignController();
115 setAccessPrivateFields(testPortalDesignController);
116 PortalDesign testPortalDesign = fillDomain();
118 testPortalDesign.setId(1);
119 when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
120 testPortalDesignController.deletePortalDesign(id, httpServletResponse);
124 public void testQueryAllPortalDesign() throws NoSuchFieldException, IllegalAccessException {
126 PortalDesignController testPortalDesignController = new PortalDesignController();
127 setAccessPrivateFields(testPortalDesignController);
128 PortalDesign testPortalDesign = fillDomain();
129 List<PortalDesign> portalDesignList = new ArrayList<>();
130 portalDesignList.add(testPortalDesign);
131 when(portalDesignRepository.findAll()).thenReturn(portalDesignList);
132 assertEquals(1, testPortalDesignController.queryAllPortalDesign().size());
136 public void testDeployPortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
138 PortalDesignController testPortalDesignController = new PortalDesignController();
139 setAccessPrivateFields(testPortalDesignController);
140 PortalDesign testPortalDesign = fillDomain();
142 testPortalDesign.setId(1);
143 when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
144 when(applicationConfiguration.getKibanaDashboardImportApi()).thenReturn("/api/kibana/dashboards/import?exclude=index-pattern");
145 testPortalDesignController.deployPortalDesign(id, httpServletResponse);
148 public void setAccessPrivateFields(PortalDesignController portalDesignController) throws NoSuchFieldException, IllegalAccessException {
150 Field testPortalDesignService = portalDesignController.getClass().getDeclaredField("portalDesignService");
151 testPortalDesignService.setAccessible(true);
152 testPortalDesignService.set(portalDesignController, portalDesignService);
153 Field testPortalDesignRepository = portalDesignController.getClass().getDeclaredField("portalDesignRepository");
154 testPortalDesignRepository.setAccessible(true);
155 testPortalDesignRepository.set(portalDesignController, portalDesignRepository);
159 public PortalDesign fillDomain(){
160 PortalDesign portalDesign = new PortalDesign();
161 portalDesign.setName("Kibana");
162 portalDesign.setBody("jsonString");
163 portalDesign.setSubmitted(false);
164 portalDesign.setNote("test");
165 DesignType designType = new DesignType();
166 designType.setName("Kibana Dashboard");
167 Portal portal = new Portal();
168 portal.setName("Kibana");
169 portal.setHost("127.0.0.1");
170 portal.setPort(5601);
171 designType.setPortal(portal);
172 portalDesign.setDesignType(designType);
173 portalDesign.setTopic(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));