Search in sources :

Example 1 with InstanceStatusChangedEvent

use of de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent in project spring-boot-admin by codecentric.

the class OpsGenieNotifier method createRequest.

protected HttpEntity<?> createRequest(InstanceEvent event, Instance instance) {
    Map<String, Object> body = new HashMap<>();
    if (user != null) {
        body.put("user", user);
    }
    if (source != null) {
        body.put("source", source);
    }
    if (event instanceof InstanceStatusChangedEvent && !StatusInfo.STATUS_UP.equals(((InstanceStatusChangedEvent) event).getStatusInfo().getStatus())) {
        body.put("message", getMessage(event, instance));
        body.put("alias", generateAlias(instance));
        body.put("description", getDescription(event, instance));
        if (actions != null) {
            body.put("actions", actions);
        }
        if (tags != null) {
            body.put("tags", tags);
        }
        if (entity != null) {
            body.put("entity", entity);
        }
        Map<String, Object> details = new HashMap<>();
        details.put("type", "link");
        details.put("href", instance.getRegistration().getHealthUrl());
        details.put("text", "Instance health-endpoint");
        body.put("details", details);
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set(HttpHeaders.AUTHORIZATION, "GenieKey " + apiKey);
    return new HttpEntity<>(body, headers);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap) InstanceStatusChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent)

Example 2 with InstanceStatusChangedEvent

use of de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent in project spring-boot-admin by codecentric.

the class StatusUpdaterTest method should_change_status_to_down.

@Test
public void should_change_status_to_down() {
    String body = "{ \"status\" : \"UP\", \"details\" : { \"foo\" : \"bar\" } }";
    this.wireMock.stubFor(get("/health").willReturn(okForContentType(ActuatorMediaType.V2_JSON, body).withHeader("Content-Length", Integer.toString(body.length()))));
    StepVerifier.create(this.eventStore).expectSubscription().then(() -> StepVerifier.create(this.updater.updateStatus(this.instance.getId())).verifyComplete()).assertNext((event) -> {
        assertThat(event).isInstanceOf(InstanceStatusChangedEvent.class);
        assertThat(event.getInstance()).isEqualTo(this.instance.getId());
        InstanceStatusChangedEvent statusChangedEvent = (InstanceStatusChangedEvent) event;
        assertThat(statusChangedEvent.getStatusInfo().getStatus()).isEqualTo("UP");
        assertThat(statusChangedEvent.getStatusInfo().getDetails()).isEqualTo(singletonMap("foo", "bar"));
    }).thenCancel().verify();
    StepVerifier.create(this.repository.find(this.instance.getId())).assertNext((app) -> assertThat(app.getStatusInfo().getStatus()).isEqualTo("UP")).verifyComplete();
    StepVerifier.create(this.repository.computeIfPresent(this.instance.getId(), (key, instance) -> Mono.just(instance.deregister()))).then(() -> StepVerifier.create(this.updater.updateStatus(this.instance.getId())).verifyComplete()).thenCancel().verify();
    StepVerifier.create(this.repository.find(this.instance.getId())).assertNext((app) -> assertThat(app.getStatusInfo().getStatus()).isEqualTo("UNKNOWN")).verifyComplete();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ActuatorMediaType(org.springframework.boot.actuate.endpoint.http.ActuatorMediaType) StepVerifier(reactor.test.StepVerifier) InstanceExchangeFilterFunctions.timeout(de.codecentric.boot.admin.server.web.client.InstanceExchangeFilterFunctions.timeout) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Endpoint(de.codecentric.boot.admin.server.domain.values.Endpoint) WireMock.okJson(com.github.tomakehurst.wiremock.client.WireMock.okJson) InstanceExchangeFilterFunctions.rewriteEndpointUrl(de.codecentric.boot.admin.server.web.client.InstanceExchangeFilterFunctions.rewriteEndpointUrl) WireMock.ok(com.github.tomakehurst.wiremock.client.WireMock.ok) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) AfterAll(org.junit.jupiter.api.AfterAll) InstanceExchangeFilterFunctions.retry(de.codecentric.boot.admin.server.web.client.InstanceExchangeFilterFunctions.retry) EventsourcingInstanceRepository(de.codecentric.boot.admin.server.domain.entities.EventsourcingInstanceRepository) Fault(com.github.tomakehurst.wiremock.http.Fault) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) ConcurrentMapEventStore(de.codecentric.boot.admin.server.eventstore.ConcurrentMapEventStore) Collections.singletonMap(java.util.Collections.singletonMap) InstanceWebClient(de.codecentric.boot.admin.server.web.client.InstanceWebClient) InMemoryEventStore(de.codecentric.boot.admin.server.eventstore.InMemoryEventStore) InstanceRepository(de.codecentric.boot.admin.server.domain.entities.InstanceRepository) WireMock.okForContentType(com.github.tomakehurst.wiremock.client.WireMock.okForContentType) Registration(de.codecentric.boot.admin.server.domain.values.Registration) WireMock.get(com.github.tomakehurst.wiremock.client.WireMock.get) Collections.emptyMap(java.util.Collections.emptyMap) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) MediaType(org.springframework.http.MediaType) InstanceId(de.codecentric.boot.admin.server.domain.values.InstanceId) WireMock.status(com.github.tomakehurst.wiremock.client.WireMock.status) Mono(reactor.core.publisher.Mono) STARTED(com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED) Options(com.github.tomakehurst.wiremock.core.Options) Test(org.junit.jupiter.api.Test) Instance(de.codecentric.boot.admin.server.domain.entities.Instance) AfterEach(org.junit.jupiter.api.AfterEach) InstanceStatusChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent) InstanceStatusChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent) Test(org.junit.jupiter.api.Test)

