You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@edgent.apache.org by dl...@apache.org on 2017/04/19 20:18:18 UTC
[1/2] incubator-edgent git commit: [Edgent-407] JsonFunctions
additions
Repository: incubator-edgent
Updated Branches:
refs/heads/master f43ec0846 -> 6fc7b66f6
[Edgent-407] JsonFunctions additions
Project: http://git-wip-us.apache.org/repos/asf/incubator-edgent/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-edgent/commit/3812c985
Tree: http://git-wip-us.apache.org/repos/asf/incubator-edgent/tree/3812c985
Diff: http://git-wip-us.apache.org/repos/asf/incubator-edgent/diff/3812c985
Branch: refs/heads/master
Commit: 3812c985a741b25465fde59c06212ca518051b04
Parents: f43ec08
Author: Dale LaBossiere <dl...@us.ibm.com>
Authored: Thu Apr 13 10:44:15 2017 -0400
Committer: Dale LaBossiere <dl...@us.ibm.com>
Committed: Thu Apr 13 10:44:15 2017 -0400
----------------------------------------------------------------------
.../edgent/topology/json/JsonFunctions.java | 112 +++++++++++++++++--
.../edgent/test/topology/JsonFunctionsTest.java | 46 ++++++++
2 files changed, 149 insertions(+), 9 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-edgent/blob/3812c985/api/topology/src/main/java/org/apache/edgent/topology/json/JsonFunctions.java
----------------------------------------------------------------------
diff --git a/api/topology/src/main/java/org/apache/edgent/topology/json/JsonFunctions.java b/api/topology/src/main/java/org/apache/edgent/topology/json/JsonFunctions.java
index d18a234..37a9eea 100644
--- a/api/topology/src/main/java/org/apache/edgent/topology/json/JsonFunctions.java
+++ b/api/topology/src/main/java/org/apache/edgent/topology/json/JsonFunctions.java
@@ -22,6 +22,7 @@ import java.nio.charset.StandardCharsets;
import org.apache.edgent.function.Function;
+import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
@@ -30,22 +31,28 @@ import com.google.gson.JsonParser;
*/
public class JsonFunctions {
+ private static final JsonElement ZERO_ELEMENT = new JsonParser().parse("0");
+ private static final Function<JsonObject,JsonElement> ZERO = jo -> ZERO_ELEMENT;
+
/**
* Get the JSON for a JsonObject.
*
- * TODO consider adding an override where the caller can specify
- * the number of significant digits to include in the string representation
- * of floating point types.
+ * <p>Returns a Function whose {@code apply(JsonObject jo)} returns the JSON
+ * for the {@code jo}.
*
- * @return the JSON
+ * @return the Function
*/
public static Function<JsonObject,String> asString() {
return jo -> jo.toString();
}
/**
- * Create a new JsonObject from JSON
- * @return the JsonObject
+ * Create a new JsonObject from JSON.
+ *
+ * <p>Returns a Function whose {@code apply(String json)} creates a JsonObject
+ * from the {@code json}.
+ *
+ * @return the Function
*/
public static Function<String,JsonObject> fromString() {
JsonParser jp = new JsonParser();
@@ -54,19 +61,106 @@ public class JsonFunctions {
/**
* Get the UTF-8 bytes representation of the JSON for a JsonObject.
- * @return the byte[]
+ *
+ * <p>Returns a Function whose {@code apply(JsonObject jo)} returns
+ * the UTF-8 bytes for the JSON of {@code jo}.
+ *
+ * @return the Function
*/
public static Function<JsonObject,byte[]> asBytes() {
return jo -> jo.toString().getBytes(StandardCharsets.UTF_8);
}
/**
- * Create a new JsonObject from the UTF8 bytes representation of JSON
- * @return the JsonObject
+ * Create a new JsonObject from the UTF8 bytes representation of JSON.
+ *
+ * <p>Returns a Function whose {@code apply(byte[] bytes)} returns
+ * a JsonObject from the {@code bytes}.
+ *
+ * @return the Function
*/
public static Function<byte[],JsonObject> fromBytes() {
JsonParser jp = new JsonParser();
return jsonbytes -> jp.parse(new String(jsonbytes, StandardCharsets.UTF_8)).getAsJsonObject();
}
+
+ /**
+ * Returns a constant function that returns a zero (0) JsonElement.
+ *
+ * <p>Useful for an unpartitioned {@code TWindow<JsonObject,JsonElement>}.
+ *
+ * @return Constant function that returns a zero (0) JsonElement.
+ */
+ public static Function<JsonObject,JsonElement> unpartitioned() {
+ return ZERO;
+ }
+
+ /**
+ * Create a JsonObject with a {@code Number} property.
+ *
+ * <p>Returns a Function whose {@code apply(T v)} returns a JsonObject having
+ * a single property named {@code propName} with the value of {@code v}.
+ *
+ * @param propName property name
+ * @return the Function
+ */
+ public static <T extends Number> Function<T,JsonObject> valueOfNumber(String propName) {
+ return v -> {
+ JsonObject jo = new JsonObject();
+ jo.addProperty(propName, v);
+ return jo;
+ };
+ }
+
+ /**
+ * Create a JsonObject with a {@code Boolean} property.
+ *
+ * <p>Returns a Function whose {@code apply(Boolean v)} creates a new JsonObject having
+ * a single property named {@code propName} with the value of {@code v}.
+ *
+ * @param propName property name
+ * @return the Function
+ */
+ public static Function<Boolean,JsonObject> valueOfBoolean(String propName) {
+ return v -> {
+ JsonObject jo = new JsonObject();
+ jo.addProperty(propName, v);
+ return jo;
+ };
+ }
+
+ /**
+ * Create a JsonObject with a {@code String} property.
+ *
+ * <p>Returns a Function whose {@code apply(String v)} creates a new JsonObject having
+ * a single property named {@code propName} with the value of {@code v}.
+ *
+ * @param propName property name
+ * @return the Function
+ */
+ public static Function<String,JsonObject> valueOfString(String propName) {
+ return v -> {
+ JsonObject jo = new JsonObject();
+ jo.addProperty(propName, v);
+ return jo;
+ };
+ }
+
+ /**
+ * Create a JsonObject with a {@code Character} property.
+ *
+ * <p>Returns a Function whose {@code apply(Character v)} creates a new JsonObject having
+ * a single property named {@code propName} with the value of {@code v}.
+ *
+ * @param propName property name
+ * @return the Function
+ */
+ public static Function<Character,JsonObject> valueOfCharacter(String propName) {
+ return v -> {
+ JsonObject jo = new JsonObject();
+ jo.addProperty(propName, v);
+ return jo;
+ };
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-edgent/blob/3812c985/api/topology/src/test/java/org/apache/edgent/test/topology/JsonFunctionsTest.java
----------------------------------------------------------------------
diff --git a/api/topology/src/test/java/org/apache/edgent/test/topology/JsonFunctionsTest.java b/api/topology/src/test/java/org/apache/edgent/test/topology/JsonFunctionsTest.java
index a9ff459..e9a031e 100644
--- a/api/topology/src/test/java/org/apache/edgent/test/topology/JsonFunctionsTest.java
+++ b/api/topology/src/test/java/org/apache/edgent/test/topology/JsonFunctionsTest.java
@@ -25,6 +25,7 @@ import org.apache.edgent.topology.json.JsonFunctions;
import org.junit.Test;
import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
@@ -75,4 +76,49 @@ public class JsonFunctionsTest {
assertEquals(jo2, jo1);
}
+
+ @Test
+ public void testUnpartitioned() {
+ Function<JsonObject,JsonElement> unpartitionedFn = JsonFunctions.unpartitioned();
+ assertEquals(0, unpartitionedFn.apply(new JsonObject()).getAsInt());
+ }
+
+ @Test
+ public void testValueOfNumber() {
+ JsonObject joShort = JsonFunctions.valueOfNumber("propName").apply(Short.MAX_VALUE);
+ assertEquals(Short.MAX_VALUE, joShort.get("propName").getAsShort());
+
+ JsonObject joInt = JsonFunctions.valueOfNumber("propName").apply(Integer.MAX_VALUE);
+ assertEquals(Integer.MAX_VALUE, joInt.get("propName").getAsInt());
+
+ JsonObject joLong = JsonFunctions.valueOfNumber("propName").apply(Long.MAX_VALUE);
+ assertEquals(Long.MAX_VALUE, joLong.get("propName").getAsLong());
+
+ JsonObject joFloat = JsonFunctions.valueOfNumber("propName").apply(Float.MAX_VALUE);
+ assertEquals(Float.MAX_VALUE, joFloat.get("propName").getAsFloat(), 0.0f);
+
+ JsonObject joDouble = JsonFunctions.valueOfNumber("propName").apply(Double.MAX_VALUE);
+ assertEquals(Double.MAX_VALUE, joDouble.get("propName").getAsDouble(), 0.0d);
+ }
+
+ @Test
+ public void testValueOfBoolean() {
+ JsonObject joTrue = JsonFunctions.valueOfBoolean("propName").apply(true);
+ assertEquals(true, joTrue.get("propName").getAsBoolean());
+
+ JsonObject joFalse = JsonFunctions.valueOfBoolean("propName").apply(false);
+ assertEquals(false, joFalse.get("propName").getAsBoolean());
+ }
+
+ @Test
+ public void testValueOfString() {
+ JsonObject jo = JsonFunctions.valueOfString("propName").apply("str1");
+ assertEquals("str1", jo.get("propName").getAsString());
+ }
+
+ @Test
+ public void testValueOfCharacter() {
+ JsonObject jo = JsonFunctions.valueOfCharacter("propName").apply('c');
+ assertEquals('c', jo.get("propName").getAsCharacter());
+ }
}
[2/2] incubator-edgent git commit: improve output of public mqtt test
skipping output due to can't connect.
Posted by dl...@apache.org.
improve output of public mqtt test skipping output due to can't connect.
not sure why the check in setupAuthInfo isn't failing for all the tests
at this time, but after a few tests fail after timing out without
receiving any published content, the rest then fail the pre-check with
Connection refused.
fwiw, things only sort of seem to be working with manual
mosquitto_{pub,sub}. running with -d shows ~25-45sec CONNECT / CONNACK
delay. but once connected the pub/sub seems to working fine.
the tests give up after ~15sec and they don't get past the connect
delay... as can be seen in a failing test's traceback (interrupted after
15 sec under Connector.connectInternal() -> Connector.awaitDone())
Project: http://git-wip-us.apache.org/repos/asf/incubator-edgent/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-edgent/commit/6fc7b66f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-edgent/tree/6fc7b66f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-edgent/diff/6fc7b66f
Branch: refs/heads/master
Commit: 6fc7b66f684e4ec2ea57af08900caad5acbc5209
Parents: 3812c98
Author: Dale LaBossiere <dl...@us.ibm.com>
Authored: Mon Apr 17 12:12:20 2017 -0400
Committer: Dale LaBossiere <dl...@us.ibm.com>
Committed: Mon Apr 17 12:12:20 2017 -0400
----------------------------------------------------------------------
.../org/apache/edgent/test/connectors/mqtt/MqttOpenTest.java | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-edgent/blob/6fc7b66f/connectors/mqtt/src/test/java/org/apache/edgent/test/connectors/mqtt/MqttOpenTest.java
----------------------------------------------------------------------
diff --git a/connectors/mqtt/src/test/java/org/apache/edgent/test/connectors/mqtt/MqttOpenTest.java b/connectors/mqtt/src/test/java/org/apache/edgent/test/connectors/mqtt/MqttOpenTest.java
index fd1d894..a40e333 100644
--- a/connectors/mqtt/src/test/java/org/apache/edgent/test/connectors/mqtt/MqttOpenTest.java
+++ b/connectors/mqtt/src/test/java/org/apache/edgent/test/connectors/mqtt/MqttOpenTest.java
@@ -26,6 +26,8 @@ import java.util.UUID;
import org.junit.Before;
import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.rules.TestName;
/**
* Uses the MQTT test broker at: tcp://test.mosquitto.org:1883
@@ -38,6 +40,8 @@ import org.junit.Ignore;
public class MqttOpenTest extends MqttStreamsTestManual {
private String uniqueid = UUID.randomUUID().toString().replace('-','_');
+ @Rule public TestName name = new TestName();
+
@Before
public void setupAuthInfo() {
this.authInfo.clear(); // mosquitto.org server isn't happy w/username&pw
@@ -47,7 +51,7 @@ public class MqttOpenTest extends MqttStreamsTestManual {
Socket s = new Socket(uri.getHost(), uri.getPort());
s.close();
} catch (Exception e) {
- System.err.println("Unable to connect to MQTT broker "+getServerURI()+" : "+e.getMessage());
+ System.err.println("Skipping "+name.getMethodName()+": Unable to connect to MQTT broker "+getServerURI()+" : "+e.getMessage());
// e.printStackTrace();
assumeTrue(false);
}
@@ -60,7 +64,7 @@ public class MqttOpenTest extends MqttStreamsTestManual {
s.close();
return true;
} catch (Exception e) {
- System.err.println("Unable to connect to MQTT broker "+getSslServerURI()+" : "+e.getMessage());
+ System.err.println(name.getMethodName()+": Unable to connect to MQTT broker "+getSslServerURI()+" : "+e.getMessage());
// e.printStackTrace();
return false;
}