a341d2a64bde082540001e0e26c3f45b9a242fe9
[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
41 /**
42  * Test TopicConfigPollingService
43  * 
44  * @author Guobiao Mo
45  *
46  */
47 @RunWith(MockitoJUnitRunner.class)
48 public class TopicConfigPollingServiceTest {
49         @Mock
50         private ApplicationConfiguration config;
51
52         @Mock
53         private DmaapService dmaapService;
54
55         @InjectMocks
56         private TopicConfigPollingService topicConfigPollingService = new TopicConfigPollingService();
57
58         public void testInit() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
59                 Method init = topicConfigPollingService.getClass().getDeclaredMethod("init");
60                 init.setAccessible(true);
61                 init.invoke(topicConfigPollingService);
62
63                 List<String> activeTopics = Arrays.asList("test");
64                 Field activeTopicsField = topicConfigPollingService.getClass().getDeclaredField("activeTopics");
65                 activeTopicsField.setAccessible(true);
66                 activeTopicsField.set(topicConfigPollingService, activeTopics);
67         }
68
69         @Test
70         public void testRun() throws InterruptedException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
71                 testInit();
72
73                 when(config.getDmaapCheckNewTopicInterval()).thenReturn(1);
74
75                 Thread thread = new Thread(topicConfigPollingService);
76                 thread.start();
77
78                 Thread.sleep(50);
79                 topicConfigPollingService.shutdown();
80                 thread.join();
81
82                 assertTrue(topicConfigPollingService.isActiveTopicsChanged(true));
83         }
84
85         @Test
86         public void testRunNoChange() throws InterruptedException {
87         
88                 when(config.getDmaapCheckNewTopicInterval()).thenReturn(1);
89
90                 Thread thread = new Thread(topicConfigPollingService);
91                 thread.start();
92
93                 Thread.sleep(50);
94                 topicConfigPollingService.shutdown();
95                 thread.join();
96
97                 assertFalse(topicConfigPollingService.isActiveTopicsChanged(false));
98         }
99
100         @Test
101         public void testGet() {
102                 assertNull(topicConfigPollingService.getEffectiveTopicConfig("test"));
103                 assertNull(topicConfigPollingService.getActiveTopics());
104
105         }
106 }