Search in sources :

Example 6 with RegistrationResult

use of org.eclipse.hono.util.RegistrationResult in project hono by eclipse.

the class RegistrationClientImplTest method testGetRegistrationInfoReturnsValueFromCache.

/**
 * Verifies that registration information is taken from cache.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetRegistrationInfoReturnsValueFromCache(final TestContext ctx) {
    // GIVEN an adapter with a cache containing a registration assertion
    // response for "device"
    client.setResponseCache(cache);
    final JsonObject registrationAssertion = newRegistrationAssertionResult();
    final RegistrationResult regResult = RegistrationResult.from(HttpURLConnection.HTTP_OK, registrationAssertion);
    when(cache.get(eq(TriTuple.of("assert", "device", "gateway")))).thenReturn(regResult);
    // WHEN getting registration information
    client.assertRegistration("device", "gateway").setHandler(ctx.asyncAssertSuccess(result -> {
        // THEN the registration information is read from the cache
        ctx.assertEquals(registrationAssertion, result);
        verify(sender, never()).send(any(Message.class));
    }));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) TestContext(io.vertx.ext.unit.TestContext) ProtonReceiver(io.vertx.proton.ProtonReceiver) Async(io.vertx.ext.unit.Async) ProtonDelivery(io.vertx.proton.ProtonDelivery) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) ExpiringValueCache(org.eclipse.hono.cache.ExpiringValueCache) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) Duration(java.time.Duration) RegistrationResult(org.eclipse.hono.util.RegistrationResult) Timeout(org.junit.rules.Timeout) SignatureAlgorithm(io.jsonwebtoken.SignatureAlgorithm) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Before(org.junit.Before) TriTuple(org.eclipse.hono.util.TriTuple) Vertx(io.vertx.core.Vertx) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) Test(org.junit.Test) ProtonHelper(io.vertx.proton.ProtonHelper) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) Date(java.sql.Date) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) Jwts(io.jsonwebtoken.Jwts) ProtonSender(io.vertx.proton.ProtonSender) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) RegistrationResult(org.eclipse.hono.util.RegistrationResult) Test(org.junit.Test)

Example 7 with RegistrationResult

use of org.eclipse.hono.util.RegistrationResult in project hono by eclipse.

the class RegistrationTestSupport method send.

private CompletableFuture<RegistrationResult> send(final String deviceId, final String action, final Integer expectedStatus) {
    try {
        final String correlationId = UUID.randomUUID().toString();
        final Message message = session.createMessage();
        message.setStringProperty(MessageHelper.APP_PROPERTY_DEVICE_ID, deviceId);
        message.setJMSType(action);
        message.setJMSReplyTo(reply);
        message.setJMSCorrelationID(correlationId);
        LOGGER.debug("adding response handler for request [correlation ID: {}]", correlationId);
        final CompletableFuture<RegistrationResult> result = c.add(correlationId, response -> {
            final Integer status = getIntProperty(response, MessageHelper.APP_PROPERTY_STATUS);
            LOGGER.debug("received response [type: {}, status: {}] for request [correlation ID: {}]", response.getClass().getName(), status, correlationId);
            int httpStatus = status;
            if (status == null || httpStatus <= 0) {
                throw new IllegalStateException("Response to " + getMessageID(response) + " contained no valid status: " + httpStatus);
            }
            if (expectedStatus != null && expectedStatus != httpStatus) {
                throw new IllegalStateException("returned status " + httpStatus);
            }
            try {
                if (response.isBodyAssignableTo(String.class)) {
                    String body = response.getBody(String.class);
                    if (body != null) {
                        LOGGER.debug("extracting response body");
                        return RegistrationResult.from(httpStatus, new JsonObject(body));
                    }
                }
            } catch (JMSException | DecodeException e) {
                LOGGER.debug("cannot extract body from response", e);
            }
            return RegistrationResult.from(httpStatus);
        });
        producer.send(message);
        return result;
    } catch (final JMSException jmsException) {
        throw new IllegalStateException("Failed to send message.", jmsException);
    }
}
Also used : Message(javax.jms.Message) JsonObject(io.vertx.core.json.JsonObject) JMSException(javax.jms.JMSException) RegistrationResult(org.eclipse.hono.util.RegistrationResult) DecodeException(io.vertx.core.json.DecodeException)

Example 8 with RegistrationResult

use of org.eclipse.hono.util.RegistrationResult in project hono by eclipse.

the class FileBasedRegistrationServiceTest method testAddDeviceFailsIfDeviceLimitIsReached.

/**
 * Verifies that the registry enforces the maximum devices per tenant limit.
 */
