BaselineExpression implements Expression { @Override public JsonObject toJson() { JsonObject result = new JsonObject(); result.addProperty("value", level); if (quartiles != null && quartiles.size() == 5) { JsonObject quartilesJson = new JsonObject(); quartilesJson.addProperty("min", quartiles.get(0)); quartilesJson.addProperty("lower", quartiles.get(1)); quartilesJson.addProperty("median", quartiles.get(2)); quartilesJson.addProperty("upper", quartiles.get(3)); quartilesJson.addProperty("max", quartiles.get(4)); result.add("quartiles", quartilesJson); } return result; } BaselineExpression(double level);  BaselineExpression(double min,
                              double lowerQuartile,
                              double median,
                              double upperQuartile,
                              double max); private  BaselineExpression(double level, double[] quartiles); static BaselineExpression create(String expressionLevelString); double[] getQuartiles(); double getLevel(); boolean isGreaterThanOrEqual(double level); @Override boolean equals(Object object); @Override int hashCode(); @Override String toString(); @Override JsonObject toJson();  }
@Test public void jsonSerializationWithoutQuartiles() { JsonObject jsonObject = new BaselineExpression(1.0).toJson(); assertThat(jsonObject.get("value").getAsDouble()).isEqualTo(1.0); assertThat(jsonObject.get("quartiles")).isNull(); }
@Test public void jsonSerializationWithQuartiles() { JsonObject jsonObject = new BaselineExpression(1.0, 2.0, 3.0, 4.0, 5.0).toJson(); assertThat(jsonObject.get("value").getAsDouble()).isEqualTo(3.0); assertThat(jsonObject.get("quartiles").getAsJsonObject().get("min").getAsDouble()) .isEqualTo(1.0); assertThat(jsonObject.get("quartiles").getAsJsonObject().get("lower").getAsDouble()) .isEqualTo(2.0); assertThat(jsonObject.get("quartiles").getAsJsonObject().get("median").getAsDouble()) .isEqualTo(3.0); assertThat(jsonObject.get("quartiles").getAsJsonObject().get("upper").getAsDouble()) .isEqualTo(4.0); assertThat(jsonObject.get("quartiles").getAsJsonObject().get("max").getAsDouble()) .isEqualTo(5.0); }