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.domain.TopicName;
35 import org.onap.datalake.feeder.repository.DbRepository;
36 import org.onap.datalake.feeder.service.DbService;
37 import org.onap.datalake.feeder.util.TestUtil;
38 import org.springframework.validation.BindingResult;
40 import javax.servlet.http.HttpServletResponse;
41 import java.io.IOException;
42 import java.lang.reflect.Field;
43 import java.util.ArrayList;
44 import java.util.HashSet;
45 import java.util.List;
46 import java.util.Optional;
48 import java.util.Collections;
50 import static org.junit.Assert.assertEquals;
51 import static org.junit.Assert.assertNotEquals;
52 import static org.junit.Assert.assertNotNull;
53 import static org.mockito.Mockito.when;
55 @RunWith(MockitoJUnitRunner.class)
56 public class DbControllerTest {
59 private HttpServletResponse httpServletResponse;
62 private DbRepository dbRepository;
65 private BindingResult mockBindingResult;
68 private DbService dbService1;
70 public DbConfig getDbConfig() {
71 DbConfig dbConfig = new DbConfig();
72 dbConfig.setName("Elecsticsearch");
73 dbConfig.setHost("localhost");
74 dbConfig.setLogin("root");
75 dbConfig.setPassword("root123");
76 dbConfig.setDatabase("Elecsticsearch");
77 dbConfig.setPort(123);
78 dbConfig.setPoperties("driver");
82 public void setAccessPrivateFields(DbController dbController) throws NoSuchFieldException,
83 IllegalAccessException {
84 // Field dbService = dbController.getClass().getDeclaredField("dbService");
85 // dbService.setAccessible(true);
86 // dbService.set(dbController, dbService1);
87 Field dbRepository1 = dbController.getClass().getDeclaredField("dbRepository");
88 dbRepository1.setAccessible(true);
89 dbRepository1.set(dbController, dbRepository);
93 public void setupTest() {
94 MockitoAnnotations.initMocks(this);
95 // While the default boolean return value for a mock is 'false',
96 // it's good to be explicit anyway:
97 when(mockBindingResult.hasErrors()).thenReturn(false);
101 public void testCreateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
102 DbController dbController = new DbController();
103 DbConfig dbConfig = getDbConfig();
104 setAccessPrivateFields(dbController);
105 PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
106 assertEquals(200, db.getStatusCode());
107 when(mockBindingResult.hasErrors()).thenReturn(true);
108 db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
109 assertEquals(null, db);
113 public void testUpdateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
114 DbController dbController = new DbController();
115 DbConfig dbConfig = getDbConfig();
116 when(mockBindingResult.hasErrors()).thenReturn(true);
117 PostReturnBody<DbConfig> db = dbController.updateDb(dbConfig, mockBindingResult,
118 httpServletResponse);
119 assertEquals(null, db);
120 //when(mockBindingResult.hasErrors()).thenReturn(false);
121 setAccessPrivateFields(dbController);
122 //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
123 assertEquals(null, db);
124 //when(mockBindingResult.hasErrors()).thenReturn(false);
125 String name = "Elecsticsearch";
126 when(dbRepository.findByName(name)).thenReturn(TestUtil.newDb(name));
127 //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
128 //assertEquals(200, db.getStatusCode());
129 Db elecsticsearch = dbController.getDb("Elecsticsearch", httpServletResponse);
130 assertNotNull(elecsticsearch);
134 public void testGetAllDbs() throws IOException, IllegalAccessException, NoSuchFieldException {
135 DbController dbController = new DbController();
136 String name = "Elecsticsearch";
137 List<Db> dbs = new ArrayList<>();
138 dbs.add(TestUtil.newDb(name));
139 setAccessPrivateFields(dbController);
140 when(dbRepository.findAll()).thenReturn(dbs);
141 List<String> list = dbController.list();
142 for (String dbName : list) {
143 assertEquals("Elecsticsearch", dbName);
145 dbController.deleteDb("Elecsticsearch", httpServletResponse);
150 public void testDeleteDb() throws IOException, IllegalAccessException, NoSuchFieldException {
151 DbController dbController = new DbController();
152 String dbName = "Elecsticsearch";
153 String topicName = "a";
154 Topic topic = TestUtil.newTopic(topicName);
155 topic.setEnabled(true);
157 Set<Topic> topics = new HashSet<>();
159 Db db1 = TestUtil.newDb(dbName);
160 db1.setTopics(topics);
161 setAccessPrivateFields(dbController);
162 Set<Topic> elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
163 assertEquals(Collections.emptySet(), elecsticsearch);
164 when(dbRepository.findByName(dbName)).thenReturn(db1);
165 elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
166 for (Topic anElecsticsearch : elecsticsearch) {
167 Topic tmp = TestUtil.newTopic(topicName);
169 assertNotEquals(tmp, anElecsticsearch);
171 dbController.deleteDb(dbName, httpServletResponse);
175 public void testPostReturnBody() throws IOException, NoSuchFieldException, IllegalAccessException {
176 DbController dbController = new DbController();
177 DbConfig dbConfig = getDbConfig();
178 setAccessPrivateFields(dbController);
179 String name = "Elecsticsearch";
180 //when(dbRepository.findByName(name)).thenReturn(newDb(name));
181 PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
186 public void testVerifyConnection() throws IOException {
187 DbController dbController = new DbController();
188 DbConfig dbConfig = getDbConfig();
189 PostReturnBody<DbConfig> dbConfigPostReturnBody = dbController.verifyDbConnection(dbConfig, httpServletResponse);
190 assertEquals(null, dbConfigPostReturnBody);