4b933beefbbd1857b90f39993f268d24ef15de87
[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.*;
33 import org.onap.datalake.feeder.domain.Design;
34 import org.onap.datalake.feeder.dto.DesignConfig;
35 import org.onap.datalake.feeder.repository.DesignTypeRepository;
36 import org.onap.datalake.feeder.repository.DesignRepository;
37 import org.onap.datalake.feeder.service.DesignService;
38 import org.onap.datalake.feeder.service.TopicService;
39 import org.springframework.validation.BindingResult;
40
41 import javax.servlet.http.HttpServletResponse;
42
43 import java.io.IOException;
44 import java.lang.reflect.Field;
45 import java.util.ArrayList;
46 import java.util.List;
47 import java.util.Optional;
48
49 import static org.junit.Assert.*;
50 import static org.mockito.Mockito.when;
51
52 @RunWith(MockitoJUnitRunner.class)
53 public class DesignControllerTest {
54   
55     //static String Kibana_Dashboard_Import_Api = "/api/kibana/dashboards/import?exclude=index-pattern";
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 DesignRepository designRepository;
68
69     @Mock
70     private TopicService topicService;
71
72     @Mock
73     private DesignTypeRepository designTypeRepository;
74
75     @InjectMocks
76     private DesignService designService;
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 testCreateDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
87
88         DesignController testDesignController = new DesignController();
89         setAccessPrivateFields(testDesignController);
90         Design testDesign = fillDomain();
91         //when(topicService.getTopic(0)).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));
92 //        when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testDesign.getDesignType()));
93         PostReturnBody<DesignConfig> postPortal = testDesignController.createDesign(testDesign.getDesignConfig(), mockBindingResult, httpServletResponse);
94         //assertEquals(postPortal.getStatusCode(), 200);
95         assertNull(postPortal);
96     }
97
98     @Test
99     public void testUpdateDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
100
101         DesignController testDesignController = new DesignController();
102         setAccessPrivateFields(testDesignController);
103         Design testDesign = fillDomain();
104         Integer id = 1;
105         when(designRepository.findById(id)).thenReturn((Optional.of(testDesign)));
106         //when(topicService.getTopic(0)).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));
107  //       when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testDesign.getDesignType()));
108         PostReturnBody<DesignConfig> postPortal = testDesignController.updateDesign(testDesign.getDesignConfig(), mockBindingResult, id, httpServletResponse);
109         //assertEquals(postPortal.getStatusCode(), 200);
110         assertNull(postPortal);
111     }
112
113     @Test
114     public void testDeleteDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
115
116         DesignController testDesignController = new DesignController();
117         setAccessPrivateFields(testDesignController);
118         Design testDesign = fillDomain();
119         Integer id = 1;
120         testDesign.setId(1);
121         when(designRepository.findById(id)).thenReturn((Optional.of(testDesign)));
122         testDesignController.deleteDesign(id, httpServletResponse);
123     }
124
125     @Test
126     public void testQueryAllDesign() throws NoSuchFieldException, IllegalAccessException {
127
128         DesignController testDesignController = new DesignController();
129         setAccessPrivateFields(testDesignController);
130         Design testDesign = fillDomain();
131         List<Design> designList = new ArrayList<>();
132         designList.add(testDesign);
133         when(designRepository.findAll()).thenReturn(designList);
134         assertEquals(1, testDesignController.queryAllDesign().size());
135     }
136
137     @Test(expected = NullPointerException.class)
138     public void testDeployDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
139
140         DesignController testDesignController = new DesignController();
141         setAccessPrivateFields(testDesignController);
142         Design testDesign = fillDomain();
143         Integer id = 1;
144         testDesign.setId(1);
145         //when(applicationConfiguration.getKibanaDashboardImportApi()).thenReturn(Kibana_Dashboard_Import_Api);
146         when(designRepository.findById(id)).thenReturn((Optional.of(testDesign)));
147         testDesignController.deployDesign(id, httpServletResponse);
148     }
149
150     public void setAccessPrivateFields(DesignController designController) throws NoSuchFieldException, IllegalAccessException {
151
152         Field testPortalDesignService = designController.getClass().getDeclaredField("designService");
153         testPortalDesignService.setAccessible(true);
154         testPortalDesignService.set(designController, designService);
155         Field testPortalDesignRepository = designController.getClass().getDeclaredField("designRepository");
156         testPortalDesignRepository.setAccessible(true);
157         testPortalDesignRepository.set(designController, designRepository);
158     }
159
160
161     public Design fillDomain(){
162         Design design = new Design();
163         design.setName("Kibana");
164         design.setBody("jsonString");
165         design.setSubmitted(false);
166         design.setNote("test");
167         DesignType designType = new DesignType();
168         designType.setName("Kibana Dashboard");
169         design.setDesignType(designType);
170         design.setTopicName(new TopicName("unauthenticated.SEC_FAULT_OUTPUT"));
171         return design;
172     }
173 }