|
7 | 7 | import org.sap.cytoscape.internal.utils.IOUtils; |
8 | 8 |
|
9 | 9 | import java.io.IOException; |
| 10 | +import java.lang.reflect.Field; |
10 | 11 | import java.sql.SQLException; |
11 | 12 | import java.sql.Types; |
12 | 13 | import java.util.*; |
@@ -346,4 +347,135 @@ public void testOnlyEdgeWorkspace(){ |
346 | 347 | Assert.assertEquals(4, sspWorkspace.getNodeTable().size()); |
347 | 348 | } |
348 | 349 |
|
| 350 | + @Test |
| 351 | + public void testIsConnected_falseBeforeConnect() throws IOException { |
| 352 | + HanaConnectionManager freshManager = new HanaConnectionManager(); |
| 353 | + Assert.assertFalse("A freshly created manager must not report as connected", freshManager.isConnected()); |
| 354 | + } |
| 355 | + |
| 356 | + @Test |
| 357 | + public void testGetInstanceIdentifier_nonEmptyAfterConnect() { |
| 358 | + try { |
| 359 | + String identifier = connectionManager.getInstanceIdentifier(); |
| 360 | + Assert.assertNotNull("getInstanceIdentifier must not return null", identifier); |
| 361 | + Assert.assertTrue("getInstanceIdentifier must return a non-empty string", identifier.length() > 0); |
| 362 | + } catch (SQLException e) { |
| 363 | + Assert.fail(e.toString()); |
| 364 | + } |
| 365 | + } |
| 366 | + |
| 367 | + @Test |
| 368 | + public void testLoadGraphWorkspaceByDbObject() { |
| 369 | + HanaGraphWorkspace ws = null; |
| 370 | + try { |
| 371 | + ws = connectionManager.loadGraphWorkspace(new HanaDbObject(testSchema, "SSP")); |
| 372 | + } catch (Exception e) { |
| 373 | + Assert.fail(e.toString()); |
| 374 | + } |
| 375 | + Assert.assertNotNull(ws); |
| 376 | + Assert.assertTrue(ws.isMetadataComplete()); |
| 377 | + Assert.assertEquals(6, ws.getEdgeTable().size()); |
| 378 | + Assert.assertEquals(4, ws.getNodeTable().size()); |
| 379 | + } |
| 380 | + |
| 381 | + @Test |
| 382 | + public void testCreateGraphWorkspace() throws Exception { |
| 383 | + String nodeTableName = "TEST_CW_NODES"; |
| 384 | + String edgeTableName = "TEST_CW_EDGES"; |
| 385 | + String workspaceName = "TEST_CW_WS"; |
| 386 | + |
| 387 | + HanaDbObject nodeTable = new HanaDbObject(testSchema, nodeTableName); |
| 388 | + HanaDbObject edgeTable = new HanaDbObject(testSchema, edgeTableName); |
| 389 | + |
| 390 | + // Create the backing tables |
| 391 | + connectionManager.createTable(nodeTable, Arrays.asList( |
| 392 | + new HanaColumnInfo(testSchema, nodeTableName, "NODE_ID", Types.INTEGER, true) |
| 393 | + )); |
| 394 | + connectionManager.createTable(edgeTable, Arrays.asList( |
| 395 | + new HanaColumnInfo(testSchema, edgeTableName, "EDGE_ID", Types.INTEGER, true), |
| 396 | + new HanaColumnInfo(testSchema, edgeTableName, "SRC", Types.INTEGER, false, true), |
| 397 | + new HanaColumnInfo(testSchema, edgeTableName, "TGT", Types.INTEGER, false, true) |
| 398 | + )); |
| 399 | + |
| 400 | + // Build a HanaGraphWorkspace with all required metadata. |
| 401 | + // nodeTableDbObject and edgeTableDbObject have no setters, so we set them via reflection. |
| 402 | + HanaGraphWorkspace ws = new HanaGraphWorkspace(new HanaDbObject(testSchema, workspaceName)); |
| 403 | + setField(ws, "nodeTableDbObject", nodeTable); |
| 404 | + setField(ws, "edgeTableDbObject", edgeTable); |
| 405 | + ws.addNodeKeyCol(new HanaColumnInfo(testSchema, nodeTableName, "NODE_ID", Types.INTEGER, true)); |
| 406 | + ws.addEdgeKeyCol(new HanaColumnInfo(testSchema, edgeTableName, "EDGE_ID", Types.INTEGER, true)); |
| 407 | + ws.addEdgeSourceCol(new HanaColumnInfo(testSchema, edgeTableName, "SRC", Types.INTEGER, false)); |
| 408 | + ws.addEdgeTargetCol(new HanaColumnInfo(testSchema, edgeTableName, "TGT", Types.INTEGER, false)); |
| 409 | + |
| 410 | + connectionManager.createGraphWorkspace(ws); |
| 411 | + |
| 412 | + // Verify it appears in the listing |
| 413 | + boolean found = false; |
| 414 | + for (HanaDbObject obj : connectionManager.listGraphWorkspaces()) { |
| 415 | + if (obj.schema.equals(testSchema) && obj.name.equals(workspaceName)) { |
| 416 | + found = true; |
| 417 | + break; |
| 418 | + } |
| 419 | + } |
| 420 | + Assert.assertTrue("Created workspace must appear in listGraphWorkspaces()", found); |
| 421 | + } |
| 422 | + |
| 423 | + /** Reflective field setter for package-private / private fields in HanaGraphWorkspace. */ |
| 424 | + private static void setField(Object target, String fieldName, Object value) throws Exception { |
| 425 | + Field f = target.getClass().getDeclaredField(fieldName); |
| 426 | + f.setAccessible(true); |
| 427 | + f.set(target, value); |
| 428 | + } |
| 429 | + |
| 430 | + @Test(expected = SQLException.class) |
| 431 | + public void testExecuteInvalidSqlThrows() throws SQLException { |
| 432 | + connectionManager.execute("THIS IS NOT VALID SQL"); |
| 433 | + } |
| 434 | + |
| 435 | + @Test |
| 436 | + public void testBulkInsertWithNullValue() { |
| 437 | + HanaDbObject targetTable = new HanaDbObject(testSchema, "TEST_BULK_NULL_TABLE"); |
| 438 | + List<HanaColumnInfo> cols = Arrays.asList( |
| 439 | + new HanaColumnInfo(targetTable.schema, targetTable.name, "COL1", Types.NVARCHAR), |
| 440 | + new HanaColumnInfo(targetTable.schema, targetTable.name, "COL2", Types.NVARCHAR) |
| 441 | + ); |
| 442 | + |
| 443 | + Map<String, Object> record = new HashMap<>(); |
| 444 | + record.put("COL1", "present"); |
| 445 | + record.put("COL2", null); // null value — exercises the setNull branch in executeBatch |
| 446 | + |
| 447 | + try { |
| 448 | + connectionManager.createTable(targetTable, cols); |
| 449 | + connectionManager.bulkInsertData(targetTable, cols, Collections.singletonList(record)); |
| 450 | + int count = connectionManager.executeQuerySingleValue(String.format( |
| 451 | + sqlStringsTest.getProperty("COUNT_RECORDS"), |
| 452 | + targetTable.schema, targetTable.name |
| 453 | + ), null, Integer.class); |
| 454 | + Assert.assertEquals(1, count); |
| 455 | + } catch (SQLException e) { |
| 456 | + Assert.fail(e.toString()); |
| 457 | + } |
| 458 | + } |
| 459 | + |
| 460 | + @Test |
| 461 | + public void testCreateTableWithPrimaryKey() { |
| 462 | + String tableName = "TEST_PK_TABLE"; |
| 463 | + HanaDbObject pkTable = new HanaDbObject(testSchema, tableName); |
| 464 | + List<HanaColumnInfo> cols = Arrays.asList( |
| 465 | + new HanaColumnInfo(testSchema, tableName, "ID", Types.INTEGER, true), |
| 466 | + new HanaColumnInfo(testSchema, tableName, "NAME", Types.NVARCHAR) |
| 467 | + ); |
| 468 | + |
| 469 | + try { |
| 470 | + connectionManager.createTable(pkTable, cols); |
| 471 | + HanaQueryResult result = connectionManager.executeQueryList(String.format( |
| 472 | + sqlStringsTest.getProperty("GENERIC_SELECT_PROJECTION"), |
| 473 | + "*", testSchema, tableName |
| 474 | + )); |
| 475 | + Assert.assertEquals(2, result.getColumnMetadata().length); |
| 476 | + } catch (SQLException e) { |
| 477 | + Assert.fail(e.toString()); |
| 478 | + } |
| 479 | + } |
| 480 | + |
349 | 481 | } |
0 commit comments