Skip to content

Commit bac594e

Browse files
BAEL-8756: Added unit test for Quarkus Panache (#18574)
* BAEL-8756: Added unit test for Quarkus Panache Signed-off-by: Diego Torres <[email protected]> * BAEL-8756: Fixed method names and added reference to parent pom Signed-off-by: Diego Torres <[email protected]> * BAEL-8756: Reference from parent pom Signed-off-by: Diego Torres <[email protected]> * BAEL-8756: Fixed unit test Signed-off-by: Diego Torres <[email protected]> * BAEL-8756: Added support to use docker test containers Signed-off-by: Diego Torres <[email protected]> * BAEL-8756: Fixed unit tests to use H2 instead of postgres containers Signed-off-by: Diego Torres <[email protected]> * BAEL-8756: Fixed unit tests Signed-off-by: Diego Torres <[email protected]> * BAEL-8756: Removed unused drivers Signed-off-by: Diego Torres <[email protected]> * BAEL-8756: Fixed H2 dependency Signed-off-by: Diego Torres <[email protected]> --------- Signed-off-by: Diego Torres <[email protected]>
1 parent 525d236 commit bac594e

File tree

8 files changed

+291
-0
lines changed

8 files changed

+291
-0
lines changed

quarkus-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<!-- <module>quarkus-rbac</module> --> <!-- JAVA-42048 -->
3434
<module>quarkus-websockets-next</module>
3535
<module>quarkus-management-interface</module>
36+
<module>quarkus-panache</module>
3637
</modules>
3738

3839
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#Maven
2+
target/
3+
pom.xml.tag
4+
pom.xml.releaseBackup
5+
pom.xml.versionsBackup
6+
release.properties
7+
.flattened-pom.xml
8+
9+
# Eclipse
10+
.project
11+
.classpath
12+
.settings/
13+
bin/
14+
15+
# IntelliJ
16+
.idea
17+
*.ipr
18+
*.iml
19+
*.iws
20+
21+
# NetBeans
22+
nb-configuration.xml
23+
24+
# Visual Studio Code
25+
.vscode
26+
.factorypath
27+
28+
# OSX
29+
.DS_Store
30+
31+
# Vim
32+
*.swp
33+
*.swo
34+
35+
# patch
36+
*.orig
37+
*.rej
38+
39+
# Local environment
40+
.env
41+
42+
# Plugin directory
43+
/.quarkus/cli/plugins/
44+
# TLS Certificates
45+
.certs/
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung.quarkus</groupId>
5+
<artifactId>quarkus-panache</artifactId>
6+
<version>1.0.0-SNAPSHOT</version>
7+
8+
<properties>
9+
<compiler-plugin.version>3.14.0</compiler-plugin.version>
10+
<maven.compiler.release>17</maven.compiler.release>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
13+
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
14+
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
15+
<quarkus.platform.version>3.22.3</quarkus.platform.version>
16+
<skipITs>true</skipITs>
17+
<surefire-plugin.version>3.5.2</surefire-plugin.version>
18+
</properties>
19+
20+
<dependencyManagement>
21+
<dependencies>
22+
<dependency>
23+
<groupId>${quarkus.platform.group-id}</groupId>
24+
<artifactId>${quarkus.platform.artifact-id}</artifactId>
25+
<version>${quarkus.platform.version}</version>
26+
<type>pom</type>
27+
<scope>import</scope>
28+
</dependency>
29+
</dependencies>
30+
</dependencyManagement>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>io.quarkus</groupId>
35+
<artifactId>quarkus-hibernate-orm-rest-data-panache</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>io.quarkus</groupId>
40+
<artifactId>quarkus-arc</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>io.quarkus</groupId>
45+
<artifactId>quarkus-jdbc-h2</artifactId>
46+
</dependency>
47+
48+
<!-- Quarkus test dependency -->
49+
<dependency>
50+
<groupId>io.quarkus</groupId>
51+
<artifactId>quarkus-junit5</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
</dependencies>
55+
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>${quarkus.platform.group-id}</groupId>
60+
<artifactId>quarkus-maven-plugin</artifactId>
61+
<version>${quarkus.platform.version}</version>
62+
<extensions>true</extensions>
63+
<executions>
64+
<execution>
65+
<goals>
66+
<goal>build</goal>
67+
<goal>generate-code</goal>
68+
<goal>generate-code-tests</goal>
69+
<goal>native-image-agent</goal>
70+
</goals>
71+
</execution>
72+
</executions>
73+
</plugin>
74+
<plugin>
75+
<artifactId>maven-compiler-plugin</artifactId>
76+
<version>${compiler-plugin.version}</version>
77+
<configuration>
78+
<parameters>true</parameters>
79+
</configuration>
80+
</plugin>
81+
<plugin>
82+
<artifactId>maven-surefire-plugin</artifactId>
83+
<version>${surefire-plugin.version}</version>
84+
<configuration>
85+
<systemPropertyVariables>
86+
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
87+
<maven.home>${maven.home}</maven.home>
88+
</systemPropertyVariables>
89+
</configuration>
90+
</plugin>
91+
<plugin>
92+
<artifactId>maven-failsafe-plugin</artifactId>
93+
<version>${surefire-plugin.version}</version>
94+
<executions>
95+
<execution>
96+
<goals>
97+
<goal>integration-test</goal>
98+
<goal>verify</goal>
99+
</goals>
100+
</execution>
101+
</executions>
102+
<configuration>
103+
<systemPropertyVariables>
104+
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
105+
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
106+
<maven.home>${maven.home}</maven.home>
107+
</systemPropertyVariables>
108+
</configuration>
109+
</plugin>
110+
</plugins>
111+
</build>
112+
113+
<profiles>
114+
<profile>
115+
<id>native</id>
116+
<activation>
117+
<property>
118+
<name>native</name>
119+
</property>
120+
</activation>
121+
<properties>
122+
<skipITs>false</skipITs>
123+
<quarkus.native.enabled>true</quarkus.native.enabled>
124+
</properties>
125+
</profile>
126+
</profiles>
127+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.quarkus;
2+
3+
import io.quarkus.hibernate.orm.panache.PanacheEntity;
4+
import jakarta.persistence.Entity;
5+
6+
@Entity
7+
public class Article extends PanacheEntity {
8+
9+
public String title;
10+
public String content;
11+
public String status;
12+
13+
public Article() {
14+
}
15+
16+
public Article(String title, String content, String status) {
17+
this.title = title;
18+
this.content = content;
19+
this.status = status;
20+
}
21+
}
22+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.quarkus;
2+
3+
import io.quarkus.hibernate.orm.panache.PanacheRepository;
4+
5+
import jakarta.enterprise.context.ApplicationScoped;
6+
7+
import java.util.List;
8+
9+
@ApplicationScoped
10+
public class ArticleRepository implements PanacheRepository<Article> {
11+
12+
public Article findByTitle(String title) {
13+
return find("title", title).firstResult();
14+
}
15+
16+
public List<Article> findPublished() {
17+
return list("status", "Published");
18+
}
19+
20+
public void deleteDrafts() {
21+
delete("status", "Draft");
22+
}
23+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
quarkus.datasource.db-kind=h2
2+
quarkus.datasource.jdbc.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
3+
quarkus.datasource.username=sa
4+
quarkus.datasource.password=sa
5+
quarkus.hibernate-orm.database.generation=drop-and-create
6+
quarkus.hibernate-orm.log.sql=true
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.quarkus;
2+
3+
import io.quarkus.test.junit.QuarkusTest;
4+
import jakarta.transaction.Transactional;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.MethodOrderer;
7+
import org.junit.jupiter.api.Order;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.TestMethodOrder;
10+
11+
import java.util.List;
12+
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertNotNull;
15+
16+
@QuarkusTest
17+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
18+
public class ArticleUnitTest {
19+
20+
@BeforeEach
21+
@Transactional
22+
public void setup() {
23+
Article.deleteAll();
24+
}
25+
26+
@Test
27+
@Transactional
28+
@Order(1)
29+
public void givenNewArticle_whenPersisted_thenItShouldHaveIdAndBeCounted() {
30+
Article article = new Article("Quarkus Panache", "Content of the article", "Published");
31+
article.persist();
32+
33+
assertNotNull(article.id);
34+
assertEquals(1, Article.count());
35+
}
36+
37+
@Test
38+
@Transactional
39+
@Order(2)
40+
public void givenMultipleArticles_whenSearchedByTitle_thenMatchingArticlesShouldBeReturned() {
41+
Article.persist(new Article("Quarkus Panache", "Quarkus Panache is an extension for Hibernate", "Draft"));
42+
Article.persist(new Article("Postgresql with Quarkus ", "Integrate Quarkus with Postgresql", "Draft"));
43+
44+
List<Article> articles = Article.list("title", "Quarkus Panache");
45+
46+
assertEquals(1, articles.size());
47+
}
48+
49+
@Test
50+
@Order(3)
51+
@Transactional
52+
public void givenPersistedArticle_whenDeleted_thenItShouldBeRemovedFromCount() {
53+
Article article = new Article("Delete Me", "Soon gone", "Draft");
54+
article.persist();
55+
56+
assertEquals(1, Article.count());
57+
58+
article.delete();
59+
60+
assertEquals(0, Article.count());
61+
}
62+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
quarkus.datasource.db-kind=h2
2+
quarkus.datasource.jdbc.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
3+
quarkus.datasource.username=sa
4+
quarkus.datasource.password=sa
5+
quarkus.hibernate-orm.database.generation=drop-and-create

0 commit comments

Comments
 (0)