/* Copyright (C) 2010 Robert Stewart (robert@wombatnation.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.log4mongo;

import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import org.apache.log4j.PropertyConfigurator;
import org.junit.*;

/**
 * Authentication-related JUnit unit tests for MongoDbAppender.
 * <p>
 * Note: these tests require that a MongoDB server is running, and (by default) assumes that server
 * is listening on the default port (27017) on localhost.
 *
 * @author Robert Stewart (robert@wombatnation.com)
 */
public class TestMongoDbAppenderAuth {

    private final static String TEST_MONGO_SERVER_HOSTNAME = "localhost";

    private final static int TEST_MONGO_SERVER_PORT = 27017;

    private final static String TEST_DATABASE_NAME = "log4mongotestauth";

    private final static String TEST_COLLECTION_NAME = "logevents";

    private final static String LOG4J_AUTH_PROPS = "src/test/resources/log4j_auth.properties";

    private final static String username = "open";

    private final static String password = "sesame";

    private final MongoClient mongo;

    private MongoCollection collection;

    public TestMongoDbAppenderAuth() throws Exception {
        mongo = new MongoClient(TEST_MONGO_SERVER_HOSTNAME, TEST_MONGO_SERVER_PORT);
    }

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        MongoClient mongo = new MongoClient(TEST_MONGO_SERVER_HOSTNAME, TEST_MONGO_SERVER_PORT);
        mongo.dropDatabase(TEST_DATABASE_NAME);
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        MongoClient mongo = new MongoClient(TEST_MONGO_SERVER_HOSTNAME, TEST_MONGO_SERVER_PORT);
        mongo.dropDatabase(TEST_DATABASE_NAME);
    }

    @Before
    public void setUp() throws Exception {
        // Ensure both the appender and the JUnit test use the same
        // collection object - provides consistency across reads (JUnit) &
        // writes (Log4J)
        collection = mongo.getDatabase(TEST_DATABASE_NAME).getCollection(TEST_COLLECTION_NAME);
        collection.drop();

    }

    /**
     * Catching the RuntimeException thrown when Log4J calls the MongoDbAppender activeOptions()
     * method isn't easy, since it is thrown in another thread.
     */
    @Test(expected = RuntimeException.class)
    @Ignore
    public void testAppenderActivateNoAuth() {
        PropertyConfigurator.configure(LOG4J_AUTH_PROPS);
    }

    /**
     * Adds the user to the test database before activating the appender.
     */
    @Test
    public void testAppenderActivateWithAuth() {
        mongo.getDB(TEST_DATABASE_NAME).addUser(username, password.toCharArray());
        PropertyConfigurator.configure(LOG4J_AUTH_PROPS);
    }

}

@@ -658,21 +658,29 @@
 mongodb.
-Mongo
+BasicDBObject
 ;%0Aimport
@@ -748,16 +748,57 @@
 ection;%0A
+import com.mongodb.client.MongoDatabase;%0A
 import o
@@ -850,15 +850,100 @@
 org.
-junit.*
+bson.Document;%0Aimport org.junit.*;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Collections
 ;%0A%0A/
@@ -3297,24 +3297,75 @@
 () %7B%0A       
+ Document result = null;%0A        MongoDatabase db =
  mongo.getDB
@@ -3363,17 +3363,23 @@
 ngo.getD
-B
+atabase
 (TEST_DA
@@ -3394,49 +3394,803 @@
 AME)
-.addUser(username, password.toCharArray()
+;%0A        BasicDBObject getUsersInfoCommand = new BasicDBObject(%22usersInfo%22,%0A                new BasicDBObject(%22user%22, username).append(%22db%22, TEST_DATABASE_NAME));%0A        BasicDBObject dropUserCommand = new BasicDBObject(%22dropUser%22, username);%0A        BasicDBObject createUserCommand = new BasicDBObject(%22createUser%22, username).append(%22pwd%22, password).append(%22roles%22,%0A                            Collections.singletonList(new BasicDBObject(%22role%22, %22dbOwner%22).append(%22db%22, TEST_DATABASE_NAME)));%0A%0A        // If test user exists from a previous run, drop it%0A        result = db.runCommand(getUsersInfoCommand);%0A        ArrayList users = (ArrayList) result.get(%22users%22);%0A        if (!users.isEmpty()) %7B%0A            db.runCommand(dropUserCommand);%0A        %7D%0A%0A        db.runCommand(createUserCommand
 );%0A 

Switch from deprecated method to supported method for creating test user