7d0b4ee284df310533533011f7a4dc030a702d50
[dcaegen2/services.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : DATALAKE
4  * ================================================================================
5  * Copyright (C) 2018-2019 Huawei. All rights reserved.
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 package org.onap.datalake.feeder.controller;
21
22 import org.apache.kafka.clients.consumer.ConsumerRecords;
23 import org.apache.kafka.clients.consumer.KafkaConsumer;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.onap.datalake.feeder.config.ApplicationConfiguration;
30 import org.onap.datalake.feeder.service.DmaapService;
31 import org.onap.datalake.feeder.service.PullService;
32 import org.onap.datalake.feeder.service.PullThread;
33 import org.springframework.context.ApplicationContext;
34
35 import java.io.IOException;
36 import java.lang.reflect.Field;
37
38 import static org.junit.Assert.assertEquals;
39 import static org.mockito.Mockito.when;
40
41
42 public class FeederControllerTest {
43
44     @InjectMocks
45     private PullService pullService1;
46
47     @Mock
48     private ApplicationConfiguration config;
49
50     @Mock
51     private ApplicationContext context;
52
53     @Mock
54     private DmaapService dmaapService1;
55
56     @Mock
57     private KafkaConsumer<String, String> kafkaConsumer;
58
59     @Before
60     public void setupTest() {
61         MockitoAnnotations.initMocks(this);
62     }
63
64     private void setAccessPrivateFields(FeederController feederController) throws NoSuchFieldException,
65             IllegalAccessException {
66         Field pullService = feederController.getClass().getDeclaredField("pullService");
67         pullService.setAccessible(true);
68         pullService.set(feederController, pullService1);
69     }
70
71     @Test
72     public void testStart() throws IOException, NoSuchFieldException, IllegalAccessException {
73         FeederController feederController = new FeederController();
74         setAccessPrivateFields(feederController);
75         PullService pullService2 = new PullService();
76         Field applicationConfig = pullService2.getClass().getDeclaredField("config");
77         applicationConfig.setAccessible(true);
78         applicationConfig.set(pullService2, config);
79         Field applicationContext = pullService2.getClass().getDeclaredField("context");
80         applicationContext.setAccessible(true);
81         applicationContext.set(pullService2, context);
82         when(config.getKafkaConsumerCount()).thenReturn(1);
83         PullThread pullThread = new PullThread(1);
84         Field dmaapService = pullThread.getClass().getDeclaredField("dmaapService");
85         dmaapService.setAccessible(true);
86         dmaapService.set(pullThread, dmaapService1);
87         Field kafkaConsumer1 = pullThread.getClass().getDeclaredField("consumer");
88         kafkaConsumer1.setAccessible(true);
89         kafkaConsumer1.set(pullThread, kafkaConsumer);
90         applicationConfig = pullThread.getClass().getDeclaredField("config");
91         applicationConfig.setAccessible(true);
92         applicationConfig.set(pullThread, config);
93         when(context.getBean(PullThread.class, 0)).thenReturn(pullThread);
94         ConsumerRecords<String, String> records = ConsumerRecords.empty();
95         when(kafkaConsumer.poll(2)).thenReturn(records);
96         String start = feederController.start();
97         assertEquals("{\"running\": true}", start);
98     }
99
100     @Test
101     public void testStop() throws NoSuchFieldException, IllegalAccessException {
102         FeederController feederController = new FeederController();
103         setAccessPrivateFields(feederController);
104         String stop = feederController.stop();
105         assertEquals("{\"running\": false}", stop);
106     }
107
108     @Test
109     public void testStatus() throws NoSuchFieldException, IllegalAccessException {
110         ApplicationConfiguration conf = new ApplicationConfiguration();
111         conf.setDatalakeVersion("0.0.1");
112         FeederController feederController = new FeederController();
113         feederController.config = conf;
114         setAccessPrivateFields(feederController);
115         String status = feederController.status();
116         assertEquals("{\"version\": \"0.0.1\", \"running\": false}", status);
117     }
118 }