Skip to content

Commit

Permalink
remove api 1 support
Browse files Browse the repository at this point in the history
Signed-off-by: Yannic Klem <yannic.klem@bosch.io>
  • Loading branch information
Yannic92 committed Feb 24, 2021
1 parent 0d17ee9 commit ecdf25c
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 291 deletions.
Expand Up @@ -13,8 +13,6 @@

package org.eclipse.ditto.client.internal;

import static org.eclipse.ditto.client.internal.BusAddressPatterns.ACL_ENTRY_PATTERN;
import static org.eclipse.ditto.client.internal.BusAddressPatterns.ACL_PATTERN;
import static org.eclipse.ditto.client.internal.BusAddressPatterns.ATTRIBUTES_PATTERN;
import static org.eclipse.ditto.client.internal.BusAddressPatterns.ATTRIBUTE_PATTERN;
import static org.eclipse.ditto.client.internal.BusAddressPatterns.DEFINITION_PATTERN;
Expand Down Expand Up @@ -48,26 +46,6 @@ static String forThing(final ThingId thingId) {
return THING_PATTERN.format(thingId);
}

/**
* @param thingId the thingId that is part of the pattern
* @return bus pattern for the acl resource
* @deprecated Permissions belong to deprecated API version 1. Use API version 2 with policies instead.
*/
@Deprecated
static String forAcl(final ThingId thingId) {
return ACL_PATTERN.format(thingId);
}

/**
* @param thingId the thingId that is part of the pattern
* @return bus pattern for the acl entry resource
* @deprecated Permissions belong to deprecated API version 1. Use API version 2 with policies instead.
*/
@Deprecated
static String forAclEntry(final ThingId thingId, final String subjectId) {
return ACL_ENTRY_PATTERN.format(thingId, subjectId);
}

/**
* @param thingId the thingId that is part of the pattern
* @return bus pattern for the thing definition resource
Expand Down
Expand Up @@ -20,8 +20,6 @@
enum BusAddressPatterns {

THING_PATTERN("/things/{0}"),
ACL_PATTERN("/things/{0}/acl"),
ACL_ENTRY_PATTERN("/things/{0}/acl/{1}"),
ATTRIBUTES_PATTERN("/things/{0}/attributes"),
ATTRIBUTE_PATTERN("/things/{0}/attributes{1}"),
DEFINITION_PATTERN("/things/{0}/definition"),
Expand Down
Expand Up @@ -56,10 +56,6 @@
import org.eclipse.ditto.signals.acks.base.Acknowledgement;
import org.eclipse.ditto.signals.base.Signal;
import org.eclipse.ditto.signals.commands.base.ErrorResponse;
import org.eclipse.ditto.signals.events.things.AclEntryCreated;
import org.eclipse.ditto.signals.events.things.AclEntryDeleted;
import org.eclipse.ditto.signals.events.things.AclEntryModified;
import org.eclipse.ditto.signals.events.things.AclModified;
import org.eclipse.ditto.signals.events.things.AttributeCreated;
import org.eclipse.ditto.signals.events.things.AttributeDeleted;
import org.eclipse.ditto.signals.events.things.AttributeModified;
Expand Down Expand Up @@ -230,13 +226,6 @@ private static PoliciesImpl configurePolicyClient(final MessagingProvider messag
private static OutgoingMessageFactory getOutgoingMessageFactoryForPolicies(
final MessagingProvider messagingProvider) {
final JsonSchemaVersion schemaVersion = messagingProvider.getMessagingConfiguration().getJsonSchemaVersion();
if (JsonSchemaVersion.V_1.equals(schemaVersion)) {
LOGGER.warn("The MessagingProvider was configured with JsonSchemaVersion V_1 which is invalid for policy" +
" commands. Therefore defaulting to V_2 for all policy commands." +
" Please consider upgrading to JsonSchemaVersion V_2 as V_1 is deprecated and will be removed" +
" in an upcoming release.");
return OutgoingMessageFactory.newInstance(JsonSchemaVersion.V_2);
}
return OutgoingMessageFactory.newInstance(schemaVersion);
}

Expand Down Expand Up @@ -331,48 +320,6 @@ private static void registerKeyBasedHandlersForIncomingEvents(final PointerBus b
emitAcknowledgement)
);

/*
* ACL - v1 only
* @deprecated as part of deprecated API 1
*/
SelectorUtil.addHandlerForThingEvent(LOGGER, bus, AclModified.TYPE, AclModified.class,
e -> BusAddressFactory.forAcl(e.getThingEntityId()),
(e, extra) -> new ImmutableChange(e.getThingEntityId(), ChangeAction.UPDATED,
JsonPointer.empty(),
e.getAccessControlList().toJson(e.getImplementedSchemaVersion()),
e.getRevision(), e.getTimestamp().orElse(null), extra, e.getDittoHeaders(),
emitAcknowledgement)
);

