Skip to content

Commit 4837ce6

Browse files
committed
Fixed severity posed by sonarqube
1 parent b82cdf7 commit 4837ce6

File tree

5 files changed

+128
-28
lines changed

5 files changed

+128
-28
lines changed

caching/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
MONGO_USER=
2+
MONGO_PASSWORD=
3+
MONGO_DB_NAME=
4+
MONGO_DB_PORT=

caching/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
/target/
2+
3+
### ENV Files ###
4+
.env

caching/pom.xml

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,30 @@
4747
<dependency>
4848
<groupId>org.mongodb</groupId>
4949
<artifactId>bson</artifactId>
50+
<version>4.9.0</version>
5051
</dependency>
51-
<dependency>
52-
<groupId>org.mongodb</groupId>
53-
<artifactId>mongodb-driver-legacy</artifactId>
54-
</dependency>
52+
<dependency>
53+
<groupId>org.mongodb</groupId>
54+
<artifactId>mongodb-driver-sync</artifactId>
55+
<version>4.9.0</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.mongodb</groupId>
59+
<artifactId>mongodb-driver-core</artifactId>
60+
<version>4.9.0</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>io.github.cdimascio</groupId>
64+
<artifactId>java-dotenv</artifactId>
65+
<version>5.2.2</version>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.projectlombok</groupId>
69+
<artifactId>lombok</artifactId>
70+
<version>1.18.30</version>
71+
<scope>provided</scope>
72+
</dependency>
73+
5574
</dependencies>
5675
<!--
5776
Due to the use of MongoDB in the test of this pattern, TRAVIS and/or MAVEN might fail if the DB connection is

