Compare commits
2 Commits
e214b7781a
...
ac2fc687e1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ac2fc687e1 | ||
|
|
c5a5ca1f57 |
8
.idea/compiler.xml
generated
8
.idea/compiler.xml
generated
@ -6,6 +6,14 @@
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
</profile>
|
||||
<profile name="Annotation profile for namedquery" enabled="true">
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<processorPath useClasspath="false">
|
||||
<entry name="$MAVEN_REPOSITORY$/org/projectlombok/lombok/1.18.26/lombok-1.18.26.jar" />
|
||||
</processorPath>
|
||||
<module name="namedquery" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
|
||||
19
pom.xml
19
pom.xml
@ -55,6 +55,12 @@
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-arc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.26</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-junit5</artifactId>
|
||||
@ -65,6 +71,12 @@
|
||||
<artifactId>rest-assured</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- In-memory H2 for tests -->
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-jdbc-h2</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -90,6 +102,13 @@
|
||||
<version>${compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<parameters>true</parameters>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.26</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
|
||||
@ -6,6 +6,9 @@ import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Table(name = "flowers")
|
||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||
@DiscriminatorColumn(name = "flower_type")
|
||||
@DiscriminatorValue("FLOWER")
|
||||
@NamedQueries({
|
||||
@NamedQuery(name = "FlowerEntity.findByColor", query = "SELECT f FROM FlowerEntity f WHERE f.color = :color"),
|
||||
@NamedQuery(name = "FlowerEntity.countAll", query = "SELECT COUNT(f) FROM FlowerEntity f")
|
||||
@ -20,4 +23,34 @@ public class FlowerEntity {
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Color color;
|
||||
|
||||
// JPA requires a no-arg constructor
|
||||
public FlowerEntity() {
|
||||
}
|
||||
|
||||
// Convenience constructor for tests and usage
|
||||
public FlowerEntity(String name, Color color) {
|
||||
this.name = name;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,24 +2,20 @@ package dev.hinterdorfer;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.persistence.EntityManager;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@ApplicationScoped
|
||||
public class FlowerPersistance {
|
||||
@Inject
|
||||
private EntityManager em;
|
||||
private FlowerRepository repository;
|
||||
|
||||
public Collection<FlowerEntity> find (final Color color) {
|
||||
return em.createNamedQuery("FlowerEntity.findByColor", FlowerEntity.class)
|
||||
.setParameter("color", color)
|
||||
.getResultList();
|
||||
return repository.findByColor(color);
|
||||
}
|
||||
|
||||
public long count() {
|
||||
return em.createNamedQuery("FlowerEntity.countAll", Long.class)
|
||||
.getSingleResult();
|
||||
return repository.countAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
33
src/main/java/dev/hinterdorfer/FlowerRepository.java
Normal file
33
src/main/java/dev/hinterdorfer/FlowerRepository.java
Normal file
@ -0,0 +1,33 @@
|
||||
package dev.hinterdorfer;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import java.util.Collection;
|
||||
|
||||
@ApplicationScoped
|
||||
public class FlowerRepository {
|
||||
|
||||
@Inject
|
||||
EntityManager em;
|
||||
|
||||
public Collection<FlowerEntity> findByColor(Color color) {
|
||||
return em.createNamedQuery("FlowerEntity.findByColor", FlowerEntity.class)
|
||||
.setParameter("color", color)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
public long countAll() {
|
||||
return em.createNamedQuery("FlowerEntity.countAll", Long.class)
|
||||
.getSingleResult();
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
em.createQuery("DELETE FROM FlowerEntity").executeUpdate();
|
||||
}
|
||||
|
||||
public void persist(FlowerEntity flower) {
|
||||
em.persist(flower);
|
||||
}
|
||||
}
|
||||
|
||||
18
src/main/java/dev/hinterdorfer/IndoorFlowerEntity.java
Normal file
18
src/main/java/dev/hinterdorfer/IndoorFlowerEntity.java
Normal file
@ -0,0 +1,18 @@
|
||||
package dev.hinterdorfer;
|
||||
|
||||
import jakarta.persistence.DiscriminatorValue;
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@DiscriminatorValue("INDOOR")
|
||||
public class IndoorFlowerEntity extends FlowerEntity {
|
||||
|
||||
// JPA requires a no-arg constructor
|
||||
public IndoorFlowerEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public IndoorFlowerEntity(String name, Color color) {
|
||||
super(name, color);
|
||||
}
|
||||
}
|
||||
17
src/main/java/dev/hinterdorfer/OutdoorFlowerEntity.java
Normal file
17
src/main/java/dev/hinterdorfer/OutdoorFlowerEntity.java
Normal file
@ -0,0 +1,17 @@
|
||||
package dev.hinterdorfer;
|
||||
|
||||
import jakarta.persistence.DiscriminatorValue;
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@DiscriminatorValue("OUTDOOR")
|
||||
public class OutdoorFlowerEntity extends FlowerEntity{
|
||||
|
||||
public OutdoorFlowerEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public OutdoorFlowerEntity(String name, Color color) {
|
||||
super(name, color);
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
quarkus.datasource.db-kind=postgresql
|
||||
quarkus.datasource.username=schul-user-jonas
|
||||
quarkus.datasource.password=123
|
||||
quarkus.datasource.password=1234
|
||||
quarkus.datasource.jdbc.url=jdbc:postgresql://pg.hinterdorfer.dev:9234/schul-user-jonas?currentSchema=namedQuery
|
||||
quarkus.datasource.jdbc.max-size=8
|
||||
quarkus.datasource.jdbc.min-size=2
|
||||
@ -9,3 +9,4 @@ quarkus.hibernate-orm.schema-management.strategy=drop-and-create
|
||||
quarkus.hibernate-orm.log.sql=true
|
||||
quarkus.hibernate-orm.log.bind-parameters=true
|
||||
|
||||
|
||||
|
||||
49
src/test/java/dev/hinterdorfer/FlowerPersistanceTest.java
Normal file
49
src/test/java/dev/hinterdorfer/FlowerPersistanceTest.java
Normal file
@ -0,0 +1,49 @@
|
||||
package dev.hinterdorfer;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
public class FlowerPersistanceTest {
|
||||
|
||||
@Inject
|
||||
FlowerPersistance persistence;
|
||||
|
||||
@Inject
|
||||
EntityManager em;
|
||||
|
||||
@BeforeEach
|
||||
@Transactional
|
||||
public void setup() {
|
||||
// clean table
|
||||
em.createQuery("DELETE FROM FlowerEntity").executeUpdate();
|
||||
|
||||
// insert two flowers programmatically
|
||||
em.persist(new FlowerEntity("Red Flower", Color.red));
|
||||
em.persist(new FlowerEntity("Blue Flower", Color.blue));
|
||||
em.flush();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByColor() {
|
||||
Collection<FlowerEntity> reds = persistence.find(Color.red);
|
||||
assertEquals(1, reds.size());
|
||||
assertTrue(reds.stream().allMatch(f -> f.getColor() == Color.red));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCount() {
|
||||
long c = persistence.count();
|
||||
assertEquals(2L, c);
|
||||
}
|
||||
}
|
||||
|
||||
46
src/test/java/dev/hinterdorfer/FlowerRepositoryTest.java
Normal file
46
src/test/java/dev/hinterdorfer/FlowerRepositoryTest.java
Normal file
@ -0,0 +1,46 @@
|
||||
package dev.hinterdorfer;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
public class FlowerRepositoryTest {
|
||||
|
||||
@Inject
|
||||
FlowerRepository repository;
|
||||
|
||||
@Inject
|
||||
EntityManager em;
|
||||
|
||||
@BeforeEach
|
||||
@Transactional
|
||||
public void setup() {
|
||||
repository.deleteAll();
|
||||
repository.persist(new FlowerEntity("Red Flower", Color.red));
|
||||
repository.persist(new FlowerEntity("Blue Flower", Color.blue));
|
||||
em.flush();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByColor() {
|
||||
Collection<FlowerEntity> reds = repository.findByColor(Color.red);
|
||||
assertEquals(1, reds.size());
|
||||
assertTrue(reds.stream().allMatch(f -> f.getColor() == Color.red));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCount() {
|
||||
long c = repository.countAll();
|
||||
assertEquals(2L, c);
|
||||
}
|
||||
}
|
||||
|
||||
16
src/test/resources/application.properties
Normal file
16
src/test/resources/application.properties
Normal file
@ -0,0 +1,16 @@
|
||||
# Test config: use in-memory H2 and prevent main import.sql from running
|
||||
quarkus.datasource.db-kind=h2
|
||||
quarkus.datasource.username=sa
|
||||
quarkus.datasource.password=
|
||||
quarkus.datasource.jdbc.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=PostgreSQL
|
||||
|
||||
# Let Hibernate create/drop schema for tests
|
||||
quarkus.hibernate-orm.database.generation=drop-and-create
|
||||
|
||||
# Disable automatic load of import.sql from main resources for tests
|
||||
quarkus.hibernate-orm.sql-load-script=
|
||||
|
||||
# Show SQL in test logs (helpful while developing tests)
|
||||
quarkus.hibernate-orm.log.sql=true
|
||||
quarkus.hibernate-orm.log.bind-parameters=true
|
||||
|
||||
Loading…
Reference in New Issue
Block a user