<commit_before>package de.oneos.cqrs.eventstore

import domain.events.EventEnvelope
import oneos.test.domain.aggregates.Device
import oneos.test.domain.events.Device_was_registered
import org.junit.Test

import static java.util.UUID.randomUUID
import static org.hamcrest.CoreMatchers.equalTo
import static org.junit.Assert.assertThat
import domain.events.Event

abstract class EventStore_ContractTest {
    static final String APPLICATION_NAME = 'CQRS Core Library'
    static final String BOUNDED_CONTEXT_NAME = 'Tests'
    static final String AGGREGATE_NAME = 'Device'

    EventStore eventStore

    UUID aggregateId = randomUUID()
    EventEnvelope eventEnvelope = new EventEnvelope<Device>(APPLICATION_NAME, BOUNDED_CONTEXT_NAME, AGGREGATE_NAME, aggregateId, aBusinessEvent())


    @Test
    void should_insert_new_event() throws Exception {
        eventStore.save(eventEnvelope)

        List<Event> history = eventStore.getEventsFor(
            eventEnvelope.applicationName,
            eventEnvelope.boundedContextName,
            eventEnvelope.aggregateName,
            eventEnvelope.aggregateId,
            'oneos.test.domain.events.'
        )

        assertThat history, equalTo([
            eventEnvelope.event
        ])
    }

    private aBusinessEvent() {
        new Device_was_registered(deviceName: "Device1")
    }

    @Test(expected = StaleStateException)
    void should_prevent_race_conditions() {
        createBusinessAggregate(aggregateId)

        BusinessAggregate instance1 = getBusinessAggregate(aggregateId)
        BusinessAggregate instance2 = getBusinessAggregate(aggregateId)

        instance1.callBusinessMethod()
        instance1.flush()

        instance2.callAnotherBusinessMethod()
        instance2.flush()
    }

    private createBusinessAggregate(UUID aggregateId) {
        eventStore.save(new EventEnvelope(
            APPLICATION_NAME,
            BOUNDED_CONTEXT_NAME,
            AGGREGATE_NAME,
            aggregateId,
            new BusinessAggregate_was_created()
        ))
    }

    private getBusinessAggregate(UUID aggregateId) {
        eventStore.getAggregate(
            APPLICATION_NAME,
            BOUNDED_CONTEXT_NAME,
            AGGREGATE_NAME,
            BusinessAggregate,
            aggregateId,
            'de.oneos.cqrs.eventstore.'
        )
    }

            APPLICATION_NAME,
            BOUNDED_CONTEXT_NAME,
            AGGREGATE_NAME,
            BusinessAggregate,
            aggregateId,
            'de.oneos.cqrs.eventstore.'
        )

        instance1.callBusinessMethod()
        instance1.flush()

        instance2.callAnotherBusinessMethod()
        instance2.flush()
    }

    static class BusinessAggregate {

        void callBusinessMethod() {
            publishEvent(new A_business_method_was_called())
        }

        void callAnotherBusinessMethod() {
            publishEvent(new Another_business_method_was_called())
        }
    }

    static class BusinessAggregate_was_created extends Event<BusinessAggregate> {
        BusinessAggregate applyTo(BusinessAggregate aggregate) { aggregate }
    }

    static class A_business_method_was_called extends Event<BusinessAggregate> {<commit_msg>Added EventStore contract test to allow consecutive events<commit_after>package de.oneos.cqrs.eventstore

import domain.events.EventEnvelope
import oneos.test.domain.aggregates.Device
import oneos.test.domain.events.Device_was_registered
import org.junit.Test

import static java.util.UUID.randomUUID
import static org.hamcrest.CoreMatchers.equalTo
import static org.junit.Assert.assertThat
import domain.events.Event

abstract class EventStore_ContractTest {
    static final String APPLICATION_NAME = 'CQRS Core Library'
    static final String BOUNDED_CONTEXT_NAME = 'Tests'
    static final String AGGREGATE_NAME = 'Device'

    EventStore eventStore

    UUID aggregateId = randomUUID()
    EventEnvelope eventEnvelope = new EventEnvelope<Device>(APPLICATION_NAME, BOUNDED_CONTEXT_NAME, AGGREGATE_NAME, aggregateId, aBusinessEvent())


    @Test
    void should_insert_new_event() throws Exception {
        eventStore.save(eventEnvelope)

        List<Event> history = eventStore.getEventsFor(
            eventEnvelope.applicationName,
            eventEnvelope.boundedContextName,
            eventEnvelope.aggregateName,
            eventEnvelope.aggregateId,
            'oneos.test.domain.events.'
        )

        assertThat history, equalTo([
            eventEnvelope.event
        ])
    }

    private aBusinessEvent() {
        new Device_was_registered(deviceName: "Device1")
    }

    @Test(expected = StaleStateException)
    void should_prevent_race_conditions() {
        createBusinessAggregate(aggregateId)

        BusinessAggregate instance1 = getBusinessAggregate(aggregateId)
        BusinessAggregate instance2 = getBusinessAggregate(aggregateId)

        instance1.callBusinessMethod()
        instance1.flush()

        instance2.callAnotherBusinessMethod()
        instance2.flush()
    }

    private createBusinessAggregate(UUID aggregateId) {
        eventStore.save(new EventEnvelope(
            APPLICATION_NAME,
            BOUNDED_CONTEXT_NAME,
            AGGREGATE_NAME,
            aggregateId,
            new BusinessAggregate_was_created()
        ))
    }

    private getBusinessAggregate(UUID aggregateId) {
        eventStore.getAggregate(
            APPLICATION_NAME,
            BOUNDED_CONTEXT_NAME,
            AGGREGATE_NAME,
            BusinessAggregate,
            aggregateId,
            'de.oneos.cqrs.eventstore.'
        )
    }

    @Test
    void should_insert_consecutive_events() {
        createBusinessAggregate(aggregateId)

        BusinessAggregate instance1 = getBusinessAggregate(aggregateId)
        instance1.callBusinessMethod()
        instance1.flush()

        BusinessAggregate instance2 = getBusinessAggregate(aggregateId)
        instance2.callAnotherBusinessMethod()
        instance2.flush()
    }

    static class BusinessAggregate {

        void callBusinessMethod() {
            publishEvent(new A_business_method_was_called())
        }

        void callAnotherBusinessMethod() {
            publishEvent(new Another_business_method_was_called())
        }
    }

    static class BusinessAggregate_was_created extends Event<BusinessAggregate> {
        BusinessAggregate applyTo(BusinessAggregate aggregate) { aggregate }
    }

    static class A_business_method_was_called extends Event<BusinessAggregate> {
{'fraction_non_alphanumeric': 0.06468424279583078, 'fraction_numerical': 0.003678724708767627, 'mean_word_length': 2.8113317757009346, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7393304', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1776, 'n_words': 307}