How to use OkHttpClient method of org.testcontainers.couchbase.CouchbaseContainer class

Best Testcontainers-java code snippet using org.testcontainers.couchbase.CouchbaseContainer.OkHttpClient

Source:CouchbaseContainer.java Github

copy

Full Screen

...20import com.github.dockerjava.api.model.ContainerNetwork;21import lombok.Cleanup;22import okhttp3.Credentials;23import okhttp3.FormBody;24import okhttp3.OkHttpClient;25import okhttp3.Request;26import okhttp3.RequestBody;27import okhttp3.Response;28import org.rnorth.ducttape.unreliables.Unreliables;29import org.testcontainers.containers.GenericContainer;30import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;31import org.testcontainers.containers.wait.strategy.WaitAllStrategy;32import org.testcontainers.utility.DockerImageName;33import java.io.IOException;34import java.util.ArrayList;35import java.util.Arrays;36import java.util.EnumSet;37import java.util.Iterator;38import java.util.List;39import java.util.Optional;40import java.util.Set;41import java.util.concurrent.TimeUnit;42import java.util.function.Predicate;43import java.util.stream.Collectors;44/**45 * The couchbase container initializes and configures a Couchbase Server single node cluster.46 * <p>47 * Note that it does not depend on a specific couchbase SDK, so it can be used with both the Java SDK 2 and 3 as well48 * as the Scala SDK 1 or newer. We recommend using the latest and greatest SDKs for the best experience.49 */50public class CouchbaseContainer extends GenericContainer<CouchbaseContainer> {51 private static final int MGMT_PORT = 8091;52 private static final int MGMT_SSL_PORT = 18091;53 private static final int VIEW_PORT = 8092;54 private static final int VIEW_SSL_PORT = 18092;55 private static final int QUERY_PORT = 8093;56 private static final int QUERY_SSL_PORT = 18093;57 private static final int SEARCH_PORT = 8094;58 private static final int SEARCH_SSL_PORT = 18094;59 private static final int ANALYTICS_PORT = 8095;60 private static final int ANALYTICS_SSL_PORT = 18095;61 private static final int KV_PORT = 11210;62 private static final int KV_SSL_PORT = 11207;63 private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("couchbase/server");64 private static final String DEFAULT_TAG = "6.5.1";65 private static final ObjectMapper MAPPER = new ObjectMapper();66 private static final OkHttpClient HTTP_CLIENT = new OkHttpClient();67 private String username = "Administrator";68 private String password = "password";69 /**70 * Enabled services does not include Analytics since most users likely do not need to test71 * with it and is also a little heavy on memory and runtime requirements. Also, it is only72 * available with the enterprise edition (EE).73 */74 private Set<CouchbaseService> enabledServices = EnumSet.of(75 CouchbaseService.KV,76 CouchbaseService.QUERY,77 CouchbaseService.SEARCH,78 CouchbaseService.INDEX79 );80 private final List<BucketDefinition> buckets = new ArrayList<>();...

Full Screen

Full Screen

Source:CouchbaseTransactionsTest.java Github

copy

Full Screen

...9import org.testcontainers.couchbase.CouchbaseContainer;10import org.testcontainers.couchbase.CouchbaseService;11import org.testcontainers.shaded.okhttp3.Credentials;12import org.testcontainers.shaded.okhttp3.FormBody;13import org.testcontainers.shaded.okhttp3.OkHttpClient;14import org.testcontainers.shaded.okhttp3.Request;15import org.testcontainers.utility.DockerImageName;16import java.io.IOException;17import java.lang.reflect.Method;18import java.time.Duration;19import static org.junit.jupiter.api.Assertions.assertNotNull;20public class CouchbaseTransactionsTest {21 public static final String COUCHBASE_SERVER_7_1_1 = "couchbase/server:7.1.1";22 public static final String MYBUCKET = "myBucket";23 public static final String MY_SCOPE = "myScope";24 public static final String MY_COLLECTION = "myCollection";25 /**26 * Test to demonstrate 3 problems with couchbase Transactions,27 * with the latest java client com.couchbase.client:java-client:3.3.328 * <p>29 * <p>30 * - Problem 1 (affects only testing):31 * See: "Problem 1 non-proper solution" in the code below32 * <p>33 * As we can see we cannot write test for Couchbase Transactions.34 * <p>35 * Fails with "DurabilityImpossibleException:36 * With the current cluster configuration, the requested durability guarantees are impossible"37 * <p>38 * A proper fix is impossible since there is no way to set Bucket.replicaNumber to zero (default = 1)39 * Also there is no way to set up a cluster of two servers that would also solve this problem.40 * <p>41 * Therefore, a proper solution is impossible.42 * <p>43 * <p>44 * <p>45 * - Problem 2 (affects production code):46 * See: "Problem 2 non-proper solution" in the code below47 * <p>48 * The `cas` value is not available in the returned objects inside transactions.49 * <p>50 * We had to use java reflection to locate and read the cas value.51 * We need the cas value in our use cases from an insert/replace calls.52 * (Use case: We return the cas value in our API for later updates with optimistic locking)53 * <p>54 * <p>55 * <p>56 * MOST IMPORTANT PROBLEM that prevent us for using the client:57 * Problem 3 (affects production code):58 * See: "Problem 3 There is no solution" in the code below.59 * <p>60 * After a simple insert inside a Transaction context, a HUGE amount (every 1ms) of logs appears.61 * <p>62 * Sample log:63 * [cb-events] INFO com.couchbase.transactions.cleanup.lost - [com.couchbase.transactions.cleanup.lost][LogEvent] Client fa245 stop on CollectionIdentifier{bucket='myBucket', scope=Optional[_default], collection=Optional[_default], isDefault=true} = false64 * <p>65 * As we can see, even if the log is `INFO` level has two basic problems that prevent us for using it:66 * 1. The "com.couchbase.transactions.cleanup.lost" seems like a problem. Probably something is lost.67 * 2. Huge amount of logs that might affect performance.68 */69 @Test70 void couchbaseTransactionsTest_demonstrateTheThreeProblems() throws IOException, InterruptedException {71 CouchbaseContainer container = new CouchbaseContainer(DockerImageName.parse(COUCHBASE_SERVER_7_1_1))72 .withEnabledServices(CouchbaseService.KV, CouchbaseService.QUERY, CouchbaseService.INDEX)73 .withStartupTimeout(Duration.ofSeconds(60));74 //.withBucket(bucketDefinition); No bucket is created here75 container.start();76 //TODO: Problem 1 non-proper solution. @Couchbase Please check77 createANewBucketUsingCouchbaseAdminRest(container, MYBUCKET);78 wait10Sec();79 Cluster cluster = Cluster.connect(80 container.getConnectionString(),81 container.getUsername(),82 container.getPassword()83 );84 cluster.bucket(MYBUCKET).collections().createScope(MY_SCOPE);85 wait10Sec();86 cluster.bucket(MYBUCKET).collections().createCollection(CollectionSpec.create(MY_COLLECTION, MY_SCOPE));87 wait10Sec();88 Collection collection = cluster.bucket(MYBUCKET).scope(MY_SCOPE).collection(MY_COLLECTION);89 cluster.transactions().run((ctx) -> {90 TransactionGetResult attemptContext = ctx.insert(collection, "doc2", JsonObject.create());91 try {92 Method internalMethod = TransactionGetResult.class.getDeclaredMethod("internal");93 internalMethod.setAccessible(true);94 //TODO: Problem 2 non-proper solution. @Couchbase Please check95 CoreTransactionGetResult internalResult = (CoreTransactionGetResult) internalMethod.invoke(attemptContext);96 long cas = internalResult.cas();97 System.out.println("-------> Cas value: " + cas);98 } catch (Exception ignore) {99 }100 });101 assertNotNull(collection.get("doc2"));102 wait10Sec();103 wait10Sec();104 wait10Sec();105 System.out.println("------> Please check the problematic logs above!");106 //TODO: Problem 3 There is no solution. @Couchbase Please check107 //Here, a HUGE amount (every 1ms) of logs appears. Just check the logs in the console.108 //Sample:109 //[cb-events] INFO com.couchbase.transactions.cleanup.lost - [com.couchbase.transactions.cleanup.lost][LogEvent] Client fa245 stop on CollectionIdentifier{bucket='myBucket', scope=Optional[_default], collection=Optional[_default], isDefault=true} = false110 }111 private static void wait10Sec() throws InterruptedException {112 Thread.sleep(10000);113 }114 /**115 * Mandatory if you want to test Transactions. Sets Bucket's replicaNumber to zero to avoid116 * DurabilityImpossibleException117 */118 private static void createANewBucketUsingCouchbaseAdminRest(CouchbaseContainer couchbaseContainer, String bucket) throws IOException {119 String createBucketRestEndpoint =120 "http://"121 + couchbaseContainer.getHost()122 + ":"123 + couchbaseContainer.getMappedPort(8091)124 + "/pools/default/buckets/";125 Request.Builder requestBuilder = new Request.Builder().url(createBucketRestEndpoint);126 requestBuilder =127 requestBuilder.header(128 "Authorization",129 Credentials.basic(couchbaseContainer.getUsername(), couchbaseContainer.getPassword()));130 requestBuilder =131 requestBuilder.method(132 "POST",133 new FormBody.Builder()134 .add("name", bucket)135 .add("ramQuotaMB", Integer.toString(100))136 .add("flushEnabled", "0")137 .add("replicaNumber", "0")138 .build());139 new OkHttpClient().newCall(requestBuilder.build()).execute();140 }141}...

Full Screen

Full Screen

OkHttpClient

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.couchbase.CouchbaseContainer;2import com.couchbase.client.java.Bucket;3import com.couchbase.client.java.Cluster;4import com.couchbase.client.java.CouchbaseCluster;5import com.couchbase.client.java.document.JsonDocument;6import com.couchbase.client.java.document.json.JsonObject;7import com.couchbase.client.java.query.N1qlQuery;8import com.couchbase.client.java.query.N1qlQueryResult;9import com.couchbase.client.java.query.N1qlQueryRow;10import java.util.List;11import okhttp3.OkHttpClient;12import okhttp3.Request;13import okhttp3.Response;14public class 1 {15 public static void main(String[] args) throws Exception {16 CouchbaseContainer couchbase = new CouchbaseContainer();17 couchbase.start();18 String couchbaseUrl = couchbase.getCouchbaseUrl();19 String bucketName = couchbase.getBucketName();20 String password = couchbase.getBucketPassword();21 Bucket bucket = getCouchbaseBucket(couchbaseUrl, bucketName, password);22 insertDocument(bucket);23 queryDocuments(bucket);24 couchbase.stop();25 }26 private static Bucket getCouchbaseBucket(String couchbaseUrl, String bucketName, String password) {27 Cluster cluster = CouchbaseCluster.create(couchbaseUrl);28 Bucket bucket = cluster.openBucket(bucketName, password);29 return bucket;30 }31 private static void insertDocument(Bucket bucket) {32 JsonObject content = JsonObject.empty().put("name", "John Doe");33 JsonDocument doc = JsonDocument.create("doc1", content);34 bucket.upsert(doc);35 }36 private static void queryDocuments(Bucket bucket) {37 N1qlQueryResult result = bucket.query(N1qlQuery.simple("SELECT name FROM `default` WHERE $1 IN name", "John"));38 List<N1qlQueryRow> rows = result.allRows();39 for (N1qlQueryRow row : rows) {40 System.out.println(row.value());41 }42 }43}44import org.testcontainers.couchbase.CouchbaseContainer;45import com.couchbase.client.java.Bucket;46import com.couchbase.client.java.Cluster;47import com.couchbase.client.java.CouchbaseCluster;48import com.couchbase.client.java.document.JsonDocument;49import com.couchbase.client

Full Screen

Full Screen

OkHttpClient

Using AI Code Generation

copy

Full Screen

1public class CouchbaseContainerTest {2 public static void main(String[] args) throws IOException {3 try (CouchbaseContainer couchbase = new CouchbaseContainer()) {4 couchbase.start();5 OkHttpClient client = couchbase.getOkHttpClient();6 Request request = new Request.Builder().url(couchbase.getHttpUrl()).build();7 Response response = client.newCall(request).execute();8 System.out.println(response.body().string());9 }10 }11}12public class CouchbaseContainerTest {13 public static void main(String[] args) throws IOException {14 try (CouchbaseContainer couchbase = new CouchbaseContainer()) {15 couchbase.start();16 String response = couchbase.doGETRequest("/");17 System.out.println(response);18 }19 }20}21public class CouchbaseContainerTest {22 public static void main(String[] args) throws IOException {23 try (CouchbaseContainer couchbase = new CouchbaseContainer()) {24 couchbase.start();25 CouchbaseClient client = couchbase.getCouchbaseClient();26 System.out.println(client);27 }28 }29}30public class CouchbaseContainerTest {31 public static void main(String[] args) throws IOException {32 try (CouchbaseContainer couchbase = new CouchbaseContainer()) {33 couchbase.start();34 CouchbaseCluster cluster = couchbase.getCouchbaseCluster();35 System.out.println(cluster);36 }37 }38}39public class CouchbaseContainerTest {40 public static void main(String[] args) throws IOException {41 try (CouchbaseContainer couchbase = new CouchbaseContainer()) {42 couchbase.start();43 Bucket bucket = couchbase.getBucket();44 System.out.println(bucket);45 }46 }47}48public class CouchbaseContainerTest {49 public static void main(String[] args) throws IOException {

Full Screen

Full Screen

OkHttpClient

Using AI Code Generation

copy

Full Screen

1public static void main(String[] args) {2 CouchbaseContainer couchbase = new CouchbaseContainer("couchbase:4.6.4")3 .withNewBucket(DefaultBucketSettings.builder()4 .enableFlush(true)5 .name("myBucket")6 .quota(100)7 .build());8 couchbase.start();9 OkHttpClient client = couchbase.newClient();10 client.newCall(new Request.Builder()11 .url(couchbase.getHttpUrl() + "/myBucket")12 .build())13 .execute();14 couchbase.stop();15}16public static void main(String[] args) {17 CouchbaseContainer couchbase = new CouchbaseContainer("couchbase:4.6.4")18 .withNewBucket(DefaultBucketSettings.builder()19 .enableFlush(true)20 .name("myBucket")21 .quota(100)22 .build());23 couchbase.start();24 Cluster cluster = couchbase.getCouchbaseCluster();25 cluster.authenticate("Administrator", "password");26 Bucket bucket = cluster.openBucket("myBucket");27 bucket.upsert(JsonDocument.create("myKey", JsonObject.create().put("foo", "bar")));28 couchbase.stop();29}30public static void main(String[] args) {31 CouchbaseContainer couchbase = new CouchbaseContainer("couchbase:4.6.4")32 .withNewBucket(DefaultBucketSettings.builder()33 .enableFlush(true)34 .name("myBucket")35 .quota(100)36 .build());37 couchbase.start();38 Cluster cluster = couchbase.getCouchbaseCluster();39 cluster.authenticate("Administrator", "password");40 Bucket bucket = cluster.openBucket("myBucket");41 JsonDocument document = bucket.get("myKey");42 System.out.println(document.content());43 couchbase.stop();44}45public static void main(String[] args) {46 CouchbaseContainer couchbase = new CouchbaseContainer("couchbase:4.6.4")47 .withNewBucket(DefaultBucketSettings.builder()

Full Screen

Full Screen

OkHttpClient

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.couchbase;2import com.couchbase.client.java.Bucket;3import com.couchbase.client.java.CouchbaseCluster;4import com.couchbase.client.java.cluster.ClusterManager;5import com.couchbase.client.java.cluster.DefaultBucketSettings;6import com.couchbase.client.java.cluster.UserSettings;7import com.couchbase.client.java.document.JsonDocument;8import com.couchbase.client.java.document.JsonDocumentCreateOptions;9import com.couchbase.client.java.document.json.JsonObject;10import com.couchbase.client.java.query.N1qlQuery;11import com.couchbase.client.java.query.N1qlQueryResult;12import com.couchbase.client.java.query.N1qlQueryRow;13import com.couchbase.client.java.query.N1qlQueryRow;14import com.couchbase.client.java.query.N1qlQueryResult;15import com.couchbase.client.java.query.N1qlQueryRow;16import com.couchbase.client.java.query.N1qlQueryResult;17import com.couchbase.client.java.query.N1qlQueryRow;18import com.couchbase.client.java.query.Statement;19import com.couchbase.client.java.query.dsl.Expression;20import com.couchbase.client.java.query.dsl.Sort;21import com.couchbase.client.java.query.dsl.path.DefaultLimitPath;22import com.couchbase.client.java.query.dsl.path.DefaultOrderByPath;23import com.couchbase.client.java.query.dsl.path.DefaultWherePath;24import com.couchbase.client.java.query.dsl.path.FromPath;25import com.couchbase.client.java.query.dsl.path.GroupByPath;26import com.couchbase.client.java.query.dsl.path.HavingPath;27import com.couchbase.client.java.query.dsl.path.JoinType;28import com.couchbase.client.java.query.dsl.path.LetPath;29import com.couchbase.client.java.query.dsl.path.LimitPath;30import com.couchbase.client.java.query.dsl.path.OffsetPath;31import com.couchbase.client.java.query.dsl.path.OrderByPath;32import com.couchbase.client.java.query.dsl.path.UnionPath;33import com.couchbase.client.java.query.dsl.path.WherePath;34import com.couchbase.client.java.query.dsl.path.WindowPath;35import com.couchbase.client.java.query.dsl.path.WithPath;36import com.couchbase.client.java.query.dsl.path.core.DefaultLetPath;37import com.couchbase.client.java.query.dsl.path.core.DefaultWindowPath;38import com.couchbase.client.java.query.dsl.path.core

Full Screen

Full Screen

OkHttpClient

Using AI Code Generation

copy

Full Screen

1import com.couchbase.client.java.Bucket;2import com.couchbase.client.java.CouchbaseCluster;3import com.couchbase.client.java.cluster.BucketSettings;4import com.couchbase.client.java.cluster.DefaultBucketSettings;5import com.couchbase.client.java.cluster.api.ClusterApiClient;6import com.couchbase.client.java.env.CouchbaseEnvironment;7import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;8import com.couchbase.client.java.query.AsyncN1qlQueryResult;9import com.couchbase.client.java.query.N1qlQuery;10import com.couchbase.client.java.query.N1qlQueryResult;11import com.couchbase.client.java.query.N1qlQueryRow;12import com.couchbase.client.java.query.dsl.Expression;13import com.couchbase.client.java.query.dsl.path.DefaultLimitPath;14import org.testcontainers.containers.CouchbaseContainer;15import java.util.List;16import java.util.concurrent.TimeUnit;17public class CreateBucket {18 public static void main(String[] args) {19 CouchbaseContainer couchbaseContainer = new CouchbaseContainer("couchbase:latest")20 .withClusterAdmin("Administrator", "password")21 .withNewBucket(DefaultBucketSettings.builder()22 .enableFlush(true)23 .name("testbucket")24 .quota(100)25 .build());26 couchbaseContainer.start();27 CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder()

Full Screen

Full Screen

OkHttpClient

Using AI Code Generation

copy

Full Screen

1import com.couchbase.client.java.Bucket;2import com.couchbase.client.java.document.JsonDocument;3import com.couchbase.client.java.query.N1qlQuery;4import com.couchbase.client.java.query.N1qlQueryResult;5import org.testcontainers.couchbase.CouchbaseContainer;6public class 1 {7 public static void main(String[] args) {8 CouchbaseContainer container = new CouchbaseContainer()9 .withBucketName("travel-sample")10 .withBucketPassword("password")11 .withNewBucket("travel-sample", 100, 100);12 container.start();13 Bucket bucket = container.getCouchbaseCluster().openBucket("travel-sample", "password");14 N1qlQueryResult result = container.getCouchbaseCluster().query(N1qlQuery.simple("SELECT * FROM `travel-sample` LIMIT 1"));15 System.out.println(result.allRows());16 container.stop();17 }18}19[{"travel-sample":{"id":"airline_10","cas":"0x0","type":"airline","name":"40-Mile Air","iata":"Q5","icao":"MLA","callsign":"MILE-AIR","country":"United States","active":"Y"}}]

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful