You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2021/03/12 20:39:04 UTC

[GitHub] [camel-kafka-connector] ffang opened a new pull request #1099: add itests for camel-cxfrs-kafka-connector #996

ffang opened a new pull request #1099:
URL: https://github.com/apache/camel-kafka-connector/pull/1099


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel-kafka-connector] ffang commented on a change in pull request #1099: add itests for camel-cxfrs-kafka-connector #996

Posted by GitBox <gi...@apache.org>.
ffang commented on a change in pull request #1099:
URL: https://github.com/apache/camel-kafka-connector/pull/1099#discussion_r595476225



##########
File path: tests/itests-cxfrs/src/test/java/org/apache/camel/kafkaconnector/cxfrs/source/CamelSourceCXFRSITCase.java
##########
@@ -0,0 +1,331 @@
+/*
+ * 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.cxfrs.source;
+
+import java.util.concurrent.ExecutionException;
+
+import javax.servlet.ServletRequest;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.component.cxf.common.message.CxfConstants;
+import org.apache.camel.kafkaconnector.common.AbstractKafkaTest;
+import org.apache.camel.kafkaconnector.common.ConnectorPropertyFactory;
+import org.apache.camel.kafkaconnector.common.clients.kafka.KafkaClient;
+import org.apache.camel.kafkaconnector.common.utils.NetworkUtils;
+import org.apache.camel.kafkaconnector.common.utils.TestUtils;
+import org.apache.camel.kafkaconnector.cxfrs.Customer;
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.ext.logging.LoggingInInterceptor;
+import org.apache.cxf.ext.logging.LoggingOutInterceptor;
+import org.apache.cxf.jaxrs.impl.ResponseImpl;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import static org.junit.Assert.assertTrue;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.fail;
+
+
+/**
+ * A simple test case that checks whether the CXF RS Consumer Endpoint produces the expected number of
+ * messages
+ */
+public class CamelSourceCXFRSITCase extends AbstractKafkaTest {
+    
+    protected static final int PORT = NetworkUtils.getFreePort("localhost");
+    protected static final String CXT = PORT + "/CxfRsConsumerTest";
+    protected static final String CXF_RS_ENDPOINT_ADDRESS = "http://localhost:" + CXT + "/rest";

Review comment:
       Thanks @orpiske for the feedbacks! I will revise accordingly.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel-kafka-connector] orpiske commented on a change in pull request #1099: add itests for camel-cxfrs-kafka-connector #996

Posted by GitBox <gi...@apache.org>.
orpiske commented on a change in pull request #1099:
URL: https://github.com/apache/camel-kafka-connector/pull/1099#discussion_r595217624



