Search in sources :

Example 6 with JsonSlurper

use of groovy.json.JsonSlurper in project pact-jvm by DiUS.

the class ValueMatcherTest method runTest.

@Test
@PactVerification("ValueMatcherProvider")
public void runTest() throws IOException {
    String result = Request.get(provider.getUrl()).addHeader("Accept", APPLICATION_JSON).execute().returnContent().asString();
    Map<String, Object> body = (Map<String, Object>) new JsonSlurper().parseText(result);
    assertThat(body, hasKey("foo"));
    Map<String, Object> foo = (Map<String, Object>) body.get("foo");
    assertThat(foo, hasKey("001"));
    assertThat(foo.get("001"), is(42));
    assertThat(body, hasKey("articles"));
    List articles = (List) body.get("articles");
    assertThat(articles.size(), is(1));
    Map<String, Object> article = (Map<String, Object>) articles.get(0);
    assertThat(article, hasKey("variants"));
    List variants = (List) article.get("variants");
    assertThat(variants.size(), is(1));
    Map<String, Object> variant = (Map<String, Object>) variants.get(0);
    assertThat(variant.keySet(), is(equalTo(Sets.newHashSet("001"))));
    List variant001 = (List) variant.get("001");
    assertThat(variant001.size(), is(1));
    Map<String, Object> firstVariant001 = (Map<String, Object>) variant001.get(0);
    assertThat(firstVariant001, hasKey("bundles"));
    List bundles = (List) firstVariant001.get("bundles");
    assertThat(bundles.size(), is(1));
    Map<String, Object> bundle = (Map<String, Object>) bundles.get(0);
    assertThat(bundle.keySet(), is(equalTo(Sets.newHashSet("001-A"))));
    Map<String, Object> bundle001A = (Map<String, Object>) bundle.get("001-A");
    assertThat(bundle001A.get("description").toString(), is("Some Description"));
    assertThat(bundle001A, hasKey("referencedArticles"));
    List referencedArticles = (List) bundle001A.get("referencedArticles");
    assertThat(referencedArticles.size(), is(1));
    Map<String, Object> referencedArticle = (Map<String, Object>) referencedArticles.get(0);
    assertThat(referencedArticle, hasKey("bundleId"));
    assertThat(referencedArticle.get("bundleId").toString(), is("23456"));
}
Also used : JsonSlurper(groovy.json.JsonSlurper) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Example 7 with JsonSlurper

use of groovy.json.JsonSlurper in project gradle by gradle.

the class BuildOperationTrace method readLogToTreeRoots.

