diff --git a/examples/src/main/java/io/zenoh/ZGet.java b/examples/src/main/java/io/zenoh/ZGet.java index c840be9..8bf6b54 100644 --- a/examples/src/main/java/io/zenoh/ZGet.java +++ b/examples/src/main/java/io/zenoh/ZGet.java @@ -46,12 +46,20 @@ public Integer call() throws ZError, InterruptedException { Config config = loadConfig(emptyArgs, configFile, connect, listen, noMulticastScouting, mode); Selector selector = Selector.tryFrom(this.selectorOpt); + ZBytes payload = Optional.ofNullable(this.payload) + .map(ZBytes::from) + .orElse(null); + ZBytes attachment = Optional.ofNullable(this.attachment) + .map(ZBytes::from) + .orElse(null); + // Load GET options GetOptions options = new GetOptions(); - options.setPayload(ZBytes.from(this.payload)); + + options.setPayload(payload); options.setTarget(QueryTarget.valueOf(this.target)); options.setTimeout(Duration.ofMillis(this.timeout)); - options.setAttachment(ZBytes.from(this.attachment)); + options.setAttachment(attachment); // A GET query can be performed in different ways, by default (using a blocking queue), using a callback @@ -146,7 +154,8 @@ private void handleReply(Reply reply) { @CommandLine.Option( names = {"-t", "--target"}, description = "The target queryables of the query. Default: BEST_MATCHING. " + - "[possible values: BEST_MATCHING, ALL, ALL_COMPLETE]" + "[possible values: BEST_MATCHING, ALL, ALL_COMPLETE]", + defaultValue = "BEST_MATCHING" ) private String target; diff --git a/zenoh-java/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt b/zenoh-java/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt index 6349356..594e9f2 100644 --- a/zenoh-java/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt +++ b/zenoh-java/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt @@ -276,7 +276,7 @@ internal class JNISession { getViaJNI( selector.keyExpr.jniKeyExpr?.ptr ?: 0, selector.keyExpr.keyExpr, - selector.parameters.toString(), + selector.parameters?.toString(), sessionPtr.get(), getCallback, fun() {}, @@ -338,7 +338,7 @@ internal class JNISession { getViaJNI( selector.keyExpr.jniKeyExpr?.ptr ?: 0, selector.keyExpr.keyExpr, - selector.parameters.toString(), + selector.parameters?.toString(), sessionPtr.get(), getCallback, handler::onClose, diff --git a/zenoh-java/src/jvmTest/java/io/zenoh/SelectorTest.java b/zenoh-java/src/jvmTest/java/io/zenoh/SelectorTest.java index 9252dd3..134a487 100644 --- a/zenoh-java/src/jvmTest/java/io/zenoh/SelectorTest.java +++ b/zenoh-java/src/jvmTest/java/io/zenoh/SelectorTest.java @@ -15,6 +15,7 @@ package io.zenoh; import io.zenoh.exceptions.ZError; +import io.zenoh.keyexpr.KeyExpr; import io.zenoh.query.Parameters; import io.zenoh.query.Selector; import org.junit.Test; @@ -47,4 +48,35 @@ public void parametersTest() { var parameters = Parameters.from("a=1;b=2;c=1|2|3"); assertEquals(List.of("1", "2", "3"), parameters.values("c")); } + + /** + * Check the queryable properly receives the query's selector with and without parameters. + */ + @Test + public void selectorQueryTest() throws ZError, InterruptedException { + var session = Zenoh.open(Config.loadDefault()); + var queryableKeyExpr = KeyExpr.tryFrom("a/b/**"); + + Selector[] receivedQuerySelector = new Selector[1]; + var queryable = session.declareQueryable(queryableKeyExpr, query -> { + receivedQuerySelector[0] = query.getSelector(); + query.close(); + } + ); + + var querySelector = Selector.tryFrom("a/b/c"); + session.get(querySelector, reply -> { + }); + Thread.sleep(1000); + assertEquals(querySelector, receivedQuerySelector[0]); + + var querySelector2 = Selector.tryFrom("a/b/c?key=value"); + session.get(querySelector2, reply -> { + }); + Thread.sleep(1000); + assertEquals(querySelector2, receivedQuerySelector[0]); + + queryable.close(); + session.close(); + } }