IndexUtils { public static DBObject getIndexDBObject(IndexMetadata indexMetadata) throws MongoValidationException { IndexType indexType = indexMetadata.getType(); DBObject indexDBObject = new BasicDBObject(); if (indexType == IndexType.DEFAULT) { for (ColumnMetadata columnMeta : indexMetadata.getColumns().values()) { indexDBObject.put(columnMeta.getName().getName(), 1); } } else if (indexType == IndexType.FULL_TEXT) { for (ColumnMetadata columnMeta : indexMetadata.getColumns().values()) { indexDBObject.put(columnMeta.getName().getName(), "text"); } } else if (indexMetadata.getType() == IndexType.CUSTOM) { indexDBObject = getCustomIndexDBObject(indexMetadata); } else { String msg = "Index type " + indexMetadata.getType().toString() + " is not supported"; LOGGER.error(msg); throw new MongoValidationException(msg); } return indexDBObject; } private  IndexUtils(); static DBObject getCustomOptions(IndexMetadata indexMetadata); static DBObject getIndexDBObject(IndexMetadata indexMetadata); static void dropIndexWithDefaultName(IndexMetadata indexMetadata, DB db);  }
@Test public void getIndexDBObjectDefaultTest() throws MongoValidationException { IndexMetadataBuilder indexMetaBuilder = new IndexMetadataBuilder(DB_NAME, TABLE_NAME, INDEX_NAME, INDEX_DEFAULT); indexMetaBuilder.addColumn(COLUMN_NAME, COLUMN_VARCHAR); indexMetaBuilder.addColumn(COLUMN_NAME2, COLUMN_VARCHAR); IndexMetadata indexMetadata = indexMetaBuilder.build(); DBObject object = IndexUtils.getIndexDBObject(indexMetadata); assertEquals("The value of the default index column should be 1", 1, object.get(COLUMN_NAME)); assertEquals("The value of the default index column should be 1", 1, object.get(COLUMN_NAME2)); }
@Test public void getIndexDBObjectFullTextTest() throws MongoValidationException { IndexMetadataBuilder indexMetaBuilder = new IndexMetadataBuilder(DB_NAME, TABLE_NAME, INDEX_NAME, IndexType.FULL_TEXT); indexMetaBuilder.addColumn(COLUMN_NAME, COLUMN_VARCHAR); IndexMetadata indexMetadata = indexMetaBuilder.build(); DBObject object = IndexUtils.getIndexDBObject(indexMetadata); assertEquals("The value of the full-text index column should be 'text'", "text", object.get(COLUMN_NAME)); }
@Test public void getHashedIndexDBObjectTest() throws MongoValidationException { IndexMetadataBuilder indexMetaBuilder = new IndexMetadataBuilder(DB_NAME, TABLE_NAME, INDEX_NAME, IndexType.CUSTOM); indexMetaBuilder.addColumn(COLUMN_NAME, COLUMN_VARCHAR); indexMetaBuilder.addOption(IndexOptions.INDEX_TYPE.getOptionName(), CustomMongoIndexType.HASHED.getIndexType()); IndexMetadata indexMetadata = indexMetaBuilder.build(); DBObject object = IndexUtils.getIndexDBObject(indexMetadata); assertEquals("The value of the hashed index column should be 'hashed'", "hashed", object.get(COLUMN_NAME)); }
@Test public void getCustomDescendIndexDBObjectTest() throws MongoValidationException { IndexMetadataBuilder indexMetaBuilder = new IndexMetadataBuilder(DB_NAME, TABLE_NAME, INDEX_NAME, IndexType.CUSTOM); indexMetaBuilder.addColumn(COLUMN_NAME, COLUMN_VARCHAR); indexMetaBuilder.addOption(IndexOptions.INDEX_TYPE.getOptionName(), CustomMongoIndexType.COMPOUND.getIndexType()); indexMetaBuilder.addOption(IndexOptions.COMPOUND_FIELDS.getOptionName(), COLUMN_NAME + ":desc"); IndexMetadata indexMetadata = indexMetaBuilder.build(); DBObject object = IndexUtils.getIndexDBObject(indexMetadata); assertEquals("The value of the descending index column should be '-1'", -1, object.get(COLUMN_NAME)); }
@Test public void getCustomCompoundIndexDBObjectTest() throws MongoValidationException { IndexMetadataBuilder indexMetaBuilder = new IndexMetadataBuilder(DB_NAME, TABLE_NAME, INDEX_NAME, IndexType.CUSTOM); indexMetaBuilder.addOption(IndexOptions.INDEX_TYPE.getOptionName(), CustomMongoIndexType.COMPOUND.getIndexType()); indexMetaBuilder.addOption(IndexOptions.COMPOUND_FIELDS.getOptionName(), COLUMN_NAME + ":desc," + COLUMN_NAME2 + ":asc"); IndexMetadata indexMetadata = indexMetaBuilder.build(); DBObject object = IndexUtils.getIndexDBObject(indexMetadata); assertEquals("The value of the descending index " + COLUMN_NAME + " should be '-1'", -1, object.get(COLUMN_NAME)); assertEquals("The value of the ascending index " + COLUMN_NAME2 + " should be '1'", 1, object.get(COLUMN_NAME2)); }