Search in sources :

Example 41 with TestContext

use of io.vertx.ext.unit.TestContext in project hono by eclipse.

the class AbstractProtocolAdapterBaseTest method testGetRegistrationAssertionSucceedsForExistingDevice.

/**
 * Verifies that the adapter successfully retrieves a registration assertion
 * for an existing device.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetRegistrationAssertionSucceedsForExistingDevice(final TestContext ctx) {
    // GIVEN an adapter connected to a registration service
    final JsonObject assertionResult = newRegistrationAssertionResult("token");
    when(registrationClient.assertRegistration(eq("device"), any())).thenReturn(Future.succeededFuture(assertionResult));
    // WHEN an assertion for the device is retrieved
    adapter.getRegistrationAssertion("tenant", "device", null).setHandler(ctx.asyncAssertSuccess(result -> {
        // THEN the result contains the registration assertion
        ctx.assertEquals(assertionResult, result);
    }));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) RunWith(org.junit.runner.RunWith) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Assert.assertThat(org.junit.Assert.assertThat) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) RegistrationClient(org.eclipse.hono.client.RegistrationClient) JsonObject(io.vertx.core.json.JsonObject) HonoClient(org.eclipse.hono.client.HonoClient) Before(org.junit.Before) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) Test(org.junit.Test) ProtonHelper(io.vertx.proton.ProtonHelper) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) Mockito(org.mockito.Mockito) HonoClientBasedAuthProvider(org.eclipse.hono.service.auth.device.HonoClientBasedAuthProvider) Rule(org.junit.Rule) Device(org.eclipse.hono.service.auth.device.Device) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 42 with TestContext

use of io.vertx.ext.unit.TestContext in project hono by eclipse.

the class CredentialsApiAuthProviderTest method testAuthenticateFailsWithExceptionReportedByCredentialsClient.

/**
 * Verifies that the auth provider propagates the exception reported by a failed invocation
 * of the credentials service.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testAuthenticateFailsWithExceptionReportedByCredentialsClient(final TestContext ctx) {
    final ServerErrorException reportedException = new ServerErrorException(503);
    when(credentialsClient.isOpen()).thenReturn(Boolean.TRUE);
    when(credentialsClient.get(anyString(), anyString())).thenReturn(Future.failedFuture(reportedException));
    provider.authenticate(UsernamePasswordCredentials.create("user@TENANT", "pwd", false), ctx.asyncAssertFailure(t -> {
        ctx.assertEquals(t, reportedException);
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test) ClientErrorException(org.eclipse.hono.client.ClientErrorException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) CredentialsClient(org.eclipse.hono.client.CredentialsClient) Rule(org.junit.Rule) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) HonoClient(org.eclipse.hono.client.HonoClient) Before(org.junit.Before) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test)

Example 43 with TestContext

use of io.vertx.ext.unit.TestContext in project hono by eclipse.

the class CredentialsApiAuthProviderTest method testAuthenticateFailsWith401ForMalformedCredentials.

/**
 * Verifies that the auth provider fails an authentication request with a 401
 * {@code ClientErrorException} if the credentials cannot be parsed.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testAuthenticateFailsWith401ForMalformedCredentials(final TestContext ctx) {
    // WHEN trying to authenticate using malformed credentials
    // that do not contain a tenant
    final JsonObject authInfo = new JsonObject().put("username", "no-tenant").put("password", "secret");
    provider.authenticate(authInfo, ctx.asyncAssertFailure(t -> {
        // THEN authentication fails with a 401 client error
        ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode());
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test) ClientErrorException(org.eclipse.hono.client.ClientErrorException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) CredentialsClient(org.eclipse.hono.client.CredentialsClient) Rule(org.junit.Rule) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) HonoClient(org.eclipse.hono.client.HonoClient) Before(org.junit.Before) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Test(org.junit.Test)

Example 44 with TestContext

use of io.vertx.ext.unit.TestContext in project hono by eclipse.

the class CredentialsApiAuthProviderTest method testAuthenticateFailsWith401ForNonExistingAuthId.

/**
 * Verifies that the auth provider fails an authentication request with a 401
 * {@code ClientErrorException} if the auth-id is unknown.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testAuthenticateFailsWith401ForNonExistingAuthId(final TestContext ctx) {
    // WHEN trying to authenticate using an auth-id that is not known
    final JsonObject authInfo = new JsonObject().put("username", "unknown@TENANT").put("password", "secret");
    when(credentialsClient.isOpen()).thenReturn(Boolean.TRUE);
    when(credentialsClient.get(anyString(), eq("unknown"))).thenReturn(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND)));
    provider.authenticate(authInfo, ctx.asyncAssertFailure(t -> {
        // THEN authentication fails with a 401 client error
        ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode());
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test) ClientErrorException(org.eclipse.hono.client.ClientErrorException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) CredentialsClient(org.eclipse.hono.client.CredentialsClient) Rule(org.junit.Rule) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) HonoClient(org.eclipse.hono.client.HonoClient) Before(org.junit.Before) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Test(org.junit.Test)

Example 45 with TestContext

use of io.vertx.ext.unit.TestContext in project hono by eclipse.

the class BaseCredentialsServiceTest method testGetFailsForMissingType.

/**
 * Verifies that the base service fails a request for getting credentials
 * with a 400 error code if the type is missing.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetFailsForMissingType(final TestContext ctx) {
    // GIVEN a request for getting credentials that does not specify a type
    final CredentialsObject malformedPayload = new CredentialsObject().setAuthId("bumlux").addSecret(CredentialsObject.emptySecret(null, null));
    final EventBusMessage request = createRequestForPayload(CredentialsConstants.CredentialsAction.get, JsonObject.mapFrom(malformedPayload));
    // WHEN processing the request
    service.processRequest(request).setHandler(ctx.asyncAssertFailure(t -> {
        // THEN the response contains a 400 error code
        ctx.assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, ((ServiceInvocationException) t).getErrorCode());
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) BeforeClass(org.junit.BeforeClass) CredentialsResult(org.eclipse.hono.util.CredentialsResult) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) EventBusMessage(org.eclipse.hono.util.EventBusMessage) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Future(io.vertx.core.Future) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) EventBusMessage(org.eclipse.hono.util.EventBusMessage) CredentialsObject(org.eclipse.hono.util.CredentialsObject) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Test(org.junit.Test)

Aggregations

TestContext (io.vertx.ext.unit.TestContext)148 Test (org.junit.Test)147 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)141 RunWith (org.junit.runner.RunWith)141 Async (io.vertx.ext.unit.Async)123 Future (io.vertx.core.Future)121 Handler (io.vertx.core.Handler)112 Vertx (io.vertx.core.Vertx)103 HttpURLConnection (java.net.HttpURLConnection)100 Before (org.junit.Before)97 Timeout (org.junit.rules.Timeout)95 JsonObject (io.vertx.core.json.JsonObject)91 Rule (org.junit.Rule)83 Mockito (org.mockito.Mockito)74 Constants (org.eclipse.hono.util.Constants)68 Assert.assertThat (org.junit.Assert.assertThat)62 Context (io.vertx.core.Context)57 CoreMatchers.is (org.hamcrest.CoreMatchers.is)54 AsyncResult (io.vertx.core.AsyncResult)53 Buffer (io.vertx.core.buffer.Buffer)52