You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by hu...@apache.org on 2021/04/08 09:18:07 UTC

[camel-quarkus] 04/04: couchbase JVM : add integration tests fixes #2326 (#2327)

This is an automated email from the ASF dual-hosted git repository.

humbedooh pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git

commit ddf4d9fd2d05ec302deb95354da4063f8138b50a
Author: Zineb BENDHIBA <be...@gmail.com>
AuthorDate: Thu Apr 8 09:13:26 2021 +0200

    couchbase JVM : add integration tests fixes #2326 (#2327)
---
 extensions-jvm/couchbase/integration-test/pom.xml  |  15 +++
 .../component/couchbase/it/CouchbaseResource.java  |  71 ++++++++++--
 ...CouchbaseTest.java => CouchbaseDeleteTest.java} |  29 ++++-
 .../{CouchbaseTest.java => CouchbasePollTest.java} |  25 +++--
 .../couchbase/it/CouchbaseTestResource.java        | 123 +++++++++++++++++++++
 .../couchbase/it/CouchbaseUpdateTest.java          |  55 +++++++++
 poms/bom-test/pom.xml                              |   5 +
 7 files changed, 299 insertions(+), 24 deletions(-)

diff --git a/extensions-jvm/couchbase/integration-test/pom.xml b/extensions-jvm/couchbase/integration-test/pom.xml
index f9bb400..67caa34 100644
--- a/extensions-jvm/couchbase/integration-test/pom.xml
+++ b/extensions-jvm/couchbase/integration-test/pom.xml
@@ -51,6 +51,21 @@
             <artifactId>rest-assured</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-integration-testcontainers-support</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.testcontainers</groupId>
+            <artifactId>couchbase</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.awaitility</groupId>
+            <artifactId>awaitility</artifactId>
+            <scope>test</scope>
+        </dependency>
 
         <!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory -->
         <dependency>
diff --git a/extensions-jvm/couchbase/integration-test/src/main/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseResource.java b/extensions-jvm/couchbase/integration-test/src/main/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseResource.java
index 15fd254..118727e 100644
--- a/extensions-jvm/couchbase/integration-test/src/main/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseResource.java
+++ b/extensions-jvm/couchbase/integration-test/src/main/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseResource.java
@@ -18,34 +18,81 @@ package org.apache.camel.quarkus.component.couchbase.it;
 
 import javax.enterprise.context.ApplicationScoped;
 import javax.inject.Inject;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
+import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
 
-import org.apache.camel.CamelContext;
+import com.couchbase.client.java.kv.GetResult;
+import org.apache.camel.ConsumerTemplate;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.component.couchbase.CouchbaseConstants;
+import org.eclipse.microprofile.config.inject.ConfigProperty;
 import org.jboss.logging.Logger;
 
+import static org.apache.camel.component.couchbase.CouchbaseConstants.COUCHBASE_DELETE;
+import static org.apache.camel.component.couchbase.CouchbaseConstants.COUCHBASE_GET;
+
 @Path("/couchbase")
 @ApplicationScoped
