]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.db.tests.common;
2
3 import java.util.Collection;
4 import java.util.ListIterator;
5
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.common.request.ReadRequest;
10 import org.simantics.db.common.request.WriteRequest;
11 import org.simantics.db.common.utils.OrderedSetUtils;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.exception.ValidationException;
14 import org.simantics.db.impl.ClusterSupport;
15 import org.simantics.db.impl.ResourceImpl;
16 import org.simantics.db.testing.common.Client;
17 import org.simantics.db.testing.common.TestBase;
18 import org.simantics.layer0.Layer0;
19
20 public class ClientOperations {
21         
22         public static boolean DEBUG = false;
23         
24     public static String createData(Client client)
25     throws DatabaseException {
26         final String name = client.getInstanceName();
27         client.getSession().syncRequest(new WriteRequest() {
28             @Override
29             public void perform(WriteGraph g) throws DatabaseException {
30                 Layer0 b = Layer0.getInstance(g);
31                 Resource newResource = g.newResource();
32                 g.claim(newResource, b.InstanceOf, null, b.Type);
33                 g.claimLiteral(newResource, b.HasName, name);
34                 g.claim(g.getRootLibrary(), b.ConsistsOf, newResource);
35             }
36         });
37         return name;
38     }
39     public static void validateData(Client client, final String name)
40     throws DatabaseException {
41         client.getSession().syncRequest(new ReadRequest() {
42             @Override
43             public void run(ReadGraph g) throws DatabaseException {
44                 Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
45                 Layer0 l0 = Layer0.getInstance(g);
46                 Collection<Resource> resources = g.getObjects(rl, l0.ConsistsOf);
47                 Resource newResource = null;
48                 for (Resource r : resources) {
49                     String value = g.getPossibleRelatedValue(r, l0.HasName);
50                     if (null != value && value.equals(name)) {
51                         newResource = r;
52                         break;
53                     }
54                 }
55                 if (newResource == null) {
56                     throw new ValidationException("Could not find resource " + name);
57                 }
58                 if (!g.isInstanceOf(newResource, l0.Type))
59                     throw new ValidationException("Created resource is not an instance of Type");
60             }
61         });
62     }
63     public static void removeData(Client client, final String name)
64     throws DatabaseException {
65 //      ++instanceNumber;
66         client.getSession().syncRequest(new WriteRequest() {
67             @Override
68             public void perform(WriteGraph g) throws DatabaseException {
69                 Layer0 b = Layer0.getInstance(g);
70                 Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
71                 Resource newResource = null;
72                 for (Resource r : resources) {
73                     String value = g.getPossibleRelatedValue(r, b.HasName);
74                     if (null != value && value.equals(name)) {
75                         newResource = r;
76                         g.deny(g.getRootLibrary(), b.ConsistsOf, newResource);
77                         break;
78                     }
79                 }
80                 if (newResource == null) {
81                     throw new ValidationException("Could not find resource " + name);
82                 }
83             }
84         });
85     }
86     public static void validateDataRemoved(Client client, final String name)
87     throws DatabaseException {
88         client.getSession().syncRequest(new ReadRequest() {
89             @Override
90             public void run(ReadGraph g) throws DatabaseException {
91                 Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
92                 Layer0 l0 = Layer0.getInstance(g);
93                 Collection<Resource> resources = g.getObjects(rl, l0.ConsistsOf);
94                 Resource newResource = null;
95                 for (Resource r : resources) {
96                     String value = g.getPossibleRelatedValue(r, l0.HasName);
97                     if (null != value && value.equals(name)) {
98                         newResource = r;
99                         break;
100                     }
101                 }
102                 if (newResource != null) {
103                     throw new ValidationException("Could find resource " + name);
104                 }
105             }
106         });
107     }
108     public static String createOrderedSet(Client client, final int size)
109     throws DatabaseException {
110         final String name = client.getInstanceName();
111         client.getSession().syncRequest(new WriteRequest() {
112             @Override
113             public void perform(WriteGraph g) throws DatabaseException {
114                 Layer0 b = Layer0.getInstance(g);
115                 Resource l = OrderedSetUtils.create(g, b.Type);
116                 g.claimLiteral(l, b.HasName, name);
117                 g.claim(g.getRootLibrary(), b.ConsistsOf, l);
118                 for (int i=0; i<size; ++i) {
119                         Resource el = g.newResource();
120                         g.claim(el, b.InstanceOf, null, b.Type);
121                         OrderedSetUtils.add(g, l, el);
122                 }
123                 g.claim(g.getRootLibrary(), b.ConsistsOf, l);
124             }
125         });
126         return name;
127     }
128     public static void validateOrderedSet(final Client client, final String name, final int size)
129     throws DatabaseException {
130         client.getSession().syncRequest(new ReadRequest() {
131             @Override
132             public void run(ReadGraph g) throws DatabaseException {
133                 Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
134                 Layer0 l0 = Layer0.getInstance(g);
135                 Collection<Resource> resources = g.getObjects(rl, l0.ConsistsOf);
136                 Resource newResource = null;
137                 for (Resource r : resources) {
138                     String value = g.getPossibleRelatedValue(r, l0.HasName);
139                     if (null != value && value.equals(name)) {
140                         newResource = r;
141                         break;
142                     }
143                 }
144                 if (newResource == null) {
145                     throw new ValidationException("Could not find resource " + name);
146                 }
147                 if (!g.isInstanceOf(newResource, l0.Type))
148                     throw new ValidationException("Created resource is not an instance of Type");
149                 ListIterator<Resource> a = OrderedSetUtils.iterator(g, newResource);
150                 int count = 0;
151                 while (a.hasNext()) {
152                         Resource r = a.next();
153                         if (DEBUG)
154                             System.out.println("DEBUG: Resource " + r);
155                         ++count;
156                 }
157                 if (size != count)
158                     throw new ValidationException("Number of elements doesn't match!");
159             }
160         });
161     }
162     public static void removeElement(Client client, final String name, final int size)
163     throws DatabaseException {
164         client.getSession().syncRequest(new WriteRequest() {
165             @Override
166             public void perform(WriteGraph g) throws DatabaseException {
167                 Layer0 b = Layer0.getInstance(g);
168                 Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
169                 Resource newResource = null;
170                 for (Resource r : resources) {
171                     String value = g.getPossibleRelatedValue(r, b.HasName);
172                     if (null != value && value.equals(name)) {
173                         newResource = r;
174                         break;
175                     }
176                 }
177                 if (newResource == null) {
178                     throw new ValidationException("Could not find resource " + name);
179                 }
180                 if (!g.isInstanceOf(newResource, b.Type))
181                     throw new ValidationException("Created resource is not an instance of Type");
182                 ListIterator<Resource> a = OrderedSetUtils.iterator(g, newResource);
183                 Resource el = null;
184                 while (a.hasNext()) {
185                         el = a.next();
186                         break;
187                 }
188                 if (el == null) {
189                     if (size != 0)
190                         throw new ValidationException("Number of elements doesn't match!");
191                         return;
192                 }
193                 OrderedSetUtils.remove(g, newResource, el);
194                 a = OrderedSetUtils.iterator(g, newResource);
195                 int count = 0;
196                 while (a.hasNext()) {
197                         Resource r = a.next();
198                         if (DEBUG)
199                             System.out.println("DEBUG: Resource " + r);
200                         ++count;
201                 }
202                 if (size != count)
203                     throw new ValidationException("Number of elements doesn't match!");
204             }
205         });
206     }
207     public static void adddElement(Client client, final String name, final int size)
208     throws DatabaseException {
209         client.getSession().syncRequest(new WriteRequest() {
210             @Override
211             public void perform(WriteGraph g) throws DatabaseException {
212                 Layer0 b = Layer0.getInstance(g);
213                 Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
214                 Resource newResource = null;
215                 for (Resource r : resources) {
216                     String value = g.getPossibleRelatedValue(r, b.HasName);
217                     if (null != value && value.equals(name)) {
218                         newResource = r;
219                         break;
220                     }
221                 }
222                 if (newResource == null) {
223                     throw new ValidationException("Could not find resource " + name);
224                 }
225                 if (!g.isInstanceOf(newResource, b.Type))
226                     throw new ValidationException("Created resource is not an instance of Type");
227                 for (int i=0; i<size; ++i) {
228                     Resource el = g.newResource();
229                     g.claim(el, b.InstanceOf, null, b.Type);
230                     OrderedSetUtils.add(g, newResource, el);
231                 }
232             }
233         });
234     }   
235 }