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.controller.domain.PostReturnBody;
31 import org.onap.datalake.feeder.domain.Db;
32 import org.onap.datalake.feeder.domain.Portal;
33 import org.onap.datalake.feeder.dto.PortalConfig;
34 import org.onap.datalake.feeder.repository.PortalRepository;
35 import org.onap.datalake.feeder.service.PortalService;
36 import org.springframework.validation.BindingResult;
38 import javax.servlet.http.HttpServletResponse;
40 import java.io.IOException;
41 import java.lang.reflect.Field;
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.util.Optional;
46 import static org.junit.Assert.assertEquals;
47 import static org.mockito.Mockito.when;
49 @RunWith(MockitoJUnitRunner.class)
50 public class PortalControllerTest {
53 private HttpServletResponse httpServletResponse;
56 private BindingResult mockBindingResult;
59 private PortalRepository portalRepository;
62 private PortalService portalService;
65 public void setupTest() {
66 MockitoAnnotations.initMocks(this);
67 when(mockBindingResult.hasErrors()).thenReturn(false);
72 public void testUpdatePortal() throws NoSuchFieldException, IllegalAccessException, IOException {
74 PortalController testPortalController = new PortalController();
75 setAccessPrivateFields(testPortalController);
76 Portal testPortal = fillDomain();
77 when(portalRepository.findById("Kibana")).thenReturn(Optional.of(testPortal));
78 PostReturnBody<PortalConfig> postPortal = testPortalController.updatePortal(testPortal.getPortalConfig(), mockBindingResult, httpServletResponse);
79 assertEquals(postPortal.getStatusCode(), 200);
80 //when(mockBindingResult.hasErrors()).thenReturn(true);
86 public void testGetPortals() throws NoSuchFieldException, IllegalAccessException {
88 PortalController testPortalController = new PortalController();
89 setAccessPrivateFields(testPortalController);
90 Portal testPortal = fillDomain();
91 List<Portal> portalList = new ArrayList<>();
92 portalList.add(testPortal);
93 when(portalRepository.findAll()).thenReturn(portalList);
94 assertEquals(1, testPortalController.getPortals().size());
99 public void setAccessPrivateFields(PortalController portalController) throws NoSuchFieldException, IllegalAccessException {
101 Field testPortalService = portalController.getClass().getDeclaredField("portalService");
102 testPortalService.setAccessible(true);
103 testPortalService.set(portalController, portalService);
104 Field testPortalRepository = portalController.getClass().getDeclaredField("portalRepository");
105 testPortalRepository.setAccessible(true);
106 testPortalRepository.set(portalController, portalRepository);
110 public Portal fillDomain(){
111 Portal portal = new Portal();
112 portal.setName("Kibana");
113 portal.setEnabled(true);
114 portal.setHost("127.0.0.1");
115 portal.setPort(5601);
116 portal.setLogin("admin");
117 portal.setPass("password");
118 portal.setDb(new Db("Elasticsearch"));