Skip to content

Commit

Permalink
POC of junit5/jupiter test migration
Browse files Browse the repository at this point in the history
Signed-off-by: Trystram Jean-Baptiste <[email protected]>
  • Loading branch information
jbtrystram committed Apr 24, 2019
1 parent 6337cac commit 16f2aaf
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 258 deletions.
12 changes: 0 additions & 12 deletions adapters/coap-vertx-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@
<url>https://www.eclipse.org/hono</url>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-unit</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.californium</groupId>
<artifactId>californium-core</artifactId>
Expand Down
18 changes: 18 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<jjwt.version>0.7.0</jjwt.version>
<jmeter.version>3.3</jmeter.version>
<junit.version>4.12</junit.version>
<junit.jupiter.version>5.4.2</junit.jupiter.version>
<logback.version>1.2.3</logback.version>
<micrometer.version>1.0.9</micrometer.version>
<mockito.version>2.24.5</mockito.version>
Expand Down Expand Up @@ -221,6 +222,23 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-junit5</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
12 changes: 10 additions & 2 deletions service-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,16 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-junit5</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@
*******************************************************************************/
package org.eclipse.hono.service.credentials;

import static org.junit.Assert.assertEquals;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.junit5.Checkpoint;
import io.vertx.junit5.VertxTestContext;
import org.eclipse.hono.util.CredentialsConstants;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.net.HttpURLConnection;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
* Abstract class used as a base for verifying behavior of {@link CompleteCredentialsService} in device registry implementations.
*
Expand All @@ -42,7 +41,7 @@ public abstract class AbstractCompleteCredentialsServiceTest {
* @param ctx The vert.x test context.
*/
@Test
public void testAddCredentialsRefusesDuplicateRegistration(final TestContext ctx) {
public void testAddCredentialsRefusesDuplicateRegistration(final VertxTestContext ctx) {

register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", ctx);

Expand All @@ -51,12 +50,10 @@ public void testAddCredentialsRefusesDuplicateRegistration(final TestContext ctx
.put(CredentialsConstants.FIELD_AUTH_ID, "myId")
.put(CredentialsConstants.FIELD_TYPE, "myType")
.put(CredentialsConstants.FIELD_SECRETS, new JsonArray());
final Async add = ctx.async();
getCompleteCredentialsService().add("tenant", payload2, ctx.asyncAssertSuccess(s -> {
assertThat(s.getStatus(), is(HttpURLConnection.HTTP_CONFLICT));
add.complete();
}));
add.await();
getCompleteCredentialsService().add("tenant", payload2, ctx.succeeding(s -> ctx.verify(() -> {
assertEquals(HttpURLConnection.HTTP_CONFLICT, s.getStatus());
ctx.completeNow();
})));
}

/**
Expand All @@ -65,14 +62,12 @@ public void testAddCredentialsRefusesDuplicateRegistration(final TestContext ctx
* @param ctx The vert.x test context.
*/
@Test
public void testGetCredentialsFailsForNonExistingCredentials(final TestContext ctx) {

final Async get = ctx.async();
getCompleteCredentialsService().get("tenant", "myType", "non-existing", ctx.asyncAssertSuccess(s -> {
assertThat(s.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND));
get.complete();
}));
get.await();
public void testGetCredentialsFailsForNonExistingCredentials(final VertxTestContext ctx) {

getCompleteCredentialsService().get("tenant", "myType", "non-existing", ctx.succeeding(s -> ctx.verify(() -> {
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, s.getStatus());
ctx.completeNow();
})));
}

/**
Expand All @@ -81,18 +76,16 @@ public void testGetCredentialsFailsForNonExistingCredentials(final TestContext c
* @param ctx The vert.x test context.
*/
@Test
public void testGetCredentialsSucceedsForExistingCredentials(final TestContext ctx) {
public void testGetCredentialsSucceedsForExistingCredentials(final VertxTestContext ctx) {

register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", ctx);

final Async get = ctx.async();
getCompleteCredentialsService().get("tenant", "myType", "myId", ctx.asyncAssertSuccess(s -> {
assertThat(s.getStatus(), is(HttpURLConnection.HTTP_OK));
assertThat(s.getPayload().getString(CredentialsConstants.FIELD_AUTH_ID), is("myId"));
assertThat(s.getPayload().getString(CredentialsConstants.FIELD_TYPE), is("myType"));
get.complete();
}));
get.await();
getCompleteCredentialsService().get("tenant", "myType", "myId", ctx.succeeding(s -> ctx.verify(() -> {
assertEquals(HttpURLConnection.HTTP_OK, s.getStatus());
assertEquals("myId", s.getPayload().getString(CredentialsConstants.FIELD_AUTH_ID));
assertEquals("myType", s.getPayload().getString(CredentialsConstants.FIELD_TYPE));
ctx.completeNow();
})));
}

/**
Expand All @@ -101,21 +94,19 @@ public void testGetCredentialsSucceedsForExistingCredentials(final TestContext c
* @param ctx The vert.x test context.
*/
@Test
public void testGetCredentialsSucceedsForProperClientContext(final TestContext ctx) {
public void testGetCredentialsSucceedsForProperClientContext(final VertxTestContext ctx) {
final JsonObject clientContext = new JsonObject()
.put("client-id", "gateway-one");

register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", clientContext, new JsonArray(), ctx);


final Async get = ctx.async();
getCompleteCredentialsService().get("tenant", "myType", "myId", clientContext, ctx.asyncAssertSuccess(s -> {
assertThat(s.getStatus(), is(HttpURLConnection.HTTP_OK));
assertThat(s.getPayload().getString(CredentialsConstants.FIELD_AUTH_ID), is("myId"));
assertThat(s.getPayload().getString(CredentialsConstants.FIELD_TYPE), is("myType"));
get.complete();
}));
get.await(2000);
getCompleteCredentialsService().get("tenant", "myType", "myId", clientContext, ctx.succeeding(s -> ctx.verify(() -> {
assertEquals(HttpURLConnection.HTTP_OK, s.getStatus());
assertEquals("myId", s.getPayload().getString(CredentialsConstants.FIELD_AUTH_ID));
assertEquals( "myType", s.getPayload().getString(CredentialsConstants.FIELD_TYPE));
ctx.completeNow();
})));
}

/**
Expand All @@ -124,7 +115,7 @@ public void testGetCredentialsSucceedsForProperClientContext(final TestContext c
* @param ctx The vert.x test context.
*/
@Test
public void testGetCredentialsFailsForWrongClientContext(final TestContext ctx) {
public void testGetCredentialsFailsForWrongClientContext(final VertxTestContext ctx) {
final JsonObject clientContext = new JsonObject()
.put("client-id", "gateway-one");

Expand All @@ -133,12 +124,10 @@ public void testGetCredentialsFailsForWrongClientContext(final TestContext ctx)
final JsonObject testContext = new JsonObject()
.put("client-id", "gateway-two");

final Async get = ctx.async();
getCompleteCredentialsService().get("tenant", "myType", "myId", testContext, ctx.asyncAssertSuccess(s -> {
assertThat(s.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND));
get.complete();
}));
get.await(2000);
getCompleteCredentialsService().get("tenant", "myType", "myId", testContext, ctx.succeeding(s -> ctx.verify(() -> {
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, s.getStatus());
ctx.completeNow();
})));
}

/**
Expand All @@ -147,17 +136,15 @@ public void testGetCredentialsFailsForWrongClientContext(final TestContext ctx)
* @param ctx The vert.x test context.
*/
@Test
public void testRemoveCredentialsByAuthIdAndTypeSucceeds(final TestContext ctx) {
public void testRemoveCredentialsByAuthIdAndTypeSucceeds(final VertxTestContext ctx) {

register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", ctx);

final Async remove = ctx.async();
getCompleteCredentialsService().remove("tenant", "myType", "myId", ctx.asyncAssertSuccess(s -> {
assertThat(s.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT));
getCompleteCredentialsService().remove("tenant", "myType", "myId", ctx.succeeding(s -> ctx.verify(() -> {
assertEquals(HttpURLConnection.HTTP_NO_CONTENT, s.getStatus());
assertNotRegistered(getCompleteCredentialsService(), "tenant", "myId", "myType", ctx);
remove.complete();
}));
remove.await();
ctx.completeNow();
})));
}

/**
Expand All @@ -167,51 +154,45 @@ public void testRemoveCredentialsByAuthIdAndTypeSucceeds(final TestContext ctx)
* @param ctx The vert.x test context.
*/
@Test
public void testRemoveCredentialsByDeviceSucceeds(final TestContext ctx) {
public void testRemoveCredentialsByDeviceSucceeds(final VertxTestContext ctx) {

register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", ctx);
register(getCompleteCredentialsService(), "tenant", "device", "myOtherId", "myOtherType", ctx);
register(getCompleteCredentialsService(), "tenant", "other-device", "thirdId", "myType", ctx);

final Async remove = ctx.async();
getCompleteCredentialsService().removeAll("tenant", "device", ctx.asyncAssertSuccess(s -> {
assertThat(s.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT));
getCompleteCredentialsService().removeAll("tenant", "device", ctx.succeeding(s -> ctx.verify(() -> {
assertEquals(HttpURLConnection.HTTP_NO_CONTENT, s.getStatus());
assertNotRegistered(getCompleteCredentialsService(), "tenant", "myId", "myType", ctx);
assertNotRegistered(getCompleteCredentialsService(), "tenant", "myOtherId", "myOtherType", ctx);
assertRegistered(getCompleteCredentialsService(), "tenant", "thirdId", "myType", ctx);
remove.complete();
}));
remove.await();
ctx.completeNow();
})));
}

protected static void assertRegistered(
final CompleteCredentialsService svc,
final String tenant,
final String authId,
final String type,
final TestContext ctx) {

final Async registration = ctx.async();
svc.get(tenant, type, authId, ctx.asyncAssertSuccess(t -> {
assertThat(t.getStatus(), is(HttpURLConnection.HTTP_OK));
registration.complete();
}));
registration.await();
final VertxTestContext ctx) {

svc.get(tenant, type, authId, ctx.succeeding(t -> ctx.verify(() -> {
assertEquals(HttpURLConnection.HTTP_OK, t.getStatus());
ctx.completeNow();
})));
}

protected static void assertNotRegistered(
final CompleteCredentialsService svc,
final String tenant,
final String authId,
final String type,
final TestContext ctx) {

final Async registration = ctx.async();
svc.get(tenant, type, authId, ctx.asyncAssertSuccess(t -> {
assertThat(t.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND));
registration.complete();
}));
registration.await();
final VertxTestContext ctx) {

svc.get(tenant, type, authId, ctx.succeeding(t -> ctx.verify(() -> {
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, t.getStatus());
ctx.completeNow();
})));
}

protected static void register(
Expand All @@ -220,7 +201,7 @@ protected static void register(
final String deviceId,
final String authId,
final String type,
final TestContext ctx) {
final VertxTestContext ctx) {
register(svc, tenant, deviceId, authId, type, null, new JsonArray(), ctx);
}

Expand All @@ -232,7 +213,7 @@ protected static void register(
final String type,
final JsonObject clientContext,
final JsonArray secrets,
final TestContext ctx) {
final VertxTestContext ctx) {

final JsonObject data = new JsonObject()
.put(CredentialsConstants.FIELD_PAYLOAD_DEVICE_ID, deviceId)
Expand All @@ -244,11 +225,10 @@ protected static void register(
data.mergeIn(clientContext);
}

final Async registration = ctx.async();
svc.add("tenant", data, ctx.asyncAssertSuccess(s -> {
assertThat(s.getStatus(), is(HttpURLConnection.HTTP_CREATED));
registration.complete();
}));
registration.await();
final Checkpoint registration = ctx.checkpoint();
svc.add("tenant", data, ctx.succeeding(s -> ctx.verify(() -> {
assertEquals(HttpURLConnection.HTTP_CREATED, s.getStatus());
ctx.completeNow();
})));
}
}
Loading

0 comments on commit 16f2aaf

Please sign in to comment.