diff --git a/pom.xml b/pom.xml
index 392b09802..a447a7b93 100644
--- a/pom.xml
+++ b/pom.xml
@@ -38,6 +38,11 @@
li.strolch
li.strolch.runtime
+
+ junit
+ junit
+ compile
+
diff --git a/src/main/java/li/strolch/testbase/runtime/OrderModelTestRunner.java b/src/main/java/li/strolch/testbase/runtime/OrderModelTestRunner.java
new file mode 100644
index 000000000..f9561c0bb
--- /dev/null
+++ b/src/main/java/li/strolch/testbase/runtime/OrderModelTestRunner.java
@@ -0,0 +1,157 @@
+package li.strolch.testbase.runtime;
+
+import static li.strolch.model.ModelGenerator.BAG_ID;
+import static li.strolch.model.ModelGenerator.PARAM_STRING_ID;
+import static li.strolch.model.ModelGenerator.createOrder;
+import static li.strolch.model.ModelGenerator.createOrders;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import li.strolch.model.Order;
+import li.strolch.model.parameter.Parameter;
+import li.strolch.persistence.api.OrderDao;
+import li.strolch.persistence.api.PersistenceHandler;
+import li.strolch.persistence.api.StrolchTransaction;
+
+@SuppressWarnings("nls")
+public class OrderModelTestRunner {
+
+ private static final String ID = "@testOrder";
+ private static final String NAME = "Test Order";
+ private static final String TYPE = "ToStock";
+
+ private RuntimeMock runtimeMock;
+
+ public OrderModelTestRunner(RuntimeMock runtimeMock) {
+ this.runtimeMock = runtimeMock;
+ }
+
+ protected PersistenceHandler getPersistenceHandler() {
+ return this.runtimeMock.getPersistenceHandler();
+ }
+
+ public void runCreateOrderTest() {
+
+ // create
+ Order newOrder = createOrder("MyTestOrder", "Test Name", "TestType"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
+ try (StrolchTransaction tx = getPersistenceHandler().openTx();) {
+ tx.getOrderDao().save(newOrder);
+ }
+ }
+
+ public void runCrudTests() {
+
+ // create
+ Order newOrder = createOrder(ID, NAME, TYPE);
+ try (StrolchTransaction tx = getPersistenceHandler().openTx();) {
+ tx.getOrderDao().save(newOrder);
+ }
+
+ // read
+ Order readOrder = null;
+ try (StrolchTransaction tx = getPersistenceHandler().openTx();) {
+ readOrder = tx.getOrderDao().queryBy(TYPE, ID);
+ }
+ assertNotNull("Should read Order with id " + ID, readOrder);
+
+ // update
+ Parameter sParam = readOrder.getParameter(BAG_ID, PARAM_STRING_ID);
+ String newStringValue = "Giddiya!";
+ sParam.setValue(newStringValue);
+ try (StrolchTransaction tx = getPersistenceHandler().openTx();) {
+ tx.getOrderDao().update(readOrder);
+ }
+
+ // read updated
+ Order updatedOrder = null;
+ try (StrolchTransaction tx = getPersistenceHandler().openTx();) {
+ updatedOrder = tx.getOrderDao().queryBy(TYPE, ID);
+ }
+ assertNotNull("Should read Order with id " + ID, updatedOrder);
+ assertFalse("Objects can't be the same reference after re-reading!", readOrder == updatedOrder);
+ Parameter updatedParam = readOrder.getParameter(BAG_ID, PARAM_STRING_ID);
+ assertEquals(newStringValue, updatedParam.getValue());
+
+ // delete
+ try (StrolchTransaction tx = getPersistenceHandler().openTx();) {
+ tx.getOrderDao().remove(readOrder);
+ }
+
+ // fail to re-read
+ try (StrolchTransaction tx = getPersistenceHandler().openTx();) {
+ Order order = tx.getOrderDao().queryBy(TYPE, ID);
+ assertNull("Should no read Order with id " + ID, order);
+ }
+ }
+
+ public void runBulkOperationTests() {
+
+ List orders = new ArrayList<>();
+ orders.addAll(createOrders(orders.size(), 5, "@", "My Order ", "MyType1"));
+ orders.addAll(createOrders(orders.size(), 5, "@", "Other Order ", "MyType2"));
+ orders.addAll(createOrders(orders.size(), 5, "@", "Further Order ", "MyType3"));
+
+ Comparator comparator = new Comparator() {
+ @Override
+ public int compare(Order o1, Order o2) {
+ return o1.getId().compareTo(o2.getId());
+ }
+ };
+ Collections.sort(orders, comparator);
+
+ try (StrolchTransaction tx = getPersistenceHandler().openTx()) {
+ tx.getOrderDao().removeAll(tx.getOrderDao().queryAll());
+ }
+
+ try (StrolchTransaction tx = getPersistenceHandler().openTx()) {
+ tx.getOrderDao().saveAll(orders);
+ }
+
+ Set expectedTypes = new HashSet<>();
+ expectedTypes.add("MyType1");
+ expectedTypes.add("MyType2");
+ expectedTypes.add("MyType3");
+
+ try (StrolchTransaction tx = getPersistenceHandler().openTx()) {
+ List allOrders = tx.getOrderDao().queryAll();
+ Collections.sort(allOrders, comparator);
+ assertEquals(orders, allOrders);
+ }
+
+ try (StrolchTransaction tx = getPersistenceHandler().openTx()) {
+ OrderDao orderDao = tx.getOrderDao();
+
+ Set types = orderDao.queryTypes();
+ assertEquals(expectedTypes, types);
+
+ Set keySet = orderDao.queryKeySet();
+ assertEquals(15, keySet.size());
+
+ for (String type : types) {
+ Set idsByType = orderDao.queryKeySet(type);
+ assertEquals(5, idsByType.size());
+
+ List ordersByType = orderDao.queryAll(type);
+ assertEquals(5, ordersByType.size());
+ }
+ }
+
+ try (StrolchTransaction tx = getPersistenceHandler().openTx()) {
+ Order order = tx.getOrderDao().queryBy("MyType1", "@_00000001");
+ assertNotNull(order);
+ order = tx.getOrderDao().queryBy("MyType2", "@_00000006");
+ assertNotNull(order);
+ order = tx.getOrderDao().queryBy("MyType3", "@_00000011");
+ assertNotNull(order);
+ }
+ }
+}
diff --git a/src/main/java/li/strolch/testbase/runtime/ResourceModelTestRunner.java b/src/main/java/li/strolch/testbase/runtime/ResourceModelTestRunner.java
new file mode 100644
index 000000000..6d46c79b8
--- /dev/null
+++ b/src/main/java/li/strolch/testbase/runtime/ResourceModelTestRunner.java
@@ -0,0 +1,157 @@
+package li.strolch.testbase.runtime;
+
+import static li.strolch.model.ModelGenerator.BAG_ID;
+import static li.strolch.model.ModelGenerator.PARAM_STRING_ID;
+import static li.strolch.model.ModelGenerator.createResource;
+import static li.strolch.model.ModelGenerator.createResources;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import li.strolch.model.Resource;
+import li.strolch.model.parameter.Parameter;
+import li.strolch.persistence.api.PersistenceHandler;
+import li.strolch.persistence.api.ResourceDao;
+import li.strolch.persistence.api.StrolchTransaction;
+
+@SuppressWarnings("nls")
+public class ResourceModelTestRunner {
+
+ private static final String ID = "@testResource"; //$NON-NLS-1$
+ private static final String NAME = "Test Resource"; //$NON-NLS-1$
+ private static final String TYPE = "Box"; //$NON-NLS-1$
+
+ private RuntimeMock runtimeMock;
+
+ public ResourceModelTestRunner(RuntimeMock runtimeMock) {
+ this.runtimeMock = runtimeMock;
+ }
+
+ protected PersistenceHandler getPersistenceHandler() {
+ return this.runtimeMock.getPersistenceHandler();
+ }
+
+ public void runCreateResourceTest() {
+
+ // create
+ Resource newResource = createResource("MyTestResource", "Test Name", "TestType"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
+ try (StrolchTransaction tx = getPersistenceHandler().openTx();) {
+ tx.getResourceDao().save(newResource);
+ }
+ }
+
+ public void runCrudTests() {
+
+ // create
+ Resource newResource = createResource(ID, NAME, TYPE);
+ try (StrolchTransaction tx = getPersistenceHandler().openTx();) {
+ tx.getResourceDao().save(newResource);
+ }
+
+ // read
+ Resource readResource = null;
+ try (StrolchTransaction tx = getPersistenceHandler().openTx();) {
+ readResource = tx.getResourceDao().queryBy(TYPE, ID);
+ }
+ assertNotNull("Should read Resource with id " + ID, readResource); //$NON-NLS-1$
+
+ // update
+ Parameter sParam = readResource.getParameter(BAG_ID, PARAM_STRING_ID);
+ String newStringValue = "Giddiya!"; //$NON-NLS-1$
+ sParam.setValue(newStringValue);
+ try (StrolchTransaction tx = getPersistenceHandler().openTx();) {
+ tx.getResourceDao().update(readResource);
+ }
+
+ // read updated
+ Resource updatedResource = null;
+ try (StrolchTransaction tx = getPersistenceHandler().openTx();) {
+ updatedResource = tx.getResourceDao().queryBy(TYPE, ID);
+ }
+ assertNotNull("Should read Resource with id " + ID, updatedResource); //$NON-NLS-1$
+ assertFalse("Objects can't be the same reference after re-reading!", readResource == updatedResource); //$NON-NLS-1$
+ Parameter updatedParam = readResource.getParameter(BAG_ID, PARAM_STRING_ID);
+ assertEquals(newStringValue, updatedParam.getValue());
+
+ // delete
+ try (StrolchTransaction tx = getPersistenceHandler().openTx();) {
+ tx.getResourceDao().remove(readResource);
+ }
+
+ // fail to re-read
+ try (StrolchTransaction tx = getPersistenceHandler().openTx();) {
+ Resource resource = tx.getResourceDao().queryBy(TYPE, ID);
+ assertNull("Should no read Resource with id " + ID, resource); //$NON-NLS-1$
+ }
+ }
+
+ public void runBulkOperationTests() {
+
+ List resources = new ArrayList<>();
+ resources.addAll(createResources(resources.size(), 5, "@", "My Resource ", "MyType1"));
+ resources.addAll(createResources(resources.size(), 5, "@", "Other Resource ", "MyType2"));
+ resources.addAll(createResources(resources.size(), 5, "@", "Further Resource ", "MyType3"));
+
+ Comparator comparator = new Comparator() {
+ @Override
+ public int compare(Resource o1, Resource o2) {
+ return o1.getId().compareTo(o2.getId());
+ }
+ };
+ Collections.sort(resources, comparator);
+
+ try (StrolchTransaction tx = getPersistenceHandler().openTx()) {
+ tx.getResourceDao().removeAll(tx.getResourceDao().queryAll());
+ }
+
+ try (StrolchTransaction tx = getPersistenceHandler().openTx()) {
+ tx.getResourceDao().saveAll(resources);
+ }
+
+ Set expectedTypes = new HashSet<>();
+ expectedTypes.add("MyType1");
+ expectedTypes.add("MyType2");
+ expectedTypes.add("MyType3");
+
+ try (StrolchTransaction tx = getPersistenceHandler().openTx()) {
+ List allResources = tx.getResourceDao().queryAll();
+ Collections.sort(allResources, comparator);
+ assertEquals(resources, allResources);
+ }
+
+ try (StrolchTransaction tx = getPersistenceHandler().openTx()) {
+ ResourceDao resourceDao = tx.getResourceDao();
+
+ Set types = resourceDao.queryTypes();
+ assertEquals(expectedTypes, types);
+
+ Set keySet = resourceDao.queryKeySet();
+ assertEquals(15, keySet.size());
+
+ for (String type : types) {
+ Set idsByType = resourceDao.queryKeySet(type);
+ assertEquals(5, idsByType.size());
+
+ List resourcesByType = resourceDao.queryAll(type);
+ assertEquals(5, resourcesByType.size());
+ }
+ }
+
+ try (StrolchTransaction tx = getPersistenceHandler().openTx()) {
+ Resource resource = tx.getResourceDao().queryBy("MyType1", "@_00000001");
+ assertNotNull(resource);
+ resource = tx.getResourceDao().queryBy("MyType2", "@_00000006");
+ assertNotNull(resource);
+ resource = tx.getResourceDao().queryBy("MyType3", "@_00000011");
+ assertNotNull(resource);
+ }
+ }
+}
diff --git a/src/main/java/li/strolch/testbase/runtime/RuntimeMock.java b/src/main/java/li/strolch/testbase/runtime/RuntimeMock.java
index adc91d4bc..f63f1f64e 100644
--- a/src/main/java/li/strolch/testbase/runtime/RuntimeMock.java
+++ b/src/main/java/li/strolch/testbase/runtime/RuntimeMock.java
@@ -29,30 +29,31 @@ import org.slf4j.LoggerFactory;
import ch.eitchnet.utils.helper.FileHelper;
-public class RuntimeMock {
+public final class RuntimeMock {
private static final Logger logger = LoggerFactory.getLogger(RuntimeMock.class);
private static final String TARGET = "target"; //$NON-NLS-1$
- private static ComponentContainer container;
- private static StrolchAgent agent;
+ private ComponentContainer container;
+ private StrolchAgent agent;
- /**
- * @return the container
- */
- public static ComponentContainer getContainer() {
- return container;
+ public ComponentContainer getContainer() {
+ return this.container;
}
- public static StrolchPrivilegeHandler getPrivilegeHandler() {
- return container.getComponent(StrolchPrivilegeHandler.class);
+ public StrolchAgent getAgent() {
+ return this.agent;
}
- public static PersistenceHandler getPersistenceHandler() {
- return container.getComponent(PersistenceHandler.class);
+ public StrolchPrivilegeHandler getPrivilegeHandler() {
+ return this.container.getComponent(StrolchPrivilegeHandler.class);
}
- public static void mockRuntime(File rootPathF, File configSrc) {
+ public PersistenceHandler getPersistenceHandler() {
+ return this.container.getComponent(PersistenceHandler.class);
+ }
+
+ public void mockRuntime(File rootPathF, File configSrc) {
if (!rootPathF.getParentFile().getName().equals(TARGET)) {
String msg = "Mocking path must be in a maven target: {0}"; //$NON-NLS-1$
@@ -90,7 +91,7 @@ public class RuntimeMock {
}
}
- public static void startContainer(File rootPathF) {
+ public void startContainer(File rootPathF) {
try {
StrolchAgent agent = new StrolchAgent();
@@ -98,8 +99,8 @@ public class RuntimeMock {
agent.initialize();
agent.start();
- RuntimeMock.agent = agent;
- RuntimeMock.container = agent.getContainer();
+ this.agent = agent;
+ this.container = agent.getContainer();
} catch (Exception e) {
logger.error("Failed to start mocked container due to: " + e.getMessage(), e); //$NON-NLS-1$
@@ -108,19 +109,19 @@ public class RuntimeMock {
}
}
- public static void destroyRuntime() {
+ public void destroyRuntime() {
- if (agent == null)
+ if (this.agent == null)
return;
try {
- agent.stop();
+ this.agent.stop();
} catch (Exception e) {
logger.info("Failed to stop container: " + e.getMessage()); //$NON-NLS-1$
}
try {
- agent.destroy();
+ this.agent.destroy();
} catch (Exception e) {
logger.info("Failed to destroy container: " + e.getMessage()); //$NON-NLS-1$
}