Skip to content

Commit

Permalink
Added a YamlTester interface to simplify running YAML based tests #475
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-lawrey committed May 18, 2022
1 parent d15d955 commit 0e8290c
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/main/java/net/openhft/chronicle/wire/TextMethodTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.openhft.chronicle.core.io.Closeable;
import net.openhft.chronicle.core.io.IOTools;
import net.openhft.chronicle.core.util.InvocationTargetRuntimeException;
import net.openhft.chronicle.wire.utils.YamlTester;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -34,6 +35,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -44,7 +46,7 @@
import java.util.function.Function;

@SuppressWarnings({"rawtypes", "unchecked"})
public class TextMethodTester<T> {
public class TextMethodTester<T> implements YamlTester {
private static final boolean REGRESS_TESTS = Jvm.getBoolean("regress.tests");
private final String input;
private final Class<T> outputClass;
Expand Down Expand Up @@ -162,26 +164,31 @@ public TextMethodTester run() throws IOException {
: new Object[]{component};

for (String setup : setups) {
Wire wire0 = createWire(BytesUtil.readFile(setup));

MethodReader reader0 = wire0.methodReaderBuilder()
.methodReaderInterceptorReturns(methodReaderInterceptorReturns)
.warnMissing(true)
.build(components);
while (readOne(reader0)) {
try {
final Bytes<?> bytes = Bytes.wrapForRead(IOTools.readFile(outputClass, setup));
Wire wire0 = createWire(bytes);

MethodReader reader0 = wire0.methodReaderBuilder()
.methodReaderInterceptorReturns(methodReaderInterceptorReturns)
.warnMissing(true)
.build(components);
while (readOne(reader0)) {
wireOut.bytes().clear();
}
wireOut.bytes().clear();
} catch (FileNotFoundException ignored) {
// continue
}
wireOut.bytes().clear();
}

if (component instanceof PostSetup)
((PostSetup) component).postSetup();

Wire wire = createWire(BytesUtil.readFile(input));
Wire wire = createWire(Bytes.wrapForRead(IOTools.readFile(outputClass, input)));

// expected
if (retainLast == null) {
expected = BytesUtil.readFile(output).toString().trim().replace("\r", "");
expected = new String(IOTools.readFile(outputClass, output), StandardCharsets.ISO_8859_1).trim().replace("\r", "");
} else {
expected = loadLastValues().toString().trim();
}
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/net/openhft/chronicle/wire/utils/YamlTester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package net.openhft.chronicle.wire.utils;

import net.openhft.chronicle.wire.TextMethodTester;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.function.Function;

public interface YamlTester {
/**
* Test a microservice implemented in a class using in.yaml comparing with out.yaml,
* with optionally setup.yaml to initialise it.
*
* @param implClass of the implementation
* @param path where the yaml files can be found
* @return the results for comparison
* @throws AssertionError if anything went wrong
*/
static YamlTester runTest(Class<?> implClass, String path) throws AssertionError {
for (Constructor<?> cons : implClass.getDeclaredConstructors()) {
if (cons.getParameterCount() == 1) {
final Class<?>[] parameterTypes = cons.getParameterTypes();
if (parameterTypes[0].isInterface()) {
return runTest((Object out) -> {
try {
return cons.newInstance(out);
} catch (Exception e) {
throw new AssertionError(e);
}
}, (Class) parameterTypes[0], path);
}
}
}
throw new IllegalArgumentException("Unable to find a constructor with one interface as an argument");
}

/**
* Test a microservice implemented in a class using in.yaml comparing with out.yaml,
* with optionally setup.yaml to initialise it.
*
* @param builder to construct a component to be tested
* @param outClass the interface of output
* @param path where the yaml files can be found
* @return the results for comparison
* @throws AssertionError if anything went wrong
*/
static <T> YamlTester runTest(Function<T, Object> builder, Class<T> outClass, String path) throws AssertionError {
try {
return new TextMethodTester<T>(
path + "/in.yaml",
builder,
outClass,
path + "/out.yaml")
.setup(path + "/setup.yaml")
.run();
} catch (IOException ioe) {
throw new AssertionError(ioe);
}
}

/**
* @return the expected String
*/
String expected();

/**
* @return the actual String
*/
String actual();
}
15 changes: 15 additions & 0 deletions src/test/java/net/openhft/chronicle/wire/utils/TestEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.openhft.chronicle.wire.utils;

import net.openhft.chronicle.wire.LongConversion;
import net.openhft.chronicle.wire.NanoTimestampLongConverter;
import net.openhft.chronicle.wire.SelfDescribingMarshallable;

public class TestEvent extends SelfDescribingMarshallable {
@LongConversion(NanoTimestampLongConverter.class)
long eventTime;

@LongConversion(NanoTimestampLongConverter.class)
long processedTime;
@LongConversion(NanoTimestampLongConverter.class)
long currentTime;
}
27 changes: 27 additions & 0 deletions src/test/java/net/openhft/chronicle/wire/utils/TestImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.openhft.chronicle.wire.utils;

import net.openhft.chronicle.wire.LongConversion;
import net.openhft.chronicle.wire.NanoTimestampLongConverter;

import static net.openhft.chronicle.core.time.SystemTimeProvider.CLOCK;

public class TestImpl implements TestIn {
private final TestOut out;
private long time;

public TestImpl(TestOut out) {
this.out = out;
}

@Override
public void time(@LongConversion(NanoTimestampLongConverter.class) long time) {
this.time = time;
}

@Override
public void testEvent(TestEvent dto) {
dto.processedTime = time;
dto.currentTime = CLOCK.currentTimeNanos();
out.testEvent(dto);
}
}
10 changes: 10 additions & 0 deletions src/test/java/net/openhft/chronicle/wire/utils/TestIn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.openhft.chronicle.wire.utils;

import net.openhft.chronicle.wire.LongConversion;
import net.openhft.chronicle.wire.NanoTimestampLongConverter;

public interface TestIn {
void time(@LongConversion(NanoTimestampLongConverter.class) long time);

void testEvent(TestEvent dto);
}
5 changes: 5 additions & 0 deletions src/test/java/net/openhft/chronicle/wire/utils/TestOut.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.openhft.chronicle.wire.utils;

public interface TestOut {
void testEvent(TestEvent dto);
}
36 changes: 36 additions & 0 deletions src/test/java/net/openhft/chronicle/wire/utils/YamlTesterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.openhft.chronicle.wire.utils;

import net.openhft.chronicle.core.time.SetTimeProvider;
import net.openhft.chronicle.core.time.SystemTimeProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class YamlTesterTest {
@Before
public void setUp() {
SystemTimeProvider.CLOCK = new SetTimeProvider("2022-05-17T20:26:00")
.autoIncrement(1, TimeUnit.MICROSECONDS);
}

@After
public void tearDown() {
SystemTimeProvider.CLOCK = SystemTimeProvider.INSTANCE;
}

@Test
public void t1() {
final YamlTester yt = YamlTester.runTest(TestImpl.class, "yaml-tester/t1");
assertEquals(yt.expected(), yt.actual());
}

@Test
public void t2() {
final YamlTester yt = YamlTester.runTest(TestImpl::new, TestOut.class, "yaml-tester/t2");
assertEquals(yt.expected(), yt.actual());
}
}
8 changes: 8 additions & 0 deletions src/test/resources/yaml-tester/t1/in.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
time: 2022-05-17T20:25:02.002
...
---
testEvent: {
eventTime: 2022-05-17T20:25:01.001
}
...
8 changes: 8 additions & 0 deletions src/test/resources/yaml-tester/t1/out.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
---
testEvent: {
eventTime: 2022-05-17T20:25:01.001,
processedTime: 2022-05-17T20:25:02.002,
currentTime: 2022-05-17T20:26:00
}
...
13 changes: 13 additions & 0 deletions src/test/resources/yaml-tester/t2/in.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
testEvent: {
eventTime: 2022-05-17T20:25:01.001
}
...
---
time: 2022-05-17T20:25:04.004
...
---
testEvent: {
eventTime: 2022-05-17T20:25:03.003
}
...
15 changes: 15 additions & 0 deletions src/test/resources/yaml-tester/t2/out.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
testEvent: {
eventTime: 2022-05-17T20:25:01.001,
processedTime: 2022-05-17T20:25:02.002,
currentTime: 2022-05-17T20:26:00
}
...
---
---
testEvent: {
eventTime: 2022-05-17T20:25:03.003,
processedTime: 2022-05-17T20:25:04.004,
currentTime: 2022-05-17T20:26:00.000001
}
...
3 changes: 3 additions & 0 deletions src/test/resources/yaml-tester/t2/setup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
time: 2022-05-17T20:25:02.002
...

0 comments on commit 0e8290c

Please sign in to comment.