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