9aa9ac931fa374f2aa77d16b344e30167b556666
[policy/apex-pdp.git] /
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.apps.uservice.test.adapt.kafka;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.util.Properties;
28
29 import kafka.admin.AdminUtils;
30 import kafka.admin.RackAwareMode;
31 import kafka.server.BrokerState;
32 import kafka.server.KafkaConfig;
33 import kafka.server.KafkaServer;
34 import kafka.utils.TestUtils;
35 import kafka.utils.ZKStringSerializer$;
36 import kafka.utils.ZkUtils;
37 import kafka.zk.EmbeddedZookeeper;
38
39 import org.I0Itec.zkclient.ZkClient;
40 import org.apache.kafka.common.utils.MockTime;
41 import org.apache.kafka.common.utils.Time;
42 import org.junit.AfterClass;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
46 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
47 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
48 import org.onap.policy.apex.service.engine.main.ApexMain;
49
50
51 public class TestKafka2Kafka {
52     // The method of starting an embedded Kafka server used in this example is based on the method
53     // on stack overflow at
54     // https://github.com/asmaier/mini-kafka
55
56     private static final long MAX_TEST_LENGTH = 20000;
57
58     private static final int EVENT_COUNT = 100;
59     private static final int EVENT_INTERVAL = 20;
60
61     private static final String ZKHOST = "127.0.0.1";
62     private static final String BROKERHOST = "127.0.0.1";
63     private static final String BROKERPORT = "39902";
64
65     private static EmbeddedZookeeper zkServer;
66     private static ZkClient zkClient;
67     private static KafkaServer kafkaServer;
68
69     @BeforeClass
70     public static void setupDummyKafkaServer() throws IOException {
71         // setup Zookeeper
72         zkServer = new EmbeddedZookeeper();
73         final String zkConnect = ZKHOST + ":" + zkServer.port();
74         zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer$.MODULE$);
75         final ZkUtils zkUtils = ZkUtils.apply(zkClient, false);
76
77         // setup Broker
78         final Properties brokerProps = new Properties();
79         brokerProps.setProperty("zookeeper.connect", zkConnect);
80         brokerProps.setProperty("broker.id", "0");
81         brokerProps.setProperty("log.dirs", Files.createTempDirectory("kafka-").toAbsolutePath().toString());
82         brokerProps.setProperty("listeners", "PLAINTEXT://" + BROKERHOST + ":" + BROKERPORT);
83         brokerProps.setProperty("offsets.topic.replication.factor", "1");
84         brokerProps.setProperty("transaction.state.log.replication.factor", "1");
85         brokerProps.setProperty("transaction.state.log.min.isr", "1");
86         final KafkaConfig config = new KafkaConfig(brokerProps);
87         final Time mock = new MockTime();
88         kafkaServer = TestUtils.createServer(config, mock);
89         kafkaServer.startup();
90         
91         // create topics
92         AdminUtils.createTopic(zkUtils, "apex-in-0", 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);
93         AdminUtils.createTopic(zkUtils, "apex-in-1", 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);
94         AdminUtils.createTopic(zkUtils, "apex-out", 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);
95         
96     }
97
98     @AfterClass
99     public static void shutdownDummyKafkaServer() throws IOException {
100         if (kafkaServer != null) {
101             kafkaServer.shutdown();
102         }
103         if (zkClient != null) {
104             zkClient.close();
105         }
106         if (zkServer != null) {
107             zkServer.shutdown();
108         }
109     }
110
111     @Test
112     public void testJsonKafkaEvents() throws MessagingException, ApexException {
113         final String[] args = {"src/test/resources/prodcons/Kafka2KafkaJsonEvent.json"};
114         testKafkaEvents(args, false, "json");
115     }
116
117     @Test
118     public void testXMLKafkaEvents() throws MessagingException, ApexException {
119         final String[] args = {"src/test/resources/prodcons/Kafka2KafkaXmlEvent.json"};
120         testKafkaEvents(args, true, "xml");
121     }
122
123     private void testKafkaEvents(final String[] args, final Boolean xmlEvents, final String topicSuffix)
124             throws MessagingException, ApexException {
125         final KafkaEventSubscriber subscriber =
126                 new KafkaEventSubscriber("apex-out-" + topicSuffix, "localhost:" + BROKERPORT);
127
128         final ApexMain apexMain = new ApexMain(args);
129         ThreadUtilities.sleep(3000);
130
131         final KafkaEventProducer producer = new KafkaEventProducer("apex-in-" + topicSuffix, "localhost:" + BROKERPORT,
132                         EVENT_COUNT, xmlEvents, EVENT_INTERVAL);
133
134         producer.sendEvents();
135
136         final long testStartTime = System.currentTimeMillis();
137
138         while (System.currentTimeMillis() < testStartTime + MAX_TEST_LENGTH
139                 && subscriber.getEventsReceivedCount() < EVENT_COUNT) {
140             ThreadUtilities.sleep(EVENT_INTERVAL);
141         }
142
143         ThreadUtilities.sleep(1000);
144
145         assertEquals(producer.getEventsSentCount(), subscriber.getEventsReceivedCount());
146
147         apexMain.shutdown();
148         subscriber.shutdown();
149         producer.shutdown();
150         ThreadUtilities.sleep(1000);
151     }
152 }