1+ package com .kapeta .spring .mongo ;
2+
3+ import com .kapeta .spring .config .providers .TestConfigProvider ;
4+ import com .kapeta .spring .config .providers .types .ResourceInfo ;
5+ import org .junit .jupiter .api .Test ;
6+ import org .springframework .boot .autoconfigure .mongo .MongoProperties ;
7+
8+
9+ import java .util .HashMap ;
10+ import java .util .Map ;
11+
12+ import static org .junit .jupiter .api .Assertions .assertEquals ;
13+
14+ public class AbstractMongoDBConfigTest {
15+
16+
17+ @ Test
18+ public void testCreateMongoUriProperties () {
19+ Map <String , String > credentials = new HashMap <>();
20+ credentials .put ("username" , "testUser" );
21+ credentials .put ("password" , "testPass" );
22+
23+ Map <String , Object > options = new HashMap <>();
24+ options .put ("ssl" , "true" );
25+
26+ TestMongoDBConfig mongoDBConfig = new TestMongoDBConfig ("testResource" );
27+
28+ ResourceInfo resourceInfo = new ResourceInfo ();
29+ resourceInfo .setCredentials (credentials );
30+ resourceInfo .setOptions (options );
31+ resourceInfo .setHost ("testHost" );
32+
33+ MongoProperties properties = mongoDBConfig .createMongoUriProperties ("testDB" , "admin" , resourceInfo );
34+
35+ assertEquals ("mongodb+srv://testUser:testPass@testHost/testDB?ssl=true&authSource=admin" , properties .getUri ());
36+ }
37+
38+
39+ @ Test
40+ public void testSSLFalse () {
41+ Map <String , String > credentials = new HashMap <>();
42+ credentials .put ("username" , "testUser" );
43+ credentials .put ("password" , "testPass" );
44+
45+ Map <String , Object > options = new HashMap <>();
46+ options .put ("ssl" , "false" );
47+
48+ TestMongoDBConfig mongoDBConfig = new TestMongoDBConfig ("testResource" );
49+
50+ ResourceInfo resourceInfo = new ResourceInfo ();
51+ resourceInfo .setCredentials (credentials );
52+ resourceInfo .setOptions (options );
53+ resourceInfo .setHost ("testHost" );
54+
55+ MongoProperties properties = mongoDBConfig .createMongoUriProperties ("testDB" , "admin" , resourceInfo );
56+
57+ assertEquals ("mongodb+srv://testUser:testPass@testHost/testDB?ssl=false&authSource=admin" , properties .getUri ());
58+ }
59+
60+ @ Test
61+ public void testEmptyAuthSource () {
62+ Map <String , String > credentials = new HashMap <>();
63+ credentials .put ("username" , "testUser" );
64+ credentials .put ("password" , "testPass" );
65+
66+ Map <String , Object > options = new HashMap <>();
67+ options .put ("ssl" , "true" );
68+
69+ TestMongoDBConfig mongoDBConfig = new TestMongoDBConfig ("testResource" );
70+
71+ ResourceInfo resourceInfo = new ResourceInfo ();
72+ resourceInfo .setCredentials (credentials );
73+ resourceInfo .setOptions (options );
74+ resourceInfo .setHost ("testHost" );
75+
76+ MongoProperties properties = mongoDBConfig .createMongoUriProperties ("testDB" , "" , resourceInfo );
77+
78+ assertEquals ("mongodb+srv://testUser:testPass@testHost/testDB?ssl=true" , properties .getUri ());
79+ }
80+
81+ private class TestMongoDBConfig extends AbstractMongoDBConfig {
82+ public TestMongoDBConfig (String resourceName ) {
83+ super (resourceName );
84+ }
85+ }
86+ }
0 commit comments