Skip to content
Snippets Groups Projects
Commit b71daace authored by René Schöne's avatar René Schöne
Browse files

Resolve "Refactorings/Clean-Up"

- add cloc and two use cases to pages
- move some aspects into separate template files
- add a method to reset the evaluation counter
- rename forwardingNTA to forwarding
- begin with default mappings using templates 
- also indirectly work on #55 to include marshalling methods only when needed
- move TestUtils to a separate package
parent da232e01
No related branches found
No related tags found
3 merge requests!39Version 1.1.0,!35Version 1.0.0,!33Resolve "Refactorings/Clean-Up"
Showing
with 780 additions and 47 deletions
package org.jastadd.ragconnect.tests.relation;
import org.jastadd.ragconnect.tests.AbstractMqttTest;
import org.jastadd.ragconnect.tests.TestUtils;
import org.jastadd.ragconnect.tests.utils.TestUtils;
import org.junit.jupiter.api.Tag;
import relationInc.ast.*;
......@@ -11,8 +11,9 @@ import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
import static org.jastadd.ragconnect.tests.TestUtils.TestChecker;
import static org.jastadd.ragconnect.tests.TestUtils.mqttUri;
import org.jastadd.ragconnect.tests.utils.TestChecker;
import static org.jastadd.ragconnect.tests.utils.TestUtils.mqttUri;
import static org.junit.jupiter.api.Assertions.*;
/**
......
package org.jastadd.ragconnect.tests.singleList;
import org.jastadd.ragconnect.tests.AbstractMqttTest;
import org.jastadd.ragconnect.tests.TestUtils;
import org.jastadd.ragconnect.tests.utils.IntList;
import org.jastadd.ragconnect.tests.utils.TestChecker;
import org.jastadd.ragconnect.tests.utils.TestUtils;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import singleList.ast.MqttHandler;
......@@ -13,8 +15,8 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import static org.jastadd.ragconnect.tests.TestUtils.*;
import static org.jastadd.ragconnect.tests.TestUtils.IntList.list;
import static org.jastadd.ragconnect.tests.utils.TestUtils.*;
import static org.jastadd.ragconnect.tests.utils.IntList.list;
import static org.junit.jupiter.api.Assertions.*;
/**
......
......@@ -2,8 +2,8 @@ package org.jastadd.ragconnect.tests.singleListVariant;
import org.assertj.core.api.Assertions;
import org.jastadd.ragconnect.tests.AbstractMqttTest;
import org.jastadd.ragconnect.tests.TestUtils.IntList;
import org.jastadd.ragconnect.tests.TestUtils.TestChecker;
import org.jastadd.ragconnect.tests.utils.IntList;
import org.jastadd.ragconnect.tests.utils.TestChecker;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
......@@ -13,9 +13,9 @@ import java.util.List;
import java.util.function.BiConsumer;
import static java.lang.Math.abs;
import static org.jastadd.ragconnect.tests.TestUtils.IntList.list;
import static org.jastadd.ragconnect.tests.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.TestUtils.testJaddContainReferenceToJackson;
import static org.jastadd.ragconnect.tests.utils.IntList.list;
import static org.jastadd.ragconnect.tests.utils.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.utils.TestUtils.testJaddContainReferenceToJackson;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
......@@ -372,10 +372,10 @@ public abstract class AbstractSingleListVariantTest extends AbstractMqttTest {
* Check against expected lists of IDs.
* If an ID is negative, do not check Opts and Lists, but use absolute value for comparison.
* The tests starts with ID 0 and does not use opts and lists at this point, so checking with > 0 is used.
* @param expectedList ids for unnamed and named endpoints without add
* @param expectedWithAddList ids for endpoints with add, but not those with opts and lists
* @param expectedList ids for unnamed and named ports without add
* @param expectedWithAddList ids for ports with add, but not those with opts and lists
* (only positive numbers can/need to be passed)
* @param expectedWithAddListForOptAndList ids for endpoints with add and with opts and lists
* @param expectedWithAddListForOptAndList ids for ports with add and with opts and lists
*/
private void checkTree(IntList expectedList,
IntList expectedWithAddList,
......
package org.jastadd.ragconnect.tests.singleListVariant;
import org.jastadd.ragconnect.tests.TestUtils;
import org.jastadd.ragconnect.tests.utils.TestUtils;
import org.junit.jupiter.api.Tag;
import singleListVariantInc.ast.MqttHandler;
import singleListVariantInc.ast.ReceiverRoot;
......
package org.jastadd.ragconnect.tests.singleListVariant;
import org.jastadd.ragconnect.tests.TestUtils;
import org.jastadd.ragconnect.tests.utils.TestUtils;
import singleListVariant.ast.*;
import java.io.IOException;
......
package org.jastadd.ragconnect.tests.tokenValue;
import org.jastadd.ragconnect.tests.AbstractMqttTest;
import org.jastadd.ragconnect.tests.TestUtils;
import org.jastadd.ragconnect.tests.TestUtils.TestChecker;
import org.junit.jupiter.api.Tag;
import org.jastadd.ragconnect.tests.utils.DefaultMappings;
import org.jastadd.ragconnect.tests.utils.TestUtils;
import org.jastadd.ragconnect.tests.utils.TestChecker;
import org.junit.jupiter.api.Test;
import tokenValueSend.ast.MqttHandler;
......@@ -11,8 +11,8 @@ import java.io.IOException;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import static org.jastadd.ragconnect.tests.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.TestUtils.testJaddContainReferenceToJackson;
import static org.jastadd.ragconnect.tests.utils.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.utils.TestUtils.testJaddContainReferenceToJackson;
import static org.junit.jupiter.api.Assertions.*;
/**
......@@ -68,20 +68,20 @@ public abstract class AbstractTokenValueSendTest extends AbstractMqttTest {
handler.newConnection(TOPIC_SEND_ONE, bytes -> {
dataOne.numberOfStringValues += 1;
dataOne.lastStringValue = TestUtils.DefaultMappings.BytesToString(bytes);
dataOne.lastStringValue = DefaultMappings.BytesToString(bytes);
});
handler.newConnection(TOPIC_SEND_TWO, bytes -> {
dataTwo.numberOfStringValues += 1;
dataTwo.lastStringValue = TestUtils.DefaultMappings.BytesToString(bytes);
dataTwo.lastStringValue = DefaultMappings.BytesToString(bytes);
});
handler.newConnection(TOPIC_SEND_THREE_VALUE, bytes -> {
dataThree.numberOfStringValues += 1;
dataThree.lastStringValue = TestUtils.DefaultMappings.BytesToString(bytes);
dataThree.lastStringValue = DefaultMappings.BytesToString(bytes);
});
handler.newConnection(TOPIC_SEND_THREE_OTHER, bytes -> {
dataThreeOther.numberOfStringValues += 1;
dataThreeOther.lastStringValue = TestUtils.DefaultMappings.BytesToString(bytes);
dataThreeOther.lastStringValue = DefaultMappings.BytesToString(bytes);
});
connectModel();
......
......@@ -6,7 +6,7 @@ import tokenValueSendInc.ast.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import static org.jastadd.ragconnect.tests.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.utils.TestUtils.mqttUri;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
......
......@@ -5,7 +5,7 @@ import tokenValueSend.ast.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import static org.jastadd.ragconnect.tests.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.utils.TestUtils.mqttUri;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
......
package org.jastadd.ragconnect.tests.tree;
import org.jastadd.ragconnect.tests.AbstractMqttTest;
import org.jastadd.ragconnect.tests.TestUtils;
import org.jastadd.ragconnect.tests.utils.TestUtils;
import org.junit.jupiter.api.Tag;
import java.io.IOException;
......@@ -16,6 +16,7 @@ import static org.junit.jupiter.api.Assertions.*;
* @author rschoene - Initial contribution
*/
@Tag("Tree")
@Tag("New")
public abstract class AbstractTreeTest extends AbstractMqttTest {
protected static final String TOPIC_ALFA = "alfa";
protected ReceiverData data;
......
package org.jastadd.ragconnect.tests.tree;
import org.jastadd.ragconnect.tests.TestUtils;
import org.jastadd.ragconnect.tests.utils.TestUtils;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import treeInc.ast.*;
......@@ -10,8 +10,8 @@ import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
import static org.jastadd.ragconnect.tests.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.TestUtils.testJaddContainReferenceToJackson;
import static org.jastadd.ragconnect.tests.utils.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.utils.TestUtils.testJaddContainReferenceToJackson;
import static org.junit.jupiter.api.Assertions.*;
/**
......
package org.jastadd.ragconnect.tests.tree;
import org.jastadd.ragconnect.tests.TestUtils;
import org.junit.jupiter.api.Tag;
import org.jastadd.ragconnect.tests.utils.TestUtils;
import org.junit.jupiter.api.Test;
import tree.ast.MqttHandler;
import tree.ast.ReceiverRoot;
......@@ -12,8 +11,8 @@ import java.io.IOException;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import static org.jastadd.ragconnect.tests.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.TestUtils.testJaddContainReferenceToJackson;
import static org.jastadd.ragconnect.tests.utils.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.utils.TestUtils.testJaddContainReferenceToJackson;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
......
package org.jastadd.ragconnect.tests.treeAllowedTokens;
import org.jastadd.ragconnect.tests.AbstractMqttTest;
import org.jastadd.ragconnect.tests.TestUtils;
import org.jastadd.ragconnect.tests.utils.DefaultMappings;
import org.jastadd.ragconnect.tests.utils.TestUtils;
import org.junit.jupiter.api.Tag;
import java.io.IOException;
......@@ -183,19 +184,19 @@ public abstract class AbstractTreeAllowedTokensTest extends AbstractMqttTest {
protected abstract void disconnectSend() throws IOException;
protected void sendInput1WhenFalse(int value) {
publisher.publish(TOPIC_INPUT1FALSE, TestUtils.DefaultMappings.IntToBytes(value));
publisher.publish(TOPIC_INPUT1FALSE, DefaultMappings.IntToBytes(value));
}
protected void sendInput1WhenTrue(int value) {
publisher.publish(TOPIC_INPUT1TRUE, TestUtils.DefaultMappings.IntToBytes(value));
publisher.publish(TOPIC_INPUT1TRUE, DefaultMappings.IntToBytes(value));
}
protected void sendInput2(String value) {
publisher.publish(TOPIC_INPUT2, TestUtils.DefaultMappings.StringToBytes(value));
publisher.publish(TOPIC_INPUT2, DefaultMappings.StringToBytes(value));
}
protected void sendInput3(double value) {
publisher.publish(TOPIC_INPUT3, TestUtils.DefaultMappings.DoubleToBytes(value));
publisher.publish(TOPIC_INPUT3, DefaultMappings.DoubleToBytes(value));
}
protected abstract void setFlag(boolean value);
......
package org.jastadd.ragconnect.tests.treeAllowedTokens;
import org.jastadd.ragconnect.tests.TestUtils;
import org.jastadd.ragconnect.tests.utils.TestUtils;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import treeAllowedTokensInc.ast.*;
......@@ -9,8 +9,8 @@ import java.io.IOException;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import static org.jastadd.ragconnect.tests.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.TestUtils.testJaddContainReferenceToJackson;
import static org.jastadd.ragconnect.tests.utils.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.utils.TestUtils.testJaddContainReferenceToJackson;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
......
package org.jastadd.ragconnect.tests.treeAllowedTokens;
import org.jastadd.ragconnect.tests.TestUtils;
import org.junit.jupiter.api.Tag;
import org.jastadd.ragconnect.tests.utils.TestUtils;
import org.junit.jupiter.api.Test;
import treeAllowedTokens.ast.*;
......@@ -9,8 +8,8 @@ import java.io.IOException;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import static org.jastadd.ragconnect.tests.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.TestUtils.testJaddContainReferenceToJackson;
import static org.jastadd.ragconnect.tests.utils.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.utils.TestUtils.testJaddContainReferenceToJackson;
import static org.junit.jupiter.api.Assertions.*;
/**
......
package org.jastadd.ragconnect.tests.utils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import static org.junit.jupiter.api.Assertions.fail;
/**
* TODO: Add description.
*
* @author rschoene - Initial contribution
*/
public class ChangeObserver {
Map<Supplier<String>, String> callableToPrevious = new HashMap<>();
private Callable<Boolean> hasChanged;
@SafeVarargs
public final void init(Supplier<String>... suppliers) {
callableToPrevious.clear();
Arrays.stream(suppliers).forEach(callable -> callableToPrevious.put(callable, callable.get()));
hasChanged = () -> callableToPrevious.entrySet().stream()
.anyMatch(entry -> !entry.getKey().get().equals(entry.getValue()));
}
public void start() {
updatePrevious();
}
private void updatePrevious() {
callableToPrevious.keySet().forEach(callable -> callableToPrevious.put(callable, callable.get()));
}
public void awaitChange() {
TestUtils.awaitMqtt().until(hasChanged);
updatePrevious();
}
public boolean hasChanged() {
try {
return hasChanged.call();
} catch (Exception e) {
fail(e);
return false;
}
}
}
package org.jastadd.ragconnect.tests.utils;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* TODO: Add description.
*
* @author rschoene - Initial contribution
*/
public class DefaultMappings {
@SuppressWarnings("rawtypes")
static class ReadNode extends defaultOnlyRead.ast.ASTNode {
public boolean DefaultBytesToBooleanMapping(byte[] input) throws Exception {
return _ragconnect__apply__DefaultBytesToBooleanMapping(input);
}
public int DefaultBytesToIntMapping(byte[] input) throws Exception {
return _ragconnect__apply__DefaultBytesToIntMapping(input);
}
public short DefaultBytesToShortMapping(byte[] input) throws Exception {
return _ragconnect__apply__DefaultBytesToShortMapping(input);
}
public long DefaultBytesToLongMapping(byte[] input) throws Exception {
return _ragconnect__apply__DefaultBytesToLongMapping(input);
}
public float DefaultBytesToFloatMapping(byte[] input) throws Exception {
return _ragconnect__apply__DefaultBytesToFloatMapping(input);
}
public double DefaultBytesToDoubleMapping(byte[] input) throws Exception {
return _ragconnect__apply__DefaultBytesToDoubleMapping(input);
}
public char DefaultBytesToCharMapping(byte[] input) throws Exception {
return _ragconnect__apply__DefaultBytesToCharMapping(input);
}
public String DefaultBytesToStringMapping(byte[] input) throws Exception {
return _ragconnect__apply__DefaultBytesToStringMapping(input);
}
}
@SuppressWarnings("rawtypes")
static class WriteNode extends defaultOnlyWrite.ast.ASTNode {
public byte[] DefaultBooleanToBytesMapping(boolean input) throws Exception {
return _ragconnect__apply__DefaultBooleanToBytesMapping(input);
}
public byte[] DefaultIntToBytesMapping(int input) throws Exception {
return _ragconnect__apply__DefaultIntToBytesMapping(input);
}
public byte[] DefaultShortToBytesMapping(short input) throws Exception {
return _ragconnect__apply__DefaultShortToBytesMapping(input);
}
public byte[] DefaultLongToBytesMapping(long input) throws Exception {
return _ragconnect__apply__DefaultLongToBytesMapping(input);
}
public byte[] DefaultFloatToBytesMapping(float input) throws Exception {
return _ragconnect__apply__DefaultFloatToBytesMapping(input);
}
public byte[] DefaultDoubleToBytesMapping(double input) throws Exception {
return _ragconnect__apply__DefaultDoubleToBytesMapping(input);
}
public byte[] DefaultCharToBytesMapping(char input) throws Exception {
return _ragconnect__apply__DefaultCharToBytesMapping(input);
}
public byte[] DefaultStringToBytesMapping(String input) throws Exception {
return _ragconnect__apply__DefaultStringToBytesMapping(input);
}
}
@FunctionalInterface
public interface SerializeFunction<E extends Throwable> {
void accept(JsonGenerator g, String fieldName) throws E;
}
static ReadNode readNode = new ReadNode();
static WriteNode writeNode = new WriteNode();
public static boolean BytesToBool(byte[] input) {
try {
return readNode.DefaultBytesToBooleanMapping(input);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static int BytesToInt(byte[] input) {
try {
return readNode.DefaultBytesToIntMapping(input);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public static short BytesToShort(byte[] input) {
try {
return readNode.DefaultBytesToShortMapping(input);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public static long BytesToLong(byte[] input) {
try {
return readNode.DefaultBytesToLongMapping(input);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public static float BytesToFloat(byte[] input) {
try {
return readNode.DefaultBytesToFloatMapping(input);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public static double BytesToDouble(byte[] input) {
try {
return readNode.DefaultBytesToDoubleMapping(input);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public static char BytesToChar(byte[] input) {
try {
return readNode.DefaultBytesToCharMapping(input);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public static String BytesToString(byte[] input) {
try {
return readNode.DefaultBytesToStringMapping(input);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static byte[] BoolToBytes(boolean input) {
try {
return writeNode.DefaultBooleanToBytesMapping(input);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static byte[] IntToBytes(int input) {
try {
return writeNode.DefaultIntToBytesMapping(input);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static byte[] ShortToBytes(short input) {
try {
return writeNode.DefaultShortToBytesMapping(input);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static byte[] LongToBytes(long input) {
try {
return writeNode.DefaultLongToBytesMapping(input);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static byte[] FloatToBytes(float input) {
try {
return writeNode.DefaultFloatToBytesMapping(input);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static byte[] DoubleToBytes(double input) {
try {
return writeNode.DefaultDoubleToBytesMapping(input);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static byte[] CharToBytes(char input) {
try {
return writeNode.DefaultCharToBytesMapping(input);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static byte[] StringToBytes(String input) {
try {
return writeNode.DefaultStringToBytesMapping(input);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static <E extends Throwable> byte[] TreeToBytes(SerializeFunction<E> serializeFunction) throws E, IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonFactory factory = new JsonFactory();
JsonGenerator generator = factory.createGenerator(outputStream, JsonEncoding.UTF8);
serializeFunction.accept(generator, null);
generator.flush();
return outputStream.toString().getBytes();
}
}
package org.jastadd.ragconnect.tests.utils;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* TODO: Add description.
*
* @author rschoene - Initial contribution
*/
public class IntList {
private final List<Integer> integers;
public IntList(Integer... values) {
integers = Arrays.stream(values).filter(Objects::nonNull).collect(Collectors.toList());
}
public List<Integer> toList() {
return integers;
}
public List<Integer> toAbsList() {
return integers.stream().map(Math::abs).collect(Collectors.toList());
}
public static IntList list(Integer... values) {
return new IntList(values);
}
}
......@@ -8,3 +8,4 @@ rootProject.name = 'ragconnect'
include 'ragconnect.base'
include 'ragconnect.tests'
include 'ragconnect.featureTest'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment