use of javax.json.JsonString in project wring by yegor256.
the class BoostEvents method pattern.
/**
* Make regex from a JSON config.
* @param json JSON config
* @return Pattern
* @throws Agent.UserException If fails
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static Pattern pattern(final JsonObject json) throws Agent.UserException {
final Collection<Pattern> ptns = new LinkedList<>();
final JsonValue value = json.get("boost");
if (value == null) {
ptns.add(Pattern.compile("never^"));
} else {
if (!(value instanceof JsonArray)) {
throw new Agent.UserException("Element 'boost' must be an array");
}
final JsonArray ignore = value.asJsonArray();
ptns.addAll(ignore.getValuesAs(JsonString.class).stream().map(JsonString::getString).map(Pattern::compile).collect(Collectors.toList()));
}
return Pattern.compile(String.format("(%s)", ptns.stream().map(Pattern::toString).collect(Collectors.joining(")|("))));
}
use of javax.json.JsonString in project azure-iot-sdk-java by Azure.
the class ToolsTest method getValueFromJsonObject_good_case.
// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_011: [The function shall get the JsonValue of the key]
// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_012: [The function shall get the JsonString from the JsonValue if the JsonValue is not null]
// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_013: [The function shall return the string value from the JsonString calling the getValueFromJsonString function]
@Test
public void getValueFromJsonObject_good_case() {
// Arrange
String jsonString = "{\"deviceId\":\"xxx-device\",\"generationId\":\"111111111111111111\",\"etag\":\"MA==\",\"connectionState\":\"Disconnected\",\"status\":\"Disabled\",\"statusReason\":null,\"connectionStateUpdatedTime\":\"0001-01-01T00:00:00\",\"statusUpdatedTime\":\"0001-01-01T00:00:00\",\"lastActivityTime\":\"0001-01-01T00:00:00\",\"cloudToDeviceMessageCount\":0,\"authentication\":{\"symmetricKey\":{\"primaryKey\":\"AAABBBCCC111222333444000\",\"secondaryKey\":\"111222333444555AAABBBCCC\"}}}";
StringReader stringReader = new StringReader(jsonString);
JsonReader jsonReader = Json.createReader(stringReader);
JsonObject jsonObject = jsonReader.readObject();
String key = "generationId";
String expResult = "111111111111111111";
// Act
String result = Tools.getValueFromJsonObject(jsonObject, key);
// Assert
assertEquals(expResult, result);
}
use of javax.json.JsonString in project azure-iot-sdk-java by Azure.
the class ToolsTest method getValueFromJsonObject_input_key_null.
// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_010: [The function shall return empty string if any of the input is null]
@Test
public void getValueFromJsonObject_input_key_null() {
// Arrange
String jsonString = "{\"deviceId\":\"xxx-device\",\"generationId\":\"111111111111111111\",\"etag\":\"MA==\",\"connectionState\":\"Disconnected\",\"status\":\"Disabled\",\"statusReason\":null,\"connectionStateUpdatedTime\":\"0001-01-01T00:00:00\",\"statusUpdatedTime\":\"0001-01-01T00:00:00\",\"lastActivityTime\":\"0001-01-01T00:00:00\",\"cloudToDeviceMessageCount\":0,\"authentication\":{\"symmetricKey\":{\"primaryKey\":\"AAABBBCCC111222333444000\",\"secondaryKey\":\"111222333444555AAABBBCCC\"}}}";
StringReader stringReader = new StringReader(jsonString);
JsonReader jsonReader = Json.createReader(stringReader);
JsonObject jsonObject = jsonReader.readObject();
String key = null;
String expResult = "";
// Act
String result = Tools.getValueFromJsonObject(jsonObject, key);
// Assert
assertEquals(expResult, result);
}
use of javax.json.JsonString in project azure-iot-sdk-java by Azure.
the class ToolsTest method getValueFromJsonString_good_case.
// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_016: [The function shall get the string value from JsonString]
// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_017: [The function shall trim the leading and trailing parenthesis from the string and return with it]
@Test
public void getValueFromJsonString_good_case() {
// Arrange
String jsonString = "{\"deviceId\":\"xxx-device\",\"generationId\":\"111111111111111111\",\"etag\":\"MA==\",\"connectionState\":\"Disconnected\",\"status\":\"Disabled\",\"statusReason\":null,\"connectionStateUpdatedTime\":\"0001-01-01T00:00:00\",\"statusUpdatedTime\":\"0001-01-01T00:00:00\",\"lastActivityTime\":\"0001-01-01T00:00:00\",\"cloudToDeviceMessageCount\":0,\"authentication\":{\"symmetricKey\":{\"primaryKey\":\"AAABBBCCC111222333444000\",\"secondaryKey\":\"111222333444555AAABBBCCC\"}}}";
StringReader stringReader = new StringReader(jsonString);
JsonReader jsonReader = Json.createReader(stringReader);
JsonObject jsonObject = jsonReader.readObject();
String key = "generationId";
JsonString jsonStringObject = jsonObject.getJsonString(key);
String expResult = "111111111111111111";
// Act
String result = Tools.getValueFromJsonString(jsonStringObject);
// Assert
assertEquals(expResult, result);
}
use of javax.json.JsonString in project azure-iot-sdk-java by Azure.
the class ToolsTest method getValueFromJsonObject_value_null.
// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_014: [The function shall return empty string if the JsonValue is null]
@Test
public void getValueFromJsonObject_value_null() {
// Arrange
String jsonString = "{\"deviceId\":\"xxx-device\",\"generationId\":null,\"etag\":\"MA==\",\"connectionState\":\"Disconnected\",\"status\":\"Disabled\",\"statusReason\":null,\"connectionStateUpdatedTime\":\"0001-01-01T00:00:00\",\"statusUpdatedTime\":\"0001-01-01T00:00:00\",\"lastActivityTime\":\"0001-01-01T00:00:00\",\"cloudToDeviceMessageCount\":0,\"authentication\":{\"symmetricKey\":{\"primaryKey\":\"AAABBBCCC111222333444000\",\"secondaryKey\":\"111222333444555AAABBBCCC\"}}}";
StringReader stringReader = new StringReader(jsonString);
JsonReader jsonReader = Json.createReader(stringReader);
JsonObject jsonObject = jsonReader.readObject();
String key = "generationId";
String expResult = "";
// Act
String result = Tools.getValueFromJsonObject(jsonObject, key);
// Assert
assertEquals(expResult, result);
}
Aggregations