<commit_before>/*
 * Copyright (c) 2010-2018. Axon Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.axonframework.eventsourcing;

import org.axonframework.eventsourcing.utils.StubAggregate;
import org.axonframework.modelling.command.Aggregate;
import org.axonframework.modelling.command.inspection.AnnotatedAggregate;
import org.axonframework.modelling.command.inspection.AnnotatedAggregateMetaModelFactory;
import org.axonframework.eventhandling.GenericDomainEventMessage;
import org.axonframework.messaging.GenericMessage;
import org.axonframework.messaging.MetaData;
import org.axonframework.messaging.unitofwork.CurrentUnitOfWork;
import org.axonframework.messaging.unitofwork.DefaultUnitOfWork;
import org.axonframework.messaging.unitofwork.UnitOfWork;
import org.junit.jupiter.api.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import static org.mockito.Mockito.*;

/**
 * @author <NAME>
 */
class EventCountSnapshotTriggerDefinitionTest {

    private EventCountSnapshotTriggerDefinition testSubject;
    private Snapshotter mockSnapshotter;
    private String aggregateIdentifier;
    private Aggregate<?> aggregate;

    private UnitOfWork<?> unitOfWork;

    @BeforeEach
    void setUp() {
        while (CurrentUnitOfWork.isStarted()) {
            CurrentUnitOfWork.get().rollback();
        }
        mockSnapshotter = mock(Snapshotter.class);
        testSubject = new EventCountSnapshotTriggerDefinition(mockSnapshotter, 3);
        aggregateIdentifier = "aggregateIdentifier";
        unitOfWork = DefaultUnitOfWork.startAndGet(new GenericMessage<>("test"));
        aggregate = AnnotatedAggregate.initialize(new StubAggregate(aggregateIdentifier),
                                                  AnnotatedAggregateMetaModelFactory.inspectAggregate(StubAggregate.class), null);

    }

    @AfterEach
    void tearDown() {
        while (CurrentUnitOfWork.isStarted()) {
            CurrentUnitOfWork.get().rollback();
        }
    }

    @Test
    void testSnapshotterTriggeredOnUnitOfWorkCleanup() {
        SnapshotTrigger trigger = testSubject.prepareTrigger(aggregate.rootType());
        GenericDomainEventMessage<String> msg = new GenericDomainEventMessage<>("type", aggregateIdentifier, 0,
                                                                                "Mock contents", MetaData.emptyInstance());
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);