private static List<BuildOperationRecord> readLogToTreeRoots(final File logFile) {
    try {
        final JsonSlurper slurper = new JsonSlurper();
        final List<BuildOperationRecord> roots = new ArrayList<>();
        final Map<Object, PendingOperation> pendings = new HashMap<>();
        final Map<Object, List<BuildOperationRecord>> childrens = new HashMap<>();
        Files.asCharSource(logFile, Charsets.UTF_8).readLines(new LineProcessor<Void>() {

            @Override
            public boolean processLine(@SuppressWarnings("NullableProblems") String line) {
                Map<String, ?> map = uncheckedNonnullCast(slurper.parseText(line));
                if (map.containsKey("startTime")) {
                    SerializedOperationStart serialized = new SerializedOperationStart(map);
                    pendings.put(serialized.id, new PendingOperation(serialized));
                    childrens.put(serialized.id, new LinkedList<>());
                } else if (map.containsKey("time")) {
                    SerializedOperationProgress serialized = new SerializedOperationProgress(map);
                    PendingOperation pending = pendings.get(serialized.id);
                    assert pending != null : "did not find owner of progress event with ID " + serialized.id;
                    pending.progress.add(serialized);
                } else {
                    SerializedOperationFinish finish = new SerializedOperationFinish(map);
                    PendingOperation pending = pendings.remove(finish.id);
                    assert pending != null;
                    List<BuildOperationRecord> children = childrens.remove(finish.id);
                    assert children != null;
                    SerializedOperationStart start = pending.start;
                    Map<String, ?> detailsMap = uncheckedCast(start.details);
                    Map<String, ?> resultMap = uncheckedCast(finish.result);
                    List<BuildOperationRecord.Progress> progresses = new ArrayList<>();
                    for (SerializedOperationProgress progress : pending.progress) {
                        Map<String, ?> progressDetailsMap = uncheckedCast(progress.details);
                        progresses.add(new BuildOperationRecord.Progress(progress.time, progressDetailsMap, progress.detailsClassName));
                    }
                    BuildOperationRecord record = new BuildOperationRecord(start.id, start.parentId, start.displayName, start.startTime, finish.endTime, detailsMap == null ? null : Collections.unmodifiableMap(detailsMap), start.detailsClassName, resultMap == null ? null : Collections.unmodifiableMap(resultMap), finish.resultClassName, finish.failureMsg, progresses, BuildOperationRecord.ORDERING.immutableSortedCopy(children));
                    if (start.parentId == null) {
                        roots.add(record);
                    } else {
                        List<BuildOperationRecord> parentChildren = childrens.get(start.parentId);
                        assert parentChildren != null : "parentChildren != null '" + line + "' from " + logFile;
                        parentChildren.add(record);
                    }
                }
                return true;
            }

            @Override
            public Void getResult() {
                return null;
            }
        });
        assert pendings.isEmpty();
        return roots;
    } catch (Exception e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
}
Also used : JsonSlurper(groovy.json.JsonSlurper) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) UncheckedException(org.gradle.internal.UncheckedException) IOException(java.io.IOException) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with JsonSlurper

use of groovy.json.JsonSlurper in project pact-jvm by DiUS.

the class WildcardKeysTest method runTest.

@Test
@PactVerification("WildcardKeysProvider")
public void runTest() throws IOException {
    String result = Request.Get("http://localhost:8081/").addHeader("Accept", APPLICATION_JSON).execute().returnContent().asString();
    Map<String, Object> body = (Map<String, Object>) new JsonSlurper().parseText(result);
    assertThat(body, hasKey("foo"));
    Map<String, Object> foo = (Map<String, Object>) body.get("foo");
    assertThat(foo, hasKey("001"));
    assertThat(foo.get("001"), is(42));
    assertThat(body, hasKey("articles"));
    List articles = (List) body.get("articles");
    assertThat(articles.size(), is(1));
    Map<String, Object> article = (Map<String, Object>) articles.get(0);
    assertThat(article, hasKey("variants"));
    List variants = (List) article.get("variants");
    assertThat(variants.size(), is(1));
    Map<String, Object> variant = (Map<String, Object>) variants.get(0);
    assertThat(variant.keySet(), is(equalTo(Sets.newHashSet("001"))));
    List variant001 = (List) variant.get("001");
    assertThat(variant001.size(), is(1));
    Map<String, Object> firstVariant001 = (Map<String, Object>) variant001.get(0);
    assertThat(firstVariant001, hasKey("bundles"));
    List bundles = (List) firstVariant001.get("bundles");
    assertThat(bundles.size(), is(1));
    Map<String, Object> bundle = (Map<String, Object>) bundles.get(0);
    assertThat(bundle.keySet(), is(equalTo(Sets.newHashSet("001-A"))));
    Map<String, Object> bundle001A = (Map<String, Object>) bundle.get("001-A");
    assertThat(bundle001A.get("description").toString(), is("Some Description"));
    assertThat(bundle001A, hasKey("referencedArticles"));
    List referencedArticles = (List) bundle001A.get("referencedArticles");
    assertThat(referencedArticles.size(), is(1));
    Map<String, Object> referencedArticle = (Map<String, Object>) referencedArticles.get(0);
    assertThat(referencedArticle, hasKey("bundleId"));
    assertThat(referencedArticle.get("bundleId").toString(), is("23456"));
}
Also used : JsonSlurper(groovy.json.JsonSlurper) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Example 9 with JsonSlurper

