Search in sources :

Example 6 with Info

use of de.codecentric.boot.admin.server.domain.values.Info in project spring-boot-admin by codecentric.

the class InstanceRegistryTest method refresh.

@Test
public void refresh() {
    // Given instance is already reegistered and has status and info.
    StatusInfo status = StatusInfo.ofUp();
    Info info = Info.from(singletonMap("foo", "bar"));
    Registration registration = Registration.create("abc", "http://localhost:8080/health").build();
    InstanceId id = idGenerator.generateId(registration);
    Instance app = Instance.create(id).register(registration).withStatusInfo(status).withInfo(info);
    StepVerifier.create(repository.save(app)).expectNextCount(1).verifyComplete();
    // When instance registers second time
    InstanceId refreshId = registry.register(Registration.create("abc", "http://localhost:8080/health").build()).block();
    assertThat(refreshId).isEqualTo(id);
    StepVerifier.create(registry.getInstance(id)).assertNext((registered) -> {
        // Then info and status are retained
        assertThat(registered.getInfo()).isEqualTo(info);
        assertThat(registered.getStatusInfo()).isEqualTo(status);
    }).verifyComplete();
}
Also used : Registration(de.codecentric.boot.admin.server.domain.values.Registration) BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) InstanceId(de.codecentric.boot.admin.server.domain.values.InstanceId) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Info(de.codecentric.boot.admin.server.domain.values.Info) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) Instance(de.codecentric.boot.admin.server.domain.entities.Instance) EventsourcingInstanceRepository(de.codecentric.boot.admin.server.domain.entities.EventsourcingInstanceRepository) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) Collections.singletonMap(java.util.Collections.singletonMap) InMemoryEventStore(de.codecentric.boot.admin.server.eventstore.InMemoryEventStore) InstanceRepository(de.codecentric.boot.admin.server.domain.entities.InstanceRepository) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) InstanceId(de.codecentric.boot.admin.server.domain.values.InstanceId) Instance(de.codecentric.boot.admin.server.domain.entities.Instance) Registration(de.codecentric.boot.admin.server.domain.values.Registration) Info(de.codecentric.boot.admin.server.domain.values.Info) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) Test(org.junit.jupiter.api.Test)

Example 7 with Info

use of de.codecentric.boot.admin.server.domain.values.Info in project spring-boot-admin by codecentric.

the class Instance method apply.

private Instance apply(InstanceEvent event, boolean isNewEvent) {
    Assert.notNull(event, "'event' must not be null");
    Assert.isTrue(this.id.equals(event.getInstance()), "'event' must refer the same instance");
    Assert.isTrue(event.getVersion() >= this.nextVersion(), () -> "Event " + event.getVersion() + " must be greater or equal to " + this.nextVersion());
    List<InstanceEvent> unsavedEvents = appendToEvents(event, isNewEvent);
    if (event instanceof InstanceRegisteredEvent) {
        Registration registration = ((InstanceRegisteredEvent) event).getRegistration();
        return new Instance(this.id, event.getVersion(), registration, true, StatusInfo.ofUnknown(), event.getTimestamp(), Info.empty(), Endpoints.empty(), updateBuildVersion(registration.getMetadata()), updateTags(registration.getMetadata()), unsavedEvents);
    } else if (event instanceof InstanceRegistrationUpdatedEvent) {
        Registration registration = ((InstanceRegistrationUpdatedEvent) event).getRegistration();
        return new Instance(this.id, event.getVersion(), registration, this.registered, this.statusInfo, this.statusTimestamp, this.info, this.endpoints, updateBuildVersion(registration.getMetadata(), this.info.getValues()), updateTags(registration.getMetadata(), this.info.getValues()), unsavedEvents);
    } else if (event instanceof InstanceStatusChangedEvent) {
        StatusInfo statusInfo = ((InstanceStatusChangedEvent) event).getStatusInfo();
        return new Instance(this.id, event.getVersion(), this.registration, this.registered, statusInfo, event.getTimestamp(), this.info, this.endpoints, this.buildVersion, this.tags, unsavedEvents);
    } else if (event instanceof InstanceEndpointsDetectedEvent) {
        Endpoints endpoints = ((InstanceEndpointsDetectedEvent) event).getEndpoints();
        return new Instance(this.id, event.getVersion(), this.registration, this.registered, this.statusInfo, this.statusTimestamp, this.info, endpoints, this.buildVersion, this.tags, unsavedEvents);
    } else if (event instanceof InstanceInfoChangedEvent) {
        Info info = ((InstanceInfoChangedEvent) event).getInfo();
        Map<String, ?> metaData = (this.registration != null) ? this.registration.getMetadata() : emptyMap();
        return new Instance(this.id, event.getVersion(), this.registration, this.registered, this.statusInfo, this.statusTimestamp, info, this.endpoints, updateBuildVersion(metaData, info.getValues()), updateTags(metaData, info.getValues()), unsavedEvents);
    } else if (event instanceof InstanceDeregisteredEvent) {
        return new Instance(this.id, event.getVersion(), this.registration, false, StatusInfo.ofUnknown(), event.getTimestamp(), Info.empty(), Endpoints.empty(), null, Tags.empty(), unsavedEvents);
    }
    return this;
}
Also used : InstanceRegisteredEvent(de.codecentric.boot.admin.server.domain.events.InstanceRegisteredEvent) InstanceInfoChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceInfoChangedEvent) InstanceEndpointsDetectedEvent(de.codecentric.boot.admin.server.domain.events.InstanceEndpointsDetectedEvent) Info(de.codecentric.boot.admin.server.domain.values.Info) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) InstanceRegistrationUpdatedEvent(de.codecentric.boot.admin.server.domain.events.InstanceRegistrationUpdatedEvent) InstanceEvent(de.codecentric.boot.admin.server.domain.events.InstanceEvent) InstanceStatusChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent) Endpoints(de.codecentric.boot.admin.server.domain.values.Endpoints) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) Registration(de.codecentric.boot.admin.server.domain.values.Registration) InstanceDeregisteredEvent(de.codecentric.boot.admin.server.domain.events.InstanceDeregisteredEvent)

