|
| 1 | +package com.mongodb; |
| 2 | + |
| 3 | +import org.junit.Before; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import java.net.UnknownHostException; |
| 7 | +import java.util.Collections; |
| 8 | + |
| 9 | +import static com.mongodb.ServerConnectionState.Connected; |
| 10 | +import static com.mongodb.ServerDescription.builder; |
| 11 | +import static java.util.Arrays.asList; |
| 12 | +import static org.junit.Assert.assertEquals; |
| 13 | + |
| 14 | +public class ServerAddressSelectorTest { |
| 15 | + |
| 16 | + private ClusterDescription clusterDescription; |
| 17 | + private ServerDescription goodServer; |
| 18 | + private ServerDescription badServer; |
| 19 | + |
| 20 | + |
| 21 | + @Before |
| 22 | + public void setUp() throws UnknownHostException { |
| 23 | + goodServer = builder().ok(true).state(Connected).address(new ServerAddress("localhost", 27018)).build(); |
| 24 | + badServer = builder().ok(false).state(Connected).address(new ServerAddress("localhost", 27019)).build(); |
| 25 | + clusterDescription = new ClusterDescription(ClusterConnectionMode.Multiple, ClusterType.ReplicaSet, |
| 26 | + asList(goodServer, badServer)); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void shouldFindServerDescriptionForServerAddress() throws UnknownHostException { |
| 31 | + assertEquals(new ServerAddressSelector(new ServerAddress("localhost", 27018)).choose(clusterDescription), asList(goodServer)); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void shouldNotFindServerDescriptionForServerAddressThatIsNotOk() throws UnknownHostException { |
| 36 | + assertEquals(new ServerAddressSelector(new ServerAddress("localhost", 27019)).choose(clusterDescription), Collections.emptyList()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void shouldNotFindServerDescriptionForServerAddressThatIsNotInClusterDescription() throws UnknownHostException { |
| 41 | + assertEquals(new ServerAddressSelector(new ServerAddress("localhost", 27017)).choose(clusterDescription), Collections.emptyList()); |
| 42 | + } |
| 43 | +} |
0 commit comments