/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.db.tests.api.story.misc; import java.util.Collection; import java.util.HashMap; import java.util.Map; import org.junit.Test; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; import org.simantics.db.WriteGraph; import org.simantics.db.testing.base.ExistingDatabaseTest; import org.simantics.db.testing.common.WriteQuery; import org.simantics.layer0.Layer0; /** * * @author Marko Luukkainen * */ public class DataModelTest extends ExistingDatabaseTest { /** * Creates new instance and then tries to find it. * @throws Exception */ @Test public void testCreateInstance() throws Exception{ Session session = getSession(); final Resource rootLib = getSession().getRootLibrary(); final String newInstanceName = "New Instance " + getRandomString(); session.syncRequest(new WriteQuery(this) { @Override public void run(WriteGraph g) throws Throwable { Layer0 b = Layer0.getInstance(g); Resource newResource = g.newResource(); g.claim(newResource, b.InstanceOf, null, b.Type); g.claimLiteral(newResource, b.HasName, newInstanceName); g.claim(rootLib, b.ConsistsOf, newResource); // comment(g, newResource, "DataModelTest.testCreateInstance"); } }); checkException(); session.syncRequest(new WriteQuery(this) { @Override public void run(WriteGraph g) throws Throwable { Layer0 b = Layer0.getInstance(g); Collection resources = g.getObjects(rootLib, b.ConsistsOf); Resource newResource = null; for (Resource r : resources) { String value = g.getPossibleRelatedValue(r, b.HasName); if (null != value && value.equals(newInstanceName)) { newResource = r; break; } } if (newResource == null) { throw new Exception("Could not find created resource"); } if (!g.isInstanceOf(newResource, b.Type)) throw new Exception("Created resource is not an instance of Type"); } }); checkException(); } /** * Creates new type and new instance of it. Then tries to find the instance and the type, and verifies that inheritance is correct. * @throws Exception */ @Test public void testCreateInstance2() throws Exception{ Session session = getSession(); final Resource rootLib = getSession().getRootLibrary(); final String random = getRandomString(); final String newTypeName = "New Type " + random; final String newInstanceName = "New Instance " + random; session.syncRequest(new WriteQuery(this) { @Override public void run(WriteGraph g) throws Throwable { Layer0 b = Layer0.getInstance(g); Resource newType = g.newResource(); g.claim(newType, b.InstanceOf, null, b.Type); g.claim(newType, b.Inherits, b.SupertypeOf, b.Type); g.claimLiteral(newType, b.HasName, newTypeName); g.claim(rootLib, b.ConsistsOf, newType); Resource newResource = g.newResource(); g.claim(newResource, b.InstanceOf, null, newType); g.claimLiteral(newResource, b.HasName, newInstanceName); g.claim(rootLib, b.ConsistsOf, newResource); Map md = new HashMap(); md.put("DataModelTest2", "DataModelTest2"); // fillMetadata(md); } }); checkException(); session.syncRequest(new TestReadRequest() { @Override public void run(ReadGraph g) throws Throwable { Layer0 b = Layer0.getInstance(g); Collection resources = g.getObjects(rootLib, b.ConsistsOf); Resource newType = null; Resource newInstance = null; for (Resource r : resources) { String value = g.getPossibleRelatedValue(r, b.HasName); if (null == value) continue; if (value.equals(newTypeName)) { newType = r; } else if (value.equals(newInstanceName)) { newInstance = r; } } if (newType == null) throw new Exception("Could not find created type"); if (newInstance == null) throw new Exception("Could not find created instance"); if (!g.isInstanceOf(newInstance, b.Type)) throw new Exception("Created resource is not an instance of Type"); if (!g.isInstanceOf(newInstance, newType)) throw new Exception("Created resource is not an instance of created type"); } }); checkException(); } }