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.repository.TopicRepository;
35 import org.onap.datalake.feeder.service.DbService;
36 import org.onap.datalake.feeder.service.DmaapService;
37 import org.onap.datalake.feeder.service.TopicService;
38 import org.springframework.validation.BindingResult;
40 import javax.servlet.http.HttpServletResponse;
41 import java.io.IOException;
42 import java.lang.reflect.Field;
43 import java.util.ArrayList;
44 import java.util.HashSet;
45 import java.util.List;
46 import java.util.Optional;
49 import static org.junit.Assert.assertEquals;
50 import static org.mockito.Mockito.when;
52 @RunWith(MockitoJUnitRunner.class)
53 public class TopicControllerTest {
55 static String DEFAULT_TOPIC_NAME = "_DL_DEFAULT_";
58 private HttpServletResponse httpServletResponse;
61 private BindingResult mockBindingResult;
64 private TopicRepository topicRepository;
68 private TopicService topicServiceMock;
71 private TopicService topicService1;
74 private ApplicationConfiguration config;
77 private DbService dbService1;
80 private DmaapService dmaapService1;
83 public void setupTest() {
84 MockitoAnnotations.initMocks(this);
85 // While the default boolean return value for a mock is 'false',
86 // it's good to be explicit anyway:
87 when(mockBindingResult.hasErrors()).thenReturn(false);
90 public void setAccessPrivateFields(TopicController topicController) throws NoSuchFieldException,
91 IllegalAccessException {
92 Field topicService = topicController.getClass().getDeclaredField("topicService");
93 topicService.setAccessible(true);
94 topicService.set(topicController, topicService1);
95 Field topicRepository1 = topicController.getClass().getDeclaredField("topicRepository");
96 topicRepository1.setAccessible(true);
97 topicRepository1.set(topicController, topicRepository);
98 // Field dbService = topicController.getClass().getDeclaredField("dbService");
99 // dbService.setAccessible(true);
100 // dbService.set(topicController, dbService1);
104 public void testListTopic() throws IOException, NoSuchFieldException, IllegalAccessException{
105 TopicController topicController = new TopicController();
106 setAccessPrivateFields(topicController);
110 public void testCreateTopic() throws IOException, NoSuchFieldException, IllegalAccessException {
111 TopicController topicController = new TopicController();
112 setAccessPrivateFields(topicController);
113 //when(topicRepository.findById("ab")).thenReturn(Optional.of(new Topic("ab")));
114 // when(config.getDefaultTopicName()).thenReturn(DEFAULT_TOPIC_NAME);
115 PostReturnBody<TopicConfig> postTopic = topicController.createTopic(new TopicConfig(), mockBindingResult, httpServletResponse);
116 assertEquals(postTopic.getStatusCode(), 200);
117 when(mockBindingResult.hasErrors()).thenReturn(true);
118 PostReturnBody<TopicConfig> topicConfig= topicController.createTopic(new TopicConfig(), mockBindingResult, httpServletResponse);
119 assertEquals(null, topicConfig);
120 when(mockBindingResult.hasErrors()).thenReturn(false);
121 TopicConfig a = new TopicConfig();
122 a.setName(DEFAULT_TOPIC_NAME);
123 when(topicRepository.findById(DEFAULT_TOPIC_NAME)).thenReturn(Optional.of(new Topic(DEFAULT_TOPIC_NAME)));
124 PostReturnBody<TopicConfig> postTopic2= topicController.createTopic(a, mockBindingResult, httpServletResponse);
125 assertEquals(null, postTopic2);
129 public void testUpdateTopic() throws IOException, NoSuchFieldException, IllegalAccessException {
130 TopicController topicController = new TopicController();
131 setAccessPrivateFields(topicController);
132 PostReturnBody<TopicConfig> postTopic = topicController.updateTopic("a", new TopicConfig(), mockBindingResult, httpServletResponse);
133 assertEquals(null, postTopic);
134 Topic a = new Topic("a");
136 when(topicRepository.findById("a")).thenReturn(Optional.of(a));
137 TopicConfig ac = new TopicConfig();
140 PostReturnBody<TopicConfig> postConfig1 = topicController.updateTopic("a", ac, mockBindingResult, httpServletResponse);
141 assertEquals(200, postConfig1.getStatusCode());
142 TopicConfig ret = postConfig1.getReturnBody();
143 assertEquals("a", ret.getName());
144 assertEquals(true, ret.isEnable());
145 when(mockBindingResult.hasErrors()).thenReturn(true);
146 PostReturnBody<TopicConfig> postConfig2 = topicController.updateTopic("a", ac, mockBindingResult, httpServletResponse);
147 assertEquals(null, postConfig2);
152 public void testListDmaapTopics() throws NoSuchFieldException, IllegalAccessException, IOException {
153 TopicController topicController = new TopicController();
154 Field dmaapService = topicController.getClass().getDeclaredField("dmaapService");
155 dmaapService.setAccessible(true);
156 dmaapService.set(topicController, dmaapService1);
157 ArrayList<String> topics = new ArrayList<>();
159 when(dmaapService1.getTopics()).thenReturn(topics);
160 List<String> strings = topicController.listDmaapTopics();
161 for (String topic : strings) {
162 assertEquals("a", topic);