Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MP Messaging hamcrestination #1765

Merged
merged 3 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions microprofile/messaging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,18 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.bundles</groupId>
<artifactId>internal-test-libs</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion microprofile/messaging/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
requires io.helidon.microprofile.config;
requires io.helidon.microprofile.server;
requires io.helidon.microprofile.reactive;
requires org.reactivestreams;
requires transitive org.reactivestreams;
requires transitive microprofile.reactive.messaging.api;
requires transitive microprofile.reactive.streams.operators.api;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
import io.helidon.config.mp.MpConfigSources;
import io.helidon.microprofile.server.ServerCdiExtension;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public abstract class AbstractCDITest {
protected SeContainer cdiContainer;

Expand Down Expand Up @@ -79,9 +79,9 @@ protected <T> void forEachBean(Class<T> beanType, Annotation annotation, Consume

protected void assertAllReceived(CountableTestBean bean) {
try {
assertTrue(bean.getTestLatch().await(2, TimeUnit.SECONDS)
, "All messages not delivered in time, number of unreceived messages: "
+ bean.getTestLatch().getCount());
assertThat("All messages not delivered in time, number of unreceived messages: "
+ bean.getTestLatch().getCount(),
bean.getTestLatch().await(2, TimeUnit.SECONDS));
} catch (InterruptedException e) {
fail(e);
}
Expand All @@ -92,14 +92,14 @@ protected static SeContainer startCdiContainer(Map<String, String> p, Class<?>..
}

private static SeContainer startCdiContainer(Map<String, String> p, Set<Class<?>> beanClasses) {
p = new HashMap<>(p);
p.put("mp.initializer.allow", "true");
Config config = ConfigProviderResolver.instance().getBuilder()
.withSources(MpConfigSources.create(p))
.withSources(MpConfigSources.create(p),
MpConfigSources.create(Map.of("mp.initializer.allow", "true")))
.build();

ConfigProviderResolver.instance()
.registerConfig(config, Thread.currentThread().getContextClassLoader());

final SeContainerInitializer initializer = SeContainerInitializer.newInstance();
initializer.addBeanClasses(beanClasses.toArray(new Class<?>[0]));
return initializer.initialize();
Expand All @@ -121,8 +121,8 @@ protected static void stopCdiContainer() {
}

protected static final class CdiTestCase {
private String name;
private Class<?>[] clazzes;
private final String name;
private final Class<?>[] clazzes;

private CdiTestCase(String name, Class<?>... clazzes) {
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import io.helidon.config.Config;
import io.helidon.config.ConfigSources;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -52,8 +53,8 @@ void currentContext() {
.put(TEST_KEY, TEST_TOPIC_CUSTOM)
.build();

assertEquals(TEST_TOPIC_CUSTOM, c.getValue(TEST_KEY, String.class));
assertEquals(AdHocConfigBuilderTest.class.getName(), c.getValue("key.serializer", String.class));
assertThat(c.getValue(TEST_KEY, String.class), is(TEST_TOPIC_CUSTOM));
assertThat(c.getValue("key.serializer", String.class), is(AdHocConfigBuilderTest.class.getName()));
}

@Test
Expand All @@ -72,7 +73,7 @@ void customValueOverride() {
.put(TEST_KEY, TEST_TOPIC_CUSTOM)
.build();

assertEquals(TEST_TOPIC_CUSTOM, c.getValue(TEST_KEY, String.class));
assertThat(c.getValue(TEST_KEY, String.class), is(TEST_TOPIC_CUSTOM));
}

@Test
Expand Down Expand Up @@ -100,7 +101,7 @@ void putAllTest() {
.putAll(config2.get("mp.messaging.connector." + TEST_CONNECTOR))
.build();

assertEquals(ADDITION_ATTR_1_VALUE, c.getValue(ADDITION_ATTR_1, String.class));
assertEquals(ADDITION_ATTR_2_VALUE, c.getValue(ADDITION_ATTR_2, String.class));
assertThat(c.getValue(ADDITION_ATTR_1, String.class), is(ADDITION_ATTR_1_VALUE));
assertThat(c.getValue(ADDITION_ATTR_2, String.class), is(ADDITION_ATTR_2_VALUE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -71,9 +72,9 @@ void orderTest(String arg) {

order.forEach(i -> futures.get(i).complete(i));

assertEquals(IntStream
assertThat(resultList, is(IntStream
.range(0, arg.length())
.boxed()
.collect(Collectors.toList()), resultList);
.collect(Collectors.toList())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@

import java.util.Map;

import org.junit.jupiter.api.Test;

import io.helidon.config.Config;
import io.helidon.config.ConfigSources;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import org.junit.jupiter.api.Test;

public class ConnectorConfigTest {
private static final String TEST_CHANNEL_VALUE = "test-channel-value";
Expand Down Expand Up @@ -63,6 +64,7 @@ public Config getChannelsConfig() {
};

org.eclipse.microprofile.config.Config connectorConfig = connector.getConnectorConfig(TEST_CHANNEL_NAME);
assertEquals(TEST_CHANNEL_VALUE, connectorConfig.getValue(TEST_KEY, String.class));
assertThat(connectorConfig.getValue(TEST_KEY, String.class),
is(TEST_CHANNEL_VALUE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;

import org.eclipse.microprofile.reactive.messaging.Message;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import io.helidon.microprofile.messaging.MessageUtils;

class MessageUtilsTest {

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -58,19 +55,22 @@ static Stream<Tuple> testSource() {

@ParameterizedTest
@MethodSource("testSource")
void wrapperTest(Tuple tuple) throws ExecutionException, InterruptedException {
void wrapperTest(Tuple tuple) {
assertExpectedType(tuple.value, tuple.type);
}

private static void assertExpectedType(Object value, Class<?> type) throws ExecutionException, InterruptedException {
private static void assertExpectedType(Object value, Class<?> type) {
Object unwrapped = MessageUtils.unwrap(value, type);
assertTrue(type.isAssignableFrom(unwrapped.getClass()),
String.format("Expected value of type %s got %s instead", type.getSimpleName(), value.getClass().getSimpleName()));
assertThat(
String.format("Expected value of type %s got %s instead",
type.getSimpleName(),
value.getClass().getSimpleName()
), type.isAssignableFrom(unwrapped.getClass()));
}

private static class Tuple {
private Object value;
private Class<?> type;
private final Object value;
private final Class<?> type;

private Tuple(Object value, Class<?> clazz) {
this.value = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.fail;

import org.eclipse.microprofile.reactive.messaging.Incoming;
Expand Down Expand Up @@ -268,8 +268,8 @@ private static Stream<MethodTestCase> locateTestMethods() {
@MethodSource("locateTestMethods")
void signatureResolving(MethodTestCase testCase) {
Optional<MethodSignatureType> signatureType = MethodSignatureResolver.create(testCase.m).resolve();
assertTrue(signatureType.isPresent());
assertEquals(testCase.expectedType, signatureType.get());
assertThat("Resolved signature type is empty", signatureType.isPresent());
assertThat(signatureType.get(), is(testCase.expectedType));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;


import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;

import org.eclipse.microprofile.reactive.messaging.Message;
import org.eclipse.microprofile.reactive.streams.operators.ReactiveStreams;
Expand Down Expand Up @@ -66,10 +65,12 @@ void innerChannelBeanTest(Method method) throws ExecutionException, InterruptedE
Object unwrappedValue = unwrapProcessor.unwrap(Message.of("test"));
if (method.getName().endsWith("Message")) {
Assertions.assertTrue(MessageUtils.isMessageType(method));
assertTrue(unwrappedValue instanceof Message);
assertThat(unwrappedValue, instanceOf(Message.class));
} else {
assertFalse(MessageUtils.isMessageType(method));
assertFalse(unwrappedValue instanceof Message);
assertThat("Expected method param to be a Message type.",
MessageUtils.isMessageType(method),
not(true));
assertThat(unwrappedValue, not(instanceOf(Message.class)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

import javax.enterprise.context.ApplicationScoped;

import java.util.Arrays;
import java.util.concurrent.CountDownLatch;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItemInArray;

/**
* This test is modified version of official tck test in version 1.0
Expand All @@ -37,7 +37,7 @@ public class ConnectedBean {

@Incoming("iterable-channel-in")
public void receiveMethod(String msg) {
assertTrue(Arrays.asList(IterableConnector.TEST_DATA).contains(msg));
assertThat(IterableConnector.TEST_DATA, hasItemInArray(msg));
LATCH.countDown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isIn;

/**
* This test is modified version of official tck test in version 1.0
Expand All @@ -50,7 +51,7 @@ public String process(String msg) {

@Incoming("inner-channel")
public void receive(String msg) {
assertTrue(PROCESSED_DATA.contains(msg));
assertThat(msg, isIn(PROCESSED_DATA));
LATCH.countDown();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

import io.helidon.microprofile.messaging.AbstractCDITest;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

Expand All @@ -41,7 +41,7 @@ void connectorTest() throws InterruptedException {
Map.of("mp.messaging.incoming.iterable-channel-in.connector", "iterable-connector"),
IterableConnector.class,
ConnectedBean.class);
assertTrue(ConnectedBean.LATCH.await(2, TimeUnit.SECONDS));
assertThat("Not connected in time.", ConnectedBean.LATCH.await(2, TimeUnit.SECONDS));
}

@Test
Expand All @@ -50,7 +50,7 @@ void connectorWithProcessorTest() throws InterruptedException {
Map.of("mp.messaging.incoming.iterable-channel-in.connector", "iterable-connector"),
IterableConnector.class,
ConnectedProcessorBean.class);
assertTrue(ConnectedProcessorBean.LATCH.await(2, TimeUnit.SECONDS));
assertThat("Not connected in time.", ConnectedProcessorBean.LATCH.await(2, TimeUnit.SECONDS));
}

@Test
Expand All @@ -59,7 +59,7 @@ void connectorWithProcessorOnlyTest() throws InterruptedException {
"mp.messaging.incoming.iterable-channel-in.connector", "iterable-connector",
"mp.messaging.outgoing.iterable-channel-out.connector", "iterable-connector");
cdiContainer = startCdiContainer(p, IterableConnector.class, ConnectedOnlyProcessorBean.class);
assertTrue(IterableConnector.LATCH.await(2, TimeUnit.SECONDS));
assertThat("Not connected in time.", IterableConnector.LATCH.await(2, TimeUnit.SECONDS));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.junit.platform.commons.util.ClassFilter;
import org.junit.platform.commons.util.ReflectionUtils;

public class InnerChannelTest extends AbstractCDITest {
public class ChannelTest extends AbstractCDITest {

@Override
public void setUp() {
Expand All @@ -47,7 +47,7 @@ public void setUp() {
static Stream<CdiTestCase> testCaseSource() {
return ReflectionUtils
.findAllClassesInPackage(
InnerChannelTest.class.getPackage().getName(),
ChannelTest.class.getPackage().getName(),
ClassFilter.of(c -> Objects.nonNull(c.getAnnotation(ApplicationScoped.class))))
.stream()
.map(CdiTestCase::from);
Expand Down
Loading