MongoMetadataEngine extends CommonsMetadataEngine<MongoClient> { @Override protected void alterTable(TableName tableName, AlterOptions alterOptions, Connection<MongoClient> connection) throws ExecutionException { DB db = connection.getNativeConnection().getDB(tableName.getCatalogName().getName()); DBCollection collection = db.getCollection(tableName.getName()); switch (alterOptions.getOption()) { case ADD_COLUMN: break; case ALTER_COLUMN: throw new MongoValidationException("Alter options is not supported"); case DROP_COLUMN: String name = alterOptions.getColumnMetadata().getName().getName(); collection.updateMulti(new BasicDBObject(), AlterOptionsUtils.buildDropColumnDBObject(name)); break; case ALTER_OPTIONS: default: throw new MongoValidationException("Alter options is not supported"); } } private  MongoMetadataEngine(MongoConnectionHandler connectionHandler); static MongoMetadataEngine getInstance(MongoConnectionHandler connectionHandler);  }
@Test(expected = ExecutionException.class) public void alterTableAlterOptionsTest() throws UnsupportedException, ExecutionException { TableName tableName = new TableName(DB_NAME, TABLE_NAME); AlterOptions alterOptions = new AlterOptions(AlterOperation.ALTER_COLUMN, null, mock(ColumnMetadata.class)); DBCollection collection = mock(DBCollection.class); when(client.getDB(DB_NAME)).thenReturn(database); when(database.getCollection(TABLE_NAME)).thenReturn(collection); mongoMetadataEngine.alterTable(tableName, alterOptions, connection); }
@Test(expected = ExecutionException.class) public void alterTableAlterColumnTest() throws UnsupportedException, ExecutionException { TableName tableName = new TableName(DB_NAME, TABLE_NAME); AlterOptions alterOptions = new AlterOptions(AlterOperation.ALTER_OPTIONS, null, mock(ColumnMetadata.class)); DBCollection collection = mock(DBCollection.class); when(client.getDB(DB_NAME)).thenReturn(database); when(database.getCollection(TABLE_NAME)).thenReturn(collection); mongoMetadataEngine.alterTable(tableName, alterOptions, connection); }
@Test public void alterTableDropColumnTest() throws UnsupportedException, ExecutionException { TableName tableName = new TableName(DB_NAME, TABLE_NAME); ColumnMetadata columnMetadata = new ColumnMetadata(new ColumnName(DB_NAME, TABLE_NAME, COLUMN_NAME), new Object[0], new ColumnType(DataType.INT)); AlterOptions alterOptions = new AlterOptions(AlterOperation.DROP_COLUMN, null, columnMetadata); DBCollection collection = mock(DBCollection.class); when(client.getDB(DB_NAME)).thenReturn(database); when(database.getCollection(TABLE_NAME)).thenReturn(collection); DBObject dropColumn = new BasicDBObject(COLUMN_NAME, new BasicDBObject("$unset", new BasicDBObject())); PowerMockito.mockStatic(AlterOptionsUtils.class); Mockito.when(AlterOptionsUtils.buildDropColumnDBObject(COLUMN_NAME)).thenReturn(dropColumn); mongoMetadataEngine.alterTable(tableName, alterOptions, connection); verify(collection, times(1)).updateMulti(any(BasicDBObject.class), Matchers.eq(dropColumn)); }