1   package com.sri.emo.commandline.defaults;
2   
3   import junit.framework.*;
4   import com.sri.emo.test.DatabaseTestFixture;
5   import com.jcorporate.expresso.services.test.TestSystemInitializer;
6   import com.sri.emo.test.EmoTestSuite;
7   import com.jcorporate.expresso.core.security.SuperUser;
8   import com.sri.emo.dbobj.Node;
9   import com.sri.emo.dbobj.selectiontree.TreeDeletionSelectorTestCase;
10  import com.sri.emo.commandline.SqlRowGenerator;
11  import com.sri.emo.dbobj.Attribute;
12  import com.sri.emo.dbobj.RelationType;
13  import com.sri.emo.dbobj.Part;
14  import com.sri.emo.dbobj.NodeType;
15  import com.sri.emo.dbobj.Relation;
16  import com.sri.emo.dbobj.PickList;
17  import com.jcorporate.expresso.services.dbobj.RowPermissions;
18  import com.jcorporate.expresso.services.dbobj.RowGroupPerms;
19  import com.jcorporate.expresso.services.dbobj.DBObjSecurity;
20  
21  /***
22   * @author Michael Rimov
23   * @version 1.0
24   */
25  public class DefaultSQLGeneratorTestCase extends TestCase {
26      private SqlRowGenerator defaultSQLGenerator = null;
27  
28      private DatabaseTestFixture databaseTestFixture = null;
29  
30  
31      protected void setUp() throws Exception {
32          super.setUp();
33          defaultSQLGenerator = new DefaultSQLGenerator();
34          databaseTestFixture = new DatabaseTestFixture(TestSystemInitializer
35              .getTestContext(), EmoTestSuite.class
36              .getResourceAsStream("relation-test-one.xml"));
37          databaseTestFixture.setUp();
38  
39      }
40  
41      protected void tearDown() throws Exception {
42          defaultSQLGenerator = null;
43          databaseTestFixture.tearDown();
44          databaseTestFixture = null;
45          super.tearDown();
46      }
47  
48      /***
49       * NOTE: currently tested against hsqldb.  May be different against mysql.
50       */
51      public void testExportNodeSql() throws Exception  {
52          Node n = new Node(SuperUser.INSTANCE);
53          n.setNodeId(TreeDeletionSelectorTestCase.STAR_WARS_ID);
54          n.retrieve();
55  
56  
57          String result = defaultSQLGenerator.generateInsert(n);
58          assertNotNull(result);
59          assertTrue(("INSERT  INTO node (NODE_OWNER, NODE_ANNOTATION, NODE_PATH, NODE_TYPE,"
60              +" NODE_COMMENT, NODE_ID, NODE_MIME_TYPE, NODE_CREATED, NODE_MODIFIED, NODE_TITLE) VALUES "
61              +"('Admin', 'The initial Star Wars released in 1975 that started it all.', null, 'MOVIEDEMO', '', "
62              +"14, null, '2005-02-03 07:02:16', '2005-06-20 14:24:57', 'Star Wars: A New Hope');")
63              .equalsIgnoreCase(result));
64  
65  
66      }
67  
68  
69      public void testGenerateAttibuteSql() throws Exception {
70          Attribute a = new Attribute(SuperUser.INSTANCE);
71          a.setAttributeId("2");
72          a.retrieve();
73  
74          String result = defaultSQLGenerator.generateInsert(a);
75          assertNotNull(result);
76          assertTrue( ("INSERT  INTO attribute (ATTRIBUTE_ID, ATTRIBUTE_CREATED, ATTRIBUTE_VALUE, ATTRIBUTE_ORDER, "
77              +"NODE_TYPE, NODE_ID, ATTRIBUTE_MODIFIED, ATTRIBUTE_COMMENT, ATTRIBUTE_TYPE) VALUES (2, "
78              +"'2004-11-09 10:03:22', '117', 1, 'MOVIEDEMO', 2, '2004-11-09 10:03:22', '', 'LENGTH');")
79              .equalsIgnoreCase(result));
80  
81      }
82  
83  
84      public void testGenerateRelationTypeSql() throws Exception {
85          RelationType rt = new RelationType();
86          rt.setRequestingUser(SuperUser.INSTANCE);
87          rt.setRelType("DEST_CONTAINS_SRC");
88          rt.retrieve();
89          String result = defaultSQLGenerator.generateInsert(rt);
90          assertNotNull(result);
91          assertTrue( ("INSERT  INTO relation_type (RELATION_STRENGTH, REL_INVERSE,"
92              +" RELATION_TYPE_NAME, REL_DISPLAY_NAME, REL_TYPE_DESCRIP) VALUES ('WEAK',"
93              +" 'DEST_IS_PART_OF_SRC', 'DEST_CONTAINS_SRC', 'I am a part of',"
94              +" 'is an association to other objects that contain or subsume this one. "
95              +"For example, a windshield is a part of an automobile.');").equalsIgnoreCase(result));
96  
97  
98  
99      }
100 
101     public void testPartSql() throws Exception {
102         Part part = new Part();
103         part.setPartId(13);
104         part.retrieve();
105         String result = defaultSQLGenerator.generateInsert(part);
106         assertNotNull(result);
107         assertTrue( ("INSERT  INTO part2 (PART_NUM, RELATION_TYPE_NAME,"
108             +" PART_DESCRIP, PART_ID, PART_TYPE, SPECIAL_HANDLER, PARENT_TYPE,"
109             +" CARDINALITY, PART_LABEL, PART_IS_ATTRIB) VALUES (2, 'OwnedBy', 'Test', 13,"
110             +" 'PERSON', '', 'Movie_Companies', 'MULTIPLE_ALLOWED', 'Owners', false);").equalsIgnoreCase(result));
111 
112     }
113 
114 
115     public void testNodeTypeSql() throws Exception {
116         NodeType nodeType = new NodeType();
117         nodeType.setField(NodeType.NODE_TYPE_ID, "3");
118         nodeType.retrieve();
119 
120         String result = defaultSQLGenerator.generateInsert(nodeType);
121         assertNotNull(result);
122         assertTrue( ("INSERT  INTO node_type (NODE_TYPE_DESCRIP, NODE_TYPE_VERSION,"
123             +" NODE_TYPE_ID, DISPLAY_TITLE, USE_ID_FOR_REF, NODE_TYPE_HANDLER, "
124             +"NODE_TYPE_NAME) VALUES ('Groups of movies that have been released as a "
125             +"whole.', 1.2, 3, 'Movie Collections', null, '', 'Movie_Series');")
126             .equalsIgnoreCase(result));
127     }
128 
129 
130     public void testRelationSql() throws Exception {
131         Relation r = new Relation();
132         r.setRelationTypeName("starring");
133         r.setSrcId("14");
134         r.setDestId("23");
135         r.retrieve();
136 
137         String result = defaultSQLGenerator.generateInsert(r);
138         assertNotNull(result);
139         assertTrue( ("INSERT  INTO relation (RELATION_CREATED, RELATION_MODIFIED, "
140             +"RELATION_TYPE, RELATION_ANNOT, RELATION_ORDER, RELATION_OWNER, RELATION_DEST,"
141             +" RELATION_SRC) VALUES ('2006-06-13 14:35:08', '2006-06-13 14:35:08', 'starring', null,"
142             +" 1, 3, 23, 14);")
143             .equalsIgnoreCase(result));
144     }
145 
146 
147     public void testPicklistSql() throws Exception {
148         PickList pl = new PickList();
149         pl.setID("11");
150         pl.retrieve();
151 
152         String result = defaultSQLGenerator.generateInsert(pl);
153         assertNotNull(result);
154         assertTrue( ("INSERT  INTO picklist (LIST_ID, COMMENT, ORDER_NUM, VALUE_IN_PICKLIST,"
155             +" NODE_TYPE_NAME, ATTRIBUTE_TYPE) VALUES (11, null, 0, 'G',"
156             +" 'MOVIEDEMO', 'Movie_Rating');")
157             .equalsIgnoreCase(result));
158     }
159 
160 
161     public void FIXME_testRowPermissions() throws Exception {
162         RowPermissions rp = new RowPermissions();
163 
164         rp.setField(RowPermissions.TABLE_NAME,"picklist");
165         rp.setField(RowPermissions.ROW_KEY,"3");
166         rp.retrieve();
167 
168         String result = defaultSQLGenerator.generateInsert(rp);
169         assertNotNull(result);
170         assertTrue( ("INSERT  INTO ROW_PERMISSIONS (ROW_KEY, PERMISSIONS, "
171             +"TARGET_TABLE, OWNER_ID) VALUES ('3', 508, 'picklist', 3);")
172             .equalsIgnoreCase(result));
173     }
174 
175     /***
176      * The following test cases will fail on integration builds because default
177      * context doesn't exist on those databases.
178      * @throws Exception
179      */
180     public void FIXME_testDBObjectSecurity() throws Exception {
181         DBObjSecurity dos = new DBObjSecurity();
182         dos.setRequestingUser(SuperUser.INSTANCE);
183         dos.setDataContext("default"); //We grab some defaults since we're not going to write because  DBUnit will clear these tables
184         dos.setField(DBObjSecurity.DBOBJECT_NAME, "com.jcorporate.expresso.ext.dbobj.AppIntegration");
185         dos.setField(DBObjSecurity.GROUP_NAME,"Admin");
186         dos.setField(DBObjSecurity.METHOD_ALLOWED_CODE,"A");
187         dos.retrieve();
188 
189         String result = defaultSQLGenerator.generateInsert(dos);
190         assertNotNull(result);
191         assertTrue( ("INSERT INTO DBOBJSECURITY (MethodCode, GroupName, DBObjectName) "
192             +"VALUES ('A', 'Admin', 'com.jcorporate.expresso.ext.dbobj.AppIntegration');")
193             .equalsIgnoreCase(result));
194     }
195 
196 
197     public void FIXME_testPermissionGroup() throws Exception {
198         RowGroupPerms grp = new RowGroupPerms();
199 
200         grp.setDataContext("default"); //We grab some defaults since we're not going to write because  DBUnit will clear these tables
201         grp.setField(RowGroupPerms.TABLE_NAME, "part2");
202         grp.setField(RowGroupPerms.ROW_KEY, "1");
203         grp.setField(RowGroupPerms.GROUP, "Admin");
204         grp.retrieve();
205 
206         String result = defaultSQLGenerator.generateInsert(grp);
207         assertNotNull(result);
208         assertTrue( ("INSERT INTO ROW_GRP_PERMS "
209             +"(ROW_KEY, PERM_GROUP, PERMISSIONS, TARGET_TABLE) "
210             +"VALUES ('1', 'Admin', 508, 'part2');")
211             .equalsIgnoreCase(result));
212     }
213 
214 
215 }