Commit 99d97b79 by Johannes Edmeier

fix deprecations

parent 74c1089d
......@@ -15,9 +15,18 @@
*/
package de.codecentric.boot.admin.config;
import de.codecentric.boot.admin.event.ClientApplicationDeregisteredEvent;
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
import de.codecentric.boot.admin.event.RoutesOutdatedEvent;
import de.codecentric.boot.admin.journal.ApplicationEventJournal;
import de.codecentric.boot.admin.journal.web.JournalController;
import de.codecentric.boot.admin.registry.ApplicationRegistry;
import de.codecentric.boot.admin.registry.web.RegistryController;
import de.codecentric.boot.admin.web.AdminController;
import de.codecentric.boot.admin.web.PrefixHandlerMapping;
import java.util.List;
import java.util.Map;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
......@@ -28,23 +37,11 @@ import org.springframework.context.event.EventListener;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.codecentric.boot.admin.event.ClientApplicationDeregisteredEvent;
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
import de.codecentric.boot.admin.event.RoutesOutdatedEvent;
import de.codecentric.boot.admin.journal.ApplicationEventJournal;
import de.codecentric.boot.admin.journal.web.JournalController;
import de.codecentric.boot.admin.registry.ApplicationRegistry;
import de.codecentric.boot.admin.registry.web.RegistryController;
import de.codecentric.boot.admin.web.AdminController;
import de.codecentric.boot.admin.web.PrefixHandlerMapping;
@Configuration
public class AdminServerWebConfiguration extends WebMvcConfigurerAdapter
implements ApplicationContextAware {
public class AdminServerWebConfiguration implements WebMvcConfigurer, ApplicationContextAware {
private final ApplicationEventPublisher publisher;
private final AdminServerProperties adminServerProperties;
private ApplicationContext applicationContext;
......@@ -64,7 +61,8 @@ public class AdminServerWebConfiguration extends WebMvcConfigurerAdapter
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
if (!hasConverter(converters, MappingJackson2HttpMessageConverter.class)) {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
.applicationContext(this.applicationContext).build();
.applicationContext(this.applicationContext)
.build();
converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
}
}
......@@ -81,8 +79,7 @@ public class AdminServerWebConfiguration extends WebMvcConfigurerAdapter
@Bean
public PrefixHandlerMapping prefixHandlerMapping() {
Map<String, Object> beans = applicationContext
.getBeansWithAnnotation(AdminController.class);
Map<String, Object> beans = applicationContext.getBeansWithAnnotation(AdminController.class);
PrefixHandlerMapping prefixHandlerMapping = new PrefixHandlerMapping(
beans.values().toArray(new Object[beans.size()]));
prefixHandlerMapping.setPrefix(adminServerProperties.getContextPath());
......
......@@ -32,6 +32,7 @@ import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfigu
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
......@@ -126,7 +127,7 @@ public class AdminServerWebConfigurationTest {
applicationContext.register(AdminServerCoreConfiguration.class);
applicationContext.register(AdminServerWebConfiguration.class);
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
TestPropertyValues.of(environment).applyTo(applicationContext);
applicationContext.refresh();
this.context = applicationContext;
}
......
......@@ -31,7 +31,7 @@ import java.util.List;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
......@@ -108,7 +108,7 @@ public class NotifierConfigurationTest {
context.register(MailSenderAutoConfiguration.class);
context.register(NotifierConfiguration.class);
EnvironmentTestUtils.addEnvironment(context, environment);
TestPropertyValues.of(environment).applyTo(context);
context.refresh();
}
......
package de.codecentric.boot.admin.notify;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.model.StatusInfo;
import java.net.URI;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
......@@ -17,9 +13,12 @@ import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.model.StatusInfo;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author Jamie Brown
......@@ -44,24 +43,20 @@ public class HipchatNotifierTest {
StatusInfo infoDown = StatusInfo.ofDown();
StatusInfo infoUp = StatusInfo.ofUp();
@SuppressWarnings("unchecked")
ArgumentCaptor<HttpEntity<Map<String, Object>>> httpRequest = ArgumentCaptor
.forClass((Class<HttpEntity<Map<String, Object>>>) (Class<?>) HttpEntity.class);
@SuppressWarnings("unchecked") ArgumentCaptor<HttpEntity<Map<String, Object>>> httpRequest = ArgumentCaptor.forClass(
(Class<HttpEntity<Map<String, Object>>>) (Class<?>) HttpEntity.class);
when(restTemplate.postForEntity(isA(String.class), httpRequest.capture(), eq(Void.class)))
.thenReturn(ResponseEntity.ok((Void) null));
when(restTemplate.postForEntity(isA(String.class), httpRequest.capture(), eq(Void.class))).thenReturn(
ResponseEntity.ok((Void) null));
notifier.notify(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
infoDown, infoUp));
Application.create("App").withId("-id-").withHealthUrl("http://health").build(), infoDown, infoUp));
assertThat(httpRequest.getValue().getHeaders()).containsEntry("Content-Type",
asList("application/json"));
assertThat(httpRequest.getValue().getHeaders()).containsEntry("Content-Type", asList("application/json"));
Map<String, Object> body = httpRequest.getValue().getBody();
assertThat(body).containsEntry("color", "green");
assertThat(body).containsEntry("message",
"<strong>App</strong>/-id- is <strong>UP</strong>");
assertThat(body).containsEntry("message", "<strong>App</strong>/-id- is <strong>UP</strong>");
assertThat(body).containsEntry("notify", Boolean.TRUE);
assertThat(body).containsEntry("message_format", "html");
......@@ -72,24 +67,20 @@ public class HipchatNotifierTest {
StatusInfo infoDown = StatusInfo.ofDown();
StatusInfo infoUp = StatusInfo.ofUp();
@SuppressWarnings("unchecked")
ArgumentCaptor<HttpEntity<Map<String, Object>>> httpRequest = ArgumentCaptor
.forClass((Class<HttpEntity<Map<String, Object>>>) (Class<?>) HttpEntity.class);
@SuppressWarnings("unchecked") ArgumentCaptor<HttpEntity<Map<String, Object>>> httpRequest = ArgumentCaptor.forClass(
(Class<HttpEntity<Map<String, Object>>>) (Class<?>) HttpEntity.class);
when(restTemplate.postForEntity(isA(String.class), httpRequest.capture(), eq(Void.class)))
.thenReturn(ResponseEntity.ok((Void) null));
when(restTemplate.postForEntity(isA(String.class), httpRequest.capture(), eq(Void.class))).thenReturn(
ResponseEntity.ok((Void) null));
notifier.notify(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
infoUp, infoDown));
Application.create("App").withId("-id-").withHealthUrl("http://health").build(), infoUp, infoDown));
assertThat(httpRequest.getValue().getHeaders()).containsEntry("Content-Type",
asList("application/json"));
assertThat(httpRequest.getValue().getHeaders()).containsEntry("Content-Type", asList("application/json"));
Map<String, Object> body = httpRequest.getValue().getBody();
assertThat(body).containsEntry("color", "red");
assertThat(body).containsEntry("message",
"<strong>App</strong>/-id- is <strong>DOWN</strong>");
assertThat(body).containsEntry("message", "<strong>App</strong>/-id- is <strong>DOWN</strong>");
assertThat(body).containsEntry("notify", Boolean.TRUE);
assertThat(body).containsEntry("message_format", "html");
}
......
package de.codecentric.boot.admin.notify;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.model.StatusInfo;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpEntity;
......@@ -17,9 +16,9 @@ import org.springframework.http.MediaType;
import org.springframework.util.Base64Utils;
import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.model.StatusInfo;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
public class LetsChatNotifierTest {
private static final String room = "text_room";
......@@ -51,7 +50,8 @@ public class LetsChatNotifierTest {
notifier.notify(getEvent(infoDown, infoUp));
HttpEntity<?> expected = expectedMessage(standardMessage(infoUp.getStatus(), appName, id));
verify(restTemplate).exchange(eq(URI.create(String.format("%s/rooms/%s/messages", host, room))), eq(HttpMethod.POST), eq(expected), eq(Void.class));
verify(restTemplate).exchange(eq(URI.create(String.format("%s/rooms/%s/messages", host, room))),
eq(HttpMethod.POST), eq(expected), eq(Void.class));
}
@Test
......@@ -63,13 +63,13 @@ public class LetsChatNotifierTest {
notifier.notify(getEvent(infoDown, infoUp));
HttpEntity<?> expected = expectedMessage("TEST");
verify(restTemplate).exchange(eq(URI.create(String.format("%s/rooms/%s/messages", host, room))), eq(HttpMethod.POST), eq(expected), eq(Void.class));
verify(restTemplate).exchange(eq(URI.create(String.format("%s/rooms/%s/messages", host, room))),
eq(HttpMethod.POST), eq(expected), eq(Void.class));
}
private ClientApplicationStatusChangedEvent getEvent(StatusInfo infoDown, StatusInfo infoUp) {
return new ClientApplicationStatusChangedEvent(
Application.create(appName).withId(id).withHealthUrl("http://health").build(),
infoDown, infoUp);
Application.create(appName).withId(id).withHealthUrl("http://health").build(), infoDown, infoUp);
}
private HttpEntity<?> expectedMessage(String message) {
......
......@@ -15,20 +15,20 @@
*/
package de.codecentric.boot.admin.notify;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import de.codecentric.boot.admin.event.ClientApplicationEvent;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.model.StatusInfo;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import de.codecentric.boot.admin.event.ClientApplicationEvent;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.model.StatusInfo;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
public class MailNotifierTest {
......@@ -40,8 +40,8 @@ public class MailNotifierTest {
sender = mock(MailSender.class);
notifier = new MailNotifier(sender);
notifier.setTo(new String[] { "foo@bar.com" });
notifier.setCc(new String[] { "bar@foo.com" });
notifier.setTo(new String[]{"foo@bar.com"});
notifier.setCc(new String[]{"bar@foo.com"});
notifier.setFrom("SBA <no-reply@example.com>");
notifier.setSubject("#{application.id} is #{to.status}");
}
......@@ -49,12 +49,12 @@ public class MailNotifierTest {
@Test
public void test_onApplicationEvent() {
notifier.notify(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
StatusInfo.ofDown(), StatusInfo.ofUp()));
Application.create("App").withId("-id-").withHealthUrl("http://health").build(), StatusInfo.ofDown(),
StatusInfo.ofUp()));
SimpleMailMessage expected = new SimpleMailMessage();
expected.setTo(new String[] { "foo@bar.com" });
expected.setCc(new String[] { "bar@foo.com" });
expected.setTo(new String[]{"foo@bar.com"});
expected.setCc(new String[]{"bar@foo.com"});
expected.setFrom("SBA <no-reply@example.com>");
expected.setText("App (-id-)\nstatus changed from DOWN to UP\n\nhttp://health");
expected.setSubject("-id- is UP");
......@@ -68,8 +68,8 @@ public class MailNotifierTest {
public void test_onApplicationEvent_disbaled() {
notifier.setEnabled(false);
notifier.notify(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
StatusInfo.ofDown(), StatusInfo.ofUp()));
Application.create("App").withId("-id-").withHealthUrl("http://health").build(), StatusInfo.ofDown(),
StatusInfo.ofUp()));
verifyNoMoreInteractions(sender);
}
......@@ -77,18 +77,18 @@ public class MailNotifierTest {
@Test
public void test_onApplicationEvent_noSend() {
notifier.notify(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
StatusInfo.ofUnknown(), StatusInfo.ofUp()));
Application.create("App").withId("-id-").withHealthUrl("http://health").build(), StatusInfo.ofUnknown(),
StatusInfo.ofUp()));
verifyNoMoreInteractions(sender);
}
@Test
public void test_onApplicationEvent_noSend_wildcard() {
notifier.setIgnoreChanges(new String[] { "*:UP" });
notifier.setIgnoreChanges(new String[]{"*:UP"});
notifier.notify(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
StatusInfo.ofOffline(), StatusInfo.ofUp()));
Application.create("App").withId("-id-").withHealthUrl("http://health").build(), StatusInfo.ofOffline(),
StatusInfo.ofUp()));
verifyNoMoreInteractions(sender);
}
......@@ -102,7 +102,7 @@ public class MailNotifierTest {
}
};
notifier.notify(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
StatusInfo.ofOffline(), StatusInfo.ofUp()));
Application.create("App").withId("-id-").withHealthUrl("http://health").build(), StatusInfo.ofOffline(),
StatusInfo.ofUp()));
}
}
......@@ -15,8 +15,8 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
......
package de.codecentric.boot.admin.registry;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
import de.codecentric.boot.admin.model.Application;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import org.junit.Test;
import org.mockito.stubbing.Answer;
import org.springframework.boot.SpringApplication;
......@@ -18,20 +13,23 @@ import org.springframework.context.event.ContextClosedEvent;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.util.concurrent.SettableListenableFuture;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
import de.codecentric.boot.admin.model.Application;
import org.springframework.web.context.WebApplicationContext;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class StatusUpdateApplicationListenerTest {
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void test_start_stop() throws Exception {
StatusUpdater statusUpdater = mock(StatusUpdater.class);
ThreadPoolTaskScheduler scheduler = mock(ThreadPoolTaskScheduler.class);
StatusUpdateApplicationListener listener = new StatusUpdateApplicationListener(
statusUpdater, scheduler);
StatusUpdateApplicationListener listener = new StatusUpdateApplicationListener(statusUpdater, scheduler);
ScheduledFuture task = mock(ScheduledFuture.class);
when(scheduler.scheduleAtFixedRate(isA(Runnable.class), eq(10_000L))).thenReturn(task);
......@@ -55,11 +53,9 @@ public class StatusUpdateApplicationListenerTest {
return future;
});
StatusUpdateApplicationListener listener = new StatusUpdateApplicationListener(
statusUpdater, scheduler);
StatusUpdateApplicationListener listener = new StatusUpdateApplicationListener(statusUpdater, scheduler);
Application application = Application.create("test").withHealthUrl("http://example.com")
.build();
Application application = Application.create("test").withHealthUrl("http://example.com").build();
listener.onClientApplicationRegistered(new ClientApplicationRegisteredEvent(application));
......
......@@ -15,27 +15,29 @@
*/
package de.codecentric.boot.admin.registry;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.*;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.model.StatusInfo;
import de.codecentric.boot.admin.registry.store.SimpleApplicationStore;
import de.codecentric.boot.admin.web.client.ApplicationOperations;
import java.io.Serializable;
import java.util.Collections;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.ResourceAccessException;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.model.StatusInfo;
import de.codecentric.boot.admin.registry.store.SimpleApplicationStore;
import de.codecentric.boot.admin.web.client.ApplicationOperations;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class StatusUpdaterTest {
......@@ -55,53 +57,47 @@ public class StatusUpdaterTest {
@Test
public void test_update_statusChanged() {
when(applicationOps.getHealth(isA(Application.class))).thenReturn(ResponseEntity.ok()
.body(Collections.<String, Serializable>singletonMap("status", "UP")));
when(applicationOps.getInfo(isA(Application.class))).thenReturn(ResponseEntity.ok()
.body(Collections.<String, Serializable>singletonMap("foo", "bar")));
when(applicationOps.getHealth(isA(Application.class))).thenReturn(
ResponseEntity.ok().body(Collections.<String, Serializable>singletonMap("status", "UP")));
when(applicationOps.getInfo(isA(Application.class))).thenReturn(
ResponseEntity.ok().body(Collections.<String, Serializable>singletonMap("foo", "bar")));
updater.updateStatus(
Application.create("foo").withId("id").withHealthUrl("health").build());
updater.updateStatus(Application.create("foo").withId("id").withHealthUrl("health").build());
Application app = store.find("id");
assertThat(app.getStatusInfo().getStatus()).isEqualTo("UP");
assertThat(app.getInfo().getValues()).containsEntry("foo", "bar");
verify(publisher)
.publishEvent(isA(ClientApplicationStatusChangedEvent.class));
verify(publisher).publishEvent(isA(ClientApplicationStatusChangedEvent.class));
}
@Test
public void test_update_statusUnchanged() {
when(applicationOps.getHealth(any(Application.class))).thenReturn(ResponseEntity
.ok(Collections.<String, Serializable>singletonMap("status", "UNKNOWN")));
when(applicationOps.getHealth(any(Application.class))).thenReturn(
ResponseEntity.ok(Collections.<String, Serializable>singletonMap("status", "UNKNOWN")));
updater.updateStatus(
Application.create("foo").withId("id").withHealthUrl("health").build());
updater.updateStatus(Application.create("foo").withId("id").withHealthUrl("health").build());
verify(publisher, never())
.publishEvent(isA(ClientApplicationStatusChangedEvent.class));
verify(publisher, never()).publishEvent(isA(ClientApplicationStatusChangedEvent.class));
verify(applicationOps, never()).getInfo(isA(Application.class));
}
@Test
public void test_update_up_noBody() {
when(applicationOps.getHealth(any(Application.class)))
.thenReturn(ResponseEntity.ok((Map<String, Serializable>) null));
when(applicationOps.getHealth(any(Application.class))).thenReturn(
ResponseEntity.ok((Map<String, Serializable>) null));
updater.updateStatus(
Application.create("foo").withId("id").withHealthUrl("health").build());
updater.updateStatus(Application.create("foo").withId("id").withHealthUrl("health").build());
assertThat(store.find("id").getStatusInfo().getStatus()).isEqualTo("UP");
}
@Test
public void test_update_down() {
when(applicationOps.getHealth(any(Application.class))).thenReturn(ResponseEntity.status(503)
.body(Collections.<String, Serializable>singletonMap("foo", "bar")));
when(applicationOps.getHealth(any(Application.class))).thenReturn(
ResponseEntity.status(503).body(Collections.<String, Serializable>singletonMap("foo", "bar")));
updater.updateStatus(
Application.create("foo").withId("id").withHealthUrl("health").build());
updater.updateStatus(Application.create("foo").withId("id").withHealthUrl("health").build());
StatusInfo statusInfo = store.find("id").getStatusInfo();
assertThat(statusInfo.getStatus()).isEqualTo("DOWN");
......@@ -110,11 +106,10 @@ public class StatusUpdaterTest {
@Test
public void test_update_down_noBody() {
when(applicationOps.getHealth(any(Application.class)))
.thenReturn(ResponseEntity.status(503).body((Map<String, Serializable>) null));
when(applicationOps.getHealth(any(Application.class))).thenReturn(
ResponseEntity.status(503).body((Map<String, Serializable>) null));
updater.updateStatus(
Application.create("foo").withId("id").withHealthUrl("health").build());
updater.updateStatus(Application.create("foo").withId("id").withHealthUrl("health").build());
StatusInfo statusInfo = store.find("id").getStatusInfo();
assertThat(statusInfo.getStatus()).isEqualTo("DOWN");
......@@ -124,11 +119,13 @@ public class StatusUpdaterTest {
@Test
public void test_update_offline() {
when(applicationOps.getHealth(any(Application.class)))
.thenThrow(new ResourceAccessException("error"));
when(applicationOps.getHealth(any(Application.class))).thenThrow(new ResourceAccessException("error"));
Application app = Application.create("foo").withId("id").withHealthUrl("health")
.withStatusInfo(StatusInfo.ofUp()).build();
Application app = Application.create("foo")
.withId("id")
.withHealthUrl("health")
.withStatusInfo(StatusInfo.ofUp())
.build();
updater.updateStatus(app);
StatusInfo statusInfo = store.find("id").getStatusInfo();
......@@ -141,16 +138,13 @@ public class StatusUpdaterTest {
@Test
public void test_updateStatusForApplications() throws InterruptedException {
updater.setStatusLifetime(100L);
Application app1 = Application.create("foo").withId("id-1").withHealthUrl("health-1")
.build();
Application app1 = Application.create("foo").withId("id-1").withHealthUrl("health-1").build();
store.save(app1);
Thread.sleep(120L); // Let the StatusInfo of id-1 expire
Application app2 = Application.create("foo").withId("id-2").withHealthUrl("health-2")
.build();
Application app2 = Application.create("foo").withId("id-2").withHealthUrl("health-2").build();
store.save(app2);
when(applicationOps.getHealth(eq(app1)))
.thenReturn(ResponseEntity.ok((Map<String, Serializable>) null));
when(applicationOps.getHealth(eq(app1))).thenReturn(ResponseEntity.ok((Map<String, Serializable>) null));
updater.updateStatusForAllApplications();
......
package de.codecentric.boot.admin.web.client;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import de.codecentric.boot.admin.model.Application;
import java.io.Serializable;
import java.net.URI;
import java.util.Map;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.http.HttpEntity;
......@@ -19,7 +13,12 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.model.Application;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ApplicationOperationsTest {
......@@ -30,17 +29,18 @@ public class ApplicationOperationsTest {
@Test
@SuppressWarnings("rawtypes")
public void test_getInfo() {
Application app = Application.create("test").withHealthUrl("http://health")
.withManagementUrl("http://mgmt").build();
Application app = Application.create("test")
.withHealthUrl("http://health")
.withManagementUrl("http://mgmt")
.build();
ArgumentCaptor<HttpEntity> requestEntity = ArgumentCaptor.forClass(HttpEntity.class);
HttpHeaders headers = new HttpHeaders();
headers.add("auth", "foo:bar");
when(headersProvider.getHeaders(eq(app))).thenReturn(headers);
when(restTemplate.exchange(eq(URI.create("http://mgmt/info")), eq(HttpMethod.GET),
requestEntity.capture(), eq(Map.class)))
.thenReturn(ResponseEntity.ok().body((Map) singletonMap("foo", "bar")));
when(restTemplate.exchange(eq(URI.create("http://mgmt/info")), eq(HttpMethod.GET), requestEntity.capture(),
eq(Map.class))).thenReturn(ResponseEntity.ok().body((Map) singletonMap("foo", "bar")));
ResponseEntity<Map<String, Serializable>> response = ops.getInfo(app);
......@@ -51,17 +51,18 @@ public class ApplicationOperationsTest {
@Test
@SuppressWarnings("rawtypes")
public void test_getHealth() {
Application app = Application.create("test").withHealthUrl("http://health")
.withManagementUrl("http://mgmt").build();
Application app = Application.create("test")
.withHealthUrl("http://health")
.withManagementUrl("http://mgmt")
.build();
ArgumentCaptor<HttpEntity> requestEntity = ArgumentCaptor.forClass(HttpEntity.class);
HttpHeaders headers = new HttpHeaders();
headers.add("auth", "foo:bar");
when(headersProvider.getHeaders(eq(app))).thenReturn(headers);
when(restTemplate.exchange(eq(URI.create("http://health")), eq(HttpMethod.GET),
requestEntity.capture(), eq(Map.class)))
.thenReturn(ResponseEntity.ok().body((Map) singletonMap("foo", "bar")));
when(restTemplate.exchange(eq(URI.create("http://health")), eq(HttpMethod.GET), requestEntity.capture(),
eq(Map.class))).thenReturn(ResponseEntity.ok().body((Map) singletonMap("foo", "bar")));
ResponseEntity<Map<String, Serializable>> response = ops.getHealth(app);
......
......@@ -51,5 +51,4 @@ public class SpringBootAdminClientAutoConfigurationTest {
@EnableAutoConfiguration
static class TestClientApplication {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment