Commit 51a6994a by Tomas Dvorak

Output valid JSON results

parent 5e9035c1
......@@ -2,7 +2,7 @@ package com.github.variety.test;
import com.github.variety.Variety;
import com.github.variety.VarietyAnalysis;
import com.mongodb.DBObject;
import com.mongodb.BasicDBList;
import com.mongodb.util.JSON;
import org.junit.After;
import org.junit.Assert;
......@@ -30,20 +30,16 @@ public class OutputFormatTest {
@Test
public void verifyJsonEntries() throws Exception {
final VarietyAnalysis analysis = variety.withFormat(Variety.FORMAT_JSON).runAnalysis();
final VarietyAnalysis analysis = variety
.withQuiet(true) // do not output any other metadata, only results
.withFormat(Variety.FORMAT_JSON)
.runAnalysis();
// TODO: output itself is not valid JSON. It contains mongo shell output (can be removed with --quiet) and variety execution info.
// At the end of output, there are printed records from result collection, every record on new line.
// Valid json output is requested in issue https://github.com/variety/variety/issues/23
// Verify, that output is parse-able json by transforming stdout to json
final BasicDBList parsed = (BasicDBList) JSON.parse(analysis.getStdOut());
// Verify, that every object is parse-able json by transforming strings to json stream
// Results are detected by line starting with character '{'.
final Stream<DBObject> objects = Stream.of(analysis.getStdOut().split("\n"))
.filter(line -> line.startsWith("{"))
.map(str -> (DBObject)JSON.parse(str));
// there should be seven different json results in the stdout
Assert.assertEquals(7, objects.count());
// there should be seven different json results
Assert.assertEquals(7, parsed.size());
}
@Test
......
......@@ -227,9 +227,7 @@ resultsDB[resultsCollectionName].find({}).forEach(function(key) {
var sortedKeys = resultsDB[resultsCollectionName].find({}).sort({totalOccurrences: -1});
if(outputFormat === 'json') {
sortedKeys.forEach(function(key) {
print(tojson(key, '', true));
});
printjson(sortedKeys.toArray()); // valid formatted json output, compressed variant is printjsononeline()
} else { // output nice ascii table with results
var table = [["key", "types", "occurrences", "percents"], ["", "", "", ""]]; // header + delimiter rows
sortedKeys.forEach(function(key) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment