Example usage for org.json.simple JSONObject containsKey

List of usage examples for org.json.simple JSONObject containsKey

Introduction

In this page you can find the example usage for org.json.simple JSONObject containsKey.

Prototype

boolean containsKey(Object key);

Source Link

Document

Returns true if this map contains a mapping for the specified key.

Usage

From source file:org.jolokia.converter.json.BeanExtractorTest.java

@Test
public void simple() throws AttributeNotFoundException {
    JSONObject res = (JSONObject) extractJson(this);
    assertEquals(res.get("number"), 10);
    assertEquals(res.get("text"), "Test");
    assertFalse((Boolean) res.get("flag"));
    assertEquals(((JSONObject) res.get("inner")).get("innerText"), "innerValue");
    assertNull(res.get("nulli"));
    assertTrue(!res.containsKey("forbiddenStream"));
    assertTrue(res.containsKey("nulli"));
    assertEquals(res.get("nacked"), "nacked object");
    assertEquals(res.get("self"), "[this]");

    JSONObject inner = (JSONObject) extractJson(this, "inner");
    assertEquals(inner.get("innerText"), "innerValue");

    JSONObject innerWithWildcardPath = (JSONObject) extractJson(this, null, "innerDate");
    assertEquals(innerWithWildcardPath.size(), 1);
    assertTrue((Long) ((JSONObject) innerWithWildcardPath.get("inner")).get("millis") <= new Date().getTime());

    BeanExtractorTest test = (BeanExtractorTest) extractObject(this);
    assertEquals(test, this);

    Date date = (Date) extractObject(this, "inner", "innerDate");
    assertTrue(date.getTime() <= new Date().getTime());

}

From source file:org.jolokia.converter.json.TabularDataExtractorTest.java

@Test
public void extractMapWithComplexType() throws OpenDataException, AttributeNotFoundException {
    CompositeTypeAndJson cdj = new CompositeTypeAndJson(STRING, "name", "roland", INTEGER, "date", 1968);
    TabularData data = getMapTabularData(cdj.getType(), cdj.getCompositeData(), TEST_VALUE);

    JSONObject result = (JSONObject) extract(true, data);
    assertEquals(result.size(), 2);/*  w w w  .  j  a  va 2  s  .  c om*/
    assertTrue(result.containsKey("indexNames"));
    assertTrue(result.containsKey("values"));
    List indexNames = (List) result.get("indexNames");
    assertEquals(indexNames.size(), 1);
    assertTrue(indexNames.contains("key"));
    List values = (List) result.get("values");
    assertEquals(values.size(), 1);
    JSONObject value = (JSONObject) values.get(0);
    JSONObject key = (JSONObject) value.get("key");
    assertEquals(key.get("name"), "roland");
    assertEquals(key.get("date"), 1968);
    assertEquals(key.size(), 2);

    assertEquals(value.get("value"), TEST_VALUE);
    assertEquals(key.size(), 2);
}

From source file:org.jolokia.converter.json.TabularDataExtractorTest.java

@Test
void extractGenericTabularDataWithJson() throws OpenDataException, AttributeNotFoundException {
    TabularData data = getComplexTabularData();
    JSONObject result = (JSONObject) extract(true, data);
    assertEquals(result.size(), 2);/*from w w  w. j a  v a  2 s. c o m*/
    assertTrue(result.containsKey("meyer"));
    assertTrue(result.containsKey("huber"));
    JSONObject meyerMap = (JSONObject) result.get("meyer");
    assertTrue(meyerMap.containsKey("xaver"));
    assertTrue(meyerMap.containsKey("zensi"));
    assertEquals(meyerMap.size(), 2);
    JSONObject zensiMap = (JSONObject) meyerMap.get("zensi");
    assertEquals(zensiMap.get("name"), "meyer");
    assertEquals(zensiMap.get("firstname"), "zensi");
    assertEquals(zensiMap.get("age"), 28);
    assertEquals(zensiMap.get("male"), false);
    assertEquals(zensiMap.size(), 4);
}

From source file:org.jolokia.converter.object.TabularDataConverter.java