SelectorUtil.addHandlerForThingEvent(LOGGER, bus, AclEntryCreated.TYPE, AclEntryCreated.class,
e -> BusAddressFactory.forAclEntry(e.getThingEntityId(),
e.getAclEntry().getAuthorizationSubject().getId()),
(e, extra) -> new ImmutableChange(e.getThingEntityId(), ChangeAction.CREATED,
JsonPointer.empty(),
e.getAclEntry().toJson(e.getImplementedSchemaVersion()),
e.getRevision(), e.getTimestamp().orElse(null), extra, e.getDittoHeaders(),
emitAcknowledgement)
);

SelectorUtil.addHandlerForThingEvent(LOGGER, bus, AclEntryModified.TYPE, AclEntryModified.class,
e -> BusAddressFactory.forAclEntry(e.getThingEntityId(),
e.getAclEntry().getAuthorizationSubject().getId()),
(e, extra) -> new ImmutableChange(e.getThingEntityId(), ChangeAction.UPDATED,
JsonPointer.empty(),
e.getAclEntry().toJson(e.getImplementedSchemaVersion()),
e.getRevision(), e.getTimestamp().orElse(null), extra, e.getDittoHeaders(),
emitAcknowledgement)
);

SelectorUtil.addHandlerForThingEvent(LOGGER, bus, AclEntryDeleted.TYPE, AclEntryDeleted.class,
e -> BusAddressFactory.forAclEntry(e.getThingEntityId(), e.getAuthorizationSubject().getId()),
(e, extra) -> new ImmutableChange(e.getThingEntityId(), ChangeAction.DELETED,
JsonPointer.empty(),
null,
e.getRevision(), e.getTimestamp().orElse(null), extra, e.getDittoHeaders(),
emitAcknowledgement)
);

