Skip to content

Commit

Permalink
Added orm panache hot reload test
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage committed Mar 31, 2020
1 parent 19592ed commit 50f474b
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 0 deletions.
24 changes: 24 additions & 0 deletions extensions/panache/hibernate-orm-panache/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<properties>
<maven.compiler.parameters>true</maven.compiler.parameters>
</properties>

<artifactId>quarkus-hibernate-orm-panache-deployment</artifactId>
<name>Quarkus - Hibernate ORM with Panache - Deployment</name>
<dependencies>
Expand Down Expand Up @@ -48,6 +52,26 @@
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.quarkus.hibernate.orm.panache.test;

import javax.persistence.Entity;

import io.quarkus.hibernate.orm.panache.PanacheEntity;

@Entity
public class MyEntity extends PanacheEntity {
public String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.quarkus.hibernate.orm.panache.test;

import javax.persistence.Entity;

import io.quarkus.hibernate.orm.panache.PanacheEntity;

@Entity
public class MyOtherEntity extends PanacheEntity {
public String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.hibernate.orm.panache.test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.annotations.jaxrs.PathParam;

@Path("other-entity")
public class MyOtherTestResource {

@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public MyOtherEntity get(@PathParam long id) {
MyOtherEntity ret = MyOtherEntity.findById(id);
if (ret == null)
throw new WebApplicationException(Response.Status.NOT_FOUND);
return ret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.hibernate.orm.panache.test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.annotations.jaxrs.PathParam;

@Path("entity")
public class MyTestResource {

@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public MyEntity get(@PathParam long id) {
MyEntity ret = MyEntity.findById(id);
if (ret == null)
throw new WebApplicationException(Response.Status.NOT_FOUND);
return ret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.quarkus.hibernate.orm.panache.test;

import static org.hamcrest.Matchers.is;

import java.util.function.Function;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusDevModeTest;
import io.restassured.RestAssured;

public class PanacheHotReloadTestCase {
@RegisterExtension
final static QuarkusDevModeTest TEST = new QuarkusDevModeTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(MyEntity.class, MyTestResource.class)
.addAsResource("application.properties")
.addAsResource("import.sql"));

@Test
public void testAddNewFieldToEntity() {
String expectedName = "{\"id\":1,\"name\":\"my name\"}";
assertBodyIs(expectedName);

TEST.modifySourceFile(MyEntity.class, new Function<String, String>() {
@Override
public String apply(String s) {
return s.replace("public String name;", "public String name;public String tag;");
}
});
TEST.modifyResourceFile("import.sql", new Function<String, String>() {
@Override
public String apply(String s) {
return s.replace(";", ";\nUPDATE MyEntity SET tag = 'related' WHERE id = 1;\n");
}
});
String hotReloadExpectedName = "{\"id\":1,\"name\":\"my name\",\"tag\":\"related\"}";
assertBodyIs(hotReloadExpectedName);
}

@Test
public void testAddEntity() {
RestAssured.when().get("/other-entity/1").then().statusCode(404);

TEST.addSourceFile(MyOtherEntity.class);
TEST.addSourceFile(MyOtherTestResource.class);

TEST.modifyResourceFile("import.sql", new Function<String, String>() {
@Override
public String apply(String s) {
return s + s.replaceAll("MyEntity", "MyOtherEntity");
}
});
RestAssured.when().get("/other-entity/1").then().statusCode(200).body(is("{\"id\":1,\"name\":\"my name\"}"));
}

private void assertBodyIs(String expectedBody) {
RestAssured.when().get("/entity/1").then().statusCode(200).body(is(expectedBody));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
quarkus.datasource.db-kind=h2
quarkus.datasource.jdbc.url=jdbc:h2:mem:test

quarkus.hibernate-orm.dialect=org.hibernate.dialect.H2Dialect
quarkus.hibernate-orm.log.sql=true
quarkus.hibernate-orm.database.generation=drop-and-create
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO MyEntity(id, name) VALUES(1, 'my name');

0 comments on commit 50f474b

Please sign in to comment.