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();
72 dbConfig.setName("Elecsticsearch");
73 dbConfig.setHost("localhost");
74 dbConfig.setLogin("root");
75 dbConfig.setPass("root123");
76 dbConfig.setDatabase("Elecsticsearch");
77 dbConfig.setPort(123);
78 dbConfig.setPoperties("driver");
79 dbConfig.setDbTypeId("ES");
83 public void setAccessPrivateFields(DbController dbController) throws NoSuchFieldException,
84 IllegalAccessException {
85 Field dbRepository1 = dbController.getClass().getDeclaredField("dbRepository");
86 dbRepository1.setAccessible(true);
87 dbRepository1.set(dbController, dbRepository);
91 public void setupTest() {
92 MockitoAnnotations.initMocks(this);
93 // While the default boolean return value for a mock is 'false',
94 // it's good to be explicit anyway:
95 when(mockBindingResult.hasErrors()).thenReturn(false);
98 @Test(expected = NullPointerException.class)
99 public void testCreateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
100 DbController dbController = new DbController();
101 DbConfig dbConfig = getDbConfig();
102 setAccessPrivateFields(dbController);
103 PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
104 assertEquals(200, db.getStatusCode());
105 when(mockBindingResult.hasErrors()).thenReturn(true);
106 db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
107 assertEquals(null, db);
111 public void testUpdateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
112 DbController dbController = new DbController();
113 DbConfig dbConfig = getDbConfig();
114 when(mockBindingResult.hasErrors()).thenReturn(true);
115 PostReturnBody<DbConfig> db = dbController.updateDb(dbConfig, mockBindingResult,
116 httpServletResponse);
117 assertEquals(null, db);
118 //when(mockBindingResult.hasErrors()).thenReturn(false);
119 setAccessPrivateFields(dbController);
120 //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
121 assertEquals(null, db);
122 //when(mockBindingResult.hasErrors()).thenReturn(false);
123 String name = "Elecsticsearch";
125 when(dbRepository.findById(testId)).thenReturn(Optional.of(TestUtil.newDb(name)));
126 //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
127 //assertEquals(200, db.getStatusCode());
128 DbConfig elecsticsearch = dbController.getDb(testId, httpServletResponse);
129 assertNotNull(elecsticsearch);
133 public void testGetAllDbs() throws IOException, IllegalAccessException, NoSuchFieldException {
134 DbController dbController = new DbController();
135 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<Integer> list = dbController.list();
142 for (int id : list) {
143 assertNotEquals(1234, id);
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);
174 @Test(expected = NullPointerException.class)
175 public void testPostReturnBody() throws IOException, NoSuchFieldException, IllegalAccessException {
176 DbController dbController = new DbController();
177 DbConfig dbConfig = getDbConfig();
178 setAccessPrivateFields(dbController);
179 PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
184 public void testVerifyConnection() throws IOException {
185 DbController dbController = new DbController();
186 DbConfig dbConfig = getDbConfig();
187 PostReturnBody<DbConfig> dbConfigPostReturnBody = dbController.verifyDbConnection(dbConfig, httpServletResponse);
188 assertEquals(null, dbConfigPostReturnBody);