Example usage for javax.json JsonObject size

List of usage examples for javax.json JsonObject size

Introduction

In this page you can find the example usage for javax.json JsonObject size.

Prototype

int size();

Source Link

Document

Returns the number of key-value mappings in this map.

Usage

From source file:io.bitgrillr.gocddockerexecplugin.DockerExecPluginTest.java

@Test
public void handleValidateError() throws Exception {
    DefaultGoPluginApiRequest request = new DefaultGoPluginApiRequest(null, null, "validate");
    Map<String, Object> body = new HashMap<>();
    Map<String, String> image = new HashMap<>();
    image.put("value", "ubuntu:");
    body.put("IMAGE", image);
    request.setRequestBody(Json.createObjectBuilder(body).build().toString());

    GoPluginApiResponse response = new DockerExecPlugin().handle(request);

    assertEquals("Expected successful response", DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE,
            response.responseCode());//  www  .  j  a  va 2 s  .  c om
    JsonObject errors = Json.createReader(new StringReader(response.responseBody())).readObject()
            .getJsonObject("errors");
    assertEquals("Expected 1 error", 1, errors.size());
    assertEquals("Wrong message", "'ubuntu:' is not a valid image identifier", errors.getString("IMAGE"));
}

From source file:io.bitgrillr.gocddockerexecplugin.DockerExecPluginTest.java

@Test
public void handleValidate() throws Exception {
    DefaultGoPluginApiRequest request = new DefaultGoPluginApiRequest(null, null, "validate");
    Map<String, Object> body = new HashMap<>();
    Map<String, String> image = new HashMap<>();
    image.put("value", "ubuntu:latest");
    body.put("IMAGE", image);
    request.setRequestBody(Json.createObjectBuilder(body).build().toString());

    GoPluginApiResponse response = new DockerExecPlugin().handle(request);

    assertEquals("Expected successful response", DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE,
            response.responseCode());/*from w  ww. j a  v  a  2 s  .  c  om*/
    JsonObject errors = Json.createReader(new StringReader(response.responseBody())).readObject()
            .getJsonObject("errors");
    assertEquals("Expected no errors", 0, errors.size());
}

From source file:nl.nn.adapterframework.align.Json2Xml.java

@Override
public void startParse(JsonValue node) throws SAXException {
    if (node instanceof JsonObject) {
        JsonObject root = (JsonObject) node;
        if (StringUtils.isEmpty(getRootElement())) {
            if (root.isEmpty()) {
                throw new SAXException("no names found");
            }/*w ww. j a  v a  2s . c  o m*/
            if (root.size() > 1) {
                String namesList = null;
                int i = 0;
                for (String name : root.keySet()) {
                    if (namesList == null) {
                        namesList = name;
                    } else {
                        namesList += "," + name;
                    }
                    if (i++ > 5) {
                        namesList += ", ...";
                        break;
                    }
                }
                throw new SAXException("too many names [" + namesList + "]");
            }
            setRootElement((String) root.keySet().toArray()[0]);
        }
        // determine somewhat heuristically whether the json contains a 'root' node:
        // if the outermost JsonObject contains only one key, that has the name of the root element, 
        // then we'll assume that that is the root element...
        if (root.size() == 1 && getRootElement().equals(root.keySet().toArray()[0])) {
            node = root.get(getRootElement());
        }
    }
    if (node instanceof JsonArray && !insertElementContainerElements && strictSyntax) {
        throw new SAXException(
                MSG_EXPECTED_SINGLE_ELEMENT + " [" + getRootElement() + "] or array element container");
    }
    super.startParse(node);
}

From source file:org.hyperledger.fabric.sdk.ChaincodeCollectionConfiguration.java

private SignaturePolicy parsePolicy(IndexedHashMap<String, MSPPrincipal> identities, JsonObject policy)
        throws ChaincodeCollectionConfigurationException {

    if (policy.size() != 1) {
        throw new ChaincodeCollectionConfigurationException(
                format("Expected policy size of 1 but got %d", policy.size()));
    }// w  w  w.j  a v  a  2 s  . com
    final String key = policy.entrySet().iterator().next().getKey();

    if ("signed-by".equals(key)) {
        final String vo = getJsonString(policy, key);

        MSPPrincipal mspPrincipal = identities.get(vo);
        if (null == mspPrincipal) {
            throw new ChaincodeCollectionConfigurationException(
                    format("No identity found by name %s in signed-by.", vo));
        }

        return SignaturePolicy.newBuilder().setSignedBy(identities.getKeysIndex(vo)).build();

    } else {

        Matcher match = noofPattern.matcher(key);
        final JsonArray vo = getJsonArray(policy, key);

        if (match.matches() && match.groupCount() == 1) {

            String matchStingNo = match.group(1).trim();
            int matchNo = Integer.parseInt(matchStingNo);

            if (vo.size() < matchNo) {

                throw new ChaincodeCollectionConfigurationException(
                        format("%s expected to have at least %d items to match but only found %d.", key,
                                matchNo, vo.size()));
            }

            SignaturePolicy.NOutOf.Builder spBuilder = SignaturePolicy.NOutOf.newBuilder().setN(matchNo);

            for (int i = vo.size() - 1; i >= 0; --i) {
                JsonValue jsonValue = vo.get(i);
                if (jsonValue.getValueType() != JsonValue.ValueType.OBJECT) {
                    throw new ChaincodeCollectionConfigurationException(
                            format("Expected object type in Nof but got %s", jsonValue.getValueType().name()));
                }

                SignaturePolicy sp = parsePolicy(identities, jsonValue.asJsonObject());
                spBuilder.addRules(sp);

            }

            return SignaturePolicy.newBuilder().setNOutOf(spBuilder.build()).build();

        } else {

            throw new ChaincodeCollectionConfigurationException(format("Unsupported policy type %s", key));
        }
    }

}