/*
 * Copyright (c) 2015 MongoDB, Inc.
 *
 * 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 com.mongodb.acceptancetest.core;

import com.mongodb.MongoClient;
import com.mongodb.client.DatabaseTestCase;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static com.mongodb.Fixture.getMongoClient;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
 * Documents the basic functionality available for the MongoClient via the Java driver.
 */
public class ClientAcceptanceTest extends DatabaseTestCase {

    @Test
    public void shouldListDatabaseNamesFromDatabase() {
        database.createCollection(getCollectionName());
        List<String> names = client.listDatabaseNames().into(new ArrayList<String>());

        assertThat(names.contains(getDatabaseName()), is(true));
    }

    @Test
    public void shouldListDatabasesFromDatabase() {
        database.drop();

        List<Document> databases = client.listDatabases().into(new ArrayList<Document>());
        int size = databases.size();

        database.createCollection(getCollectionName());
        databases = client.listDatabases().into(new ArrayList<Document>());
        assertThat(databases.size(), is(greaterThan(size)));
    }


    @Test
    public void shouldListDatabasesNamesFromDatabase() {
        database.drop();

        List<String> databases = client.listDatabaseNames().into(new ArrayList<String>());
        int size = databases.size();

        database.createCollection(getCollectionName());
        databases = client.listDatabaseNames().into(new ArrayList<String>());
        assertThat(databases.size(), is(greaterThan(size)));
        assertTrue(databases.contains(getDatabaseName()));
    }

    @Test
    public void shouldBeAbleToListAllTheDatabasesAvailable() {
        MongoClient mongoClient = getMongoClient();
        MongoDatabase firstDatabase = mongoClient.getDatabase("FirstNewDatabase");
        MongoDatabase secondDatabase = mongoClient.getDatabase("SecondNewDatabase");
        MongoDatabase otherDatabase = mongoClient.getDatabase("DatabaseThatDoesNotExistYet");

        try {
            // given
            firstDatabase.getCollection("coll").insertOne(new Document("aDoc", "to force database creation"));
            secondDatabase.getCollection("coll").insertOne(new Document("aDoc", "to force database creation"));

            //when
            List<String> databaseNames = mongoClient.listDatabaseNames().into(new ArrayList<String>());

            //then
            assertThat(databaseNames, hasItems(firstDatabase.getName(), secondDatabase.getName()));
            assertThat(databaseNames, not(hasItem(otherDatabase.getName())));
        } finally {
            //tear down
            firstDatabase.drop();
            secondDatabase.drop();
        }
    }
}

@@ -639,40 +639,8 @@
 e;%0A%0A
-import com.mongodb.MongoClient;%0A
 impo
@@ -675,24 +675,24 @@
 seTestCase;%0A
+
 import com.m
@@ -742,24 +742,90 @@
 n.Document;%0A
+import org.hamcrest.BaseMatcher;%0Aimport org.hamcrest.Description;%0A
 import org.j
@@ -892,58 +892,8 @@
 t;%0A%0A
-import static com.mongodb.Fixture.getMongoClient;%0A
 impo
@@ -1036,57 +1036,8 @@
 ot;%0A
-import static org.hamcrest.Matchers.greaterThan;%0A
 impo
@@ -1117,51 +1117,8 @@
 hat;
-%0Aimport static org.junit.Assert.assertTrue;
 %0A%0A/*
@@ -1591,1001 +1591,52 @@
 ould
-ListDatabasesFromDatabase() %7B%0A        database.drop();%0A%0A        List%3CDocument%3E databases = client.listDatabases().into(new ArrayList%3CDocument%3E());%0A        int size = databases.size();%0A%0A        database.createCollection(getCollectionName());%0A        databases = client.listDatabases().into(new ArrayList%3CDocument%3E());%0A        assertThat(databases.size(), is(greaterThan(size)));%0A    %7D%0A%0A%0A    @Test%0A    public void shouldListDatabasesNamesFromDatabase() %7B%0A        database.drop();%0A%0A        List%3CString%3E databases = client.listDatabaseNames().into(new ArrayList%3CString%3E());%0A        int size = databases.size();%0A%0A        database.createCollection(getCollectionName());%0A        databases = client.listDatabaseNames().into(new ArrayList%3CString%3E());%0A        assertThat(databases.size(), is(greaterThan(size)));%0A        assertTrue(databases.contains(getDatabaseName()));%0A    %7D%0A%0A    @Test%0A    public void shouldBeAbleToListAllTheDatabasesAvailable() %7B%0A        MongoClient mongoClient = getMongoClient();
+BeAbleToListAllTheDatabaseNamesAvailable() %7B
 %0A   
@@ -1662,38 +1662,33 @@
 firstDatabase = 
-mongoC
+c
 lient.getDatabas
@@ -1741,38 +1741,33 @@
 econdDatabase = 
-mongoC
+c
 lient.getDatabas
@@ -1828,22 +1828,17 @@
 abase = 
-mongoC
+c
 lient.ge
@@ -2203,14 +2203,9 @@
 s = 
-mongoC
+c
 lien
@@ -2525,32 +2525,32 @@
 atabase.drop();%0A
-
             seco
@@ -2583,11 +2583,1089 @@
  %7D%0A    %7D
+%0A%0A    @Test%0A    public void shouldListDatabase() %7B%0A        List%3CDocument%3E databases = client.listDatabases().into(new ArrayList%3CDocument%3E());%0A%0A        database.createCollection(getCollectionName());%0A        databases = client.listDatabases().into(new ArrayList%3CDocument%3E());%0A        assertThat(databases, new DatabaseNameMatcher(getDatabaseName()));%0A    %7D%0A%0A    private static final class DatabaseNameMatcher extends BaseMatcher%3CList%3CDocument%3E%3E %7B%0A%0A        private final String name;%0A%0A        DatabaseNameMatcher(final String name) %7B%0A            this.name = name;%0A        %7D%0A%0A        @Override%0A        public boolean matches(final Object item) %7B%0A            List%3CDocument%3E databases = (List%3CDocument%3E) item;%0A            for (Document cur : databases) %7B%0A                if (cur.get(%22name%22).equals(name)) %7B%0A                    return true;%0A                %7D%0A            %7D%0A            return false;%0A        %7D%0A%0A        @Override%0A        public void describeTo(final Description description) %7B%0A            description.appendText(%22Document containing a name of %22 + name);%0A        %7D%0A    %7D
 %0A%7D%0A

Simplify ClientAcceptanceTest so that it no longer relies on dropping a database.