Skip to content

Commit

Permalink
[eclipse-ditto#964] moved EmptyEvent class to upper level because of …
Browse files Browse the repository at this point in the history
…global event registry tests

* there the import to EmptyEvent was added for things;policies;connectivity

Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch.io>
  • Loading branch information
thjaeckle committed Feb 26, 2021
1 parent 3281953 commit 332cfb1
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import org.eclipse.ditto.services.utils.config.InstanceIdentifierSupplier;
import org.eclipse.ditto.services.utils.persistence.mongo.config.ActivityCheckConfig;
import org.eclipse.ditto.services.utils.persistentactors.AbstractShardedPersistenceActor;
import org.eclipse.ditto.services.utils.persistentactors.EmptyEvent;
import org.eclipse.ditto.services.utils.persistentactors.commands.CommandStrategy;
import org.eclipse.ditto.services.utils.persistentactors.commands.DefaultContext;
import org.eclipse.ditto.services.utils.persistentactors.events.EventStrategy;
Expand Down Expand Up @@ -383,7 +384,8 @@ protected void processPingCommand(final PingCommand ping) {
// persistence actor was sent a "ping" with empty journal tag:
// build in adding the "always-alive" tag here by persisting an "empty" event which is just tagged to be
// "always alive"
final EmptyEvent emptyEvent = new EmptyEvent(entityId, EmptyEvent.EFFECT_ALWAYS_ALIVE, getRevisionNumber() + 1,
final EmptyEvent
emptyEvent = new EmptyEvent(entityId, EmptyEvent.EFFECT_ALWAYS_ALIVE, getRevisionNumber() + 1,
DittoHeaders.newBuilder()
.correlationId(ping.getCorrelationId().orElse(null))
.journalTags(Set.of(JOURNAL_TAG_ALWAYS_ALIVE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.ditto.services.connectivity;

import org.eclipse.ditto.services.models.things.DittoThingSnapshotTaken;
import org.eclipse.ditto.services.utils.persistentactors.EmptyEvent;
import org.eclipse.ditto.services.utils.test.GlobalEventRegistryTestCases;
import org.eclipse.ditto.signals.events.connectivity.ConnectionCreated;
import org.eclipse.ditto.signals.events.policies.ResourceDeleted;
Expand All @@ -27,7 +28,8 @@ public ConnectivityServiceGlobalEventRegistryTest() {
ResourceDeleted.class,
FeatureDeleted.class,
SubscriptionCreated.class,
DittoThingSnapshotTaken.class
DittoThingSnapshotTaken.class,
EmptyEvent.class
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.eclipse.ditto.services.policies.starter;

import org.eclipse.ditto.services.utils.persistentactors.EmptyEvent;
import org.eclipse.ditto.services.utils.test.GlobalEventRegistryTestCases;
import org.eclipse.ditto.signals.events.policies.ResourceDeleted;
import org.eclipse.ditto.signals.events.things.ThingDeleted;
Expand All @@ -24,7 +25,8 @@ public PoliciesServiceGlobalEventRegistryTest() {

// added due to ditto-model-placeholders
SubscriptionComplete.class,
ThingDeleted.class
ThingDeleted.class,
EmptyEvent.class
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
package org.eclipse.ditto.services.things.starter;

import org.eclipse.ditto.services.models.things.DittoThingSnapshotTaken;
import org.eclipse.ditto.services.utils.persistentactors.EmptyEvent;
import org.eclipse.ditto.services.utils.test.GlobalEventRegistryTestCases;
import org.eclipse.ditto.signals.events.things.FeatureDeleted;

public final class ThingsServiceGlobalEventRegistryTest extends GlobalEventRegistryTestCases {

public ThingsServiceGlobalEventRegistryTest() {
super(FeatureDeleted.class, DittoThingSnapshotTaken.class);
super(FeatureDeleted.class, DittoThingSnapshotTaken.class, EmptyEvent.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,18 @@
package org.eclipse.ditto.services.utils.persistentactors;

import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.model.base.entity.id.DefaultEntityId;
import org.eclipse.ditto.model.base.entity.id.EntityId;
import org.eclipse.ditto.model.base.entity.metadata.Metadata;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeExceptionBuilder;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.headers.WithDittoHeaders;
import org.eclipse.ditto.model.base.json.FieldType;
import org.eclipse.ditto.model.base.json.JsonParsableEvent;
import org.eclipse.ditto.model.base.json.JsonSchemaVersion;
import org.eclipse.ditto.services.utils.akka.PingCommand;
import org.eclipse.ditto.services.utils.akka.PingCommandResponse;
Expand All @@ -53,7 +39,6 @@
import org.eclipse.ditto.services.utils.persistentactors.results.ResultVisitor;
import org.eclipse.ditto.signals.commands.base.Command;
import org.eclipse.ditto.signals.events.base.Event;
import org.eclipse.ditto.signals.events.base.EventJsonDeserializer;

import akka.actor.ActorRef;
import akka.japi.pf.ReceiveBuilder;
Expand Down Expand Up @@ -686,147 +671,4 @@ public String toString() {
}
}

/**
* An event which can be persisted to the backing event journal which does not contain any change "instruction" to
* alter the managed entity's state.
*/
@Immutable
@JsonParsableEvent(name = EmptyEvent.NAME, typePrefix= EmptyEvent.TYPE_PREFIX)
protected static final class EmptyEvent implements Event<EmptyEvent> {

/**
* Known effect of the "empty event" which shall keep an persistence actor always alive.
*/
public static final JsonValue EFFECT_ALWAYS_ALIVE = JsonValue.of("alwaysAlive");

private static final String TYPE_PREFIX = "persistence-actor-internal:";

private static final String NAME = "empty-event";

private static final String TYPE = TYPE_PREFIX + NAME;

private static final JsonFieldDefinition<String> JSON_ENTITY_ID =
JsonFactory.newStringFieldDefinition("entityId", FieldType.REGULAR, JsonSchemaVersion.V_2);

private static final JsonFieldDefinition<JsonValue> JSON_EFFECT =
JsonFactory.newJsonValueFieldDefinition("effect", FieldType.REGULAR, JsonSchemaVersion.V_2);

private final EntityId entityId;
private final JsonValue effect;
private final long revision;
private final DittoHeaders dittoHeaders;

public EmptyEvent(final EntityId entityId, final JsonValue effect, final long revision,
final DittoHeaders dittoHeaders) {
this.entityId = entityId;
this.revision = revision;
this.effect = effect;
this.dittoHeaders = dittoHeaders;
}

/**
* Creates a {@code EmptyEvent} event from a JSON object.
*
* @param jsonObject the JSON object of which the event is to be created.
* @param dittoHeaders the headers of the command which was the cause of this event.
* @return the event.
* @throws NullPointerException if any argument is {@code null}.
* @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected
* format.
*/
@SuppressWarnings("unused") // used via reflection
public static EmptyEvent fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
return new EventJsonDeserializer<EmptyEvent>(TYPE, jsonObject)
.deserialize((revision, timestamp, metadata) -> {
final EntityId readEntityId = DefaultEntityId.of(jsonObject.getValueOrThrow(JSON_ENTITY_ID));
final JsonValue readEffect = jsonObject.getValueOrThrow(JSON_EFFECT);
return new EmptyEvent(readEntityId, readEffect, revision, dittoHeaders);
});
}

/**
* Returns the effect of the empty event - might also be a Json {@code null} if no effect is provided.
*
* @return the effect of the empty event.
*/
public JsonValue getEffect() {
return effect;
}

@Override
public DittoHeaders getDittoHeaders() {
return dittoHeaders;
}

@Override
public EmptyEvent setDittoHeaders(final DittoHeaders dittoHeaders) {
return new EmptyEvent(entityId, effect, revision, dittoHeaders);
}

@Override
public String getType() {
return TYPE;
}

@Nonnull
@Override
public String getManifest() {
return getType();
}

@Override
public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) {
final JsonObjectBuilder jsonObjectBuilder = JsonFactory.newObjectBuilder()
.set(Event.JsonFields.TYPE, getType())
.set(JSON_ENTITY_ID, entityId.toString())
.set(JSON_EFFECT, effect);
return jsonObjectBuilder.build();
}

@Override
public EntityId getEntityId() {
return entityId;
}

@Override
public JsonPointer getResourcePath() {
return JsonPointer.empty();
}

@Override
public String getResourceType() {
return "empty";
}

@Override
public long getRevision() {
return revision;
}

@Override
public EmptyEvent setRevision(final long revision) {
return new EmptyEvent(entityId, effect, revision, dittoHeaders);
}

@Override
public Optional<Instant> getTimestamp() {
return Optional.empty();
}

@Override
public Optional<Metadata> getMetadata() {
return Optional.empty();
}

@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"entityId=" + entityId +
", effect=" + effect +
", revision=" + revision +
", dittoHeaders=" + dittoHeaders +
"]";
}
}

}

0 comments on commit 332cfb1

Please sign in to comment.