Skip to content

Commit

Permalink
Copy some test utilities to the integration test module
Browse files Browse the repository at this point in the history
Copy some test utilities to the integration test module

#755
  • Loading branch information
smcvb committed Oct 11, 2018
1 parent 2e9e7d7 commit c0bab39
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 0 deletions.
52 changes: 52 additions & 0 deletions integrationtests/src/test/java/org/axonframework/AssertUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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;

import java.util.concurrent.TimeUnit;

/**
* Utility class for special assertions
*/
public class AssertUtils {

private AssertUtils() {

}

/**
* Assert that the given {@code assertion} succeeds with the given {@code time} and {@code unit}.
* @param time The time in which the assertion must pass
* @param unit The unit in which time is expressed
* @param assertion the assertion to succeed within the deadline
*/
public static void assertWithin(int time, TimeUnit unit, Runnable assertion) {
long now = System.currentTimeMillis();
long deadline = now + unit.toMillis(time);
do {
try {
assertion.run();
break;
} catch (AssertionError e) {
if (now >= deadline) {
throw e;
}
}
now = System.currentTimeMillis();
} while (true);
}

}
107 changes: 107 additions & 0 deletions integrationtests/src/test/java/org/axonframework/EventTestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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;

import org.axonframework.common.IdentifierFactory;
import org.axonframework.eventhandling.DomainEventMessage;
import org.axonframework.eventhandling.GenericDomainEventMessage;
import org.axonframework.messaging.MetaData;

import java.time.Instant;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

// TODO copied from EventStoreTestUtils - consolidate
public abstract class EventTestUtils {

public static final String PAYLOAD = "payload";
public static final String AGGREGATE = "aggregate";
private static final String TYPE = "type";
private static final MetaData METADATA = MetaData.emptyInstance();

public static List<DomainEventMessage<?>> createEvents(int numberOfEvents) {
return IntStream.range(0, numberOfEvents)
.mapToObj(sequenceNumber -> createEvent(TYPE,
IdentifierFactory.getInstance().generateIdentifier(),
AGGREGATE,
sequenceNumber,
PAYLOAD + sequenceNumber,
METADATA))
.collect(Collectors.toList());
}

public static List<DomainEventMessage<?>> createUUIDEvents(int numberOfEvents) {
return IntStream.range(0, numberOfEvents).mapToObj(
sequenceNumber -> createEvent(TYPE,
IdentifierFactory.getInstance().generateIdentifier(),
UUID.randomUUID().toString(),
sequenceNumber,
PAYLOAD + sequenceNumber,
METADATA))
.collect(Collectors.toList());
}

public static DomainEventMessage<String> createEvent() {
return createEvent(0);
}

public static DomainEventMessage<String> createEvent(long sequenceNumber) {
return createEvent(AGGREGATE, sequenceNumber);
}

public static DomainEventMessage<String> createEvent(long sequenceNumber, Instant timestamp) {
return new GenericDomainEventMessage<>(TYPE, AGGREGATE, sequenceNumber, PAYLOAD, METADATA,
IdentifierFactory.getInstance().generateIdentifier(), timestamp);
}

public static DomainEventMessage<String> createEvent(String aggregateId, long sequenceNumber) {
return createEvent(aggregateId, sequenceNumber, PAYLOAD);
}

public static DomainEventMessage<String> createEvent(String aggregateId, long sequenceNumber, String payload) {
return createEvent(TYPE,
IdentifierFactory.getInstance().generateIdentifier(),
aggregateId,
sequenceNumber,
payload,
METADATA);
}

public static DomainEventMessage<String> createEvent(String eventId, String aggregateId, long sequenceNumber) {
return createEvent(TYPE, eventId, aggregateId, sequenceNumber, PAYLOAD, METADATA);
}

public static DomainEventMessage<String> createEvent(String type,
String eventId,
String aggregateId,
long sequenceNumber,
String payload,
MetaData metaData) {
return new GenericDomainEventMessage<>(type,
aggregateId,
sequenceNumber,
payload,
metaData,
eventId,
GenericDomainEventMessage.clock.instant());
}

private EventTestUtils() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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;

/**
* Mock exception that provides no stack trace.
* TODO check whether to move this to a test-utils package or something like that
* @author Allard Buijze
* @since 2.0
*/
public class MockException extends RuntimeException {

public MockException(String message) {
super(message);
}

public MockException() {
super("Mock");
}

@Override
public synchronized Throwable fillInStackTrace() {
return this;
}

@Override
public StackTraceElement[] getStackTrace() {
return new StackTraceElement[]{};
}
}

0 comments on commit c0bab39

Please sign in to comment.