1+ /*
2+ * Copyright (c) 2019 Otávio Santana and others
3+ * All rights reserved. This program and the accompanying materials
4+ * are made available under the terms of the Eclipse Public License v1.0
5+ * and Apache License v2.0 which accompanies this distribution.
6+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+ * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+ *
9+ * You may elect to redistribute this code under either of these licenses.
10+ *
11+ * Contributors:
12+ *
13+ * Otavio Santana
14+ */
15+ package org .jnosql .diana .mongodb .document ;
16+
17+ import com .mongodb .AuthenticationMechanism ;
18+ import com .mongodb .MongoCredential ;
19+ import org .jnosql .diana .api .JNoSQLException ;
20+ import org .jnosql .diana .api .Settings ;
21+ import org .junit .jupiter .api .Test ;
22+
23+ import java .util .Arrays ;
24+
25+ import static com .mongodb .AuthenticationMechanism .GSSAPI ;
26+ import static com .mongodb .AuthenticationMechanism .PLAIN ;
27+ import static com .mongodb .AuthenticationMechanism .SCRAM_SHA_1 ;
28+ import static com .mongodb .AuthenticationMechanism .SCRAM_SHA_256 ;
29+ import static org .jnosql .diana .mongodb .document .MongoDBDocumentConfigurations .AUTHENTICATION_MECHANISM ;
30+ import static org .jnosql .diana .mongodb .document .MongoDBDocumentConfigurations .AUTHENTICATION_SOURCE ;
31+ import static org .jnosql .diana .mongodb .document .MongoDBDocumentConfigurations .PASSWORD ;
32+ import static org .jnosql .diana .mongodb .document .MongoDBDocumentConfigurations .USER ;
33+ import static org .junit .jupiter .api .Assertions .assertEquals ;
34+ import static org .junit .jupiter .api .Assertions .assertThrows ;
35+ import static org .junit .jupiter .api .Assertions .assertTrue ;
36+
37+ class MongoAuthenticationTest {
38+
39+ @ Test
40+ public void shouldReturnErrorWhenTheNumberParameterIsInvalid () {
41+ Settings settings = Settings .builder ().put (USER .get (), "value" )
42+ .build ();
43+
44+ assertThrows (JNoSQLException .class , () -> MongoAuthentication .of (settings ));
45+
46+ }
47+
48+ @ Test
49+ public void shouldReturnOneAuthentication () {
50+ Settings settings = Settings .builder ()
51+ .put (AUTHENTICATION_SOURCE .get (), "database" )
52+ .put (PASSWORD .get (), "password" )
53+ .put (USER .get (), "user" )
54+ .build ();
55+
56+ MongoCredential credential = MongoAuthentication .of (settings ).get ();
57+ assertEquals ("database" , credential .getSource ());
58+ assertTrue (Arrays .equals ("password" .toCharArray (), credential .getPassword ()));
59+ assertEquals ("user" , credential .getUserName ());
60+ assertEquals (PLAIN .getMechanismName (), credential .getMechanism ());
61+
62+ }
63+
64+ @ Test
65+ public void shouldReturnOneAuthenticationWithGSSAPI () {
66+ Settings settings = Settings .builder ()
67+ .put (AUTHENTICATION_SOURCE .get (), "database" )
68+ .put (PASSWORD .get (), "password" )
69+ .put (USER .get (), "user" )
70+ .put (AUTHENTICATION_MECHANISM .get (), "GSSAPI" )
71+ .build ();
72+
73+ MongoCredential credential = MongoAuthentication .of (settings ).get ();
74+ assertEquals ("$external" , credential .getSource ());
75+ assertEquals ("user" , credential .getUserName ());
76+ assertEquals (GSSAPI .getMechanismName (), credential .getMechanism ());
77+
78+ }
79+
80+ @ Test
81+ public void shouldReturnOneAuthenticationWithMongoX509 () {
82+ Settings settings = Settings .builder ()
83+ .put (AUTHENTICATION_SOURCE .get (), "database" )
84+ .put (PASSWORD .get (), "password" )
85+ .put (USER .get (), "user" )
86+ .put (AUTHENTICATION_MECHANISM .get (), "MONGODB-X509" )
87+ .build ();
88+
89+ MongoCredential credential = MongoAuthentication .of (settings ).get ();
90+ assertEquals ("$external" , credential .getSource ());
91+ assertEquals ("user" , credential .getUserName ());
92+ assertEquals (AuthenticationMechanism .MONGODB_X509 .getMechanismName (), credential .getMechanism ());
93+ }
94+
95+ @ Test
96+ public void shouldReturnOneAuthenticationWithSCRAMSHA1 () {
97+ Settings settings = Settings .builder ()
98+ .put (AUTHENTICATION_SOURCE .get (), "database" )
99+ .put (PASSWORD .get (), "password" )
100+ .put (USER .get (), "user" )
101+ .put (AUTHENTICATION_MECHANISM .get (), "SCRAM-SHA-1" )
102+ .build ();
103+
104+ MongoCredential credential = MongoAuthentication .of (settings ).get ();
105+ assertEquals ("database" , credential .getSource ());
106+ assertTrue (Arrays .equals ("password" .toCharArray (), credential .getPassword ()));
107+ assertEquals ("user" , credential .getUserName ());
108+ assertEquals (SCRAM_SHA_1 .getMechanismName (), credential .getMechanism ());
109+ }
110+
111+ @ Test
112+ public void shouldReturnOneAuthenticationWithSCRAMSHA256 () {
113+ Settings settings = Settings .builder ()
114+ .put (AUTHENTICATION_SOURCE .get (), "database" )
115+ .put (PASSWORD .get (), "password" )
116+ .put (USER .get (), "user" )
117+ .put (AUTHENTICATION_MECHANISM .get (), "SCRAM-SHA-256" )
118+ .build ();
119+
120+ MongoCredential credential = MongoAuthentication .of (settings ).get ();
121+ assertEquals ("database" , credential .getSource ());
122+ assertTrue (Arrays .equals ("password" .toCharArray (), credential .getPassword ()));
123+ assertEquals ("user" , credential .getUserName ());
124+ assertEquals (SCRAM_SHA_256 .getMechanismName (), credential .getMechanism ());
125+ }
126+
127+ }
0 commit comments