MongoMetadataEngine extends CommonsMetadataEngine<MongoClient> { @Override protected TableMetadata provideTableMetadata(TableName tableName, ClusterName clusterName, Connection<MongoClient> connection) throws ConnectorException { DB db = connection.getNativeConnection().getDB(tableName.getCatalogName().getName()); DBCollection collection = db.getCollection(tableName.getName()); TableMetadataBuilder tableMetadataBuilder = new TableMetadataBuilder(tableName.getCatalogName().getName(), tableName.getName(), clusterName.getName()); String sampleNumber = connection.getSessionObject(String.class, "sample_probability"); if (sampleNumber == null){ sampleNumber = ConfigurationOptions.SAMPLE_PROBABILITY.getDefaultValue()[0]; } boolean firstField = true; for (Map.Entry<String, String> entry : DiscoverMetadataUtils.discoverFieldsWithType(collection, sampleNumber).entrySet()) { if (entry.getValue().equals("string")) { tableMetadataBuilder.addColumn(entry.getKey(), new ColumnType(DataType.TEXT)); } else if (entry.getValue().equals("boolean")) { tableMetadataBuilder.addColumn(entry.getKey(), new ColumnType(DataType.BOOLEAN)); } else if (entry.getValue().equals("number")) { tableMetadataBuilder.addColumn(entry.getKey(), new ColumnType(DataType.DOUBLE)); } else if (entry.getValue().equals("native")) { tableMetadataBuilder.addColumn(entry.getKey(), new ColumnType(DataType.NATIVE)); } else { continue; } if(firstField){ tableMetadataBuilder.withPartitionKey(entry.getKey()); firstField = false; } } for (IndexMetadata indexMetadata : DiscoverMetadataUtils.discoverIndexes(collection)) { tableMetadataBuilder.addIndex(indexMetadata); } return tableMetadataBuilder.build(); } private  MongoMetadataEngine(MongoConnectionHandler connectionHandler); static MongoMetadataEngine getInstance(MongoConnectionHandler connectionHandler);  }
@Test public void provideTableMetadataTest() throws ConnectorException { DBCollection collection = mock(DBCollection.class); when(client.getDB(DB_NAME)).thenReturn(database); when(database.getCollection(TABLE_NAME)).thenReturn(collection); PowerMockito.mockStatic(DiscoverMetadataUtils.class); when(DiscoverMetadataUtils.discoverFieldsWithType(collection,"1")).thenReturn(fielddWithType); when(DiscoverMetadataUtils.discoverIndexes(collection)).thenReturn( getIndexMetadata(COLUMN_NAME2, IndexType.CUSTOM)); TableMetadata tableMetadataProvided = mongoMetadataEngine.provideTableMetadata(new TableName(DB_NAME, TABLE_NAME), new ClusterName(CLUSTER_NAME), connection); Assert.assertEquals("The table name is not the expected", TABLE_NAME, tableMetadataProvided.getName().getName()); Assert.assertEquals("The cluster name is not the expected", CLUSTER_NAME, tableMetadataProvided.getClusterRef() .getName()); Assert.assertEquals("The catalog name is not the expected", DB_NAME, tableMetadataProvided.getName() .getCatalogName().getName()); Assert.assertEquals("The primary key must have only one field", 1, tableMetadataProvided.getPrimaryKey().size()); Assert.assertEquals("The primary key must be the first field", tableMetadataProvided.getColumns().entrySet().iterator().next().getKey().getName(), tableMetadataProvided.getPrimaryKey().get(0) .getName()); Assert.assertEquals("The columns must have one column", 1, tableMetadataProvided.getColumns().size()); Assert.assertTrue("The column found in existing bson is not the expected", tableMetadataProvided.getColumns() .keySet().contains(new ColumnName(DB_NAME, TABLE_NAME, COLUMN_NAME))); Assert.assertTrue("The column found in existing index is not the expected", tableMetadataProvided.getColumns().keySet().contains(new ColumnName(DB_NAME, TABLE_NAME, COLUMN_NAME))); Assert.assertEquals("There must be a single index", 1, tableMetadataProvided.getIndexes().size()); Assert.assertTrue("The index name is not the expected", tableMetadataProvided.getIndexes().containsKey(new IndexName(DB_NAME, TABLE_NAME, INDEX_NAME))); IndexMetadata indexMetadata = tableMetadataProvided.getIndexes().get( new IndexName(DB_NAME, TABLE_NAME, INDEX_NAME)); Assert.assertEquals("The index type is not the expected", IndexType.CUSTOM, indexMetadata.getType()); Assert.assertEquals("There must be a single column", 1, indexMetadata.getColumns().size()); Assert.assertTrue("There index column is not the expected", indexMetadata.getColumns().containsKey(new ColumnName(DB_NAME, TABLE_NAME, COLUMN_NAME2))); }