Example 8 with Info

use of de.codecentric.boot.admin.server.domain.values.Info in project spring-boot-admin by codecentric.

the class InstanceTest method should_track_unsaved_events.

@Test
public void should_track_unsaved_events() {
    Registration registration = Registration.create("foo", "http://health").build();
    Info info = Info.from(singletonMap("foo", "bar"));
    Instance newInstance = Instance.create(InstanceId.of("id"));
    assertThat(newInstance.isRegistered()).isFalse();
    assertThatThrownBy(newInstance::getRegistration).isInstanceOf(IllegalStateException.class);
    assertThat(newInstance.getInfo()).isEqualTo(Info.empty());
    assertThat(newInstance.getStatusInfo()).isEqualTo(StatusInfo.ofUnknown());
    assertThat(newInstance.getUnsavedEvents()).isEmpty();
    Instance instance = newInstance.register(registration).register(registration);
    assertThat(instance.getRegistration()).isEqualTo(registration);
    assertThat(instance.isRegistered()).isTrue();
    assertThat(instance.getVersion()).isEqualTo(0L);
    Registration registration2 = Registration.create("foo2", "http://health").build();
    instance = instance.register(registration2);
    assertThat(instance.getRegistration()).isEqualTo(registration2);
    assertThat(instance.isRegistered()).isTrue();
    assertThat(instance.getVersion()).isEqualTo(1L);
    instance = instance.withStatusInfo(StatusInfo.ofUp()).withStatusInfo(StatusInfo.ofUp());
    assertThat(instance.getStatusInfo()).isEqualTo(StatusInfo.ofUp());
    assertThat(instance.getVersion()).isEqualTo(2L);
    instance = instance.withInfo(info).withInfo(info);
    assertThat(instance.getInfo()).isEqualTo(info);
    assertThat(instance.getVersion()).isEqualTo(3L);
    instance = instance.deregister().deregister();
    assertThat(instance.isRegistered()).isFalse();
    assertThat(instance.getRegistration()).isEqualTo(registration2);
    assertThat(instance.getInfo()).isEqualTo(Info.empty());
    assertThat(instance.getStatusInfo()).isEqualTo(StatusInfo.ofUnknown());
    assertThat(instance.getVersion()).isEqualTo(4L);
    assertThat(instance.getUnsavedEvents().stream().map(InstanceEvent::getType)).containsExactly("REGISTERED", "REGISTRATION_UPDATED", "STATUS_CHANGED", "INFO_CHANGED", "DEREGISTERED");
}
Also used : Registration(de.codecentric.boot.admin.server.domain.values.Registration) Info(de.codecentric.boot.admin.server.domain.values.Info) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) InstanceEvent(de.codecentric.boot.admin.server.domain.events.InstanceEvent) Test(org.junit.jupiter.api.Test)

Example 9 with Info

use of de.codecentric.boot.admin.server.domain.values.Info in project spring-boot-admin by codecentric.

the class InfoMixinTest method verifyDeserialize.

