]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/story/misc/DataModelTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / story / misc / DataModelTest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.tests.api.story.misc;
13
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.junit.Test;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.testing.base.ExistingDatabaseTest;
24 import org.simantics.db.testing.common.WriteQuery;
25 import org.simantics.layer0.Layer0;
26
27 /**
28  * 
29  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
30  *
31  */
32 public class DataModelTest extends ExistingDatabaseTest {
33         
34         /**
35          * Creates new instance and then tries to find it.
36          * @throws Exception
37          */
38         @Test
39         public void testCreateInstance() throws Exception{
40                 Session session = getSession();
41                 final Resource rootLib = getSession().getRootLibrary();
42         final String newInstanceName = "New Instance " + getRandomString(); 
43                 
44                 session.syncRequest(new WriteQuery(this) {
45                         @Override
46                         public void run(WriteGraph g) throws Throwable {
47                                 Layer0 b = Layer0.getInstance(g);
48                                 Resource newResource = g.newResource();
49                                 g.claim(newResource, b.InstanceOf, null, b.Type);
50                                 g.claimLiteral(newResource, b.HasName, newInstanceName);
51                                 g.claim(rootLib, b.ConsistsOf, newResource);
52 //                              comment(g, newResource, "DataModelTest.testCreateInstance");
53                         }
54                 });
55                 
56                 checkException();
57                 
58                 session.syncRequest(new WriteQuery(this) {
59                         @Override
60                         public void run(WriteGraph g) throws Throwable {
61                                 Layer0 b = Layer0.getInstance(g);
62                                 Collection<Resource> resources = g.getObjects(rootLib, b.ConsistsOf);
63                                 Resource newResource = null;
64                                 for (Resource r : resources) {
65                                     String value = g.getPossibleRelatedValue(r, b.HasName);
66                                         if (null != value && value.equals(newInstanceName)) {
67                                                 newResource = r;
68                                                 break;
69                                         }
70                                 }
71                                 if (newResource == null) {
72                                         throw new Exception("Could not find created resource");
73                                 }
74                                 if (!g.isInstanceOf(newResource, b.Type))
75                                         throw new Exception("Created resource is not an instance of Type");
76                         }
77                 });
78                 
79                 checkException();
80                 
81         }
82         
83         /**
84          * Creates new type and new instance of it. Then tries to find the instance and the type, and verifies that inheritance is correct. 
85          * @throws Exception
86          */
87         @Test
88         public void testCreateInstance2() throws Exception{
89                 Session session = getSession();
90                 final Resource rootLib = getSession().getRootLibrary();
91         final String random = getRandomString();
92         final String newTypeName = "New Type " + random; 
93         final String newInstanceName = "New Instance " + random; 
94                 
95                 session.syncRequest(new WriteQuery(this) {
96                         @Override
97                         public void run(WriteGraph g) throws Throwable {
98                             Layer0 b = Layer0.getInstance(g);
99                                 Resource newType = g.newResource();
100                 g.claim(newType, b.InstanceOf, null, b.Type);
101                 g.claim(newType, b.Inherits, b.SupertypeOf, b.Type);
102                 g.claimLiteral(newType, b.HasName, newTypeName);
103                 g.claim(rootLib, b.ConsistsOf, newType);
104                 Resource newResource = g.newResource();
105                 g.claim(newResource, b.InstanceOf, null, newType);
106                 g.claimLiteral(newResource, b.HasName, newInstanceName);
107                 g.claim(rootLib, b.ConsistsOf, newResource);
108                                 Map<String, String> md = new HashMap<String, String>(); 
109                                 md.put("DataModelTest2", "DataModelTest2");
110 //                              fillMetadata(md);
111                         }
112                         
113                 });
114                 
115                 checkException();
116                 
117                 session.syncRequest(new TestReadRequest() {
118                         @Override
119                         public void run(ReadGraph g) throws Throwable {
120                                 Layer0 b = Layer0.getInstance(g);
121                                 Collection<Resource> resources = g.getObjects(rootLib, b.ConsistsOf);
122                                 Resource newType = null;
123                                 Resource newInstance = null;
124                                 for (Resource r : resources) {
125                     String value = g.getPossibleRelatedValue(r, b.HasName);
126                     if (null == value)
127                         continue;
128                     if (value.equals(newTypeName)) {
129                                                 newType = r;
130                                         } else if (value.equals(newInstanceName)) {
131                                                 newInstance = r;
132                                         }
133                                 }
134                                 if (newType == null) 
135                                         throw new Exception("Could not find created type");
136                                 if (newInstance == null) 
137                                         throw new Exception("Could not find created instance");
138                                 
139                                 if (!g.isInstanceOf(newInstance, b.Type))
140                                         throw new Exception("Created resource is not an instance of Type");
141                                 if (!g.isInstanceOf(newInstance, newType))
142                                         throw new Exception("Created resource is not an instance of created type");
143                         }
144                 });
145                 
146                 checkException();
147                 
148         }
149
150 }