2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.onap.datalake.feeder.controller;
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.Puller;
33 import org.springframework.context.ApplicationContext;
35 import java.io.IOException;
36 import java.lang.reflect.Field;
38 import static org.junit.Assert.assertEquals;
39 import static org.mockito.Mockito.when;
42 public class FeederControllerTest {
45 private PullService pullService1;
48 private ApplicationConfiguration config;
51 private ApplicationContext context;
54 private DmaapService dmaapService1;
57 private KafkaConsumer<String, String> kafkaConsumer;
60 public void setupTest() {
61 MockitoAnnotations.initMocks(this);
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);
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 Puller pullThread = new Puller();
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(Puller.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);*/
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);
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);