/*
* Attributes
*/
Expand Down
Expand Up @@ -89,8 +89,6 @@
*/
public final class OutgoingMessageFactory {

private static final Logger LOGGER = LoggerFactory.getLogger(OutgoingMessageFactory.class);

private static final EntityTagMatchers ASTERISK =
EntityTagMatchers.fromList(Collections.singletonList(EntityTagMatcher.asterisk()));

Expand Down Expand Up @@ -122,8 +120,6 @@ public static OutgoingMessageFactory newInstance(final JsonSchemaVersion jsonSch
*/
public CreateThing createThing(final Thing thing, @Nullable JsonObject initialPolicy,
final Option<?>... options) {
logWarningsForAclPolicyUsage(thing);

validateOptions(initialPolicy, options);

final DittoHeaders dittoHeaders = buildDittoHeaders(false, options);
Expand All @@ -150,8 +146,6 @@ public ModifyThing putThing(final Thing thing, @Nullable final JsonObject initia
checkNotNull(thing, "thing");
final ThingId thingId = thing.getEntityId().orElseThrow(() -> new IllegalArgumentException("Thing had no ID!"));

logWarningsForAclPolicyUsage(thing);

validateOptions(initialPolicy, options);

final DittoHeaders dittoHeaders = buildDittoHeaders(true, options);
Expand All @@ -176,8 +170,6 @@ public ModifyThing updateThing(final Thing thing, final Option<?>... options) {
checkNotNull(thing, "thing");
final ThingId thingId = thing.getEntityId().orElseThrow(() -> new IllegalArgumentException("Thing had no ID!"));

logWarningsForAclPolicyUsage(thing);

final DittoHeaders headersWithoutIfMatch = buildDittoHeaders(false, options);
final DittoHeaders headers = headersWithoutIfMatch.toBuilder()
.ifMatch(ASTERISK)
Expand All @@ -197,7 +189,6 @@ public ModifyThing updateThing(final Thing thing, final Option<?>... options) {
*/
MergeThing mergeThing(final ThingId thingId, final Thing thing, final Option<?>[] options) {
checkNotNull(thing, "thing");
logWarningsForAclPolicyUsage(thing);

final DittoHeaders headersWithoutIfMatch = buildDittoHeaders(false, options);
final DittoHeaders headers = headersWithoutIfMatch.toBuilder()
Expand All @@ -207,19 +198,6 @@ MergeThing mergeThing(final ThingId thingId, final Thing thing, final Option<?>[
return MergeThing.withThing(thingId, thing, headers);
}

private void logWarningsForAclPolicyUsage(final Thing thing) {
if (jsonSchemaVersion == JsonSchemaVersion.V_1 && thing.getPolicyEntityId().isPresent()) {
LOGGER.warn("Creating/modifying a Thing with a defined 'policyId' when client was configured to use " +
"Ditto Protocol in 'schemaVersion' 1 (which is ACL based). That will most likely result in " +
"unexpected behavior.");
}
if (jsonSchemaVersion == JsonSchemaVersion.V_2 && thing.getAccessControlList().isPresent()) {
LOGGER.warn("Creating/modifying a Thing with a defined 'acl' when client was configured to use " +
"Ditto Protocol in 'schemaVersion' 2 (which is policy based). That will most likely result in " +
"unexpected behavior.");
}
}

public RetrieveThing retrieveThing(final CharSequence thingId) {
return RetrieveThing.of(ThingId.of(thingId), buildDittoHeaders(false));
}
Expand Down
Expand Up @@ -57,16 +57,6 @@ public String visitPolicyId(final JsonPointer path, @Nullable final ThingId thin
return BusAddressFactory.forPolicyId(checkNotNull(thingId));
}

@Override
public String visitAcl(final JsonPointer path, @Nullable final ThingId thingId) {
return BusAddressFactory.forAcl(checkNotNull(thingId));
}

@Override
public String visitAclEntry(final JsonPointer path, @Nullable final ThingId thingId) {
return BusAddressFactory.forAclEntry(checkNotNull(thingId), extractSubjectId(path));
}

@Override
public String visitAttributes(final JsonPointer path, @Nullable final ThingId thingId) {
return BusAddressFactory.forAttributes(checkNotNull(thingId));
Expand Down
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2020 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.client.live.messages;

import java.util.function.BiConsumer;

/**
* Interface encapsulating a {@link java.util.function.BiConsumer} which is notified about responses with either the
* response of type {@link R} (if it was successful) or with an {@link Throwable} if there occurred an error.
* Does also hold the type of the expected Message response.
*
* @param <R> the type of the expected response. Can be equal to {@link C} if consumed signal is the actual response.
* @param <C> the type of the expected consumed signal.
*/
public interface ResponseConsumer<R, C> {

/**
* Returns the type of the expected response.
*
* @return the type of the expected response.
*/
Class<R> getResponseType();

/**
* The BiConsumer which is notified about responses with either
* the response of type {@link R} (if it was successful) or with an {@link Throwable} if there occurred an error.
*
* @return the BiConsumer notified about responses.
*/
BiConsumer<C, Throwable> getResponseConsumer();
}
Expand Up @@ -17,7 +17,6 @@
import static org.eclipse.ditto.json.JsonFactory.emptyPointer;
import static org.eclipse.ditto.json.JsonFactory.newObjectBuilder;
import static org.eclipse.ditto.json.JsonFactory.newPointer;
import static org.eclipse.ditto.model.base.auth.AuthorizationModelFactory.newAuthSubject;
import static org.junit.Assert.assertEquals;

import java.util.UUID;
Expand All @@ -42,7 +41,6 @@
import org.eclipse.ditto.model.things.Attributes;
import org.eclipse.ditto.model.things.Feature;
import org.eclipse.ditto.model.things.FeatureProperties;
import org.eclipse.ditto.model.things.Permission;
import org.eclipse.ditto.model.things.Thing;
import org.eclipse.ditto.model.things.ThingId;
import org.eclipse.ditto.model.things.ThingsModelFactory;
Expand Down Expand Up @@ -122,23 +120,10 @@ public class ChangeUpwardsDownwardsPropagationTest extends AbstractDittoClientTe
public void before() {
super.before();
thingId1 = newThingId(UUID.randomUUID().toString());
thing1 = ThingsModelFactory.newThingBuilder().setId(thingId1)
.setPermissions(
ThingsModelFactory.newAclEntry(newAuthSubject(TEST_USER_SID), Permission.READ,
Permission.WRITE),
ThingsModelFactory.newAclEntry(newAuthSubject(TestConstants.CLIENT_ID),
Permission.READ, Permission.WRITE,
Permission.ADMINISTRATE))
.build();
thing1 = ThingsModelFactory.newThingBuilder().setId(thingId1).build();

thingId2 = newThingId(UUID.randomUUID().toString());
thing2withAttributes = ThingsModelFactory.newThingBuilder().setId(thingId2)
.setPermissions(
ThingsModelFactory.newAclEntry(newAuthSubject(TEST_USER_SID), Permission.READ,
Permission.WRITE),
ThingsModelFactory.newAclEntry(newAuthSubject(TestConstants.CLIENT_ID),
Permission.READ, Permission.WRITE,
Permission.ADMINISTRATE))
.setAttributes(ATTRIBUTES2)
.build();

Expand Down Expand Up @@ -177,8 +162,9 @@ public void testUpwardsRegisterForThingChangeWhenThingIsCreated() throws Excepti

final Message<ThingEvent> thingCreated =
MessagesModelFactory.<ThingEvent>newMessageBuilder(messageHeaders).payload(
ThingCreated.of(thing1, 1,
DittoHeaders.newBuilder().schemaVersion(JsonSchemaVersion.V_1).build()
ThingCreated.of(thing1, 1, null,
DittoHeaders.newBuilder().schemaVersion(JsonSchemaVersion.V_2).build(),
null
)).build();

messaging.receiveEvent(thingCreated);
Expand Down Expand Up @@ -208,8 +194,9 @@ public void testSignalEnrichment() throws Exception {
.build();
final Message<ThingEvent> thingCreated =
MessagesModelFactory.<ThingEvent>newMessageBuilder(messageHeaders)
.payload(ThingCreated.of(thing1, 1,
DittoHeaders.newBuilder().schemaVersion(JsonSchemaVersion.V_1).build()
.payload(ThingCreated.of(thing1, 1, null,
DittoHeaders.newBuilder().schemaVersion(JsonSchemaVersion.V_2).build(),
null
))
.extra(extra)
.build();
Expand Down Expand Up @@ -258,8 +245,9 @@ public void testMultipleChangeHandlersAreInvokedOnSingleChange() throws Exceptio

final Message<ThingEvent> thingCreated =
MessagesModelFactory.<ThingEvent>newMessageBuilder(messageHeaders).payload(
ThingCreated.of(thing1, 1,
DittoHeaders.newBuilder().schemaVersion(JsonSchemaVersion.V_1).build()
ThingCreated.of(thing1, 1, null,
DittoHeaders.newBuilder().schemaVersion(JsonSchemaVersion.V_2).build(),
null
)).build();

// only create the Thing once:
Expand Down Expand Up @@ -297,7 +285,7 @@ public void testUpwardsRegisterForThingChangeWhenThingIsDeleted() throws Excepti

final Message<ThingEvent> thingDeleted =
MessagesModelFactory.<ThingEvent>newMessageBuilder(messageHeaders).payload(
ThingDeleted.of(thingId1, 1, DittoHeaders.empty())).build();
ThingDeleted.of(thingId1, 1, null, DittoHeaders.empty(), null)).build();

messaging.receiveEvent(thingDeleted);

Expand Down Expand Up @@ -337,8 +325,8 @@ public void testUpwardsRegisterForThingChangeWhenAttributesAreModified() throws
// set the attributes
final Message<ThingEvent> attributesCreated =
MessagesModelFactory.<ThingEvent>newMessageBuilder(messageHeaders).payload(
AttributesCreated.of(thingId1, ThingsModelFactory.newAttributes(attributesToSet), 1,
DittoHeaders.empty())).build();
AttributesCreated.of(thingId1, ThingsModelFactory.newAttributes(attributesToSet), 1, null,
DittoHeaders.empty(), null)).build();

messaging.receiveEvent(attributesCreated);

Expand Down Expand Up @@ -385,7 +373,8 @@ public void testUpwardsRegisterForThingChangeWhenSingleAttributeIsModified() thr
// set the attributes
final Message<ThingEvent> attributeCreated =
MessagesModelFactory.<ThingEvent>newMessageBuilder(messageHeaders).payload(
AttributeCreated.of(thingId1, newAttribute, complex, 1, DittoHeaders.empty())).build();
AttributeCreated.of(thingId1, newAttribute, complex, 1, null, DittoHeaders.empty(), null))
.build();

messaging.receiveEvent(attributeCreated);

Expand Down Expand Up @@ -560,7 +549,7 @@ public void testDownwardsRegisterForSingleAttributeChangeWhenThingIsCreated() th
// create the thing with attributes
final Message<ThingEvent> thingCreated =
MessagesModelFactory.<ThingEvent>newMessageBuilder(messageHeaders).payload(
ThingCreated.of(thing2withAttributes, 1, DittoHeaders.empty())).build();
ThingCreated.of(thing2withAttributes, 1, null, DittoHeaders.empty(), null)).build();

messaging.receiveEvent(thingCreated);

Expand Down
Expand Up @@ -28,11 +28,9 @@

import org.assertj.core.api.Assertions;
import org.eclipse.ditto.client.internal.AbstractDittoClientTest;
import org.eclipse.ditto.client.messaging.internal.MockMessagingProvider;
import org.eclipse.ditto.client.options.Options;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonMissingFieldException;
import org.eclipse.ditto.model.base.json.JsonSchemaVersion;
import org.eclipse.ditto.model.policies.Policy;
import org.eclipse.ditto.signals.commands.policies.PolicyCommand;
import org.eclipse.ditto.signals.commands.policies.PolicyCommandResponse;
Expand All @@ -53,19 +51,6 @@
*/
public final class DittoClientPoliciesTest extends AbstractDittoClientTest {

@Test
public void verifyClientDefaultsToSchemaVersion2ForPolicyCommands() {
messaging = new MockMessagingProvider(JsonSchemaVersion.V_1);
final DittoClient client = DittoClients.newInstance(messaging)
.connect()
.toCompletableFuture()
.join();
assertEventualCompletion(client.policies().retrieve(POLICY_ID).thenRun(client::destroy));
final RetrievePolicy command = expectMsgClass(RetrievePolicy.class);
reply(RetrievePolicyResponse.of(POLICY_ID, POLICY, command.getDittoHeaders()));
assertThat(command).hasSchemaVersion(JsonSchemaVersion.V_2);
}

@Test
public void testCreatePolicy() throws Exception {
final CompletableFuture<Policy> policyCompletableFuture =
Expand Down

0 comments on commit ecdf25c

Please sign in to comment.