summaryrefslogtreecommitdiffstats
path: root/das-java/branches/das-java-beta2/rdb/src/test/java/org/apache/tuscany/das/rdb/test/MissingPKTests.java
blob: f12e383cae55c640f6f14a32b3c79a35f8f2168b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.tuscany.das.rdb.test;

import java.util.List;

import org.apache.tuscany.das.rdb.Command;
import org.apache.tuscany.das.rdb.DAS;
import org.apache.tuscany.das.rdb.test.data.BookData;
import org.apache.tuscany.das.rdb.test.data.DogData;
import org.apache.tuscany.das.rdb.test.data.MultiSchemaData;
import org.apache.tuscany.das.rdb.test.data.OrderDetailsData;
import org.apache.tuscany.das.rdb.test.data.OrderDetailsDescriptionData;
import org.apache.tuscany.das.rdb.test.data.OwnerData;
import org.apache.tuscany.das.rdb.test.data.OwnerDogData;
import org.apache.tuscany.das.rdb.test.data.PartData;
import org.apache.tuscany.das.rdb.test.data.SingerData;
import org.apache.tuscany.das.rdb.test.data.SongData;
import org.apache.tuscany.das.rdb.test.framework.DasTest;

import commonj.sdo.DataObject;

public class MissingPKTests  extends DasTest {
    protected void setUp() throws Exception {
        super.setUp();
        new PartData(getAutoConnection()).refresh();
        new DogData(getAutoConnection()).refresh();
        new OwnerData(getAutoConnection()).refresh();
        new OwnerDogData(getAutoConnection()).refresh();
        new OrderDetailsData(getAutoConnection()).refresh();
        new OrderDetailsDescriptionData(getAutoConnection()).refresh();
        new MultiSchemaData(getAutoConnection()).refresh();
        new BookData(getAutoConnection()).refresh();
        new SingerData(getAutoConnection()).refresh();
        new SongData(getAutoConnection()).refresh();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    //join with both tables PKs present in SELECT, PK data in child is null
    public void testNullDataInPK() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPK.xml"), getConnection());
    	Command select = das.getCommand("testNullDataInPK");
    	try{
    		select.executeQuery();
    		fail("Expected exception");
    	}catch(RuntimeException e){
    		if(e.getMessage().
    				indexOf("in query does not include Primary Key "+
    						"column or has null value in it, can not proceed!") >0){
    			assertTrue(true);//expected failure
    		}
    	}
    }

