Search in sources :

Example 1 with InstanceId

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

the class ConcurrentMapEventStore method doAppend.

protected boolean doAppend(List<InstanceEvent> events) {
    if (events.isEmpty()) {
        return true;
    }
    InstanceId id = events.get(0).getInstance();
    if (!events.stream().allMatch((event) -> event.getInstance().equals(id))) {
        throw new IllegalArgumentException("'events' must only refer to the same instance.");
    }
    List<InstanceEvent> oldEvents = eventLog.computeIfAbsent(id, (key) -> new ArrayList<>(maxLogSizePerAggregate + 1));
    long lastVersion = getLastVersion(oldEvents);
    if (lastVersion >= events.get(0).getVersion()) {
        throw createOptimisticLockException(events.get(0), lastVersion);
    }
    List<InstanceEvent> newEvents = new ArrayList<>(oldEvents);
    newEvents.addAll(events);
    if (newEvents.size() > maxLogSizePerAggregate) {
        log.debug("Threshold for {} reached. Compacting events", id);
        compact(newEvents);
    }
    if (eventLog.replace(id, oldEvents, newEvents)) {
        log.debug("Events appended to log {}", events);
        return true;
    }
    log.debug("Unsuccessful attempt append the events {} ", events);
    return false;
}
Also used : Logger(org.slf4j.Logger) InstanceId(de.codecentric.boot.admin.server.domain.values.InstanceId) LoggerFactory(org.slf4j.LoggerFactory) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Mono(reactor.core.publisher.Mono) Collectors.reducing(java.util.stream.Collectors.reducing) Function(java.util.function.Function) BinaryOperator(java.util.function.BinaryOperator) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Objects(java.util.Objects) Flux(reactor.core.publisher.Flux) List(java.util.List) Map(java.util.Map) Optional(java.util.Optional) Comparator.comparing(java.util.Comparator.comparing) Comparator(java.util.Comparator) Collections(java.util.Collections) InstanceEvent(de.codecentric.boot.admin.server.domain.events.InstanceEvent) InstanceId(de.codecentric.boot.admin.server.domain.values.InstanceId) ArrayList(java.util.ArrayList) InstanceEvent(de.codecentric.boot.admin.server.domain.events.InstanceEvent)

Example 2 with InstanceId

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

the class InstanceEndpointsDetectedEventMixinTest method verifySerializeWithEmptyEndpoints.

@Test
public void verifySerializeWithEmptyEndpoints() throws IOException {
    InstanceId id = InstanceId.of("test123");
    Instant timestamp = Instant.ofEpochSecond(1587751031).truncatedTo(ChronoUnit.SECONDS);
    InstanceEndpointsDetectedEvent event = new InstanceEndpointsDetectedEvent(id, 0L, timestamp, Endpoints.empty());
    JsonContent<InstanceEndpointsDetectedEvent> jsonContent = jsonTester.write(event);
    assertThat(jsonContent).extractingJsonPathStringValue("$.instance").isEqualTo("test123");
    assertThat(jsonContent).extractingJsonPathNumberValue("$.version").isEqualTo(0);
    assertThat(jsonContent).extractingJsonPathNumberValue("$.timestamp").isEqualTo(1587751031.000000000);
    assertThat(jsonContent).extractingJsonPathStringValue("$.type").isEqualTo("ENDPOINTS_DETECTED");
    assertThat(jsonContent).extractingJsonPathArrayValue("$.endpoints").isEmpty();
}
Also used : InstanceId(de.codecentric.boot.admin.server.domain.values.InstanceId) Instant(java.time.Instant) InstanceEndpointsDetectedEvent(de.codecentric.boot.admin.server.domain.events.InstanceEndpointsDetectedEvent) Test(org.junit.jupiter.api.Test)

Example 3 with InstanceId

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

the class InstanceIdMixinTest method verifyDeserialize.

