StorageUtils { public static Object buildPK(TableMetadata tableMetadata, Row row) throws MongoValidationException { Object pk = null; DBObject bsonPK = null; Object cellValue; List<ColumnName> primaryKeyList = tableMetadata.getPrimaryKey(); if (!primaryKeyList.isEmpty()) { if (primaryKeyList.size() == 1) { cellValue = row.getCell(primaryKeyList.get(0).getName()).getValue(); validatePKDataType(tableMetadata.getColumns().get(primaryKeyList.get(0)).getColumnType()); pk = cellValue; } else { bsonPK = new BasicDBObject(); for (ColumnName columnName : primaryKeyList) { cellValue = row.getCell(columnName.getName()).getValue(); validatePKDataType(tableMetadata.getColumns().get(columnName).getColumnType()); bsonPK.put(columnName.getName(), cellValue); } pk = bsonPK; } } return pk; } private  StorageUtils(); static Object buildPK(TableMetadata tableMetadata, Row row);  }
@Test public void buildPKTest() throws MongoValidationException { TableMetadataBuilder tableMetaBuilder = new TableMetadataBuilder(DB_NAME, COLLECTION_NAME, CLUSTER_NAME); tableMetaBuilder.addColumn(ROW_NAME, VARCHAR_COLUMN_TYPE).addColumn(OTHER_ROW_NAME, INT_COLUMN_TYPE); tableMetaBuilder.withPartitionKey(ROW_NAME); TableMetadata tableMetadata = tableMetaBuilder.build(); Row row = new Row(); row.addCell(ROW_NAME, new Cell(CELL_VALUE)); row.addCell(OTHER_ROW_NAME, new Cell(OTHER_CELL_VALUE)); Object pk = StorageUtils.buildPK(tableMetadata, row); assertEquals("The value is not the expected", CELL_VALUE, (String) pk); }
@Test public void buildCompoundPKTest() throws MongoValidationException { TableMetadataBuilder tableMetaBuilder = new TableMetadataBuilder(DB_NAME, COLLECTION_NAME, CLUSTER_NAME); tableMetaBuilder.addColumn(ROW_NAME, VARCHAR_COLUMN_TYPE).addColumn(OTHER_ROW_NAME, INT_COLUMN_TYPE); tableMetaBuilder.withPartitionKey(ROW_NAME); tableMetaBuilder.withClusterKey(OTHER_ROW_NAME); TableMetadata tableMetadata = tableMetaBuilder.build(); Row row = new Row(); row.addCell(ROW_NAME, new Cell(CELL_VALUE)); row.addCell(OTHER_ROW_NAME, new Cell(INTEGER_CELL_VALUE)); DBObject pk = (DBObject) StorageUtils.buildPK(tableMetadata, row); assertTrue("The primary key must contain " + ROW_NAME, pk.containsField(ROW_NAME)); assertTrue("The primary key must contain " + OTHER_ROW_NAME, pk.containsField(OTHER_ROW_NAME)); assertEquals("The string value is not the expected", CELL_VALUE, pk.get(ROW_NAME)); assertEquals("The integer value is not the expected", INTEGER_CELL_VALUE, pk.get(OTHER_ROW_NAME)); }
@Test(expected = MongoValidationException.class) public void buildMalformedPKTest() throws MongoValidationException { TableMetadataBuilder tableMetaBuilder = new TableMetadataBuilder(DB_NAME, COLLECTION_NAME, CLUSTER_NAME); tableMetaBuilder.addColumn(ROW_NAME, VARCHAR_COLUMN_TYPE).addColumn(OTHER_ROW_NAME, new ColumnType(DataType.SET)); tableMetaBuilder.withPartitionKey(OTHER_ROW_NAME); TableMetadata tableMetadata = tableMetaBuilder.build(); Row row = new Row(); row.addCell(OTHER_ROW_NAME, new Cell(Collections.EMPTY_SET)); StorageUtils.buildPK(tableMetadata, row); }