package org.simantics.db.tests.api.support.serialisationSupport; import java.util.Collection; import org.junit.Before; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.service.SerialisationSupport; import org.simantics.db.testing.base.ExistingDatabaseTest; import org.simantics.db.testing.common.WriteQuery; import org.simantics.layer0.Layer0; public class RandomAccessIdTestCommon extends ExistingDatabaseTest { protected long randomAccessID; protected Resource resource; protected final String newName = "New name " + getRandomString(); protected Resource rl; @Override @Before public void setUp() throws Exception { super.setUp(); rl = getSession().getRootLibrary(); } protected void createResourceWithName() throws DatabaseException { // Create a new resource. getSession().syncRequest(new WriteQuery(this) { public void run(WriteGraph g) throws Throwable { Layer0 b = Layer0.getInstance(g); Resource instance = g.newResource(); g.claim(instance, b.InstanceOf, b.Type); g.claim(rl, b.ConsistsOf, instance); g.claimLiteral(instance, b.HasName, newName); }; }); checkException(); } protected void validateResourceWithName() throws DatabaseException { // Find the created resource. getSession().syncRequest(new TestReadRequest() { @Override public void run(ReadGraph g) throws Throwable { Layer0 l0 = Layer0.getInstance(g); Collection resources = g.getObjects(rl, l0.ConsistsOf); Resource found = null; for (Resource r : resources) { String name = g.getPossibleRelatedValue(r, l0.HasName); if (null != name && name.equals(newName)) { found = r; break; } } if (found == null) { throw new DatabaseException("Cannot find created resource"); } resource = found; } }); checkException(); } protected void createRandomAccessId() throws DatabaseException { // Create random access id for resource. SerialisationSupport support = getSession().getService(SerialisationSupport.class); randomAccessID = support.getRandomAccessId(resource); if (randomAccessID == 0) throw new DatabaseException("Could not create random access id"); } protected void validateRandomAccessId() throws DatabaseException { // test that random access id works and the given resource is the instance that we created getSession().syncRequest(new TestReadRequest() { @Override public void run(ReadGraph g) throws Throwable { Layer0 l0 = Layer0.getInstance(g); SerialisationSupport support = getSession().getService(SerialisationSupport.class); Resource res = support.getResource(randomAccessID); String name = g.getRelatedValue(res, l0.HasName); if (!name.equals(newName)) { throw new Exception("Name is different that was written, returned resource is not a correct one!"); } } }); checkException(); } }