Example 3 with InstanceStatusChangedEvent

use of de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent in project spring-boot-admin by codecentric.

the class InfoUpdateTriggerTest method should_start_and_stop_monitor.

@Test
public void should_start_and_stop_monitor() throws Exception {
    // given
    this.trigger.stop();
    this.trigger.setInterval(Duration.ofMillis(10));
    this.trigger.setLifetime(Duration.ofMillis(10));
    this.trigger.start();
    await().until(this.events::wasSubscribed);
    this.events.next(new InstanceStatusChangedEvent(this.instance.getId(), this.instance.getVersion(), StatusInfo.ofDown()));
    Thread.sleep(50L);
    // then it should start updating one time for registration and at least once for
    // monitor
    verify(this.updater, atLeast(2)).updateInfo(this.instance.getId());
    // given long lifetime
    this.trigger.setLifetime(Duration.ofSeconds(10));
    Thread.sleep(50L);
    clearInvocations(this.updater);
    // when the lifetime is not expired
    Thread.sleep(50L);
    // should never update
    verify(this.updater, never()).updateInfo(any(InstanceId.class));
    // when trigger ist destroyed
    this.trigger.setLifetime(Duration.ofMillis(10));
    this.trigger.stop();
    clearInvocations(this.updater);
    Thread.sleep(15L);
    // it should stop updating
    verify(this.updater, never()).updateInfo(any(InstanceId.class));
}
Also used : InstanceId(de.codecentric.boot.admin.server.domain.values.InstanceId) InstanceStatusChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent) Test(org.junit.jupiter.api.Test)

Example 4 with InstanceStatusChangedEvent

use of de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent in project spring-boot-admin by codecentric.

the class MicrosoftTeamsNotifierTest method test_getStatusChangedMessageForAppReturns_correctContent.

@Test
void test_getStatusChangedMessageForAppReturns_correctContent() {
    Message message = notifier.getStatusChangedMessage(instance, notifier.createEvaluationContext(new InstanceStatusChangedEvent(instance.getId(), 1L, StatusInfo.ofDown()), instance));
    assertMessage(message, notifier.getStatusChangedTitle(), notifier.getMessageSummary(), "Test App with id TestAppId changed status from UNKNOWN to DOWN", RED);
}
Also used : Message(de.codecentric.boot.admin.server.notify.MicrosoftTeamsNotifier.Message) InstanceStatusChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent) Test(org.junit.jupiter.api.Test)

Example 5 with InstanceStatusChangedEvent

use of de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent in project spring-boot-admin by codecentric.

the class MicrosoftTeamsNotifierTest method test_onApplicationStatusChangedEvent_resolve.

@Test
@SuppressWarnings("unchecked")
void test_onApplicationStatusChangedEvent_resolve() {
    InstanceStatusChangedEvent event = new InstanceStatusChangedEvent(instance.getId(), 1L, StatusInfo.ofUp());
    StepVerifier.create(notifier.doNotify(event, instance)).verifyComplete();
    ArgumentCaptor<HttpEntity<Message>> entity = ArgumentCaptor.forClass(HttpEntity.class);
    verify(mockRestTemplate).postForEntity(eq(URI.create("http://example.com")), entity.capture(), eq(Void.class));
    assertThat(entity.getValue().getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
    assertMessage(entity.getValue().getBody(), notifier.getStatusChangedTitle(), notifier.getMessageSummary(), "Test App with id TestAppId changed status from UNKNOWN to UP", GREEN);
}
Also used : HttpEntity(org.springframework.http.HttpEntity) InstanceStatusChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent) Test(org.junit.jupiter.api.Test)

Aggregations

InstanceStatusChangedEvent (de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent)51 Test (org.junit.jupiter.api.Test)47 StatusInfo (de.codecentric.boot.admin.server.domain.values.StatusInfo)9 HttpEntity (org.springframework.http.HttpEntity)6 InstanceRegisteredEvent (de.codecentric.boot.admin.server.domain.events.InstanceRegisteredEvent)5 InstanceId (de.codecentric.boot.admin.server.domain.values.InstanceId)5 URI (java.net.URI)5 InstanceEvent (de.codecentric.boot.admin.server.domain.events.InstanceEvent)4 HashMap (java.util.HashMap)4 InstanceDeregisteredEvent (de.codecentric.boot.admin.server.domain.events.InstanceDeregisteredEvent)3 Message (de.codecentric.boot.admin.server.notify.MicrosoftTeamsNotifier.Message)3 Instant (java.time.Instant)3 JSONObject (org.json.JSONObject)3 Instance (de.codecentric.boot.admin.server.domain.entities.Instance)2 Registration (de.codecentric.boot.admin.server.domain.values.Registration)2 MimeMessage (javax.mail.internet.MimeMessage)2 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)1 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)1 WireMock.get (com.github.tomakehurst.wiremock.client.WireMock.get)1 WireMock.ok (com.github.tomakehurst.wiremock.client.WireMock.ok)1