Skip to content

Commit

Permalink
Upgrade to Spring Boot 3.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sivaprasadreddy committed Dec 1, 2023
1 parent 6df0c4c commit 4a7ae57
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 147 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup Java
uses: actions/setup-java@v3
Expand Down
2 changes: 1 addition & 1 deletion .sdkmanrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
java=17.0.4-tem
java=17.0.8-tem
maven=3.8.6
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ Configure the *testcontainers-jooq-codegen-maven-plugin* in `pom.xml` as follows
[source,xml]
----
<properties>
<testcontainers.version>1.18.3</testcontainers.version>
<testcontainers-jooq-codegen-maven-plugin.version>0.0.2</testcontainers-jooq-codegen-maven-plugin.version>
<testcontainers.version>1.19.3</testcontainers.version>
<testcontainers-jooq-codegen-maven-plugin.version>0.0.3</testcontainers-jooq-codegen-maven-plugin.version>
<jooq.version>3.18.3</jooq.version>
<postgresql.version>42.6.0</postgresql.version>
</properties>
Expand Down Expand Up @@ -124,7 +124,7 @@ Configure the *testcontainers-jooq-codegen-maven-plugin* in `pom.xml` as follows
<configuration>
<database>
<type>POSTGRES</type>
<containerImage>postgres:15.3-alpine</containerImage>
<containerImage>postgres:16-alpine</containerImage>
</database>
<flyway>
<locations>
Expand Down Expand Up @@ -155,7 +155,7 @@ Configure the *testcontainers-jooq-codegen-maven-plugin* in `pom.xml` as follows
Let's understand the plugin configuration.

* As we are using PostgreSQL database, we have configured the postgres JDBC driver and Testcontainers postgresql libraries as dependencies of the plugin.
* Under `<configuration>/<database>` section, we have configured the type of the database, POSTGRES, that we want to use it for our code generation, and specified the Docker image name, `postgres:15.3-alpine`, which will be used to create the database instance.
* Under `<configuration>/<database>` section, we have configured the type of the database, POSTGRES, that we want to use it for our code generation, and specified the Docker image name, `postgres:16-alpine`, which will be used to create the database instance.
* Under `<configuration>/<flyway>` section we have specified the location of Flyway migration scripts path.
* We have also configured the *packageName* and *target* location for the generated code. You can configure all the configuration options supported by the official *jooq-code-generator* plugin.

Expand Down
23 changes: 16 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.0</version>
<version>3.2.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.testcontainers</groupId>
Expand All @@ -17,7 +17,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
<testcontainers-jooq-codegen-maven-plugin.version>0.0.2</testcontainers-jooq-codegen-maven-plugin.version>
<testcontainers-jooq-codegen-maven-plugin.version>0.0.3</testcontainers-jooq-codegen-maven-plugin.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -89,7 +89,7 @@
<configuration>
<database>
<type>POSTGRES</type>
<containerImage>postgres:15.3-alpine</containerImage>
<containerImage>postgres:16-alpine</containerImage>
</database>
<flyway>
<locations>
Expand All @@ -116,14 +116,23 @@
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.37.0</version>
<version>2.41.0</version>
<configuration>
<java>
<importOrder />
<removeUnusedImports />
<palantirJavaFormat>
<version>2.30.0</version>
</palantirJavaFormat>
<prettier>
<devDependencies>
<prettier>3.0.3</prettier>
<prettier-plugin-java>2.3.0</prettier-plugin-java>
</devDependencies>
<config>
<parser>java</parser>
<tabWidth>2</tabWidth>
<printWidth>80</printWidth>
<plugins>prettier-plugin-java</plugins>
</config>
</prettier>
<formatAnnotations />
</java>
</configuration>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/testcontainers/demo/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
8 changes: 7 additions & 1 deletion src/main/java/com/testcontainers/demo/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

import java.time.LocalDateTime;

public record Comment(Long id, String name, String content, LocalDateTime createdAt, LocalDateTime updatedAt) {}
public record Comment(
Long id,
String name,
String content,
LocalDateTime createdAt,
LocalDateTime updatedAt
) {}
15 changes: 8 additions & 7 deletions src/main/java/com/testcontainers/demo/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import java.util.List;

