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.assertNotNull;
 
  24 import static org.junit.Assert.assertTrue;
 
  25 import static org.mockito.Mockito.when;
 
  27 import java.lang.reflect.Field;
 
  28 import java.lang.reflect.InvocationTargetException;
 
  29 import java.lang.reflect.Method;
 
  30 import java.util.Arrays;
 
  31 import java.util.HashMap;
 
  32 import java.util.HashSet;
 
  36 import org.junit.Before;
 
  37 import org.junit.Test;
 
  38 import org.junit.runner.RunWith;
 
  39 import org.mockito.InjectMocks;
 
  40 import org.mockito.Mock;
 
  41 import org.mockito.junit.MockitoJUnitRunner;
 
  42 import org.onap.datalake.feeder.config.ApplicationConfiguration;
 
  43 import org.onap.datalake.feeder.domain.Kafka;
 
  44 import org.onap.datalake.feeder.util.TestUtil;
 
  47  * Test TopicConfigPollingService
 
  52 @RunWith(MockitoJUnitRunner.class)
 
  53 public class TopicConfigPollingServiceTest {
 
  55         private ApplicationConfiguration config;
 
  58         private DmaapService dmaapService;
 
  61         private TopicConfigPollingService topicConfigPollingService = new TopicConfigPollingService();
 
  63         static String KAFKA_NAME = "kafka1";
 
  66         public void init() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
 
  67                 Method init = topicConfigPollingService.getClass().getDeclaredMethod("init");
 
  68                 init.setAccessible(true);
 
  69                 init.invoke(topicConfigPollingService);
 
  71                 Set<String> activeTopics = new HashSet<>(Arrays.asList("test"));
 
  72                 Map<Integer, Set<String>> activeTopicMap = new HashMap<>();
 
  73                 activeTopicMap.put(1, activeTopics);
 
  75                 Field activeTopicsField = TopicConfigPollingService.class.getDeclaredField("activeTopicMap");
 
  76                 activeTopicsField.setAccessible(true);
 
  77                 activeTopicsField.set(topicConfigPollingService, activeTopicMap);
 
  79                 Method initMethod = TopicConfigPollingService.class.getDeclaredMethod("init");
 
  80                 initMethod.setAccessible(true);
 
  81                 initMethod.invoke(topicConfigPollingService);
 
  85         public void testRun() throws InterruptedException {
 
  87                 when(config.getCheckTopicInterval()).thenReturn(1L);
 
  89                 Thread thread = new Thread(topicConfigPollingService);
 
  93                 topicConfigPollingService.shutdown();
 
  96                 assertTrue(topicConfigPollingService.isActiveTopicsChanged(new Kafka()));
 
 100         public void testRunNoChange() throws InterruptedException {
 
 102                 when(config.getCheckTopicInterval()).thenReturn(1L);
 
 104                 Thread thread = new Thread(topicConfigPollingService);
 
 108                 topicConfigPollingService.shutdown();
 
 111                 assertTrue(topicConfigPollingService.isActiveTopicsChanged(new Kafka()));
 
 115         public void testGet() {
 
 116                 Kafka kafka = TestUtil.newKafka(KAFKA_NAME);
 
 118                 //assertNull(topicConfigPollingService.getEffectiveTopic (kafka, "test"));
 
 119                 assertNotNull(topicConfigPollingService.getActiveTopics(kafka));