@Test
public void testAddDeviceFailsIfDeviceLimitIsReached() {
    // GIVEN a registry whose devices-per-tenant limit has been reached
    props.setMaxDevicesPerTenant(1);
    registrationService.addDevice(TENANT, DEVICE, null);
    // WHEN registering an additional device for the tenant
    RegistrationResult result = registrationService.addDevice(TENANT, "newDevice", null);
    // THEN the result contains a FORBIDDEN status code and the device has not been added to the registry
    assertThat(result.getStatus(), is(HttpURLConnection.HTTP_FORBIDDEN));
    assertThat(registrationService.getDevice(TENANT, "newDevice").getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND));
}
Also used : RegistrationResult(org.eclipse.hono.util.RegistrationResult) Test(org.junit.Test)

Example 9 with RegistrationResult

use of org.eclipse.hono.util.RegistrationResult in project hono by eclipse.

the class FileBasedRegistrationServiceTest method testUpdateDeviceFailsIfModificationIsDisabled.

/**
 * Verifies that the <em>modificationEnabled</em> property prevents updating an existing entry.
 */
@Test
public void testUpdateDeviceFailsIfModificationIsDisabled() {
    // GIVEN a registry that has been configured to not allow modification of entries
    // which contains a device
    props.setModificationEnabled(false);
    registrationService.addDevice(TENANT, DEVICE, null);
    // WHEN trying to update the device
    RegistrationResult result = registrationService.updateDevice(TENANT, DEVICE, new JsonObject().put("updated", true));
    // THEN the result contains a FORBIDDEN status code and the device has not been updated
    assertThat(result.getStatus(), is(HttpURLConnection.HTTP_FORBIDDEN));
    assertFalse(registrationService.getDevice(TENANT, DEVICE).getPayload().containsKey("updated"));
}
Also used : JsonObject(io.vertx.core.json.JsonObject) RegistrationResult(org.eclipse.hono.util.RegistrationResult) Test(org.junit.Test)

Example 10 with RegistrationResult

use of org.eclipse.hono.util.RegistrationResult in project hono by eclipse.

the class FileBasedRegistrationServiceTest method testRemoveDeviceFailsIfModificationIsDisabled.

/**
 * Verifies that the <em>modificationEnabled</em> property prevents removing an existing entry.
 */
@Test
public void testRemoveDeviceFailsIfModificationIsDisabled() {
    // GIVEN a registry that has been configured to not allow modification of entries
    // which contains a device
    props.setModificationEnabled(false);
    registrationService.addDevice(TENANT, DEVICE, null);
    // WHEN trying to remove the device
    RegistrationResult result = registrationService.removeDevice(TENANT, DEVICE);
    // THEN the result contains a FORBIDDEN status code and the device has not been removed
    assertThat(result.getStatus(), is(HttpURLConnection.HTTP_FORBIDDEN));
    assertThat(registrationService.getDevice(TENANT, DEVICE).getStatus(), is(HttpURLConnection.HTTP_OK));
}
Also used : RegistrationResult(org.eclipse.hono.util.RegistrationResult) Test(org.junit.Test)

Aggregations

RegistrationResult (org.eclipse.hono.util.RegistrationResult)12 JsonObject (io.vertx.core.json.JsonObject)7 ClientErrorException (org.eclipse.hono.client.ClientErrorException)6 Test (org.junit.Test)5 Handler (io.vertx.core.Handler)3 HttpURLConnection (java.net.HttpURLConnection)3 CacheDirective (org.eclipse.hono.util.CacheDirective)3 RegistrationConstants (org.eclipse.hono.util.RegistrationConstants)3 AsyncResult (io.vertx.core.AsyncResult)2 CompositeFuture (io.vertx.core.CompositeFuture)2 Future (io.vertx.core.Future)2 Objects (java.util.Objects)2 EventBusService (org.eclipse.hono.service.EventBusService)2 EventBusMessage (org.eclipse.hono.util.EventBusMessage)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Qualifier (org.springframework.beans.factory.annotation.Qualifier)2 Jwts (io.jsonwebtoken.Jwts)1 SignatureAlgorithm (io.jsonwebtoken.SignatureAlgorithm)1 Context (io.vertx.core.Context)1 Vertx (io.vertx.core.Vertx)1