Skip to content

Commit

Permalink
Added unit test for checking that serialization with jackson-cbor wor…
Browse files Browse the repository at this point in the history
…ks for `JsonValueSourceRef`.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Feb 24, 2021
1 parent 3fb92b8 commit f578260
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
Expand Up @@ -19,21 +19,27 @@
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.typesafe.config.ConfigFactory;

import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.stream.Materializer;
import akka.stream.SourceRef;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
import akka.stream.javadsl.StreamRefs;
import akka.testkit.TestKit;
import akka.testkit.javadsl.TestKit;
import nl.jqno.equalsverifier.EqualsVerifier;
import scala.concurrent.duration.FiniteDuration;

Expand Down Expand Up @@ -79,23 +85,51 @@ public void getInstanceWithNullSourceRef() {

@Test
public void getSourceRefReturnsExpected() {
final var jsonValueSourceRef = getSourceRef();
final var sourceRef = getSourceRef(List.of(JsonValue.of("Hello"), JsonValue.of(" "), JsonValue.of("Ditto!")));

final var underTest = JsonValueSourceRef.of(jsonValueSourceRef);
final var underTest = JsonValueSourceRef.of(sourceRef);

assertThat(underTest.getSourceRef()).isEqualTo(jsonValueSourceRef);
assertThat(underTest.getSourceRef()).isEqualTo(sourceRef);
}

@Test
public void getSourceReturnsNotNull() {
final var underTest = JsonValueSourceRef.of(getSourceRef());
public void getSourceReturnsNotNull() throws InterruptedException, ExecutionException, TimeoutException {
final var jsonValues = List.of(JsonValue.of("Hello"), JsonValue.of(" "), JsonValue.of("Ditto!"));
final var underTest = JsonValueSourceRef.of(getSourceRef(jsonValues));

assertThat(underTest.getSource()).isNotNull();
final var actualJsonValues = materializeSource(underTest.getSource());

assertThat(actualJsonValues).isEqualTo(jsonValues);
}

private static SourceRef<JsonValue> getSourceRef() {
final var source = Source.from(List.of(JsonValue.of("Hello"), JsonValue.of(" "), JsonValue.of("Ditto!")));
@Test
public void serializationWorks() throws InterruptedException, ExecutionException, TimeoutException {
final var jsonValues = List.of(JsonValue.of("Hello!"), JsonObject.empty(), JsonArray.of(1, 2, true));
final var underTest = JsonValueSourceRef.of(getSourceRef(jsonValues));
final var messageReceiver = new TestKit(actorSystem);
final var messageSender = new TestKit(actorSystem);
final var messageReceiverRef = messageReceiver.getRef();

messageReceiverRef.tell(underTest, messageSender.getRef());

final var deserializedJsonValueSourceRef = messageReceiver.expectMsgClass(underTest.getClass());

final var deserializedJsonValues = materializeSource(deserializedJsonValueSourceRef.getSource());

assertThat(deserializedJsonValues).isEqualTo(jsonValues);
}

private static SourceRef<JsonValue> getSourceRef(final Iterable<JsonValue> jsonValues) {
final var source = Source.from(jsonValues);
return source.runWith(StreamRefs.sourceRef(), Materializer.apply(actorSystem));
}

private static List<JsonValue> materializeSource(final Source<JsonValue, NotUsed> source)
throws InterruptedException, ExecutionException, TimeoutException {

final var completionStage = source.runWith(Sink.seq(), actorSystem);
final var completableFuture = completionStage.toCompletableFuture();
return completableFuture.get(3, TimeUnit.SECONDS);
}

}
Expand Up @@ -3,10 +3,12 @@ akka {
serialize-messages = on

serializers {
cbor-json-value = "org.eclipse.ditto.services.utils.cluster.CborJsonValueSerializer"
jackson-cbor = "akka.serialization.jackson.JacksonCborSerializer"
}

serialization-bindings {
"org.eclipse.ditto.json.JsonValue" = cbor-json-value
"org.eclipse.ditto.services.utils.cluster.AkkaJacksonCborSerializable" = jackson-cbor
}
}
Expand Down

0 comments on commit f578260

Please sign in to comment.