StatusUtils { public static Status getSummaryStatus(Map<String, Status> statuses) { Level level = Level.OK; StringBuilder msg = new StringBuilder(); for (Map.Entry<String, Status> entry : statuses.entrySet()) { String key = entry.getKey(); Status status = entry.getValue(); Level l = status.getLevel(); if (Level.ERROR.equals(l)) { level = Level.ERROR; if (msg.length() > 0) { msg.append(","); } msg.append(key); } else if (Level.WARN.equals(l)) { if (!Level.ERROR.equals(level)) { level = Level.WARN; } if (msg.length() > 0) { msg.append(","); } msg.append(key); } } return new Status(level, msg.toString()); }  static Status getSummaryStatus(Map<String, Status> statuses);  }
@Test public void testGetSummaryStatus1() throws Exception { Status status1 = new Status(Status.Level.ERROR); Status status2 = new Status(Status.Level.WARN); Status status3 = new Status(Status.Level.OK); Map<String, Status> statuses = new HashMap<String, Status>(); statuses.put("status1", status1); statuses.put("status2", status2); statuses.put("status3", status3); Status status = StatusUtils.getSummaryStatus(statuses); assertThat(status.getLevel(), is(Status.Level.ERROR)); assertThat(status.getMessage(), containsString("status1")); assertThat(status.getMessage(), containsString("status2")); assertThat(status.getMessage(), not(containsString("status3"))); }
@Test public void testGetSummaryStatus2() throws Exception { Status status1 = new Status(Status.Level.WARN); Status status2 = new Status(Status.Level.OK); Map<String, Status> statuses = new HashMap<String, Status>(); statuses.put("status1", status1); statuses.put("status2", status2); Status status = StatusUtils.getSummaryStatus(statuses); assertThat(status.getLevel(), is(Status.Level.WARN)); assertThat(status.getMessage(), containsString("status1")); assertThat(status.getMessage(), not(containsString("status2"))); }
@Test public void testGetSummaryStatus3() throws Exception { Status status1 = new Status(Status.Level.OK); Map<String, Status> statuses = new HashMap<String, Status>(); statuses.put("status1", status1); Status status = StatusUtils.getSummaryStatus(statuses); assertThat(status.getLevel(), is(Status.Level.OK)); assertThat(status.getMessage(), isEmptyOrNullString()); }