public record Post(
Long id,
String title,
String content,
User createdBy,
List<Comment> comments,
LocalDateTime createdAt,
LocalDateTime updatedAt) {}
Long id,
String title,
String content,
User createdBy,
List<Comment> comments,
LocalDateTime createdAt,
LocalDateTime updatedAt
) {}
62 changes: 33 additions & 29 deletions src/main/java/com/testcontainers/demo/domain/PostRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,39 @@

@Repository
class PostRepository {
private final DSLContext dsl;

PostRepository(DSLContext dsl) {
this.dsl = dsl;
}
private final DSLContext dsl;

public Optional<Post> getPostById(Long id) {
return this.dsl
.select(
POSTS.ID,
POSTS.TITLE,
POSTS.CONTENT,
row(POSTS.users().ID, POSTS.users().NAME, POSTS.users().EMAIL)
.mapping(User::new)
.as("createdBy"),
multiset(select(
COMMENTS.ID,
COMMENTS.NAME,
COMMENTS.CONTENT,
COMMENTS.CREATED_AT,
COMMENTS.UPDATED_AT)
.from(COMMENTS)
.where(POSTS.ID.eq(COMMENTS.POST_ID)))
.as("comments")
.convertFrom(r -> r.map(mapping(Comment::new))),
POSTS.CREATED_AT,
POSTS.UPDATED_AT)
.from(POSTS)
.where(POSTS.ID.eq(id))
.fetchOptional(mapping(Post::new));
}
PostRepository(DSLContext dsl) {
this.dsl = dsl;
}

public Optional<Post> getPostById(Long id) {
return this.dsl.select(
POSTS.ID,
POSTS.TITLE,
POSTS.CONTENT,
row(POSTS.users().ID, POSTS.users().NAME, POSTS.users().EMAIL)
.mapping(User::new)
.as("createdBy"),
multiset(
select(
COMMENTS.ID,
COMMENTS.NAME,
COMMENTS.CONTENT,
COMMENTS.CREATED_AT,
COMMENTS.UPDATED_AT
)
.from(COMMENTS)
.where(POSTS.ID.eq(COMMENTS.POST_ID))
)
.as("comments")
.convertFrom(r -> r.map(mapping(Comment::new))),
POSTS.CREATED_AT,
POSTS.UPDATED_AT
)
.from(POSTS)
.where(POSTS.ID.eq(id))
.fetchOptional(mapping(Post::new));
}
}
39 changes: 19 additions & 20 deletions src/main/java/com/testcontainers/demo/domain/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,26 @@

@Repository
class UserRepository {
private final DSLContext dsl;

UserRepository(DSLContext dsl) {
this.dsl = dsl;
}
private final DSLContext dsl;

public User createUser(User user) {
return this.dsl
.insertInto(USERS)
.set(USERS.NAME, user.name())
.set(USERS.EMAIL, user.email())
.set(USERS.CREATED_AT, LocalDateTime.now())
.returningResult(USERS.ID, USERS.NAME, USERS.EMAIL)
.fetchOne(mapping(User::new));
}
UserRepository(DSLContext dsl) {
this.dsl = dsl;
}

public Optional<User> getUserByEmail(String email) {
return this.dsl
.select(USERS.ID, USERS.NAME, USERS.EMAIL)
.from(USERS)
.where(USERS.EMAIL.equalIgnoreCase(email))
.fetchOptional(mapping(User::new));
}
public User createUser(User user) {
return this.dsl.insertInto(USERS)
.set(USERS.NAME, user.name())
.set(USERS.EMAIL, user.email())
.set(USERS.CREATED_AT, LocalDateTime.now())
.returningResult(USERS.ID, USERS.NAME, USERS.EMAIL)
.fetchOne(mapping(User::new));
}

public Optional<User> getUserByEmail(String email) {
return this.dsl.select(USERS.ID, USERS.NAME, USERS.EMAIL)
.from(USERS)
.where(USERS.EMAIL.equalIgnoreCase(email))
.fetchOptional(mapping(User::new));
}
}
12 changes: 6 additions & 6 deletions src/test/java/com/testcontainers/demo/ContainersConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
@TestConfiguration(proxyBeanMethods = false)
public class ContainersConfig {

@Bean
@ServiceConnection
@RestartScope
PostgreSQLContainer<?> postgreSQLContainer() {
return new PostgreSQLContainer<>("postgres:15.3-alpine");
}
@Bean
@ServiceConnection
@RestartScope
PostgreSQLContainer<?> postgreSQLContainer() {
return new PostgreSQLContainer<>("postgres:16-alpine");
}
}
10 changes: 7 additions & 3 deletions src/test/java/com/testcontainers/demo/TestApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import org.springframework.boot.SpringApplication;

