[New] Implemented opt-in audit trail in Strolch
The audit trail has its own map on the Realm and a trail is written by realm at the end of the transaction. You can write your own audit trail using tx.getAuditTrail(). Enable the audit trail by setting the realm configuration value 'enableAuditTrail'.
This commit is contained in:
parent
54c9038c9f
commit
cc9cfcadba
|
@ -2,9 +2,12 @@ package li.strolch.testbase.runtime;
|
|||
|
||||
import li.strolch.persistence.api.StrolchTransaction;
|
||||
import li.strolch.runtime.StrolchConstants;
|
||||
import li.strolch.runtime.privilege.PrivilegeHandler;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import ch.eitchnet.privilege.model.Certificate;
|
||||
|
||||
public abstract class AbstractModelTest {
|
||||
|
||||
protected abstract RuntimeMock getRuntimeMock();
|
||||
|
@ -13,7 +16,9 @@ public abstract class AbstractModelTest {
|
|||
|
||||
@Test
|
||||
public void shouldStartContainer() {
|
||||
try (StrolchTransaction tx = getRuntimeMock().getRealm(this.realmName).openTx()) {
|
||||
PrivilegeHandler privilegeHandler = getRuntimeMock().getContainer().getPrivilegeHandler();
|
||||
Certificate certificate = privilegeHandler.authenticate("test", "test".getBytes());
|
||||
try (StrolchTransaction tx = getRuntimeMock().getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
tx.getOrderMap().getAllKeys(tx);
|
||||
}
|
||||
}
|
||||
|
@ -73,4 +78,10 @@ public abstract class AbstractModelTest {
|
|||
ResourceModelTestRunner testRunner = new ResourceModelTestRunner(getRuntimeMock(), this.realmName);
|
||||
testRunner.runBulkOperationTests();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldTestAudits() {
|
||||
AuditModelTestRunner testRunner = new AuditModelTestRunner(getRuntimeMock(), this.realmName);
|
||||
testRunner.runTestForAudits();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,319 @@
|
|||
/*
|
||||
* 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.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package li.strolch.testbase.runtime;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import li.strolch.agent.api.AuditTrail;
|
||||
import li.strolch.agent.api.StrolchRealm;
|
||||
import li.strolch.model.ModelGenerator;
|
||||
import li.strolch.model.audit.Audit;
|
||||
import li.strolch.persistence.api.StrolchTransaction;
|
||||
import li.strolch.runtime.privilege.PrivilegeHandler;
|
||||
import ch.eitchnet.privilege.model.Certificate;
|
||||
import ch.eitchnet.utils.collections.DateRange;
|
||||
|
||||
/**
|
||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class AuditModelTestRunner {
|
||||
|
||||
private RuntimeMock runtimeMock;
|
||||
private String realmName;
|
||||
private Certificate certificate;
|
||||
|
||||
/**
|
||||
* @param runtimeMock
|
||||
* @param realmName
|
||||
*/
|
||||
public AuditModelTestRunner(RuntimeMock runtimeMock, String realmName) {
|
||||
this.runtimeMock = runtimeMock;
|
||||
this.realmName = realmName;
|
||||
|
||||
PrivilegeHandler privilegeHandler = runtimeMock.getContainer().getPrivilegeHandler();
|
||||
certificate = privilegeHandler.authenticate("test", "test".getBytes());
|
||||
}
|
||||
|
||||
public void runTestForAudits() {
|
||||
|
||||
StrolchRealm realm = runtimeMock.getRealm(realmName);
|
||||
|
||||
Calendar cal = Calendar.getInstance();
|
||||
|
||||
cal.set(2000, 6, 1);
|
||||
Date beforeCurrent = cal.getTime();
|
||||
cal.set(2000, 6, 2);
|
||||
Date current = cal.getTime();
|
||||
cal.set(2000, 6, 3);
|
||||
Date afterCurrent = cal.getTime();
|
||||
cal.set(2000, 1, 1);
|
||||
Date earlier = cal.getTime();
|
||||
cal.set(2000, 11, 1);
|
||||
Date later = cal.getTime();
|
||||
|
||||
DateRange earlierRange = new DateRange().from(earlier, true).to(beforeCurrent, true);
|
||||
DateRange equalsRange = new DateRange().from(current, true).to(current, true);
|
||||
DateRange laterRange = new DateRange().from(afterCurrent, true).to(later, true);
|
||||
DateRange containsRange = new DateRange().from(earlier, true).to(later, true);
|
||||
|
||||
// first cleanup
|
||||
cleanup(realm);
|
||||
|
||||
// single element actions
|
||||
{
|
||||
Audit audit = ModelGenerator.randomAudit();
|
||||
audit.setDate(current);
|
||||
|
||||
// add
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
auditTrail.add(tx, audit);
|
||||
}
|
||||
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
Set<String> types = auditTrail.getTypes(tx);
|
||||
assertEquals(1, types.size());
|
||||
assertTrue(types.contains(audit.getElementType()));
|
||||
}
|
||||
|
||||
// has
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
assertTrue(auditTrail.hasAudit(tx, audit.getElementType(), audit.getId()));
|
||||
|
||||
Audit dbAudit = auditTrail.getBy(tx, audit.getElementType(), audit.getId());
|
||||
assertNotNull(dbAudit);
|
||||
assertEquals(audit, dbAudit);
|
||||
|
||||
dbAudit = auditTrail.getBy(tx, "Foo", audit.getId());
|
||||
assertNull(dbAudit);
|
||||
}
|
||||
|
||||
// remove
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
auditTrail.remove(tx, audit);
|
||||
}
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
Audit dbAudit = auditTrail.getBy(tx, audit.getElementType(), audit.getId());
|
||||
assertNull(dbAudit);
|
||||
}
|
||||
|
||||
// update
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
auditTrail.add(tx, audit);
|
||||
}
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
Audit dbAudit = auditTrail.getBy(tx, audit.getElementType(), audit.getId());
|
||||
dbAudit.setAction("Foo");
|
||||
auditTrail.update(tx, dbAudit);
|
||||
}
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
Audit dbAudit = auditTrail.getBy(tx, audit.getElementType(), audit.getId());
|
||||
assertEquals("Foo", dbAudit.getAction());
|
||||
}
|
||||
}
|
||||
|
||||
// querying
|
||||
{
|
||||
Audit audit = ModelGenerator.randomAudit();
|
||||
audit.setDate(current);
|
||||
|
||||
// querySize
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
assertEquals(1, auditTrail.querySize(tx, audit.getElementType(), equalsRange));
|
||||
assertEquals(1, auditTrail.querySize(tx, audit.getElementType(), containsRange));
|
||||
assertEquals(0, auditTrail.querySize(tx, audit.getElementType(), earlierRange));
|
||||
assertEquals(0, auditTrail.querySize(tx, audit.getElementType(), laterRange));
|
||||
}
|
||||
}
|
||||
|
||||
// mass element actions
|
||||
{
|
||||
List<Audit> audits = new ArrayList<>();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Audit randomAudit = ModelGenerator.randomAudit();
|
||||
randomAudit.setElementType("FooBar");
|
||||
randomAudit.setDate(current);
|
||||
audits.add(randomAudit);
|
||||
}
|
||||
Collections.sort(audits, new AuditByIdComparator());
|
||||
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
auditTrail.addAll(tx, audits);
|
||||
}
|
||||
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
assertEquals(100, auditTrail.querySize(tx, "FooBar", containsRange));
|
||||
|
||||
List<Audit> allElements = auditTrail.getAllElements(tx, "FooBar", containsRange);
|
||||
Collections.sort(allElements, new AuditByIdComparator());
|
||||
assertEquals(audits, allElements);
|
||||
|
||||
allElements = auditTrail.getAllElements(tx, "FooBar", earlierRange);
|
||||
assertEquals(0, allElements.size());
|
||||
allElements = auditTrail.getAllElements(tx, "FooBar", laterRange);
|
||||
assertEquals(0, allElements.size());
|
||||
}
|
||||
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
auditTrail.removeAll(tx, audits);
|
||||
assertEquals(0, auditTrail.querySize(tx, "FooBar", containsRange));
|
||||
}
|
||||
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
assertEquals(0, auditTrail.querySize(tx, "FooBar", containsRange));
|
||||
}
|
||||
}
|
||||
|
||||
// update all
|
||||
{
|
||||
List<Audit> audits = new ArrayList<>();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Audit randomAudit = ModelGenerator.randomAudit();
|
||||
randomAudit.setElementType("Bar");
|
||||
randomAudit.setDate(current);
|
||||
randomAudit.setAction("BarFoo");
|
||||
audits.add(randomAudit);
|
||||
}
|
||||
Collections.sort(audits, new AuditByIdComparator());
|
||||
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
auditTrail.addAll(tx, audits);
|
||||
}
|
||||
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
List<Audit> allElements = auditTrail.getAllElements(tx, "Bar", containsRange);
|
||||
Collections.sort(allElements, new AuditByIdComparator());
|
||||
assertEquals(audits, allElements);
|
||||
|
||||
for (Audit dbAudit : allElements) {
|
||||
assertEquals("BarFoo", dbAudit.getAction());
|
||||
}
|
||||
|
||||
for (Audit dbAudit : allElements) {
|
||||
dbAudit.setAction("Foo");
|
||||
}
|
||||
|
||||
auditTrail.updateAll(tx, allElements);
|
||||
}
|
||||
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
List<Audit> allElements = auditTrail.getAllElements(tx, "Bar", containsRange);
|
||||
for (Audit dbAudit : allElements) {
|
||||
assertEquals("Foo", dbAudit.getAction());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// remove all
|
||||
{
|
||||
// first cleanup
|
||||
cleanup(realm);
|
||||
|
||||
List<Audit> audits = new ArrayList<>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
Audit randomAudit = ModelGenerator.randomAudit();
|
||||
randomAudit.setDate(current);
|
||||
randomAudit.setElementType("BarBarBar");
|
||||
audits.add(randomAudit);
|
||||
}
|
||||
for (int i = 0; i < 5; i++) {
|
||||
Audit randomAudit = ModelGenerator.randomAudit();
|
||||
randomAudit.setDate(current);
|
||||
randomAudit.setElementType("FooFooFoo");
|
||||
audits.add(randomAudit);
|
||||
}
|
||||
for (int i = 0; i < 5; i++) {
|
||||
Audit randomAudit = ModelGenerator.randomAudit();
|
||||
randomAudit.setDate(current);
|
||||
randomAudit.setElementType("BarFooBar");
|
||||
audits.add(randomAudit);
|
||||
}
|
||||
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
auditTrail.addAll(tx, audits);
|
||||
}
|
||||
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
assertEquals(15, auditTrail.querySize(tx, containsRange));
|
||||
assertEquals(5, auditTrail.querySize(tx, "BarBarBar", containsRange));
|
||||
assertEquals(5, auditTrail.querySize(tx, "FooFooFoo", containsRange));
|
||||
assertEquals(5, auditTrail.querySize(tx, "BarFooBar", containsRange));
|
||||
}
|
||||
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
assertEquals(5, auditTrail.removeAll(tx, "BarBarBar", containsRange));
|
||||
assertEquals(10, auditTrail.querySize(tx, containsRange));
|
||||
|
||||
assertEquals(5, auditTrail.removeAll(tx, "FooFooFoo", containsRange));
|
||||
assertEquals(5, auditTrail.querySize(tx, containsRange));
|
||||
|
||||
assertEquals(5, auditTrail.removeAll(tx, "BarFooBar", containsRange));
|
||||
assertEquals(0, auditTrail.querySize(tx, containsRange));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanup(StrolchRealm realm) {
|
||||
|
||||
DateRange dateRange = new DateRange().from(new Date(0), true).to(new Date((long) Math.pow(2, 50)), true);
|
||||
|
||||
try (StrolchTransaction tx = realm.openTx(certificate, "test")) {
|
||||
AuditTrail auditTrail = tx.getAuditTrail();
|
||||
Set<String> types = auditTrail.getTypes(tx);
|
||||
for (String type : types) {
|
||||
auditTrail.removeAll(tx, type, dateRange);
|
||||
}
|
||||
|
||||
assertEquals(0, auditTrail.querySize(tx, dateRange));
|
||||
}
|
||||
}
|
||||
|
||||
private final class AuditByIdComparator implements Comparator<Audit> {
|
||||
@Override
|
||||
public int compare(Audit o1, Audit o2) {
|
||||
return o1.getId().compareTo(o2.getId());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -21,6 +21,8 @@ import li.strolch.agent.impl.DataStoreMode;
|
|||
import li.strolch.model.Order;
|
||||
import li.strolch.model.parameter.Parameter;
|
||||
import li.strolch.persistence.api.StrolchTransaction;
|
||||
import li.strolch.runtime.privilege.PrivilegeHandler;
|
||||
import ch.eitchnet.privilege.model.Certificate;
|
||||
|
||||
@SuppressWarnings("nls")
|
||||
public class OrderModelTestRunner {
|
||||
|
@ -31,17 +33,21 @@ public class OrderModelTestRunner {
|
|||
|
||||
private RuntimeMock runtimeMock;
|
||||
private String realmName;
|
||||
private Certificate certificate;
|
||||
|
||||
public OrderModelTestRunner(RuntimeMock runtimeMock, String realmName) {
|
||||
this.runtimeMock = runtimeMock;
|
||||
this.realmName = realmName;
|
||||
|
||||
PrivilegeHandler privilegeHandler = runtimeMock.getContainer().getPrivilegeHandler();
|
||||
certificate = privilegeHandler.authenticate("test", "test".getBytes());
|
||||
}
|
||||
|
||||
public void runCreateOrderTest() {
|
||||
|
||||
// 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();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
tx.getOrderMap().add(tx, newOrder);
|
||||
}
|
||||
}
|
||||
|
@ -49,7 +55,7 @@ public class OrderModelTestRunner {
|
|||
public void runQuerySizeTest() {
|
||||
|
||||
// remove all
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
tx.getOrderMap().removeAll(tx, tx.getOrderMap().getAllElements(tx));
|
||||
}
|
||||
|
||||
|
@ -57,14 +63,14 @@ public class OrderModelTestRunner {
|
|||
Order order1 = createOrder("myTestOrder1", "Test Name", "QTestType1"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
||||
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();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
tx.getOrderMap().add(tx, order1);
|
||||
tx.getOrderMap().add(tx, order2);
|
||||
tx.getOrderMap().add(tx, order3);
|
||||
}
|
||||
|
||||
// query size
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
long size = tx.getOrderMap().querySize(tx);
|
||||
assertEquals("Should have three objects", 3, size);
|
||||
|
||||
|
@ -86,13 +92,13 @@ public class OrderModelTestRunner {
|
|||
|
||||
// create
|
||||
Order newOrder = createOrder(ID, NAME, TYPE);
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
tx.getOrderMap().add(tx, newOrder);
|
||||
}
|
||||
|
||||
// read
|
||||
Order readOrder = null;
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
readOrder = tx.getOrderMap().getBy(tx, TYPE, ID);
|
||||
}
|
||||
assertNotNull("Should read Order with id " + ID, readOrder);
|
||||
|
@ -101,13 +107,13 @@ public class OrderModelTestRunner {
|
|||
Parameter<String> sParam = readOrder.getParameter(BAG_ID, PARAM_STRING_ID);
|
||||
String newStringValue = "Giddiya!";
|
||||
sParam.setValue(newStringValue);
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
tx.getOrderMap().update(tx, readOrder);
|
||||
}
|
||||
|
||||
// read updated
|
||||
Order updatedOrder = null;
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
updatedOrder = tx.getOrderMap().getBy(tx, TYPE, ID);
|
||||
}
|
||||
assertNotNull("Should read Order with id " + ID, updatedOrder);
|
||||
|
@ -117,12 +123,12 @@ public class OrderModelTestRunner {
|
|||
assertEquals(newStringValue, updatedParam.getValue());
|
||||
|
||||
// delete
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
tx.getOrderMap().remove(tx, readOrder);
|
||||
}
|
||||
|
||||
// fail to re-read
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
Order order = tx.getOrderMap().getBy(tx, TYPE, ID);
|
||||
assertNull("Should no read Order with id " + ID, order);
|
||||
}
|
||||
|
@ -146,57 +152,57 @@ public class OrderModelTestRunner {
|
|||
Collections.sort(orders, comparator);
|
||||
|
||||
// first clear the map, so that we have a clean state
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
OrderMap orderMap = tx.getOrderMap();
|
||||
orderMap.removeAll(tx, orderMap.getAllElements(tx));
|
||||
}
|
||||
|
||||
{
|
||||
// make sure it is empty
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
OrderMap orderMap = tx.getOrderMap();
|
||||
assertEquals(0, orderMap.querySize(tx));
|
||||
}
|
||||
|
||||
// now add some orders
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
tx.getOrderMap().addAll(tx, orders);
|
||||
}
|
||||
|
||||
// make sure we have our expected size
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
OrderMap orderMap = tx.getOrderMap();
|
||||
assertEquals(orders.size(), orderMap.querySize(tx));
|
||||
assertEquals(5, orderMap.querySize(tx, "MyType3"));
|
||||
}
|
||||
|
||||
// now use the remove all by type
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
tx.getOrderMap().removeAllBy(tx, "MyType3");
|
||||
}
|
||||
|
||||
// again make sure we have our expected size
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
OrderMap orderMap = tx.getOrderMap();
|
||||
assertEquals(orders.size() - 5, orderMap.querySize(tx));
|
||||
assertEquals(0, orderMap.querySize(tx, "MyType3"));
|
||||
}
|
||||
|
||||
// now use the remove all
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
long removed = tx.getOrderMap().removeAll(tx);
|
||||
assertEquals(orders.size() - 5, removed);
|
||||
}
|
||||
|
||||
// again make sure we have our expected size
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
OrderMap orderMap = tx.getOrderMap();
|
||||
assertEquals(0, orderMap.querySize(tx));
|
||||
}
|
||||
}
|
||||
|
||||
// now add all again
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
tx.getOrderMap().addAll(tx, orders);
|
||||
}
|
||||
|
||||
|
@ -205,13 +211,13 @@ public class OrderModelTestRunner {
|
|||
expectedTypes.add("MyType2");
|
||||
expectedTypes.add("MyType3");
|
||||
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
List<Order> allOrders = tx.getOrderMap().getAllElements(tx);
|
||||
Collections.sort(allOrders, comparator);
|
||||
assertEquals(orders, allOrders);
|
||||
}
|
||||
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
OrderMap orderMap = tx.getOrderMap();
|
||||
|
||||
Set<String> types = orderMap.getTypes(tx);
|
||||
|
@ -229,7 +235,7 @@ public class OrderModelTestRunner {
|
|||
}
|
||||
}
|
||||
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
Order order = tx.getOrderMap().getBy(tx, "MyType1", "@_00000001");
|
||||
assertNotNull(order);
|
||||
order = tx.getOrderMap().getBy(tx, "MyType2", "@_00000006");
|
||||
|
|
|
@ -21,6 +21,8 @@ import li.strolch.agent.impl.DataStoreMode;
|
|||
import li.strolch.model.Resource;
|
||||
import li.strolch.model.parameter.Parameter;
|
||||
import li.strolch.persistence.api.StrolchTransaction;
|
||||
import li.strolch.runtime.privilege.PrivilegeHandler;
|
||||
import ch.eitchnet.privilege.model.Certificate;
|
||||
|
||||
@SuppressWarnings("nls")
|
||||
public class ResourceModelTestRunner {
|
||||
|
@ -31,17 +33,21 @@ public class ResourceModelTestRunner {
|
|||
|
||||
private RuntimeMock runtimeMock;
|
||||
private String realmName;
|
||||
private Certificate certificate;
|
||||
|
||||
public ResourceModelTestRunner(RuntimeMock runtimeMock, String realmName) {
|
||||
this.runtimeMock = runtimeMock;
|
||||
this.realmName = realmName;
|
||||
|
||||
PrivilegeHandler privilegeHandler = runtimeMock.getContainer().getPrivilegeHandler();
|
||||
certificate = privilegeHandler.authenticate("test", "test".getBytes());
|
||||
}
|
||||
|
||||
public void runCreateResourceTest() {
|
||||
|
||||
// create
|
||||
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();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
tx.getResourceMap().add(tx, newResource);
|
||||
}
|
||||
}
|
||||
|
@ -49,7 +55,7 @@ public class ResourceModelTestRunner {
|
|||
public void runQuerySizeTest() {
|
||||
|
||||
// remove all
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
tx.getResourceMap().removeAll(tx, tx.getResourceMap().getAllElements(tx));
|
||||
}
|
||||
|
||||
|
@ -57,14 +63,14 @@ public class ResourceModelTestRunner {
|
|||
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();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
tx.getResourceMap().add(tx, resource1);
|
||||
tx.getResourceMap().add(tx, resource2);
|
||||
tx.getResourceMap().add(tx, resource3);
|
||||
}
|
||||
|
||||
// query size
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
long size = tx.getResourceMap().querySize(tx);
|
||||
assertEquals("Should have three objects", 3, size);
|
||||
|
||||
|
@ -86,13 +92,13 @@ public class ResourceModelTestRunner {
|
|||
|
||||
// create
|
||||
Resource newResource = createResource(ID, NAME, TYPE);
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
tx.getResourceMap().add(tx, newResource);
|
||||
}
|
||||
|
||||
// read
|
||||
Resource readResource = null;
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
readResource = tx.getResourceMap().getBy(tx, TYPE, ID);
|
||||
}
|
||||
assertNotNull("Should read Resource with id " + ID, readResource); //$NON-NLS-1$
|
||||
|
@ -101,13 +107,13 @@ public class ResourceModelTestRunner {
|
|||
Parameter<String> 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();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
tx.getResourceMap().update(tx, readResource);
|
||||
}
|
||||
|
||||
// read updated
|
||||
Resource updatedResource = null;
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
updatedResource = tx.getResourceMap().getBy(tx, TYPE, ID);
|
||||
}
|
||||
assertNotNull("Should read Resource with id " + ID, updatedResource); //$NON-NLS-1$
|
||||
|
@ -117,12 +123,12 @@ public class ResourceModelTestRunner {
|
|||
assertEquals(newStringValue, updatedParam.getValue());
|
||||
|
||||
// delete
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
tx.getResourceMap().remove(tx, readResource);
|
||||
}
|
||||
|
||||
// fail to re-read
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx();) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test");) {
|
||||
Resource resource = tx.getResourceMap().getBy(tx, TYPE, ID);
|
||||
assertNull("Should no read Resource with id " + ID, resource); //$NON-NLS-1$
|
||||
}
|
||||
|
@ -146,57 +152,57 @@ public class ResourceModelTestRunner {
|
|||
Collections.sort(resources, comparator);
|
||||
|
||||
// first clear the map, so that we have a clean state
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
ResourceMap resourceMap = tx.getResourceMap();
|
||||
resourceMap.removeAll(tx, resourceMap.getAllElements(tx));
|
||||
}
|
||||
|
||||
{
|
||||
// make sure it is empty
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
ResourceMap resourceMap = tx.getResourceMap();
|
||||
assertEquals(0, resourceMap.querySize(tx));
|
||||
}
|
||||
|
||||
// now add some resources
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
tx.getResourceMap().addAll(tx, resources);
|
||||
}
|
||||
|
||||
// make sure we have our expected size
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
ResourceMap resourceMap = tx.getResourceMap();
|
||||
assertEquals(resources.size(), resourceMap.querySize(tx));
|
||||
assertEquals(5, resourceMap.querySize(tx, "MyType3"));
|
||||
}
|
||||
|
||||
// now use the remove all by type
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
tx.getResourceMap().removeAllBy(tx, "MyType3");
|
||||
}
|
||||
|
||||
// again make sure we have our expected size
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
ResourceMap resourceMap = tx.getResourceMap();
|
||||
assertEquals(resources.size() - 5, resourceMap.querySize(tx));
|
||||
assertEquals(0, resourceMap.querySize(tx, "MyType3"));
|
||||
}
|
||||
|
||||
// now use the remove all
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
long removed = tx.getResourceMap().removeAll(tx);
|
||||
assertEquals(resources.size() - 5, removed);
|
||||
}
|
||||
|
||||
// again make sure we have our expected size
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
ResourceMap resourceMap = tx.getResourceMap();
|
||||
assertEquals(0, resourceMap.querySize(tx));
|
||||
}
|
||||
}
|
||||
|
||||
// now add all again
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
tx.getResourceMap().addAll(tx, resources);
|
||||
}
|
||||
|
||||
|
@ -205,13 +211,13 @@ public class ResourceModelTestRunner {
|
|||
expectedTypes.add("MyType2");
|
||||
expectedTypes.add("MyType3");
|
||||
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
List<Resource> allResources = tx.getResourceMap().getAllElements(tx);
|
||||
Collections.sort(allResources, comparator);
|
||||
assertEquals(resources, allResources);
|
||||
}
|
||||
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
ResourceMap resourceMap = tx.getResourceMap();
|
||||
|
||||
Set<String> types = resourceMap.getTypes(tx);
|
||||
|
@ -229,7 +235,7 @@ public class ResourceModelTestRunner {
|
|||
}
|
||||
}
|
||||
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx()) {
|
||||
try (StrolchTransaction tx = this.runtimeMock.getRealm(this.realmName).openTx(certificate, "test")) {
|
||||
Resource resource = tx.getResourceMap().getBy(tx, "MyType1", "@_00000001");
|
||||
assertNotNull(resource);
|
||||
resource = tx.getResourceMap().getBy(tx, "MyType2", "@_00000006");
|
||||
|
|
|
@ -25,7 +25,7 @@ import li.strolch.agent.api.ComponentContainer;
|
|||
import li.strolch.agent.api.StrolchAgent;
|
||||
import li.strolch.agent.api.StrolchRealm;
|
||||
import li.strolch.runtime.configuration.RuntimeConfiguration;
|
||||
import li.strolch.runtime.privilege.StrolchPrivilegeHandler;
|
||||
import li.strolch.runtime.privilege.PrivilegeHandler;
|
||||
import li.strolch.service.api.ServiceHandler;
|
||||
import li.strolch.service.api.ServiceResult;
|
||||
import li.strolch.service.api.ServiceResultState;
|
||||
|
@ -54,8 +54,8 @@ public final class RuntimeMock {
|
|||
return this.agent;
|
||||
}
|
||||
|
||||
public StrolchPrivilegeHandler getPrivilegeHandler() {
|
||||
return this.container.getComponent(StrolchPrivilegeHandler.class);
|
||||
public PrivilegeHandler getPrivilegeHandler() {
|
||||
return this.container.getPrivilegeHandler();
|
||||
}
|
||||
|
||||
public ServiceHandler getServiceHandler() {
|
||||
|
|
Loading…
Reference in New Issue