Example usage for com.fasterxml.jackson.databind.node ArrayNode get

List of usage examples for com.fasterxml.jackson.databind.node ArrayNode get

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.node ArrayNode get.

Prototype

public JsonNode get(String paramString) 

Source Link

Usage

From source file:org.wisdom.wamp.WampControllerPubSubTest.java

/**
 * Use the following PUBLISH message format: [TYPE, URI, Event, Exclusions]
 * @throws RegistryException/* w  w w.  j  a  v  a  2 s. c  o m*/
 */
@Test
public void testExclusion() throws RegistryException {
    clear();
    json = new JsonService();
    WampController controller = createWampControllerAndConnectClient(json);

    controller.open("id2");
    clear();

    WampClient client2 = controller.getClientById("id2").getValue();

    // client 1 subscribes to a topic
    clear();
    ArrayNode msg = json.newArray();
    msg.add(MessageType.SUBSCRIBE.code());
    msg.add("http://example.com:9001/wamp/topic");
    controller.onMessage(CLIENT_ID, msg);

    // client 2 subscribes to the same topic
    clear();
    msg = json.newArray();
    msg.add(MessageType.SUBSCRIBE.code());
    msg.add("http://example.com:9001/wamp/topic");
    controller.onMessage("id2", msg);

    // client 2 sends an event on the topic, client 2 is a subscriber of the topic
    clear();
    msg = json.newArray();
    msg.add(MessageType.PUBLISH.code());
    msg.add("http://example.com:9001/wamp/topic");
    msg.add("hello");
    controller.onMessage("id2", msg);

    // We have two messages in the queue
    assertThat(message.size()).isEqualTo(2);
    ArrayNode node = get(0);
    assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code());
    assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic");
    assertThat(node.get(2).asText()).isEqualTo("hello");
    node = get(1);
    assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code());
    assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic");
    assertThat(node.get(2).asText()).isEqualTo("hello");

    clear();
    // Send the same message but with the an exclude list with client2 (the sender)
    msg = json.newArray();
    msg.add(MessageType.PUBLISH.code());
    msg.add("http://example.com:9001/wamp/topic");
    msg.add("hello");
    msg.add(json.newArray().add(client2.session()));
    controller.onMessage("id2", msg);

    // We have only one message in the queue
    assertThat(message.size()).isEqualTo(1);
    node = get(0);
    assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code());
    assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic");
    assertThat(node.get(2).asText()).isEqualTo("hello");

    clear();

    // Send the same message but with an empty exclude list
    msg = json.newArray();
    msg.add(MessageType.PUBLISH.code());
    msg.add("http://example.com:9001/wamp/topic");
    msg.add("hello");
    msg.add(json.newArray());
    controller.onMessage("id2", msg);

    assertThat(message.size()).isEqualTo(2);
    node = get(0);
    assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code());
    assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic");
    assertThat(node.get(2).asText()).isEqualTo("hello");
    node = get(1);
    assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code());
    assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic");
    assertThat(node.get(2).asText()).isEqualTo("hello");
}

From source file:org.wisdom.wamp.WampController.java

@OnMessage(Constants.WAMP_ROUTE)
public void onMessage(@Parameter("client") String id, @Body ArrayNode message) {
    MessageType type = MessageType.getType(message.get(0).asInt());
    Map.Entry<String, WampClient> entry = getClientById(id);

    switch (type) {
    case PREFIX://  www  .  j  a  va  2s.  c om
        handlePrefixMessage(id, entry, message);
        break;
    case CALL:
        handleCallMessage(id, entry, message);
        break;
    case SUBSCRIBE:
        handleSubscription(id, entry, message);
        break;
    case UNSUBSCRIBE:
        handleUnsubscription(id, entry, message);
        break;
    case PUBLISH:
        handlePublication(id, entry, message);
        break;
    default:
        LOGGER.error("Illegal WAMP message type code {}", type.code());
        break;
    }
}

From source file:org.wisdom.wamp.WampControllerPubSubTest.java

/**
 * Use the following PUBLISH message format: [TYPE, URI, Event, Exclusions, Eligible]
 * @throws RegistryException/*  w  w w .  ja  va 2s.  c om*/
 */
@Test
public void testEligible() throws RegistryException {
    clear();
    json = new JsonService();
    WampController controller = createWampControllerAndConnectClient(json);

    controller.open("id2");
    clear();

    WampClient client1 = controller.getClientById(CLIENT_ID).getValue();
    WampClient client2 = controller.getClientById("id2").getValue();

    // client 1 subscribes to a topic
    clear();
    ArrayNode msg = json.newArray();
    msg.add(MessageType.SUBSCRIBE.code());
    msg.add("http://example.com:9001/wamp/topic");
    controller.onMessage(CLIENT_ID, msg);

    // client 2 subscribes to the same topic
    clear();
    msg = json.newArray();
    msg.add(MessageType.SUBSCRIBE.code());
    msg.add("http://example.com:9001/wamp/topic");
    controller.onMessage("id2", msg);

    // client 2 sends an event on the topic, client 2 is a subscriber of the topic
    clear();
    msg = json.newArray();
    msg.add(MessageType.PUBLISH.code());
    msg.add("http://example.com:9001/wamp/topic");
    msg.add("hello");
    controller.onMessage("id2", msg);

    // We have two messages in the queue
    assertThat(message.size()).isEqualTo(2);
    ArrayNode node = get(0);
    assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code());
    assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic");
    assertThat(node.get(2).asText()).isEqualTo("hello");
    node = get(1);
    assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code());
    assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic");
    assertThat(node.get(2).asText()).isEqualTo("hello");

    clear();
    // Send the same message but with the an empty exclude list, but only the client 1 in the eligible list
    msg = json.newArray();
    msg.add(MessageType.PUBLISH.code());
    msg.add("http://example.com:9001/wamp/topic");
    msg.add("hello");
    msg.add(json.newArray());
    msg.add(json.newArray().add(client1.session()));
    controller.onMessage("id2", msg);

    // We have only one message in the queue
    assertThat(message.size()).isEqualTo(1);
    node = get(0);
    assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code());
    assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic");
    assertThat(node.get(2).asText()).isEqualTo("hello");

    clear();

    // Send the same message but with an empty exclude list, and empty eligible list
    msg = json.newArray();
    msg.add(MessageType.PUBLISH.code());
    msg.add("http://example.com:9001/wamp/topic");
    msg.add("hello");
    msg.add(json.newArray());
    msg.add(json.newArray());
    controller.onMessage("id2", msg);

    assertThat(message.size()).isEqualTo(0);

    clear();

    // Send the same message but with an empty exclude list, and eligible list containing the 2 clients
    msg = json.newArray();
    msg.add(MessageType.PUBLISH.code());
    msg.add("http://example.com:9001/wamp/topic");
    msg.add("hello");
    msg.add(json.newArray());
    msg.add(json.newArray().add(client1.session()).add(client2.session()));
    controller.onMessage("id2", msg);

    assertThat(message.size()).isEqualTo(2);
    node = get(0);
    assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code());
    assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic");
    assertThat(node.get(2).asText()).isEqualTo("hello");
    node = get(1);
    assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code());
    assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic");
    assertThat(node.get(2).asText()).isEqualTo("hello");
}

From source file:org.walkmod.conf.providers.yml.RemoveIncludesOrExcludesYMLAction.java

private void removesIncludesOrExcludesList(ObjectNode node) {
    ArrayNode wildcardArray = null;

    ArrayNode result = new ArrayNode(provider.getObjectMapper().getNodeFactory());
    String label = "includes";
    if (isExcludes) {
        label = "excludes";
    }// w  ww  .j a  v a 2 s.c o m
    if (node.has(label)) {
        JsonNode wildcard = node.get(label);
        if (wildcard.isArray()) {
            wildcardArray = (ArrayNode) wildcard;
        }
    }
    if (wildcardArray != null) {
        int limit = wildcardArray.size();
        for (int i = 0; i < limit; i++) {
            String aux = wildcardArray.get(i).asText();
            if (!includes.contains(aux)) {
                result.add(wildcardArray.get(i));
            }
        }
    }
    if (result.size() > 0) {
        node.set(label, result);
    } else {
        node.remove(label);
    }

}

From source file:org.apache.solr.kelvin.testcases.DateRangeCondition.java

