Java Code Examples for javax.json.JsonObject#getJsonString()

The following examples show how to use javax.json.JsonObject#getJsonString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestSiteToSiteStatusReportingTask.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializedForm() throws IOException, InitializationException {
    final ProcessGroupStatus pgStatus = generateProcessGroupStatus("root", "Awesome", 1, 0);

    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    properties.put(SiteToSiteUtils.BATCH_SIZE, "4");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_NAME_FILTER_REGEX, "Awesome.*");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_TYPE_FILTER_REGEX, ".*");

    MockSiteToSiteStatusReportingTask task = initTask(properties, pgStatus);
    task.onTrigger(context);

    assertEquals(16, task.dataSent.size());
    final String msg = new String(task.dataSent.get(0), StandardCharsets.UTF_8);
    JsonReader jsonReader = Json.createReader(new ByteArrayInputStream(msg.getBytes()));
    JsonObject firstElement = jsonReader.readArray().getJsonObject(0);
    JsonString componentId = firstElement.getJsonString("componentId");
    assertEquals(pgStatus.getId(), componentId.getString());
    JsonNumber terminatedThreads = firstElement.getJsonNumber("terminatedThreadCount");
    assertEquals(1, terminatedThreads.longValue());
    JsonString versionedFlowState = firstElement.getJsonString("versionedFlowState");
    assertEquals("UP_TO_DATE", versionedFlowState.getString());
}
 
Example 2
Source File: TestSiteToSiteStatusReportingTask.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testConnectionStatus() throws IOException, InitializationException {
    final ProcessGroupStatus pgStatus = generateProcessGroupStatus("root", "Awesome", 1, 0);

    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    properties.put(SiteToSiteUtils.BATCH_SIZE, "4");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_NAME_FILTER_REGEX, "Awesome.*");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_TYPE_FILTER_REGEX, "(Connection)");

    MockSiteToSiteStatusReportingTask task = initTask(properties, pgStatus);
    task.onTrigger(context);

    final String msg = new String(task.dataSent.get(0), StandardCharsets.UTF_8);
    JsonReader jsonReader = Json.createReader(new ByteArrayInputStream(msg.getBytes()));
    JsonObject object = jsonReader.readArray().getJsonObject(0);
    JsonString backpressure = object.getJsonString("isBackPressureEnabled");
    JsonString source = object.getJsonString("sourceName");
    assertEquals("true", backpressure.getString());
    assertEquals("source", source.getString());
    JsonString dataSizeThreshold = object.getJsonString("backPressureDataSizeThreshold");
    JsonNumber bytesThreshold = object.getJsonNumber("backPressureBytesThreshold");
    assertEquals("1 KB", dataSizeThreshold.getString());
    assertEquals(1024, bytesThreshold.intValue());
    assertNull(object.get("destinationName"));
}
 
Example 3
Source File: TestSiteToSiteStatusReportingTask.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testPortStatus() throws IOException, InitializationException {
    ProcessGroupStatus pgStatus = generateProcessGroupStatus("root", "Awesome", 1, 0);

    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    properties.put(SiteToSiteUtils.BATCH_SIZE, "4");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_NAME_FILTER_REGEX, "Awesome.*");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_TYPE_FILTER_REGEX, "(InputPort)");
    properties.put(SiteToSiteStatusReportingTask.ALLOW_NULL_VALUES,"false");

    MockSiteToSiteStatusReportingTask task = initTask(properties, pgStatus);
    task.onTrigger(context);

    final String msg = new String(task.dataSent.get(0), StandardCharsets.UTF_8);
    JsonReader jsonReader = Json.createReader(new ByteArrayInputStream(msg.getBytes()));
    JsonObject object = jsonReader.readArray().getJsonObject(0);
    JsonString runStatus = object.getJsonString("runStatus");
    assertEquals(RunStatus.Stopped.name(), runStatus.getString());
    boolean isTransmitting = object.getBoolean("transmitting");
    assertFalse(isTransmitting);
    JsonNumber inputBytes = object.getJsonNumber("inputBytes");
    assertEquals(5, inputBytes.intValue());
    assertNull(object.get("activeThreadCount"));
}
 
Example 4
Source File: TestSiteToSiteStatusReportingTask.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoteProcessGroupStatus() throws IOException, InitializationException {
    final ProcessGroupStatus pgStatus = generateProcessGroupStatus("root", "Awesome", 1, 0);

    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    properties.put(SiteToSiteUtils.BATCH_SIZE, "4");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_NAME_FILTER_REGEX, "Awesome.*");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_TYPE_FILTER_REGEX, "(RemoteProcessGroup)");

    MockSiteToSiteStatusReportingTask task = initTask(properties, pgStatus);
    task.onTrigger(context);

    assertEquals(3, task.dataSent.size());
    final String msg = new String(task.dataSent.get(0), StandardCharsets.UTF_8);
    JsonReader jsonReader = Json.createReader(new ByteArrayInputStream(msg.getBytes()));
    JsonObject firstElement = jsonReader.readArray().getJsonObject(0);
    JsonNumber activeThreadCount = firstElement.getJsonNumber("activeThreadCount");
    assertEquals(1L, activeThreadCount.longValue());
    JsonString transmissionStatus = firstElement.getJsonString("transmissionStatus");
    assertEquals("Transmitting", transmissionStatus.getString());
}
 
