Skip to content

Commit

Permalink
#288 fix BusAddresFactory and PathToBusBusAddressVisitor and add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Guggemos <dominik.guggemos@bosch.io>
  • Loading branch information
dguggemos committed Feb 1, 2021
1 parent 082d9de commit 1cae413
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
import static org.eclipse.ditto.client.internal.BusAddressPatterns.FEATURES_PATTERN;
import static org.eclipse.ditto.client.internal.BusAddressPatterns.FEATURE_DEFINITION_PATTERN;
import static org.eclipse.ditto.client.internal.BusAddressPatterns.FEATURE_DESIRED_PROPERTIES_PATTERN;
Expand All @@ -39,65 +40,136 @@ class BusAddressFactory {
private static final ThingResourceMapper<ThingId, String> RESOURCE_PATH_MAPPER =
ThingResourceMapper.from(PathToBusAddressVisitor.getInstance());

/**
* @param thingId the thingId that is part of the pattern
* @return bus pattern for the thing resource
*/
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);
}

static String forThingDefinition(final ThingId thingId, final String subjectId) {
return ACL_PATTERN.format(thingId, subjectId);
/**
* @param thingId the thingId that is part of the pattern
* @return bus pattern for the thing definition resource
*/
static String forThingDefinition(final ThingId thingId) {
return DEFINITION_PATTERN.format(thingId);
}

/**
* @param thingId the thingId that is part of the pattern
* @return bus pattern for the policy id resource
*/
static String forPolicyId(final ThingId thingId) {
return POLICY_ID_PATTERN.format(thingId);
}

/**
* @param thingId the thingId that is part of the pattern
* @return bus pattern for the attributes resource
*/
static String forAttributes(final ThingId thingId) {
return ATTRIBUTES_PATTERN.format(thingId);
}

/**
* @param thingId the thingId that is part of the pattern
* @param attributePath the attributePath that is part of the pattern
* @return bus pattern for an attribute resource
*/
static String forAttribute(final ThingId thingId, final JsonPointer attributePath) {
return ATTRIBUTE_PATTERN.format(thingId, attributePath);
}

/**
* @param thingId the thingId that is part of the pattern
* @param featureId the feature that is part of the pattern
* @return bus pattern for the feature resource
*/
static String forFeature(final ThingId thingId, final String featureId) {
return FEATURE_PATTERN.format(thingId, featureId);
}

/**
* @param thingId the thingId that is part of the pattern
* @return bus pattern for the features resource
*/
static String forFeatures(final ThingId thingId) {
return FEATURES_PATTERN.format(thingId);
}

/**
* @param thingId the thingId that is part of the pattern
* @param featureId the feature that is part of the pattern
* @return bus pattern for the feature definition resource
*/
static String forFeatureDefinition(final ThingId thingId, final String featureId) {
return FEATURE_DEFINITION_PATTERN.format(thingId, featureId);
}

/**
* @param thingId the thingId that is part of the pattern
* @param featureId the feature that is part of the pattern
* @return bus pattern for the feature properties resource
*/
static String forFeatureProperties(final ThingId thingId, final String featureId) {
return FEATURE_PROPERTIES_PATTERN.format(thingId, featureId);
}

/**
* @param thingId the thingId that is part of the pattern
* @param featureId the feature that is part of the pattern
* @param propertyPath the propertyPath that is part of the pattern
* @return bus pattern for the feature property resource
*/
static String forFeatureProperty(final ThingId thingId, final String featureId, final JsonPointer propertyPath) {
return FEATURE_PROPERTY_PATTERN.format(thingId, featureId, propertyPath);
}

/**
* @param thingId the thingId that is part of the pattern
* @param featureId the feature that is part of the pattern
* @return bus pattern for the desired properties resource
*/
static String forFeatureDesiredProperties(final ThingId thingId, final String featureId) {
return FEATURE_DESIRED_PROPERTIES_PATTERN.format(thingId, featureId);
}

/**
* @param thingId the thingId that is part of the pattern
* @param featureId the feature that is part of the pattern
* @param desiredPropertyPath the desiredPropertyPath that is part of the pattern
* @return bus pattern for the desired property resource
*/
static String forFeatureDesiredProperty(final ThingId thingId, final String featureId,
final JsonPointer propertyPath) {
return FEATURE_DESIRED_PROPERTY_PATTERN.format(thingId, featureId, propertyPath);
final JsonPointer desiredPropertyPath) {
return FEATURE_DESIRED_PROPERTY_PATTERN.format(thingId, featureId, desiredPropertyPath);
}

/**
* @param thingId the thingId that is part of the pattern
* @return bus pattern for a ThingMerged event
*/
static String forThingMergedEvent(final ThingId thingId, final JsonPointer path) {
return RESOURCE_PATH_MAPPER.map(path, thingId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public String visitThing(final JsonPointer path, @Nullable final ThingId thingId

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

@Override
Expand Down Expand Up @@ -105,7 +105,7 @@ public String visitFeatureProperty(final JsonPointer path, @Nullable final Thing

@Override
public String visitFeatureDesiredProperties(final JsonPointer path, @Nullable final ThingId thingId) {
return BusAddressFactory.forFeatureProperties(checkNotNull(thingId), extractFeatureId(path));
return BusAddressFactory.forFeatureDesiredProperties(checkNotNull(thingId), extractFeatureId(path));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) 2021 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.internal;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.ditto.client.TestConstants.Feature.FLUX_CAPACITOR_ID;
import static org.eclipse.ditto.client.TestConstants.Thing.THING_ID;

import org.eclipse.ditto.json.JsonPointer;
import org.junit.Test;

/**
* Tests {@link org.eclipse.ditto.client.internal.BusAddressFactory}.
*/
public class BusAddressFactoryTest {

private static final String THING_PATTERN = "/things/" + THING_ID;

@Test
public void forThing() {
assertThat(BusAddressFactory.forThing(THING_ID)).isEqualTo(THING_PATTERN);
}

@Test
public void forAcl() {
assertThat(BusAddressFactory.forAcl(THING_ID)).isEqualTo(THING_PATTERN + "/acl");
}

@Test
public void forAclEntry() {
assertThat(BusAddressFactory.forAclEntry(THING_ID, "subjectId"))
.isEqualTo(THING_PATTERN + "/acl/subjectId");
}

@Test
public void forThingDefinition() {
assertThat(BusAddressFactory.forThingDefinition(THING_ID))
.isEqualTo(THING_PATTERN + "/definition");
}

@Test
public void forPolicyId() {
assertThat(BusAddressFactory.forPolicyId(THING_ID))
.isEqualTo(THING_PATTERN + "/policyId");
}

@Test
public void forAttributes() {
assertThat(BusAddressFactory.forAttributes(THING_ID))
.isEqualTo(THING_PATTERN + "/attributes");
}

@Test
public void forAttribute() {
assertThat(BusAddressFactory.forAttribute(THING_ID, JsonPointer.of("location/latitude")))
.isEqualTo(THING_PATTERN + "/attributes/location/latitude");
}

@Test
public void forFeature() {
assertThat(BusAddressFactory.forFeature(THING_ID, FLUX_CAPACITOR_ID))
.isEqualTo(THING_PATTERN + "/features/" + FLUX_CAPACITOR_ID);
}

@Test
public void forFeatures() {
assertThat(BusAddressFactory.forFeatures(THING_ID)).isEqualTo(THING_PATTERN + "/features");
}

@Test
public void forFeatureDefinition() {
assertThat(BusAddressFactory.forFeatureDefinition(THING_ID, FLUX_CAPACITOR_ID))
.isEqualTo(THING_PATTERN + "/features/" + FLUX_CAPACITOR_ID + "/definition");
}

@Test
public void forFeatureProperties() {
assertThat(BusAddressFactory.forFeatureProperties(THING_ID, FLUX_CAPACITOR_ID))
.isEqualTo(THING_PATTERN + "/features/" + FLUX_CAPACITOR_ID + "/properties");
}

@Test
public void forFeatureProperty() {
assertThat(BusAddressFactory.forFeatureProperty(THING_ID, FLUX_CAPACITOR_ID,
JsonPointer.of("status/temperature")))
.isEqualTo(THING_PATTERN + "/features/" + FLUX_CAPACITOR_ID + "/properties/status/temperature");
}

@Test
public void forFeatureDesiredProperties() {
assertThat(BusAddressFactory.forFeatureDesiredProperties(THING_ID, FLUX_CAPACITOR_ID))
.isEqualTo(THING_PATTERN + "/features/" + FLUX_CAPACITOR_ID + "/desiredProperties");
}

@Test
public void forFeatureDesiredProperty() {
assertThat(BusAddressFactory.forFeatureDesiredProperty(THING_ID, FLUX_CAPACITOR_ID,
JsonPointer.of("status/temperature")))
.isEqualTo(THING_PATTERN + "/features/" + FLUX_CAPACITOR_ID + "/desiredProperties/status/temperature");
}

@Test
public void forThingMergedEvent() {
assertThat(BusAddressFactory.forThingMergedEvent(THING_ID, JsonPointer.of("attributes/location/latitude")))
.isEqualTo(THING_PATTERN + "/attributes/location/latitude");
}
}
Loading

0 comments on commit 1cae413

Please sign in to comment.