use of groovy.json.JsonSlurper in project hale by halestudio.

the class JsonValueRepresentation method getValueRepresentation.

@Override
public Object getValueRepresentation(Value value) {
    if (value == null) {
        return null;
    }
    if (value.isRepresentedAsDOM()) {
        // try conversion using complex value mechanism
        Object intern = value.getValue();
        if (intern == null) {
            return null;
        }
        ComplexValueDefinition cdv = ComplexValueExtension.getInstance().getDefinition(intern.getClass());
        if (cdv != null && cdv.getJsonConverter() != null) {
            return cdv.getJsonConverter().toJson(intern);
        }
        // fall-back to generic XML-JSON conversion
        Element element = value.getDOMRepresentation();
        if (element == null) {
            return null;
        } else {
            String json = null;
            try {
                String xmlString = XmlUtil.serialize(element, false);
                try (StringReader xmlReader = new StringReader(xmlString);
                    StringWriter jsonWriter = new StringWriter()) {
                    JsonXML.toJson(xmlReader, jsonWriter);
                    json = jsonWriter.toString();
                }
            } catch (Exception e) {
                log.error("Failed to created JSON representation of value", e);
                return null;
            }
            Object res = new JsonSlurper().parseText(json);
            return res;
        }
    } else {
        return value.getStringRepresentation();
    }
}
Also used : JsonSlurper(groovy.json.JsonSlurper) StringWriter(java.io.StringWriter) ComplexValueDefinition(eu.esdihumboldt.hale.common.core.io.extension.ComplexValueDefinition) Element(org.w3c.dom.Element) StringReader(java.io.StringReader)

Example 10 with JsonSlurper

use of groovy.json.JsonSlurper in project nifi by apache.

the class ExecuteGroovyScriptTest method test_sql_04_insert_and_json.

@Test
public void test_sql_04_insert_and_json() throws Exception {
    // read blob from database written at previous step and write to flow file
    runner.setProperty(proc.SCRIPT_FILE, TEST_RESOURCE_LOCATION + "test_sql_04_insert_and_json.groovy");
    runner.setProperty("SQL.mydb", "dbcp");
    runner.setValidateExpressionUsage(false);
    runner.assertValid();
    runner.enqueue(new FileInputStream(TEST_RESOURCE_LOCATION + "test_sql_04_insert_and_json.json"));
    runner.run();
    // number of inserted rows
    runner.assertAllFlowFilesTransferred(proc.REL_SUCCESS.getName(), 3);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(proc.REL_SUCCESS.getName());
    MockFlowFile resultFile = result.get(0);
    List<String> lines = ResourceGroovyMethods.readLines(new File(TEST_RESOURCE_LOCATION + "test_sql_04_insert_and_json.json"), "UTF-8");
    // pass through to&from json before compare
    resultFile.assertContentEquals(JsonOutput.toJson(new JsonSlurper().parseText(lines.get(1))), "UTF-8");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) JsonSlurper(groovy.json.JsonSlurper) File(java.io.File) MockFlowFile(org.apache.nifi.util.MockFlowFile) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

JsonSlurper (groovy.json.JsonSlurper)14 Map (java.util.Map)9 List (java.util.List)6 IOException (java.io.IOException)4 File (java.io.File)3 StringReader (java.io.StringReader)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 DslPart (au.com.dius.pact.consumer.dsl.DslPart)2 PactDslJsonArray (au.com.dius.pact.consumer.dsl.PactDslJsonArray)2 RequestResponsePact (au.com.dius.pact.model.RequestResponsePact)2 XmlSlurper (groovy.util.XmlSlurper)2 GPathResult (groovy.util.slurpersupport.GPathResult)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 StringWriter (java.io.StringWriter)2 ArgsLookup (com.disney.groovity.ArgsLookup)1 BindingDecorator (com.disney.groovity.BindingDecorator)1 Groovity (com.disney.groovity.Groovity)1 GroovityBuilder (com.disney.groovity.GroovityBuilder)1