fc1e8a3c0e012b83ebda8c64b04d65f730869d6b
[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.assertFalse;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.when;
27
28 import java.lang.reflect.Field;
29 import java.lang.reflect.InvocationTargetException;
30 import java.lang.reflect.Method;
31 import java.util.Arrays;
32 import java.util.List;
33
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.onap.datalake.feeder.config.ApplicationConfiguration;
40 import org.onap.datalake.feeder.domain.Kafka;
41
42 /**
43  * Test TopicConfigPollingService
44  * 
45  * @author Guobiao Mo
46  *
47  */
48 @RunWith(MockitoJUnitRunner.class)
49 public class TopicConfigPollingServiceTest {
50         @Mock
51         private ApplicationConfiguration config;
52
53         @Mock
54         private DmaapService dmaapService;
55
56         @InjectMocks
57         private TopicConfigPollingService topicConfigPollingService = new TopicConfigPollingService();
58
59         public void testInit() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
60                 Method init = topicConfigPollingService.getClass().getDeclaredMethod("init");
61                 init.setAccessible(true);
62                 init.invoke(topicConfigPollingService);
63
64                 List<String> activeTopics = Arrays.asList("test");
65                 Field activeTopicsField = topicConfigPollingService.getClass().getDeclaredField("activeTopics");
66                 activeTopicsField.setAccessible(true);
67                 activeTopicsField.set(topicConfigPollingService, activeTopics);
68         }
69
70         @Test
71         public void testRun() throws InterruptedException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
72                 testInit();
73
74                 when(config.getDmaapCheckNewTopicInterval()).thenReturn(1);
75
76                 Thread thread = new Thread(topicConfigPollingService);
77                 thread.start();
78
79                 Thread.sleep(50);
80                 topicConfigPollingService.shutdown();
81                 thread.join();
82
83                 assertTrue(topicConfigPollingService.isActiveTopicsChanged(true));
84         }
85
86         @Test
87         public void testRunNoChange() throws InterruptedException {
88         
89                 when(config.getDmaapCheckNewTopicInterval()).thenReturn(1);
90
91                 Thread thread = new Thread(topicConfigPollingService);
92                 thread.start();
93
94                 Thread.sleep(50);
95                 topicConfigPollingService.shutdown();
96                 thread.join();
97
98                 assertFalse(topicConfigPollingService.isActiveTopicsChanged(false));
99         }
100
101         @Test
102         public void testGet() {
103                 Kafka kafka=null;
104                 assertNull(topicConfigPollingService.getEffectiveTopicConfig("test"));
105                 assertNull(topicConfigPollingService.getActiveTopics(kafka));
106
107         }
108 }