Skip to content

Commit

Permalink
Merge branch 'develop' into feature/code-quality
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenzo-ingenito committed Jan 10, 2025
2 parents 10b4a16 + 8f7f78f commit b1f2c91
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,34 @@
*/
package it.finanze.sanita.fse2.ms.gtw.rulesmanager.repository.impl;

import static com.mongodb.client.model.Accumulators.push;
import static com.mongodb.client.model.Aggregates.group;
import static com.mongodb.client.model.Aggregates.match;
import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.repository.entity.SchemaETY.FIELD_DELETED;
import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.repository.entity.SchemaETY.FIELD_ID;
import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.repository.entity.SchemaETY.FIELD_LAST_SYNC;
import static java.util.Arrays.asList;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoException;
import com.mongodb.MongoNamespace;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.RenameCollectionOptions;

import it.finanze.sanita.fse2.ms.gtw.rulesmanager.exceptions.eds.EdsDbException;
import it.finanze.sanita.fse2.ms.gtw.rulesmanager.repository.IExecutorRepo;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.mongodb.client.model.Accumulators.push;
import static com.mongodb.client.model.Aggregates.group;
import static com.mongodb.client.model.Aggregates.match;
import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.repository.entity.SchemaETY.*;
import static java.util.Arrays.asList;

@Repository
public class ExecutorRepo implements IExecutorRepo {
Expand Down Expand Up @@ -64,17 +69,34 @@ public void rename(String src, String target) throws EdsDbException {
rename(mongo.getCollection(src), target);
}

// public boolean exists(String name) throws EdsDbException {
// // Working var
// boolean exists;
// try {
// // Verify
// exists = mongo.collectionExists(name);
// } catch (MongoException e) {
// // Catch data-layer runtime exceptions and turn into a checked exception
// throw new EdsDbException("Unable to verify collection existence", e);
// }
// return exists;
// }

public boolean exists(String name) throws EdsDbException {
// Working var
boolean exists;
try {
// Verify
exists = mongo.collectionExists(name);
// Get the MongoDatabase instance from MongoTemplate
MongoDatabase database = mongo.getDb();

// Get the list of collection names in the database
List<String> collectionNames = new ArrayList<>();
database.listCollectionNames().into(collectionNames);

// Check if the collection exists
return collectionNames.contains(name);
} catch (MongoException e) {
// Catch data-layer runtime exceptions and turn into a checked exception
// Catch MongoDB exceptions and throw a custom exception
throw new EdsDbException("Unable to verify collection existence", e);
}
return exists;
}

public void drop(String name) throws EdsDbException {
Expand All @@ -91,7 +113,8 @@ public MongoCollection<Document> create(String name) throws EdsDbException {
// Working var
MongoCollection<Document> collection;
// Verify we do not overwrite an existing collection
if(mongo.collectionExists(name)) {
// if(mongo.collectionExists(name)) {
if(exists(name)) {
throw new EdsDbException("The collection already exists: " + name);
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void setup() throws Exception {
}

@Test
void rename() {
void rename() throws EdsDbException {
// Drop if exists
mongo.dropCollection(TEST_COLL_A);
mongo.dropCollection(TEST_COLL_B);
Expand All @@ -74,42 +74,25 @@ void rename() {
// Rename and drop obsolete target
assertDoesNotThrow(() -> repository.rename(tmp, TEST_COLL_B));
// Verify if new one exists
assertTrue(mongo.collectionExists(TEST_COLL_B));
assertTrue(repository.exists(TEST_COLL_B));
// Verify if old one has been removed
assertFalse(mongo.collectionExists(TEST_COLL_A));
assertFalse(repository.exists(TEST_COLL_A));
// Now drop
mongo.dropCollection(TEST_COLL_B);
}

@Test
void exists() {
void drop() throws EdsDbException {
// Drop if exists
mongo.dropCollection(TEST_COLL_A);
// Create collection
mongo.createCollection(TEST_COLL_A);
// Verify exists
assertDoesNotThrow(() -> assertTrue(repository.exists(TEST_COLL_A)));
assertDoesNotThrow(() -> assertFalse(repository.exists(TEST_COLL_B)));
// Now drop
mongo.dropCollection(TEST_COLL_A);
// Provide knowledge
doThrow(new MongoException("Test")).when(mongo).collectionExists(anyString());
// Drop
assertThrows(EdsDbException.class, () -> repository.exists(TEST_COLL_A));
}

@Test
void drop() {
// Drop if exists
mongo.dropCollection(TEST_COLL_A);
// Create collection
mongo.createCollection(TEST_COLL_A);
// Verify exists
assertTrue(mongo.collectionExists(TEST_COLL_A));
assertTrue(repository.exists(TEST_COLL_A));
// Drop
assertDoesNotThrow(() -> repository.drop(TEST_COLL_A));
// Verify not exists
assertFalse(mongo.collectionExists(TEST_COLL_A));
assertFalse(repository.exists(TEST_COLL_A));
// Drop collection that doesn't exist
assertDoesNotThrow(() -> repository.drop(TEST_COLL_B));
// Provide knowledge
Expand All @@ -119,19 +102,19 @@ void drop() {
}

@Test
void create() {
void create() throws EdsDbException {
// Drop if exists
mongo.dropCollection(TEST_COLL_A);
// Create collection
assertDoesNotThrow(() -> repository.create(TEST_COLL_A));
// Verify exists
assertTrue(mongo.collectionExists(TEST_COLL_A));
assertTrue(repository.exists(TEST_COLL_A));
// Now create one with the same name
assertThrows(EdsDbException.class, () -> repository.create(TEST_COLL_A));
// Drop
assertDoesNotThrow(() -> mongo.dropCollection(TEST_COLL_A));
// Verify not exists
assertFalse(mongo.collectionExists(TEST_COLL_A));
assertFalse(repository.exists(TEST_COLL_A));
// Provide knowledge
doThrow(new MongoException("Test")).when(mongo).createCollection(anyString());
// Create
Expand All @@ -149,7 +132,7 @@ void cloner() {
// Clone base repository already filled with docs
MongoCollection<Document> copy = repository.clone(TEST_BASE_COLLECTION, TEST_COLL_A);
// Verify it now exists
assertTrue(mongo.collectionExists(TEST_COLL_A));
assertTrue(repository.exists(TEST_COLL_A));
// Verify documents
assertEquals(copy.countDocuments(), SCHEMA_TEST_SIZE);
// Now compare deeply
Expand All @@ -164,11 +147,11 @@ void cloner() {
}

@Test
void sync() {
void sync() throws EdsDbException {
// Drop
mongo.dropCollection(TEST_COLL_A);
// Verify base repository is up
assertTrue(mongo.collectionExists(TEST_BASE_COLLECTION));
assertTrue(repository.exists(TEST_BASE_COLLECTION));
// Now set sync
Date currentSync = new Date();
// Set it
Expand All @@ -183,11 +166,11 @@ void sync() {
}

@Test
void ids() {
void ids() throws EdsDbException {
// Drop
mongo.dropCollection(TEST_COLL_A);
// Verify base repository is up
assertTrue(mongo.collectionExists(TEST_BASE_COLLECTION));
assertTrue(repository.exists(TEST_BASE_COLLECTION));
// Verify does not throw
assertDoesNotThrow(() -> {
// Retrieve active docs
Expand All @@ -200,9 +183,9 @@ void ids() {
}

@Test
void count() {
void count() throws EdsDbException {
// Verify base repository is up
assertTrue(mongo.collectionExists(TEST_BASE_COLLECTION));
assertTrue(repository.exists(TEST_BASE_COLLECTION));
// Verify does not throw
assertDoesNotThrow(() -> {
// Retrieve active docs
Expand Down
Loading

0 comments on commit b1f2c91

Please sign in to comment.