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));
}));
}
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);
}
}
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));
}
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"));
}
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));
}
Aggregations