@Test
public void verifyDeserialize() throws JSONException, JsonProcessingException {
    String json = new JSONObject().put("build", new JSONObject().put("version", "1.0.0")).put("foo", "bar").toString();
    Info info = objectMapper.readValue(json, Info.class);
    assertThat(info).isNotNull();
    assertThat(info.getValues()).containsOnly(entry("build", Collections.singletonMap("version", "1.0.0")), entry("foo", "bar"));
}
Also used : JSONObject(org.json.JSONObject) Info(de.codecentric.boot.admin.server.domain.values.Info) Test(org.junit.jupiter.api.Test)

Example 10 with Info

use of de.codecentric.boot.admin.server.domain.values.Info in project spring-boot-admin by codecentric.

the class InfoUpdaterTest method should_retry.

@Test
public void should_retry() {
    // given
    Registration registration = Registration.create("foo", this.wireMock.url("/health")).build();
    Instance instance = Instance.create(InstanceId.of("onl")).register(registration).withEndpoints(Endpoints.single("info", this.wireMock.url("/info"))).withStatusInfo(StatusInfo.ofUp());
    StepVerifier.create(this.repository.save(instance)).expectNextCount(1).verifyComplete();
    this.wireMock.stubFor(get("/info").inScenario("retry").whenScenarioStateIs(STARTED).willReturn(aResponse().withFixedDelay(5000)).willSetStateTo("recovered"));
    String body = "{ \"foo\": \"bar\" }";
    this.wireMock.stubFor(get("/info").inScenario("retry").whenScenarioStateIs("recovered").willReturn(okJson(body).withHeader("Content-Length", Integer.toString(body.length()))));
    // when
    StepVerifier.create(this.eventStore).expectSubscription().then(() -> StepVerifier.create(this.updater.updateInfo(instance.getId())).verifyComplete()).assertNext((event) -> assertThat(event).isInstanceOf(InstanceInfoChangedEvent.class)).thenCancel().verify();
    StepVerifier.create(this.repository.find(instance.getId())).assertNext((app) -> assertThat(app.getInfo()).isEqualTo(Info.from(singletonMap("foo", "bar")))).verifyComplete();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Endpoints(de.codecentric.boot.admin.server.domain.values.Endpoints) StepVerifier(reactor.test.StepVerifier) InstanceExchangeFilterFunctions.timeout(de.codecentric.boot.admin.server.web.client.InstanceExchangeFilterFunctions.timeout) InstanceInfoChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceInfoChangedEvent) 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) Info(de.codecentric.boot.admin.server.domain.values.Info) InstanceExchangeFilterFunctions.rewriteEndpointUrl(de.codecentric.boot.admin.server.web.client.InstanceExchangeFilterFunctions.rewriteEndpointUrl) 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) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) WireMock.serverError(com.github.tomakehurst.wiremock.client.WireMock.serverError) 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) 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) InstanceId(de.codecentric.boot.admin.server.domain.values.InstanceId) 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) Instance(de.codecentric.boot.admin.server.domain.entities.Instance) Registration(de.codecentric.boot.admin.server.domain.values.Registration) InstanceInfoChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceInfoChangedEvent) Test(org.junit.jupiter.api.Test)

Aggregations

Info (de.codecentric.boot.admin.server.domain.values.Info)11 Test (org.junit.jupiter.api.Test)10 InstanceInfoChangedEvent (de.codecentric.boot.admin.server.domain.events.InstanceInfoChangedEvent)7 Registration (de.codecentric.boot.admin.server.domain.values.Registration)7 StatusInfo (de.codecentric.boot.admin.server.domain.values.StatusInfo)7 EventsourcingInstanceRepository (de.codecentric.boot.admin.server.domain.entities.EventsourcingInstanceRepository)5 Instance (de.codecentric.boot.admin.server.domain.entities.Instance)5 InstanceRepository (de.codecentric.boot.admin.server.domain.entities.InstanceRepository)5 Endpoints (de.codecentric.boot.admin.server.domain.values.Endpoints)5 InstanceId (de.codecentric.boot.admin.server.domain.values.InstanceId)5 InMemoryEventStore (de.codecentric.boot.admin.server.eventstore.InMemoryEventStore)5 Collections.singletonMap (java.util.Collections.singletonMap)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 StepVerifier (reactor.test.StepVerifier)5 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)4 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)4 WireMock.get (com.github.tomakehurst.wiremock.client.WireMock.get)4 WireMock.okJson (com.github.tomakehurst.wiremock.client.WireMock.okJson)4 WireMock.serverError (com.github.tomakehurst.wiremock.client.WireMock.serverError)4