17
17
package com .mongodb .connection ;
18
18
19
19
import com .mongodb .MongoCompressor ;
20
+ import com .mongodb .ReadPreference ;
21
+ import com .mongodb .ServerAddress ;
22
+ import com .mongodb .internal .connection .NoOpSessionContext ;
23
+ import com .mongodb .internal .validator .NoOpFieldNameValidator ;
20
24
import com .mongodb .selector .ServerSelector ;
25
+ import org .bson .BsonDocument ;
26
+ import org .bson .BsonDouble ;
27
+ import org .bson .BsonString ;
28
+ import org .bson .codecs .BsonDocumentCodec ;
21
29
import org .junit .After ;
22
- import org .junit .Before ;
23
30
import org .junit .Test ;
24
31
25
32
import java .util .Collections ;
26
33
import java .util .List ;
27
34
28
35
import static com .mongodb .ClusterFixture .getCredentialList ;
36
+ import static com .mongodb .ClusterFixture .getDefaultDatabaseName ;
29
37
import static com .mongodb .ClusterFixture .getPrimary ;
38
+ import static com .mongodb .ClusterFixture .getSecondary ;
30
39
import static com .mongodb .ClusterFixture .getSslSettings ;
31
40
import static java .util .Collections .singletonList ;
41
+ import static org .junit .Assert .assertEquals ;
32
42
import static org .junit .Assert .assertNotNull ;
33
43
import static org .junit .Assert .assertTrue ;
34
44
35
45
public class SingleServerClusterTest {
36
46
private SingleServerCluster cluster ;
37
47
38
- @ Before
39
- public void setUp () throws Exception {
48
+
49
+ private void setUpCluster ( final ServerAddress serverAddress ) {
40
50
SocketStreamFactory streamFactory = new SocketStreamFactory (SocketSettings .builder ().build (),
41
- getSslSettings ());
51
+ getSslSettings ());
42
52
ClusterId clusterId = new ClusterId ();
43
53
ClusterSettings clusterSettings = ClusterSettings .builder ()
44
- .mode (ClusterConnectionMode .SINGLE )
45
- .hosts (singletonList (getPrimary () ))
46
- .build ();
54
+ .mode (ClusterConnectionMode .SINGLE )
55
+ .hosts (singletonList (serverAddress ))
56
+ .build ();
47
57
cluster = new SingleServerCluster (clusterId ,
48
- clusterSettings ,
49
- new DefaultClusterableServerFactory (clusterId , clusterSettings , ServerSettings .builder ().build (),
50
- ConnectionPoolSettings .builder ().maxSize (1 ).build (),
51
- streamFactory , streamFactory , getCredentialList (),
58
+ clusterSettings ,
59
+ new DefaultClusterableServerFactory (clusterId , clusterSettings , ServerSettings .builder ().build (),
60
+ ConnectionPoolSettings .builder ().maxSize (1 ).build (),
61
+ streamFactory , streamFactory , getCredentialList (),
52
62
53
- null , null , null ,
54
- Collections .<MongoCompressor >emptyList ()));
63
+ null , null , null ,
64
+ Collections .<MongoCompressor >emptyList ()));
55
65
}
56
66
57
67
@ After
@@ -61,25 +71,54 @@ public void tearDown() {
61
71
62
72
@ Test
63
73
public void shouldGetDescription () {
74
+ // given
75
+ setUpCluster (getPrimary ());
76
+
77
+ // expect
64
78
assertNotNull (cluster .getDescription ());
65
79
}
66
80
67
81
@ Test
68
82
public void descriptionShouldIncludeSettings () {
83
+ // given
84
+ setUpCluster (getPrimary ());
85
+
86
+ // expect
69
87
assertNotNull (cluster .getDescription ().getClusterSettings ());
70
88
assertNotNull (cluster .getDescription ().getServerSettings ());
71
89
}
72
90
73
91
@ Test
74
92
@ SuppressWarnings ("deprecation" )
75
- public void shouldGetServerWithOkDescription () throws InterruptedException {
93
+ public void shouldGetServerWithOkDescription () {
94
+ // given
95
+ setUpCluster (getPrimary ());
96
+
97
+ // when
76
98
Server server = cluster .selectServer (new ServerSelector () {
77
99
@ Override
78
100
public List <ServerDescription > select (final ClusterDescription clusterDescription ) {
79
101
return clusterDescription .getPrimaries ();
80
102
}
81
103
});
104
+
105
+ // then
82
106
assertTrue (server .getDescription ().isOk ());
83
107
}
84
108
109
+ @ Test
110
+ public void shouldSuccessfullyQueryASecondaryWithPrimaryReadPreference () {
111
+ // given
112
+ ServerAddress secondary = getSecondary ();
113
+ setUpCluster (secondary );
114
+ String collectionName = getClass ().getName ();
115
+ Connection connection = cluster .getServer (secondary ).getConnection ();
116
+
117
+ // when
118
+ BsonDocument result = connection .command (getDefaultDatabaseName (), new BsonDocument ("find" , new BsonString (collectionName )),
119
+ new NoOpFieldNameValidator (), ReadPreference .primary (), new BsonDocumentCodec (), NoOpSessionContext .INSTANCE );
120
+
121
+ // then
122
+ assertEquals (new BsonDouble (1.0 ).intValue (), result .getNumber ("ok" ).intValue ());
123
+ }
85
124
}
0 commit comments