2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.apps.uservice.test.adapt.kafka;
23 import static org.junit.Assert.assertEquals;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.util.Properties;
29 import org.I0Itec.zkclient.ZkClient;
30 import org.apache.kafka.common.utils.MockTime;
31 import org.apache.kafka.common.utils.Time;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
36 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
37 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
38 import org.onap.policy.apex.service.engine.main.ApexMain;
40 import kafka.admin.AdminUtils;
41 import kafka.admin.RackAwareMode;
42 import kafka.server.KafkaConfig;
43 import kafka.server.KafkaServer;
44 import kafka.utils.TestUtils;
45 import kafka.utils.ZKStringSerializer$;
46 import kafka.utils.ZkUtils;
47 import kafka.zk.EmbeddedZookeeper;
49 public class TestKafka2Kafka {
50 // The method of starting an embedded Kafka server used in this example is based on the method
51 // on stack overflow at
52 // https://github.com/asmaier/mini-kafka
54 private static final long MAX_TEST_LENGTH = 20000;
56 private static final int EVENT_COUNT = 100;
57 private static final int EVENT_INTERVAL = 20;
59 private static final String ZKHOST = "127.0.0.1";
60 private static final String BROKERHOST = "127.0.0.1";
61 private static final String BROKERPORT = "39902";
63 private static EmbeddedZookeeper zkServer;
64 private static ZkClient zkClient;
65 private static KafkaServer kafkaServer;
68 public static void setupDummyKafkaServer() throws IOException {
70 zkServer = new EmbeddedZookeeper();
71 final String zkConnect = ZKHOST + ":" + zkServer.port();
72 zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer$.MODULE$);
73 final ZkUtils zkUtils = ZkUtils.apply(zkClient, false);
76 final Properties brokerProps = new Properties();
77 brokerProps.setProperty("zookeeper.connect", zkConnect);
78 brokerProps.setProperty("broker.id", "0");
79 brokerProps.setProperty("log.dirs", Files.createTempDirectory("kafka-").toAbsolutePath().toString());
80 brokerProps.setProperty("listeners", "PLAINTEXT://" + BROKERHOST + ":" + BROKERPORT);
81 brokerProps.setProperty("offsets.topic.replication.factor", "1");
82 brokerProps.setProperty("transaction.state.log.replication.factor", "1");
83 brokerProps.setProperty("transaction.state.log.min.isr", "1");
84 final KafkaConfig config = new KafkaConfig(brokerProps);
85 final Time mock = new MockTime();
86 kafkaServer = TestUtils.createServer(config, mock);
89 AdminUtils.createTopic(zkUtils, "apex-in-0", 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);
90 AdminUtils.createTopic(zkUtils, "apex-in-1", 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);
91 AdminUtils.createTopic(zkUtils, "apex-out", 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);
95 public static void shutdownDummyKafkaServer() throws IOException {
96 if (kafkaServer != null) {
97 kafkaServer.shutdown();
99 if (zkClient != null) {
102 if (zkServer != null) {
108 public void testJsonKafkaEvents() throws MessagingException, ApexException {
109 final String[] args = {"src/test/resources/prodcons/Kafka2KafkaJsonEvent.json"};
110 testKafkaEvents(args, false, "json");
114 public void testXMLKafkaEvents() throws MessagingException, ApexException {
115 final String[] args = {"src/test/resources/prodcons/Kafka2KafkaXmlEvent.json"};
116 testKafkaEvents(args, true, "xml");
119 private void testKafkaEvents(final String[] args, final Boolean xmlEvents, final String topicSuffix)
120 throws MessagingException, ApexException {
121 final KafkaEventSubscriber subscriber =
122 new KafkaEventSubscriber("apex-out-" + topicSuffix, "localhost:" + BROKERPORT);
123 final KafkaEventProducer producer = new KafkaEventProducer("apex-in-" + topicSuffix, "localhost:" + BROKERPORT,
124 EVENT_COUNT, xmlEvents, EVENT_INTERVAL);
126 final ApexMain apexMain = new ApexMain(args);
127 ThreadUtilities.sleep(3000);
129 producer.sendEvents();
131 final long testStartTime = System.currentTimeMillis();
133 while (System.currentTimeMillis() < testStartTime + MAX_TEST_LENGTH
134 && subscriber.getEventsReceivedCount() < EVENT_COUNT) {
135 ThreadUtilities.sleep(EVENT_INTERVAL);
138 ThreadUtilities.sleep(1000);
140 assertEquals(producer.getEventsSentCount(), subscriber.getEventsReceivedCount());
143 subscriber.shutdown();
145 ThreadUtilities.sleep(1000);