Skip to content

Commit

Permalink
Dev UI: Add Hibernate JsonRPC Tests
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger committed Jun 19, 2023
1 parent bd8a4ff commit 8cb021f
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 65 deletions.
5 changes: 5 additions & 0 deletions extensions/hibernate-orm/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-http-dev-ui-spi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-http-dev-ui-tests</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkus.hibernate.orm.devui;

import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.hibernate.orm.devui.namedpu.MyNamedPuEntity;
import io.quarkus.test.QuarkusDevModeTest;

public class DevUIHibernateOrmActiveFalseAndNamedPuActiveTrueTest extends DevUIHibernateOrmTest {

@RegisterExtension
static final QuarkusDevModeTest test = new QuarkusDevModeTest()
.withApplicationRoot((jar) -> jar.addAsResource(
new StringAsset("quarkus.datasource.db-kind=h2\n"
+ "quarkus.datasource.jdbc.url=jdbc:h2:mem:test\n"
+ "quarkus.datasource.\"nameddatasource\".db-kind=h2\n"
+ "quarkus.datasource.\"nameddatasource\".jdbc.url=jdbc:h2:mem:test2\n"
// Hibernate ORM is inactive for the default PU
+ "quarkus.hibernate-orm.active=false\n"
+ "quarkus.hibernate-orm.datasource=<default>\n"
+ "quarkus.hibernate-orm.packages=io.quarkus.test.devconsole\n"
// ... but it's (implicitly) active for a named PU
+ "quarkus.hibernate-orm.\"namedpu\".datasource=nameddatasource\n"
+ "quarkus.hibernate-orm.\"namedpu\".packages=io.quarkus.hibernate.orm.devui.namedpu\n"),
"application.properties")
.addClasses(MyEntity.class)
.addClasses(MyNamedPuEntity.class));

public DevUIHibernateOrmActiveFalseAndNamedPuActiveTrueTest() {
super("namedpu", "MyNamedPuEntity", "io.quarkus.hibernate.orm.devui.namedpu.MyNamedPuEntity");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.hibernate.orm.devui;

import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusDevModeTest;

public class DevUIHibernateOrmActiveFalseTest extends DevUIHibernateOrmTest {

@RegisterExtension
static final QuarkusDevModeTest test = new QuarkusDevModeTest()
.withApplicationRoot((jar) -> jar.addAsResource(
new StringAsset("quarkus.datasource.db-kind=h2\n"
+ "quarkus.datasource.jdbc.url=jdbc:h2:mem:test\n"
// Hibernate ORM is inactive: the dev console should be empty.
+ "quarkus.hibernate-orm.active=false\n"),
"application.properties")
.addClasses(MyEntity.class));

public DevUIHibernateOrmActiveFalseTest() {
super(0, 0);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.hibernate.orm.devui;

import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusDevModeTest;

public class DevUIHibernateOrmSmokeTest extends DevUIHibernateOrmTest {

@RegisterExtension
static final QuarkusDevModeTest test = new QuarkusDevModeTest()
.withApplicationRoot((jar) -> jar.addAsResource(
new StringAsset("quarkus.datasource.db-kind=h2\n"
+ "quarkus.datasource.jdbc.url=jdbc:h2:mem:test\n"),
"application.properties")
.addClasses(MyEntity.class));

public DevUIHibernateOrmSmokeTest() {
super("<default>", "MyEntity", "io.quarkus.hibernate.orm.devui.MyEntity");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package io.quarkus.hibernate.orm.devui;

import java.util.Iterator;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.JsonNode;

import io.quarkus.devui.tests.DevUIJsonRPCTest;

/**
* All tests test the same api call, with different configuration and different expected results
* This abstract class reduce the code in each test
*/
public abstract class DevUIHibernateOrmTest extends DevUIJsonRPCTest {

private String expectedPersistenceUnitName = null;
private String expectedTableName = null;
private String expectedClassName = null;
private int expectedNumberOfEntityTypes = 1;
private int expectedNumberOfPersistenceUnits = 1;

public DevUIHibernateOrmTest(String expectedPersistenceUnitName, String expectedTableName, String expectedClassName) {
this.expectedPersistenceUnitName = expectedPersistenceUnitName;
this.expectedTableName = expectedTableName;
this.expectedClassName = expectedClassName;
}

public DevUIHibernateOrmTest(int expectedNumberOfEntityTypes,
int expectedNumberOfPersistenceUnits) {
this.expectedNumberOfEntityTypes = expectedNumberOfEntityTypes;
this.expectedNumberOfPersistenceUnits = expectedNumberOfPersistenceUnits;
}

@Test
public void testGetInfo() throws Exception {
JsonNode getInfoResponse = super.executeJsonRPCMethod("getInfo");
Assertions.assertNotNull(getInfoResponse);

JsonNode persistenceUnits = getInfoResponse.get("persistenceUnits");
Assertions.assertNotNull(persistenceUnits);
Assertions.assertTrue(persistenceUnits.isArray());

if (expectedPersistenceUnitName == null) {
Assertions.assertEquals(0, persistenceUnits.size());
} else {
Assertions.assertEquals(1, persistenceUnits.size());
Iterator<JsonNode> persistenceUnitsIterator = persistenceUnits.elements();
while (persistenceUnitsIterator.hasNext()) {
JsonNode defaultPersistenceUnit = persistenceUnitsIterator.next();
JsonNode name = defaultPersistenceUnit.get("name");
Assertions.assertEquals(expectedPersistenceUnitName, name.asText());

JsonNode managedEntities = defaultPersistenceUnit.get("managedEntities");
Assertions.assertNotNull(managedEntities);
Assertions.assertTrue(managedEntities.isArray());

Iterator<JsonNode> managedEntitiesIterator = managedEntities.elements();
while (managedEntitiesIterator.hasNext()) {
JsonNode myEntity = managedEntitiesIterator.next();
Assertions.assertEquals(expectedNumberOfPersistenceUnits, persistenceUnits.size());

String tableName = myEntity.get("tableName").asText();
Assertions.assertEquals(expectedTableName, tableName);

String className = myEntity.get("className").asText();
Assertions.assertEquals(expectedClassName, className);

}

JsonNode namedQueries = defaultPersistenceUnit.get("namedQueries");
Assertions.assertNotNull(namedQueries);
Assertions.assertTrue(namedQueries.isArray());

}
}
}

@Test
public void testGetNumberOfPersistenceUnits() throws Exception {
JsonNode getNumberOfPersistenceUnitsResponse = super.executeJsonRPCMethod("getNumberOfPersistenceUnits");
Assertions.assertNotNull(getNumberOfPersistenceUnitsResponse);
Assertions.assertTrue(getNumberOfPersistenceUnitsResponse.isInt());
Assertions.assertEquals(expectedNumberOfPersistenceUnits, getNumberOfPersistenceUnitsResponse.asInt());
}

@Test
public void testGetNumberOfEntityTypes() throws Exception {
JsonNode getNumberOfEntityTypesResponse = super.executeJsonRPCMethod("getNumberOfEntityTypes");
Assertions.assertNotNull(getNumberOfEntityTypesResponse);
Assertions.assertTrue(getNumberOfEntityTypesResponse.isInt());
Assertions.assertEquals(expectedNumberOfEntityTypes, getNumberOfEntityTypesResponse.asInt());
}

@Test
public void testGetNumberOfNamedQueries() throws Exception {
JsonNode getNumberOfNamedQueriesResponse = super.executeJsonRPCMethod("getNumberOfNamedQueries");
Assertions.assertNotNull(getNumberOfNamedQueriesResponse);
Assertions.assertTrue(getNumberOfNamedQueriesResponse.isInt());
Assertions.assertEquals(0, getNumberOfNamedQueriesResponse.asInt());
}

@Override
protected String getNamespace() {
return "io.quarkus.quarkus-hibernate-orm";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.hibernate.orm.devui;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class MyEntity {

@Id
Long id;

@Column
String field;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkus.hibernate.orm.devui.namedpu;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class MyNamedPuEntity {

@Id
Long id;

String field;

}
5 changes: 5 additions & 0 deletions integration-tests/devmode/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

<dependencies>
<!-- test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-http-dev-ui-tests</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-deployment</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,4 @@ public void testLegacyPages() {
.statusCode(200)
.body(Matchers.containsString("No named queries were found."));
}

@Test
public void testPages() {
// TODO #31970 restore tests of the page's content as we used to do for the old Dev UI

RestAssured.get("q/dev-v1/io.quarkus.quarkus-hibernate-orm/persistence-units")
.then()
.statusCode(200);
// .body(Matchers.not(Matchers.containsString("&lt;default&gt;")))
// .body(Matchers.containsString("namedpu"));

RestAssured.get("q/dev-v1/io.quarkus.quarkus-hibernate-orm/managed-entities")
.then()
.statusCode(200);
// .body(Matchers.not(Matchers.containsString(MyEntity.class.getName())))
// .body(Matchers.containsString(MyNamedPuEntity.class.getName()));

RestAssured.get("q/dev-v1/io.quarkus.quarkus-hibernate-orm/named-queries")
.then()
.statusCode(200);
// .body(Matchers.containsString("No named queries were found."));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,4 @@ public void testLegacyPages() {
.statusCode(200)
.body(Matchers.containsString("No persistence units were found"));
}

@Test
public void testPages() {
// TODO #31970 restore tests of the page's content as we used to do for the old Dev UI

RestAssured.get("q/dev-ui/hibernate-orm/persistence-units")
.then()
.statusCode(200);
// .body(Matchers.containsString("No persistence units were found"));

RestAssured.get("q/dev-ui/hibernate-orm/entity-types")
.then()
.statusCode(200);
// .body(Matchers.containsString("No persistence units were found"));

RestAssured.get("q/dev-ui/hibernate-orm/named-queries")
.then()
.statusCode(200);
// .body(Matchers.containsString("No persistence units were found"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,4 @@ public void testLegacyPages() {
.statusCode(200)
.body(Matchers.containsString("No named queries were found."));
}

@Test
public void testPages() {
// TODO #31970 restore tests of the page's content as we used to do for the old Dev UI

RestAssured.get("q/dev-ui/hibernate-orm/persistence-units")
.then()
.statusCode(200);
// .body(Matchers.containsString("&lt;default&gt;"));

RestAssured.get("q/dev-ui/hibernate-orm/entity-types")
.then()
.statusCode(200);
// .body(Matchers.containsString("io.quarkus.test.devconsole.MyEntity"));

RestAssured.get("q/dev-ui/hibernate-orm/named-queries")
.then()
.statusCode(200);
// .body(Matchers.containsString("No named queries were found."));
}

}

0 comments on commit 8cb021f

Please sign in to comment.