private Map<String, ?> statusDetailsOf(final List<StatusDetail> statusDetails) { final Map<String, Object> map = new LinkedHashMap<>(); for (StatusDetail entry : statusDetails) { map.put(toCamelCase(entry.getName()), new LinkedHashMap<String, String>() {{ put("status", entry.getStatus().name()); put("message", entry.getMessage()); putAll(entry.getDetails().entrySet().stream().collect(Collectors.toMap(entry -> toCamelCase(entry.getKey()), Map.Entry::getValue))); }}); } return map; }
private Map<String, ?> statusDetailsOf(final List<StatusDetail> statusDetails) { final Map<String, Object> map = new LinkedHashMap<>(); for (StatusDetail entry : statusDetails) { final List<Map<String, String>> links = toLinks(entry.getLinks()); map.put(toCamelCase(entry.getName()), new LinkedHashMap<String, Object>() {{ put("status", entry.getStatus().name()); put("message", entry.getMessage()); put("links", links); putAll(entry.getDetails().entrySet().stream().collect(Collectors.toMap(entry -> toCamelCase(entry.getKey()), Map.Entry::getValue))); }}); } return map; }
@Test public void shouldAcceptIfNoJobRan() { // given when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(emptyList()); // when final StatusDetail statusDetail = errorOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(statusDetail.getStatus(), is(Status.OK)); }
@Test @SuppressWarnings("unchecked") public void shouldIndicateErrorIfJobCouldNotBeRetievedFromRepository() { // given when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenThrow(RuntimeException.class); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail statusDetail = errorOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(statusDetail.getStatus(), is(Status.ERROR)); }
@Test public void shouldReturnOKStatus() { //given when(mongoDatabase.runCommand(new Document().append("ping", 1))).thenReturn(new Document().append("ok", 1.0d)); //when final StatusDetail statusDetail = testee.statusDetails().get(0); //then assertThat(statusDetail.getStatus(), is(OK)); }
@Test public void shouldIndicateWarningIfLastJobRunWasDead() { // given final List<JobInfo> jobInfos = singletonList(someStoppedJob(DEAD, 1)); when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(jobInfos); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail statusDetail = errorOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(statusDetail.getStatus(), is(Status.WARNING)); assertThat(statusDetail.getMessage(), is("Job died")); }
@Test public void shouldReturnErrorStatusWhenDatabaseDoesntReturnOKForPing() { //given when(mongoDatabase.runCommand(new Document().append("ping", 1))).thenReturn(new Document().append("error", 1.0d)); //when final StatusDetail statusDetail = testee.statusDetails().get(0); //then assertThat(statusDetail.getStatus(), is(ERROR)); assertThat(statusDetail.getMessage(), containsString("Mongo database unreachable or ping command failed.")); }
@Test public void shouldReturnErrorStatusWhenDatabaseTimesOut() { //given when(mongoDatabase.runCommand(new Document().append("ping", 1))).thenThrow(new MongoTimeoutException("Timeout")); //when final StatusDetail statusDetail = testee.statusDetails().get(0); //then assertThat(statusDetail.getStatus(), is(ERROR)); assertThat(statusDetail.getMessage(), containsString("Mongo database check ran into timeout")); }
@Test public void shouldReturnErrorStatusOnAnyException() { //given when(mongoDatabase.runCommand(new Document().append("ping", 1))).thenThrow(new MongoException("SomeException")); //when final StatusDetail statusDetail = testee.statusDetails().get(0); //then assertThat(statusDetail.getStatus(), is(ERROR)); assertThat(statusDetail.getMessage(), containsString("Exception during database check")); } }
@Test public void shouldRemoveDetail() { // given StatusDetail statusDetail = statusDetail("foo", WARNING, "message", singletonMap("foo", "bar")); // when statusDetail = statusDetail.withoutDetail("foo"); // then assertThat(statusDetail.getName(), is("foo")); assertThat(statusDetail.getMessage(), is("message")); assertThat(statusDetail.getStatus(), is(WARNING)); assertThat(statusDetail.getDetails(), not(hasEntry("foo", "bar"))); }
@Test public void shouldIndicateInitialStatus() { // given final MutableStatusDetailIndicator indicator = new MutableStatusDetailIndicator(statusDetail("foo", OK, "message")); // then final StatusDetail statusDetail = indicator.statusDetails().get(0); assertThat(statusDetail.getName(), is("foo")); assertThat(statusDetail.getStatus(), is(OK)); assertThat(statusDetail.getMessage(), is("message")); }
@Test public void shouldResultInOk() { // given StatusDetail statusDetail = statusDetail("foo", WARNING, "message", singletonMap("foo", "bar")); // when statusDetail = statusDetail.toOk("different message"); // then assertThat(statusDetail.getName(), is("foo")); assertThat(statusDetail.getMessage(), is("different message")); assertThat(statusDetail.getStatus(), is(OK)); assertThat(statusDetail.getDetails(), hasEntry("foo", "bar")); }
@Test public void shouldResultInWarning() { // given StatusDetail statusDetail = statusDetail("foo", OK, "message", singletonMap("foo", "bar")); // when statusDetail = statusDetail.toWarning("different message"); // then assertThat(statusDetail.getName(), is("foo")); assertThat(statusDetail.getMessage(), is("different message")); assertThat(statusDetail.getStatus(), is(WARNING)); assertThat(statusDetail.getDetails(), hasEntry("foo", "bar")); }
@Test public void shouldOverwriteDetail() { // given StatusDetail statusDetail = statusDetail("foo", WARNING, "message", singletonMap("foo", "bar")); // when statusDetail = statusDetail.withDetail("foo","baz"); // then assertThat(statusDetail.getName(), is("foo")); assertThat(statusDetail.getMessage(), is("message")); assertThat(statusDetail.getStatus(), is(WARNING)); assertThat(statusDetail.getDetails(), hasEntry("foo", "baz")); }
@Test public void shouldResultInError() { // given StatusDetail statusDetail = statusDetail("foo", WARNING, "message", singletonMap("foo", "bar")); // when statusDetail = statusDetail.toError("different message"); // then assertThat(statusDetail.getName(), is("foo")); assertThat(statusDetail.getMessage(), is("different message")); assertThat(statusDetail.getStatus(), is(ERROR)); assertThat(statusDetail.getDetails(), hasEntry("foo", "bar")); }
@Test public void shouldIndicateOkStatus() { // given final StatusDetail initial = statusDetail("foo", ERROR, "message"); final MutableStatusDetailIndicator indicator = new MutableStatusDetailIndicator(initial); // when indicator.toOk("ok now"); // then final StatusDetail statusDetail = indicator.statusDetails().get(0); assertThat(statusDetail.getName(), is("foo")); assertThat(statusDetail.getMessage(), is("ok now")); assertThat(statusDetail.getStatus(), is(OK)); }
@Test public void shouldDeleteAdditionalDetail() { // given final StatusDetail initial = statusDetail("foo", WARNING, "message", singletonMap("foo", "baz")); final MutableStatusDetailIndicator indicator = new MutableStatusDetailIndicator(initial); // when indicator.withoutDetail("bar"); // then final StatusDetail statusDetail = indicator.statusDetails().get(0); assertThat(statusDetail.getName(), is("foo")); assertThat(statusDetail.getMessage(), is("message")); assertThat(statusDetail.getStatus(), is(WARNING)); assertThat(statusDetail.getDetails(), not(hasEntry("bar", "baz"))); }
@Test public void shouldIndicateWarnStatus() { // given final StatusDetail initial = statusDetail("foo", ERROR, "message"); final MutableStatusDetailIndicator indicator = new MutableStatusDetailIndicator(initial); // when indicator.toWarning("something strange"); // then final StatusDetail statusDetail = indicator.statusDetails().get(0); assertThat(statusDetail.getName(), is("foo")); assertThat(statusDetail.getMessage(), is("something strange")); assertThat(statusDetail.getStatus(), is(WARNING)); }
@Test public void shouldIndicateErrorStatus() { // given final StatusDetail initial = statusDetail("foo", WARNING, "message"); final MutableStatusDetailIndicator indicator = new MutableStatusDetailIndicator(initial); // when indicator.toError("broken"); // then final StatusDetail statusDetail = indicator.statusDetails().get(0); assertThat(statusDetail.getName(), is("foo")); assertThat(statusDetail.getMessage(), is("broken")); assertThat(statusDetail.getStatus(), is(ERROR)); }
@Test public void shouldAddDetail() { // given StatusDetail statusDetail = statusDetail("foo", WARNING, "message", singletonMap("foo", "bar")); // when statusDetail = statusDetail.withDetail("bar", "baz"); // then assertThat(statusDetail.getName(), is("foo")); assertThat(statusDetail.getMessage(), is("message")); assertThat(statusDetail.getStatus(), is(WARNING)); assertThat(statusDetail.getDetails(), hasEntry("foo", "bar")); assertThat(statusDetail.getDetails(), hasEntry("bar", "baz")); }