11package de .bwaldvogel .mongo .backend ;
22
3+ import static de .bwaldvogel .mongo .backend .AbstractMongoBackend .ADMIN_DB_NAME ;
34import static org .assertj .core .api .Assertions .assertThat ;
45import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
6+ import static org .mockito .ArgumentMatchers .any ;
7+ import static org .mockito .Mockito .when ;
58
9+ import java .net .InetSocketAddress ;
610import java .util .Arrays ;
711import java .util .Collections ;
812import java .util .List ;
13+ import java .util .concurrent .CompletionStage ;
914
1015import org .junit .jupiter .api .BeforeEach ;
1116import org .junit .jupiter .api .Test ;
17+ import org .mockito .Mockito ;
1218
1319import de .bwaldvogel .mongo .MongoBackend ;
1420import de .bwaldvogel .mongo .MongoDatabase ;
1521import de .bwaldvogel .mongo .bson .Document ;
1622import de .bwaldvogel .mongo .exception .CursorNotFoundException ;
1723import de .bwaldvogel .mongo .wire .message .MongoGetMore ;
1824import de .bwaldvogel .mongo .wire .message .MongoKillCursors ;
25+ import io .netty .channel .Channel ;
1926
2027class AbstractMongoBackendTest {
2128
@@ -32,7 +39,15 @@ public void setup() {
3239
3340 @ Override
3441 protected MongoDatabase openOrCreateDatabase (String databaseName ) {
35- return null ;
42+ MongoDatabase mockDatabase = Mockito .mock (AbstractMongoDatabase .class );
43+
44+ Document fakeResponse = new Document ();
45+ Utils .markOkay (fakeResponse );
46+ fakeResponse .put ("message" , "fakeResponse" );
47+
48+ when (mockDatabase .handleCommand (any (), any (), any (), any ())).thenReturn (fakeResponse );
49+
50+ return mockDatabase ;
3651 }
3752 };
3853 }
@@ -73,4 +88,66 @@ void testHandleKillCursor() {
7388 assertThat (cursorRegistry .getCursor (cursor2 .getId ())).isNotNull ();
7489 }
7590
91+ @ Test
92+ void testHandleCommandSync () {
93+ Channel channel = Mockito .mock (Channel .class );
94+ when (channel .remoteAddress ()).thenReturn (new InetSocketAddress ("127.0.1.254" , 27017 ));
95+
96+ Document response = backend .handleCommand (channel , null , "whatsmyuri" , null );
97+ assertThat (response ).isNotNull ();
98+ assertThat (response .get ("ok" )).isEqualTo (1.0 );
99+ assertThat (response .get ("you" )).isEqualTo ("127.0.1.254:27017" );
100+ }
101+
102+ @ Test
103+ void testHandleCommandAsync () throws Exception {
104+ Channel channel = Mockito .mock (Channel .class );
105+ when (channel .remoteAddress ()).thenReturn (new InetSocketAddress ("127.0.1.254" , 27017 ));
106+
107+ CompletionStage <Document > responseFuture = backend .handleCommandAsync (channel , null , "whatsmyuri" , null );
108+ Document response = responseFuture .toCompletableFuture ().get ();
109+ assertThat (response ).isNotNull ();
110+ assertThat (response .get ("ok" )).isEqualTo (1.0 );
111+ assertThat (response .get ("you" )).isEqualTo ("127.0.1.254:27017" );
112+ }
113+
114+ @ Test
115+ void testHandleAdminCommand () {
116+ Channel channel = Mockito .mock (Channel .class );
117+
118+ Document response = backend .handleCommand (channel , ADMIN_DB_NAME , "ping" , null );
119+ assertThat (response ).isNotNull ();
120+ assertThat (response .get ("ok" )).isEqualTo (1.0 );
121+ }
122+
123+ @ Test
124+ void testHandleAdminCommandAsync () throws Exception {
125+ Channel channel = Mockito .mock (Channel .class );
126+
127+ CompletionStage <Document > responseFuture = backend .handleCommandAsync (channel , ADMIN_DB_NAME , "ping" , null );
128+ Document response = responseFuture .toCompletableFuture ().get ();
129+ assertThat (response ).isNotNull ();
130+ assertThat (response .get ("ok" )).isEqualTo (1.0 );
131+ }
132+
133+ @ Test
134+ void testMongoDatabaseHandleCommand () {
135+ Channel channel = Mockito .mock (Channel .class );
136+
137+ Document response = backend .handleCommand (channel , "mockDatabase" , "find" , null );
138+ assertThat (response ).isNotNull ();
139+ assertThat (response .get ("ok" )).isEqualTo (1.0 );
140+ assertThat (response .get ("message" )).isEqualTo ("fakeResponse" );
141+ }
142+
143+ @ Test
144+ void testMongoDatabaseHandleCommandAsync () throws Exception {
145+ Channel channel = Mockito .mock (Channel .class );
146+
147+ CompletionStage <Document > responseFuture = backend .handleCommandAsync (channel , "mockDatabase" , "find" , null );
148+ Document response = responseFuture .toCompletableFuture ().get ();
149+ assertThat (response ).isNotNull ();
150+ assertThat (response .get ("ok" )).isEqualTo (1.0 );
151+ assertThat (response .get ("message" )).isEqualTo ("fakeResponse" );
152+ }
76153}
0 commit comments