-
Notifications
You must be signed in to change notification settings - Fork 100
/
CamelSinkElasticSearchITCase.java
137 lines (115 loc) · 5.48 KB
/
CamelSinkElasticSearchITCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.kafkaconnector.elasticsearch.sink;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.camel.kafkaconnector.common.ConnectorPropertyFactory;
import org.apache.camel.kafkaconnector.common.test.CamelSinkTestSupport;
import org.apache.camel.kafkaconnector.elasticsearch.clients.ElasticSearchClient;
import org.apache.camel.kafkaconnector.elasticsearch.common.ElasticSearchCommon;
import org.apache.camel.kafkaconnector.elasticsearch.common.ElasticSearchIndexMessageProducer;
import org.apache.camel.test.infra.elasticsearch.services.ElasticSearchService;
import org.apache.camel.test.infra.elasticsearch.services.ElasticSearchServiceFactory;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
@Disabled("TODO: Wait for xxx to be released in the kamelet catalog before enabling")
@DisabledIfSystemProperty(named = "kafka.instance.type", matches = "local-(kafka|strimzi)-container",
disabledReason = "Hangs when running with the embedded Kafka Connect instance")
public class CamelSinkElasticSearchITCase extends CamelSinkTestSupport {
@RegisterExtension
public static ElasticSearchService elasticSearch = ElasticSearchServiceFactory.createService();
private static final Logger LOG = LoggerFactory.getLogger(CamelElasticSearchPropertyFactory.class);
private static final ObjectMapper MAPPER = new ObjectMapper();
private ElasticSearchClient client;
private String topicName;
private final int expect = 10;
private int received;
private final String transformKey = "index-test";
@Override
protected String[] getConnectorsInTest() {
return new String[] {"camel-elasticsearch-index-sink-kafka-connector"};
}
@BeforeEach
public void setUp() {
topicName = getTopicForTest(this);
client = new ElasticSearchClient(elasticSearch.getElasticSearchHost(), elasticSearch.getPort(),
ElasticSearchCommon.DEFAULT_ELASTICSEARCH_INDEX);
received = 0;
}
@Override
protected void consumeMessages(CountDownLatch latch) {
try {
client.waitForIndex();
LOG.debug("Waiting for data");
client.waitForData(expect);
} finally {
latch.countDown();
}
}
@Override
protected void verifyMessages(CountDownLatch latch) throws InterruptedException {
if (latch.await(30, TimeUnit.SECONDS)) {
SearchHits hits = client.getData();
assertNotNull(hits);
hits.forEach(this::verifyHit);
assertEquals(expect, received,
"Didn't process the expected amount of messages: " + received + " != " + expect);
} else {
fail("Failed to receive the messages within the specified time");
}
}
private void verifyHit(SearchHit searchHit) {
String source = searchHit.getSourceAsString();
LOG.debug("Search hit: {} ", source);
assertNotNull(source);
assertFalse(source.isEmpty());
try {
JsonNode rootNode = MAPPER.readTree(source);
assertEquals(String.valueOf(received), rootNode.at("/counter").asText());
} catch (JsonProcessingException e) {
LOG.error("Error in parsing json elasticsearch search hit answer: " + e.getMessage() + e.getCause());
fail();
}
received++;
}
@Test
@Timeout(90)
public void testIndexOperation() throws Exception {
ConnectorPropertyFactory propertyFactory = CamelElasticSearchPropertyFactory
.basic()
.withTopics(topicName)
.withClusterName(ElasticSearchCommon.DEFAULT_ELASTICSEARCH_CLUSTER)
.withHostAddress(elasticSearch.getHttpHostAddress())
.withIndexName(ElasticSearchCommon.DEFAULT_ELASTICSEARCH_INDEX);
runTest(propertyFactory, new ElasticSearchIndexMessageProducer(getKafkaService().getBootstrapServers(), topicName, expect));
}
}