Example 5
Source File: JsonSupport.java    From FHIR with Apache License 2.0 5 votes vote down vote up
public static Class<?> getResourceType(JsonObject jsonObject) {
    JsonString resourceTypeString = jsonObject.getJsonString("resourceType");
    if (resourceTypeString == null) {
        throw new IllegalArgumentException("Missing required element: 'resourceType'");
    }
    String resourceTypeName = resourceTypeString.getString();
    Class<?> resourceType = ModelSupport.getResourceType(resourceTypeName);
    if (resourceType == null) {
        throw new IllegalArgumentException("Invalid resource type: '" + resourceTypeName + "'");
    }
    return resourceType;
}
 
Example 6
Source File: TestContainerChanges.java    From rapid with MIT License 5 votes vote down vote up
@Test
public void shouldInspectContainer() {
    final Response listContainers = target("containers").path("json").request(MediaType.APPLICATION_JSON).get();
    final JsonArray containers = listContainers.readEntity(JsonArray.class);
    final JsonObject runningCurrentContainer = (JsonObject) containers.get(0);
    final JsonString expectedId = runningCurrentContainer.getJsonString("Id");

    Response changes = getResponse(target("containers").path(expectedId.getString()).path("changes"));
    assertEquals(200, changes.getStatus());
}
 
Example 7
Source File: TestContainerTop.java    From rapid with MIT License 5 votes vote down vote up
@Test
public void inspectContainer() {
    final Response listContainers = getResponse(target("containers").path("json"));
    final JsonArray containers = listContainers.readEntity(JsonArray.class);
    final JsonObject container = (JsonObject) containers.get(0);
    final JsonString expectedId = container.getJsonString("Id");

    Response top = getResponse(target("containers").path(expectedId.getString()).path("top"));
    assertEquals(200, top.getStatus());

    final JsonObject actualContainer = top.readEntity(JsonObject.class);
    int expectedProcessSize = 1;
    assertEquals(expectedProcessSize, actualContainer.getJsonArray("Processes").size());
}
 
Example 8
Source File: TestContainerLog.java    From rapid with MIT License 5 votes vote down vote up
@Test
public void inspectContainer() {
    final Response listContainers = target("containers").path("json").request(MediaType.APPLICATION_JSON).get();
    final JsonArray containers = listContainers.readEntity(JsonArray.class);
    final JsonObject container = (JsonObject) containers.get(0);
    final JsonString expectedId = container.getJsonString("Id");

    Response log = getResponse(target("containers").path(expectedId.getString()).path("logs").queryParam("stdout", true));
    assertEquals(OK.getStatusCode(), log.getStatus());
}
 
Example 9
Source File: TestContainerInspect.java    From rapid with MIT License 5 votes vote down vote up
@Test
public void inspectContainer() {
    final Response listContainers = target("containers").path("json").request(MediaType.APPLICATION_JSON).get();
    final JsonArray containers = listContainers.readEntity(JsonArray.class);
    final JsonObject container = (JsonObject) containers.get(0);
    final JsonString expectedId = container.getJsonString("Id");

    Response inspect = getResponse(target("containers").path(expectedId.getString()).path("json"));
    assertEquals(200, inspect.getStatus());

    final JsonObject actualContainer  = inspect.readEntity(JsonObject.class);
    assertEquals(expectedId.getString(), actualContainer.getJsonString("Id").getString());
}
 
Example 10
Source File: CloudantCredentials.java    From sample.microservices.12factorapp with Apache License 2.0 5 votes vote down vote up
private void parseVcapServices(String vcapServicesEnv) {
        if (vcapServicesEnv == null) {
            return;
        }
	JsonObject vcapServices = Json.createReader(new StringReader(vcapServicesEnv)).readObject();
	JsonArray cloudantObjectArray = vcapServices.getJsonArray("cloudantNoSQLDB");
	JsonObject cloudantObject = cloudantObjectArray.getJsonObject(0);
	JsonObject cloudantCredentials = cloudantObject.getJsonObject("credentials");
	JsonString cloudantUsername = cloudantCredentials.getJsonString("username");
	username = cloudantUsername.getString();
	JsonString cloudantPassword = cloudantCredentials.getJsonString("password");
	password = cloudantPassword.getString();
	JsonString cloudantUrl = cloudantCredentials.getJsonString("url");
	url = cloudantUrl.getString();
}
 
