c3289887f2f50b5bc17e2024ace9f58667639451
[policy/apex-pdp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.uservice.adapt.kafka;
23
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
27
28 import com.salesforce.kafka.test.junit4.SharedKafkaTestResource;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.concurrent.TimeUnit;
33
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;
39
40 /**
41  * The Class TestKafka2Kafka tests Kafka event sending and reception.
42  */
43 public class TestKafka2Kafka {
44     private static final long MAX_TEST_LENGTH = 300000;
45
46     private static final int EVENT_COUNT = 25;
47     private static final int EVENT_INTERVAL = 20;
48
49     /**
50      * Clear relative file root environment variable.
51      */
52     @Before
53     public void clearRelativeFileRoot() {
54         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
55     }
56
57     @ClassRule
58     public static final SharedKafkaTestResource sharedKafkaTestResource = new SharedKafkaTestResource()
59         // Start a cluster with 1 brokers.
60         .withBrokers(1)
61         // Disable topic auto-creation.
62         .withBrokerProperty("auto.create.topics.enable", "false");
63
64     /**
65      * Test json kafka events.
66      *
67      * @throws Exception the apex exception
68      */
69     @Test
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");
75     }
76
77     /**
78      * Test XML kafka events.
79      *
80      * @throws Exception the apex exception
81      */
82     @Test
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};
87
88         testKafkaEvents(args, true, "xml");
89     }
90
91     /**
92      * Test kafka events.
93      *
94      * @param args the args
95      * @param xmlEvents the xml events
96      * @param topicSuffix the topic suffix
97      * @throws Exception on errors
98      */
99     private void testKafkaEvents(String[] args, final Boolean xmlEvents, final String topicSuffix) throws Exception {
100
101         sharedKafkaTestResource.getKafkaTestUtils().createTopic("apex-out-" + topicSuffix, 1, (short) 1);
102         sharedKafkaTestResource.getKafkaTestUtils().createTopic("apex-in-" + topicSuffix, 1, (short) 1);
103
104         final KafkaEventSubscriber subscriber =
105             new KafkaEventSubscriber("apex-out-" + topicSuffix, sharedKafkaTestResource);
106
107         await().atMost(30, TimeUnit.SECONDS).until(() -> subscriber.isAlive());
108
109         final ApexMain apexMain = new ApexMain(args);
110         await().atMost(10, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
111
112         long initWaitEndTIme = System.currentTimeMillis() + 10000;
113
114         await().atMost(12, TimeUnit.SECONDS).until(() -> initWaitEndTIme < System.currentTimeMillis());
115
116         final KafkaEventProducer producer = new KafkaEventProducer("apex-in-" + topicSuffix, sharedKafkaTestResource,
117             EVENT_COUNT, xmlEvents, EVENT_INTERVAL);
118
119         await().atMost(30, TimeUnit.SECONDS).until(() -> producer.isAlive());
120
121         producer.sendEvents();
122
123         // Wait for the producer to send all its events
124         await().atMost(MAX_TEST_LENGTH, TimeUnit.MILLISECONDS)
125             .until(() -> producer.getEventsSentCount() >= EVENT_COUNT);
126
127         await().atMost(MAX_TEST_LENGTH, TimeUnit.MILLISECONDS)
128             .until(() -> subscriber.getEventsReceivedCount() >= EVENT_COUNT);
129
130         apexMain.shutdown();
131         await().atMost(30, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
132
133         subscriber.shutdown();
134         await().atMost(30, TimeUnit.SECONDS).until(() -> !subscriber.isAlive());
135
136         producer.shutdown();
137         await().atMost(30, TimeUnit.SECONDS).until(() -> !producer.isAlive());
138
139         assertEquals(producer.getEventsSentCount(), subscriber.getEventsReceivedCount());
140     }
141
142     private String getConditionedConfigFile(final String configurationFileName) {
143         try {
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());
149
150             return tempConfigFile.getCanonicalPath();
151         } catch (IOException e) {
152             fail("test should not throw an exception");
153             return null;
154         }
155     }
156 }