/** * Create a copy of this StatusDetail, updates the message, changes the status to OK, * and return the new StatusDetail. * * @param message the new message * @return StatusDetail */ public StatusDetail toOk(final String message) { return statusDetail(name, OK, message, details); }
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; }
public void toError(String message) { update(statusDetail.toError(message)); }
@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 shouldIndicateOkIfNoJobDefinitionsAvailable() { // given final JobStatusCalculator defaultCalculator = mock(JobStatusCalculator.class); when(defaultCalculator.getKey()).thenReturn("warningOnLastJobFailed"); final JobDefinitionService noJobDefinitions = mock(JobDefinitionService.class); when(noJobDefinitions.getJobDefinitions()).thenReturn(emptyList()); // when final StatusDetail statusDetail = testee.jobStatusDetailIndicator( noJobDefinitions, singletonList(defaultCalculator)).statusDetails() .get(0); // then assertThat(statusDetail.getStatus(), is(OK)); assertThat(statusDetail.getName(), is("Jobs")); assertThat(statusDetail.getMessage(), is("No job definitions configured in application.")); }
@Test public void shouldHaveAdditionalAttributes() { // given final StatusDetail statusDetail = statusDetail("foo", ERROR, "message", singletonMap("foo", "bar")); // when final Map<String,String> theMap = statusDetail.getDetails(); // then assertThat(theMap, hasEntry("foo", "bar")); }
@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 public void shouldNotHaveUriOrRunningIfNoJobPresent() { // given when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(emptyList()); 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.getLinks(), is(emptyList())); assertThat(statusDetail.getDetails(), not(hasKey("Running"))); }
public void update(final StatusDetail statusDetail) { if (!this.statusDetail.getName().equals(statusDetail.getName())) { throw new IllegalArgumentException("Must not update StatusDetail with different names. That would be confusing."); } this.statusDetail = requireNonNull(statusDetail, "Parameter StatusDetail must not be null"); }
@Test @SuppressWarnings("unchecked") public void shouldIndicateThatJobIsNotRunning() { // given final List<JobInfo> jobInfos = singletonList(someStoppedJob(OK, 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.getDetails(), not(hasKey("Running"))); assertThat(statusDetail.getDetails(), hasKey("Stopped")); }
public void withoutDetail(String key) { update(statusDetail.withoutDetail(key)); } }
public void toOk(String message) { update(statusDetail.toOk(message)); }
public void withDetail(String key, String value) { update(statusDetail.withDetail(key, value)); }
public void toWarning(String message) { update(statusDetail.toWarning(message)); }
@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 @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)); }
public void update(final StatusDetail statusDetail) { if (!this.statusDetail.getName().equals(statusDetail.getName())) { throw new IllegalArgumentException("Must not update StatusDetail with different names. That would be confusing."); } this.statusDetail = requireNonNull(statusDetail, "Parameter StatusDetail must not be null"); }
public void withoutDetail(String key) { update(statusDetail.withoutDetail(key)); } }
public void toOk(String message) { update(statusDetail.toOk(message)); }
public void withDetail(String key, String value) { update(statusDetail.withDetail(key, value)); }