supports multiple Kafka clusters and DBs
[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.domain.TopicName;
37 import org.onap.datalake.feeder.dto.PortalDesignConfig;
38 import org.onap.datalake.feeder.repository.DesignTypeRepository;
39 import org.onap.datalake.feeder.repository.PortalDesignRepository;
40 import org.onap.datalake.feeder.service.PortalDesignService;
41 import org.onap.datalake.feeder.service.TopicService;
42 import org.springframework.validation.BindingResult;
43
44 import javax.servlet.http.HttpServletResponse;
45
46 import java.io.IOException;
47 import java.lang.reflect.Field;
48 import java.util.ArrayList;
49 import java.util.List;
50 import java.util.Optional;
51
52 import static org.junit.Assert.*;
53 import static org.mockito.Mockito.when;
54
55 @RunWith(MockitoJUnitRunner.class)
56 public class PortalDesignControllerTest {
57   
58     //static String Kibana_Dashboard_Import_Api = "/api/kibana/dashboards/import?exclude=index-pattern";
59
60     @Mock
61     private HttpServletResponse httpServletResponse;
62
63     @Mock
64     private BindingResult mockBindingResult;
65
66     @Mock
67     private ApplicationConfiguration applicationConfiguration;
68
69     @Mock
70     private PortalDesignRepository portalDesignRepository;
71
72     @Mock
73     private TopicService topicService;
74
75     @Mock
76     private DesignTypeRepository designTypeRepository;
77
78     @InjectMocks
79     private PortalDesignService portalDesignService;
80
81
82     @Before
83     public void setupTest() {
84         MockitoAnnotations.initMocks(this);
85         when(mockBindingResult.hasErrors()).thenReturn(false);
86     }
87
88     @Test
89     public void testCreatePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
90
91         PortalDesignController testPortalDesignController = new PortalDesignController();
92         setAccessPrivateFields(testPortalDesignController);
93         PortalDesign testPortalDesign = fillDomain();
94         when(topicService.getTopic("unauthenticated.SEC_FAULT_OUTPUT")).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));
95 //        when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testPortalDesign.getDesignType()));
96         PostReturnBody<PortalDesignConfig> postPortal = testPortalDesignController.createPortalDesign(testPortalDesign.getPortalDesignConfig(), mockBindingResult, httpServletResponse);
97         //assertEquals(postPortal.getStatusCode(), 200);
98         assertNull(postPortal);
99     }
100
101     @Test
102     public void testUpdatePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
103
104         PortalDesignController testPortalDesignController = new PortalDesignController();
105         setAccessPrivateFields(testPortalDesignController);
106         PortalDesign testPortalDesign = fillDomain();
107         Integer id = 1;
108         when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
109         when(topicService.getTopic("unauthenticated.SEC_FAULT_OUTPUT")).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));
110  //       when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testPortalDesign.getDesignType()));
111         PostReturnBody<PortalDesignConfig> postPortal = testPortalDesignController.updatePortalDesign(testPortalDesign.getPortalDesignConfig(), mockBindingResult, id, httpServletResponse);
112         //assertEquals(postPortal.getStatusCode(), 200);
113         assertNull(postPortal);
114     }
115
116     @Test
117     public void testDeletePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
118
119         PortalDesignController testPortalDesignController = new PortalDesignController();
120         setAccessPrivateFields(testPortalDesignController);
121         PortalDesign testPortalDesign = fillDomain();
122         Integer id = 1;
123         testPortalDesign.setId(1);
124         when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
125         testPortalDesignController.deletePortalDesign(id, httpServletResponse);
126     }
127
128     @Test
129     public void testQueryAllPortalDesign() throws NoSuchFieldException, IllegalAccessException {
130
131         PortalDesignController testPortalDesignController = new PortalDesignController();
132         setAccessPrivateFields(testPortalDesignController);
133         PortalDesign testPortalDesign = fillDomain();
134         List<PortalDesign> portalDesignList = new ArrayList<>();
135         portalDesignList.add(testPortalDesign);
136         when(portalDesignRepository.findAll()).thenReturn(portalDesignList);
137         assertEquals(1, testPortalDesignController.queryAllPortalDesign().size());
138     }
139
140     @Test
141     public void testDeployPortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
142
143         PortalDesignController testPortalDesignController = new PortalDesignController();
144         setAccessPrivateFields(testPortalDesignController);
145         PortalDesign testPortalDesign = fillDomain();
146         Integer id = 1;
147         testPortalDesign.setId(1);
148         //when(applicationConfiguration.getKibanaDashboardImportApi()).thenReturn(Kibana_Dashboard_Import_Api);
149         when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
150         testPortalDesignController.deployPortalDesign(id, httpServletResponse);
151     }
152
153     public void setAccessPrivateFields(PortalDesignController portalDesignController) throws NoSuchFieldException, IllegalAccessException {
154
155         Field testPortalDesignService = portalDesignController.getClass().getDeclaredField("portalDesignService");
156         testPortalDesignService.setAccessible(true);
157         testPortalDesignService.set(portalDesignController, portalDesignService);
158         Field testPortalDesignRepository = portalDesignController.getClass().getDeclaredField("portalDesignRepository");
159         testPortalDesignRepository.setAccessible(true);
160         testPortalDesignRepository.set(portalDesignController, portalDesignRepository);
161     }
162
163
164     public PortalDesign fillDomain(){
165         PortalDesign portalDesign = new PortalDesign();
166         portalDesign.setName("Kibana");
167         portalDesign.setBody("jsonString");
168         portalDesign.setSubmitted(false);
169         portalDesign.setNote("test");
170         DesignType designType = new DesignType();
171         designType.setName("Kibana Dashboard");
172         Portal portal = new Portal();
173         portal.setName("Kibana");
174         portal.setHost("127.0.0.1");
175         portal.setPort(5601);
176         designType.setPortal(portal);
177         portalDesign.setDesignType(designType);
178         portalDesign.setTopicName(new TopicName("unauthenticated.SEC_FAULT_OUTPUT"));
179         return  portalDesign;
180     }
181 }