From c95dcbb07749b99ed448a5b4f4a182069631ab5f Mon Sep 17 00:00:00 2001 From: "alexandru.borza" Date: Sun, 25 Jun 2023 18:28:58 +0300 Subject: [PATCH] add live test --- .../spring-data-yugabytedb/pom.xml | 96 +++++++++---------- .../src/main/java/com/baeldung/Main.java | 34 +++---- .../src/main/java/com/baeldung/User.java | 45 ++++----- .../src/main/resources/docker-compose.yml | 13 +++ .../java/com/baeldung/YugabyteDBLiveTest.java | 37 +++++++ 5 files changed, 136 insertions(+), 89 deletions(-) create mode 100644 persistence-modules/spring-data-yugabytedb/src/main/resources/docker-compose.yml create mode 100644 persistence-modules/spring-data-yugabytedb/src/test/java/com/baeldung/YugabyteDBLiveTest.java diff --git a/persistence-modules/spring-data-yugabytedb/pom.xml b/persistence-modules/spring-data-yugabytedb/pom.xml index c1095a20ca..fb4e4c160d 100644 --- a/persistence-modules/spring-data-yugabytedb/pom.xml +++ b/persistence-modules/spring-data-yugabytedb/pom.xml @@ -1,55 +1,55 @@ - 4.0.0 - spring-data-yugabytedb - 1.0 - spring-data-yugabytedb - jar + xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + spring-data-yugabytedb + 1.0 + spring-data-yugabytedb + jar - - com.baeldung - parent-boot-2 - 0.0.1-SNAPSHOT - ../../parent-boot-2 - + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + - - - org.springframework.boot - spring-boot-starter-web - - - org.projectlombok - lombok - - - org.springframework - spring-test - - - org.springframework.boot - spring-boot-starter-test - test - - - org.postgresql - postgresql - - - org.springframework.boot - spring-boot-starter-data-jpa - - + + + org.springframework.boot + spring-boot-starter-web + + + org.projectlombok + lombok + + + org.springframework + spring-test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.postgresql + postgresql + + + org.springframework.boot + spring-boot-starter-data-jpa + + - - - - org.apache.maven.plugins - maven-surefire-plugin - - - + + + + org.apache.maven.plugins + maven-surefire-plugin + + + diff --git a/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/Main.java b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/Main.java index 27b087790a..8638528f6a 100644 --- a/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/Main.java +++ b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/Main.java @@ -8,25 +8,25 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Main implements CommandLineRunner { - @Autowired - private UserRepository userRepository; + @Autowired + private UserRepository userRepository; - public static void main(String[] args) { - SpringApplication.run(Main.class, args); - } + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + } - @Override - public void run(String... args) throws InterruptedException { + @Override + public void run(String... args) throws InterruptedException { - int iterationCount = 1_000; - int elementsPerIteration = 100; + int iterationCount = 1_000; + int elementsPerIteration = 100; - for (int i = 0; i < iterationCount; i++) { - for (long j = 0; j < elementsPerIteration; j++) { - User user = new User(); - userRepository.save(user); - } - Thread.sleep(1000); - } - } + for (int i = 0; i < iterationCount; i++) { + for (long j = 0; j < elementsPerIteration; j++) { + User user = new User(); + userRepository.save(user); + } + Thread.sleep(1000); + } + } } diff --git a/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/User.java b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/User.java index 278bc6c9ae..54bbf2617c 100644 --- a/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/User.java +++ b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/User.java @@ -11,34 +11,31 @@ import javax.persistence.Table; @Table(name = "users") public class User { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; - @Column - private String name; + @Column + private String name; - Long getId() { - return id; - } + public Long getId() { + return id; + } - void setId(Long id) { - this.id = id; - } + public void setId(Long id) { + this.id = id; + } - String getName() { - return name; - } + public String getName() { + return name; + } - void setName(String name) { - this.name = name; - } + public void setName(String name) { + this.name = name; + } - @Override - public String toString() { - return "User{" + - "id=" + id + - ", name='" + name + '\'' + - '}'; - } + @Override + public String toString() { + return "User{" + "id=" + id + ", name='" + name + '\'' + '}'; + } } diff --git a/persistence-modules/spring-data-yugabytedb/src/main/resources/docker-compose.yml b/persistence-modules/spring-data-yugabytedb/src/main/resources/docker-compose.yml new file mode 100644 index 0000000000..6d95320525 --- /dev/null +++ b/persistence-modules/spring-data-yugabytedb/src/main/resources/docker-compose.yml @@ -0,0 +1,13 @@ +version: '3' + +services: + yugabytedb: + image: yugabytedb/yugabyte:latest + container_name: yugabyte + user: root + ports: + - '5433:5433' + - '7000:7000' + - '9000:9000' + command: ["bin/yugabyted", "start", "--daemon=false"] + diff --git a/persistence-modules/spring-data-yugabytedb/src/test/java/com/baeldung/YugabyteDBLiveTest.java b/persistence-modules/spring-data-yugabytedb/src/test/java/com/baeldung/YugabyteDBLiveTest.java new file mode 100644 index 0000000000..8a9de9bab4 --- /dev/null +++ b/persistence-modules/spring-data-yugabytedb/src/test/java/com/baeldung/YugabyteDBLiveTest.java @@ -0,0 +1,37 @@ +package com.baeldung; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +/* + To run this test we need to run the databases first. + A dedicated docker-compose.yml file is located under the resources directory. + We can run it by simple executing `docker-compose up`. + */ +@SpringJUnitConfig +@SpringBootTest +@TestPropertySource("classpath:application.properties") +public class YugabyteDBLiveTest { + + @Autowired + private UserRepository userRepository; + + @Test + void givenTwoUsers_whenPersistUsingJPARepository_thenUserAreSaved() { + User user1 = new User(); + user1.setName("Alex"); + User user2 = new User(); + user2.setName("John"); + userRepository.save(user1); + userRepository.save(user2); + List allUsers = userRepository.findAll(); + assertEquals(2, allUsers.size()); + } +}