Search in sources :

Example 1 with AbstractNotification

use of org.eclipse.hono.notification.AbstractNotification in project hono by eclipse.

the class CredentialsChangeNotificationTest method testDeserialization.

/**
 * Verifies that a serialized notification is deserialized correctly.
 */
@Test
public void testDeserialization() {
    final AbstractNotification abstractNotification = Json.decodeValue(JsonObject.mapFrom(notification).toBuffer(), AbstractNotification.class);
    assertThat(abstractNotification).isNotNull();
    assertThat(abstractNotification).isInstanceOf(CredentialsChangeNotification.class);
    final CredentialsChangeNotification newNotification = (CredentialsChangeNotification) abstractNotification;
    assertThat(newNotification.getSource()).isEqualTo(NotificationConstants.SOURCE_DEVICE_REGISTRY);
    assertThat(newNotification.getCreationTime()).isEqualTo(Instant.parse(CREATION_TIME));
    assertThat(newNotification.getTenantId()).isEqualTo(TENANT_ID);
    assertThat(newNotification.getDeviceId()).isEqualTo(DEVICE_ID);
}
Also used : AbstractNotification(org.eclipse.hono.notification.AbstractNotification) Test(org.junit.jupiter.api.Test)

Example 2 with AbstractNotification

use of org.eclipse.hono.notification.AbstractNotification in project hono by eclipse.

the class TenantChangeNotificationTest method testDeserialization.

/**
 * Verifies that a serialized notification is deserialized correctly.
 */
@Test
public void testDeserialization() {
    final AbstractNotification abstractNotification = Json.decodeValue(JsonObject.mapFrom(notification).toBuffer(), AbstractNotification.class);
    assertThat(abstractNotification).isNotNull();
    assertThat(abstractNotification).isInstanceOf(TenantChangeNotification.class);
    final TenantChangeNotification newNotification = (TenantChangeNotification) abstractNotification;
    assertThat(newNotification.getSource()).isEqualTo(NotificationConstants.SOURCE_DEVICE_REGISTRY);
    assertThat(newNotification.getCreationTime()).isEqualTo(Instant.parse(CREATION_TIME));
    assertThat(newNotification.getChange()).isEqualTo(CHANGE);
    assertThat(newNotification.getTenantId()).isEqualTo(TENANT_ID);
    assertThat(newNotification.isEnabled()).isEqualTo(ENABLED);
}
Also used : AbstractNotification(org.eclipse.hono.notification.AbstractNotification) Test(org.junit.jupiter.api.Test)

Example 3 with AbstractNotification

use of org.eclipse.hono.notification.AbstractNotification in project hono by eclipse.

the class ProtonBasedNotificationSenderTest method testPublishNotificationSucceeds.

/**
 * Verifies that the sender sends a notification message with the expected format.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testPublishNotificationSucceeds(final VertxTestContext ctx) {
    final String tenantId = "my-tenant";
    final Instant creationTime = Instant.parse("2007-12-03T10:15:30Z");
    final TenantChangeNotification notification = new TenantChangeNotification(LifecycleChange.CREATE, tenantId, creationTime, false);
    final ArgumentCaptor<Handler<ProtonDelivery>> dispositionHandlerCaptor = VertxMockSupport.argumentCaptorHandler();
    final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
    // WHEN publishing a notification
    final Future<Void> publishNotificationFuture = notificationSender.publish(notification).onComplete(ctx.succeedingThenComplete());
    // VERIFY that the message is being sent
    verify(sender).send(messageCaptor.capture(), dispositionHandlerCaptor.capture());
    // VERIFY that the future waits for the disposition to be updated by the peer
    assertThat(publishNotificationFuture.isComplete()).isFalse();
    // THEN the disposition is updated and the peer accepts the message
    dispositionHandlerCaptor.getValue().handle(protonDelivery);
    // VERIFY that the message is as expected
    final Message message = messageCaptor.getValue();
    assertThat(message.getAddress()).isEqualTo(NotificationAddressHelper.getAddress(notification.getType()));
    assertThat(message.getContentType()).isEqualTo(NotificationConstants.CONTENT_TYPE);
    final AbstractNotification decodedNotification = Json.decodeValue(MessageHelper.getPayload(message), AbstractNotification.class);
    assertThat(decodedNotification.getClass()).isEqualTo(notification.getClass());
    assertThat(publishNotificationFuture.isComplete()).isTrue();
}
Also used : Message(org.apache.qpid.proton.message.Message) Instant(java.time.Instant) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) AbstractNotification(org.eclipse.hono.notification.AbstractNotification) Test(org.junit.jupiter.api.Test)

Example 4 with AbstractNotification

use of org.eclipse.hono.notification.AbstractNotification in project hono by eclipse.

the class DeviceChangeNotificationTest method testDeserialization.

/**
 * Verifies that a serialized notification is deserialized correctly.
 */
