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.junit.Assert.assertNull;
 
  51 import static org.mockito.Mockito.when;
 
  53 @RunWith(MockitoJUnitRunner.class)
 
  54 public class TopicControllerTest {
 
  56     static String DEFAULT_TOPIC_NAME = "_DL_DEFAULT_";
 
  59     private HttpServletResponse httpServletResponse;
 
  62     private BindingResult mockBindingResult;
 
  65     private TopicRepository topicRepository;
 
  69     private TopicService topicServiceMock;
 
  72     private TopicService topicService1;
 
  75     private ApplicationConfiguration config;
 
  78     private DbService dbService1;
 
  81     private DmaapService dmaapService1;
 
  84     public void setupTest() {
 
  85         MockitoAnnotations.initMocks(this);
 
  86         // While the default boolean return value for a mock is 'false',
 
  87         // it's good to be explicit anyway:
 
  88         when(mockBindingResult.hasErrors()).thenReturn(false);
 
  91     public void setAccessPrivateFields(TopicController topicController) throws NoSuchFieldException,
 
  92             IllegalAccessException {
 
  93         Field topicService = topicController.getClass().getDeclaredField("topicService");
 
  94         topicService.setAccessible(true);
 
  95         topicService.set(topicController, topicService1);
 
  96         Field topicRepository1 = topicController.getClass().getDeclaredField("topicRepository");
 
  97         topicRepository1.setAccessible(true);
 
  98         topicRepository1.set(topicController, topicRepository);
 
  99 //        Field dbService = topicController.getClass().getDeclaredField("dbService");
 
 100   //      dbService.setAccessible(true);
 
 101     //    dbService.set(topicController, dbService1);
 
 105     public void testListTopic() throws IOException, NoSuchFieldException, IllegalAccessException{
 
 106         TopicController topicController = new TopicController();
 
 107         setAccessPrivateFields(topicController);
 
 111     public void testCreateTopic() throws IOException, NoSuchFieldException, IllegalAccessException {
 
 112         TopicController topicController = new TopicController();
 
 113         setAccessPrivateFields(topicController);
 
 114         //when(topicRepository.findById("ab")).thenReturn(Optional.of(new Topic("ab")));
 
 115        // when(config.getDefaultTopicName()).thenReturn(DEFAULT_TOPIC_NAME);
 
 116         PostReturnBody<TopicConfig> postTopic = topicController.createTopic(new TopicConfig(), mockBindingResult, httpServletResponse);
 
 117         assertEquals(postTopic.getStatusCode(), 200);
 
 118         when(mockBindingResult.hasErrors()).thenReturn(true);
 
 119         PostReturnBody<TopicConfig> topicConfig= topicController.createTopic(new TopicConfig(), mockBindingResult, httpServletResponse);
 
 120         assertEquals(null, topicConfig);
 
 121         when(mockBindingResult.hasErrors()).thenReturn(false);
 
 122         TopicConfig a = new TopicConfig();
 
 123         a.setName(DEFAULT_TOPIC_NAME);
 
 124         //when(topicRepository.findById(DEFAULT_TOPIC_NAME)).thenReturn(Optional.of(new Topic(DEFAULT_TOPIC_NAME)));
 
 125         PostReturnBody<TopicConfig> postTopic2= topicController.createTopic(a, mockBindingResult, httpServletResponse);
 
 126         //assertEquals(null, postTopic2);
 
 130     public void testUpdateTopic() throws IOException, NoSuchFieldException, IllegalAccessException {
 
 131         TopicController topicController = new TopicController();
 
 132         setAccessPrivateFields(topicController);
 
 133         PostReturnBody<TopicConfig> postTopic = topicController.updateTopic("a", new TopicConfig(), mockBindingResult, httpServletResponse);
 
 134         assertEquals(null, postTopic);
 
 135         Topic a = new Topic("a");
 
 137         //when(topicRepository.findById(1)).thenReturn(Optional.of(a));
 
 138         TopicConfig ac = new TopicConfig();
 
 141         PostReturnBody<TopicConfig> postConfig1 = topicController.updateTopic("a", ac, mockBindingResult, httpServletResponse);
 
 142         //assertEquals(200, postConfig1.getStatusCode());
 
 143         assertNull(postConfig1);
 
 144         //TopicConfig ret = postConfig1.getReturnBody();
 
 145         //assertEquals("a", ret.getName());
 
 146         //assertEquals(true, ret.isEnabled());
 
 147         when(mockBindingResult.hasErrors()).thenReturn(true);
 
 148         PostReturnBody<TopicConfig> postConfig2 = topicController.updateTopic("a", ac, mockBindingResult, httpServletResponse);
 
 149         assertEquals(null, postConfig2);
 
 154     public void testListDmaapTopics() throws NoSuchFieldException, IllegalAccessException, IOException {
 
 155         TopicController topicController = new TopicController();
 
 156         Field dmaapService = topicController.getClass().getDeclaredField("dmaapService");
 
 157         dmaapService.setAccessible(true);
 
 158         dmaapService.set(topicController, dmaapService1);
 
 159         ArrayList<String> topics = new ArrayList<>();
 
 161         when(dmaapService1.getTopics()).thenReturn(topics);
 
 162         List<String> strings = topicController.listDmaapTopics();
 
 163         for (String topic : strings) {
 
 164             assertEquals("a", topic);