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.controller.domain.PostReturnBody;
31 import org.onap.datalake.feeder.dto.TopicConfig;
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.TopicNameRepository;
36 import org.onap.datalake.feeder.repository.TopicRepository;
37 import org.onap.datalake.feeder.service.DbService;
38 import org.onap.datalake.feeder.service.DmaapService;
39 import org.onap.datalake.feeder.service.TopicService;
40 import org.onap.datalake.feeder.util.TestUtil;
41 import org.springframework.validation.BindingResult;
43 import javax.servlet.http.HttpServletResponse;
44 import java.io.IOException;
45 import java.lang.reflect.Field;
46 import java.util.ArrayList;
47 import java.util.HashSet;
48 import java.util.List;
49 import java.util.Optional;
52 import static org.junit.Assert.assertEquals;
53 import static org.junit.Assert.assertNull;
54 import static org.mockito.Mockito.when;
56 @RunWith(MockitoJUnitRunner.class)
57 public class TopicControllerTest {
59 static String DEFAULT_TOPIC_NAME = "_DL_DEFAULT_";
62 private HttpServletResponse httpServletResponse;
65 private BindingResult mockBindingResult;
68 private TopicRepository topicRepository;
71 private TopicService topicService;
74 private TopicNameRepository topicNameRepository;
77 TopicController topicController;
80 private ApplicationConfiguration config;
83 private DbService dbService;
86 private DmaapService dmaapService;
89 public void setupTest() throws NoSuchFieldException, IllegalAccessException {
90 // While the default boolean return value for a mock is 'false',
91 // it's good to be explicit anyway:
92 when(mockBindingResult.hasErrors()).thenReturn(false);
96 public void testListTopic() throws IOException, NoSuchFieldException, IllegalAccessException {
100 public void testCreateTopic() throws IOException {
101 Topic a = TestUtil.newTopic("a");
105 TopicConfig ac = a.getTopicConfig();
107 when(topicService.fillTopicConfiguration(ac)).thenReturn(a);
108 PostReturnBody<TopicConfig> postTopic = topicController.createTopic(ac, mockBindingResult, httpServletResponse);
109 assertEquals(postTopic.getStatusCode(), 200);
111 when(mockBindingResult.hasErrors()).thenReturn(true);
112 PostReturnBody<TopicConfig> topicConfig = topicController.createTopic(ac, mockBindingResult, httpServletResponse);
113 assertEquals(null, topicConfig);
117 public void testUpdateTopic() throws IOException {
118 Topic a = TestUtil.newTopic("a");
122 TopicConfig ac = a.getTopicConfig();
124 when(topicService.getTopic(1)).thenReturn(a);
125 PostReturnBody<TopicConfig> postConfig1 = topicController.updateTopic(1, ac, mockBindingResult, httpServletResponse);
126 assertEquals(200, postConfig1.getStatusCode());
127 TopicConfig ret = postConfig1.getReturnBody();
128 assertEquals("a", ret.getName());
129 assertEquals(true, ret.isEnabled());
131 topicController.updateTopic(0, ac, mockBindingResult, httpServletResponse);
133 when(topicService.getTopic(1)).thenReturn(null);
134 topicController.updateTopic(1, ac, mockBindingResult, httpServletResponse);
136 when(mockBindingResult.hasErrors()).thenReturn(true);
137 PostReturnBody<TopicConfig> postConfig2 = topicController.updateTopic(1, ac, mockBindingResult, httpServletResponse);
138 assertNull(postConfig2);
143 public void testGetTopic() throws IOException {
144 Topic a = TestUtil.newTopic("a");
148 when(topicService.getTopic(1)).thenReturn(a);
149 TopicConfig ac = topicController.getTopic(1, httpServletResponse);
150 when(topicService.getTopic(1)).thenReturn(null);
151 ac = topicController.getTopic(1, httpServletResponse);
155 public void testDeleteTopic() throws IOException {
156 Topic a = TestUtil.newTopic("a");
160 when(topicService.getTopic(1)).thenReturn(a);
161 topicController.deleteTopic(1, httpServletResponse);
162 when(topicService.getTopic(1)).thenReturn(null);
163 topicController.deleteTopic(1, httpServletResponse);
167 public void testList() {
168 ArrayList<Topic> topics = new ArrayList<>();
169 topics.add(TestUtil.newTopic("a"));
170 topics.add(TestUtil.newTopic(DEFAULT_TOPIC_NAME));
171 when(topicRepository.findAll()).thenReturn(topics);
173 List<String> strings = topicController.list();
174 for (String topic : strings) {
175 System.out.println(topic);