21327f94151a03d0c6bc84758e71c378cc45449d
[dcaegen2/services.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : DATALAKE
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.datalake.feeder.controller;
22
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;
37
38 import javax.servlet.http.HttpServletResponse;
39
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;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.mockito.Mockito.when;
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class PortalControllerTest {
51
52     @Mock
53     private HttpServletResponse httpServletResponse;
54
55     @Mock
56     private BindingResult mockBindingResult;
57
58     @Mock
59     private PortalRepository portalRepository;
60
61     @InjectMocks
62     private PortalService portalService;
63
64     @Before
65     public void setupTest() {
66         MockitoAnnotations.initMocks(this);
67         when(mockBindingResult.hasErrors()).thenReturn(false);
68     }
69
70
71     @Test
72     public void testUpdatePortal() throws NoSuchFieldException, IllegalAccessException, IOException {
73
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);
81
82     }
83
84
85     @Test
86     public void testGetPortals() throws NoSuchFieldException, IllegalAccessException {
87
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());
95
96     }
97
98
99     public void setAccessPrivateFields(PortalController portalController) throws NoSuchFieldException, IllegalAccessException {
100
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);
107     }
108
109
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"));
119         return  portal;
120     }
121 }