        verify(mockSnapshotter, never()).scheduleSnapshot(aggregate.rootType(), aggregateIdentifier);
        CurrentUnitOfWork.get()
                         .onCommit(uow -> verify(mockSnapshotter, never())
                                 .scheduleSnapshot(aggregate.rootType(), aggregateIdentifier));
        CurrentUnitOfWork.commit();
        verify(mockSnapshotter).scheduleSnapshot(aggregate.rootType(), aggregateIdentifier);
    }

    @Test
    void testSnapshotterTriggeredOnUnitOfWorkCommit() {
        SnapshotTrigger trigger = testSubject.prepareTrigger(aggregate.rootType());
        GenericDomainEventMessage<String> msg = new GenericDomainEventMessage<>("type", aggregateIdentifier, 0,
                                                                                "Mock contents", MetaData.emptyInstance());
        trigger.initializationFinished();
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);

        verify(mockSnapshotter, never()).scheduleSnapshot(aggregate.rootType(), aggregateIdentifier);
        CurrentUnitOfWork.get()
                         .onCommit(uow -> verify(mockSnapshotter)
                                 .scheduleSnapshot(aggregate.rootType(), aggregateIdentifier));
        CurrentUnitOfWork.commit();
    }

    @Test
    void testSnapshotterNotTriggered() {
        SnapshotTrigger trigger = testSubject.prepareTrigger(aggregate.rootType());
        GenericDomainEventMessage<String> msg = new GenericDomainEventMessage<>("type", aggregateIdentifier, 0,
                                                                                "Mock contents", MetaData.emptyInstance());
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);

        verify(mockSnapshotter, never()).scheduleSnapshot(aggregate.getClass(), aggregateIdentifier);
        CurrentUnitOfWork.commit();
        verify(mockSnapshotter, never()).scheduleSnapshot(aggregate.getClass(), aggregateIdentifier);
    }

    @Test
    void testCounterDoesNotResetWhenSerialized() throws IOException, ClassNotFoundException {
        SnapshotTrigger trigger = testSubject.prepareTrigger(aggregate.rootType());
        GenericDomainEventMessage<String> msg = new GenericDomainEventMessage<>("type", aggregateIdentifier, 0,
                                                                                "Mock contents", MetaData.emptyInstance());
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();<commit_msg>Added a test for rolling back a UoW.<commit_after>/*
 * Copyright (c) 2010-2018. Axon Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.axonframework.eventsourcing;

import org.axonframework.eventsourcing.utils.StubAggregate;
import org.axonframework.modelling.command.Aggregate;
import org.axonframework.modelling.command.inspection.AnnotatedAggregate;
import org.axonframework.modelling.command.inspection.AnnotatedAggregateMetaModelFactory;
import org.axonframework.eventhandling.GenericDomainEventMessage;
import org.axonframework.messaging.GenericMessage;
import org.axonframework.messaging.MetaData;
import org.axonframework.messaging.unitofwork.CurrentUnitOfWork;
import org.axonframework.messaging.unitofwork.DefaultUnitOfWork;
import org.axonframework.messaging.unitofwork.UnitOfWork;
import org.junit.jupiter.api.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import static org.mockito.Mockito.*;

/**
 * @author <NAME>
 */
class EventCountSnapshotTriggerDefinitionTest {

    private EventCountSnapshotTriggerDefinition testSubject;
    private Snapshotter mockSnapshotter;
    private String aggregateIdentifier;
    private Aggregate<?> aggregate;

    private UnitOfWork<?> unitOfWork;

    @BeforeEach
    void setUp() {
        while (CurrentUnitOfWork.isStarted()) {
            CurrentUnitOfWork.get().rollback();
        }
        mockSnapshotter = mock(Snapshotter.class);
        testSubject = new EventCountSnapshotTriggerDefinition(mockSnapshotter, 3);
        aggregateIdentifier = "aggregateIdentifier";
        unitOfWork = DefaultUnitOfWork.startAndGet(new GenericMessage<>("test"));
        aggregate = AnnotatedAggregate.initialize(new StubAggregate(aggregateIdentifier),
                                                  AnnotatedAggregateMetaModelFactory.inspectAggregate(StubAggregate.class), null);

    }

    @AfterEach
    void tearDown() {
        while (CurrentUnitOfWork.isStarted()) {
            CurrentUnitOfWork.get().rollback();
        }
    }

    @Test
    void testSnapshotterTriggeredOnUnitOfWorkCleanup() {
        SnapshotTrigger trigger = testSubject.prepareTrigger(aggregate.rootType());
        GenericDomainEventMessage<String> msg = new GenericDomainEventMessage<>("type", aggregateIdentifier, 0,
                                                                                "Mock contents", MetaData.emptyInstance());
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);

        verify(mockSnapshotter, never()).scheduleSnapshot(aggregate.rootType(), aggregateIdentifier);
        CurrentUnitOfWork.get()
                         .onCommit(uow -> verify(mockSnapshotter, never())
                                 .scheduleSnapshot(aggregate.rootType(), aggregateIdentifier));
        CurrentUnitOfWork.commit();
        verify(mockSnapshotter).scheduleSnapshot(aggregate.rootType(), aggregateIdentifier);
    }

    @Test
    void testSnapshotterTriggeredOnUnitOfWorkCommit() {
        SnapshotTrigger trigger = testSubject.prepareTrigger(aggregate.rootType());
        GenericDomainEventMessage<String> msg = new GenericDomainEventMessage<>("type", aggregateIdentifier, 0,
                                                                                "Mock contents", MetaData.emptyInstance());
        trigger.initializationFinished();
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);

        verify(mockSnapshotter, never()).scheduleSnapshot(aggregate.rootType(), aggregateIdentifier);
        CurrentUnitOfWork.commit();
        verify(mockSnapshotter).scheduleSnapshot(aggregate.rootType(), aggregateIdentifier);
    }

    @Test
    void testSnapshotterTriggeredOnUnitOfWorkRollback() {
        SnapshotTrigger trigger = testSubject.prepareTrigger(aggregate.rootType());
        GenericDomainEventMessage<String> msg = new GenericDomainEventMessage<>("type", aggregateIdentifier, 0,
                                                                                "Mock contents", MetaData.emptyInstance());
        trigger.initializationFinished();
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);

        verify(mockSnapshotter, never()).scheduleSnapshot(aggregate.rootType(), aggregateIdentifier);
        CurrentUnitOfWork.get().rollback();
        verify(mockSnapshotter, never()).scheduleSnapshot(aggregate.rootType(), aggregateIdentifier);
    }

    @Test
    void testSnapshotterNotTriggered() {
        SnapshotTrigger trigger = testSubject.prepareTrigger(aggregate.rootType());
        GenericDomainEventMessage<String> msg = new GenericDomainEventMessage<>("type", aggregateIdentifier, 0,
                                                                                "Mock contents", MetaData.emptyInstance());
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);

        verify(mockSnapshotter, never()).scheduleSnapshot(aggregate.getClass(), aggregateIdentifier);
        CurrentUnitOfWork.commit();
        verify(mockSnapshotter, never()).scheduleSnapshot(aggregate.getClass(), aggregateIdentifier);
    }

    @Test
    void testCounterDoesNotResetWhenSerialized() throws IOException, ClassNotFoundException {
        SnapshotTrigger trigger = testSubject.prepareTrigger(aggregate.rootType());
        GenericDomainEventMessage<String> msg = new GenericDomainEventMessage<>("type", aggregateIdentifier, 0,
                                                                                "Mock contents", MetaData.emptyInstance());
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);
        trigger.eventHandled(msg);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
{'fraction_non_alphanumeric': 0.09143686502177069, 'fraction_numerical': 0.0028221254636348974, 'mean_word_length': 3.0705612077453233, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2149866', 'n_tokens_mistral': 3014, 'n_tokens_neox': 2925, 'n_words': 586}