PumpEnactResult { public JSONObject json(Profile profile) { JSONObject result = new JSONObject(); try { if (bolusDelivered > 0) { result.put("smb", bolusDelivered); } else if (isTempCancel) { result.put("rate", 0); result.put("duration", 0); } else if (isPercent) { Double abs = Round.roundTo(profile.getBasal() * percent / 100, 0.01); result.put("rate", abs); result.put("duration", duration); } else { result.put("rate", absolute); result.put("duration", duration); } } catch (JSONException e) { log.error("Unhandled exception", e); } return result; }  PumpEnactResult success(boolean success); PumpEnactResult enacted(boolean enacted); PumpEnactResult comment(String comment); PumpEnactResult comment(int comment); PumpEnactResult duration(int duration); PumpEnactResult absolute(double absolute); PumpEnactResult percent(int percent); PumpEnactResult isPercent(boolean isPercent); PumpEnactResult isTempCancel(boolean isTempCancel); PumpEnactResult bolusDelivered(double bolusDelivered); PumpEnactResult carbsDelivered(double carbsDelivered); PumpEnactResult queued(boolean queued); String log(); String toString(); String toHtml(); JSONObject json(Profile profile); public boolean success; public boolean enacted; public String comment; public int duration; public double absolute; public int percent; public boolean isPercent; public boolean isTempCancel; public double bolusDelivered; public double carbsDelivered; public boolean queued; }
@Test public void jsonTest() throws Exception { JSONObject o; per = new PumpEnactResult().enacted(true).bolusDelivered(10).comment("AAA"); o = per.json(AAPSMocker.getValidProfile()); JSONAssert.assertEquals("{\"smb\":10}", o, false); per = new PumpEnactResult().enacted(true).isTempCancel(true).comment("AAA"); o = per.json(AAPSMocker.getValidProfile()); JSONAssert.assertEquals("{\"rate\":0,\"duration\":0}", o, false); per = new PumpEnactResult().enacted(true).isPercent(true).percent(90).duration(20).comment("AAA"); o = per.json(AAPSMocker.getValidProfile()); JSONAssert.assertEquals("{\"rate\":0.9,\"duration\":20}", o, false); per = new PumpEnactResult().enacted(true).isPercent(false).absolute(1).duration(30).comment("AAA"); o = per.json(AAPSMocker.getValidProfile()); JSONAssert.assertEquals("{\"rate\":1,\"duration\":30}", o, false); }