Repository navigation
Expand file tree
/
Copy pathMongoDatabaseIT.java
More file actions
215 lines (175 loc) · 7.93 KB
/
Copy pathMongoDatabaseIT.java
File metadata and controls
215 lines (175 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package dbProcs;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import testUtils.TestProperties;
public class MongoDatabaseIT {
private static String TEST_PATH = "mongo_challenge_test";
private static final Logger log = LogManager.getLogger(MongoDatabaseIT.class);
@BeforeAll
public static void initAll() throws IOException {
TestProperties.setTestPropertiesFileDirectory(log);
TestProperties.createMysqlResource();
TestProperties.createMongoResource();
}
@BeforeEach
public void setupEach() {
MongoDatabase.resetInstance();
}
@AfterEach
public void teardownEach() {
MongoDatabase.resetInstance();
}
@Test
@DisplayName("Should Return Type MongoCredentials")
public void getMongoChallengeCredentials_ShouldReturnTypeMongoCredentials() throws IOException {
assertInstanceOf(
MongoCredential.class, MongoDatabase.getMongoChallengeCredentials(null, TEST_PATH));
}
@Test
@DisplayName("Should read properties file for mongo challenge credentials")
public void getMongoChallengeCredentials_ShouldReadPropertiesFileForCreds() throws IOException {
MongoCredential creds = MongoDatabase.getMongoChallengeCredentials(null, TEST_PATH);
String username = creds.getUserName();
char[] password = creds.getPassword();
String databasename = creds.getSource();
assertEquals("test_user", username);
assertArrayEquals("test_password".toCharArray(), password);
assertEquals("test_dbname", databasename);
}
@Test
@DisplayName("Should Return Type String")
public void getMongoChallengeCollName_ShouldReturnTypeString() {
String collName = MongoDatabase.getMongoChallengeCollName(null, TEST_PATH);
assertNotNull(collName);
assertInstanceOf(String.class, collName);
}
@Test
@DisplayName("Should read properties file with connection details to challenge")
public void getMongoChallengeCollName_ReadPropertiesFile() {
String collName = MongoDatabase.getMongoChallengeCollName(null, TEST_PATH);
assertEquals("test_collection", collName);
}
@Test
@DisplayName("Should return a MongoClient instance")
public void getMongoDbConnection_ShouldReturnTypeMongoClient() {
MongoCredential credential =
MongoCredential.createScramSha1Credential("test", "test", "test".toCharArray());
assertInstanceOf(MongoClient.class, MongoDatabase.getMongoDbConnection(null));
assertInstanceOf(MongoClient.class, MongoDatabase.getMongoDbConnection(null, credential));
}
@Test
@DisplayName("Must return type (Mongo) DB")
@Disabled(
"Fongo 2.1.0 is incompatible with mongo-java-driver 3.12.14 (NPE in"
+ " createOperationExecutor)")
public void getMongoDatabase_ShouldReturnTypeDB() {}
@Test
@DisplayName("Read properties file for db name")
@Disabled(
"Fongo 2.1.0 is incompatible with mongo-java-driver 3.12.14 (NPE in"
+ " createOperationExecutor)")
public void getMongoDatabase_ReadDbName() {}
// ============= Singleton Pattern Tests =============
@Test
@DisplayName("Should return same MongoClient instance on multiple calls (singleton)")
public void getMongoDbConnection_ShouldReturnSingletonInstance() {
MongoClient client1 = MongoDatabase.getMongoDbConnection(null);
MongoClient client2 = MongoDatabase.getMongoDbConnection(null);
assertNotNull(client1, "First MongoClient should not be null");
assertNotNull(client2, "Second MongoClient should not be null");
assertSame(client1, client2, "Multiple calls should return the same MongoClient instance");
}
@Test
@DisplayName("Should return same MongoClient for same credentials (singleton)")
public void getMongoDbConnection_WithCredentials_ShouldReturnSingletonPerCredential() {
MongoCredential credential1 =
MongoCredential.createScramSha1Credential("user1", "db1", "pass1".toCharArray());
MongoCredential credential2 =
MongoCredential.createScramSha1Credential("user1", "db1", "pass1".toCharArray());
MongoClient client1 = MongoDatabase.getMongoDbConnection(null, credential1);
MongoClient client2 = MongoDatabase.getMongoDbConnection(null, credential2);
assertNotNull(client1, "First MongoClient should not be null");
assertNotNull(client2, "Second MongoClient should not be null");
assertSame(client1, client2, "Same credentials should return the same MongoClient instance");
}
@Test
@DisplayName("Should track initialization state correctly")
public void isInitialized_ShouldTrackState() {
assertFalse(MongoDatabase.isInitialized(), "Should not be initialized before first use");
MongoDatabase.getMongoDbConnection(null);
assertTrue(MongoDatabase.isInitialized(), "Should be initialized after first use");
MongoDatabase.resetInstance();
assertFalse(MongoDatabase.isInitialized(), "Should not be initialized after reset");
}
@Test
@DisplayName("Should handle concurrent singleton initialization safely")
public void getMongoDbConnection_ShouldBeThreadSafe() throws InterruptedException {
final int numThreads = 10;
final CountDownLatch startLatch = new CountDownLatch(1);
final CountDownLatch doneLatch = new CountDownLatch(numThreads);
final AtomicReference<MongoClient> firstClient = new AtomicReference<>();
final java.util.concurrent.atomic.AtomicBoolean allSame =
new java.util.concurrent.atomic.AtomicBoolean(true);
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
for (int i = 0; i < numThreads; i++) {
executor.submit(
() -> {
try {
startLatch.await();
MongoClient client = MongoDatabase.getMongoDbConnection(null);
if (!firstClient.compareAndSet(null, client)) {
if (firstClient.get() != client) {
allSame.set(false);
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
doneLatch.countDown();
}
});
}
startLatch.countDown();
boolean completed = doneLatch.await(30, TimeUnit.SECONDS);
executor.shutdown();
assertTrue(completed, "All threads should complete within timeout");
assertTrue(allSame.get(), "All threads should get the same MongoClient instance");
}
@Test
@DisplayName("Shutdown should close all connections")
public void shutdown_ShouldCloseConnections() {
MongoDatabase.getMongoDbConnection(null);
assertTrue(MongoDatabase.isInitialized(), "Should be initialized");
MongoDatabase.shutdown();
assertFalse(MongoDatabase.isInitialized(), "Should not be initialized after shutdown");
}
@Test
@DisplayName("Reset should be equivalent to shutdown")
public void resetInstance_ShouldBeEquivalentToShutdown() {
MongoDatabase.getMongoDbConnection(null);
assertTrue(MongoDatabase.isInitialized(), "Should be initialized");
MongoDatabase.resetInstance();
assertFalse(MongoDatabase.isInitialized(), "Should not be initialized after reset");
}
}