2 * ============LICENSE_START=======================================================
 
   4 * ================================================================================
 
   5 * Copyright 2019 China Mobile
 
   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=========================================================
 
  21 package org.onap.datalake.feeder.service;
 
  23 import static org.junit.Assert.assertEquals;
 
  24 import static org.junit.Assert.assertNotNull;
 
  25 import static org.junit.Assert.assertNull;
 
  26 import static org.junit.Assert.assertTrue;
 
  27 import static org.mockito.Mockito.doThrow;
 
  28 import static org.mockito.Mockito.when;
 
  30 import java.io.IOException;
 
  31 import java.util.HashSet;
 
  32 import java.util.Optional;
 
  35 import org.junit.Test;
 
  36 import org.junit.runner.RunWith;
 
  37 import org.mockito.InjectMocks;
 
  38 import org.mockito.Mock;
 
  39 import org.mockito.junit.MockitoJUnitRunner;
 
  40 import org.onap.datalake.feeder.config.ApplicationConfiguration;
 
  41 import org.onap.datalake.feeder.domain.Db;
 
  42 import org.onap.datalake.feeder.domain.Topic;
 
  43 import org.onap.datalake.feeder.repository.TopicRepository;
 
  46  * Test Service for Topic
 
  51 @RunWith(MockitoJUnitRunner.class)
 
  52 public class TopicServiceTest {
 
  54         static String DEFAULT_TOPIC_NAME = "_DL_DEFAULT_";
 
  57         private ApplicationConfiguration config;
 
  60         private TopicRepository topicRepository;
 
  63         private ElasticsearchService elasticsearchService;
 
  66         private TopicService topicService;
 
  69         public void testGetTopic() {
 
  71                 when(topicRepository.findById(name)).thenReturn(Optional.of(new Topic(name)));
 
  72                 assertEquals(topicService.getTopic(name), new Topic(name));
 
  76         public void testGetTopicNull() {
 
  78                 when(topicRepository.findById(name)).thenReturn(Optional.empty());
 
  79                 assertNull(topicService.getTopic(name));
 
  83         public void testGetDefaultTopic() {
 
  84                 when(topicRepository.findById(DEFAULT_TOPIC_NAME)).thenReturn(Optional.of(new Topic(DEFAULT_TOPIC_NAME)));
 
  85                 when(config.getDefaultTopicName()).thenReturn(DEFAULT_TOPIC_NAME);
 
  86                 assertEquals(topicService.getDefaultTopic(), new Topic(DEFAULT_TOPIC_NAME));
 
  87                 assertTrue(topicService.istDefaultTopic(new Topic(DEFAULT_TOPIC_NAME)));
 
  90         @Test(expected = IOException.class)
 
  91         public void testGetEffectiveTopic() throws IOException {
 
  93                 Topic topic = new Topic(name);
 
  94                 topic.setEnabled(true);
 
  95                 Set<Db> dbSet = new HashSet<>();
 
  96                 dbSet.add(new Db("Elasticsearch"));
 
  99                 when(topicRepository.findById(name)).thenReturn(Optional.of(topic));
 
 100                 when(topicRepository.findById(null)).thenReturn(Optional.empty());
 
 101                 doThrow(IOException.class).when(elasticsearchService).ensureTableExist(name);
 
 103                 assertEquals(topicService.getEffectiveTopic(name), topicService.getEffectiveTopic(name, false));
 
 105                 assertNotNull(topicService.getEffectiveTopic(null));
 
 107                 topicService.getEffectiveTopic(name, true);