diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/repository/impl/ExecutorRepo.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/repository/impl/ExecutorRepo.java index 6b723bc..2119293 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/repository/impl/ExecutorRepo.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/repository/impl/ExecutorRepo.java @@ -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 { @@ -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 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 { @@ -91,7 +113,8 @@ public MongoCollection create(String name) throws EdsDbException { // Working var MongoCollection 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 { diff --git a/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/EDSRepoTest.java b/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/EDSRepoTest.java index b0fc42c..4c59889 100644 --- a/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/EDSRepoTest.java +++ b/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/EDSRepoTest.java @@ -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); @@ -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 @@ -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 @@ -149,7 +132,7 @@ void cloner() { // Clone base repository already filled with docs MongoCollection 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 @@ -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 @@ -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 @@ -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 diff --git a/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/base/db/AbstractSchemaDB.java b/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/base/db/AbstractSchemaDB.java index 6f5e8d5..ffbd8e0 100644 --- a/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/base/db/AbstractSchemaDB.java +++ b/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/base/db/AbstractSchemaDB.java @@ -14,101 +14,116 @@ import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.eds.base.EDSTestUtils.compareDeeply; +import java.util.ArrayList; +import java.util.List; + import org.springframework.data.mongodb.core.MongoTemplate; +import com.mongodb.MongoException; +import com.mongodb.client.MongoDatabase; + import it.finanze.sanita.fse2.ms.gtw.rulesmanager.config.eds.changeset.ChangesetCFG; import it.finanze.sanita.fse2.ms.gtw.rulesmanager.eds.base.entities.AbstractEntityHandler; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.exceptions.eds.EdsDbException; public abstract class AbstractSchemaDB { - + public static String TEST_BASE_COLLECTION = "eds-test-x"; - private final MongoTemplate mongo; - private final AbstractEntityHandler hnd; - private final ChangesetCFG config; - - public AbstractSchemaDB(MongoTemplate mongo, AbstractEntityHandler hnd, ChangesetCFG config) { - this.mongo = mongo; - this.hnd = hnd; - this.config = config; - } - - public void setupTest() throws Exception { - setupRepository(TEST_BASE_COLLECTION); - } - - public void setupProduction() throws Exception { - setupRepository(config.getProduction()); - } - - public void setupStaging() throws Exception { - setupRepository(config.getStaging()); - } - - private void setupRepository(String name) throws Exception { - // Verify if previous test database exists - if (isSchemaAvailable(name)) { - // Drop it, we are going to create a new one - mongo.dropCollection(name); - } - // Make test collection - createEmptySchema(name); - // Init entities - hnd.initTestEntities(); - // Add to collection - addEntityToSchema(name); - } - - public void clearTest() { - clearRepository(TEST_BASE_COLLECTION); - } - - public void clearProduction() { - clearRepository(config.getProduction()); - } - - public void clearStaging() { - clearRepository(config.getStaging()); - } - - private void clearRepository(String name) { - mongo.dropCollection(name); - hnd.clearTestEntities(); - } - - private void createEmptySchema(String name) { - mongo.createCollection(name); - } - - private void addEntityToSchema(String name) { - mongo.insert(hnd.getEntities(), name); - } - - public boolean isProductionAvailable() { - return isSchemaAvailable(config.getProduction()); - } - - public boolean isStagingAvailable() { - return isSchemaAvailable(config.getStaging()); - } - - private boolean isSchemaAvailable(String name) { - return mongo.getCollectionNames().contains(name); - } - - public AbstractEntityHandler handler() { - return hnd; - } - - public boolean verifyIntegrityProduction() { - return verifyIntegrity(config.getProduction()); - } - - public boolean verifyIntegrityStaging() { - return verifyIntegrity(config.getStaging()); - } - - private boolean verifyIntegrity(String name) { - return compareDeeply(hnd.getDocuments(), mongo.getCollection(name)); - } + private final MongoTemplate mongo; + private final AbstractEntityHandler hnd; + private final ChangesetCFG config; + + public AbstractSchemaDB(MongoTemplate mongo, AbstractEntityHandler hnd, ChangesetCFG config) { + this.mongo = mongo; + this.hnd = hnd; + this.config = config; + } + + public void setupTest() throws Exception { + setupRepository(TEST_BASE_COLLECTION); + } + + public void setupProduction() throws Exception { + setupRepository(config.getProduction()); + } + + public void setupStaging() throws Exception { + setupRepository(config.getStaging()); + } + + private void setupRepository(String name) throws Exception { + // Verify if previous test database exists + if (isSchemaAvailable(name)) { + // Drop it, we are going to create a new one + mongo.dropCollection(name); + } + // Make test collection + createEmptySchema(name); + // Init entities + hnd.initTestEntities(); + // Add to collection + addEntityToSchema(name); + } + + public void clearTest() { + clearRepository(TEST_BASE_COLLECTION); + } + + public void clearProduction() { + clearRepository(config.getProduction()); + } + + public void clearStaging() { + clearRepository(config.getStaging()); + } + + private void clearRepository(String name) { + mongo.dropCollection(name); + hnd.clearTestEntities(); + } + + private void createEmptySchema(String name) { + mongo.createCollection(name); + } + + private void addEntityToSchema(String name) { + mongo.insert(hnd.getEntities(), name); + } + + public boolean isProductionAvailable() { + return isSchemaAvailable(config.getProduction()); + } + + public boolean isStagingAvailable() { + return isSchemaAvailable(config.getStaging()); + } + + private boolean isSchemaAvailable(String name) { + // Get the MongoDatabase instance from MongoTemplate + MongoDatabase database = mongo.getDb(); + + // Get the list of collection names in the database + List collectionNames = new ArrayList<>(); + database.listCollectionNames().into(collectionNames); + + // Check if the collection exists + return collectionNames.contains(name); + } + + public AbstractEntityHandler handler() { + return hnd; + } + + public boolean verifyIntegrityProduction() { + return verifyIntegrity(config.getProduction()); + } + + public boolean verifyIntegrityStaging() { + return verifyIntegrity(config.getStaging()); + } + + private boolean verifyIntegrity(String name) { + return compareDeeply(hnd.getDocuments(), mongo.getCollection(name)); + } } diff --git a/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/DictExecutorTest.java b/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/DictExecutorTest.java index d13f17d..a28d8fd 100644 --- a/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/DictExecutorTest.java +++ b/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/DictExecutorTest.java @@ -12,10 +12,20 @@ */ package it.finanze.sanita.fse2.ms.gtw.rulesmanager.eds.executors; -import it.finanze.sanita.fse2.ms.gtw.rulesmanager.eds.base.db.impl.EDSTermsDB; -import it.finanze.sanita.fse2.ms.gtw.rulesmanager.mock.MockDictionaryExecutor; -import it.finanze.sanita.fse2.ms.gtw.rulesmanager.repository.entity.TerminologyETY; -import it.finanze.sanita.fse2.ms.gtw.rulesmanager.scheduler.actions.impl.DerivedActionEDS; +import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.config.Constants.Profile.TEST; +import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.eds.base.entities.impl.EDSTermsHandler.EXPECTED_DICTIONARIES; +import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.eds.base.entities.impl.EDSTermsHandler.EXPECTED_SYSTEMS; +import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums.ActionRes.OK; +import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums.ActionRes.CallbackRes.CB_OK; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; + +import java.util.List; + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -26,15 +36,12 @@ import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.test.context.ActiveProfiles; -import java.util.List; - -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.config.Constants.Profile.TEST; -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.eds.base.entities.impl.EDSTermsHandler.EXPECTED_DICTIONARIES; -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.eds.base.entities.impl.EDSTermsHandler.EXPECTED_SYSTEMS; -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums.ActionRes.CallbackRes.CB_OK; -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums.ActionRes.OK; -import static org.junit.jupiter.api.Assertions.*; -import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.eds.base.db.impl.EDSTermsDB; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.exceptions.eds.EdsDbException; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.mock.MockDictionaryExecutor; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.repository.IExecutorRepo; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.repository.entity.TerminologyETY; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.scheduler.actions.impl.DerivedActionEDS; @SpringBootTest @ActiveProfiles(TEST) @@ -46,6 +53,8 @@ class DictExecutorTest { @Autowired private MockDictionaryExecutor executor; @Autowired + private IExecutorRepo executorRepo; + @Autowired private EDSTermsDB db; @BeforeAll @@ -61,32 +70,48 @@ void onClean() { } @Test - void cleanBackup() { + void cleanBackup() throws EdsDbException { // Call createBackup assertEquals(OK, executor.createBackup()); - assertTrue(mongo.collectionExists(executor.getConfig().getBackup())); +// assertTrue(mongo.collectionExists(executor.getConfig().getBackup())); + assertTrue(executorRepo.exists(executor.getConfig().getBackup())); // Call onCleanBackup assertEquals(OK, executor.onCleanBackup()); - assertFalse(mongo.collectionExists(executor.getConfig().getBackup())); +// assertFalse(mongo.collectionExists(executor.getConfig().getBackup())); + assertFalse(executorRepo.exists(executor.getConfig().getBackup())); } @Test - void processing() { - assertDoesNotThrow(() -> { - // Setup staging - assertEquals(OK, executor.createStaging()); - db.setupStaging(); - assertTrue(mongo.collectionExists(executor.getConfig().getStaging())); - // Call processing - assertEquals(OK, executor.onProcessing()); - List terminologies = mongo.findAll(TerminologyETY.class, executor.getConfig().getStaging()); - assertEquals(EXPECTED_DICTIONARIES, terminologies.size()); - // If this return false, at least one element has a different system from the one declared - assertTrue( - terminologies.stream().map(TerminologyETY::getSystem).allMatch(EXPECTED_SYSTEMS::contains) - ); - }); + void processing() throws Exception { + // Setup staging + assertEquals(OK, executor.createStaging()); + db.setupStaging(); +// assertTrue(mongo.collectionExists(executor.getConfig().getStaging())); + assertTrue(executorRepo.exists(executor.getConfig().getStaging())); + // Call processing + assertEquals(OK, executor.onProcessing()); + List terminologies = mongo.findAll(TerminologyETY.class, executor.getConfig().getStaging()); + assertEquals(EXPECTED_DICTIONARIES, terminologies.size()); + // If this return false, at least one element has a different system from the one declared + assertTrue( + terminologies.stream().map(TerminologyETY::getSystem).allMatch(EXPECTED_SYSTEMS::contains) + ); +// assertDoesNotThrow(() -> { +// // Setup staging +// assertEquals(OK, executor.createStaging()); +// db.setupStaging(); +//// assertTrue(mongo.collectionExists(executor.getConfig().getStaging())); +// assertTrue(executorRepo.exists(executor.getConfig().getStaging())); +// // Call processing +// assertEquals(OK, executor.onProcessing()); +// List terminologies = mongo.findAll(TerminologyETY.class, executor.getConfig().getStaging()); +// assertEquals(EXPECTED_DICTIONARIES, terminologies.size()); +// // If this return false, at least one element has a different system from the one declared +// assertTrue( +// terminologies.stream().map(TerminologyETY::getSystem).allMatch(EXPECTED_SYSTEMS::contains) +// ); +// }); } @Test diff --git a/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/EngineExecutorTest.java b/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/EngineExecutorTest.java index 1acd2e1..432f13d 100644 --- a/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/EngineExecutorTest.java +++ b/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/EngineExecutorTest.java @@ -73,13 +73,16 @@ void onClean() { } @Test - void cleanBackup() { + void cleanBackup() throws EdsDbException { // Call createBackup assertEquals(OK, executor.createBackup()); - assertTrue(mongo.collectionExists(executor.getConfig().getBackup())); +// assertTrue(mongo.collectionExists(executor.getConfig().getBackup())); + assertTrue(repository.exists(executor.getConfig().getBackup())); + // Call onCleanBackup assertEquals(OK, executor.onCleanBackup()); - assertFalse(mongo.collectionExists(executor.getConfig().getBackup())); +// assertFalse(mongo.collectionExists(executor.getConfig().getBackup())); + assertFalse(repository.exists(executor.getConfig().getBackup())); } @Test @@ -93,7 +96,7 @@ void processing() { assertDoesNotThrow(() -> { db.setupStaging(); assertEquals(OK, executor.onStaging()); - assertTrue(mongo.collectionExists(executor.getConfig().getStaging())); + assertTrue(repository.exists(executor.getConfig().getStaging())); assertEquals(OK, executor.onProcessing()); List engines = mongo.findAll(EngineETY.class , executor.getConfig().getStaging()); assertEquals(1, engines.size()); @@ -110,12 +113,16 @@ void processing() { void recovery() throws EdsDbException { // Process staging and create backup assertEquals(OK, executor.onStaging()); - assertTrue(mongo.collectionExists(executor.getConfig().getStaging())); - assertTrue(mongo.collectionExists(executor.getConfig().getBackup())); +// assertTrue(mongo.collectionExists(executor.getConfig().getStaging())); + assertTrue(repository.exists(executor.getConfig().getStaging())); +// assertTrue(mongo.collectionExists(executor.getConfig().getBackup())); + assertTrue(repository.exists(executor.getConfig().getBackup())); // Attempt to restore backup as production assertEquals(CB_OK, executor.onRecovery()); - assertTrue(mongo.collectionExists(executor.getConfig().getProduction())); - assertFalse(mongo.collectionExists(executor.getConfig().getBackup())); +// assertTrue(mongo.collectionExists(executor.getConfig().getProduction())); + assertTrue(repository.exists(executor.getConfig().getProduction())); +// assertFalse(mongo.collectionExists(executor.getConfig().getBackup())); + assertFalse(repository.exists(executor.getConfig().getBackup())); // Emulate runtime exception doThrow(new EdsDbException("Test error")).when(repository).rename(anyString(), anyString()); assertEquals(CB_KO, executor.onRecovery()); diff --git a/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/ExecutorTest.java b/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/ExecutorTest.java index fe5516f..230bbe4 100644 --- a/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/ExecutorTest.java +++ b/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/ExecutorTest.java @@ -19,7 +19,9 @@ import it.finanze.sanita.fse2.ms.gtw.rulesmanager.dto.eds.data.SchemaDTO; import it.finanze.sanita.fse2.ms.gtw.rulesmanager.dto.eds.data.SchemaDTO.Schema; import it.finanze.sanita.fse2.ms.gtw.rulesmanager.eds.base.db.impl.EDSSchemaDB; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.exceptions.BusinessException; import it.finanze.sanita.fse2.ms.gtw.rulesmanager.exceptions.eds.EdsClientException; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.exceptions.eds.EdsDbException; import it.finanze.sanita.fse2.ms.gtw.rulesmanager.mock.MockSchemaExecutor; import it.finanze.sanita.fse2.ms.gtw.rulesmanager.repository.IExecutorRepo; import it.finanze.sanita.fse2.ms.gtw.rulesmanager.service.impl.ConfigSRV; @@ -110,16 +112,18 @@ void recovery() { } @Test - void clean() { + void clean() throws EdsDbException { // Setup production setupProduction(); // Create collection mongo.createCollection(executor.getConfig().getStaging()); - assertTrue(mongo.collectionExists(executor.getConfig().getStaging())); +// assertTrue(mongo.collectionExists(executor.getConfig().getStaging())); + assertTrue(repository.exists(executor.getConfig().getStaging())); // Execute assertEquals(OK, executor.onClean()); // Verify - assertFalse(mongo.collectionExists(executor.getConfig().getStaging())); +// assertFalse(mongo.collectionExists(executor.getConfig().getStaging())); + assertFalse(repository.exists(executor.getConfig().getStaging())); // No collection exists, call executor without collection assertEquals(OK, executor.onClean()); // Verify production integrity @@ -177,28 +181,34 @@ void changesetNotEmpty() throws Exception { @Test - void staging() { + void staging() throws EdsDbException { // Case #1 - Production exists // Create collection setupProduction(); // Verify exists - assertTrue(mongo.collectionExists(executor.getConfig().getProduction())); +// assertTrue(mongo.collectionExists(executor.getConfig().getProduction())); + assertTrue(repository.exists(executor.getConfig().getProduction())); // Execute assertEquals(OK, executor.onStaging()); // Now staging and production co-exists - assertTrue(mongo.collectionExists(executor.getConfig().getStaging())); - assertTrue(mongo.collectionExists(executor.getConfig().getProduction())); +// assertTrue(mongo.collectionExists(executor.getConfig().getStaging())); + assertTrue(repository.exists(executor.getConfig().getStaging())); +// assertTrue(mongo.collectionExists(executor.getConfig().getProduction())); + assertTrue(repository.exists(executor.getConfig().getProduction())); // Verify production integrity db.verifyIntegrityProduction(); // Emptying database resetDB(); // Case #2 - Production not exists - assertFalse(mongo.collectionExists(executor.getConfig().getProduction())); +// assertFalse(mongo.collectionExists(executor.getConfig().getProduction())); + assertFalse(repository.exists(executor.getConfig().getProduction())); // Execute assertEquals(OK, executor.onStaging()); // There is only staging - assertTrue(mongo.collectionExists(executor.getConfig().getStaging())); - assertFalse(mongo.collectionExists(executor.getConfig().getProduction())); +// assertTrue(mongo.collectionExists(executor.getConfig().getStaging())); + assertTrue(repository.exists(executor.getConfig().getStaging())); +// assertFalse(mongo.collectionExists(executor.getConfig().getProduction())); + assertFalse(repository.exists(executor.getConfig().getProduction())); } @Test @@ -541,8 +551,15 @@ private void resetListeners() { private void resetDB() { mongo.dropCollection(executor.getConfig().getProduction()); mongo.dropCollection(executor.getConfig().getStaging()); - assertFalse(mongo.collectionExists(executor.getConfig().getStaging())); - assertFalse(mongo.collectionExists(executor.getConfig().getProduction())); + try { + assertFalse(repository.exists(executor.getConfig().getStaging())); + assertFalse(repository.exists(executor.getConfig().getProduction())); + } catch(Exception ex) { + log.error("Error"); + throw new BusinessException(ex); + } +// assertFalse(mongo.collectionExists(executor.getConfig().getStaging())); +// assertFalse(mongo.collectionExists(executor.getConfig().getProduction())); } } diff --git a/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/TermsExecutorTest.java b/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/TermsExecutorTest.java index 8ec23d2..1749ed3 100644 --- a/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/TermsExecutorTest.java +++ b/src/test/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/eds/executors/TermsExecutorTest.java @@ -16,6 +16,7 @@ import it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.IEDSClient; import it.finanze.sanita.fse2.ms.gtw.rulesmanager.dto.eds.changeset.chunk.ChangeSetChunkDTO; import it.finanze.sanita.fse2.ms.gtw.rulesmanager.eds.base.db.impl.EDSTermsDB; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.exceptions.BusinessException; import it.finanze.sanita.fse2.ms.gtw.rulesmanager.exceptions.eds.EdsClientException; import it.finanze.sanita.fse2.ms.gtw.rulesmanager.mock.impl.MockTermsExecutor; import it.finanze.sanita.fse2.ms.gtw.rulesmanager.repository.IExecutorRepo; @@ -231,8 +232,14 @@ private void setupProduction() { private void resetDB() { mongo.dropCollection(executor.getConfig().getProduction()); mongo.dropCollection(executor.getConfig().getStaging()); - assertFalse(mongo.collectionExists(executor.getConfig().getStaging())); - assertFalse(mongo.collectionExists(executor.getConfig().getProduction())); + try { + assertFalse(repository.exists(executor.getConfig().getStaging())); + assertFalse(repository.exists(executor.getConfig().getProduction())); + } catch(Exception ex) { + throw new BusinessException(ex); + } +// assertFalse(mongo.collectionExists(executor.getConfig().getStaging())); +// assertFalse(mongo.collectionExists(executor.getConfig().getProduction())); } }