    //join with both tables PKs present in SELECT, child row is complete null, outer join
    public void testOuterJoin() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPK.xml"), getConnection());
    	Command select = das.getCommand("testOuterJoin");
    	DataObject root = select.executeQuery();
    	List singers = root.getList("SINGER");
    	List songs = root.getList("SONG");//as there is no relationship (explicit/implicit)
    	assertNotNull(singers);
    	assertEquals(1, singers.size());
    	assertNotNull(songs);
    	assertEquals(0, songs.size());
    }

    //join with both tables' PKs present in SELECT
    public void test11() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPK.xml"), getConnection());
    	Command select = das.getCommand("test11");
    	DataObject root = select.executeQuery();
    	List owners = root.getList("OWNER");
    	List dogs = root.getList("DOG");//as there is no relationship (explicit/implicit)
    	assertNotNull(owners);
    	assertNotNull(dogs);
    	for(int i=0; i<owners.size(); i++){
    		DataObject dobj = (DataObject)owners.get(i);
    	}
    	assertEquals(3, owners.size());
    	assertEquals(3, dogs.size());
    }

    //join with parent table's PK missing in SELECT
    public void test22() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPK.xml"), getConnection());
    	Command select = das.getCommand("test22");
    	try{
    		select.executeQuery();
    		fail("Expected exception");
    	}catch(RuntimeException e){
    		if(e.getMessage().
    				indexOf("in query does not include Primary Key "+
    						"column or has null value in it, can not proceed!") >0){
    			assertTrue(true);//expected failure
    		}
    	}
    }

    //join with child table's PK missing in SELECT
    public void test33() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPK.xml"), getConnection());
    	Command select = das.getCommand("test33");
    	try{
    		select.executeQuery();
    		fail("Expected exception");
    	}catch(RuntimeException e){
    		if(e.getMessage().
    				indexOf("in query does not include Primary Key "+
    						"column or has null value in it, can not proceed!") >0){
    			assertTrue(true);//expected failure
    		}
    	}
    }

    //join with both tables' PK missing in SELECT
    public void test44() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPK.xml"), getConnection());
    	Command select = das.getCommand("test44");
    	try{
    		select.executeQuery();
    		fail("Expected exception");
    	}catch(RuntimeException e){
    		if(e.getMessage().
    				indexOf("in query does not include Primary Key "+
    						"column or has null value in it, can not proceed!") >0){
    			assertTrue(true);//expected failure
    		}
    	}
    }

    //join with both tables' PK present in SELECT (use relationship)
    public void test11_rel() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPKREL.xml"), getConnection());
    	Command select = das.getCommand("test11");
    	DataObject root = select.executeQuery();
    	List owners = root.getList("OWNER");
    	assertNotNull(owners);
    	assertEquals(3, owners.size());

    	if(owners != null){
    		for(int i=0; i<owners.size(); i++){
    			List dogs = ((DataObject)owners.get(i)).getList("owns"); //use relationship
    	    	assertNotNull(dogs);

    	    	if( (((DataObject)owners.get(i)).getInt("ID") == 1 ) &&
    	    			dogs.size()==1){
    	    		assertTrue(true);//expected
    	    	}

    	    	if( (((DataObject)owners.get(i)).getInt("ID") == 2 ) &&
    	    			dogs.size()==1){
    	    		assertTrue(true);//expected
    	    	}

    	    	if( (((DataObject)owners.get(i)).getInt("ID") == 3 ) &&
    	    			dogs.size()==1){
    	    		assertTrue(true);//expected
    	    	}
    		}
    	}
    }

    //join with parent table's PK missing in SELECT (use relationship)
    public void test22_rel() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPKREL.xml"), getConnection());
    	Command select = das.getCommand("test22");
    	try{
    		select.executeQuery();
    		fail("Expected exception");
    	}catch(RuntimeException e){
    		if(e.getMessage().
    				indexOf("in query does not include Primary Key "+
    						"column or has null value in it, can not proceed!") >0){
    			assertTrue(true);//expected failure
    		}
    	}
    }

    //join with child table's PK missing in SELECT (use relationship)
    public void test33_rel() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPKREL.xml"), getConnection());
    	Command select = das.getCommand("test33");
    	try{
    		select.executeQuery();
    		fail("Expected exception");
    	}catch(RuntimeException e){
    		if(e.getMessage().
    				indexOf("in query does not include Primary Key "+
    						"column or has null value in it, can not proceed!") >0){
    			assertTrue(true);//expected failure
    		}
    	}
    }

    //join with both tables' PK missing in SELECT (use relationship)
    public void test44_rel() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPKREL.xml"), getConnection());
    	Command select = das.getCommand("test44");
    	try{
    		select.executeQuery();
    		fail("Expected exception");
    	}catch(RuntimeException e){
    		if(e.getMessage().
    				indexOf("in query does not include Primary Key "+
    						"column or has null value in it, can not proceed!") >0){
    			assertTrue(true);//expected failure
    		}
    	}
    }

    public void testCompoundPks() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPKREL.xml"), getConnection());
    	Command select = das.getCommand("compound_pks");
    	DataObject root = select.executeQuery();
    	assertEquals(4, root.getList("ORDERDETAILS").size());
    }

    public void testCompoundPksFail() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPKREL.xml"), getConnection());
    	Command select = das.getCommand("compound_pks_fail");
    	try{
    		select.executeQuery();
    		fail("Expected exception");
    	}catch(RuntimeException e){
    		if(e.getMessage().
    				indexOf("in query does not include Primary Key "+
    						"column or has null value in it, can not proceed!") >0){
    			assertTrue(true);//expected failure
    		}
    	}
    }

    public void testCompoundPksJoin() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPKREL.xml"), getConnection());
    	Command select = das.getCommand("compound_pks_join");
    	DataObject root = select.executeQuery();
    	List ordDetList = root.getList("ORDERDETAILS");
    	assertEquals(4, ordDetList.size());
    	for(int i=0; i<ordDetList.size(); i++){
    		DataObject curDO = (DataObject)ordDetList.get(i);
    		List ordDetDescList = curDO.getList("orderDetailsDesc");
    		if(curDO.getInt("ORDERID")==1 && curDO.getInt("PRODUCTID")==1 && ordDetDescList.size()==2){
    			assertTrue(true);//expected
    		}

    		if(curDO.getInt("ORDERID")==1 && curDO.getInt("PRODUCTID")==2 && ordDetDescList.size()==2){
    			assertTrue(true);//expected
    		}

    		if(curDO.getInt("ORDERID")==2 && curDO.getInt("PRODUCTID")==1 && ordDetDescList.size()==1){
    			assertTrue(true);//expected
    		}

    		if(curDO.getInt("ORDERID")==2 && curDO.getInt("PRODUCTID")==2 && ordDetDescList.size()==1){
    			assertTrue(true);//expected
    		}
    	}
    }

    public void testCompoundPksJoinFail() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPKREL.xml"), getConnection());
    	Command select = das.getCommand("compound_pks_join_fail");
    	try{
    		select.executeQuery();
    		fail("Expected exception");
    	}catch(RuntimeException e){
    		if(e.getMessage().
    				indexOf("in query does not include Primary Key "+
    						"column or has null value in it, can not proceed!") >0){
    			assertTrue(true);//expected failure
    		}
    	}
    }

    //case when no PK defined in Config but column ID is there in table and is not in SELECT clause
    //convention over configuration should assume it as PK and fail select
    public void testPartFail() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPK.xml"), getConnection());
    	Command select = das.getCommand("testPartFail");
    	try{
    		select.executeQuery();
    		fail("Expected exception");
    	}catch(RuntimeException e){
    		if(e.getMessage().
    				indexOf("in query does not include Primary Key "+
    						"column or has null value in it, can not proceed!") >0){
    			assertTrue(true);//expected failure
    		}
    	}
    }

    public void testMultiSchemaPKMiss() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MultiSchemaDasConfig14.xml"), getConnection());

    	//Explicit compound key relationship between DASTEST1.ORDERDETAILS and DASTEST3.ORDERDETAILSDESC
    	//ORDERID <-> ORDERID, PRODUCTID<->PRODUCTID
    	// Read some order details and related order details descs
        Command select = das
                .createCommand("SELECT DASTEST1.ORDERDETAILS.ORDERID, DASTEST1.ORDERDETAILS.PRODUCTID," +
                		"DASTEST3.ORDERDETAILSDESC.ORDERID FROM DASTEST1.ORDERDETAILS LEFT JOIN DASTEST3.ORDERDETAILSDESC " +
                		" ON DASTEST1.ORDERDETAILS.ORDERID = DASTEST3.ORDERDETAILSDESC.ORDERID " +
                		" AND DASTEST1.ORDERDETAILS.PRODUCTID = DASTEST3.ORDERDETAILSDESC.PRODUCTID");

        try{
    		select.executeQuery();
    		fail("Expected exception");
    	}catch(RuntimeException e){
    		if(e.getMessage().
    				indexOf("in query does not include Primary Key "+
    						"column or has null value in it, can not proceed!") >0){
    			assertTrue(true);//expected failure
    		}
    	}
    }

    public void testTypePropsDifferent() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("MissingPK.xml"), getConnection());
    	Command select = das.getCommand("get all books");
        try{
    		select.executeQuery();
    		fail("Expected exception");
    	}catch(RuntimeException e){
    		if(e.getMessage().
    				indexOf("in query does not include Primary Key "+
    						"column or has null value in it, can not proceed!") >0){
    			assertTrue(true);//expected failure
    		}
    	}
    }
}