package com.github.kohanyirobert.mongoroid;

import com.github.kohanyirobert.ebson.BsonDocuments;

import com.google.common.net.HostAndPort;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

// @checkstyle:off MagicNumber|InnerAssignment
public final class MongoReadPreferenceSimpleSingleTest {

  private MongoConnection.Builder builder;

  public MongoReadPreferenceSimpleSingleTest() {}

  @Before
  public void setUp() {
    builder = MongoConnections.builder()
        .config(MongoConfigs.builder()
            .poolSize(1)
            .refreshInterval(1, TimeUnit.SECONDS)
            .refreshTimeout(5, TimeUnit.SECONDS)
            .retryInterval(1, TimeUnit.SECONDS)
            .retryTimeout(5, TimeUnit.SECONDS)
            .build())
        .seed(MongoSeeds.builder()
            .address(27018)
            .address(27019)
            .address(27020)
            .build());
  }

  @Test(expected = MongoRefreshTimeoutException.class)
  public void readPreferenceNoPrimaryOrSecondaryWithoutTags() throws MongoException {
    try (MongoConnection connection = builder.readPreference(
        MongoReadPreferences.builder()
            .primary(false)
            .secondary(false)
            .build())
        .build()) {
      connection.database("admin").command("isMaster");
    }
  }

  @Test
  public void readPreferencePrimaryOnly() throws MongoException {
    try (MongoConnection connection = builder.readPreference(
        MongoReadPreferences.builder()
            .primary(true)
            .secondary(false)
            .build())
        .build()) {
      for (int i = 0; i < 100; i++)
        Assert.assertTrue(connection.database("admin").command("isMaster")
            .get("ismaster", Boolean.class).booleanValue());
    }
  }

  @Test
  public void readPreferenceSecondaryOnly() throws MongoException {
    try (MongoConnection connection = builder.readPreference(
        MongoReadPreferences.builder()
            .primary(true)
            .secondary(false)
            .build())
        .build()) {
      for (int i = 0; i < 100; i++)
        Assert.assertTrue(connection.database("admin").command("isMaster")
            .get("ismaster", Boolean.class).booleanValue());
    }
  }

  @Test
  public void readPreferenceWithSingleTag() throws MongoException {
    try (MongoConnection connection = builder.readPreference(
        MongoReadPreferences.builder()
            .primary(false)
            .secondary(true)
            .build())
        .build()) {
      for (int i = 0; i < 100; i++)
        Assert.assertTrue(connection.database("admin").command("isMaster")
            .get("secondary", Boolean.class).booleanValue());
    }
  }

  @Test
  public void readPreferenceWithMultipleTag() throws MongoException {
    try (MongoConnection connection = builder.readPreference(
        MongoReadPreferences.builder()
            .primary(false)
            .secondary(false)
            .tags(BsonDocuments.of("notebook", "a"),
                BsonDocuments.of("notebook", "b"))
            .build())
        .build()) {
      for (int i = 0; i < 100; i++) {
        int port = HostAndPort.fromString(connection.database("admin")
            .command("isMaster").get("me", String.class)).getPort();
        Assert.assertTrue(port == 27018 || port == 27019);
      }
    }
  }
}

@@ -38,16 +38,68 @@
 oroid;%0A%0A
+import com.github.kohanyirobert.ebson.BsonDocument;%0A
 import c
@@ -2058,35 +2058,36 @@
        .primary(
-tru
+fals
 e)%0A            .
@@ -2088,36 +2088,35 @@
      .secondary(
-fals
+tru
 e)%0A            .
@@ -2269,32 +2269,33 @@
       .get(%22
-ismaster
+secondary
 %22, Boolean.c
@@ -2548,36 +2548,143 @@
      .secondary(
-true
+false)%0A            // the server tagged with a is secondary%0A            .tags(BsonDocuments.of(%22notebook%22, %22a%22)
 )%0A            .b
@@ -3124,32 +3124,95 @@
 econdary(false)%0A
+            // the servers tagged with a and b are secondaries%0A
             .tag
@@ -3251,24 +3251,8 @@
 a%22),
-%0A               
  Bso
@@ -3375,101 +3375,132 @@
     
-int port = HostAndPort.fromString(connection.database(%22admin%22)%0A            .command(%22
+BsonDocument isMaster = connection.database(%22admin%22).command(%22isMaster%22);%0A        int port = HostAndPort.fromString(
 isMaster
 %22).g
@@ -3495,18 +3495,16 @@
 isMaster
-%22)
 .get(%22me
@@ -3591,16 +3591,100 @@
 27019);%0A
+        Assert.assertTrue(isMaster.get(%22secondary%22, Boolean.class).booleanValue());%0A
       %7D%0A

Fix test cases in the read preferences test class.