DifferentialJsonResultsParser extends JsonExceptionHandlingController { public ImmutableList<DiffAnalytics> parseDifferentialResults(JsonObject obj) { try { ImmutableList.Builder<DiffAnalytics> diffAnalyticsListBuilder = ImmutableList.builder(); JsonArray results = obj.getAsJsonArray("results"); for (JsonElement e : results) { DiffAnalytics diffAnalytics = buildDiffAnalyticsObject(e); if (Optional.ofNullable(diffAnalytics).isPresent()) { diffAnalyticsListBuilder.add(diffAnalytics); } } return diffAnalyticsListBuilder.build(); } catch (JsonParseException e) { throw new RuntimeException("Input Json is missing a results field OR results field is not an Array", e); } } @Inject  DifferentialJsonResultsParser(ContrastTrader contrastTrader); ImmutableList<DiffAnalytics> parseDifferentialResults(JsonObject obj);  }
@Test public void emptyJsonResultArrayProducesEmptyList() { JsonObject result = buildJsonObject(0, false); List<DiffAnalytics> expression = subject.parseDifferentialResults(result); assertThat(expression, hasSize(0)); }
@Test public void numberOfJsonObjectsInResultArrayProducesEqualLengthList() { JsonObject result = buildJsonObject(3, false); List<DiffAnalytics> expression = subject.parseDifferentialResults(result); assertThat(expression, hasSize(3)); }
@Test public void secondJsonObjectsInResultArrayEqualsSecondElementOfList() { JsonObject result = buildJsonObject(4, false); List<DiffAnalytics> expression = subject.parseDifferentialResults(result); assertThat(expression.get(1).getExperimentAccession(), isA(String.class)); assertThat(expression.get(1).getBioentityId(), isA(String.class)); assertThat(expression.get(1).getSpecies(), isA(String.class)); assertThat(expression.get(1).getExpression().getPValue(), isA(Double.class)); assertThat(expression.get(1).getExpression().getFoldChange(), isA(Double.class)); }
@Test public void testingJsonObjectWithoutTstatistic() { JsonObject testObject = new JsonObject(); testObject.addProperty("experiment_accession", "e" + 4); testObject.addProperty("contrast_id", "c" + 4); testObject.addProperty("bioentity_identifier", "id" + 4); testObject.addProperty("species", "org" + 4); testObject.addProperty("p_value", 1.14); testObject.addProperty("fold_change", -1.14); testObject.addProperty("keyword_symbol", 4); JsonArray testArray = new JsonArray(); testArray.add(testObject); JsonObject result = new JsonObject(); result.add("results", testArray); List<DiffAnalytics> expression = subject.parseDifferentialResults(result); assertThat(expression, hasSize(1)); }
@Test public void rejectInvalidJsonObjectsInResultArray() { JsonObject result = buildJsonObject(3, true); List<DiffAnalytics> expression = subject.parseDifferentialResults(result); assertThat(expression, hasSize(3)); }