Skip to content

Commit

Permalink
Issue #561: reviewed facade - renamed, moved out of separate package,…
Browse files Browse the repository at this point in the history
… removed initialization of roundtrip facade in interface

Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch-si.com>
  • Loading branch information
thjaeckle committed Dec 13, 2019
1 parent d263048 commit 410cbad
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 56 deletions.
Expand Up @@ -10,43 +10,30 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.models.things.facade;
package org.eclipse.ditto.services.models.things;

import java.time.Duration;
import java.util.concurrent.CompletionStage;

import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.things.ThingId;

import akka.actor.ActorRef;

/**
* Asynchronous interface for retrieving fixed parts of things.
* either by request-response, by caching or by any other source of information.
*/
public interface PartialThingFacade {
public interface ThingEnrichingFacade {

/**
* Retrieve parts of a thing.
*
* @param thingId ID of the thing.
* @param jsonFieldSelector the selected fields of the thing.
* @param dittoHeaders Ditto headers containing authorization information.
* @return future that completes with the parts of a thing or fails with an error.
*/
CompletionStage<JsonObject> retrievePartialThing(ThingId thingId, DittoHeaders dittoHeaders);
CompletionStage<JsonObject> retrievePartialThing(ThingId thingId, JsonFieldSelector jsonFieldSelector,
DittoHeaders dittoHeaders);

/**
* Create a new partial-thing-facade by round-trip.
*
* @param commandHandler the actor who handles retrieve-thing commands.
* @param jsonFieldSelector the selected fields of the thing.
* @param askTimeout how long to wait for a response.
* @return the partial-thing-facade by round-trip.
*/
static PartialThingFacade byRoundTrip(final ActorRef commandHandler, final JsonFieldSelector jsonFieldSelector,
final Duration askTimeout) {
return new PartialThingFacadeByRoundTrip(jsonFieldSelector, commandHandler, askTimeout);
}
}
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.models.things.facade;
package org.eclipse.ditto.services.models.things;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;
Expand All @@ -30,28 +30,26 @@
/**
* Retrieve fixed parts of things by asking an actor.
*/
final class PartialThingFacadeByRoundTrip implements PartialThingFacade {
final class ThingEnrichingFacadeByRoundTrip implements ThingEnrichingFacade {

private final JsonFieldSelector jsonFieldSelector;
private final ActorRef commandHandler;
private final Duration askTimeout;

PartialThingFacadeByRoundTrip(final JsonFieldSelector jsonFieldSelector,
final ActorRef commandHandler, final Duration askTimeout) {
this.jsonFieldSelector = jsonFieldSelector;
ThingEnrichingFacadeByRoundTrip(final ActorRef commandHandler, final Duration askTimeout) {
this.commandHandler = commandHandler;
this.askTimeout = askTimeout;
}

@Override
public CompletionStage<JsonObject> retrievePartialThing(final ThingId thingId, final DittoHeaders dittoHeaders) {
public CompletionStage<JsonObject> retrievePartialThing(final ThingId thingId,
final JsonFieldSelector jsonFieldSelector, final DittoHeaders dittoHeaders) {

final RetrieveThing command =
RetrieveThing.getBuilder(thingId, dittoHeaders).withSelectedFields(jsonFieldSelector).build();

final CompletionStage<Object> askResult = Patterns.ask(commandHandler, command, askTimeout);

return askResult.thenCompose(PartialThingFacadeByRoundTrip::extractPartialThing);
return askResult.thenCompose(ThingEnrichingFacadeByRoundTrip::extractPartialThing);
}

private static CompletionStage<JsonObject> extractPartialThing(final Object object) {
Expand Down

This file was deleted.

Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.models.things.facade;
package org.eclipse.ditto.services.models.things;

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

Expand All @@ -30,7 +30,7 @@

import akka.pattern.AskTimeoutException;

public final class PartialThingFacadeByRoundTripTest {
public final class ThingEnrichingFacadeByRoundTripTest {

private static final JsonFieldSelector SELECTOR =
JsonFieldSelector.newInstance("policyId,attributes/x,features/y/properties/z");
Expand All @@ -39,11 +39,11 @@ public final class PartialThingFacadeByRoundTripTest {
public void success() {
DittoTestSystem.run(this, kit -> {
// GIVEN: PartialThingFacadeByRoundTrip.retrievePartialThing()
final PartialThingFacade underTest =
PartialThingFacade.byRoundTrip(kit.getRef(), SELECTOR, Duration.ofSeconds(10L));
final ThingEnrichingFacade underTest =
new ThingEnrichingFacadeByRoundTrip(kit.getRef(), Duration.ofSeconds(10L));
final ThingId thingId = ThingId.dummy();
final DittoHeaders headers = DittoHeaders.newBuilder().correlationId(UUID.randomUUID().toString()).build();
final CompletionStage<JsonObject> askResult = underTest.retrievePartialThing(thingId, headers);
final CompletionStage<JsonObject> askResult = underTest.retrievePartialThing(thingId, SELECTOR, headers);

// WHEN: Command handler receives expected RetrieveThing and responds with RetrieveThingResponse
kit.expectMsg(RetrieveThing.getBuilder(thingId, headers).withSelectedFields(SELECTOR).build());
Expand All @@ -64,11 +64,11 @@ public void success() {
public void thingNotAccessible() {
DittoTestSystem.run(this, kit -> {
// GIVEN: PartialThingFacadeByRoundTrip.retrievePartialThing()
final PartialThingFacade underTest =
PartialThingFacade.byRoundTrip(kit.getRef(), SELECTOR, Duration.ofSeconds(10L));
final ThingEnrichingFacade underTest =
new ThingEnrichingFacadeByRoundTrip(kit.getRef(), Duration.ofSeconds(10L));
final ThingId thingId = ThingId.dummy();
final DittoHeaders headers = DittoHeaders.newBuilder().correlationId(UUID.randomUUID().toString()).build();
final CompletionStage<JsonObject> askResult = underTest.retrievePartialThing(thingId, headers);
final CompletionStage<JsonObject> askResult = underTest.retrievePartialThing(thingId, SELECTOR, headers);

// WHEN: Command handler receives expected RetrieveThing and responds with ThingNotAccessibleException
kit.expectMsg(RetrieveThing.getBuilder(thingId, headers).withSelectedFields(SELECTOR).build());
Expand All @@ -86,11 +86,11 @@ public void thingNotAccessible() {
public void unexpectedMessage() {
DittoTestSystem.run(this, kit -> {
// GIVEN: PartialThingFacadeByRoundTrip.retrievePartialThing()
final PartialThingFacade underTest =
PartialThingFacade.byRoundTrip(kit.getRef(), SELECTOR, Duration.ofSeconds(10L));
final ThingEnrichingFacade underTest =
new ThingEnrichingFacadeByRoundTrip(kit.getRef(), Duration.ofSeconds(10L));
final ThingId thingId = ThingId.dummy();
final DittoHeaders headers = DittoHeaders.newBuilder().correlationId(UUID.randomUUID().toString()).build();
final CompletionStage<JsonObject> askResult = underTest.retrievePartialThing(thingId, headers);
final CompletionStage<JsonObject> askResult = underTest.retrievePartialThing(thingId, SELECTOR, headers);

// WHEN: Command handler receives expected RetrieveThing and responds with a random object
kit.expectMsg(RetrieveThing.getBuilder(thingId, headers).withSelectedFields(SELECTOR).build());
Expand All @@ -109,11 +109,11 @@ public void unexpectedMessage() {
public void timeout() {
DittoTestSystem.run(this, kit -> {
// GIVEN: PartialThingFacadeByRoundTrip.retrievePartialThing() with a short timeout
final PartialThingFacade underTest =
PartialThingFacade.byRoundTrip(kit.getRef(), SELECTOR, Duration.ofMillis(1L));
final ThingEnrichingFacade underTest =
new ThingEnrichingFacadeByRoundTrip(kit.getRef(), Duration.ofMillis(1L));
final ThingId thingId = ThingId.dummy();
final DittoHeaders headers = DittoHeaders.newBuilder().correlationId(UUID.randomUUID().toString()).build();
final CompletionStage<JsonObject> askResult = underTest.retrievePartialThing(thingId, headers);
final CompletionStage<JsonObject> askResult = underTest.retrievePartialThing(thingId, SELECTOR, headers);

// WHEN: Command handler does not respond
kit.expectMsg(RetrieveThing.getBuilder(thingId, headers).withSelectedFields(SELECTOR).build());
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down

0 comments on commit 410cbad

Please sign in to comment.