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=========================================================
20 package org.onap.datalake.feeder.controller;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.onap.datalake.feeder.config.ApplicationConfiguration;
30 import org.onap.datalake.feeder.domain.Db;
31 import org.onap.datalake.feeder.domain.Topic;
32 import org.onap.datalake.feeder.repository.TopicRepository;
33 import org.onap.datalake.feeder.service.DbService;
34 import org.onap.datalake.feeder.service.DmaapService;
35 import org.onap.datalake.feeder.service.TopicService;
36 import org.springframework.validation.BindingResult;
38 import javax.servlet.http.HttpServletResponse;
39 import java.io.IOException;
40 import java.lang.reflect.Field;
41 import java.util.ArrayList;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Optional;
47 import static org.junit.Assert.assertEquals;
48 import static org.mockito.Mockito.when;
50 @RunWith(MockitoJUnitRunner.class)
51 public class TopicControllerTest {
53 static String DEFAULT_TOPIC_NAME = "_DL_DEFAULT_";
56 private HttpServletResponse httpServletResponse;
59 private BindingResult mockBindingResult;
62 private TopicRepository topicRepository;
65 private TopicService topicService1;
68 private ApplicationConfiguration config;
71 private DbService dbService1;
74 private DmaapService dmaapService1;
77 public void setupTest() {
78 MockitoAnnotations.initMocks(this);
79 // While the default boolean return value for a mock is 'false',
80 // it's good to be explicit anyway:
81 when(mockBindingResult.hasErrors()).thenReturn(false);
84 public void setAccessPrivateFields(TopicController topicController) throws NoSuchFieldException,
85 IllegalAccessException {
86 Field topicService = topicController.getClass().getDeclaredField("topicService");
87 topicService.setAccessible(true);
88 topicService.set(topicController, topicService1);
89 Field topicRepository1 = topicController.getClass().getDeclaredField("topicRepository");
90 topicRepository1.setAccessible(true);
91 topicRepository1.set(topicController, topicRepository);
92 Field dbService = topicController.getClass().getDeclaredField("dbService");
93 dbService.setAccessible(true);
94 dbService.set(topicController, dbService1);
98 public void testCreateTopic() throws IOException, NoSuchFieldException, IllegalAccessException {
99 TopicController topicController = new TopicController();
100 setAccessPrivateFields(topicController);
101 when(topicRepository.findById(DEFAULT_TOPIC_NAME)).thenReturn(Optional.of(new Topic(DEFAULT_TOPIC_NAME)));
102 when(config.getDefaultTopicName()).thenReturn(DEFAULT_TOPIC_NAME);
103 Topic topicName = topicController.createTopic(new Topic("a"), mockBindingResult, httpServletResponse);
104 assertEquals(new Topic("a"), topicName);
105 when(mockBindingResult.hasErrors()).thenReturn(true);
106 topicName = topicController.createTopic(new Topic("a"), mockBindingResult, httpServletResponse);
107 assertEquals(null, topicName);
108 when(mockBindingResult.hasErrors()).thenReturn(false);
109 Topic a = new Topic("a");
111 when(topicRepository.findById("a")).thenReturn(Optional.of(a));
112 topicName = topicController.createTopic(new Topic("a"), mockBindingResult, httpServletResponse);
113 assertEquals(null, topicName);
117 public void testUpdateTopic() throws IOException, NoSuchFieldException, IllegalAccessException {
118 TopicController topicController = new TopicController();
119 setAccessPrivateFields(topicController);
120 Topic topicName = topicController.updateTopic(new Topic("a"), mockBindingResult, httpServletResponse);
121 assertEquals(null, topicName);
122 Topic a = new Topic("a");
124 when(topicRepository.findById("a")).thenReturn(Optional.of(a));
125 topicName = topicController.updateTopic(new Topic("a"), mockBindingResult, httpServletResponse);
126 assertEquals(new Topic("a"), topicName);
127 when(mockBindingResult.hasErrors()).thenReturn(true);
128 topicName = topicController.updateTopic(new Topic("a"), mockBindingResult, httpServletResponse);
129 assertEquals(null, topicName);
131 ArrayList<Topic> topics = new ArrayList<>();
133 when(topicRepository.findAll()).thenReturn(topics);
134 Iterable<Topic> list = topicController.list();
135 for (Topic newTopic : list) {
136 assertEquals(a, newTopic);
141 public void testAddDb() throws NoSuchFieldException, IllegalAccessException, IOException {
142 TopicController topicController = new TopicController();
143 setAccessPrivateFields(topicController);
144 String dbName = "Elecsticsearch";
146 Topic topic = new Topic(name);
147 topic.setEnabled(true);
148 Set<Db> dbSet = new HashSet<>();
149 dbSet.add(new Db(dbName));
152 when(topicRepository.findById(name)).thenReturn(Optional.of(topic));
153 topicController.addDb("a", dbName, httpServletResponse);
154 topicController.deleteDb("a", dbName, httpServletResponse);
158 public void testGetTopicDbs() throws NoSuchFieldException, IllegalAccessException, IOException {
159 TopicController topicController = new TopicController();
160 setAccessPrivateFields(topicController);
161 String dbName = "Elecsticsearch";
163 Topic topic = new Topic(name);
164 topic.setEnabled(true);
165 Set<Db> dbSet = new HashSet<>();
166 dbSet.add(new Db(dbName));
169 when(topicRepository.findById(name)).thenReturn(Optional.of(topic));
170 topicController.getTopicDbs("a");
174 public void testListDmaapTopics() throws NoSuchFieldException, IllegalAccessException, IOException {
175 TopicController topicController = new TopicController();
176 Field dmaapService = topicController.getClass().getDeclaredField("dmaapService");
177 dmaapService.setAccessible(true);
178 dmaapService.set(topicController, dmaapService1);
179 ArrayList<String> topics = new ArrayList<>();
181 when(dmaapService1.getTopics()).thenReturn(topics);
182 List<String> strings = topicController.listDmaapTopics();
183 for (String topic : strings) {
184 assertEquals("a", topic);