public class TestApplication {
public static void main(String[] args) {
SpringApplication.from(Application::main).with(ContainersConfig.class).run(args);
}

public static void main(String[] args) {
SpringApplication
.from(Application::main)
.with(ContainersConfig.class)
.run(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,27 @@
import org.springframework.test.context.jdbc.Sql;

@SpringBootTest(
properties = {"spring.test.database.replace=none", "spring.datasource.url=jdbc:tc:postgresql:15.3-alpine:///db"
})
properties = {
"spring.test.database.replace=none",
"spring.datasource.url=jdbc:tc:postgresql:16-alpine:///db",
}
)
@Sql("/test-data.sql")
class PostRepositoryTest {

@Autowired
PostRepository repository;
@Autowired
PostRepository repository;

@Test
void shouldGetPostById() {
Post post = repository.getPostById(1L).orElseThrow();
@Test
void shouldGetPostById() {
Post post = repository.getPostById(1L).orElseThrow();

assertThat(post.id()).isEqualTo(1L);
assertThat(post.title()).isEqualTo("Post 1 Title");
assertThat(post.content()).isEqualTo("Post 1 content");
assertThat(post.createdBy().id()).isEqualTo(1L);
assertThat(post.createdBy().name()).isEqualTo("Siva");
assertThat(post.createdBy().email()).isEqualTo("[email protected]");
assertThat(post.comments()).hasSize(2);
}
assertThat(post.id()).isEqualTo(1L);
assertThat(post.title()).isEqualTo("Post 1 Title");
assertThat(post.content()).isEqualTo("Post 1 content");
assertThat(post.createdBy().id()).isEqualTo(1L);
assertThat(post.createdBy().name()).isEqualTo("Siva");
assertThat(post.createdBy().email()).isEqualTo("[email protected]");
assertThat(post.comments()).hasSize(2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,41 @@
import org.springframework.test.context.jdbc.Sql;

@JooqTest(
properties = {"spring.test.database.replace=none", "spring.datasource.url=jdbc:tc:postgresql:15.3-alpine:///db"
})
properties = {
"spring.test.database.replace=none",
"spring.datasource.url=jdbc:tc:postgresql:16-alpine:///db",
}
)
@Sql("/test-data.sql")
class UserRepositoryJooqTest {

@Autowired
DSLContext dsl;
@Autowired
DSLContext dsl;

UserRepository repository;
UserRepository repository;

@BeforeEach
void setUp() {
this.repository = new UserRepository(dsl);
}
@BeforeEach
void setUp() {
this.repository = new UserRepository(dsl);
}

@Test
void shouldCreateUserSuccessfully() {
User user = new User(null, "John", "[email protected]");
@Test
void shouldCreateUserSuccessfully() {
User user = new User(null, "John", "[email protected]");

User savedUser = repository.createUser(user);
User savedUser = repository.createUser(user);

assertThat(savedUser.id()).isNotNull();
assertThat(savedUser.name()).isEqualTo("John");
assertThat(savedUser.email()).isEqualTo("[email protected]");
}
assertThat(savedUser.id()).isNotNull();
assertThat(savedUser.name()).isEqualTo("John");
assertThat(savedUser.email()).isEqualTo("[email protected]");
}

@Test
void shouldGetUserByEmail() {
User user = repository.getUserByEmail("[email protected]").orElseThrow();
@Test
void shouldGetUserByEmail() {
User user = repository.getUserByEmail("[email protected]").orElseThrow();

assertThat(user.id()).isEqualTo(1L);
assertThat(user.name()).isEqualTo("Siva");
assertThat(user.email()).isEqualTo("[email protected]");
}
assertThat(user.id()).isEqualTo(1L);
assertThat(user.name()).isEqualTo("Siva");
assertThat(user.email()).isEqualTo("[email protected]");
}
}
Loading

0 comments on commit 4a7ae57

Please sign in to comment.