2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 Huawei. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.datalake.feeder.controller;
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.dto.DbConfig;
31 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
32 import org.onap.datalake.feeder.domain.Db;
33 import org.onap.datalake.feeder.domain.Topic;
34 import org.onap.datalake.feeder.repository.DbRepository;
35 import org.onap.datalake.feeder.service.DbService;
36 import org.springframework.validation.BindingResult;
38 import javax.servlet.http.HttpServletResponse;
39 import java.io.IOException;
40 import java.lang.reflect.Field;
41 import java.util.ArrayList;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Optional;
47 import static org.junit.Assert.assertEquals;
48 import static org.mockito.Mockito.when;
50 @RunWith(MockitoJUnitRunner.class)
51 public class DbControllerTest {
54 private HttpServletResponse httpServletResponse;
57 private DbRepository dbRepository;
60 private BindingResult mockBindingResult;
63 private DbService dbService1;
65 public DbConfig getDbConfig() {
66 DbConfig dbConfig = new DbConfig();
67 dbConfig.setName("Elecsticsearch");
68 dbConfig.setHost("localhost");
69 dbConfig.setLogin("root");
70 dbConfig.setPassword("root123");
71 dbConfig.setDatabase("Elecsticsearch");
72 dbConfig.setPort(123);
73 dbConfig.setPoperties("driver");
77 public void setAccessPrivateFields(DbController dbController) throws NoSuchFieldException,
78 IllegalAccessException {
79 Field dbService = dbController.getClass().getDeclaredField("dbService");
80 dbService.setAccessible(true);
81 dbService.set(dbController, dbService1);
82 Field dbRepository1 = dbController.getClass().getDeclaredField("dbRepository");
83 dbRepository1.setAccessible(true);
84 dbRepository1.set(dbController, dbRepository);
88 public void setupTest() {
89 MockitoAnnotations.initMocks(this);
90 // While the default boolean return value for a mock is 'false',
91 // it's good to be explicit anyway:
92 when(mockBindingResult.hasErrors()).thenReturn(false);
96 public void testCreateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
97 DbController dbController = new DbController();
98 DbConfig dbConfig = getDbConfig();
99 setAccessPrivateFields(dbController);
100 PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
101 assertEquals(200, db.getStatusCode());
102 when(mockBindingResult.hasErrors()).thenReturn(true);
103 db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
104 assertEquals(null, db);
108 public void testUpdateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
109 DbController dbController = new DbController();
110 DbConfig dbConfig = getDbConfig();
111 when(mockBindingResult.hasErrors()).thenReturn(true);
112 PostReturnBody<DbConfig> db = dbController.updateDb(dbConfig, mockBindingResult,
113 httpServletResponse);
114 assertEquals(null, db);
115 when(mockBindingResult.hasErrors()).thenReturn(false);
116 setAccessPrivateFields(dbController);
117 db = dbController.updateDb(dbConfig, mockBindingResult,
118 httpServletResponse);
119 assertEquals(null, db);
120 when(mockBindingResult.hasErrors()).thenReturn(false);
121 String name = "Elecsticsearch";
122 when(dbRepository.findById(name)).thenReturn(Optional.of(new Db(name)));
123 db = dbController.updateDb(dbConfig, mockBindingResult,
124 httpServletResponse);
125 assertEquals(200, db.getStatusCode());
126 Db elecsticsearch = dbController.getDb("Elecsticsearch", httpServletResponse);
127 assertEquals(null, elecsticsearch);
131 public void testGetAllDbs() throws IOException, IllegalAccessException, NoSuchFieldException {
132 DbController dbController = new DbController();
133 String name = "Elecsticsearch";
134 List<Db> dbs = new ArrayList<>();
135 dbs.add(new Db(name));
136 setAccessPrivateFields(dbController);
137 when(dbRepository.findAll()).thenReturn(dbs);
138 List<String> list = dbController.list();
139 for (String dbName : list) {
140 assertEquals("Elecsticsearch", dbName);
142 dbController.deleteDb("Elecsticsearch", httpServletResponse);
147 public void testDeleteDb() throws IOException, IllegalAccessException, NoSuchFieldException {
148 DbController dbController = new DbController();
149 String dbName = "Elecsticsearch";
150 String topicName = "a";
151 Topic topic = new Topic(topicName);
152 topic.setEnabled(true);
153 Set<Topic> topics = new HashSet<>();
155 Db db1 = new Db(dbName);
156 db1.setTopics(topics);
157 setAccessPrivateFields(dbController);
158 Set<Topic> elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
159 assertEquals(null, elecsticsearch);
160 when(dbRepository.findByName(dbName)).thenReturn(db1);
161 elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
162 for (Topic anElecsticsearch : elecsticsearch) {
163 assertEquals(new Topic(topicName), anElecsticsearch);
165 dbController.deleteDb(dbName, httpServletResponse);
169 public void testPostReturnBody() throws IOException, NoSuchFieldException, IllegalAccessException {
170 DbController dbController = new DbController();
171 DbConfig dbConfig = getDbConfig();
172 setAccessPrivateFields(dbController);
173 String name = "Elecsticsearch";
174 when(dbRepository.findById(name)).thenReturn(Optional.of(new Db(name)));
175 PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
176 assertEquals(null, db);
180 public void testVerifyConnection() throws IOException {
181 DbController dbController = new DbController();
182 DbConfig dbConfig = getDbConfig();
183 PostReturnBody<DbConfig> dbConfigPostReturnBody = dbController.verifyDbConnection(dbConfig, httpServletResponse);
184 assertEquals(null, dbConfigPostReturnBody);