Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed computation of topic segments for MQTT DevFactory context #419

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
**********************************************************************/
package org.eclipse.sensinact.gateway.southbound.mqtt.factory;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.function.Predicate;

import org.eclipse.sensinact.gateway.southbound.device.factory.DeviceFactoryException;
import org.eclipse.sensinact.gateway.southbound.device.factory.IDeviceMappingHandler;
Expand Down Expand Up @@ -142,26 +144,32 @@ public void onMqttMessage(String handlerId, String topic, IMqttMessage message)
}
}

private void fillTopicSegments(String topic, Map<String, String> context) {
void fillTopicSegments(String topic, Map<String, String> context) {
context.put("topic", topic);

int i = 0;
String segment;
int idx = 0;

do {
int slash = topic.indexOf('/', idx);
if (slash > 0) {
segment = topic.substring(idx, slash);
idx = slash + 1;
} else {
segment = topic.substring(idx);
idx = -1;
}
context.put("topic-".concat(Integer.toString(i)), segment);
i++;
} while (idx >= 0);

context.put("topic-last", segment);
boolean startingSlash = topic.indexOf('/') == 0;
boolean endingSlash = !topic.isEmpty() && topic.lastIndexOf('/') == topic.length() - 1;
String[] parts = Arrays.stream(topic.split("/")).filter(Predicate.not(String::isEmpty)).toArray(String[]::new);

int segmentIdx = 0;
if (startingSlash || parts.length == 0) {
context.put("topic-0", "");
segmentIdx++;
}

for (String part : parts) {
context.put("topic-" + segmentIdx, part);
segmentIdx++;
}

if (endingSlash) {
context.put("topic-" + segmentIdx, "");
}

if (endingSlash || parts.length == 0) {
context.put("topic-last", "");
} else {
context.put("topic-last", parts[parts.length - 1]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*********************************************************************
* Copyright (c) 2024 Kentyou.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thomas Calmant (Kentyou) - initial implementation
**********************************************************************/
package org.eclipse.sensinact.gateway.southbound.mqtt.factory;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;

public class ContextTest {

@Test
void testTopicContext() throws Exception {
final MqttDeviceFactoryHandler handler = new MqttDeviceFactoryHandler();
final Map<String, String> segments = new HashMap<>();

// Empty topic (shouldn't exist according to the specification)
handler.fillTopicSegments("", segments);
assertEquals("", segments.get("topic"));
assertEquals("", segments.get("topic-last"));
assertEquals("", segments.get("topic-0"));
assertFalse(segments.containsKey("topic-1"));

// Space topic
segments.clear();
handler.fillTopicSegments(" ", segments);
assertEquals(" ", segments.get("topic"));
assertEquals(" ", segments.get("topic-last"));
assertEquals(" ", segments.get("topic-0"));
assertFalse(segments.containsKey("topic-1"));

// Slash topic
segments.clear();
handler.fillTopicSegments("/", segments);
assertEquals("/", segments.get("topic"));
assertEquals("", segments.get("topic-last"));
assertEquals("", segments.get("topic-0"));
assertEquals("", segments.get("topic-1"));
assertFalse(segments.containsKey("topic-2"));

// Long topic
segments.clear();
handler.fillTopicSegments("foo/bar/foobar", segments);
assertEquals("foo/bar/foobar", segments.get("topic"));
assertEquals("foo", segments.get("topic-0"));
assertEquals("bar", segments.get("topic-1"));
assertEquals("foobar", segments.get("topic-2"));
assertEquals("foobar", segments.get("topic-last"));
assertFalse(segments.containsKey("topic-3"));

// Starting slash shall not be ignored, as explained in the MQTT specification
segments.clear();
handler.fillTopicSegments("/foo/bar/foobar", segments);
assertEquals("/foo/bar/foobar", segments.get("topic"));
assertEquals("", segments.get("topic-0"));
assertEquals("foo", segments.get("topic-1"));
assertEquals("bar", segments.get("topic-2"));
assertEquals("foobar", segments.get("topic-3"));
assertEquals("foobar", segments.get("topic-last"));
assertFalse(segments.containsKey("topic-4"));

// Trailing slash shall not be ignored, as explained in the MQTT specification
segments.clear();
handler.fillTopicSegments("foo/bar/foobar/", segments);
assertEquals("foo/bar/foobar/", segments.get("topic"));
assertEquals("foo", segments.get("topic-0"));
assertEquals("bar", segments.get("topic-1"));
assertEquals("foobar", segments.get("topic-2"));
assertEquals("", segments.get("topic-3"));
assertEquals("", segments.get("topic-last"));
assertFalse(segments.containsKey("topic-4"));

// Double-slashes shall be considered as 1, as explained in the MQTT
// specification
segments.clear();
handler.fillTopicSegments("foo//bar", segments);
assertEquals("foo//bar", segments.get("topic"));
assertEquals("foo", segments.get("topic-0"));
assertEquals("bar", segments.get("topic-1"));
assertEquals("bar", segments.get("topic-last"));
assertFalse(segments.containsKey("topic-3"));
}
}
Loading