Skip to content

Commit

Permalink
[eclipse-ditto#964] add SubjectExpiryNotification and annotation-base…
Browse files Browse the repository at this point in the history
…d serialization.

Signed-off-by: Yufei Cai <yufei.cai@bosch.io>
  • Loading branch information
yufei-cai committed Feb 12, 2021
1 parent 9a043f7 commit 717f954
Show file tree
Hide file tree
Showing 20 changed files with 848 additions and 4 deletions.
Expand Up @@ -30,7 +30,7 @@
public @interface JsonParsableEvent {

/**
* Returns the name of the command.
* Returns the name of the event.
*
* @return the name.
*/
Expand Down
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.model.base.json;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.atteo.classindex.IndexAnnotated;

/**
* This annotated marks a notification as deserializable from Json when calling the specified
* {@link org.eclipse.ditto.model.base.json.JsonParsableNotification#method()} with {@link org.eclipse.ditto.json.JsonObject} as first
* and {@link org.eclipse.ditto.model.base.headers.DittoHeaders} as second argument.
* The {@link org.eclipse.ditto.model.base.json.JsonParsableNotification#type()} is used as identifier of this notification.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@IndexAnnotated
public @interface JsonParsableNotification {

/**
* Used as identifier of the exception.
*
* @return the error code.
*/
String type();

/**
* The name of the method accepting a {@link org.eclipse.ditto.json.JsonObject} as first argument and
* {@link org.eclipse.ditto.model.base.headers.DittoHeaders} as seconds argument.
* The Method must return an instance of the exception annotated with this annotation.
*
* @return the name of this method.
*/
String method() default "fromJson";

}
4 changes: 4 additions & 0 deletions services/gateway/util/pom.xml
Expand Up @@ -76,6 +76,10 @@
<groupId>org.eclipse.ditto</groupId>
<artifactId>ditto-signals-events-things</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.ditto</groupId>
<artifactId>ditto-signals-notifications-policies</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.ditto</groupId>
Expand Down
Expand Up @@ -12,17 +12,25 @@
*/
package org.eclipse.ditto.services.gateway.util;

import static org.assertj.core.api.Assertions.assertThat;

import java.time.Instant;
import java.util.Collections;
import java.util.Map;

import org.assertj.core.api.AbstractMapAssert;
import org.assertj.core.api.Assertions;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.model.base.auth.AuthorizationSubject;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.Jsonifiable;
import org.eclipse.ditto.model.messages.AuthorizationSubjectBlockedException;
import org.eclipse.ditto.model.messages.MessageFormatInvalidException;
import org.eclipse.ditto.model.messages.MessageSendNotAllowedException;
import org.eclipse.ditto.model.messages.MessageTimeoutException;
import org.eclipse.ditto.model.messages.SubjectInvalidException;
import org.eclipse.ditto.model.messages.TimeoutInvalidException;
import org.eclipse.ditto.model.policies.PolicyId;
import org.eclipse.ditto.services.models.policies.PoliciesMappingStrategies;
import org.eclipse.ditto.services.models.things.ThingsMappingStrategies;
import org.eclipse.ditto.services.models.thingsearch.ThingSearchMappingStrategies;
Expand All @@ -41,6 +49,7 @@
import org.eclipse.ditto.signals.commands.messages.SendMessageAcceptedResponse;
import org.eclipse.ditto.signals.commands.messages.SendThingMessage;
import org.eclipse.ditto.signals.commands.messages.SendThingMessageResponse;
import org.eclipse.ditto.signals.notifications.policies.SubjectExpiryNotification;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -101,6 +110,26 @@ public void devOpsStrategiesAreKnown() {
.knows(RetrieveStatisticsResponse.TYPE);
}

@Test
public void deserializeSubjectExpiryNotification() {
final Instant expiry = Instant.now();
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder().randomCorrelationId().build();
final SubjectExpiryNotification notification = SubjectExpiryNotification.of(
PolicyId.of("policy:id"),
expiry,
Collections.singleton(AuthorizationSubject.newInstance("ditto:ditto")),
dittoHeaders
);

final JsonObject json = notification.toJson();
final PoliciesMappingStrategies underTest = PoliciesMappingStrategies.getInstance();
final Jsonifiable<?> output = underTest.getMappingStrategy(notification.getManifest())
.orElseThrow()
.parse(json, dittoHeaders);

assertThat(output).isEqualTo(notification);
}

private StrategyAssert assertThatStrategy() {
return new StrategyAssert(underTest);
}
Expand Down
4 changes: 4 additions & 0 deletions services/models/connectivity/pom.xml
Expand Up @@ -65,6 +65,10 @@
<groupId>org.eclipse.ditto</groupId>
<artifactId>ditto-signals-events-connectivity</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.ditto</groupId>
<artifactId>ditto-signals-notifications-policies</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.models.connectivity;

import static org.assertj.core.api.Assertions.assertThat;

import java.time.Instant;
import java.util.Collections;

import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.model.base.auth.AuthorizationSubject;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.Jsonifiable;
import org.eclipse.ditto.model.policies.PolicyId;
import org.eclipse.ditto.services.models.policies.PoliciesMappingStrategies;
import org.eclipse.ditto.signals.notifications.policies.SubjectExpiryNotification;
import org.junit.Test;

/**
* Tests {@link ConnectivityMappingStrategies}.
*/
public final class ConnectivityMappingStrategiesTest {

@Test
public void deserializeSubjectExpiryNotification() {
final Instant expiry = Instant.now();
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder().randomCorrelationId().build();
final SubjectExpiryNotification notification = SubjectExpiryNotification.of(
PolicyId.of("policy:id"),
expiry,
Collections.singleton(AuthorizationSubject.newInstance("ditto:ditto")),
dittoHeaders
);

final JsonObject json = notification.toJson();
final PoliciesMappingStrategies underTest = PoliciesMappingStrategies.getInstance();
final Jsonifiable<?> output = underTest.getMappingStrategy(notification.getManifest())
.orElseThrow()
.parse(json, dittoHeaders);

assertThat(output).isEqualTo(notification);
}

}
4 changes: 4 additions & 0 deletions services/models/policies/pom.xml
Expand Up @@ -55,6 +55,10 @@
<groupId>org.eclipse.ditto</groupId>
<artifactId>ditto-signals-events-policies</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.ditto</groupId>
<artifactId>ditto-signals-notifications-policies</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.ditto</groupId>
Expand Down
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.models.policies;

import static org.assertj.core.api.Assertions.assertThat;

import java.time.Instant;
import java.util.Collections;

import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.model.base.auth.AuthorizationSubject;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.Jsonifiable;
import org.eclipse.ditto.model.policies.PolicyId;
import org.eclipse.ditto.signals.notifications.policies.SubjectExpiryNotification;
import org.junit.Test;

/**
* Tests {@link PoliciesMappingStrategies}.
*/
public final class PoliciesMappingStrategiesTest {

@Test
public void deserializeSubjectExpiryNotification() {
final Instant expiry = Instant.now();
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder().randomCorrelationId().build();
final SubjectExpiryNotification notification = SubjectExpiryNotification.of(
PolicyId.of("policy:id"),
expiry,
Collections.singleton(AuthorizationSubject.newInstance("ditto:ditto")),
dittoHeaders
);

final JsonObject json = notification.toJson();
final PoliciesMappingStrategies underTest = PoliciesMappingStrategies.getInstance();
final Jsonifiable<?> output = underTest.getMappingStrategy(notification.getManifest())
.orElseThrow()
.parse(json, dittoHeaders);

assertThat(output).isEqualTo(notification);
}
}
4 changes: 4 additions & 0 deletions services/utils/cluster/pom.xml
Expand Up @@ -73,6 +73,10 @@
<groupId>org.eclipse.ditto</groupId>
<artifactId>ditto-signals-commands-devops</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.ditto</groupId>
<artifactId>ditto-signals-notifications-base</artifactId>
</dependency>

<dependency>
<groupId>com.typesafe.akka</groupId>
Expand Down
Expand Up @@ -20,10 +20,10 @@
import org.eclipse.ditto.model.base.json.Jsonifiable;
import org.eclipse.ditto.signals.base.GlobalErrorRegistry;
import org.eclipse.ditto.signals.base.JsonParsable;
import org.eclipse.ditto.signals.base.JsonParsableRegistry;
import org.eclipse.ditto.signals.commands.base.GlobalCommandRegistry;
import org.eclipse.ditto.signals.commands.base.GlobalCommandResponseRegistry;
import org.eclipse.ditto.signals.events.base.GlobalEventRegistry;
import org.eclipse.ditto.signals.notifications.base.GlobalNotificationRegistry;

@Immutable
public final class GlobalMappingStrategies extends MappingStrategies {
Expand Down Expand Up @@ -62,6 +62,7 @@ private static MappingStrategies getGlobalStrategies() {
.add(GlobalCommandRegistry.getInstance())
.add(GlobalCommandResponseRegistry.getInstance())
.add(GlobalEventRegistry.getInstance())
.add(GlobalNotificationRegistry.getInstance())
.build();
}

Expand Down
Expand Up @@ -57,7 +57,7 @@ protected AbstractGlobalJsonParsableRegistry(
* Annotation based strategies will be overridden if they have the same key.
*/
protected AbstractGlobalJsonParsableRegistry(
final Class<T> parsedClass,
final Class<?> parsedClass,
final Class<A> annotationClass,
final AbstractAnnotationBasedJsonParsableFactory<T, A> annotationBasedJsonParsableFactory,
final Map<String, JsonParsable<T>> parseStrategies) {
Expand All @@ -80,7 +80,7 @@ private static <T> Map<String, JsonParsable<T>> mergeParsingStrategies(

@SuppressWarnings("unchecked") //Suppressed because the cast of cls is ensured by the two filters before.
private static <T, A extends Annotation> Map<String, JsonParsable<T>> initAnnotationBasedParseStrategies(
final Class<T> baseClass,
final Class<?> baseClass,
final Class<A> annotationClass,
final AbstractAnnotationBasedJsonParsableFactory<T, A> annotationBasedJsonParsableFactory) {

Expand Down

0 comments on commit 717f954

Please sign in to comment.