]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.db.tests/src/org/simantics/db/tests/common/ClientOperations.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / common / ClientOperations.java
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 (file)
index 0000000..1aa9aae
--- /dev/null
@@ -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<Resource> 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<Resource> 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<Resource> 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<size; ++i) {
+                       Resource el = g.newResource();
+                       g.claim(el, b.InstanceOf, null, b.Type);
+                       OrderedSetUtils.add(g, l, el);
+                }
+                g.claim(g.getRootLibrary(), b.ConsistsOf, l);
+            }
+        });
+        return name;
+    }
+    public static void validateOrderedSet(final Client client, final String name, final int size)
+    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<Resource> 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<Resource> 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<Resource> 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<Resource> 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<Resource> 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<size; ++i) {
+                    Resource el = g.newResource();
+                    g.claim(el, b.InstanceOf, null, b.Type);
+                    OrderedSetUtils.add(g, newResource, el);
+                }
+            }
+        });
+    }  
+}