SLING-5437 add connection check for couchbase resource provider
define separate NoSqlAdapter methods for creating index definitions, to ensure they are only executed after connection test succeeds
set versions to 1.1.0

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1725576 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index ab83eae..1750693 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
     </parent>
 
     <artifactId>org.apache.sling.nosql.mongodb-resourceprovider</artifactId>
-    <version>1.0.1-SNAPSHOT</version>
+    <version>1.1.0-SNAPSHOT</version>
     <packaging>bundle</packaging>
   
     <name>Apache Sling NoSQL MongoDB Resource Provider</name>
@@ -49,7 +49,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.nosql.generic</artifactId>
-            <version>1.0.1-SNAPSHOT</version>
+            <version>1.1.0-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
     
diff --git a/src/main/java/org/apache/sling/nosql/mongodb/resourceprovider/impl/MongoDBNoSqlAdapter.java b/src/main/java/org/apache/sling/nosql/mongodb/resourceprovider/impl/MongoDBNoSqlAdapter.java
index ab66fc1..9b993e4 100644
--- a/src/main/java/org/apache/sling/nosql/mongodb/resourceprovider/impl/MongoDBNoSqlAdapter.java
+++ b/src/main/java/org/apache/sling/nosql/mongodb/resourceprovider/impl/MongoDBNoSqlAdapter.java
@@ -66,32 +66,6 @@
     public MongoDBNoSqlAdapter(MongoClient mongoClient, String database, String collection) {
         MongoDatabase db = mongoClient.getDatabase(database);
         this.collection = db.getCollection(collection);
-        
-        // create index on parent path field (if it does not exist yet)
-        try {
-            Document parenPathtIndex = new Document(PN_PARENT_PATH, 1);
-            this.collection.createIndex(parenPathtIndex);
-        }
-        catch (DuplicateKeyException ex) {
-            // index already exists, ignore
-        }
-        catch (Throwable ex) {
-            log.error("Unable to create index on " + PN_PARENT_PATH + ": " + ex.getMessage(), ex);
-        }
-        
-        // create unique index on path field (if it does not exist yet)
-        try {
-            Document pathIndex = new Document(PN_PATH, 1);
-            IndexOptions idxOptions = new IndexOptions();
-            idxOptions.unique(true);
-            this.collection.createIndex(pathIndex, idxOptions);
-        }
-        catch (DuplicateKeyException ex) {
-            // index already exists, ignore
-        }
-        catch (Throwable ex) {
-            log.error("Unable to create unique index on " + PN_PATH + ": " + ex.getMessage(), ex);
-        }
     }
 
     @Override
@@ -156,4 +130,34 @@
             throw new LoginException(e);
         }
     }
+
+    @Override
+    public void createIndexDefinitions() {
+        // create index on parent path field (if it does not exist yet)
+        try {
+            Document parenPathtIndex = new Document(PN_PARENT_PATH, 1);
+            this.collection.createIndex(parenPathtIndex);
+        }
+        catch (DuplicateKeyException ex) {
+            // index already exists, ignore
+        }
+        catch (Throwable ex) {
+            log.error("Unable to create index on " + PN_PARENT_PATH + ": " + ex.getMessage(), ex);
+        }
+        
+        // create unique index on path field (if it does not exist yet)
+        try {
+            Document pathIndex = new Document(PN_PATH, 1);
+            IndexOptions idxOptions = new IndexOptions();
+            idxOptions.unique(true);
+            this.collection.createIndex(pathIndex, idxOptions);
+        }
+        catch (DuplicateKeyException ex) {
+            // index already exists, ignore
+        }
+        catch (Throwable ex) {
+            log.error("Unable to create unique index on " + PN_PATH + ": " + ex.getMessage(), ex);
+        }
+    }
+
 }
diff --git a/src/test/java/org/apache/sling/nosql/mongodb/resourceprovider/integration/IndexCreationIT.java b/src/test/java/org/apache/sling/nosql/mongodb/resourceprovider/integration/IndexCreationIT.java
index e99ec8f..28f1256 100644
--- a/src/test/java/org/apache/sling/nosql/mongodb/resourceprovider/integration/IndexCreationIT.java
+++ b/src/test/java/org/apache/sling/nosql/mongodb/resourceprovider/integration/IndexCreationIT.java
@@ -21,7 +21,8 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
-import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.sling.nosql.mongodb.resourceprovider.impl.MongoDBNoSqlAdapter;
 import org.bson.Document;
@@ -29,6 +30,7 @@
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
 import com.mongodb.MongoClient;
 
 /**
@@ -43,12 +45,14 @@
 	private String collection;
 		
 	@Before
-	public void setUp() {
+	public void setUp() throws Exception {
 	    String connectionString = System.getProperty("connectionString", "localhost:27017");
         database =  System.getProperty("database", "sling") + "_indextest";
         collection = System.getProperty("collection", "resources");
 		mongoClient = new MongoClient(connectionString);
 		underTest = new MongoDBNoSqlAdapter(mongoClient, database, collection);
+		underTest.checkConnection();
+		underTest.createIndexDefinitions();
 	}
 
 	@After
@@ -61,17 +65,14 @@
 	public void testIndexesPresent() {
 		assertNotNull(underTest);
 		
-		//expecting 2 indexes (_id, parentPath)
-		int expected = 2;
-		int actual = 0;
+		final List<String> expectedIndexNames= ImmutableList.<String>of("_id_", "parentPath_1");
 		
-		final String[] expectedIndexesNames=  {"_id_", "parentPath_1"};
-		
-		for (Document d : mongoClient.getDatabase(database).getCollection(collection).listIndexes()){
-			assert Arrays.asList(expectedIndexesNames).contains( d.get("name") );
-			actual++;
+		List<String> actualIndexNames = new ArrayList<String>();
+		for (Document d : mongoClient.getDatabase(database).getCollection(collection).listIndexes()) {
+		    actualIndexNames.add(d.get("name").toString());
 		}
-		assertEquals(expected, actual);
+		
+		assertEquals(expectedIndexNames, actualIndexNames);
 	}
 
 }