@Test
public void testDeserialization() {
    final JsonObject json = new JsonObject().put(NotificationConstants.JSON_FIELD_TYPE, "device-change-v1").put(NotificationConstants.JSON_FIELD_SOURCE, NotificationConstants.SOURCE_DEVICE_REGISTRY).put(NotificationConstants.JSON_FIELD_CREATION_TIME, CREATION_TIME).put(NotificationConstants.JSON_FIELD_DATA_CHANGE, LifecycleChange.DELETE).put(NotificationConstants.JSON_FIELD_TENANT_ID, TENANT_ID).put(NotificationConstants.JSON_FIELD_DEVICE_ID, DEVICE_ID).put(NotificationConstants.JSON_FIELD_DATA_ENABLED, true);
    final AbstractNotification abstractNotification = Json.decodeValue(json.toBuffer(), AbstractNotification.class);
    assertThat(abstractNotification).isNotNull();
    assertThat(abstractNotification).isInstanceOf(DeviceChangeNotification.class);
    final DeviceChangeNotification newNotification = (DeviceChangeNotification) abstractNotification;
    assertThat(newNotification.getSource()).isEqualTo(NotificationConstants.SOURCE_DEVICE_REGISTRY);
    assertThat(newNotification.getCreationTime()).isEqualTo(Instant.parse(CREATION_TIME));
    assertThat(newNotification.getChange()).isEqualTo(LifecycleChange.DELETE);
    assertThat(newNotification.getTenantId()).isEqualTo(TENANT_ID);
    assertThat(newNotification.getDeviceId()).isEqualTo(DEVICE_ID);
    assertThat(newNotification.isEnabled()).isTrue();
}
Also used : JsonObject(io.vertx.core.json.JsonObject) AbstractNotification(org.eclipse.hono.notification.AbstractNotification) Test(org.junit.jupiter.api.Test)

Example 5 with AbstractNotification

use of org.eclipse.hono.notification.AbstractNotification in project hono by eclipse.

the class AllDevicesOfTenantDeletedNotificationTest method testDeserialization.

/**
 * Verifies that a serialized notification is deserialized correctly.
 */
@Test
public void testDeserialization() {
    final AbstractNotification abstractNotification = Json.decodeValue(JsonObject.mapFrom(notification).toBuffer(), AbstractNotification.class);
    assertThat(abstractNotification).isNotNull();
    assertThat(abstractNotification).isInstanceOf(AllDevicesOfTenantDeletedNotification.class);
    final AllDevicesOfTenantDeletedNotification newNotification = (AllDevicesOfTenantDeletedNotification) abstractNotification;
    assertThat(newNotification.getSource()).isEqualTo(NotificationConstants.SOURCE_DEVICE_REGISTRY);
    assertThat(newNotification.getCreationTime()).isEqualTo(Instant.parse(CREATION_TIME));
    assertThat(newNotification.getTenantId()).isEqualTo(TENANT_ID);
}
Also used : AbstractNotification(org.eclipse.hono.notification.AbstractNotification) Test(org.junit.jupiter.api.Test)

Aggregations

AbstractNotification (org.eclipse.hono.notification.AbstractNotification)6 Test (org.junit.jupiter.api.Test)6 Handler (io.vertx.core.Handler)2 Instant (java.time.Instant)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 MqttConnectReturnCode (io.netty.handler.codec.mqtt.MqttConnectReturnCode)1 MqttQoS (io.netty.handler.codec.mqtt.MqttQoS)1 Span (io.opentracing.Span)1 SpanContext (io.opentracing.SpanContext)1 AsyncResult (io.vertx.core.AsyncResult)1 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 Promise (io.vertx.core.Promise)1 Vertx (io.vertx.core.Vertx)1 Buffer (io.vertx.core.buffer.Buffer)1 JsonObject (io.vertx.core.json.JsonObject)1 Timeout (io.vertx.junit5.Timeout)1 VertxExtension (io.vertx.junit5.VertxExtension)1 VertxTestContext (io.vertx.junit5.VertxTestContext)1 MqttAuth (io.vertx.mqtt.MqttAuth)1