Example 11
Source File: TestSiteToSiteStatusReportingTask.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessorStatus() throws IOException, InitializationException {
    final ProcessGroupStatus pgStatus = generateProcessGroupStatus("root", "Awesome", 1, 0);

    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    properties.put(SiteToSiteUtils.BATCH_SIZE, "4");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_NAME_FILTER_REGEX, "Awesome.*");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_TYPE_FILTER_REGEX, "(Processor)");

    MockSiteToSiteStatusReportingTask task = initTask(properties, pgStatus);
    task.onTrigger(context);

    final String msg = new String(task.dataSent.get(0), StandardCharsets.UTF_8);
    JsonReader jsonReader = Json.createReader(new ByteArrayInputStream(msg.getBytes()));
    JsonObject object = jsonReader.readArray().getJsonObject(0);
    JsonString parentName = object.getJsonString("parentName");
    assertTrue(parentName.getString().startsWith("Awesome.1-"));
    JsonString parentPath = object.getJsonString("parentPath");
    assertTrue(parentPath.getString().startsWith("NiFi Flow / Awesome.1"));
    JsonString runStatus = object.getJsonString("runStatus");
    assertEquals(RunStatus.Running.name(), runStatus.getString());
    JsonNumber inputBytes = object.getJsonNumber("inputBytes");
    assertEquals(9, inputBytes.intValue());
    JsonObject counterMap = object.getJsonObject("counters");
    assertNotNull(counterMap);
    assertEquals(10, counterMap.getInt("counter1"));
    assertEquals(5, counterMap.getInt("counter2"));
    assertNull(object.get("processorType"));
}
 
Example 12
Source File: Message.java    From diirt with MIT License 5 votes vote down vote up
/**
 * Un-marshals a string, or throws an exception if it's not able to.
 *
 * @param jObject the JSON object
 * @param name the attribute name where the string is stored
 * @return the message string
 * @throws MessageDecodeException if the field is missing or of the wrong type
 */
static String stringMandatory(JsonObject jObject, String name) throws MessageDecodeException {
    try {
        JsonString jsonString = jObject.getJsonString(name);
        if (jsonString == null) {
            throw MessageDecodeException.missingMandatoryAttribute(jObject, name);
        } else {
            return jsonString.getString();
        }
    } catch (ClassCastException  e) {
        throw MessageDecodeException.wrongAttributeType(jObject, name, "string");
    }
}
 
Example 13
Source File: Message.java    From diirt with MIT License 5 votes vote down vote up
/**
 * Un-marshals a string, or returns the default value if it's not able to.
 *
 * @param jObject the JSON object
 * @param name the attribute name where the string is stored
 * @param defaultValue the value to use if no attribute is found
 * @return the message string
 * @throws MessageDecodeException if the field is of the wrong type
 */
static String stringOptional(JsonObject jObject, String name, String defaultValue) throws MessageDecodeException {
    try {
        JsonString jsonString = jObject.getJsonString(name);
        if (jsonString == null) {
            return defaultValue;
        } else {
            return jsonString.getString();
        }
    } catch (ClassCastException  e) {
        throw MessageDecodeException.wrongAttributeType(jObject, name, "string");
    }
}
 
Example 14
Source File: applianceCommon.java    From fido2 with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Given a JSON string and a search-key, this method looks up the 'key' in
 * the JSON and if found, returns the associated value. Returns NULL in all
 * error conditions.
 *
 * @param jsonstr String containing JSON
 * @param key String containing the search-key
 * @param datatype String containing the data-type of the value-object
 * @return Object containing the value for the specified key if valid; null
 * in all error cases.
 */
public static Object getJsonValue(String jsonstr, String key, String datatype) {
    if (jsonstr == null || jsonstr.isEmpty()) {
        if (key == null || key.isEmpty()) {
            if (datatype == null || datatype.isEmpty()) {
                return null;
            }
        }
    }

    try (JsonReader jsonreader = Json.createReader(new StringReader(jsonstr))) {
        JsonObject json = jsonreader.readObject();

        if (!json.containsKey(key)) {
            strongkeyLogger.log(applianceConstants.APPLIANCE_LOGGER, Level.FINE, "APPL-ERR-1003", "'" + key + "' does not exist in the json");
            return null;
        }

        switch (datatype) {
            case "Boolean":
                return json.getBoolean(key);
            case "Int":
                return json.getInt(key);
            case "Long":
                return json.getJsonNumber(key).longValueExact();
            case "JsonArray":
                return json.getJsonArray(key);
            case "JsonNumber":
                return json.getJsonNumber(key);
            case "JsonObject":
                return json.getJsonObject(key);
            case "JsonString":
                return json.getJsonString(key);
            case "String":
                return json.getString(key);
            default:
                return null;
        }
    } catch (Exception ex) {
        strongkeyLogger.log(applianceConstants.APPLIANCE_LOGGER, Level.WARNING, "APPL-ERR-1000", ex.getLocalizedMessage());
        return null;
    }
}