Unit test 3
[dcaegen2/services.git] / components / datalake-handler / feeder / src / test / java / org / onap / datalake / feeder / controller / PortalDesignControllerTest.java
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.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;
42
43 import javax.servlet.http.HttpServletResponse;
44
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;
50
51 import static org.junit.Assert.*;
52 import static org.mockito.Mockito.when;
53
54 @RunWith(MockitoJUnitRunner.class)
55 public class PortalDesignControllerTest {
56
57     @Mock
58     private HttpServletResponse httpServletResponse;
59
60     @Mock
61     private BindingResult mockBindingResult;
62
63     @Mock
64     private ApplicationConfiguration applicationConfiguration;
65
66     @Mock
67     private PortalDesignRepository portalDesignRepository;
68
69     @Mock
70     private TopicService topicService;
71
72     @Mock
73     private DesignTypeRepository designTypeRepository;
74
75     @InjectMocks
76     private PortalDesignService portalDesignService;
77
78
79     @Before
80     public void setupTest() {
81         MockitoAnnotations.initMocks(this);
82         when(mockBindingResult.hasErrors()).thenReturn(false);
83     }
84
85     @Test
86     public void testCreatePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
87
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);
95     }
96
97     @Test
98     public void testUpdatePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
99
100         PortalDesignController testPortalDesignController = new PortalDesignController();
101         setAccessPrivateFields(testPortalDesignController);
102         PortalDesign testPortalDesign = fillDomain();
103         Integer id = 1;
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);
109     }
110
111     @Test
112     public void testDeletePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
113
114         PortalDesignController testPortalDesignController = new PortalDesignController();
115         setAccessPrivateFields(testPortalDesignController);
116         PortalDesign testPortalDesign = fillDomain();
117         Integer id = 1;
118         testPortalDesign.setId(1);
119         when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
120         testPortalDesignController.deletePortalDesign(id, httpServletResponse);
121     }
122
123     @Test
124     public void testQueryAllPortalDesign() throws NoSuchFieldException, IllegalAccessException {
125
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());
133     }
134
135     @Test
136     public void testDeployPortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
137
138         PortalDesignController testPortalDesignController = new PortalDesignController();
139         setAccessPrivateFields(testPortalDesignController);
140         PortalDesign testPortalDesign = fillDomain();
141         Integer id = 1;
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);
146     }
147
148     public void setAccessPrivateFields(PortalDesignController portalDesignController) throws NoSuchFieldException, IllegalAccessException {
149
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);
156     }
157
158
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"));
174         return  portalDesign;
175     }
176 }