From 7cacb733895e3233dbbbcbbc86281b89bc51a5ec Mon Sep 17 00:00:00 2001 From: Graham Cox Date: Wed, 15 Feb 2023 07:16:43 +0000 Subject: [PATCH] BAEL-6210: Examples for RethinkDB article (#13481) --- persistence-modules/rethinkdb/pom.xml | 53 ++++++++ .../rethinkdb/InsertIntegrationTest.java | 62 ++++++++++ .../rethinkdb/QueryIntegrationTest.java | 81 ++++++++++++ .../rethinkdb/StreamingIntegrationTest.java | 117 ++++++++++++++++++ .../rethinkdb/TablesIntegrationTest.java | 39 ++++++ .../java/com/baeldung/rethinkdb/TestBase.java | 44 +++++++ .../rethinkdb/UpdateIntegrationTest.java | 55 ++++++++ 7 files changed, 451 insertions(+) create mode 100644 persistence-modules/rethinkdb/pom.xml create mode 100644 persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/InsertIntegrationTest.java create mode 100644 persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/QueryIntegrationTest.java create mode 100644 persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/StreamingIntegrationTest.java create mode 100644 persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/TablesIntegrationTest.java create mode 100644 persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/TestBase.java create mode 100644 persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/UpdateIntegrationTest.java diff --git a/persistence-modules/rethinkdb/pom.xml b/persistence-modules/rethinkdb/pom.xml new file mode 100644 index 0000000000..17c7036ed3 --- /dev/null +++ b/persistence-modules/rethinkdb/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + rethinkdb + rethinkdb + Code snippets for RethinkDB articles + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + com.rethinkdb + rethinkdb-driver + 2.4.4 + + + + org.projectlombok + lombok + provided + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + 17 + + + diff --git a/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/InsertIntegrationTest.java b/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/InsertIntegrationTest.java new file mode 100644 index 0000000000..244959d854 --- /dev/null +++ b/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/InsertIntegrationTest.java @@ -0,0 +1,62 @@ +package com.baeldung.rethinkdb; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static com.rethinkdb.RethinkDB.r; + +/** + * Some tests demonstrating inserting data. + */ +public class InsertIntegrationTest extends TestBase { + /** + * Create a table for the tests. + */ + @BeforeEach + public void createTable() { + r.db(DB_NAME).tableCreate(tableName).run(conn); + } + + /** + * Insert a single simple record into the database. + */ + @Test + public void insertSimpleRecord() { + r.db(DB_NAME).table(tableName) + .insert( + r.hashMap() + .with("name", "Baeldung") + ) + .run(conn); + } + + @Test + public void insertMap() { + r.db(DB_NAME).table(tableName) + .insert( + Map.of("name", "Baeldung") + ) + .run(conn); + } + + @Test + public void insertComplex() { + r.db(DB_NAME).table(tableName) + .insert( + r.hashMap() + .with("name", "Baeldung") + .with("articles", r.array( + r.hashMap() + .with("name", "String Interpolation in Java") + .with("url", "https://www.baeldung.com/java-string-interpolation"), + r.hashMap() + .with("name", "Access HTTPS REST Service Using Spring RestTemplate") + .with("url", "https://www.baeldung.com/spring-resttemplate-secure-https-service") + ) + ) + ) + .run(conn); + } +} diff --git a/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/QueryIntegrationTest.java b/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/QueryIntegrationTest.java new file mode 100644 index 0000000000..263dda9bc6 --- /dev/null +++ b/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/QueryIntegrationTest.java @@ -0,0 +1,81 @@ +package com.baeldung.rethinkdb; + +import com.rethinkdb.net.Result; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import static com.rethinkdb.RethinkDB.r; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Some tests demonstrating querying data. + */ +public class QueryIntegrationTest extends TestBase { + /** + * Create a table for the tests. + */ + @BeforeEach + public void createTable() { + r.db(DB_NAME).tableCreate(tableName).run(conn); + + r.db(DB_NAME).table(tableName) + .insert( + r.hashMap() + .with("id", "article1") + .with("name", "String Interpolation in Java") + .with("url", "https://www.baeldung.com/java-string-interpolation") + ).run(conn); + r.db(DB_NAME).table(tableName) + .insert( + r.hashMap() + .with("id", "article2") + .with("name", "Access HTTPS REST Service Using Spring RestTemplate") + .with("url", "https://www.baeldung.com/spring-resttemplate-secure-https-service") + ).run(conn); + } + + @Test + public void listAll() { + Result results = r.db(DB_NAME).table(tableName).run(conn, Map.class); + + // We can't ensure the order the results come back in. + Set expected = new HashSet<>(Set.of( + "String Interpolation in Java", + "Access HTTPS REST Service Using Spring RestTemplate" + )); + + for (Map result : results) { + assertTrue(expected.remove(result.get("name"))); + } + + assertTrue(expected.isEmpty()); + } + + @Test + public void listSome() { + Result results = r.db(DB_NAME) + .table(tableName) + .filter(r -> r.g("name").eq("String Interpolation in Java")) + .run(conn, Map.class); + + // We can't ensure the order the results come back in. + Set expected = Set.of("https://www.baeldung.com/java-string-interpolation"); + + assertEquals(expected, results.stream() + .map(r -> r.get("url")) + .collect(Collectors.toSet())); + } + + @Test + public void getByKey() { + Result results = r.db(DB_NAME).table(tableName).get("article1").run(conn, Map.class); + + assertEquals("String Interpolation in Java", results.first().get("name")); + } +} diff --git a/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/StreamingIntegrationTest.java b/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/StreamingIntegrationTest.java new file mode 100644 index 0000000000..4ca147cf68 --- /dev/null +++ b/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/StreamingIntegrationTest.java @@ -0,0 +1,117 @@ +package com.baeldung.rethinkdb; + +import com.rethinkdb.net.Result; +import org.junit.jupiter.api.Test; + +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import static com.rethinkdb.RethinkDB.r; + +/** + * Some tests demonstrating streaming live changes to data. + */ +public class StreamingIntegrationTest extends TestBase { + @Test + public void getLiveInserts() throws InterruptedException { + ExecutorService executorService = Executors.newCachedThreadPool(); + + r.db(DB_NAME).tableCreate(tableName).run(conn); + + r.db(DB_NAME).table(tableName).insert(r.hashMap().with("index", 0)).run(conn); + + executorService.submit(() -> { + Result cursor = r.db(DB_NAME).table(tableName).changes().run(conn, Map.class); + + cursor.stream().forEach(record -> System.out.println("Record: " + record)); + }); + + for (int i = 0; i < 10; ++i) { + r.db(DB_NAME).table(tableName).insert(r.hashMap().with("index", i)).run(conn); + TimeUnit.MILLISECONDS.sleep(100); + } + + executorService.shutdownNow(); + } + + @Test + public void getSomeLiveInserts() throws InterruptedException { + ExecutorService executorService = Executors.newCachedThreadPool(); + + r.db(DB_NAME).tableCreate(tableName).run(conn); + + r.db(DB_NAME).table(tableName).insert(r.hashMap().with("index", 0)).run(conn); + + executorService.submit(() -> { + Result cursor = r.db(DB_NAME).table(tableName) + .filter(r -> r.g("index").eq(5)) + .changes() + .run(conn, Map.class); + + cursor.stream().forEach(record -> System.out.println("Record: " + record)); + }); + + for (int i = 0; i < 10; ++i) { + r.db(DB_NAME).table(tableName).insert(r.hashMap().with("index", i)).run(conn); + TimeUnit.MILLISECONDS.sleep(100); + } + + executorService.shutdownNow(); + } + + @Test + public void getLiveUpdates() throws InterruptedException { + ExecutorService executorService = Executors.newCachedThreadPool(); + + r.db(DB_NAME).tableCreate(tableName).run(conn); + + r.db(DB_NAME).table(tableName).insert(r.hashMap().with("index", 0)).run(conn); + + executorService.submit(() -> { + Result cursor = r.db(DB_NAME).table(tableName).changes().run(conn, Map.class); + + cursor.stream().forEach(record -> System.out.println("Record: " + record)); + }); + + for (int i = 0; i < 10; ++i) { + r.db(DB_NAME).table(tableName).update(r.hashMap().with("index", i)).run(conn); + TimeUnit.MILLISECONDS.sleep(100); + } + + executorService.shutdownNow(); + } + + @Test + public void getLiveDeletes() throws InterruptedException { + ExecutorService executorService = Executors.newCachedThreadPool(); + + r.db(DB_NAME).tableCreate(tableName).run(conn); + + for (int i = 0; i < 10; ++i) { + r.db(DB_NAME).table(tableName).insert(r.hashMap().with("index", i)).run(conn); + } + + executorService.submit(() -> { + Result cursor = r.db(DB_NAME).table(tableName).changes().run(conn, Map.class); + + cursor.stream().forEach(record -> System.out.println("Record: " + record)); + }); + + r.db(DB_NAME).table(tableName) + .filter(r -> r.g("index").eq(1)) + .delete() + .run(conn); + r.db(DB_NAME).table(tableName) + .filter(r -> r.g("index").eq(3)) + .delete() + .run(conn); + r.db(DB_NAME).table(tableName) + .filter(r -> r.g("index").eq(5)) + .delete() + .run(conn); + + executorService.shutdownNow(); + } +} diff --git a/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/TablesIntegrationTest.java b/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/TablesIntegrationTest.java new file mode 100644 index 0000000000..d60e500373 --- /dev/null +++ b/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/TablesIntegrationTest.java @@ -0,0 +1,39 @@ +package com.baeldung.rethinkdb; + +import com.rethinkdb.gen.exc.ReqlOpFailedError; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static com.rethinkdb.RethinkDB.r; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Some tests demonstrating working with tables. + */ +public class TablesIntegrationTest extends TestBase { + + @Test + public void createTable() { + r.db(DB_NAME).tableCreate(tableName).run(conn); + } + + @Test + public void createTableTwice() { + r.db(DB_NAME).tableCreate(tableName).run(conn); + + Assertions.assertThrows(ReqlOpFailedError.class, () -> { + r.db(DB_NAME).tableCreate(tableName).run(conn); + }); + } + + @Test + public void listTables() { + r.db(DB_NAME).tableCreate(tableName).run(conn); + + List tables = r.db(DB_NAME).tableList().run(conn, List.class).first(); + + assertTrue(tables.contains(tableName)); + } +} diff --git a/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/TestBase.java b/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/TestBase.java new file mode 100644 index 0000000000..396f6655ea --- /dev/null +++ b/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/TestBase.java @@ -0,0 +1,44 @@ +package com.baeldung.rethinkdb; + +import com.rethinkdb.net.Connection; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + +import java.util.UUID; + +import static com.rethinkdb.RethinkDB.r; + +/** + * Base class for RethinkDB tests. + */ +public class TestBase { + /** The database name to work with */ + protected static final String DB_NAME = "test"; + + /** A randomly generated table name so they never collide */ + protected final String tableName = UUID.randomUUID().toString().replaceAll("-",""); + + /** A database connection */ + protected Connection conn; + + /** + * Connect to the database for each test + */ + @BeforeEach + public void connect() { + conn = r.connection() + .hostname("localhost") + .port(28015) + .connect(); + } + + /** + * Disconnect from the database after each test + */ + @AfterEach + public void disconnect() { + if (this.conn != null) { + conn.close(); + } + } +} diff --git a/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/UpdateIntegrationTest.java b/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/UpdateIntegrationTest.java new file mode 100644 index 0000000000..39fad3a878 --- /dev/null +++ b/persistence-modules/rethinkdb/src/test/java/com/baeldung/rethinkdb/UpdateIntegrationTest.java @@ -0,0 +1,55 @@ +package com.baeldung.rethinkdb; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static com.rethinkdb.RethinkDB.r; + +/** + * Some tests demonstrating updating data. + */ +public class UpdateIntegrationTest extends TestBase { + /** + * Create a table for the tests. + */ + @BeforeEach + public void createTable() { + r.db(DB_NAME).tableCreate(tableName).run(conn); + + r.db(DB_NAME).table(tableName) + .insert( + r.hashMap() + .with("id", "article1") + .with("name", "String Interpolation in Java") + .with("url", "https://www.baeldung.com/java-string-interpolation") + ).run(conn); + r.db(DB_NAME).table(tableName) + .insert( + r.hashMap() + .with("id", "article2") + .with("name", "Access HTTPS REST Service Using Spring RestTemplate") + .with("url", "https://www.baeldung.com/spring-resttemplate-secure-https-service") + ).run(conn); + } + + @Test + public void updateAll() { + r.db(DB_NAME).table(tableName).update(r.hashMap().with("site", "Baeldung")).run(conn); + } + + @Test + public void updateSome() { + r.db(DB_NAME).table(tableName) + .filter(r -> r.g("name").eq("String Interpolation in Java")) + .update(r.hashMap().with("category", "java")) + .run(conn); + } + + @Test + public void delete() { + r.db(DB_NAME).table(tableName) + .filter(r -> r.g("name").eq("String Interpolation in Java")) + .delete() + .run(conn); + } +}