Skip to content

Commit

Permalink
fix calculation of ThingId/PolicyId when create command did not conta…
Browse files Browse the repository at this point in the history
…in an ID

Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch.io>
  • Loading branch information
thjaeckle committed May 9, 2022
1 parent 57040d1 commit e20c472
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
Expand Up @@ -142,14 +142,21 @@ private static CreatePolicy prependDefaultNamespaceToCreatePolicy(final CreatePo
final Optional<String> namespace = policy.getNamespace();
if (!namespace.isPresent() || namespace.get().equals("")) {
final Policy policyInDefaultNamespace = policy.toBuilder()
.setId(PolicyId.of(DEFAULT_NAMESPACE, createPolicy.getEntityId().toString().substring(1)))
.setId(calculatePolicyId(createPolicy))
.build();
return new CreatePolicy(policyInDefaultNamespace, createPolicy.getDittoHeaders());
} else {
return createPolicy;
}
}

private static PolicyId calculatePolicyId(final CreatePolicy createPolicy) {
final Optional<PolicyId> providedPolicyId = createPolicy.getPolicy().getEntityId();
return providedPolicyId
.map(policyId -> PolicyId.of(DEFAULT_NAMESPACE, policyId.toString().substring(1)))
.orElseGet(() -> PolicyId.inNamespaceWithRandomName(DEFAULT_NAMESPACE));
}

/**
* Returns the {@code Policy} to create.
*
Expand Down
Expand Up @@ -241,7 +241,7 @@ private static CreateThing prependDefaultNamespaceToCreateThing(final CreateThin
final Optional<String> namespace = thing.getNamespace();
if (!namespace.isPresent() || namespace.get().equals("")) {
final Thing thingInDefaultNamespace = thing.toBuilder()
.setId(ThingId.of(DEFAULT_NAMESPACE, createThing.getEntityId().toString().substring(1)))
.setId(calculateThingId(createThing))
.build();
final JsonObject initialPolicy = createThing.getInitialPolicy().orElse(null);
return new CreateThing(thingInDefaultNamespace, initialPolicy, createThing.getDittoHeaders());
Expand All @@ -250,6 +250,13 @@ private static CreateThing prependDefaultNamespaceToCreateThing(final CreateThin
}
}

private static ThingId calculateThingId(final CreateThing createThing) {
final Optional<ThingId> providedThingId = createThing.getThing().getEntityId();
return providedThingId
.map(thingId -> ThingId.of(DEFAULT_NAMESPACE, thingId.toString().substring(1)))
.orElseGet(() -> ThingId.inNamespaceWithRandomName(DEFAULT_NAMESPACE));
}

/**
* Returns the {@code Thing} to create.
*
Expand Down
Expand Up @@ -25,7 +25,6 @@
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.signals.commands.modify.CreateThing;
import org.junit.Ignore;
import org.junit.Test;

/**
Expand Down Expand Up @@ -92,12 +91,14 @@ public void filterOutOtherBuiltInDittoAcknowledgementLabels() {
AcknowledgementRequest.of(DittoAcknowledgementLabel.SEARCH_PERSISTED))
.randomCorrelationId()
.build();
final CreateThing command = CreateThing.of(Thing.newBuilder().build(), null, dittoHeaders);

final Thing newThing = Thing.newBuilder().setGeneratedId().build();
final CreateThing command = CreateThing.of(newThing, null, dittoHeaders);
final DittoHeaders expectedHeaders = dittoHeaders.toBuilder()
.acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.LIVE_RESPONSE))
.responseRequired(true)
.build();
final CreateThing expected = CreateThing.of(Thing.newBuilder().build(), null, expectedHeaders);
final CreateThing expected = CreateThing.of(newThing, null, expectedHeaders);
final ThingLiveCommandAckRequestSetter underTest = ThingLiveCommandAckRequestSetter.getInstance();

assertThat(underTest.apply(command)).isEqualTo(expected);
Expand Down
Expand Up @@ -23,7 +23,6 @@
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.signals.commands.modify.CreateThing;
import org.junit.Ignore;
import org.junit.Test;

/**
Expand Down Expand Up @@ -80,13 +79,15 @@ public void filterOutLiveResponseLabel() {
AcknowledgementRequest.of(DittoAcknowledgementLabel.SEARCH_PERSISTED))
.randomCorrelationId()
.build();
final CreateThing command = CreateThing.of(Thing.newBuilder().build(), null, dittoHeaders);

final Thing newThing = Thing.newBuilder().setGeneratedId().build();
final CreateThing command = CreateThing.of(newThing, null, dittoHeaders);
final DittoHeaders expectedHeaders = dittoHeaders.toBuilder()
.acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.TWIN_PERSISTED),
AcknowledgementRequest.of(DittoAcknowledgementLabel.SEARCH_PERSISTED))
.responseRequired(true)
.build();
final CreateThing expected = CreateThing.of(Thing.newBuilder().build(), null, expectedHeaders);
final CreateThing expected = CreateThing.of(newThing, null, expectedHeaders);
final ThingModifyCommandAckRequestSetter underTest = ThingModifyCommandAckRequestSetter.getInstance();

assertThat(underTest.apply(command)).isEqualTo(expected);
Expand Down

0 comments on commit e20c472

Please sign in to comment.