[Major] Only use tx.get*Map() if you really need it!

This commit is contained in:
Robert von Burg 2018-06-22 13:14:14 +02:00
parent bbf021f73b
commit 1c2d048c45
12 changed files with 141 additions and 163 deletions

View File

@ -1,16 +1,17 @@
package li.strolch.agent;
import static java.util.stream.Collectors.toList;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.Collectors;
import li.strolch.RuntimeMock;
import li.strolch.agent.api.StrolchAgent;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.model.AbstractStrolchElement;
import li.strolch.model.ModelGenerator;
import li.strolch.model.StrolchElement;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.privilege.model.Certificate;
import org.junit.AfterClass;
@ -95,12 +96,10 @@ public class ParallelTests {
StrolchRealm realm = runtimeMock.getAgent().getContainer().getRealm(cert);
try (StrolchTransaction tx = realm.openTx(cert, ParallelTests.class)) {
List<String> ids = tx.getResourceMap().stream(tx).map(AbstractStrolchElement::getId)
.collect(Collectors.toList());
List<String> ids = tx.streamResources().map(StrolchElement::getId).collect(toList());
logger.info("There are " + ids.size() + " Resources!");
ids = tx.getResourceMap().stream(tx, "SomeType", "TestType").map(AbstractStrolchElement::getId)
.collect(Collectors.toList());
ids = tx.streamResources("SomeType", "TestType").map(StrolchElement::getId).collect(toList());
logger.info("There are " + ids.size() + " Resources of type SomeType");
}
}

View File

