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;
45 import java.util.Optional;
47 import java.util.Collections;
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.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 dbRepository1 = dbController.getClass().getDeclaredField("dbRepository");
85 dbRepository1.setAccessible(true);
86 dbRepository1.set(dbController, dbRepository);
90 public void setupTest() {
91 MockitoAnnotations.initMocks(this);
92 // While the default boolean return value for a mock is 'false',
93 // it's good to be explicit anyway:
94 when(mockBindingResult.hasErrors()).thenReturn(false);
98 public void testCreateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
99 DbController dbController = new DbController();
100 DbConfig dbConfig = getDbConfig();
101 setAccessPrivateFields(dbController);
102 PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
103 assertEquals(200, db.getStatusCode());
104 when(mockBindingResult.hasErrors()).thenReturn(true);
105 db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
106 assertEquals(null, db);
110 public void testUpdateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
111 DbController dbController = new DbController();
112 DbConfig dbConfig = getDbConfig();
113 when(mockBindingResult.hasErrors()).thenReturn(true);
114 PostReturnBody<DbConfig> db = dbController.updateDb(dbConfig, mockBindingResult,
115 httpServletResponse);
116 assertEquals(null, db);
117 //when(mockBindingResult.hasErrors()).thenReturn(false);
118 setAccessPrivateFields(dbController);
119 //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
120 assertEquals(null, db);
121 //when(mockBindingResult.hasErrors()).thenReturn(false);
122 String name = "Elecsticsearch";
123 when(dbRepository.findByName(name)).thenReturn(TestUtil.newDb(name));
124 //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
125 //assertEquals(200, db.getStatusCode());
126 Db elecsticsearch = dbController.getDb("Elecsticsearch", httpServletResponse);
127 assertNotNull(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(TestUtil.newDb(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 = TestUtil.newTopic(topicName);
152 topic.setEnabled(true);
154 Set<Topic> topics = new HashSet<>();
156 Db db1 = TestUtil.newDb(dbName);
157 db1.setTopics(topics);
158 setAccessPrivateFields(dbController);
159 Set<Topic> elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
160 assertEquals(Collections.emptySet(), elecsticsearch);
161 when(dbRepository.findByName(dbName)).thenReturn(db1);
162 elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
163 for (Topic anElecsticsearch : elecsticsearch) {
164 Topic tmp = TestUtil.newTopic(topicName);
166 assertNotEquals(tmp, anElecsticsearch);
168 dbController.deleteDb(dbName, httpServletResponse);
172 public void testPostReturnBody() throws IOException, NoSuchFieldException, IllegalAccessException {
173 DbController dbController = new DbController();
174 DbConfig dbConfig = getDbConfig();
175 setAccessPrivateFields(dbController);
176 PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
181 public void testVerifyConnection() throws IOException {
182 DbController dbController = new DbController();
183 DbConfig dbConfig = getDbConfig();
184 PostReturnBody<DbConfig> dbConfigPostReturnBody = dbController.verifyDbConnection(dbConfig, httpServletResponse);
185 assertEquals(null, dbConfigPostReturnBody);