IndexUtils { public static DBObject getCustomOptions(IndexMetadata indexMetadata) throws MongoValidationException { DBObject indexOptionsDBObject = new BasicDBObject(); String indexName = indexMetadata.getName().getName(); Map<String, Selector> options = SelectorOptionsUtils.processOptions(indexMetadata.getOptions()); if (options != null) { Selector boolSelector = options.get(SPARSE.getOptionName()); if (boolSelector != null) { indexOptionsDBObject.put("sparse", ((BooleanSelector) boolSelector).getValue()); } boolSelector = options.get(UNIQUE.getOptionName()); if (boolSelector != null) { indexOptionsDBObject.put("unique", ((BooleanSelector) boolSelector).getValue()); } } if (indexName != null && !indexName.trim().isEmpty()) { indexOptionsDBObject.put("name", indexName); } return indexOptionsDBObject; } private  IndexUtils(); static DBObject getCustomOptions(IndexMetadata indexMetadata); static DBObject getIndexDBObject(IndexMetadata indexMetadata); static void dropIndexWithDefaultName(IndexMetadata indexMetadata, DB db);  }
@Test public void getBasicCustomOptionsTest() throws MongoValidationException { IndexMetadataBuilder indexMetaBuilder = new IndexMetadataBuilder(DB_NAME, TABLE_NAME, INDEX_NAME, INDEX_DEFAULT); indexMetaBuilder.addColumn(COLUMN_NAME, COLUMN_VARCHAR); IndexMetadata indexMetadata = indexMetaBuilder.build(); DBObject object = IndexUtils.getCustomOptions(indexMetadata); assertEquals("The index name is not the expected", object.get("name"), INDEX_NAME); }
@Test public void getCustomOptionsTest() throws MongoValidationException { IndexMetadataBuilder indexMetaBuilder = new IndexMetadataBuilder(DB_NAME, TABLE_NAME, INDEX_NAME, INDEX_DEFAULT); indexMetaBuilder.addColumn(COLUMN_NAME, COLUMN_VARCHAR); indexMetaBuilder.addOption(IndexOptions.SPARSE.getOptionName(), true).addOption( IndexOptions.UNIQUE.getOptionName(), false); IndexMetadata indexMetadata = indexMetaBuilder.build(); DBObject object = IndexUtils.getCustomOptions(indexMetadata); assertEquals("The index name is not the expected", object.get("name"), INDEX_NAME); assertEquals("The 'sparse' property should be true", object.get("sparse"), true); assertEquals("The 'unique' property should be false", object.get("unique"), false); }