2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.uservice.adapt.kafka;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
28 import com.salesforce.kafka.test.junit4.SharedKafkaTestResource;
31 import java.io.IOException;
32 import java.util.concurrent.TimeUnit;
34 import org.junit.Before;
35 import org.junit.ClassRule;
36 import org.junit.Test;
37 import org.onap.policy.apex.service.engine.main.ApexMain;
38 import org.onap.policy.common.utils.resources.TextFileUtils;
41 * The Class TestKafka2Kafka tests Kafka event sending and reception.
43 public class TestKafka2Kafka {
44 private static final long MAX_TEST_LENGTH = 300000;
46 private static final int EVENT_COUNT = 25;
47 private static final int EVENT_INTERVAL = 20;
50 * Clear relative file root environment variable.
53 public void clearRelativeFileRoot() {
54 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
58 public static final SharedKafkaTestResource sharedKafkaTestResource = new SharedKafkaTestResource()
59 // Start a cluster with 1 brokers.
61 // Disable topic auto-creation.
62 .withBrokerProperty("auto.create.topics.enable", "false");
65 * Test json kafka events.
67 * @throws Exception the apex exception
70 public void testJsonKafkaEvents() throws Exception {
71 final String conditionedConfigFile = getConditionedConfigFile(
72 "target" + File.separator + "examples/config/SampleDomain/Kafka2KafkaJsonEvent.json");
73 final String[] args = {"-rfr", "target", "-c", conditionedConfigFile};
74 testKafkaEvents(args, false, "json");
78 * Test XML kafka events.
80 * @throws Exception the apex exception
83 public void testXmlKafkaEvents() throws Exception {
84 final String conditionedConfigFile = getConditionedConfigFile(
85 "target" + File.separator + "examples/config/SampleDomain/Kafka2KafkaXmlEvent.json");
86 final String[] args = {"-rfr", "target", "-c", conditionedConfigFile};
88 testKafkaEvents(args, true, "xml");
94 * @param args the args
95 * @param xmlEvents the xml events
96 * @param topicSuffix the topic suffix
97 * @throws Exception on errors
99 private void testKafkaEvents(String[] args, final Boolean xmlEvents, final String topicSuffix) throws Exception {
101 sharedKafkaTestResource.getKafkaTestUtils().createTopic("apex-out-" + topicSuffix, 1, (short) 1);
102 sharedKafkaTestResource.getKafkaTestUtils().createTopic("apex-in-" + topicSuffix, 1, (short) 1);
104 final KafkaEventSubscriber subscriber =
105 new KafkaEventSubscriber("apex-out-" + topicSuffix, sharedKafkaTestResource);
107 await().atMost(30, TimeUnit.SECONDS).until(() -> subscriber.isAlive());
109 final ApexMain apexMain = new ApexMain(args);
110 await().atMost(10, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
112 long initWaitEndTIme = System.currentTimeMillis() + 10000;
114 await().atMost(12, TimeUnit.SECONDS).until(() -> initWaitEndTIme < System.currentTimeMillis());
116 final KafkaEventProducer producer = new KafkaEventProducer("apex-in-" + topicSuffix, sharedKafkaTestResource,
117 EVENT_COUNT, xmlEvents, EVENT_INTERVAL);
119 await().atMost(30, TimeUnit.SECONDS).until(() -> producer.isAlive());
121 producer.sendEvents();
123 // Wait for the producer to send all its events
124 await().atMost(MAX_TEST_LENGTH, TimeUnit.MILLISECONDS)
125 .until(() -> producer.getEventsSentCount() >= EVENT_COUNT);
127 await().atMost(MAX_TEST_LENGTH, TimeUnit.MILLISECONDS)
128 .until(() -> subscriber.getEventsReceivedCount() >= EVENT_COUNT);
131 await().atMost(30, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
133 subscriber.shutdown();
134 await().atMost(30, TimeUnit.SECONDS).until(() -> !subscriber.isAlive());
137 await().atMost(30, TimeUnit.SECONDS).until(() -> !producer.isAlive());
139 assertEquals(producer.getEventsSentCount(), subscriber.getEventsReceivedCount());
142 private String getConditionedConfigFile(final String configurationFileName) {
144 File tempConfigFile = File.createTempFile("Kafka_", ".json");
145 tempConfigFile.deleteOnExit();
146 String configAsString = TextFileUtils.getTextFileAsString(configurationFileName)
147 .replaceAll("localhost:39902", sharedKafkaTestResource.getKafkaConnectString());
148 TextFileUtils.putStringAsFile(configAsString, tempConfigFile.getCanonicalFile());
150 return tempConfigFile.getCanonicalPath();
151 } catch (IOException e) {
152 fail("test should not throw an exception");