BAEL-6073 added scylladb code (#13307)
* BAEL-6073 added scylladb code * BAEL-6073 renamed test class name
This commit is contained in:
parent
0fbdb1198a
commit
63b714f00f
|
@ -0,0 +1,85 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.examples.scylladb</groupId>
|
||||
<artifactId>scylladb</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>scylladb</name>
|
||||
<description>Sample ScyllaDB Project</description>
|
||||
<properties>
|
||||
<testcontainers.version>1.17.6</testcontainers.version>
|
||||
</properties>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.scylladb</groupId>
|
||||
<artifactId>java-driver-core</artifactId>
|
||||
<version>4.14.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.scylladb</groupId>
|
||||
<artifactId>java-driver-query-builder</artifactId>
|
||||
<version>4.14.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>testcontainers</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>testcontainers-bom</artifactId>
|
||||
<version>${testcontainers.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,89 @@
|
|||
package com.baeldung.scylladb;
|
||||
|
||||
import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.insertInto;
|
||||
import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.literal;
|
||||
import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.selectFrom;
|
||||
import static com.datastax.oss.driver.api.querybuilder.SchemaBuilder.createKeyspace;
|
||||
import static com.datastax.oss.driver.api.querybuilder.SchemaBuilder.createTable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.scylladb.model.User;
|
||||
import com.datastax.oss.driver.api.core.CqlSession;
|
||||
import com.datastax.oss.driver.api.core.cql.ResultSet;
|
||||
import com.datastax.oss.driver.api.core.cql.Row;
|
||||
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
|
||||
import com.datastax.oss.driver.api.core.type.DataTypes;
|
||||
import com.datastax.oss.driver.api.querybuilder.insert.InsertInto;
|
||||
import com.datastax.oss.driver.api.querybuilder.schema.CreateKeyspaceStart;
|
||||
import com.datastax.oss.driver.api.querybuilder.schema.CreateTableStart;
|
||||
import com.datastax.oss.driver.api.querybuilder.select.Select;
|
||||
|
||||
public class ScylladbApplication {
|
||||
|
||||
private String keySpaceName;
|
||||
private String tableName;
|
||||
|
||||
public ScylladbApplication(String keySpaceName, String tableName) {
|
||||
this.keySpaceName = keySpaceName;
|
||||
this.tableName = tableName;
|
||||
CreateKeyspaceStart createKeyspace = createKeyspace(keySpaceName);
|
||||
SimpleStatement keypaceStatement = createKeyspace.ifNotExists()
|
||||
.withSimpleStrategy(3)
|
||||
.build();
|
||||
|
||||
CreateTableStart createTable = createTable(keySpaceName, tableName);
|
||||
SimpleStatement tableStatement = createTable.ifNotExists()
|
||||
.withPartitionKey("id", DataTypes.BIGINT)
|
||||
.withColumn("name", DataTypes.TEXT)
|
||||
.build();
|
||||
|
||||
try (CqlSession session = CqlSession.builder().build()) {
|
||||
ResultSet rs = session.execute(keypaceStatement);
|
||||
if (null == rs.getExecutionInfo().getErrors() || rs.getExecutionInfo().getErrors().isEmpty()) {
|
||||
rs = session.execute(tableStatement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getAllUserNames() {
|
||||
List<String> userNames = new ArrayList<>();
|
||||
try (CqlSession session = CqlSession.builder().build()) {
|
||||
String query = String.format("select * from %s.%s",keySpaceName,tableName);
|
||||
ResultSet rs = session.execute(query);
|
||||
for (Row r : rs.all())
|
||||
userNames.add(r.getString("name"));
|
||||
}
|
||||
return userNames;
|
||||
}
|
||||
|
||||
public List<User> getUsersByUserName(String userName) {
|
||||
List<User> userList = new ArrayList<>();
|
||||
try (CqlSession session = CqlSession.builder().build()) {
|
||||
Select query = selectFrom(keySpaceName, tableName).all()
|
||||
.whereColumn("name")
|
||||
.isEqualTo(literal(userName))
|
||||
.allowFiltering();
|
||||
SimpleStatement statement = query.build();
|
||||
ResultSet rs = session.execute(statement);
|
||||
for (Row r : rs)
|
||||
userList.add(new User(r.getLong("id"), r.getString("name")));
|
||||
}
|
||||
return userList;
|
||||
}
|
||||
|
||||
public boolean addNewUser(User user) {
|
||||
boolean response = false;
|
||||
try (CqlSession session = CqlSession.builder().build()) {
|
||||
InsertInto insert = insertInto(keySpaceName, tableName);
|
||||
SimpleStatement statement = insert.value("id", literal(user.getId()))
|
||||
.value("name", literal(user.getName()))
|
||||
.build();
|
||||
ResultSet rs = session.execute(statement);
|
||||
response = null == rs.getExecutionInfo().getErrors() || rs.getExecutionInfo().getErrors().isEmpty();
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.scylladb.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class User {
|
||||
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
datastax-java-driver:
|
||||
basic:
|
||||
contact-points: 127.0.0.1:9042
|
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.scylladb;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
|
||||
import com.baeldung.scylladb.model.User;
|
||||
import com.github.dockerjava.api.command.CreateContainerCmd;
|
||||
import com.github.dockerjava.api.model.ExposedPort;
|
||||
import com.github.dockerjava.api.model.PortBinding;
|
||||
import com.github.dockerjava.api.model.Ports;
|
||||
|
||||
@Testcontainers
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
public class ScyllaDBApplicationLiveTest {
|
||||
|
||||
private static final String IMAGE_NAME = "scylladb/scylla";
|
||||
private static final int hostPort = 9042;
|
||||
private static final int containerExposedPort = 9042;
|
||||
private static Consumer<CreateContainerCmd> cmd = e -> e.withPortBindings(new PortBinding(Ports.Binding.bindPort(hostPort),
|
||||
new ExposedPort(containerExposedPort)));
|
||||
|
||||
@Container
|
||||
private static final GenericContainer scyllaDbContainer = new GenericContainer(DockerImageName.parse(IMAGE_NAME))
|
||||
.withExposedPorts(containerExposedPort)
|
||||
.withCreateContainerCmdModifier(cmd);
|
||||
|
||||
private ScylladbApplication scylladbApplication;
|
||||
|
||||
@BeforeAll
|
||||
void setUp() {
|
||||
scylladbApplication = new ScylladbApplication("baeldung", "User");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenKeySpaceAndTable_whenInsertData_thenShouldBeAbleToFindData() {
|
||||
User user = new User(10, "John");
|
||||
scylladbApplication.addNewUser(user);
|
||||
|
||||
List<User> userList = scylladbApplication.getUsersByUserName("John");
|
||||
assertEquals(1, userList.size());
|
||||
assertEquals("John", userList.get(0).getName());
|
||||
assertEquals(10, userList.get(0).getId());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenKeySpaceAndTable_whenInsertData_thenRowCountIncreases() {
|
||||
int initialCount = scylladbApplication.getAllUserNames().size();
|
||||
User user = new User(11, "Doe");
|
||||
scylladbApplication.addNewUser(user);
|
||||
|
||||
int expectedCount = initialCount+1;
|
||||
int updatedCount = scylladbApplication.getAllUserNames().size();
|
||||
assertEquals(expectedCount, updatedCount);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
datastax-java-driver:
|
||||
basic:
|
||||
contact-points: 127.0.0.1:9042
|
Loading…
Reference in New Issue