X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fcommon%2FClientOperations.java;fp=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fcommon%2FClientOperations.java;h=1aa9aae1cefbf0b258391c919d213e29185fcfec;hb=67fd62f9c742337ec80eef658192db198a0efaac;hp=0000000000000000000000000000000000000000;hpb=cde82ba81327d5515fdca362f7f4c70f5103ae80;p=simantics%2Fplatform.git diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/common/ClientOperations.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/common/ClientOperations.java new file mode 100644 index 000000000..1aa9aae1c --- /dev/null +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/common/ClientOperations.java @@ -0,0 +1,235 @@ +package org.simantics.db.tests.common; + +import java.util.Collection; +import java.util.ListIterator; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.utils.OrderedSetUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ValidationException; +import org.simantics.db.impl.ClusterSupport; +import org.simantics.db.impl.ResourceImpl; +import org.simantics.db.testing.common.Client; +import org.simantics.db.testing.common.TestBase; +import org.simantics.layer0.Layer0; + +public class ClientOperations { + + public static boolean DEBUG = false; + + public static String createData(Client client) + throws DatabaseException { + final String name = client.getInstanceName(); + client.getSession().syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph g) throws DatabaseException { + Layer0 b = Layer0.getInstance(g); + Resource newResource = g.newResource(); + g.claim(newResource, b.InstanceOf, null, b.Type); + g.claimLiteral(newResource, b.HasName, name); + g.claim(g.getRootLibrary(), b.ConsistsOf, newResource); + } + }); + return name; + } + public static void validateData(Client client, final String name) + throws DatabaseException { + client.getSession().syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph g) throws DatabaseException { + Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI); + Layer0 l0 = Layer0.getInstance(g); + Collection resources = g.getObjects(rl, l0.ConsistsOf); + Resource newResource = null; + for (Resource r : resources) { + String value = g.getPossibleRelatedValue(r, l0.HasName); + if (null != value && value.equals(name)) { + newResource = r; + break; + } + } + if (newResource == null) { + throw new ValidationException("Could not find resource " + name); + } + if (!g.isInstanceOf(newResource, l0.Type)) + throw new ValidationException("Created resource is not an instance of Type"); + } + }); + } + public static void removeData(Client client, final String name) + throws DatabaseException { +// ++instanceNumber; + client.getSession().syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph g) throws DatabaseException { + Layer0 b = Layer0.getInstance(g); + Collection resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf); + Resource newResource = null; + for (Resource r : resources) { + String value = g.getPossibleRelatedValue(r, b.HasName); + if (null != value && value.equals(name)) { + newResource = r; + g.deny(g.getRootLibrary(), b.ConsistsOf, newResource); + break; + } + } + if (newResource == null) { + throw new ValidationException("Could not find resource " + name); + } + } + }); + } + public static void validateDataRemoved(Client client, final String name) + throws DatabaseException { + client.getSession().syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph g) throws DatabaseException { + Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI); + Layer0 l0 = Layer0.getInstance(g); + Collection resources = g.getObjects(rl, l0.ConsistsOf); + Resource newResource = null; + for (Resource r : resources) { + String value = g.getPossibleRelatedValue(r, l0.HasName); + if (null != value && value.equals(name)) { + newResource = r; + break; + } + } + if (newResource != null) { + throw new ValidationException("Could find resource " + name); + } + } + }); + } + public static String createOrderedSet(Client client, final int size) + throws DatabaseException { + final String name = client.getInstanceName(); + client.getSession().syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph g) throws DatabaseException { + Layer0 b = Layer0.getInstance(g); + Resource l = OrderedSetUtils.create(g, b.Type); + g.claimLiteral(l, b.HasName, name); + g.claim(g.getRootLibrary(), b.ConsistsOf, l); + for (int i=0; i resources = g.getObjects(rl, l0.ConsistsOf); + Resource newResource = null; + for (Resource r : resources) { + String value = g.getPossibleRelatedValue(r, l0.HasName); + if (null != value && value.equals(name)) { + newResource = r; + break; + } + } + if (newResource == null) { + throw new ValidationException("Could not find resource " + name); + } + if (!g.isInstanceOf(newResource, l0.Type)) + throw new ValidationException("Created resource is not an instance of Type"); + ListIterator a = OrderedSetUtils.iterator(g, newResource); + int count = 0; + while (a.hasNext()) { + Resource r = a.next(); + if (DEBUG) + System.out.println("DEBUG: Resource " + r); + ++count; + } + if (size != count) + throw new ValidationException("Number of elements doesn't match!"); + } + }); + } + public static void removeElement(Client client, final String name, final int size) + throws DatabaseException { + client.getSession().syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph g) throws DatabaseException { + Layer0 b = Layer0.getInstance(g); + Collection resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf); + Resource newResource = null; + for (Resource r : resources) { + String value = g.getPossibleRelatedValue(r, b.HasName); + if (null != value && value.equals(name)) { + newResource = r; + break; + } + } + if (newResource == null) { + throw new ValidationException("Could not find resource " + name); + } + if (!g.isInstanceOf(newResource, b.Type)) + throw new ValidationException("Created resource is not an instance of Type"); + ListIterator a = OrderedSetUtils.iterator(g, newResource); + Resource el = null; + while (a.hasNext()) { + el = a.next(); + break; + } + if (el == null) { + if (size != 0) + throw new ValidationException("Number of elements doesn't match!"); + return; + } + OrderedSetUtils.remove(g, newResource, el); + a = OrderedSetUtils.iterator(g, newResource); + int count = 0; + while (a.hasNext()) { + Resource r = a.next(); + if (DEBUG) + System.out.println("DEBUG: Resource " + r); + ++count; + } + if (size != count) + throw new ValidationException("Number of elements doesn't match!"); + } + }); + } + public static void adddElement(Client client, final String name, final int size) + throws DatabaseException { + client.getSession().syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph g) throws DatabaseException { + Layer0 b = Layer0.getInstance(g); + Collection resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf); + Resource newResource = null; + for (Resource r : resources) { + String value = g.getPossibleRelatedValue(r, b.HasName); + if (null != value && value.equals(name)) { + newResource = r; + break; + } + } + if (newResource == null) { + throw new ValidationException("Could not find resource " + name); + } + if (!g.isInstanceOf(newResource, b.Type)) + throw new ValidationException("Created resource is not an instance of Type"); + for (int i=0; i