bd26519b632aded3f5d3ca4a0d98fb695807a81b
[dcaegen2/services.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : DATALAKE
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.datalake.feeder.service;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.when;
26
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;
33 import java.util.Map;
34 import java.util.Set;
35
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;
45
46 /**
47  * Test TopicConfigPollingService
48  * 
49  * @author Guobiao Mo
50  *
51  */
52 @RunWith(MockitoJUnitRunner.class)
53 public class TopicConfigPollingServiceTest {
54         @Mock
55         private ApplicationConfiguration config;
56
57         @Mock
58         private DmaapService dmaapService;
59
60         @InjectMocks
61         private TopicConfigPollingService topicConfigPollingService = new TopicConfigPollingService();
62
63         static String KAFKA_NAME = "kafka1";
64
65         @Before
66         public void init() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
67                 Method init = topicConfigPollingService.getClass().getDeclaredMethod("init");
68                 init.setAccessible(true);
69                 init.invoke(topicConfigPollingService);
70
71                 Set<String> activeTopics = new HashSet<>(Arrays.asList("test"));
72                 Map<Integer, Set<String>> activeTopicMap = new HashMap<>();
73                 activeTopicMap.put(1, activeTopics);
74
75                 Field activeTopicsField = TopicConfigPollingService.class.getDeclaredField("activeTopicMap");
76                 activeTopicsField.setAccessible(true);
77                 activeTopicsField.set(topicConfigPollingService, activeTopicMap);
78
79                 Method initMethod = TopicConfigPollingService.class.getDeclaredMethod("init");
80                 initMethod.setAccessible(true);
81                 initMethod.invoke(topicConfigPollingService);
82         }
83
84         @Test
85         public void testRun() throws InterruptedException {
86
87                 when(config.getCheckTopicInterval()).thenReturn(1L);
88
89                 Thread thread = new Thread(topicConfigPollingService);
90                 thread.start();
91
92                 Thread.sleep(50);
93                 topicConfigPollingService.shutdown();
94                 thread.join();
95
96                 assertTrue(topicConfigPollingService.isActiveTopicsChanged(new Kafka()));
97         }
98
99         @Test
100         public void testRunNoChange() throws InterruptedException {
101
102                 when(config.getCheckTopicInterval()).thenReturn(1L);
103
104                 Thread thread = new Thread(topicConfigPollingService);
105                 thread.start();
106
107                 Thread.sleep(50);
108                 topicConfigPollingService.shutdown();
109                 thread.join();
110
111                 assertTrue(topicConfigPollingService.isActiveTopicsChanged(new Kafka()));
112         }
113
114         @Test
115         public void testGet() {
116                 Kafka kafka = TestUtil.newKafka(KAFKA_NAME);
117                 kafka.setId(1);
118                 //assertNull(topicConfigPollingService.getEffectiveTopic (kafka, "test"));
119                 assertNotNull(topicConfigPollingService.getActiveTopics(kafka));
120
121         }
122
123 }