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;
49 import static org.junit.Assert.assertEquals;
50 import static org.junit.Assert.assertNotEquals;
51 import static org.junit.Assert.assertNotNull;
52 import static org.mockito.Mockito.when;
54 @RunWith(MockitoJUnitRunner.class)
55 public class DbControllerTest {
58 private HttpServletResponse httpServletResponse;
61 private DbRepository dbRepository;
64 private BindingResult mockBindingResult;
67 private DbService dbService1;
69 public DbConfig getDbConfig() {
70 DbConfig dbConfig = new DbConfig();
71 dbConfig.setName("Elecsticsearch");
72 dbConfig.setHost("localhost");
73 dbConfig.setLogin("root");
74 dbConfig.setPassword("root123");
75 dbConfig.setDatabase("Elecsticsearch");
76 dbConfig.setPort(123);
77 dbConfig.setPoperties("driver");
81 public void setAccessPrivateFields(DbController dbController) throws NoSuchFieldException,
82 IllegalAccessException {
83 // Field dbService = dbController.getClass().getDeclaredField("dbService");
84 // dbService.setAccessible(true);
85 // dbService.set(dbController, dbService1);
86 Field dbRepository1 = dbController.getClass().getDeclaredField("dbRepository");
87 dbRepository1.setAccessible(true);
88 dbRepository1.set(dbController, dbRepository);
92 public void setupTest() {
93 MockitoAnnotations.initMocks(this);
94 // While the default boolean return value for a mock is 'false',
95 // it's good to be explicit anyway:
96 when(mockBindingResult.hasErrors()).thenReturn(false);
100 public void testCreateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
101 DbController dbController = new DbController();
102 DbConfig dbConfig = getDbConfig();
103 setAccessPrivateFields(dbController);
104 PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
105 assertEquals(200, db.getStatusCode());
106 when(mockBindingResult.hasErrors()).thenReturn(true);
107 db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
108 assertEquals(null, db);
112 public void testUpdateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
113 DbController dbController = new DbController();
114 DbConfig dbConfig = getDbConfig();
115 when(mockBindingResult.hasErrors()).thenReturn(true);
116 PostReturnBody<DbConfig> db = dbController.updateDb(dbConfig, mockBindingResult,
117 httpServletResponse);
118 assertEquals(null, db);
119 //when(mockBindingResult.hasErrors()).thenReturn(false);
120 setAccessPrivateFields(dbController);
121 //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
122 assertEquals(null, db);
123 //when(mockBindingResult.hasErrors()).thenReturn(false);
124 String name = "Elecsticsearch";
125 when(dbRepository.findByName(name)).thenReturn(TestUtil.newDb(name));
126 //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
127 //assertEquals(200, db.getStatusCode());
128 Db elecsticsearch = dbController.getDb("Elecsticsearch", httpServletResponse);
129 assertNotNull(elecsticsearch);
133 public void testGetAllDbs() throws IOException, IllegalAccessException, NoSuchFieldException {
134 DbController dbController = new DbController();
135 String name = "Elecsticsearch";
136 List<Db> dbs = new ArrayList<>();
137 dbs.add(TestUtil.newDb(name));
138 setAccessPrivateFields(dbController);
139 when(dbRepository.findAll()).thenReturn(dbs);
140 List<String> list = dbController.list();
141 for (String dbName : list) {
142 assertEquals("Elecsticsearch", dbName);
144 dbController.deleteDb("Elecsticsearch", httpServletResponse);
149 public void testDeleteDb() throws IOException, IllegalAccessException, NoSuchFieldException {
150 DbController dbController = new DbController();
151 String dbName = "Elecsticsearch";
152 String topicName = "a";
153 Topic topic = TestUtil.newTopic(topicName);
154 topic.setEnabled(true);
156 Set<Topic> topics = new HashSet<>();
158 Db db1 = TestUtil.newDb(dbName);
159 db1.setTopics(topics);
160 setAccessPrivateFields(dbController);
161 Set<Topic> elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
162 assertEquals(null, elecsticsearch);
163 when(dbRepository.findByName(dbName)).thenReturn(db1);
164 elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
165 for (Topic anElecsticsearch : elecsticsearch) {
166 Topic tmp = TestUtil.newTopic(topicName);
168 assertNotEquals(tmp, anElecsticsearch);
170 dbController.deleteDb(dbName, httpServletResponse);
174 public void testPostReturnBody() throws IOException, NoSuchFieldException, IllegalAccessException {
175 DbController dbController = new DbController();
176 DbConfig dbConfig = getDbConfig();
177 setAccessPrivateFields(dbController);
178 String name = "Elecsticsearch";
179 //when(dbRepository.findByName(name)).thenReturn(newDb(name));
180 PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
185 public void testVerifyConnection() throws IOException {
186 DbController dbController = new DbController();
187 DbConfig dbConfig = getDbConfig();
188 PostReturnBody<DbConfig> dbConfigPostReturnBody = dbController.verifyDbConnection(dbConfig, httpServletResponse);
189 assertEquals(null, dbConfigPostReturnBody);