@Test
public void verifyDeserialize() throws JSONException, JsonProcessingException {
    InstanceId id = objectMapper.readValue("\"abc\"", InstanceId.class);
    assertThat(id).isEqualTo(InstanceId.of("abc"));
}
Also used : InstanceId(de.codecentric.boot.admin.server.domain.values.InstanceId) Test(org.junit.jupiter.api.Test)

Example 4 with InstanceId

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

the class InstanceIdMixinTest method verifySerialize.

@Test
public void verifySerialize() throws IOException {
    InstanceId id = InstanceId.of("abc");
    String result = objectMapper.writeValueAsString(id);
    assertThat(result).isEqualTo("\"abc\"");
}
Also used : InstanceId(de.codecentric.boot.admin.server.domain.values.InstanceId) Test(org.junit.jupiter.api.Test)

Example 5 with InstanceId

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

the class InstanceInfoChangedEventMixinTest method verifySerialize.

@Test
public void verifySerialize() throws IOException {
    InstanceId id = InstanceId.of("test123");
    Instant timestamp = Instant.ofEpochSecond(1587751031).truncatedTo(ChronoUnit.SECONDS);
    Map<String, Object> data = new HashMap<>();
    data.put("build", Collections.singletonMap("version", "1.0.0"));
    data.put("foo", "bar");
    InstanceInfoChangedEvent event = new InstanceInfoChangedEvent(id, 12345678L, timestamp, Info.from(data));
    JsonContent<InstanceInfoChangedEvent> jsonContent = jsonTester.write(event);
    assertThat(jsonContent).extractingJsonPathStringValue("$.instance").isEqualTo("test123");
    assertThat(jsonContent).extractingJsonPathNumberValue("$.version").isEqualTo(12345678);
    assertThat(jsonContent).extractingJsonPathNumberValue("$.timestamp").isEqualTo(1587751031.000000000);
    assertThat(jsonContent).extractingJsonPathStringValue("$.type").isEqualTo("INFO_CHANGED");
    assertThat(jsonContent).extractingJsonPathMapValue("$.info").containsOnlyKeys("build", "foo");
    assertThat(jsonContent).extractingJsonPathStringValue("$.info['build'].['version']").isEqualTo("1.0.0");
    assertThat(jsonContent).extractingJsonPathStringValue("$.info['foo']").isEqualTo("bar");
}
Also used : InstanceId(de.codecentric.boot.admin.server.domain.values.InstanceId) HashMap(java.util.HashMap) Instant(java.time.Instant) JSONObject(org.json.JSONObject) InstanceInfoChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceInfoChangedEvent) Test(org.junit.jupiter.api.Test)

Aggregations

InstanceId (de.codecentric.boot.admin.server.domain.values.InstanceId)32 Test (org.junit.jupiter.api.Test)28 Instant (java.time.Instant)18 Registration (de.codecentric.boot.admin.server.domain.values.Registration)13 StatusInfo (de.codecentric.boot.admin.server.domain.values.StatusInfo)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 StepVerifier (reactor.test.StepVerifier)7 Instance (de.codecentric.boot.admin.server.domain.entities.Instance)6 ArrayList (java.util.ArrayList)6 Mono (reactor.core.publisher.Mono)6 InstanceRepository (de.codecentric.boot.admin.server.domain.entities.InstanceRepository)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Flux (reactor.core.publisher.Flux)5 EventsourcingInstanceRepository (de.codecentric.boot.admin.server.domain.entities.EventsourcingInstanceRepository)4 InstanceRegisteredEvent (de.codecentric.boot.admin.server.domain.events.InstanceRegisteredEvent)4 InstanceStatusChangedEvent (de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent)4 Info (de.codecentric.boot.admin.server.domain.values.Info)4 InMemoryEventStore (de.codecentric.boot.admin.server.eventstore.InMemoryEventStore)4 Collections.singletonMap (java.util.Collections.singletonMap)4 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)4