@ -165,7 +165,7 @@ public class QueryTest {
Resource res1 = createResource("@1", "Test Resource", "MyType");
try (StrolchTransaction tx = container.getRealm(StrolchConstants.DEFAULT_REALM)
.openTx(certificate, "test")) {
tx.getResourceMap().add(tx, res1);
tx.add(res1);
tx.commitOnClose();
}
@ -193,7 +193,7 @@ public class QueryTest {
Resource res1 = createResource("@1", "Test Resource", "MyType");
try (StrolchTransaction tx = container.getRealm(StrolchConstants.DEFAULT_REALM)
.openTx(certificate, "test")) {
tx.getResourceMap().add(tx, res1);
tx.add(res1);
tx.commitOnClose();
}
@ -220,7 +220,7 @@ public class QueryTest {
Resource res1 = createResource("@1", "Test Resource", "MyType");
try (StrolchTransaction tx = container.getRealm(StrolchConstants.DEFAULT_REALM)
.openTx(certificate, "test")) {
tx.getResourceMap().add(tx, res1);
tx.add(res1);
tx.commitOnClose();
}
@ -248,7 +248,7 @@ public class QueryTest {
Resource res1 = createResource("@1", "Test Resource", "MyType");
try (StrolchTransaction tx = container.getRealm(StrolchConstants.DEFAULT_REALM)
.openTx(certificate, "test")) {
tx.getResourceMap().add(tx, res1);
tx.add(res1);
tx.commitOnClose();
}
@ -276,8 +276,8 @@ public class QueryTest {
Resource res2 = createResource("@2", "Test Resource", "MyType");
try (StrolchTransaction tx = container.getRealm(StrolchConstants.DEFAULT_REALM)
.openTx(certificate, "test")) {
tx.getResourceMap().add(tx, res1);
tx.getResourceMap().add(tx, res2);
tx.add(res1);
tx.add(res2);
tx.commitOnClose();
}

View File

@ -125,14 +125,14 @@ public class ObserverUpdateTest {
// create order
Order newOrder = createOrder("MyTestOrder", "Test Name", "TestType", new Date(), State.CREATED); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = realm.openTx(certificate, "test")) { //$NON-NLS-1$
tx.getOrderMap().add(tx, newOrder);
tx.add(newOrder);
tx.commitOnClose();
}
// create resource
Resource newResource = createResource("MyTestResource", "Test Name", "TestType"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = realm.openTx(certificate, "test");) { //$NON-NLS-1$
tx.getResourceMap().add(tx, newResource);
tx.add(newResource);
tx.commitOnClose();
}

View File

@ -21,11 +21,6 @@ import static org.junit.Assert.assertNull;
import java.io.File;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.agent.impl.DataStoreMode;
import li.strolch.model.ModelGenerator;
@ -35,6 +30,10 @@ import li.strolch.privilege.model.Certificate;
import li.strolch.runtime.privilege.PrivilegeHandler;
import li.strolch.testbase.runtime.AbstractModelTest;
import li.strolch.testbase.runtime.RuntimeMock;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class RealmTest extends AbstractModelTest {
@ -94,7 +93,7 @@ public class RealmTest extends AbstractModelTest {
}
try (StrolchTransaction tx = firstRealm.openTx(certificate, TEST)) {
Resource res = tx.getResourceMap().getBy(tx, type, expectedId1);
Resource res = tx.getResourceBy(type, expectedId1);
assertEquals("Should find object previously added in same realm!", expectedRes1, res); //$NON-NLS-1$
}
}
@ -109,7 +108,7 @@ public class RealmTest extends AbstractModelTest {
}
try (StrolchTransaction tx = secondRealm.openTx(certificate, TEST)) {
Resource res = tx.getResourceMap().getBy(tx, type, expectedId2);
Resource res = tx.getResourceBy(type, expectedId2);
assertEquals("Should find object previously added in same realm!", expectedRes2, res); //$NON-NLS-1$
}
}
@ -117,7 +116,7 @@ public class RealmTest extends AbstractModelTest {
{
StrolchRealm secondRealm = runtimeMock.getRealm(SECOND);
try (StrolchTransaction tx = secondRealm.openTx(certificate, TEST)) {
Resource res = tx.getResourceMap().getBy(tx, type, expectedId1);
Resource res = tx.getResourceBy(type, expectedId1);
assertNull("Should not find object added in differenct realm!", res); //$NON-NLS-1$
}
}

View File

@ -1,12 +1,12 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
*
* 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.
@ -71,10 +71,10 @@ public class ExistingDbTest {
Certificate certificate = privilegeHandler.authenticate(TEST, TEST.toCharArray());
try (StrolchTransaction tx = runtimeMock.getRealm(StrolchConstants.DEFAULT_REALM).openTx(certificate, TEST)) {
Resource resource = tx.getResourceMap().getBy(tx, "MyType", "@1"); //$NON-NLS-1$ //$NON-NLS-2$
Resource resource = tx.getResourceBy("MyType", "@1"); //$NON-NLS-1$ //$NON-NLS-2$
assertNotNull("Should be able to read existing element from db", resource); //$NON-NLS-1$
Order order = tx.getOrderMap().getBy(tx, "MyType", "@1"); //$NON-NLS-1$//$NON-NLS-2$
Order order = tx.getOrderBy("MyType", "@1"); //$NON-NLS-1$//$NON-NLS-2$
assertNotNull("Should be able to read existing element from db", order); //$NON-NLS-1$
}
}

View File

@ -104,12 +104,12 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
try (StrolchTransaction tx = openTx(realmName, ctx.getCertificate())) {
// iterate all activities
for (Activity activity : tx.getActivityMap().getAllElements(tx)) {
tx.streamActivities().forEach(activity -> {
// we only want to restart activities which were in execution
State state = activity.getState();
if (!state.inExecutionPhase())
continue;
return;
logger.info("Starting Execution of " + activity.getLocator() + " on realm " + realmName);
@ -125,7 +125,7 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
// register for execution
this.registeredActivities.addElement(realmName, activity.getLocator());
}
});
// commit changes to state
tx.commitOnClose();

View File

@ -439,11 +439,15 @@ public class GenericReport extends ReportPolicy {
case StrolchConstants.INTERPRETATION_RESOURCE_REF:
return tx().getResourceMap().getElementsBy(tx(), objectTypeP.getUom()).stream();
return tx().streamResources(objectTypeP.getUom());
case StrolchConstants.INTERPRETATION_ORDER_REF:
return tx().getOrderMap().getElementsBy(tx(), objectTypeP.getUom()).stream();
return tx().streamOrders(objectTypeP.getUom());
case StrolchConstants.INTERPRETATION_ACTIVITY_REF:
return tx().streamActivities(objectTypeP.getUom());
default:

View File

@ -1,16 +1,7 @@
package li.strolch.command;
import static li.strolch.service.test.AbstractRealmServiceTest.CONFIG_SRC;
import static li.strolch.service.test.AbstractRealmServiceTest.REALM_CACHED;
import static li.strolch.service.test.AbstractRealmServiceTest.REALM_TRANSIENT;
import static li.strolch.service.test.AbstractRealmServiceTest.dropSchema;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.BeforeClass;
import org.junit.Test;
import static li.strolch.service.test.AbstractRealmServiceTest.*;
import static org.junit.Assert.*;
import li.strolch.model.ModelGenerator;
import li.strolch.model.Order;
@ -22,6 +13,8 @@ import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.privilege.base.AccessDeniedException;
import li.strolch.privilege.model.Certificate;
import li.strolch.testbase.runtime.RuntimeMock;
import org.junit.BeforeClass;
import org.junit.Test;
public class InMemoryTransactionTest {
@ -216,7 +209,7 @@ public class InMemoryTransactionTest {
// should exist
try (StrolchTransaction tx = openTx(realmName)) {
assertTrue("Resource should exist!", tx.getResourceMap().hasElement(tx, type, id));
assertTrue("Resource should exist!", tx.hasResource(type, id));
}
// update
@ -251,7 +244,7 @@ public class InMemoryTransactionTest {
// should not exist
try (StrolchTransaction tx = openTx(realmName)) {
assertFalse("Resource should not exist!", tx.getResourceMap().hasElement(tx, type, id));
assertFalse("Resource should not exist!", tx.hasResource(type, id));
}
// create again
@ -263,7 +256,7 @@ public class InMemoryTransactionTest {
// should exist
try (StrolchTransaction tx = openTx(realmName)) {
assertTrue("Resource should exist!", tx.getResourceMap().hasElement(tx, type, id));
assertTrue("Resource should exist!", tx.hasResource(type, id));
}
}
@ -284,7 +277,7 @@ public class InMemoryTransactionTest {
// should exist
try (StrolchTransaction tx = openTx(realmName)) {
assertTrue("Order should exist!", tx.getOrderMap().hasElement(tx, type, id));
assertTrue("Order should exist!", tx.hasOrder(type, id));
}
// update
@ -319,7 +312,7 @@ public class InMemoryTransactionTest {
// should not exist
try (StrolchTransaction tx = openTx(realmName)) {
assertFalse("Order should not exist!", tx.getOrderMap().hasElement(tx, type, id));
assertFalse("Order should not exist!", tx.hasOrder(type, id));
}
// create again
@ -331,7 +324,7 @@ public class InMemoryTransactionTest {
// should exist
try (StrolchTransaction tx = openTx(realmName)) {
assertTrue("Order should exist!", tx.getOrderMap().hasElement(tx, type, id));
assertTrue("Order should exist!", tx.hasOrder(type, id));
}
}
@ -352,7 +345,7 @@ public class InMemoryTransactionTest {
// should exist
try (StrolchTransaction tx = openTx(realmName)) {
assertTrue("Activity should exist!", tx.getActivityMap().hasElement(tx, type, id));
assertTrue("Activity should exist!", tx.hasActivity(type, id));
}
// update
@ -387,7 +380,7 @@ public class InMemoryTransactionTest {
// should not exist
try (StrolchTransaction tx = openTx(realmName)) {
assertFalse("Activity should not exist!", tx.getActivityMap().hasElement(tx, type, id));
assertFalse("Activity should not exist!", tx.hasActivity(type, id));
}
// create again
@ -399,7 +392,7 @@ public class InMemoryTransactionTest {
// should exist
try (StrolchTransaction tx = openTx(realmName)) {
assertTrue("Activity should exist!", tx.getActivityMap().hasElement(tx, type, id));
assertTrue("Activity should exist!", tx.hasActivity(type, id));
}
}
@ -418,7 +411,7 @@ public class InMemoryTransactionTest {
// should exist
try (StrolchTransaction tx = openTx(realmName)) {
assertTrue("Resource should exist!", tx.getResourceMap().hasElement(tx, type, id));
assertTrue("Resource should exist!", tx.hasResource(type, id));
}
}
@ -438,7 +431,7 @@ public class InMemoryTransactionTest {
// should not exist
try (StrolchTransaction tx = openTx(realmName)) {
assertFalse("Resource should not exist!", tx.getResourceMap().hasElement(tx, type, id));
assertFalse("Resource should not exist!", tx.hasResource(type, id));
}
}
@ -457,7 +450,7 @@ public class InMemoryTransactionTest {
// should exist
try (StrolchTransaction tx = openTx(realmName)) {
assertTrue("Order should exist!", tx.getOrderMap().hasElement(tx, type, id));
assertTrue("Order should exist!", tx.hasOrder(type, id));
}
}
@ -477,7 +470,7 @@ public class InMemoryTransactionTest {
// create, update and remove
try (StrolchTransaction tx = openTx(realmName)) {
assertFalse("Order should not exist!", tx.getOrderMap().hasElement(tx, type, id));
assertFalse("Order should not exist!", tx.hasOrder(type, id));
}
}
@ -496,7 +489,7 @@ public class InMemoryTransactionTest {
// should exist
try (StrolchTransaction tx = openTx(realmName)) {
assertTrue("Activity should exist!", tx.getActivityMap().hasElement(tx, type, id));
assertTrue("Activity should exist!", tx.hasActivity(type, id));
}
}
@ -516,7 +509,7 @@ public class InMemoryTransactionTest {
// should not exist
try (StrolchTransaction tx = openTx(realmName)) {
assertFalse("Activity should not exist!", tx.getActivityMap().hasElement(tx, type, id));
assertFalse("Activity should not exist!", tx.hasActivity(type, id));
}
}
}

View File

@ -3,8 +3,12 @@ package li.strolch.soql.core;
import java.util.*;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.Tags;
import li.strolch.model.query.StrolchElementQuery;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.search.ActivitySearch;
import li.strolch.search.OrderSearch;
import li.strolch.search.ResourceSearch;
import li.strolch.soql.antlr4.generated.SOQLLexer;
import li.strolch.soql.antlr4.generated.SOQLParser;
import org.antlr.v4.runtime.CharStream;
@ -14,10 +18,9 @@ import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
/**
* Main class to process a SOQL query. It performs the queries to retrieve the
* input objects as defined in the FROM clause and evaluates the compiled
* statement on any cartesian product, returning the objects defined in the
* SELECT clause.
* Main class to process a SOQL query. It performs the queries to retrieve the input objects as defined in the FROM
* clause and evaluates the compiled statement on any cartesian product, returning the objects defined in the SELECT
* clause.
*
* @author msmock
*/
@ -30,8 +33,7 @@ public class QueryProcessor {
private Map<String, List<? extends StrolchRootElement>> inputCollections;
/**
* Set the input map of collections to take the input objects from. For testing
* purposes only.
* Set the input map of collections to take the input objects from. For testing purposes only.
*
* @param inputCollections
* the input data
@ -95,8 +97,7 @@ public class QueryProcessor {
* @param keys
* the keys
*
* @return List of Lists of the elements to be taken as input for the compiled
* statement
* @return List of Lists of the elements to be taken as input for the compiled statement
*/
private List<List<StrolchRootElement>> buildCartesianProduct(
Map<String, List<? extends StrolchRootElement>> inputCollections, Object[] keys) {
@ -187,16 +188,16 @@ public class QueryProcessor {
String clazzKey = entities.get(key);
switch (clazzKey) {
case "Resource":
result.put(key, tx.getResourceMap().getAllElements(tx));
case Tags.RESOURCE:
result.put(key, new ResourceSearch().search(tx).toList());
break;
case "Order":
result.put(key, tx.getOrderMap().getAllElements(tx));
case Tags.ORDER:
result.put(key, new OrderSearch().search(tx).toList());
break;
case "Activity":
result.put(key, tx.getActivityMap().getAllElements(tx));
case Tags.ACTIVITY:
result.put(key, new ActivitySearch().search(tx).toList());
break;
default:

View File

@ -55,7 +55,7 @@ public class ActivityModelTestRunner {
Activity newActivity = createActivity("MyTestActivity", "Test Name", "TestType",
TimeOrdering.SERIES); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test")) {
tx.getActivityMap().add(tx, newActivity);
tx.add(newActivity);
tx.commitOnClose();
}
}
@ -76,9 +76,9 @@ public class ActivityModelTestRunner {
Activity activity3 = createActivity("myTestActivity3", "Test Name", "QTestType3",
TimeOrdering.SERIES); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test")) {
tx.getActivityMap().add(tx, activity1);
tx.getActivityMap().add(tx, activity2);
tx.getActivityMap().add(tx, activity3);
tx.add(activity1);
tx.add(activity2);
tx.add(activity3);
tx.commitOnClose();
}
@ -106,14 +106,14 @@ public class ActivityModelTestRunner {
// create
Activity newActivity = createActivity(ID, NAME, TYPE, TimeOrdering.SERIES);
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test")) {
tx.getActivityMap().add(tx, newActivity);
tx.add(newActivity);
tx.commitOnClose();
}
// read
Activity readActivity;
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test")) {
readActivity = tx.getActivityMap().getBy(tx, TYPE, ID);
readActivity = tx.getActivityBy(TYPE, ID);
}
assertNotNull("Should read Activity with id " + ID, readActivity);
@ -122,14 +122,14 @@ public class ActivityModelTestRunner {
String newStringValue = "Giddiya!";
sParam.setValue(newStringValue);
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test")) {
tx.getActivityMap().update(tx, readActivity);
tx.update(readActivity);
tx.commitOnClose();
}
// read updated
Activity updatedActivity;
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test")) {
updatedActivity = tx.getActivityMap().getBy(tx, TYPE, ID);
updatedActivity = tx.getActivityBy(TYPE, ID);
}
assertNotNull("Should read Activity with id " + ID, updatedActivity);
if (this.runtimeMock.getRealm(this.realmName).getMode() != DataStoreMode.CACHED)
@ -139,13 +139,13 @@ public class ActivityModelTestRunner {
// delete
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test")) {
tx.getActivityMap().remove(tx, readActivity);
tx.remove(readActivity);
tx.commitOnClose();
}
// fail to re-read
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test")) {
Activity activity = tx.getActivityMap().getBy(tx, TYPE, ID);
Activity activity = tx.getActivityBy(TYPE, ID);
assertNull("Should no read Activity with id " + ID, activity);
}
}
@ -260,11 +260,11 @@ public class ActivityModelTestRunner {
}
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test")) {
Activity activity = tx.getActivityMap().getBy(tx, "MyType1", "@00000001");
Activity activity = tx.getActivityBy("MyType1", "@00000001");
assertNotNull(activity);
activity = tx.getActivityMap().getBy(tx, "MyType2", "@00000006");
activity = tx.getActivityBy("MyType2", "@00000006");
assertNotNull(activity);
activity = tx.getActivityMap().getBy(tx, "MyType3", "@00000011");
activity = tx.getActivityBy("MyType3", "@00000011");
assertNotNull(activity);
}
}

View File

@ -1,12 +1,12 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
*
* 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.
@ -15,26 +15,16 @@
*/
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 static li.strolch.model.ModelGenerator.*;
import static org.junit.Assert.*;
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 java.util.*;
import li.strolch.agent.api.OrderMap;
import li.strolch.agent.impl.DataStoreMode;
import li.strolch.model.Order;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.StrolchElement;
import li.strolch.model.parameter.StringParameter;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.privilege.model.Certificate;
import li.strolch.runtime.privilege.PrivilegeHandler;
@ -63,7 +53,7 @@ public class OrderModelTestRunner {
// create
Order newOrder = createOrder("MyTestOrder", "Test Name", "TestType"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test");) {
tx.getOrderMap().add(tx, newOrder);
tx.add(newOrder);
tx.commitOnClose();
}
}
@ -85,9 +75,9 @@ public class OrderModelTestRunner {
Order order2 = createOrder("myTestOrder2", "Test Name", "QTestType2"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
Order order3 = createOrder("myTestOrder3", "Test Name", "QTestType3"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test");) {
tx.getOrderMap().add(tx, order1);
tx.getOrderMap().add(tx, order2);
tx.getOrderMap().add(tx, order3);
tx.add(order1);
tx.add(order2);
tx.add(order3);
tx.commitOnClose();
}
@ -115,46 +105,46 @@ public class OrderModelTestRunner {
// create
Order newOrder = createOrder(ID, NAME, TYPE);
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test");) {
tx.getOrderMap().add(tx, newOrder);
tx.add(newOrder);
tx.commitOnClose();
}
// read
Order readOrder = null;
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test");) {
readOrder = tx.getOrderMap().getBy(tx, TYPE, ID);
readOrder = tx.getOrderBy(TYPE, ID);
}
assertNotNull("Should read Order with id " + ID, readOrder);
// update
Parameter<String> sParam = readOrder.getParameter(BAG_ID, PARAM_STRING_ID);
StringParameter sParam = readOrder.getParameter(BAG_ID, PARAM_STRING_ID);
String newStringValue = "Giddiya!";
sParam.setValue(newStringValue);
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test");) {
tx.getOrderMap().update(tx, readOrder);
tx.update(readOrder);
tx.commitOnClose();
}
// read updated
Order updatedOrder = null;
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test");) {
updatedOrder = tx.getOrderMap().getBy(tx, TYPE, ID);
updatedOrder = tx.getOrderBy(TYPE, ID);
}
assertNotNull("Should read Order with id " + ID, updatedOrder);
if (this.runtimeMock.getRealm(this.realmName).getMode() != DataStoreMode.CACHED)
assertFalse("Objects can't be the same reference after re-reading!", readOrder == updatedOrder);
Parameter<String> updatedParam = readOrder.getParameter(BAG_ID, PARAM_STRING_ID);
assertNotSame("Objects can't be the same reference after re-reading!", readOrder, updatedOrder);
StringParameter updatedParam = readOrder.getParameter(BAG_ID, PARAM_STRING_ID);
assertEquals(newStringValue, updatedParam.getValue());
// delete
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test");) {
tx.getOrderMap().remove(tx, readOrder);
tx.remove(readOrder);
tx.commitOnClose();
}
// fail to re-read
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test");) {
Order order = tx.getOrderMap().getBy(tx, TYPE, ID);
Order order = tx.getOrderBy(TYPE, ID);
assertNull("Should not read Order with id " + ID, order);
}
}
@ -168,8 +158,7 @@ public class OrderModelTestRunner {
orders.addAll(createOrders(orders.size(), 5, "@", "Further Order", "MyType3"));
// sort them so we know which order our objects are
Comparator<Order> comparator = (o1, o2) -> o1.getId().compareTo(o2.getId());
Collections.sort(orders, comparator);
orders.sort(Comparator.comparing(StrolchElement::getId));
// first clear the map, so that we have a clean state
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test")) {
@ -244,7 +233,7 @@ public class OrderModelTestRunner {
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test")) {
List<Order> allOrders = tx.getOrderMap().getAllElements(tx);
Collections.sort(allOrders, comparator);
allOrders.sort(Comparator.comparing(StrolchElement::getId));
assertEquals(orders, allOrders);
}
@ -267,11 +256,11 @@ public class OrderModelTestRunner {
}
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test")) {
Order order = tx.getOrderMap().getBy(tx, "MyType1", "@00000001");
Order order = tx.getOrderBy("MyType1", "@00000001");
assertNotNull(order);
order = tx.getOrderMap().getBy(tx, "MyType2", "@00000006");
order = tx.getOrderBy("MyType2", "@00000006");
assertNotNull(order);
order = tx.getOrderMap().getBy(tx, "MyType3", "@00000011");
order = tx.getOrderBy("MyType3", "@00000011");
assertNotNull(order);
}
}

View File

@ -1,12 +1,12 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
*
* 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.
@ -15,26 +15,16 @@
*/
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 static li.strolch.model.ModelGenerator.*;
import static org.junit.Assert.*;
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 java.util.*;
import li.strolch.agent.api.ResourceMap;
import li.strolch.agent.impl.DataStoreMode;
import li.strolch.model.Resource;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.StrolchElement;
import li.strolch.model.parameter.StringParameter;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.privilege.model.Certificate;
import li.strolch.runtime.privilege.PrivilegeHandler;
@ -61,9 +51,10 @@ public class ResourceModelTestRunner {
public void runCreateResourceTest() {
// create
Resource newResource = createResource("MyTestResource", "Test Name", "TestType"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
Resource newResource = createResource("MyTestResource", "Test Name",
"TestType"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test");) {
tx.getResourceMap().add(tx, newResource);
tx.add(newResource);
tx.commitOnClose();
}
}
@ -77,13 +68,16 @@ public class ResourceModelTestRunner {
}
// create three resources
Resource resource1 = createResource("myTestResource1", "Test Name", "QTestType1"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
Resource resource2 = createResource("myTestResource2", "Test Name", "QTestType2"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
Resource resource3 = createResource("myTestResource3", "Test Name", "QTestType3"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
Resource resource1 = createResource("myTestResource1", "Test Name",
"QTestType1"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
Resource resource2 = createResource("myTestResource2", "Test Name",
"QTestType2"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
Resource resource3 = createResource("myTestResource3", "Test Name",
"QTestType3"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test");) {
tx.getResourceMap().add(tx, resource1);
tx.getResourceMap().add(tx, resource2);
tx.getResourceMap().add(tx, resource3);
tx.add(resource1);
tx.add(resource2);
tx.add(resource3);
tx.commitOnClose();
}
@ -111,19 +105,19 @@ public class ResourceModelTestRunner {
// create
Resource newResource = createResource(ID, NAME, TYPE);
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test");) {
tx.getResourceMap().add(tx, newResource);
tx.add(newResource);
tx.commitOnClose();
}
// read
Resource readResource = null;
Resource readResource;
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test");) {
readResource = tx.getResourceMap().getBy(tx, TYPE, ID);
readResource = tx.getResourceBy(TYPE, ID);
}
assertNotNull("Should read Resource with id " + ID, readResource); //$NON-NLS-1$
// update
Parameter<String> sParam = readResource.getParameter(BAG_ID, PARAM_STRING_ID);
StringParameter sParam = readResource.getParameter(BAG_ID, PARAM_STRING_ID);
String newStringValue = "Giddiya!"; //$NON-NLS-1$
sParam.setValue(newStringValue);
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test");) {
@ -132,14 +126,14 @@ public class ResourceModelTestRunner {
}
// read updated
Resource updatedResource = null;
Resource updatedResource;
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test");) {
updatedResource = tx.getResourceMap().getBy(tx, TYPE, ID);
updatedResource = tx.getResourceBy(TYPE, ID);
}
assertNotNull("Should read Resource with id " + ID, updatedResource); //$NON-NLS-1$
if (this.runtimeMock.getRealm(this.realmName).getMode() != DataStoreMode.CACHED)
assertFalse("Objects can't be the same reference after re-reading!", readResource == updatedResource); //$NON-NLS-1$
Parameter<String> updatedParam = readResource.getParameter(BAG_ID, PARAM_STRING_ID);
assertNotSame("Objects can't be the same reference after re-reading!", readResource, updatedResource); //$NON-NLS-1$
StringParameter updatedParam = readResource.getParameter(BAG_ID, PARAM_STRING_ID);
assertEquals(newStringValue, updatedParam.getValue());
// delete
@ -150,7 +144,7 @@ public class ResourceModelTestRunner {
// fail to re-read
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test");) {
Resource resource = tx.getResourceMap().getBy(tx, TYPE, ID);
Resource resource = tx.getResourceBy(TYPE, ID);
assertNull("Should no read Resource with id " + ID, resource); //$NON-NLS-1$
}
}
@ -164,8 +158,7 @@ public class ResourceModelTestRunner {
resources.addAll(createResources(resources.size(), 5, "@", "Further Resource", "MyType3"));
// sort them so we know which order our objects are
Comparator<Resource> comparator = (o1, o2) -> o1.getId().compareTo(o2.getId());
Collections.sort(resources, comparator);
resources.sort(Comparator.comparing(StrolchElement::getId));
// first clear the map, so that we have a clean state
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test")) {
@ -240,7 +233,7 @@ public class ResourceModelTestRunner {
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test")) {
List<Resource> allResources = tx.getResourceMap().getAllElements(tx);
Collections.sort(allResources, comparator);
allResources.sort(Comparator.comparing(StrolchElement::getId));
assertEquals(resources, allResources);
}
@ -263,11 +256,11 @@ public class ResourceModelTestRunner {
}
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(this.certificate, "test")) {
Resource resource = tx.getResourceMap().getBy(tx, "MyType1", "@00000001");
Resource resource = tx.getResourceBy("MyType1", "@00000001");
assertNotNull(resource);
resource = tx.getResourceMap().getBy(tx, "MyType2", "@00000006");
resource = tx.getResourceBy("MyType2", "@00000006");
assertNotNull(resource);
resource = tx.getResourceMap().getBy(tx, "MyType3", "@00000011");
resource = tx.getResourceBy("MyType3", "@00000011");
assertNotNull(resource);
}
}