+@Consumes(MediaType.TEXT_PLAIN)
 public class CouchbaseResource {
 
     private static final Logger LOG = Logger.getLogger(CouchbaseResource.class);
 
-    private static final String COMPONENT_COUCHBASE = "couchbase";
     @Inject
-    CamelContext context;
+    ProducerTemplate producerTemplate;
+    @Inject
+    ConsumerTemplate consumerTemplate;
+
+    @ConfigProperty(name = "couchbase.connection.uri")
+    String connectionUri;
+    @ConfigProperty(name = "couchbase.bucket.name")
+    String bucketName;
+    @ConfigProperty(name = "couchbase.timeout", defaultValue = "120000")
+    long timeout;
+
+    @PUT
+    @Path("id/{id}")
+    @Produces(MediaType.TEXT_PLAIN)
+    public boolean insert(@PathParam("id") String id, String msg) {
+        LOG.infof("inserting message %s with id %s", msg, id);
+        String endpoint = String.format("%s&queryTimeout=%s", connectionUri, timeout);
+        return producerTemplate.requestBodyAndHeader(endpoint, msg, CouchbaseConstants.HEADER_ID, id, Boolean.class);
+    }
+
+    @GET
+    @Path("{id}")
+    @Produces(MediaType.TEXT_PLAIN)
+    public String getById(@PathParam("id") String id) {
+        LOG.infof("Getting object with id : %s", id);
+        String endpoint = String.format("%s&operation=%s&queryTimeout=%s", connectionUri, COUCHBASE_GET, timeout);
+        GetResult result = producerTemplate.requestBodyAndHeader(endpoint, null, CouchbaseConstants.HEADER_ID, id,
+                GetResult.class);
+        return result != null ? result.contentAs(String.class) : null;
+    }
+
+    @DELETE
+    @Path("{id}")
+    @Produces(MediaType.TEXT_PLAIN)
+    public boolean delete(@PathParam("id") String id) {
+        LOG.infof("Deleting object with id : %s", id);
+        String endpoint = String.format("%s&operation=%s&queryTimeout=%s", connectionUri, COUCHBASE_DELETE, timeout);
+        producerTemplate.sendBodyAndHeader(endpoint, null, CouchbaseConstants.HEADER_ID, id);
+        return true;
+    }
 
-    @Path("/load/component/couchbase")
     @GET
+    @Path("poll")
     @Produces(MediaType.TEXT_PLAIN)
-    public Response loadComponentCouchbase() throws Exception {
-        /* This is an autogenerated test */
-        if (context.getComponent(COMPONENT_COUCHBASE) != null) {
-            return Response.ok().build();
-        }
-        LOG.warnf("Could not load [%s] from the Camel context", COMPONENT_COUCHBASE);
-        return Response.status(500, COMPONENT_COUCHBASE + " could not be loaded from the Camel context").build();
+    public String poll() {
+        LOG.infof("polling one document");
+        String endpoint = String.format("%s&designDocumentName=%s&viewName=%s&limit=1", connectionUri, bucketName, bucketName);
+        GetResult result = consumerTemplate.receiveBody(endpoint, timeout, GetResult.class);
+        return result != null ? result.contentAs(String.class) : null;
     }
 }
diff --git a/extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseTest.java b/extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseDeleteTest.java
similarity index 56%
copy from extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseTest.java
copy to extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseDeleteTest.java
index c98dadb..1c7c02f 100644
--- a/extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseTest.java
+++ b/extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseDeleteTest.java
@@ -16,19 +16,38 @@
  */
 package org.apache.camel.quarkus.component.couchbase.it;
 
+import io.quarkus.test.common.QuarkusTestResource;
+import io.quarkus.test.common.http.TestHTTPEndpoint;
 import io.quarkus.test.junit.QuarkusTest;
-import io.restassured.RestAssured;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.CoreMatchers.equalTo;
 
 @QuarkusTest
-class CouchbaseTest {
+@TestHTTPEndpoint(CouchbaseResource.class)
+@QuarkusTestResource(CouchbaseTestResource.class)
+@DisabledIfEnvironmentVariable(named = "CI", matches = "true")
+class CouchbaseDeleteTest {
 
     @Test
-    public void loadComponentCouchbase() {
-        /* A simple autogenerated test */
-        RestAssured.get("/couchbase/load/component/couchbase")
+    void testDelete() {
+        // getting the document
+        given()
+                .when()
+                .get("/DocumentID_1")
+                .then()
+                .statusCode(200)
+                .body(equalTo("hello1"));
+
+        // deleting the document
+        given()
+                .when()
+                .delete("DocumentID_1")
                 .then()
                 .statusCode(200);
+
     }
 
 }
diff --git a/extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseTest.java b/extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbasePollTest.java
similarity index 58%
rename from extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseTest.java
rename to extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbasePollTest.java
index c98dadb..f9efba4 100644
--- a/extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseTest.java
+++ b/extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbasePollTest.java
@@ -16,19 +16,30 @@
  */
 package org.apache.camel.quarkus.component.couchbase.it;
 
+import io.quarkus.test.common.QuarkusTestResource;
+import io.quarkus.test.common.http.TestHTTPEndpoint;
 import io.quarkus.test.junit.QuarkusTest;
-import io.restassured.RestAssured;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.CoreMatchers.equalTo;
 
 @QuarkusTest
-class CouchbaseTest {
+@TestHTTPEndpoint(CouchbaseResource.class)
+@QuarkusTestResource(CouchbaseTestResource.class)
+@DisabledIfEnvironmentVariable(named = "CI", matches = "true")
+public class CouchbasePollTest {
 
     @Test
-    public void loadComponentCouchbase() {
-        /* A simple autogenerated test */
-        RestAssured.get("/couchbase/load/component/couchbase")
+    void testPoll() {
+        // poll the first document
+        String message = "hello0";
+        given()
+                .when()
+                .get("/poll")
                 .then()
-                .statusCode(200);
+                .statusCode(200)
+                .body(equalTo(message));
     }
-
 }
diff --git a/extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseTestResource.java b/extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseTestResource.java
new file mode 100644
index 0000000..bd3177d
--- /dev/null
+++ b/extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseTestResource.java
@@ -0,0 +1,123 @@
+/*
+ * 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.quarkus.component.couchbase.it;
+
+import java.time.Duration;
+import java.util.Collections;
+import java.util.Map;
+
+import com.couchbase.client.java.Cluster;
+import com.couchbase.client.java.manager.bucket.BucketSettings;
+import com.couchbase.client.java.manager.bucket.BucketType;
+import com.couchbase.client.java.manager.view.DesignDocument;
+import com.couchbase.client.java.manager.view.View;
+import com.couchbase.client.java.view.DesignDocumentNamespace;
+import org.apache.camel.quarkus.testcontainers.ContainerResourceLifecycleManager;
+import org.apache.camel.util.CollectionHelper;
+import org.testcontainers.couchbase.CouchbaseContainer;
+import org.testcontainers.utility.DockerImageName;
+
+public class CouchbaseTestResource implements ContainerResourceLifecycleManager {
+    private final static DockerImageName COUCHBASE_IMAGE = DockerImageName.parse("couchbase/server:6.5.1");
+    public static final int KV_PORT = 11210;
+    public static final int MANAGEMENT_PORT = 8091;
+    public static final int VIEW_PORT = 8092;
+    public static final int QUERY_PORT = 8093;
+    public static final int SEARCH_PORT = 8094;
+    protected String bucketName = "testBucket";
+
+    private CustomCouchbaseContainer container;
+    protected Cluster cluster;
+
+    private class CustomCouchbaseContainer extends CouchbaseContainer {
+        public CustomCouchbaseContainer() {
+            super(COUCHBASE_IMAGE);
+
+            addFixedExposedPort(KV_PORT, KV_PORT);
+            addFixedExposedPort(MANAGEMENT_PORT, MANAGEMENT_PORT);
+            addFixedExposedPort(VIEW_PORT, VIEW_PORT);
+            addFixedExposedPort(QUERY_PORT, QUERY_PORT);
+            addFixedExposedPort(SEARCH_PORT, SEARCH_PORT);
+        }
+    }
+
+    @Override
+    public Map<String, String> start() {
+        container = new CustomCouchbaseContainer();
+        container.start();
+
+        initBucket();
+
+        return CollectionHelper.mapOf("couchbase.connection.uri", getConnectionUri(),
+                "couchbase.bucket.name", bucketName);
+    }
+
+    @Override
+    public void stop() {
+        if (cluster != null) {
+            cluster.buckets().dropBucket(bucketName);
+            cluster.disconnect();
+        }
+        if (container != null) {
+            container.stop();
+        }
+    }
+
+    public String getConnectionUri() {
+        return String.format("couchbase:http://%s:%d?bucket=%s&username=%s&password=%s", getHostname(),
+                getPort(), bucketName, getUsername(), getPassword());
+    }
+
+    public String getUsername() {
+        return container.getUsername();
+    }
+
+    public String getPassword() {
+        return container.getPassword();
+    }
+
+    public String getHostname() {
+        return container.getHost();
+    }
+
+    public int getPort() {
+        return container.getBootstrapHttpDirectPort();
+    }
+
+    private void initBucket() {
+        cluster = Cluster.connect(container.getConnectionString(), this.getUsername(), this.getPassword());
+
+        cluster.buckets().createBucket(
+                BucketSettings.create(bucketName).bucketType(BucketType.COUCHBASE).flushEnabled(true));
+
+        cluster.bucket(bucketName);
+        DesignDocument designDoc = new DesignDocument(
+                bucketName,
+                Collections.singletonMap(bucketName, new View("function (doc, meta) {  emit(meta.id, doc);}")));
+        cluster.bucket(bucketName).viewIndexes().upsertDesignDocument(designDoc, DesignDocumentNamespace.PRODUCTION);
+        // wait for cluster
+        cluster.bucket(bucketName).waitUntilReady(Duration.ofSeconds(30));
+
+        // insert some documents
+        for (int i = 0; i < 3; i++) {
+            cluster.bucket(bucketName).defaultCollection().upsert("DocumentID_" + i, "hello" + i);
+        }
+
+        // wait for cluster
+        cluster.bucket(bucketName).waitUntilReady(Duration.ofSeconds(30));
+    }
+}
diff --git a/extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseUpdateTest.java b/extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseUpdateTest.java
new file mode 100644
index 0000000..d8ea416
--- /dev/null
+++ b/extensions-jvm/couchbase/integration-test/src/test/java/org/apache/camel/quarkus/component/couchbase/it/CouchbaseUpdateTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.quarkus.component.couchbase.it;
+
+import io.quarkus.test.common.QuarkusTestResource;
+import io.quarkus.test.common.http.TestHTTPEndpoint;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.http.ContentType;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.CoreMatchers.equalTo;
+
+@QuarkusTest
+@TestHTTPEndpoint(CouchbaseResource.class)
+@QuarkusTestResource(CouchbaseTestResource.class)
+@DisabledIfEnvironmentVariable(named = "CI", matches = "true")
+public class CouchbaseUpdateTest {
+
+    @Test
+    void testUpdate() {
+        // updating the document
+        given()
+                .contentType(ContentType.TEXT)
+                .body("updating hello2")
+                .when()
+                .put("/id/DocumentID_2")
+                .then()
+                .statusCode(200)
+                .body(equalTo("true"));
+
+        // check the result of update
+        given()
+                .when()
+                .get("/DocumentID_2")
+                .then()
+                .statusCode(200)
+                .body(equalTo("updating hello2"));
+    }
+}
diff --git a/poms/bom-test/pom.xml b/poms/bom-test/pom.xml
index 9dd6e36..f7c94f7 100644
--- a/poms/bom-test/pom.xml
+++ b/poms/bom-test/pom.xml
@@ -169,6 +169,11 @@
             </dependency>
             <dependency>
                 <groupId>org.testcontainers</groupId>
+                <artifactId>couchbase</artifactId>
+                <version>${testcontainers.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.testcontainers</groupId>
                 <artifactId>localstack</artifactId>
                 <version>${testcontainers.version}</version>
             </dependency>