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.junit.MockitoJUnitRunner;
 
  28 import org.onap.datalake.feeder.config.ApplicationConfiguration;
 
  29 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
 
  30 import org.onap.datalake.feeder.domain.Topic;
 
  31 import org.onap.datalake.feeder.dto.TopicConfig;
 
  32 import org.onap.datalake.feeder.repository.TopicNameRepository;
 
  33 import org.onap.datalake.feeder.repository.TopicRepository;
 
  34 import org.onap.datalake.feeder.service.DbService;
 
  35 import org.onap.datalake.feeder.service.DmaapService;
 
  36 import org.onap.datalake.feeder.service.TopicService;
 
  37 import org.onap.datalake.feeder.util.TestUtil;
 
  38 import org.springframework.validation.BindingResult;
 
  40 import javax.servlet.http.HttpServletResponse;
 
  41 import java.io.IOException;
 
  42 import java.util.ArrayList;
 
  43 import java.util.List;
 
  45 import static org.junit.Assert.assertEquals;
 
  46 import static org.junit.Assert.assertNull;
 
  47 import static org.mockito.Mockito.when;
 
  49 @RunWith(MockitoJUnitRunner.class)
 
  50 public class TopicControllerTest {
 
  52         static String DEFAULT_TOPIC_NAME = "_DL_DEFAULT_";
 
  55         private HttpServletResponse httpServletResponse;
 
  58         private BindingResult mockBindingResult;
 
  61         private TopicRepository topicRepository;
 
  64         private TopicService topicService;
 
  67         private TopicNameRepository topicNameRepository;
 
  70         TopicController topicController;
 
  73         private ApplicationConfiguration config;
 
  76         private DbService dbService;
 
  79         private DmaapService dmaapService;
 
  82         public void setupTest() throws NoSuchFieldException, IllegalAccessException {
 
  83                 // While the default boolean return value for a mock is 'false',
 
  84                 // it's good to be explicit anyway:
 
  85                 when(mockBindingResult.hasErrors()).thenReturn(false);
 
  89         public void testListTopic() throws IOException, NoSuchFieldException, IllegalAccessException {
 
  93         public void testCreateTopic() throws IOException {
 
  94                 Topic a = TestUtil.newTopic("a");
 
  98                 TopicConfig ac = a.getTopicConfig();
 
 100                 when(topicService.fillTopicConfiguration(ac)).thenReturn(a);
 
 101                 PostReturnBody<TopicConfig> postTopic = topicController.createTopic(ac, mockBindingResult, httpServletResponse);
 
 102                 assertEquals(postTopic.getStatusCode(), 200);
 
 104                 when(mockBindingResult.hasErrors()).thenReturn(true);
 
 105                 PostReturnBody<TopicConfig> topicConfig = topicController.createTopic(ac, mockBindingResult, httpServletResponse);
 
 106                 assertEquals(null, topicConfig);
 
 110         public void testUpdateTopic() throws IOException {
 
 111                 Topic a = TestUtil.newTopic("a");
 
 115                 TopicConfig ac = a.getTopicConfig();
 
 117                 when(topicService.getTopic(1)).thenReturn(a);
 
 118                 PostReturnBody<TopicConfig> postConfig1 = topicController.updateTopic(1, ac, mockBindingResult, httpServletResponse);
 
 119                 assertEquals(200, postConfig1.getStatusCode());
 
 120                 TopicConfig ret = postConfig1.getReturnBody();
 
 121                 assertEquals("a", ret.getName());
 
 122                 assertEquals(true, ret.isEnabled());
 
 124                 topicController.updateTopic(0, ac, mockBindingResult, httpServletResponse);
 
 126                 when(topicService.getTopic(1)).thenReturn(null);
 
 127                 topicController.updateTopic(1, ac, mockBindingResult, httpServletResponse);
 
 129                 when(mockBindingResult.hasErrors()).thenReturn(true);
 
 130                 PostReturnBody<TopicConfig> postConfig2 = topicController.updateTopic(1, ac, mockBindingResult, httpServletResponse);
 
 131                 assertNull(postConfig2);
 
 136         public void testGetTopic() throws IOException {
 
 137                 Topic a = TestUtil.newTopic("a");
 
 141                 when(topicService.getTopic(1)).thenReturn(a);
 
 142                 TopicConfig ac = topicController.getTopic(1, httpServletResponse);
 
 143                 when(topicService.getTopic(1)).thenReturn(null);
 
 144                 ac = topicController.getTopic(1, httpServletResponse);
 
 148         public void testDeleteTopic() throws IOException {
 
 149                 Topic a = TestUtil.newTopic("a");
 
 153                 when(topicService.getTopic(1)).thenReturn(a);
 
 154                 topicController.deleteTopic(1, httpServletResponse);
 
 155                 when(topicService.getTopic(1)).thenReturn(null);
 
 156                 topicController.deleteTopic(1, httpServletResponse);
 
 160         public void testList() {
 
 161                 ArrayList<Topic> topics = new ArrayList<>();
 
 162                 topics.add(TestUtil.newTopic("a"));
 
 163                 topics.add(TestUtil.newTopic(DEFAULT_TOPIC_NAME));
 
 164                 when(topicRepository.findAll()).thenReturn(topics);
 
 166                 List<Integer> ids = topicController.list();
 
 167                 for (Integer topic : ids) {
 
 168                         System.out.println(topic);