]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/serialisationSupport/RandomAccessIdTestCommon.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / support / serialisationSupport / RandomAccessIdTestCommon.java
1 package org.simantics.db.tests.api.support.serialisationSupport;
2
3 import java.util.Collection;
4
5 import org.junit.Before;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.service.SerialisationSupport;
11 import org.simantics.db.testing.base.ExistingDatabaseTest;
12 import org.simantics.db.testing.common.WriteQuery;
13 import org.simantics.layer0.Layer0;
14
15 public class RandomAccessIdTestCommon extends ExistingDatabaseTest {
16     protected long randomAccessID;
17     protected Resource resource;
18     protected final String newName = "New name " + getRandomString(); 
19     protected Resource rl;
20     @Override
21     @Before
22     public void setUp() throws Exception {
23         super.setUp();
24         rl = getSession().getRootLibrary();
25     }
26     protected void createResourceWithName()
27     throws DatabaseException {
28         // Create a new resource.
29         getSession().syncRequest(new WriteQuery(this) {
30             public void run(WriteGraph g) throws Throwable {
31                 Layer0 b = Layer0.getInstance(g);
32                 Resource instance = g.newResource();
33                 g.claim(instance, b.InstanceOf, b.Type);
34                 g.claim(rl, b.ConsistsOf, instance);
35                 g.claimLiteral(instance, b.HasName, newName);
36             };
37         });
38         checkException();
39     }
40     protected void validateResourceWithName()
41     throws DatabaseException {
42         // Find the created resource.
43         getSession().syncRequest(new TestReadRequest() {
44             @Override
45             public void run(ReadGraph g) throws Throwable {
46                 Layer0 l0 = Layer0.getInstance(g);
47                 Collection<Resource> resources = g.getObjects(rl, l0.ConsistsOf);
48                 Resource found = null;
49                 for (Resource r : resources) {
50                     String name = g.getPossibleRelatedValue(r, l0.HasName);
51                     if (null != name && name.equals(newName)) {
52                         found = r;
53                         break;
54                     }
55                 }
56                 if (found == null) {
57                     throw new DatabaseException("Cannot find created resource");
58                 }
59                 resource = found;
60             }
61         });
62         checkException();
63     }
64     protected void createRandomAccessId()
65     throws DatabaseException {
66         // Create random access id for resource.
67         SerialisationSupport support = getSession().getService(SerialisationSupport.class);
68         randomAccessID = support.getRandomAccessId(resource);
69         if (randomAccessID == 0)
70             throw new DatabaseException("Could not create random access id");
71     }
72     protected void validateRandomAccessId()
73     throws DatabaseException {
74         // test that random access id works and the given resource is the instance that we created
75         getSession().syncRequest(new TestReadRequest() {
76             @Override
77             public void run(ReadGraph g) throws Throwable {
78                 Layer0 l0 = Layer0.getInstance(g);
79                 SerialisationSupport support = getSession().getService(SerialisationSupport.class);
80                 Resource res = support.getResource(randomAccessID);
81                 String name = g.getRelatedValue(res, l0.HasName);
82                 if (!name.equals(newName)) {
83                     throw new Exception("Name is different that was written, returned resource is not a correct one!");
84                 }
85             }
86         });
87         checkException();
88     }
89 }