public List<ConditionFailureTestEvent> verifyConditions(ITestCase testCase, Properties queryParams,
        Map<String, Object> decodedResponses, Object testSpecificArgument) {
    List<ConditionFailureTestEvent> ret = new ArrayList<ConditionFailureTestEvent>();
    ArrayNode results = new ArrayNode(null);
    if (decodedResponses.containsKey(XmlDoclistExtractorResponseAnalyzer.DOC_LIST)) {
        results = (ArrayNode) decodedResponses.get(XmlDoclistExtractorResponseAnalyzer.DOC_LIST);
    }//from   w  w w. j  av  a 2s.com
    int lengthToCheck = getLengthToCheck(results);
    for (int i = 0; i < lengthToCheck; i++) {
        if (results.size() <= i) {
            ret.add(new MissingResultTestEvent(testCase, queryParams, "result set too short", i));
        } else {
            JsonNode row = results.get(i);
            if (!hasField(row, fieldToCheck)) {
                ret.add(new MissingFieldTestEvent(testCase, queryParams,
                        "missing field from result - " + fieldToCheck, i));
            } else {
                String fieldValue = getField(row, fieldToCheck).asText();
                Date dateReturned = null;
                try {
                    dateReturned = parseDate(fieldValue);
                } catch (Exception e) {
                    ret.add(new ConditionsNotMetTestEvent(testCase, queryParams,
                            String.format("date parsing error %s", fieldValue), i));
                    continue;
                }
                boolean error = false;
                if (leftInclusive && leftDate.compareTo(dateReturned) > 0)
                    error = true;
                if (!leftInclusive && leftDate.compareTo(dateReturned) >= 0)
                    error = true;
                if (rightInclusive && rightDate.compareTo(dateReturned) < 0)
                    error = true;
                if (!rightInclusive && rightDate.compareTo(dateReturned) <= 0)
                    error = true;
                if (error && !this.reverseConditions)
                    ret.add(new ConditionsNotMetTestEvent(testCase, queryParams,
                            String.format("date range error %s not in %s", fieldValue, this.dateRangeString),
                            i));
                if (!error && this.reverseConditions)
                    ret.add(new ConditionsNotMetTestEvent(testCase, queryParams, String.format(
                            "reverse date range error %s not in %s", fieldValue, this.dateRangeString), i));
            }
        }
    }
    return ret;
}

From source file:org.apache.solr.kelvin.testcases.ValueListCondition.java