##########
File path: tests/itests-cxfrs/src/test/java/org/apache/camel/kafkaconnector/cxfrs/source/CamelSourceCXFRSITCase.java
##########
@@ -0,0 +1,331 @@
+/*
+ * 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.cxfrs.source;
+
+import java.util.concurrent.ExecutionException;
+
+import javax.servlet.ServletRequest;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.component.cxf.common.message.CxfConstants;
+import org.apache.camel.kafkaconnector.common.AbstractKafkaTest;
+import org.apache.camel.kafkaconnector.common.ConnectorPropertyFactory;
+import org.apache.camel.kafkaconnector.common.clients.kafka.KafkaClient;
+import org.apache.camel.kafkaconnector.common.utils.NetworkUtils;
+import org.apache.camel.kafkaconnector.common.utils.TestUtils;
+import org.apache.camel.kafkaconnector.cxfrs.Customer;
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.ext.logging.LoggingInInterceptor;
+import org.apache.cxf.ext.logging.LoggingOutInterceptor;
+import org.apache.cxf.jaxrs.impl.ResponseImpl;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import static org.junit.Assert.assertTrue;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.fail;
+
+
+/**
+ * A simple test case that checks whether the CXF RS Consumer Endpoint produces the expected number of
+ * messages
+ */
+public class CamelSourceCXFRSITCase extends AbstractKafkaTest {

Review comment:
       I'd try using the CamelSourceTestSupport class as the base class for the tests, as it abstracts away some of the setup, test logic and tear down complexity.

##########
File path: tests/itests-cxfrs/src/test/java/org/apache/camel/kafkaconnector/cxfrs/source/CamelSourceCXFRSITCase.java
##########
@@ -0,0 +1,331 @@
+/*
+ * 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.cxfrs.source;
+
+import java.util.concurrent.ExecutionException;
+
+import javax.servlet.ServletRequest;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.component.cxf.common.message.CxfConstants;
+import org.apache.camel.kafkaconnector.common.AbstractKafkaTest;
+import org.apache.camel.kafkaconnector.common.ConnectorPropertyFactory;
+import org.apache.camel.kafkaconnector.common.clients.kafka.KafkaClient;
+import org.apache.camel.kafkaconnector.common.utils.NetworkUtils;
+import org.apache.camel.kafkaconnector.common.utils.TestUtils;
+import org.apache.camel.kafkaconnector.cxfrs.Customer;
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.ext.logging.LoggingInInterceptor;
+import org.apache.cxf.ext.logging.LoggingOutInterceptor;
+import org.apache.cxf.jaxrs.impl.ResponseImpl;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import static org.junit.Assert.assertTrue;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.fail;
+
+
+/**
+ * A simple test case that checks whether the CXF RS Consumer Endpoint produces the expected number of
+ * messages
+ */
+public class CamelSourceCXFRSITCase extends AbstractKafkaTest {
+    
+    protected static final int PORT = NetworkUtils.getFreePort("localhost");
+    protected static final String CXT = PORT + "/CxfRsConsumerTest";
+    protected static final String CXF_RS_ENDPOINT_ADDRESS = "http://localhost:" + CXT + "/rest";
+    protected static final String CXF_RS_ENDPOINT_URI =  CXF_RS_ENDPOINT_ADDRESS
+        + "?resourceClasses=org.apache.camel.kafkaconnector.cxfrs.CustomerServiceResource";
+
+    
+            
+    private static String[] receivedValue = {"[126]", "[123]", "[400]"};
+
+    
+    private static final Logger LOG = LoggerFactory.getLogger(CamelSourceCXFRSITCase.class);
+
+    private int received;
+    private final int expect = 3;
+    
+
+    
+    @Override
+    protected String[] getConnectorsInTest() {
+        return new String[] {"camel-cxfrs-kafka-connector"};
+    }
+
+    @BeforeEach
+    public void setUp() {
+        received = 0;
+        
+    }
+
+    private <T> boolean checkRecord(ConsumerRecord<String, Object> record) {
+        LOG.debug("Received: {}", record.value());
+        assertEquals(receivedValue[received], record.value());
+        received++;
+
+        if (received == expect) {
+            return false;
+        }
+
+        return true;
+    }
+
+    
+    private <T> boolean checkRecordWithProcessor(ConsumerRecord<String, Object> record) {
+        LOG.debug("Received: {}", record.value());
+        
+        switch (received) {
+            case 0:
+                assertTrue(((String)record.value()).startsWith("org.apache.camel.kafkaconnector.cxfrs.Customer"));
+                break;
+            case 1:
+            case 2:    
+                assertTrue(((String)record.value()).startsWith("org.apache.cxf.jaxrs.impl.ResponseImpl"));
+                break;
+            
+        }
+        received++;
+
+        if (received == expect) {
+            return false;
+        }
+
+        return true;
+    }
+
+
+
+    public void runBasicStringTest(ConnectorPropertyFactory connectorPropertyFactory, boolean withProcessor) throws ExecutionException, InterruptedException {
+        connectorPropertyFactory.log();
+Bus bus = BusFactory.newInstance().createBus();
+        
+        bus.getInInterceptors().add(new LoggingInInterceptor());
+        bus.getOutInterceptors().add(new LoggingOutInterceptor());
+        getKafkaConnectService().initializeConnector(connectorPropertyFactory);
+        getKafkaConnectService().initializeConnectorBlocking(connectorPropertyFactory, 1);
+        
+        
+        Thread.sleep(5000);
+        try {
+            doTestGetCustomer("rest", withProcessor);
+        } catch (Exception e) {
+            LOG.info("Test Invocation Failure", e);
+        }
+        
+        
+        LOG.debug("Creating the consumer ...");
+        KafkaClient<String, Object> kafkaClient = new KafkaClient<>(getKafkaService().getBootstrapServers());
+        if (!withProcessor) {
+            kafkaClient.consume(TestUtils.getDefaultTestTopic(this.getClass()), this::checkRecord);
+        } else {
+            kafkaClient.consume(TestUtils.getDefaultTestTopic(this.getClass()), this::checkRecordWithProcessor);
+        }
+        LOG.debug("Created the consumer ...");
+
+        assertEquals(received, expect, "Didn't process the expected amount of messages");
+    }
+
+    
+
+    @Test
+    @Timeout(2000)
+    public void testBasicSendReceive() {
+        try {
+            ConnectorPropertyFactory connectorPropertyFactory = CamelSourceCXFRSPropertyFactory
+                    .basic()
+                    .withKafkaTopic(TestUtils.getDefaultTestTopic(this.getClass()))
+                    .withAddress(CXF_RS_ENDPOINT_ADDRESS)
+                    .withResourceClass("org.apache.camel.kafkaconnector.cxfrs.CustomerServiceResource");
+                  
+                                        
+
+            runBasicStringTest(connectorPropertyFactory, false);
+        } catch (Exception e) {
+            LOG.error("CXF test failed: {}", e.getMessage(), e);
+            fail(e.getMessage());
+        }
+    }
+    
+    @Test
+    @Timeout(2000)
+    public void testBasicSendReceiveWithoutProcessor() {
+        try {
+            ConnectorPropertyFactory connectorPropertyFactory = CamelSourceCXFRSPropertyFactory
+                    .basic()
+                    .withKafkaTopic(TestUtils.getDefaultTestTopic(this.getClass()))
+                    .withAddress(CXF_RS_ENDPOINT_ADDRESS)
+                    .withResourceClass("org.apache.camel.kafkaconnector.cxfrs.CustomerServiceResource");
+                    
+                                        
+
+            runBasicStringTest(connectorPropertyFactory, false);
+        } catch (Exception e) {
+            LOG.error("CXF test failed: {}", e.getMessage(), e);
+            fail(e.getMessage());
+        }
+    }
+    
+    @Test
+    @Timeout(20)
+    public void testBasicSendReceiveUsingUrl() {
+        try {
+            ConnectorPropertyFactory connectorPropertyFactory = CamelSourceCXFRSPropertyFactory
+                    .basic()
+                    .withKafkaTopic(TestUtils.getDefaultTestTopic(this.getClass()))
+                    .withUrl(CXF_RS_ENDPOINT_URI).buildUrl();
+                    
+                    
+
+            runBasicStringTest(connectorPropertyFactory, false);
+        } catch (Exception e) {
+            LOG.error("CXF test failed: {}", e.getMessage(), e);
+            fail(e.getMessage());
+        }
+    }
+
+    
+    
+    private void invokeGetCustomer(String uri, String expect, boolean withProcessor) throws Exception {
+        HttpGet get = new HttpGet(uri);
+        get.addHeader("Accept", "application/json");
+        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
+
+        try {
+            HttpResponse response = httpclient.execute(get);
+            if (withProcessor) {
+                assertEquals(200, response.getStatusLine().getStatusCode());
+                assertEquals(expect,
+                         EntityUtils.toString(response.getEntity()));
+            }
+        } finally {
+            httpclient.close();
+        }
+    }
+
+    private void doTestGetCustomer(String contextUri, boolean withProcessor) throws Exception {
+        invokeGetCustomer("http://localhost:" + CXT + "/" + contextUri + "/customerservice/customers/126",
+                          "{\"Customer\":{\"id\":126,\"name\":\"CKC\"}}",
+                          withProcessor);
+        invokeGetCustomer("http://localhost:" + CXT + "/" + contextUri + "/customerservice/customers/123",
+                          "customer response back!",
+                          withProcessor);
+        invokeGetCustomer("http://localhost:" + CXT + "/" + contextUri + "/customerservice/customers/400",
+            "The remoteAddress is 127.0.0.1",
+            withProcessor);
+
+    }
+
+
+    public abstract static class AbstractTestProcessor implements Processor {

Review comment:
       I'd probably look into moving these processing code to a separate Camel-based test service (in a similar way to the one we have in the syslog tests, for example). That way, we can potentially reuse more test code when adding new scenarios to this.

##########
File path: tests/itests-cxfrs/src/test/java/org/apache/camel/kafkaconnector/cxfrs/source/CamelSourceCXFRSITCase.java
##########
@@ -0,0 +1,331 @@
+/*
+ * 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.cxfrs.source;
+
+import java.util.concurrent.ExecutionException;
+
+import javax.servlet.ServletRequest;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.component.cxf.common.message.CxfConstants;
+import org.apache.camel.kafkaconnector.common.AbstractKafkaTest;
+import org.apache.camel.kafkaconnector.common.ConnectorPropertyFactory;
+import org.apache.camel.kafkaconnector.common.clients.kafka.KafkaClient;
+import org.apache.camel.kafkaconnector.common.utils.NetworkUtils;
+import org.apache.camel.kafkaconnector.common.utils.TestUtils;
+import org.apache.camel.kafkaconnector.cxfrs.Customer;
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.ext.logging.LoggingInInterceptor;
+import org.apache.cxf.ext.logging.LoggingOutInterceptor;
+import org.apache.cxf.jaxrs.impl.ResponseImpl;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import static org.junit.Assert.assertTrue;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.fail;
+
+
+/**
+ * A simple test case that checks whether the CXF RS Consumer Endpoint produces the expected number of
+ * messages
+ */
+public class CamelSourceCXFRSITCase extends AbstractKafkaTest {
+    
+    protected static final int PORT = NetworkUtils.getFreePort("localhost");
+    protected static final String CXT = PORT + "/CxfRsConsumerTest";
+    protected static final String CXF_RS_ENDPOINT_ADDRESS = "http://localhost:" + CXT + "/rest";

Review comment:
       I'd use the `NetworkUtils.getLocalHostname()` method to avoid problems when running these on misconfigured hosts (we did saw some problems with false positives on Github actions due to this).




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel-kafka-connector] ffang merged pull request #1099: add itests for camel-cxfrs-kafka-connector #996

Posted by GitBox <gi...@apache.org>.
ffang merged pull request #1099:
URL: https://github.com/apache/camel-kafka-connector/pull/1099


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org