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