private boolean checkForFullTabularDataRepresentation(JSONObject pValue, TabularType pType) {
    if (pValue.containsKey("indexNames") && pValue.containsKey("values") && pValue.size() == 2) {
        Object jsonVal = pValue.get("indexNames");
        if (!(jsonVal instanceof JSONArray)) {
            throw new IllegalArgumentException(
                    "Index names for tabular data must given as JSON array, not " + jsonVal.getClass());
        }//  ww  w . j  a  v a  2  s  .co  m
        JSONArray indexNames = (JSONArray) jsonVal;
        List<String> tabularIndexNames = pType.getIndexNames();
        if (indexNames.size() != tabularIndexNames.size()) {
            throw new IllegalArgumentException(
                    "Given array with index names must have " + tabularIndexNames.size() + " entries "
                            + "(given: " + indexNames + ", required: " + tabularIndexNames + ")");
        }
        for (Object index : indexNames) {
            if (!tabularIndexNames.contains(index.toString())) {
                throw new IllegalArgumentException("No index with name '" + index + "' known " + "(given: "
                        + indexNames + ", required: " + tabularIndexNames + ")");
            }
        }
        return true;
    }
    return false;
}

From source file:org.jolokia.jvmagent.handler.JolokiaHttpHandlerTest.java

@Test
public void invalidMethod() throws URISyntaxException, IOException, ParseException {
    HttpExchange exchange = prepareExchange("http://localhost:8080/");

    // Simple GET method
    expect(exchange.getRequestMethod()).andReturn("PUT");
    Headers header = new Headers();
    ByteArrayOutputStream out = prepareResponse(handler, exchange, header);
    handler.doHandle(exchange);//from  w w  w.  jav  a  2s  .c om

    JSONObject resp = (JSONObject) new JSONParser().parse(out.toString());
    assertTrue(resp.containsKey("error"));
    assertEquals(resp.get("error_type"), IllegalArgumentException.class.getName());
    assertTrue(((String) resp.get("error")).contains("PUT"));
}

From source file:org.jolokia.jvmagent.handler.JolokiaHttpHandlerTest.java

@Test
public void customRestrictor() throws URISyntaxException, IOException, ParseException {
    System.setProperty("jolokia.test1.policy.location", "access-restrictor.xml");
    System.setProperty("jolokia.test2.policy.location", "access-restrictor");
    for (String[] params : new String[][] { { "classpath:/access-restrictor.xml", "not allowed" },
            { "file:///not-existing.xml", "No access" },
            { "classpath:/${prop:jolokia.test1.policy.location}", "not allowed" },
            { "classpath:/${prop:jolokia.test2.policy.location}.xml", "not allowed" } }) {
        Configuration config = getConfig(ConfigKey.POLICY_LOCATION, params[0]);
        JSONObject resp = simpleMemoryGetReadRequest(config);
        assertTrue(resp.containsKey("error"));
        assertTrue(((String) resp.get("error")).contains(params[1]));
    }/* www.  ja v a2 s . c o  m*/
}

From source file:org.jolokia.jvmagent.handler.JolokiaHttpHandlerTest.java

@Test
public void customTestRestrictorTrue() throws URISyntaxException, IOException, ParseException {

    Configuration config = getConfig(ConfigKey.RESTRICTOR_CLASS, AllowAllRestrictor.class.getName());
    JSONObject resp = simpleMemoryGetReadRequest(config);
    assertFalse(resp.containsKey("error"));

}

From source file:org.jolokia.jvmagent.handler.JolokiaHttpHandlerTest.java

@Test
public void customTestRestrictorFalse() throws URISyntaxException, IOException, ParseException {
    Configuration config = getConfig(ConfigKey.RESTRICTOR_CLASS, DenyAllRestrictor.class.getName());
    JSONObject resp = simpleMemoryGetReadRequest(config);
    assertTrue(resp.containsKey("error"));
    assertTrue(((String) resp.get("error")).contains("No access"));
}

From source file:org.jolokia.jvmagent.handler.JolokiaHttpHandlerTest.java

@Test
public void customTestRestrictorWithConfigTrue() throws URISyntaxException, IOException, ParseException {
    Configuration config = getConfig(ConfigKey.RESTRICTOR_CLASS, TestRestrictorWithConfig.class.getName(),
            ConfigKey.POLICY_LOCATION, "true");
    JSONObject resp = simpleMemoryGetReadRequest(config);
    assertFalse(resp.containsKey("error"));
}

From source file:org.jolokia.jvmagent.handler.JolokiaHttpHandlerTest.java

@Test
public void customTestRestrictorWithConfigFalse() throws URISyntaxException, IOException, ParseException {
    Configuration config = getConfig(ConfigKey.RESTRICTOR_CLASS, TestRestrictorWithConfig.class.getName(),
            ConfigKey.POLICY_LOCATION, "false");
    JSONObject resp = simpleMemoryGetReadRequest(config);
    assertTrue(resp.containsKey("error"));
    assertTrue(((String) resp.get("error")).contains("No access"));
}