Skip to content

Commit 3a67970

Browse files
o. Error message changes for table and entity mismatch
o. Removed table creation from put operation and updated unit test failure for the same
1 parent 9833d4a commit 3a67970

File tree

8 files changed

+257
-193
lines changed

8 files changed

+257
-193
lines changed

src/main/java/com/oracle/nosql/spring/data/core/NosqlTemplate.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,8 @@ public String getTableName(Class<?> domainClass) {
9393
@Override
9494
public boolean createTableIfNotExists(
9595
NosqlEntityInformation<?, ?> entityInformation) {
96-
String ddl = getCreateTableDDL(entityInformation);
97-
try {
98-
doCheckExistingTable(entityInformation);
99-
} catch (IllegalArgumentException iae) {
100-
String msg = String.format("Error executing DDL '%s': Table %s " +
101-
"exists but definitions do not match : %s", ddl,
102-
entityInformation.getTableName(), iae.getMessage());
103-
throw new IllegalArgumentException(msg, iae);
104-
}
105-
return doCreateTable(entityInformation, ddl);
96+
doCheckExistingTable(entityInformation);
97+
return doCreateTable(entityInformation);
10698
}
10799

108100
@SuppressWarnings("unchecked")

src/main/java/com/oracle/nosql/spring/data/core/NosqlTemplateBase.java

Lines changed: 140 additions & 136 deletions
Large diffs are not rendered by default.

src/main/java/com/oracle/nosql/spring/data/core/ReactiveNosqlTemplate.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,8 @@ public String getTableName(Class<?> domainClass) {
7878
public Mono<Boolean> createTableIfNotExists(
7979
NosqlEntityInformation<?, ?> entityInformation) {
8080
Assert.notNull(entityInformation, "Entity information should not be null");
81-
82-
String ddl = getCreateTableDDL(entityInformation);
83-
try {
84-
doCheckExistingTable(entityInformation);
85-
} catch (IllegalArgumentException iae) {
86-
String msg = String.format("Error executing DDL '%s': Table %s " +
87-
"exists but definitions do not match : %s" , ddl,
88-
entityInformation.getTableName(), iae.getMessage());
89-
throw new IllegalArgumentException(msg, iae);
90-
}
91-
return Mono.just(doCreateTable(entityInformation, ddl));
81+
doCheckExistingTable(entityInformation);
82+
return Mono.just(doCreateTable(entityInformation));
9283
}
9384

9485
/**

src/test/java/com/oracle/nosql/spring/data/test/TestTTL.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public void setup() {
4747
template.dropTableIfExists(EntityWith10DaysTTL.class.getSimpleName());
4848
template.dropTableIfExists(EntityWithDefaultTTL.class.getSimpleName());
4949
template.dropTableIfExists(EntityWithNegativeTTL.class.getSimpleName());
50+
51+
template.createTableIfNotExists(template.
52+
getNosqlEntityInformation(EntityWith10DaysTTL.class));
53+
template.createTableIfNotExists(template.
54+
getNosqlEntityInformation(EntityWithDefaultTTL.class));
5055
}
5156

5257
@After

src/test/java/com/oracle/nosql/spring/data/test/composite/MachineApp.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public static void staticSetup() throws ClassNotFoundException {
5353
@Before
5454
public void setup() {
5555
template.dropTableIfExists(Machine.class.getSimpleName());
56-
56+
template.createTableIfNotExists(template.
57+
getNosqlEntityInformation(Machine.class));
5758
machineCache = new HashMap<>();
5859
List<IpAddress> routeAddress = new ArrayList<>();
5960
routeAddress.add(new IpAddress("127.0.0.1"));

src/test/java/com/oracle/nosql/spring/data/test/composite/MachineAppWithoutAnnotation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public static void staticSetup() throws ClassNotFoundException {
5151
@Before
5252
public void setup() {
5353
template.dropTableIfExists(MachineWithoutAnnotation.class.getSimpleName());
54+
template.createTableIfNotExists(template.
55+
getNosqlEntityInformation(MachineWithoutAnnotation.class));
5456
}
5557

5658
@After

src/test/java/com/oracle/nosql/spring/data/test/composite/ReactiveMachineApp.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public static void staticSetup() throws ClassNotFoundException {
5151
@Before
5252
public void setup() {
5353
template.dropTableIfExists(Machine.class.getSimpleName());
54+
template.createTableIfNotExists(template.
55+
getNosqlEntityInformation(Machine.class));
5456
machineCache = new HashMap<>();
5557
List<IpAddress> routeAddress = new ArrayList<>();
5658
routeAddress.add(new IpAddress("127.0.0.1"));

src/test/java/com/oracle/nosql/spring/data/test/composite/TestTableCreation.java

Lines changed: 102 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2424

2525
import static junit.framework.TestCase.assertEquals;
26+
import static junit.framework.TestCase.assertTrue;
2627
import static junit.framework.TestCase.fail;
2728

2829
/**
@@ -101,12 +102,11 @@ public void testCompositeEntityWithShardKey() {
101102
public void testCompositeEntityWithNoShardKey() {
102103
Class<?> domainClass = CompositeEntityWithNoShardKey.class;
103104
try {
104-
NosqlEntityInformation<?, ?> entityInformation =
105-
template.getNosqlEntityInformation(domainClass);
106-
template.createTableIfNotExists(entityInformation);
105+
template.getNosqlEntityInformation(domainClass);
107106
fail("Expecting IllegalArgumentException but didn't get");
108-
} catch (IllegalArgumentException ignored) {
109-
107+
} catch (IllegalArgumentException iae) {
108+
assertTrue(iae.getMessage().contains(
109+
"At least one of the @NosqlKey must be shard key"));
110110
}
111111
}
112112

@@ -133,35 +133,36 @@ public void testCompositeEntityWithOrder() {
133133
public void testCompositeEntityWithMultipleKeys() {
134134
Class<?> domainClass = CompositeEntityWithMultipleKeys.class;
135135
try {
136-
NosqlEntityInformation<?, ?> entityInformation =
137-
template.getNosqlEntityInformation(domainClass);
136+
template.getNosqlEntityInformation(domainClass);
138137
fail("Expecting IllegalArgumentException but didn't get");
139-
} catch (IllegalArgumentException ignored) {
140-
138+
} catch (IllegalArgumentException iae) {
139+
assertTrue(iae.getMessage().contains(
140+
"Order of non shard keys must be greater than all the" +
141+
" shard keys"));
141142
}
142143
}
143144

144145
@Test
145146
public void testCompositeEntityWithRepeatingOrder() {
146147
Class<?> domainClass = CompositeEntityWithRepeatingOrder.class;
147148
try {
148-
NosqlEntityInformation<?, ?> entityInformation =
149-
template.getNosqlEntityInformation(domainClass);
149+
template.getNosqlEntityInformation(domainClass);
150150
fail("Expecting IllegalArgumentException but didn't get");
151-
} catch (IllegalArgumentException ignored) {
152-
151+
} catch (IllegalArgumentException iae) {
152+
assertTrue(iae.getMessage().contains("Order of keys must be " +
153+
"unique"));
153154
}
154155
}
155156

156157
@Test
157158
public void testCompositeEntityWithMissingOrder() {
158159
Class<?> domainClass = CompositeEntityWithMissingOrder.class;
159160
try {
160-
NosqlEntityInformation<?, ?> entityInformation =
161-
template.getNosqlEntityInformation(domainClass);
161+
template.getNosqlEntityInformation(domainClass);
162162
fail("Expecting IllegalArgumentException but didn't get");
163-
} catch (IllegalArgumentException ignored) {
164-
163+
} catch (IllegalArgumentException iae) {
164+
assertTrue(iae.getMessage().contains(
165+
"If order is specified, it must be specified on all key"));
165166
}
166167
}
167168

@@ -173,8 +174,9 @@ public void testCompositeEntityWithMissingNonShardOrder() {
173174
template.getNosqlEntityInformation(domainClass);
174175
template.createTableIfNotExists(entityInformation);
175176
fail("Expecting IllegalArgumentException but didn't get");
176-
} catch (IllegalArgumentException ignored) {
177-
177+
} catch (IllegalArgumentException iae) {
178+
assertTrue(iae.getMessage().contains(
179+
"If order is specified, it must be specified on all key"));
178180
}
179181
}
180182

@@ -218,24 +220,26 @@ public void testCompositeEntityCaseInsensitive() {
218220

219221
@Test
220222
public void testCompositeKeyCollision() {
221-
//shard and non shard key collision
223+
// shard and non shard key collision
222224
Class<?> domainClass = CompositeEntityFieldCollision.class;
223225
try {
224226
template.getNosqlEntityInformation(domainClass);
225227
fail("Expecting IllegalArgumentException but didn't get");
226-
} catch (IllegalArgumentException ignored) {
227-
228+
} catch (IllegalArgumentException iae) {
229+
assertTrue(iae.getMessage().contains("Conflicting name"));
228230
}
229231
}
230232

231233
@Test
232234
public void testCompositeEntityKvJsonField() {
233-
//shard and non shard key collision
235+
// shard and non shard key collision
234236
Class<?> domainClass = CompositeEntityKvJsonField.class;
235237
try {
236238
template.getNosqlEntityInformation(domainClass);
237239
fail("Expecting IllegalArgumentException but didn't get");
238-
} catch (IllegalArgumentException ignored) {
240+
} catch (IllegalArgumentException iae) {
241+
assertTrue(iae.getMessage().contains(
242+
"composite key can not be named 'kv_json_'"));
239243
}
240244
}
241245

@@ -253,7 +257,9 @@ public void testTableDDLMismatchOrderOfShardKeys() {
253257
template.getNosqlClient().doTableRequest(tableRequest, 10000, 2000);
254258
template.createTableIfNotExists(template.getNosqlEntityInformation(domainClass));
255259
fail("Expecting IllegalArgumentException but didn't get");
256-
} catch (IllegalArgumentException ignored) {
260+
} catch (IllegalArgumentException iae) {
261+
assertTrue(iae.getMessage().contains("Shard primary keys " +
262+
"mismatch"));
257263
template.dropTableIfExists(domainClass.getSimpleName());
258264
}
259265
}
@@ -273,7 +279,9 @@ public void testTableDDLMismatchOrderOfNonShardKeys() {
273279
template.getNosqlClient().doTableRequest(tableRequest, 10000, 2000);
274280
template.createTableIfNotExists(template.getNosqlEntityInformation(domainClass));
275281
fail("Expecting IllegalArgumentException but didn't get");
276-
} catch (IllegalArgumentException ignored) {
282+
} catch (IllegalArgumentException iae) {
283+
assertTrue(iae.getMessage().contains("Non-shard primary keys " +
284+
"mismatch"));
277285
template.dropTableIfExists(domainClass.getSimpleName());
278286
}
279287
}
@@ -292,7 +300,9 @@ public void testTableDDLMismatchTypeOfShardKey() {
292300
template.getNosqlClient().doTableRequest(tableRequest, 10000, 2000);
293301
template.createTableIfNotExists(template.getNosqlEntityInformation(domainClass));
294302
fail("Expecting IllegalArgumentException but didn't get");
295-
} catch (IllegalArgumentException ignored) {
303+
} catch (IllegalArgumentException iae) {
304+
assertTrue(iae.getMessage().contains("Shard primary keys " +
305+
"mismatch"));
296306
template.dropTableIfExists(domainClass.getSimpleName());
297307
}
298308
}
@@ -313,7 +323,9 @@ public void testTableDDLMismatchTypeOfNonShardKey() {
313323
template.getNosqlClient().doTableRequest(tableRequest, 10000, 2000);
314324
template.createTableIfNotExists(template.getNosqlEntityInformation(domainClass));
315325
fail("Expecting IllegalArgumentException but didn't get");
316-
} catch (IllegalArgumentException ignored) {
326+
} catch (IllegalArgumentException iae) {
327+
assertTrue(iae.getMessage().contains(
328+
"Non-shard primary keys mismatch"));
317329
template.dropTableIfExists(domainClass.getSimpleName());
318330
}
319331
}
@@ -332,7 +344,9 @@ public void testTableMismatchDifferentColumn() {
332344
template.getNosqlClient().doTableRequest(tableRequest, 10000, 2000);
333345
template.createTableIfNotExists(template.getNosqlEntityInformation(domainClass));
334346
fail("Expecting IllegalArgumentException but didn't get");
335-
} catch (IllegalArgumentException ignored) {
347+
} catch (IllegalArgumentException iae) {
348+
assertTrue(iae.getMessage().contains("Shard primary keys " +
349+
"mismatch"));
336350
template.dropTableIfExists(domainClass.getSimpleName());
337351
}
338352
}
@@ -350,7 +364,9 @@ public void testTableMismatchMissingColumn() {
350364
template.getNosqlClient().doTableRequest(tableRequest, 10000, 2000);
351365
template.createTableIfNotExists(template.getNosqlEntityInformation(domainClass));
352366
fail("Expecting IllegalArgumentException but didn't get");
353-
} catch (IllegalArgumentException ignored) {
367+
} catch (IllegalArgumentException iae) {
368+
assertTrue(iae.getMessage().contains("Shard primary keys " +
369+
"mismatch"));
354370
template.dropTableIfExists(domainClass.getSimpleName());
355371
}
356372
}
@@ -369,7 +385,11 @@ public void testTableMismatchMissingJson() {
369385
template.getNosqlClient().doTableRequest(tableRequest, 10000, 2000);
370386
template.createTableIfNotExists(template.getNosqlEntityInformation(domainClass));
371387
fail("Expecting IllegalArgumentException but didn't get");
372-
} catch (IllegalArgumentException ignored) {
388+
} catch (IllegalArgumentException iae) {
389+
assertTrue(iae.getMessage().contains("Shard primary keys " +
390+
"mismatch"));
391+
assertTrue(iae.getMessage().contains("Non-primary key columns " +
392+
"mismatch"));
373393
template.dropTableIfExists(domainClass.getSimpleName());
374394
}
375395
}
@@ -382,13 +402,41 @@ public void testTableMismatchMissingJsonType() {
382402
"KV_json_ LONG, PRIMARY KEY(ID1,ID2))",
383403
domainClass.getSimpleName());
384404
tableRequest.setStatement(ddl);
385-
tableRequest.setTableLimits(new TableLimits(100,100,1));
405+
tableRequest.setTableLimits(new TableLimits(100, 100, 1));
386406
try {
387407
template.dropTableIfExists(domainClass.getSimpleName());
388408
template.getNosqlClient().doTableRequest(tableRequest, 10000, 2000);
389409
template.createTableIfNotExists(template.getNosqlEntityInformation(domainClass));
390410
fail("Expecting IllegalArgumentException but didn't get");
391-
} catch (IllegalArgumentException ignored) {
411+
} catch (IllegalArgumentException iae) {
412+
assertTrue(iae.getMessage().contains("Non-primary key columns " +
413+
"mismatch"));
414+
template.dropTableIfExists(domainClass.getSimpleName());
415+
}
416+
}
417+
418+
@Test
419+
public void testTableMismatchAll() {
420+
Class<?> domainClass = MismatchEntity.class;
421+
TableRequest tableRequest = new TableRequest();
422+
String ddl = String.format("CREATE TABLE %s (id1 STRING, id2 STRING, " +
423+
"KV_json_ LONG, age integer, PRIMARY KEY(SHARD(ID1)," +
424+
"ID2))",
425+
domainClass.getSimpleName());
426+
tableRequest.setStatement(ddl);
427+
tableRequest.setTableLimits(new TableLimits(100, 100, 1));
428+
try {
429+
template.dropTableIfExists(domainClass.getSimpleName());
430+
template.getNosqlClient().doTableRequest(tableRequest, 10000, 2000);
431+
template.createTableIfNotExists(template.getNosqlEntityInformation(domainClass));
432+
fail("Expecting IllegalArgumentException but didn't get");
433+
} catch (IllegalArgumentException iae) {
434+
assertTrue(iae.getMessage().contains("Shard primary keys " +
435+
"mismatch"));
436+
assertTrue(iae.getMessage().contains("Non-shard primary keys " +
437+
"mismatch"));
438+
assertTrue(iae.getMessage().contains("Non-primary key columns " +
439+
"mismatch"));
392440
template.dropTableIfExists(domainClass.getSimpleName());
393441
}
394442
}
@@ -408,7 +456,9 @@ public void testTableMismatchIdentity() {
408456
template.getNosqlClient().doTableRequest(tableRequest, 10000, 2000);
409457
template.createTableIfNotExists(template.getNosqlEntityInformation(domainClass));
410458
fail("Expecting IllegalArgumentException but didn't get");
411-
} catch (IllegalArgumentException ignored) {
459+
} catch (IllegalArgumentException iae) {
460+
assertTrue(iae.getMessage().contains("Identity information " +
461+
"mismatch"));
412462
template.dropTableIfExists(domainClass.getSimpleName());
413463
}
414464
}
@@ -428,7 +478,9 @@ public void testTableMismatchIdentity1() {
428478
template.getNosqlClient().doTableRequest(tableRequest, 10000, 2000);
429479
template.createTableIfNotExists(template.getNosqlEntityInformation(domainClass));
430480
fail("Expecting IllegalArgumentException but didn't get");
431-
} catch (IllegalArgumentException ignored) {
481+
} catch (IllegalArgumentException iae) {
482+
assertTrue(iae.getMessage().contains("Identity information " +
483+
"mismatch"));
432484
template.dropTableIfExists(domainClass.getSimpleName());
433485
}
434486
}
@@ -751,4 +803,19 @@ public static class TTLEntity1 {
751803
@NosqlId(generated = true)
752804
private int id;
753805
}
806+
807+
@NosqlTable
808+
public static class MismatchEntity {
809+
@NosqlId
810+
private MismatchKey id;
811+
}
812+
813+
public static class MismatchKey {
814+
@NosqlKey(shardKey = true, order = 0)
815+
private int i;
816+
@NosqlKey(shardKey = true, order = 1)
817+
private int j;
818+
@NosqlKey(shardKey = false, order = 3)
819+
private int k;
820+
}
754821
}

0 commit comments

Comments
 (0)