public List<ConditionFailureTestEvent> verifyConditions(ITestCase testCase, Properties queryParams,
        Map<String, Object> decodedResponses, Object testSpecificArgument) {
    List<ConditionFailureTestEvent> ret = new ArrayList<ConditionFailureTestEvent>();
    ArrayNode results = new ArrayNode(null);
    if (decodedResponses.containsKey(XmlDoclistExtractorResponseAnalyzer.DOC_LIST)) {
        results = (ArrayNode) decodedResponses.get(XmlDoclistExtractorResponseAnalyzer.DOC_LIST);
    }//from w ww. jav  a  2 s .c o m
    int lengthToCheck = getLengthToCheck(results);
    for (int i = 0; i < lengthToCheck; i++) {
        if (results.size() <= i) {
            ret.add(new MissingResultTestEvent(testCase, queryParams, "result set too short", i));
        } else {
            JsonNode row = results.get(i);
            if (!hasField(row, field)) {
                ret.add(new MissingFieldTestEvent(testCase, queryParams,
                        "missing field " + field + " from result", i));
            } else {
                JsonNode fieldValue = getField(row, field);
                fieldValue = ConfigurableLoader.assureArray(fieldValue);
                boolean found = false;
                ArrayList<String> allTextValues = new ArrayList<String>();
                for (int j = 0; j < fieldValue.size(); j++) {
                    String fieldText = fieldValue.get(j).asText();
                    allTextValues.add(fieldText);
                    if (checkContains(fieldText)) {
                        found = true;
                        break;
                    } else if (legacy) {
                        for (String cond : correctValuesList) {
                            if (cond.endsWith("*")) {
                                if (fieldText.startsWith(cond.substring(0, cond.length() - 1))) {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (!found && !reverseConditions)
                    ret.add(new ConditionsNotMetTestEvent(testCase, queryParams, "unexpected vaule ["
                            + StringUtils.join(allTextValues, ',') + "] not in " + correctValuesList.toString(),
                            i));
                else if (found && reverseConditions) {
                    ret.add(new ConditionsNotMetTestEvent(testCase, queryParams,
                            "[" + StringUtils.join(allTextValues, ',') + "] matches "
                                    + correctValuesList.toString(),
                            i));
                }
            }
        }
    }
    return ret;
}

From source file:org.teavm.flavour.json.test.SerializerTest.java

@Test
public void writesArray() {
    int[] array = { 23, 42 };
    JsonNode node = JSONRunner.serialize(array);

    assertTrue("Root node should be JSON array", node.isArray());

    ArrayNode arrayNode = (ArrayNode) node;
    assertEquals("Length must be 2", 2, arrayNode.size());

    JsonNode firstNode = arrayNode.get(0);
    assertTrue("Item must be numeric", firstNode.isNumber());
    assertEquals(23, firstNode.asInt());
}

From source file:org.apache.solr.kelvin.testcases.SimpleCondition.java

public List<ConditionFailureTestEvent> verifyConditions(ITestCase testCase, Properties queryParams,
        Map<String, Object> decodedResponses, Object testSpecificArgument) {
    List<ConditionFailureTestEvent> ret = new ArrayList<ConditionFailureTestEvent>();
    ArrayNode results = new ArrayNode(null);
    if (decodedResponses.containsKey(XmlDoclistExtractorResponseAnalyzer.DOC_LIST)) {
        results = (ArrayNode) decodedResponses.get(XmlDoclistExtractorResponseAnalyzer.DOC_LIST);
    }/*from  ww w  .  j  av  a  2 s .  com*/
    int lengthToCheck = getLengthToCheck(results);
    for (int i = 0; i < lengthToCheck; i++) {
        if (results.size() <= i) {
            ret.add(new MissingResultTestEvent(testCase, queryParams, "result set too short", i));
        } else {
            JsonNode row = results.get(i);
            boolean allFields = true;
            for (String field : this.fields) {
                if (!hasField(row, field)) {
                    ret.add(new MissingFieldTestEvent(testCase, queryParams,
                            "missing field from result - " + field, i));
                    allFields = false;
                }
            }
            if (allFields) {
                String allText = "";
                for (String field : fields) {
                    JsonNode fieldValue = getField(row, field);
                    if (fieldValue.isArray()) {
                        for (int j = 0; j < fieldValue.size(); j++) {
                            allText = allText + " " + fieldValue.get(j);
                        }
                        //checkAllWords(testCase, queryParams, ret, i,
                        //      allText);

                    } else {
                        String stringFieldValue = fieldValue.asText();
                        allText = allText + " " + stringFieldValue;
                    }
                }
                checkAllWords(testCase, queryParams, ret, i, allText);
            }
        }
    }
    return ret;
}

From source file:org.walkmod.conf.providers.YAMLConfigurationProvider.java

@Override
public void addPluginConfig(final PluginConfig pluginConfig, boolean recursive) throws TransformerException {

    File cfg = new File(fileName);

    ArrayNode pluginList = null;/*from  w  ww. j  ava2 s .  c  om*/
    JsonNode node = null;
    try {
        node = mapper.readTree(cfg);
    } catch (Exception e) {

    }
    if (node == null) {
        node = new ObjectNode(mapper.getNodeFactory());
    }
    if (recursive && node.has("modules")) {
        JsonNode aux = node.get("modules");
        if (aux.isArray()) {
            ArrayNode modules = (ArrayNode) aux;
            int max = modules.size();
            for (int i = 0; i < max; i++) {
                JsonNode module = modules.get(i);
                if (module.isTextual()) {
                    String moduleDir = module.asText();

                    try {
                        File auxFile = new File(fileName).getCanonicalFile().getParentFile();
                        YAMLConfigurationProvider child = new YAMLConfigurationProvider(
                                auxFile.getAbsolutePath() + File.separator + moduleDir + File.separator
                                        + "walkmod.yml");
                        child.createConfig();
                        child.addPluginConfig(pluginConfig, recursive);
                    } catch (IOException e) {
                        throw new TransformerException(e);
                    }

                }
            }
        }
    } else {
        if (!node.has("plugins")) {
            pluginList = new ArrayNode(mapper.getNodeFactory());
            if (node.isObject()) {
                ObjectNode aux = (ObjectNode) node;
                aux.set("plugins", pluginList);
            } else {
                throw new TransformerException("The root element is not a JSON node");
            }
        } else {
            JsonNode aux = node.get("plugins");
            if (aux.isArray()) {
                pluginList = (ArrayNode) node.get("plugins");
            } else {
                throw new TransformerException("The plugins element is not a valid array");
            }
        }
        pluginList.add(new TextNode(pluginConfig.getGroupId() + ":" + pluginConfig.getArtifactId() + ":"
                + pluginConfig.getVersion()));
        write(node);
    }
}

From source file:org.gitana.platform.client.node.NodeImpl.java

@Override
public List<String> getTranslationEditions() {
    Response response = getRemote().get(getResourceUri() + "/i18n/editions");

    ArrayNode array = (ArrayNode) response.getObjectNode().get("editions");

    List<String> editions = new ArrayList<String>();
    for (int i = 0; i < array.size(); i++) {
        editions.add(array.get(i).textValue());
    }//ww  w.  j  a  v a  2 s. c o  m

    return editions;
}