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.controller.domain.PostReturnBody;
31 import org.onap.datalake.feeder.domain.Db;
32 import org.onap.datalake.feeder.domain.Topic;
33 import org.onap.datalake.feeder.dto.DbConfig;
34 import org.onap.datalake.feeder.repository.DbRepository;
35 import org.onap.datalake.feeder.service.DbService;
36 import org.onap.datalake.feeder.util.TestUtil;
37 import org.springframework.validation.BindingResult;
39 import javax.servlet.http.HttpServletResponse;
40 import java.io.IOException;
41 import java.lang.reflect.Field;
42 import java.util.ArrayList;
43 import java.util.HashSet;
44 import java.util.List;
46 import java.util.Collections;
47 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;
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();
73 dbConfig.setName("Elecsticsearch");
74 dbConfig.setHost("localhost");
75 dbConfig.setLogin("root");
76 dbConfig.setPass("root123");
77 dbConfig.setDatabase("Elecsticsearch");
78 dbConfig.setPort(123);
79 dbConfig.setPoperties("driver");
80 dbConfig.setDbTypeId("ES");
84 public void setAccessPrivateFields(DbController dbController) throws NoSuchFieldException,
85 IllegalAccessException {
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);
99 @Test(expected = NullPointerException.class)
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.getId(), 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";
126 when(dbRepository.findById(testId)).thenReturn(Optional.of(TestUtil.newDb(name)));
127 //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
128 //assertEquals(200, db.getStatusCode());
129 DbConfig elecsticsearch = dbController.getDb(testId, httpServletResponse);
130 assertNotNull(elecsticsearch);
134 public void testGetAllDbs() throws IOException, IllegalAccessException, NoSuchFieldException {
135 DbController dbController = new DbController();
136 String name = "Elecsticsearch";
138 List<Db> dbs = new ArrayList<>();
139 dbs.add(TestUtil.newDb(name));
140 setAccessPrivateFields(dbController);
141 when(dbRepository.findAll()).thenReturn(dbs);
142 List<Integer> list = dbController.list();
143 for (int id : list) {
144 assertNotEquals(1234, id);
146 //dbController.deleteDb("Elecsticsearch", httpServletResponse);
151 public void testDeleteDb() throws IOException, IllegalAccessException, NoSuchFieldException {
152 DbController dbController = new DbController();
153 String dbName = "Elecsticsearch";
154 String topicName = "a";
155 Topic topic = TestUtil.newTopic(topicName);
156 topic.setEnabled(true);
158 Set<Topic> topics = new HashSet<>();
160 Db db1 = TestUtil.newDb(dbName);
161 db1.setTopics(topics);
162 setAccessPrivateFields(dbController);
163 Set<Topic> elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
164 assertEquals(Collections.emptySet(), elecsticsearch);
165 when(dbRepository.findByName(dbName)).thenReturn(db1);
166 elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
167 for (Topic anElecsticsearch : elecsticsearch) {
168 Topic tmp = TestUtil.newTopic(topicName);
170 assertNotEquals(tmp, anElecsticsearch);
172 //dbController.deleteDb(dbName, httpServletResponse);
175 @Test(expected = NullPointerException.class)
176 public void testPostReturnBody() throws IOException, NoSuchFieldException, IllegalAccessException {
177 DbController dbController = new DbController();
178 DbConfig dbConfig = getDbConfig();
179 setAccessPrivateFields(dbController);
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);