<commit_before>/*
 * Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.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 com.mongodb;


import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Arrays.asList;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import static org.mongodb.Fixture.clusterIsType;
import static org.mongodb.Fixture.disableMaxTimeFailPoint;
import static org.mongodb.Fixture.enableMaxTimeFailPoint;
import static org.mongodb.Fixture.serverVersionAtLeast;
import static org.mongodb.connection.ClusterType.REPLICA_SET;


public class JavaClientOldTest extends DatabaseTestCase {

    @Test
    public void testAggregation() {
        DBObject foo = new BasicDBObject("name", "foo").append("count", 5);
        DBObject bar = new BasicDBObject("name", "bar").append("count", 2);
        DBObject baz = new BasicDBObject("name", "foo").append("count", 7);
        collection.insert(foo, bar, baz);

        DBObject projection = new BasicDBObject("name", 1).append("count", 1);

        DBObject group = new BasicDBObject().append("_id", "$name")
            .append("docsPerName", new BasicDBObject("$sum", 1))
            .append("countPerName", new BasicDBObject("$sum", "$count"));

        AggregationOutput out = collection.aggregate(
            Arrays.<DBObject>asList(new BasicDBObject("$project", projection), new BasicDBObject("$group", group)));

        Map<String, DBObject> results = new HashMap<String, DBObject>();
        for (final DBObject result : out.results()) {
            results.put((String) result.get("_id"), result);
        }

        DBObject fooResult = results.get("foo");
        assertNotNull(fooResult);
        assertEquals(2, fooResult.get("docsPerName"));
        assertEquals(12, fooResult.get("countPerName"));

        DBObject barResult = results.get("bar");
        assertNotNull(barResult);
        assertEquals(1, barResult.get("docsPerName"));
        assertEquals(2, barResult.get("countPerName"));

        DBObject aggregationCommand = out.getCommand();
        assertNotNull(aggregationCommand);
        assertEquals(collection.getName(), aggregationCommand.get("aggregate"));
        assertNotNull(aggregationCommand.get("pipeline"));
    }

    @Test
    public void testAggregationCursor() {
        assumeTrue(serverVersionAtLeast(asList(2, 5, 3)));

        List<DBObject> pipeline = prepareData();

        verify(pipeline, AggregationOptions.builder()
            .batchSize(1)
            .outputMode(AggregationOptions.OutputMode.CURSOR)
            .allowDiskUsage(true)
            .build());

        verify(pipeline, AggregationOptions.builder()
            .batchSize(1)
            .outputMode(AggregationOptions.OutputMode.INLINE)
            .allowDiskUsage(true)
            .build());

        verify(pipeline, AggregationOptions.builder()
            .batchSize(1)
            .outputMode(AggregationOptions.OutputMode.CURSOR)
            .build());
    }

    @Test
    public void testInlineAndDollarOut() {
        assumeTrue(serverVersionAtLeast(asList(2, 5, 3)));
        String aggCollection = "aggCollection";
        database.getCollection(aggCollection)
            .drop();
        assertEquals(0, database.getCollection(aggCollection)
            .count());
        List<DBObject> pipeline = new ArrayList<DBObject>(prepareData());
        pipeline.add(new BasicDBObject("$out", aggCollection));

        AggregationOutput out = collection.aggregate(pipeline);
        assertFalse(out.results()
            .iterator()
            .hasNext());
        assertEquals(2, database.getCollection(aggCollection)
            .count());
    }

    @Test
    public void testDollarOut() {
        assumeTrue(serverVersionAtLeast(asList(2, 5, 3)));
        String aggCollection = "aggCollection";
        database.getCollection(aggCollection)
            .drop();
        Assert.assertEquals(0, database.getCollection(aggCollection)
            .count());

        List<DBObject> pipeline = new ArrayList<DBObject>(prepareData());
        pipeline.add(new BasicDBObject("$out", aggCollection));
        verify(pipeline, AggregationOptions.builder()
            .outputMode(AggregationOptions.OutputMode.CURSOR)
            .build());
        assertEquals(2, database.getCollection(aggCollection)
            .count());
    }

    @Test
    public void testDollarOutOnSecondary() throws UnknownHostException {
        assumeTrue(serverVersionAtLeast(asList(2, 5, 3)));
        assumeTrue(clusterIsType(REPLICA_SET));
        ServerAddress primary = new ServerAddress("localhost");
        Mongo mongo = new MongoClient(asList(primary, new ServerAddress("localhost", 27018), new ServerAddress("localhost", 27019)));

        DB rsDatabase = mongo.getDB(database.getName());
        DBCollection aggCollection = rsDatabase.getCollection(collection.getName());
        aggCollection.drop();

        List<DBObject> pipeline = new ArrayList<DBObject>(prepareData());
        pipeline.add(new BasicDBObject("$out", "aggCollection"));
        AggregationOptions options = AggregationOptions.builder()
            .outputMode(AggregationOptions.OutputMode.CURSOR)
            .build();
        MongoCursor cursor = verify(pipeline, options, ReadPreference.secondary(), aggCollection);
        assertEquals(2, rsDatabase.getCollection("aggCollection")
            .count());
        assertEquals(primary, cursor.getServerAddress());
    }

    @Test
    @Ignore
    public void testAggregateOnSecondary() throws UnknownHostException {
        assumeTrue(clusterIsType(REPLICA_SET));

        ServerAddress primary = new ServerAddress("localhost");
        ServerAddress secondary = new ServerAddress("localhost", 27018);
        MongoClient rsClient = new MongoClient(asList(primary, secondary));
        DB rsDatabase = rsClient.getDB(database.getName());
        rsDatabase.dropDatabase();
        DBCollection aggCollection = rsDatabase.getCollection(collection.getName());
        aggCollection.drop();

        List<DBObject> pipeline = new ArrayList<DBObject>(prepareData());
        AggregationOptions options = AggregationOptions.builder()
            .outputMode(AggregationOptions.OutputMode.INLINE)<commit_msg>Fixing a broken test that hadn't running on Jenkins, but now is after the latest change to the Gradle build.<commit_after>/*
 * Copyright (c) 2008 - 2014 MongoDB Inc. <http://mongodb.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 com.mongodb;


import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Arrays.asList;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import static org.mongodb.Fixture.clusterIsType;
import static org.mongodb.Fixture.disableMaxTimeFailPoint;
import static org.mongodb.Fixture.enableMaxTimeFailPoint;
import static org.mongodb.Fixture.serverVersionAtLeast;
import static org.mongodb.connection.ClusterType.REPLICA_SET;


public class JavaClientOldTest extends DatabaseTestCase {

    @Test
    public void testAggregation() {
        DBObject foo = new BasicDBObject("name", "foo").append("count", 5);
        DBObject bar = new BasicDBObject("name", "bar").append("count", 2);
        DBObject baz = new BasicDBObject("name", "foo").append("count", 7);
        collection.insert(foo, bar, baz);

        DBObject projection = new BasicDBObject("name", 1).append("count", 1);

        DBObject group = new BasicDBObject().append("_id", "$name")
            .append("docsPerName", new BasicDBObject("$sum", 1))
            .append("countPerName", new BasicDBObject("$sum", "$count"));

        AggregationOutput out = collection.aggregate(
            Arrays.<DBObject>asList(new BasicDBObject("$project", projection), new BasicDBObject("$group", group)));

        Map<String, DBObject> results = new HashMap<String, DBObject>();
        for (final DBObject result : out.results()) {
            results.put((String) result.get("_id"), result);
        }

        DBObject fooResult = results.get("foo");
        assertNotNull(fooResult);
        assertEquals(2, fooResult.get("docsPerName"));
        assertEquals(12, fooResult.get("countPerName"));

        DBObject barResult = results.get("bar");
        assertNotNull(barResult);
        assertEquals(1, barResult.get("docsPerName"));
        assertEquals(2, barResult.get("countPerName"));

        DBObject aggregationCommand = out.getCommand();
        assertNotNull(aggregationCommand);
        assertEquals(collection.getName(), aggregationCommand.get("aggregate"));
        assertNotNull(aggregationCommand.get("pipeline"));
    }

    @Test
    public void testAggregationCursor() {
        assumeTrue(serverVersionAtLeast(asList(2, 5, 3)));

        List<DBObject> pipeline = prepareData();

        verify(pipeline, AggregationOptions.builder()
            .batchSize(1)
            .outputMode(AggregationOptions.OutputMode.CURSOR)
            .allowDiskUsage(true)
            .build());

        verify(pipeline, AggregationOptions.builder()
            .batchSize(1)
            .outputMode(AggregationOptions.OutputMode.INLINE)
            .allowDiskUsage(true)
            .build());

        verify(pipeline, AggregationOptions.builder()
            .batchSize(1)
            .outputMode(AggregationOptions.OutputMode.CURSOR)
            .build());
    }

    @Test
    public void testInlineAndDollarOut() {
        assumeTrue(serverVersionAtLeast(asList(2, 5, 3)));
        String aggCollection = "aggCollection";
        database.getCollection(aggCollection)
            .drop();
        assertEquals(0, database.getCollection(aggCollection)
            .count());
        List<DBObject> pipeline = new ArrayList<DBObject>(prepareData());
        pipeline.add(new BasicDBObject("$out", aggCollection));

        AggregationOutput out = collection.aggregate(pipeline);
        assertFalse(out.results()
            .iterator()
            .hasNext());
        assertEquals(2, database.getCollection(aggCollection)
            .count());
    }

    @Test
    public void testDollarOut() {
        assumeTrue(serverVersionAtLeast(asList(2, 5, 3)));
        String aggCollection = "aggCollection";
        database.getCollection(aggCollection)
            .drop();
        Assert.assertEquals(0, database.getCollection(aggCollection)
            .count());

        List<DBObject> pipeline = new ArrayList<DBObject>(prepareData());
        pipeline.add(new BasicDBObject("$out", aggCollection));
        verify(pipeline, AggregationOptions.builder()
            .outputMode(AggregationOptions.OutputMode.CURSOR)
            .build());
        assertEquals(2, database.getCollection(aggCollection)
            .count());
    }

    @Test
    public void testDollarOutOnSecondary() throws UnknownHostException, InterruptedException {
        assumeTrue(serverVersionAtLeast(asList(2, 5, 3)));
        assumeTrue(clusterIsType(REPLICA_SET));

        List<DBObject> pipeline = new ArrayList<DBObject>(prepareData());
        pipeline.add(new BasicDBObject("$out", "aggCollection"));
        AggregationOptions options = AggregationOptions.builder()
            .outputMode(AggregationOptions.OutputMode.CURSOR)
            .build();
        MongoCursor cursor = verify(pipeline, options, ReadPreference.secondary(), collection);
        assertEquals(2, database.getCollection("aggCollection")
            .count());
        assertEquals(Fixture.getPrimary(), cursor.getServerAddress());
    }

    @Test
    @Ignore
    public void testAggregateOnSecondary() throws UnknownHostException {
        assumeTrue(clusterIsType(REPLICA_SET));

        ServerAddress primary = new ServerAddress("localhost");
        ServerAddress secondary = new ServerAddress("localhost", 27018);
        MongoClient rsClient = new MongoClient(asList(primary, secondary));
        DB rsDatabase = rsClient.getDB(database.getName());
        rsDatabase.dropDatabase();
        DBCollection aggCollection = rsDatabase.getCollection(collection.getName());
        aggCollection.drop();

        List<DBObject> pipeline = new ArrayList<DBObject>(prepareData());
        AggregationOptions options = AggregationOptions.builder()
            .outputMode(AggregationOptions.OutputMode.INLINE)
{'fraction_non_alphanumeric': 0.11411327110674434, 'fraction_numerical': 0.007719839988771142, 'mean_word_length': 3.36046511627907, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4669498', 'n_tokens_mistral': 3865, 'n_tokens_neox': 3705, 'n_words': 842}