caching/src/main/java/com/iluwatar/caching/database/MongoDb.java

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/*
2-
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license.
3+
* Module model-view-viewmodel is using ZK framework licensed under LGPL
4+
* (see lgpl-3.0.txt).
35
*
46
* The MIT License
57
* Copyright © 2014-2022 Ilkka Seppälä
@@ -31,12 +33,16 @@
3133

3234
import com.iluwatar.caching.UserAccount;
3335
import com.iluwatar.caching.constants.CachingConstants;
34-
import com.mongodb.MongoClient;
35-
import com.mongodb.MongoClientOptions;
36+
import com.mongodb.MongoClientSettings;
3637
import com.mongodb.MongoCredential;
3738
import com.mongodb.ServerAddress;
39+
import com.mongodb.client.MongoClient;
40+
import com.mongodb.client.MongoClients;
3841
import com.mongodb.client.MongoDatabase;
3942
import com.mongodb.client.model.UpdateOptions;
43+
import io.github.cdimascio.dotenv.Dotenv;
44+
import java.util.Collections;
45+
import lombok.Setter;
4046
import lombok.extern.slf4j.Slf4j;
4147
import org.bson.Document;
4248

@@ -46,29 +52,69 @@
4652
*/
4753
@Slf4j
4854
public class MongoDb implements DbManager {
49-
private static final String DATABASE_NAME = "admin";
50-
private static final String MONGO_USER = "root";
51-
private static final String MONGO_PASSWORD = "rootpassword";
55+
56+
/**Initialize Dotenv object.*/
57+
public static final Dotenv DOTENV = Dotenv.load();
58+
59+
/**Get database name from .env file.*/
60+
private static final String DATABASE_NAME = DOTENV.get("MONGO_DB_NAME");
61+
62+
/**Get mongo username from .env file.*/
63+
private static final String MONGO_USER = DOTENV.get("MONGO_USER");
64+
65+
/**Get mongo password from .env file.*/
66+
private static final String MONGO_PASSWORD = DOTENV.get("MONGO_PASSWORD");
67+
68+
/**Get mongo db port number from .env file.*/
69+
private static final String MONGO_DB_PORT = DOTENV.get("MONGO_DB_PORT");
70+
71+
/**Declaring MongoClient Object.*/
5272
private MongoClient client;
53-
private MongoDatabase db;
5473

55-
void setDb(MongoDatabase db) {
56-
this.db = db;
57-
}
74+
/**Setting MongoDatabase object using Lombok annotation.*/
75+
@Setter
76+
private MongoDatabase db;
5877

5978
/**
60-
* Connect to Db. Check th connection
79+
* Connect to Db. Check th connection.
6180
*/
6281
@Override
6382
public void connect() {
64-
MongoCredential mongoCredential = MongoCredential.createCredential(MONGO_USER,
65-
DATABASE_NAME,
66-
MONGO_PASSWORD.toCharArray());
67-
MongoClientOptions options = MongoClientOptions.builder().build();
68-
client = new MongoClient(new ServerAddress(), mongoCredential, options);
83+
// Create credentials
84+
assert MONGO_USER != null;
85+
assert DATABASE_NAME != null;
86+
assert MONGO_PASSWORD != null;
87+
assert MONGO_DB_PORT != null;
88+
MongoCredential credential = MongoCredential.createCredential(
89+
MONGO_USER,
90+
DATABASE_NAME,
91+
MONGO_PASSWORD.toCharArray()
92+
);
93+
94+
// Set the cluster settings for server address
95+
MongoClientSettings settings = MongoClientSettings.builder()
96+
.applyToClusterSettings(builder ->
97+
builder.hosts(
98+
Collections.singletonList(
99+
new ServerAddress(
100+
"localhost",
101+
Integer.parseInt(MONGO_DB_PORT)
102+
)
103+
)
104+
))
105+
.credential(credential) // Add credentials
106+
.build();
107+
108+
// Create the mongoDBClient with settings
109+
client = MongoClients.create(settings);
110+
111+
// Get the database
69112
db = client.getDatabase(DATABASE_NAME);
70113
}
71114

115+
/**
116+
* Close the connection.
117+
*/
72118
@Override
73119
public void disconnect() {
74120
client.close();

caching/src/test/java/com/iluwatar/caching/database/MongoDbTest.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import com.mongodb.client.FindIterable;
3030
import com.mongodb.client.MongoCollection;
3131
import com.mongodb.client.MongoDatabase;
32+
import com.mongodb.client.result.InsertOneResult;
33+
import com.mongodb.client.result.UpdateResult;
3234
import org.bson.Document;
3335
import org.junit.jupiter.api.BeforeEach;
3436
import org.junit.jupiter.api.Test;
@@ -48,7 +50,7 @@ class MongoDbTest {
4850

4951
@Mock
5052
MongoDatabase db;
51-
private MongoDb mongoDb = new MongoDb();
53+
private final MongoDb mongoDb = new MongoDb();
5254

5355
private UserAccount userAccount;
5456

@@ -67,8 +69,8 @@ void connect() {
6769
@Test
6870
void readFromDb() {
6971
Document document = new Document(USER_ID, ID)
70-
.append(USER_NAME, NAME)
71-
.append(ADD_INFO, ADDITIONAL_INFO);
72+
.append(USER_NAME, NAME)
73+
.append(ADD_INFO, ADDITIONAL_INFO);
7274
MongoCollection<Document> mongoCollection = mock(MongoCollection.class);
7375
when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection);
7476

@@ -77,27 +79,53 @@ void readFromDb() {
7779

7880
when(findIterable.first()).thenReturn(document);
7981

80-
assertEquals(mongoDb.readFromDb(ID),userAccount);
82+
assertEquals(mongoDb.readFromDb(ID), userAccount);
8183
}
8284

8385
@Test
8486
void writeToDb() {
87+
// Create a mock for the MongoCollection
8588
MongoCollection<Document> mongoCollection = mock(MongoCollection.class);
89+
90+
// Stub the getCollection method to return the mocked mongoCollection
8691
when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection);
87-
assertDoesNotThrow(()-> {mongoDb.writeToDb(userAccount);});
92+
93+
// Create a mock for the InsertOneResult (assuming you want to capture the return type)
94+
InsertOneResult mockResult = mock(InsertOneResult.class);
95+
96+
// Mock the insertOne method to return the mocked result
97+
when(mongoCollection.insertOne(any(Document.class))).thenReturn(mockResult);
98+
99+
// Assert that calling writeToDb does not throw an exception
100+
assertDoesNotThrow(() -> {
101+
mongoDb.writeToDb(userAccount);
102+
});
88103
}
89104

105+
90106
@Test
91107
void updateDb() {
92108
MongoCollection<Document> mongoCollection = mock(MongoCollection.class);
93109
when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection);
94-
assertDoesNotThrow(()-> {mongoDb.updateDb(userAccount);});
110+
111+
// If updateOne is not void, you should return a mock or a new instance of UpdateResult
112+
UpdateResult mockResult = mock(UpdateResult.class);
113+
when(mongoCollection.updateOne(any(Document.class), any(Document.class))).thenReturn(mockResult);
114+
115+
assertDoesNotThrow(() -> { mongoDb.updateDb(userAccount); });
95116
}
96117

97118
@Test
98119
void upsertDb() {
99120
MongoCollection<Document> mongoCollection = mock(MongoCollection.class);
100121
when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection);
101-
assertDoesNotThrow(()-> {mongoDb.upsertDb(userAccount);});
122+
123+
// Similar to updateDb
124+
UpdateResult mockResult = mock(UpdateResult.class);
125+
when(mongoCollection.updateOne(any(Document.class), any(Document.class))).thenReturn(mockResult);
126+
127+
assertDoesNotThrow(() -> { mongoDb.upsertDb(userAccount); });
102128
}
103-
}
129+
130+
131+
}

0 commit comments

Comments
 (0)