diff --git a/.github/labeler.yml b/.github/labeler.yml index 2a5e0eae2..4fb14baff 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -37,3 +37,7 @@ accessibility: playwright: - maqs-playwright/* - maqs-playwright/**/* + +noSQL: + -maqs-nosql/* + -maqs-nosql/**/* diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index e531e9399..92d92be55 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -108,7 +108,7 @@ jobs: strategy: fail-fast: false matrix: - module-name: [ maqs-utilities, maqs-base, maqs-appium, maqs-selenium, maqs-webservices, maqs-cucumber, maqs-accessibility, maqs-database, maqs-playwright ] + module-name: [ maqs-utilities, maqs-base, maqs-appium, maqs-selenium, maqs-webservices, maqs-cucumber, maqs-accessibility, maqs-database, maqs-playwright, maqs-nosql ] steps: - name: Check if tests can be run if: matrix.module-name == 'maqs-appium' && github.actor == 'dependabot[bot]' diff --git a/Docker/MAQSMongoDB/docker-compose.yml b/Docker/MAQSMongoDB/docker-compose.yml new file mode 100644 index 000000000..10479a005 --- /dev/null +++ b/Docker/MAQSMongoDB/docker-compose.yml @@ -0,0 +1,23 @@ +version: '2' + +services: + mongo: + image: mongo + restart: always + ports: + - "27017:27017" + volumes: + - ./seed/seed.js:/docker-entrypoint-initdb.d/seed.js + mongo-express: + image: mongo-express + restart: always + ports: + - 8081:8081 + links: + - mongo + environment: + ME_CONFIG_MONGODB_ADMINUSERNAME: + ME_CONFIG_MONGODB_ADMINPASSWORD: + + # Run a custom bash script that bootstraps the database after it is started. + command: [ '/bin/bash', '/mnt/host/initialize_and_start_sqlserver.sh' ] \ No newline at end of file diff --git a/Docker/MAQSMongoDB/seed/seed.js b/Docker/MAQSMongoDB/seed/seed.js new file mode 100644 index 000000000..badfaf331 --- /dev/null +++ b/Docker/MAQSMongoDB/seed/seed.js @@ -0,0 +1,22 @@ +db = db.getSiblingDB('MongoDatabaseTest') +db.MongoTestCollection.drop(); +db.MongoTestCollection.insertMany([ + { + "lid": "test1", + "isChanged": true, + "order": 1 + }, + { + "lid": "test2", + "isChanged": false, + "order": 2 + }, + { + "lid": "test3", + "isChanged": false + }, + { + "lid": "test4", + "isChanged": false + } +]) \ No newline at end of file diff --git a/Docker/docker-compose.yml b/Docker/docker-compose.yml new file mode 100644 index 000000000..44c3c95f1 --- /dev/null +++ b/Docker/docker-compose.yml @@ -0,0 +1,15 @@ +# The extends functionality is not yet available in +# Docker config files versioned 3 and higher. +# If the version is bumped, this config file will have to change. +# See https://github.com/moby/moby/issues/31101. +version: '2' + +services: + mssql: + extends: + file: ./MAQSSQLServer/docker-compose.yml + service: mssql + mongo: + extends: + file: ./MAQSMongoDB/docker-compose.yml + service: mongo \ No newline at end of file diff --git a/docker/MAQSMongoDB/docker-compose.yml b/docker/MAQSMongoDB/docker-compose.yml index 5be3b1e86..10479a005 100644 --- a/docker/MAQSMongoDB/docker-compose.yml +++ b/docker/MAQSMongoDB/docker-compose.yml @@ -3,11 +3,11 @@ version: '2' services: mongo: image: mongo - restart: always + restart: always ports: - - "27017:27017" + - "27017:27017" volumes: - - ./seed/seed.js:/docker-entrypoint-initdb.d/seed.js + - ./seed/seed.js:/docker-entrypoint-initdb.d/seed.js mongo-express: image: mongo-express restart: always @@ -16,5 +16,8 @@ services: links: - mongo environment: - ME_CONFIG_MONGODB_ADMINUSERNAME: - ME_CONFIG_MONGODB_ADMINPASSWORD: \ No newline at end of file + ME_CONFIG_MONGODB_ADMINUSERNAME: + ME_CONFIG_MONGODB_ADMINPASSWORD: + + # Run a custom bash script that bootstraps the database after it is started. + command: [ '/bin/bash', '/mnt/host/initialize_and_start_sqlserver.sh' ] \ No newline at end of file diff --git a/docker/MAQSMongoDB/seed/seed.js b/docker/MAQSMongoDB/seed/seed.js index 81d443938..badfaf331 100644 --- a/docker/MAQSMongoDB/seed/seed.js +++ b/docker/MAQSMongoDB/seed/seed.js @@ -19,4 +19,4 @@ db.MongoTestCollection.insertMany([ "lid": "test4", "isChanged": false } -]) +]) \ No newline at end of file diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 2b04f0b3c..44c3c95f1 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -6,18 +6,10 @@ version: '2' services: mssql: - extends: + extends: file: ./MAQSSQLServer/docker-compose.yml service: mssql mongo: extends: file: ./MAQSMongoDB/docker-compose.yml - service: mongo - imap: - extends: - file: ./MAQSEmail/docker-compose.yml - service: imap - webservice: - extends: - service: webservice - file: ./MAQSService/docker-compose.yml + service: mongo \ No newline at end of file diff --git a/maqs-nosql/config.xml b/maqs-nosql/config.xml new file mode 100644 index 000000000..54da3ca88 --- /dev/null +++ b/maqs-nosql/config.xml @@ -0,0 +1,41 @@ + + + + + + 100 + + + 10000 + + + YES + + + VERBOSE + + + TXT + + + ./target/logs + + + mongodb://localhost:27017 + MongoDatabaseTest + MongoTestCollection + 30 + + diff --git a/maqs-nosql/pom.xml b/maqs-nosql/pom.xml new file mode 100644 index 000000000..4eb7307ca --- /dev/null +++ b/maqs-nosql/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + + + com.cognizantsoftvision.maqs + maqs-java + ${revision} + + + com.cognizantsoftvision.maqs.noSQL + maqs-noSQL + MAQS NoSQL Testing Module + ${revision} + + + 4.5.0 + + + + + com.cognizantsoftvision.maqs.base + maqs-base + ${project.version} + + + com.cognizantsoftvision.maqs.utilities + maqs-utilities + ${project.version} + compile + + + org.mongodb + mongodb-driver-sync + ${mongoDB.version} + + + org.testng + testng + + + diff --git a/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/BaseMongoTest.java b/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/BaseMongoTest.java new file mode 100644 index 000000000..50ea6fc89 --- /dev/null +++ b/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/BaseMongoTest.java @@ -0,0 +1,106 @@ +/* + * Copyright 2022 (C) Cognizant SoftVision, All rights Reserved + */ + +package com.cognizantsoftvision.maqs.nosql; + +import com.cognizantsoftvision.maqs.base.BaseExtendableTest; +import com.mongodb.client.MongoCollection; +import java.util.function.Supplier; +import org.bson.Document; +import org.testng.ITestResult; + +/** + * The Base Mongo Test class. + */ +public class BaseMongoTest extends BaseExtendableTest { + + /** + * Initializes a new instance of the BaseMongoTest class, + * Set up the database client for each test class. + */ + public BaseMongoTest() { + // Currently, not populated with any logic + } + + /** + * Gets the mongoDB driver. + * @return the mongoDB driver + */ + public MongoDBDriver getMongoDBDriver() { + return this.getTestObject().getMongoDBDriver(); + } + + /** + * Sets the MongoDB driver. + * @param driver the MongoDB driver to be set. + */ + public void setMongoDBDriver(MongoDBDriver driver) { + this.getTestObject().overrideMongoDBDriver(driver); + } + + /** + * Override the Mongo driver - does not lazy load. + * @param driver New Mongo driver + */ + public void overrideConnectionDriver(MongoDBDriver driver) { + this.getTestObject().overrideMongoDBDriver(driver); + } + + /** + * Override the Mongo driver - respects lazy loading. + * @param overrideCollectionConnection The collection function + */ + public void overrideConnectionDriver(Supplier> overrideCollectionConnection) { + this.getTestObject().overrideMongoDBDriver(overrideCollectionConnection); + } + + /** + * Override the Mongo driver - respects lazy loading. + * @param connectionString Client connection string + * @param databaseString Database connection string + * @param collectionString Mongo collection string + */ + public void overrideConnectionDriver(String connectionString, String databaseString, String collectionString) { + this.getTestObject().overrideMongoDBDriver(connectionString, databaseString, collectionString); + } + + /** + * Get the base web service url. + * @return The base web service url + */ + protected String getBaseConnectionString() { + return MongoDBConfig.getConnectionString(); + } + + /** + * Get the base web service url. + * @return The base web service url + */ + protected String getBaseDatabaseString() { + return MongoDBConfig.getDatabaseString(); + } + + /** + * Get the base web service url. + * @return The base web service url + */ + protected String getBaseCollectionString() { + return MongoDBConfig.getCollectionString(); + } + + @Override + protected void createNewTestObject() { + this.setTestObject(new MongoTestObject(this.getBaseConnectionString(), this.getBaseDatabaseString(), + this.getBaseCollectionString(), this.createLogger(), this.getFullyQualifiedTestClassName())); + } + + /** + * Steps to take before logging teardown results. + * @param resultType The test result + */ + @Override + protected void beforeLoggingTeardown(ITestResult resultType) { + // Currently, not populated with any logic + } +} diff --git a/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/IMongoTestObject.java b/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/IMongoTestObject.java new file mode 100644 index 000000000..50f45ea32 --- /dev/null +++ b/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/IMongoTestObject.java @@ -0,0 +1,48 @@ +/* + * Copyright 2022 (C) Cognizant SoftVision, All rights Reserved + */ + +package com.cognizantsoftvision.maqs.nosql; + +import com.cognizantsoftvision.maqs.base.ITestObject; +import com.mongodb.client.MongoCollection; +import java.util.function.Supplier; +import org.bson.Document; + +/** + * The Mongo Test Object interface. + */ +public interface IMongoTestObject extends ITestObject { + + /** + * Gets the Mongo driver. + * @return the mongo database driver + */ + MongoDBDriver getMongoDBDriver(); + + /** + * Gets the Mongo driver manager. + * @return the mongo database manager + */ + MongoDriverManager getMongoDBManager(); + + /** + * Override the Mongo driver a collection function. + * @param overrideCollectionConnection The collection function + */ + void overrideMongoDBDriver(Supplier> overrideCollectionConnection); + + /** + * Override the Mongo driver settings. + * @param driver New Mongo driver + */ + void overrideMongoDBDriver(MongoDBDriver driver); + + /** + * Override the Mongo driver settings. + * @param connectionString Client connection string + * @param databaseString Database connection string + * @param collectionString Mongo collection string + */ + void overrideMongoDBDriver(String connectionString, String databaseString, String collectionString); +} diff --git a/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/MongoDBConfig.java b/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/MongoDBConfig.java new file mode 100644 index 000000000..5aa790599 --- /dev/null +++ b/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/MongoDBConfig.java @@ -0,0 +1,55 @@ +/* + * Copyright 2022 (C) Cognizant SoftVision, All rights Reserved + */ + +package com.cognizantsoftvision.maqs.nosql; + +import com.cognizantsoftvision.maqs.utilities.helper.Config; +import com.cognizantsoftvision.maqs.utilities.helper.ConfigSection; + +/** + * The MongoDB Config class. + */ +public class MongoDBConfig { + + private MongoDBConfig() { + } + + /** + * The MongoDB configuration section. + */ + private static final ConfigSection MONGO_SECTION = ConfigSection.MONGO_MAQS; + + /** + * Get the client connection string. + * @return The connection type + */ + public static String getConnectionString() { + return Config.getValueForSection(MONGO_SECTION, "MongoConnectionString"); + } + + /** + * Get the database connection string. + * @return The database name + */ + public static String getDatabaseString() { + return Config.getValueForSection(MONGO_SECTION, "MongoDatabase"); + } + + /** + * Get the mongo collection string. + * @return The mongo collection string + */ + public static String getCollectionString() { + return Config.getValueForSection(MONGO_SECTION, "MongoCollection"); + } + + /** + * Get the database timeout in seconds. + * @return The timeout in seconds from the config file or default + * of 30 seconds when no config.xml key is found + */ + public static int getQueryTimeout() { + return Integer.parseInt(Config.getValueForSection(MONGO_SECTION, "MongoTimeout", "30")); + } +} diff --git a/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/MongoDBDriver.java b/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/MongoDBDriver.java new file mode 100644 index 000000000..b68677a51 --- /dev/null +++ b/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/MongoDBDriver.java @@ -0,0 +1,182 @@ +/* + * Copyright 2022 (C) Cognizant SoftVision, All rights Reserved + */ + +package com.cognizantsoftvision.maqs.nosql; + +import com.mongodb.MongoClientSettings; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import java.util.ArrayList; +import java.util.List; +import org.bson.Document; + +/** + * The MongoDB Driver class. + * Wraps the MongoCollection and related helper functions. + */ +public class MongoDBDriver implements AutoCloseable { + + /** + * Initializes a new instance of the MongoDBDriver class. + * @param collection The collection object + */ + public MongoDBDriver(MongoCollection collection) { + setCollection(collection); + } + + /** + * Initializes a new instance of the MongoDBDriver class. + * @param connectionString Server address + * @param databaseString Name of the database + * @param collectionString Name of the collection + */ + public MongoDBDriver(String connectionString, String databaseString, String collectionString) { + setMongoClient(connectionString); + setDatabase(this.getMongoClient(), databaseString); + setCollection(this.getDatabase(), collectionString); + } + + /** + * Initializes a new instance of the MongoDBDriver class. + * @param collectionString Name of the collection + */ + public MongoDBDriver(String collectionString) { + setCollection(MongoFactory.getCollection(MongoDBConfig.getConnectionString(), + MongoDBConfig.getDatabaseString(), collectionString)); + } + + /** + * Initializes a new instance of the MongoDBDriver class. + * @param clientSettings the mongo client settings + * @param collectionString Name of the collection + */ + public MongoDBDriver(MongoClientSettings clientSettings, String databaseString, String collectionString) { + this.setMongoClient(clientSettings); + setCollection(this.getMongoClient().getDatabase(databaseString).getCollection(collectionString)); + } + + /** + * Initializes a new instance of the MongoDBDriver class. + */ + public MongoDBDriver() { + setCollection(MongoFactory.getDefaultCollection()); + } + + /** + * The Mongo DB client. + */ + private MongoClient client; + + /** + * Gets the client object. + * @return the mongo client + */ + public MongoClient getMongoClient() { + return this.client; + } + + /** + * Sets the client object. + * @param connectionString the new mongo Client to be set. + */ + public void setMongoClient(String connectionString) { + this.client = MongoClients.create(connectionString); + } + + public void setMongoClient(MongoClientSettings mongoClientSettings) { + this.client = MongoClients.create(mongoClientSettings); + } + + /** + * The MongoDB Database. + */ + private MongoDatabase database; + + /** + * Gets the database object. + * @return the MongoDB database object + */ + public MongoDatabase getDatabase() { + return this.database; + } + + /** + * Sets the database object. + * @param mongoDatabase the name of the mongo database + */ + public void setDatabase(String mongoDatabase) { + this.database = this.getMongoClient().getDatabase(mongoDatabase); + } + + /** + * Sets the database object. + * @param mongoClient the mongo DB client of the database + * @param mongoDatabase the name of the mongo database + */ + public void setDatabase(MongoClient mongoClient, String mongoDatabase) { + this.client = mongoClient; + setDatabase(mongoDatabase); + } + + /** + * The MongoDB collection. + */ + private MongoCollection collection; + + /** + * Gets the collection object. + * @return a mongo collection + */ + public MongoCollection getCollection() { + return collection; + } + + /** + * Sets the Mongo Collection object. + * @param newCollection the collection to be set + */ + private void setCollection(MongoCollection newCollection) { + this.collection = newCollection; + } + + /** + * Sets the Mongo Collection object. + * @param database the mongo DB database of the collection + * @param collection the string value of the collection + */ + private void setCollection(MongoDatabase database, String collection) { + this.collection = database.getCollection(collection); + } + + /** + * List all the items in the collection. + * @return List of the items in the collection + */ + public List listAllCollectionItems() { + return this.getCollection().find().into(new ArrayList<>()); + } + + /** + * Checks if the collection contains any records. + * @return True if the collection is empty, false otherwise + */ + public boolean isCollectionEmpty() { + return this.getCollection().countDocuments() == 0; + } + + /** + * Counts all the items in the collection. + * @return Number of items in the collection + */ + public int countAllItemsInCollection() { + return (int) this.getCollection().countDocuments(); + } + + @Override + public void close() { + this.getMongoClient().close(); + } +} diff --git a/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/MongoDriverManager.java b/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/MongoDriverManager.java new file mode 100644 index 000000000..390a23207 --- /dev/null +++ b/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/MongoDriverManager.java @@ -0,0 +1,108 @@ +/* + * Copyright 2022 (C) Cognizant SoftVision, All rights Reserved + */ + +package com.cognizantsoftvision.maqs.nosql; + +import com.cognizantsoftvision.maqs.base.BaseTestObject; +import com.cognizantsoftvision.maqs.base.DriverManager; +import com.cognizantsoftvision.maqs.utilities.logging.LoggingConfig; +import com.cognizantsoftvision.maqs.utilities.logging.LoggingEnabled; +import com.cognizantsoftvision.maqs.utilities.logging.MessageType; +import com.mongodb.client.MongoCollection; +import java.util.function.Supplier; +import org.bson.Document; + +/** + * The Mongo Driver Manager class. + */ +public class MongoDriverManager extends DriverManager { + + /** + * Cached copy of the connection driver. + */ + private MongoDBDriver driver; + + /** + * Initializes a new instance of the MongoDriverManager class. + * @param connectionString Client connection string + * @param databaseString Database connection string + * @param collectionString Mongo collection string + * @param testObject Test object this driver is getting added to + */ + public MongoDriverManager(String connectionString, String databaseString, + String collectionString, BaseTestObject testObject) { + super(() -> new MongoDBDriver( + MongoFactory.getCollection(connectionString, databaseString, collectionString)), testObject); + } + + /** + * Initializes a new instance of the MongoDriverManager class. + * @param getCollection Function for getting a Mongo collection connection + * @param testObject Test object this driver is getting added to + */ + public MongoDriverManager(Supplier getCollection, BaseTestObject testObject) { + super(getCollection, testObject); + } + + /** + * Override the Mongo driver. + * @param overrideDriver The new Mongo driver + */ + public void overrideDriver(MongoDBDriver overrideDriver) { + driver = overrideDriver; + this.setBaseDriver(new MongoDBDriver(overrideDriver.getCollection())); + } + + /** + * Override the Mongo driver - respects lazy loading. + * @param connectionString Connection string of mongo DB + * @param databaseString Database string to use + * @param collectionString Collection string to use + */ + public void overrideDriver(String connectionString, String databaseString, String collectionString) { + driver = null; + this.overrideDriverGet(() -> MongoFactory.getCollection(connectionString, databaseString, collectionString)); + } + + /** + * Override the Mongo driver - respects lazy loading. + * @param overrideCollectionConnection The new collection connection + */ + public void overrideDriver(Supplier> overrideCollectionConnection) { + driver = null; + this.overrideDriverGet(overrideCollectionConnection); + } + + /** + * Get the Mongo driver. + * @return The Mongo driver + */ + public MongoDBDriver getMongoDriver() { + // Create default Web Service Driver if null. + if (this.driver == null) { + MongoCollection temp = getBase().getCollection(); + + if (LoggingConfig.getLoggingEnabledSetting() == LoggingEnabled.NO) { + this.getLogger().logMessage(MessageType.INFORMATION, "Getting Mongo driver"); + } + + this.driver = new MongoDBDriver(temp); + } + return this.driver; + } + + protected void overrideDriverGet(Supplier> driverGet) { + this.getMongoDriver(); + } + + @Override + public void close() { + if (!this.isDriverInitialized()) { + return; + } + + driver.getMongoClient().close(); + this.baseDriver = null; + } +} diff --git a/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/MongoFactory.java b/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/MongoFactory.java new file mode 100644 index 000000000..ae34995fd --- /dev/null +++ b/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/MongoFactory.java @@ -0,0 +1,53 @@ +/* + * Copyright 2022 (C) Cognizant SoftVision, All rights Reserved + */ + +package com.cognizantsoftvision.maqs.nosql; + +import static com.mongodb.client.MongoClients.create; + +import com.mongodb.MongoClientException; +import com.mongodb.MongoClientSettings; +import com.mongodb.MongoException; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import org.bson.Document; + +/** + * The Mongo Factory Class. + */ +public class MongoFactory { + + private MongoFactory() { + } + + /** + * Get the email client using connection information from the test run configuration. + * @return The email connection + */ + public static MongoCollection getDefaultCollection() { + return getCollection(MongoDBConfig.getConnectionString(), + MongoDBConfig.getDatabaseString(), MongoDBConfig.getCollectionString()); + } + + /** + * Get the email client using connection information from the test run configuration. + * @param connectionString the connection string + * @param databaseString the database string + * @param collectionString the collection string + * @return The email connection + */ + public static MongoCollection getCollection( + String connectionString, String databaseString, String collectionString) { + try (MongoClient mongoClient = create(connectionString)) { + MongoDatabase database = mongoClient.getDatabase(databaseString); + return database.getCollection(collectionString); + } catch (MongoClientException e) { + throw new MongoClientException("connection was not created"); + } catch (MongoException e) { + throw new MongoException("database does not exist"); + } + } +} diff --git a/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/MongoTestObject.java b/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/MongoTestObject.java new file mode 100644 index 000000000..be1c0e454 --- /dev/null +++ b/maqs-nosql/src/main/java/com/cognizantsoftvision/maqs/nosql/MongoTestObject.java @@ -0,0 +1,69 @@ +/* + * Copyright 2022 (C) Cognizant SoftVision, All rights Reserved + */ + +package com.cognizantsoftvision.maqs.nosql; + +import com.cognizantsoftvision.maqs.base.BaseTestObject; +import com.cognizantsoftvision.maqs.utilities.logging.Logger; +import com.mongodb.client.MongoCollection; +import java.util.function.Supplier; +import org.bson.Document; + +/** + * The Mongo Test Object class. + */ +public class MongoTestObject extends BaseTestObject implements IMongoTestObject { + + /** + * Initializes a new instance of the MongoTestObject class. + * @param connectionString Client connection string + * @param databaseString Database connection string + * @param collectionString Mongo collection string + * @param logger The test's logger + * @param fullyQualifiedTestName The test's fully qualified test name + */ + public MongoTestObject(String connectionString, String databaseString, String collectionString, + Logger logger, String fullyQualifiedTestName) { + super(logger, fullyQualifiedTestName); + this.getManagerStore().put((MongoDriverManager.class).getCanonicalName(), + new MongoDriverManager(connectionString,databaseString,collectionString, this)); + } + + /** + * {@inheritDoc} + */ + public MongoDriverManager getMongoDBManager() { + return this.getManagerStore().getDriver(MongoDriverManager.class.getCanonicalName()); + } + + /** + * {@inheritDoc} + */ + public MongoDBDriver getMongoDBDriver() { + return this.getMongoDBManager().getMongoDriver(); + } + + /** + * {@inheritDoc} + */ + public void overrideMongoDBDriver(String connectionString, String databaseString, String collectionString) { + this.getMongoDBManager().overrideDriver(connectionString, databaseString, collectionString); + } + + /** + * {@inheritDoc} + */ + public void overrideMongoDBDriver(MongoDBDriver driver) { + this.getManagerStore().put(MongoDriverManager.class.getCanonicalName(), + new MongoDriverManager(() -> driver, this)); + } + + /** + * {@inheritDoc} + */ + @Override + public void overrideMongoDBDriver(Supplier> overrideCollectionConnection) { + this.getMongoDBManager().overrideDriver(overrideCollectionConnection); + } +} diff --git a/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/BaseMongoUnitTest.java b/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/BaseMongoUnitTest.java new file mode 100644 index 000000000..c6962205a --- /dev/null +++ b/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/BaseMongoUnitTest.java @@ -0,0 +1,81 @@ +/* + * Copyright 2022 (C) Cognizant SoftVision, All rights Reserved + */ + +package com.cognizantsoftvision.maqs.nosql; + +import com.cognizantsoftvision.maqs.utilities.helper.TestCategories; +import org.testng.Assert; +import org.testng.annotations.Test; + +/** + * The Base Mongo unit test class. + */ +public class BaseMongoUnitTest extends BaseMongoTest { + + /** + * Test the get mongo db driver. + */ + @Test(groups = TestCategories.MONGO) + public void testGetMongoDBDriver() { + this.setMongoDBDriver(new MongoDBDriver( + MongoDBConfig.getConnectionString(), MongoDBConfig.getDatabaseString(), MongoDBConfig.getCollectionString())); + MongoDBDriver mongoDBDriver = this.getMongoDBDriver(); + Assert.assertNotNull(mongoDBDriver, + "Checking that MongoDB Driver is not null through BaseMongoTest"); + } + + /** + * Test the set mongo db driver. + */ + @Test(groups = TestCategories.MONGO) + public void testSetMongoDBDriver() { + int hashCode = this.getMongoDBDriver().hashCode(); + try { + this.setMongoDBDriver(new MongoDBDriver()); + } catch (Exception e) { + e.printStackTrace(); + } + int hashCode1 = this.getMongoDBDriver().hashCode(); + Assert.assertNotEquals(hashCode, hashCode1); + } + + /** + * Test the override connection driver with the mongo db driver. + */ + @Test(groups = TestCategories.MONGO) + public void testOverrideConnectionDriverWithMongoDBDriver() { + overrideConnectionDriver(this.getMongoDBDriver()); + Assert.assertNotNull(getMongoDBDriver()); + overrideConnectionDriver(this.getBaseConnectionString(), + this.getBaseDatabaseString(), this.getBaseCollectionString()); + Assert.assertNotNull(getMongoDBDriver()); + } + + /** + * Test getting the connection string. + */ + @Test(groups = TestCategories.MONGO) + public void testGetBaseMongoConnectionStringTest() { + String connection = this.getBaseConnectionString(); + Assert.assertEquals(connection, "mongodb://localhost:27017", "connection strings do not match"); + } + + /** + * Test getting the database string. + */ + @Test(groups = TestCategories.MONGO) + public void testGetBaseMongoStringTest() { + String databaseString = this.getBaseDatabaseString(); + Assert.assertEquals(databaseString, "MongoDatabaseTest", "database string do not match"); + } + + /** + * Test getting the connection string. + */ + @Test(groups = TestCategories.MONGO) + public void testGetBaseMongoCollectionStringTest() { + String collection = this.getBaseCollectionString(); + Assert.assertEquals(collection, "MongoTestCollection", "collection strings do not match"); + } +} diff --git a/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/MongoDBConfigUnitTest.java b/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/MongoDBConfigUnitTest.java new file mode 100644 index 000000000..d5fe4fa84 --- /dev/null +++ b/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/MongoDBConfigUnitTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2022 (C) Cognizant SoftVision, All rights Reserved + */ + +package com.cognizantsoftvision.maqs.nosql; + +import com.cognizantsoftvision.maqs.utilities.helper.TestCategories; +import org.testng.Assert; +import org.testng.annotations.Test; + +/** + * The Mongo Database Config unit test class. + */ +public class MongoDBConfigUnitTest { + + /** + * Test getting the connection string. + */ + @Test(groups = TestCategories.MONGO) + public void testGetMongoDBConnectionStringTest() { + String connection = MongoDBConfig.getConnectionString(); + Assert.assertEquals(connection, "mongodb://localhost:27017", "connection strings do not match"); + } + + /** + * Test getting the database string. + */ + @Test(groups = TestCategories.MONGO) + public void testGetMongoDBDatabaseStringTest() { + String databaseString = MongoDBConfig.getDatabaseString(); + Assert.assertEquals(databaseString, "MongoDatabaseTest", "database string do not match"); + } + + /** + * Test getting the connection string. + */ + @Test(groups = TestCategories.MONGO) + public void testGetMongoDBCollectionStringTest() { + String collection = MongoDBConfig.getCollectionString(); + Assert.assertEquals(collection, "MongoTestCollection", "collection strings do not match"); + } + + /** + * Test getting the timeout value. + */ + @Test(groups = TestCategories.MONGO) + public void testGetMongoDBQueryTimeout() { + int databaseTimeout = MongoDBConfig.getQueryTimeout(); + Assert.assertEquals(databaseTimeout, 30, "Timeout is incorrect"); + } +} diff --git a/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/MongoDBDriverUnitTest.java b/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/MongoDBDriverUnitTest.java new file mode 100644 index 000000000..86acfc16c --- /dev/null +++ b/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/MongoDBDriverUnitTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2022 (C) Cognizant SoftVision, All rights Reserved + */ + +package com.cognizantsoftvision.maqs.nosql; + +import com.cognizantsoftvision.maqs.utilities.helper.TestCategories; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoCollection; +import org.bson.Document; +import org.testng.Assert; +import org.testng.annotations.Test; +import java.util.List; + +/** + * The Mongo Database Driver unit test class. + */ +public class MongoDBDriverUnitTest extends BaseMongoTest { + + /** + * Test setting up the mongo db driver. + */ + @Test(groups = TestCategories.MONGO) + public void testMongoDBDriver() { + MongoCollection collection = MongoFactory.getDefaultCollection(); + MongoDBDriver driver = new MongoDBDriver(collection); + Assert.assertNotNull(driver); + + driver = new MongoDBDriver(MongoDBConfig.getConnectionString(), + MongoDBConfig.getDatabaseString(), MongoDBConfig.getConnectionString()); + Assert.assertNotNull(driver); + + driver = new MongoDBDriver(MongoDBConfig.getCollectionString()); + Assert.assertNotNull(driver); + } + + /** + * Test getting the mongo client. + */ + @Test(groups = TestCategories.MONGO) + public void testGetMongoClient() { + this.setMongoDBDriver(new MongoDBDriver()); + MongoClient client = this.getMongoDBDriver().getMongoClient(); + Assert.assertNotNull(client); + } + + /** + * Test setting the mongo client. + */ + @Test(groups = TestCategories.MONGO) + public void testSetMongoClient() { + this.setMongoDBDriver(new MongoDBDriver()); + this.getMongoDBDriver().setMongoClient(MongoDBConfig.getConnectionString()); + Assert.assertNotNull(this.getMongoDBDriver().getMongoClient()); + } + + /** + * Test the list all collection items helper function. + */ + @Test(groups = TestCategories.MONGO) + public void testListAllCollectionItems() { + this.setMongoDBDriver(new MongoDBDriver()); + List collectionItems = this.getMongoDBDriver().listAllCollectionItems(); + for (Document bson : collectionItems){ + Assert.assertTrue(bson.containsKey("lid")); + } + + Assert.assertEquals(collectionItems.size(), 4); + } + + @Test(groups = TestCategories.MONGO) + public void testIsCollectionEmpty() { + boolean collection = this.getMongoDBDriver().isCollectionEmpty(); + Assert.assertTrue(collection); + } + + /** + * Test the count all collection items helper function + */ + @Test(groups = TestCategories.MONGO) + public void testCountAllItemsInCollection() { + Assert.assertEquals(this.getMongoDBDriver().countAllItemsInCollection(), 4); + } +} diff --git a/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/MongoDBFunctionalUnitTest.java b/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/MongoDBFunctionalUnitTest.java new file mode 100644 index 000000000..5e784b07f --- /dev/null +++ b/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/MongoDBFunctionalUnitTest.java @@ -0,0 +1,78 @@ +/* + * Copyright 2022 (C) Cognizant SoftVision, All rights Reserved + */ + +package com.cognizantsoftvision.maqs.nosql; + +import com.cognizantsoftvision.maqs.utilities.helper.TestCategories; +import com.mongodb.client.model.Filters; +import org.bson.Document; +import org.bson.conversions.Bson; +import org.testng.Assert; +import org.testng.annotations.Test; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * The Mongo Database Functional unit test class. + */ +public class MongoDBFunctionalUnitTest extends BaseMongoTest { + + /** + * Test the collection works as expected when getting the login id. + */ + @Test(groups = TestCategories.MONGO) + public void testMongoGetLoginID() { + Bson filter = Filters.eq("lid", "test3"); + //String value = this.getMongoDBDriver().getCollection().find(filter).ToList()[0]["lid"].ToString(); + List value = this.getMongoDBDriver().getCollection().find(filter).into(new ArrayList<>()); + Assert.assertEquals(value.get(0).toString(), "test3"); + } + + /** + * Test the collection works as expected when running a query and returning the first result. + */ + @Test(groups = TestCategories.MONGO) + public void testMongoQueryAndReturnFirst() { + Bson filter = Filters.eq("lid", "test3"); + // MongoCollection document = this.getMongoDBDriver().getCollection().find(filter).ToList().First(); + Document document = this.getMongoDBDriver().getCollection().find(filter).first(); + Assert.assertEquals(Objects.requireNonNull(document).get("lid").toString(), "test3"); + } + + /** + * Test the collection works as expected when finding a list with a key. + */ + @Test(groups = TestCategories.MONGO) + public void testMongoFindListWithKey() { + //var filter = Builders.Filter.Exists("lid"); + Bson filter = Filters.exists("lid"); + List documentList = this.getMongoDBDriver().getCollection().find(filter).into(new ArrayList<>()); + + for (Document documents : documentList) { + Assert.assertNotEquals(documents.get("lid").toString(), ""); + } + Assert.assertEquals(documentList.size(), 4); + } + + /** + * Test the collection works as expected. + */ + @Test(groups = TestCategories.MONGO) + public void testMongoLinqQuery() { + /* + QueryBuilder queries = + from e in this.getMongoDBDriver().getCollection() + where e["lid"] == "test1" + select e; + */ + + Bson filter = Filters.eq("lid", "test1"); + List retList = this.getMongoDBDriver().getCollection().find(filter).into(new ArrayList<>()); + + for (Document value : retList) { + Assert.assertEquals(value.get("lid"), "test1"); + } + } +} diff --git a/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/MongoDriverManagerUnitTest.java b/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/MongoDriverManagerUnitTest.java new file mode 100644 index 000000000..9bcdfd457 --- /dev/null +++ b/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/MongoDriverManagerUnitTest.java @@ -0,0 +1,123 @@ +/* + * Copyright 2022 (C) Cognizant SoftVision, All rights Reserved + */ + +package com.cognizantsoftvision.maqs.nosql; + +import com.cognizantsoftvision.maqs.utilities.helper.TestCategories; +import com.mongodb.client.MongoCollection; +import org.bson.Document; +import org.testng.Assert; +import org.testng.annotations.Test; + +/** + * The Mongo Driver Manager unit test class. + */ +public class MongoDriverManagerUnitTest extends BaseMongoTest{ + + /** + * Test overriding the default driver. + */ + @Test(groups = TestCategories.MONGO) + public void respectDefaultDriverOverride() { + MongoDBDriver mongoDriver = new MongoDBDriver(); + this.getTestObject().getMongoDBManager().overrideDriver(mongoDriver); + Assert.assertEquals(mongoDriver.getCollection(), this.getMongoDBDriver().getCollection()); + Assert.assertEquals(mongoDriver.getDatabase(), this.getMongoDBDriver().getDatabase()); + Assert.assertEquals(mongoDriver.getMongoClient(), this.getMongoDBDriver().getMongoClient()); + } + + /** + * Override driver with the collection string. + */ + @Test(groups = TestCategories.MONGO) + public void respectCollectionDriverOverride() { + // MongoDBConfig.GetConnectionString(), MongoDBConfig.GetDatabaseString(), collectionString + MongoDBDriver mongoDriver = new MongoDBDriver(MongoDBConfig.getCollectionString()); + this.getTestObject().getMongoDBManager().overrideDriver(mongoDriver); + Assert.assertEquals(mongoDriver.getCollection(), this.getMongoDBDriver().getCollection()); + } + + /** + * Override drive with all 3 connection parameters. + */ + @Test(groups = TestCategories.MONGO) + public void respectDriverConnectionsOverride() { + MongoDBDriver mongoDriver = new MongoDBDriver(MongoDBConfig.getConnectionString(), + MongoDBConfig.getDatabaseString(), MongoDBConfig.getCollectionString()); + this.getTestObject().getMongoDBManager().overrideDriver(mongoDriver); + Assert.assertEquals(mongoDriver.getCollection(), this.getMongoDBDriver().getCollection()); + } + + /** + * Override driver directly. + */ + @Test(groups = TestCategories.MONGO) + public void respectDirectDriverOverride() { + MongoDBDriver mongoDriver = new MongoDBDriver(MongoDBConfig.getCollectionString()); + this.setMongoDBDriver(mongoDriver); + Assert.assertEquals(mongoDriver.getCollection(), this.getMongoDBDriver().getCollection()); + } + + /** + * Override driver with new driver. + */ + @Test(groups = TestCategories.MONGO) + public void respectNewDriverOverride() { + MongoDBDriver mongoDriver = new MongoDBDriver(MongoDBConfig.getCollectionString()); + this.getTestObject().overrideMongoDBDriver(mongoDriver); + Assert.assertEquals(mongoDriver.getCollection(), this.getMongoDBDriver().getCollection()); + } + + /** + * Override drive with collection function. + */ + @Test(groups = TestCategories.MONGO) + public void respectCollectionOverride() { + MongoCollection collection = MongoFactory.getDefaultCollection(); + this.getTestObject().overrideMongoDBDriver(() -> collection); + Assert.assertEquals(collection, this.getMongoDBDriver().getCollection()); + } + + /** + * Override drive with all 3 connection strings. + */ + @Test(groups = TestCategories.MONGO) + public void respectDriverConnectionStingsOverride() { + MongoCollection collection = this.getMongoDBDriver().getCollection(); + this.getTestObject().overrideMongoDBDriver(MongoDBConfig.getConnectionString(), + MongoDBConfig.getDatabaseString(), MongoDBConfig.getCollectionString()); + Assert.assertNotEquals(collection, this.getMongoDBDriver().getCollection()); + } + + /** + * Override in base with collection function. + */ + @Test(groups = TestCategories.MONGO) + public void respectCollectionOverrideInBase() { + MongoCollection collection = MongoFactory.getDefaultCollection(); + this.overrideConnectionDriver(() -> collection); + Assert.assertEquals(collection, this.getMongoDBDriver().getCollection()); + } + + /** + * Override in base with new driver. + */ + @Test(groups = TestCategories.MONGO) + public void RespectDriverOverrideInBase() { + MongoCollection collection = MongoFactory.getDefaultCollection(); + this.overrideConnectionDriver(new MongoDBDriver(collection)); + Assert.assertEquals(collection, this.getMongoDBDriver().getCollection()); + } + + /** + * Override drive with strings in base. + */ + @Test(groups = TestCategories.MONGO) + public void RespectConnectionStingsOverrideInBase() { + MongoCollection collection = this.getMongoDBDriver().getCollection(); + this.overrideConnectionDriver(MongoDBConfig.getConnectionString(), + MongoDBConfig.getDatabaseString(), MongoDBConfig.getCollectionString()); + Assert.assertNotEquals(collection, this.getMongoDBDriver().getCollection()); + } +} diff --git a/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/MongoTestObjectUnitTest.java b/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/MongoTestObjectUnitTest.java new file mode 100644 index 000000000..ab5f2132d --- /dev/null +++ b/maqs-nosql/src/test/java/com/cognizantsoftvision/maqs/nosql/MongoTestObjectUnitTest.java @@ -0,0 +1,90 @@ +/* + * Copyright 2022 (C) Cognizant SoftVision, All rights Reserved + */ + +package com.cognizantsoftvision.maqs.nosql; + +import com.cognizantsoftvision.maqs.utilities.helper.TestCategories; +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings; +import com.mongodb.client.MongoCollection; +import org.bson.Document; +import org.testng.Assert; +import org.testng.annotations.Test; + +/** + * The Mongo Test Object unit test class. + */ +public class MongoTestObjectUnitTest extends BaseMongoTest { + + /** + * Tests if the collection override is respected. + */ + @Test(groups = TestCategories.MONGO) + public void overrideCollectionFunction() { + MongoCollection collection = this.getMongoDBDriver().getCollection(); + MongoCollection newCollection = MongoFactory.getDefaultCollection(); + + this.getTestObject().overrideMongoDBDriver(() -> newCollection); + Assert.assertNotEquals(collection, this.getMongoDBDriver().getCollection()); + Assert.assertEquals(newCollection, this.getMongoDBDriver().getCollection()); + Assert.assertFalse(this.getMongoDBDriver().isCollectionEmpty()); + } + + /** + * Tests if the connection string overrides respected. + */ + @Test(groups = TestCategories.MONGO) + public void overrideConnectionStrings() { + MongoCollection collection = this.getMongoDBDriver().getCollection(); + this.getTestObject().overrideMongoDBDriver(MongoDBConfig.getConnectionString(), + MongoDBConfig.getDatabaseString(), MongoDBConfig.getCollectionString()); + + Assert.assertNotEquals(collection, this.getMongoDBDriver().getCollection()); + Assert.assertFalse(this.getMongoDBDriver().isCollectionEmpty()); + } + + /** + * Tests if the driver override respected. + */ + @Test(groups = TestCategories.MONGO) + public void overrideDriver() { + MongoDBDriver firstDriver = this.getMongoDBDriver(); + MongoDBDriver newDriver = new MongoDBDriver(MongoFactory.getDefaultCollection()); + this.getTestObject().overrideMongoDBDriver(newDriver); + + Assert.assertNotEquals(firstDriver, this.getMongoDBDriver()); + Assert.assertEquals(newDriver, this.getMongoDBDriver()); + Assert.assertFalse(this.getMongoDBDriver().isCollectionEmpty()); + } + + /** + * Tests if the custom driver is overridable. + */ + @Test(groups = TestCategories.MONGO) + public void overrideWithCustomDriver() { + MongoDBDriver firstDriver = this.getMongoDBDriver(); + + MongoClientSettings settings = MongoClientSettings.builder() + .applyConnectionString(new ConnectionString(MongoDBConfig.getConnectionString())) + .build(); + + MongoDBDriver newDriver = new MongoDBDriver(settings, + MongoDBConfig.getDatabaseString(), MongoDBConfig.getCollectionString()); + this.getTestObject().overrideMongoDBDriver(newDriver); + + Assert.assertNotEquals(firstDriver, this.getMongoDBDriver()); + Assert.assertEquals(newDriver, this.getMongoDBDriver()); + Assert.assertFalse(this.getMongoDBDriver().isCollectionEmpty()); + } + + /** + * Make sure the test objects map properly. + */ + @Test(groups = TestCategories.MONGO) + public void TestMongoDBTestObjectMapCorrectly() { + Assert.assertEquals(this.getTestObject().getLogger(), this.getLogger(), "Logs don't match"); + //Assert.assertEquals(this.getTestObject().getPerfTimerCollection(), this.getPerfTimerCollection(), "Perf Timer Collections don't match"); + Assert.assertEquals(this.getTestObject().getMongoDBDriver(), this.getMongoDBDriver(), "Web service driver don't match"); + } +} diff --git a/maqs-utilities/src/main/java/com/cognizantsoftvision/maqs/utilities/helper/ConfigSection.java b/maqs-utilities/src/main/java/com/cognizantsoftvision/maqs/utilities/helper/ConfigSection.java index c126b38ea..f7b022962 100644 --- a/maqs-utilities/src/main/java/com/cognizantsoftvision/maqs/utilities/helper/ConfigSection.java +++ b/maqs-utilities/src/main/java/com/cognizantsoftvision/maqs/utilities/helper/ConfigSection.java @@ -40,6 +40,11 @@ public enum ConfigSection { */ GLOBAL_MAQS("GlobalMaqs"), + /** + * The default mongoDB maqs section. + */ + MONGO_MAQS("MongoMaqs"), + /** * The default playwright maqs section. */ diff --git a/maqs-utilities/src/main/java/com/cognizantsoftvision/maqs/utilities/helper/TestCategories.java b/maqs-utilities/src/main/java/com/cognizantsoftvision/maqs/utilities/helper/TestCategories.java index 21ac714fa..dafe37b46 100644 --- a/maqs-utilities/src/main/java/com/cognizantsoftvision/maqs/utilities/helper/TestCategories.java +++ b/maqs-utilities/src/main/java/com/cognizantsoftvision/maqs/utilities/helper/TestCategories.java @@ -61,4 +61,9 @@ private TestCategories() { * String for web service unit test category. */ public static final String WEB_SERVICE = "Web Service Unit Tests"; + + /** + * String for MongoDB unit test category. + */ + public static final String MONGO = "MongoDB Unit Tests"; } diff --git a/pom.xml b/pom.xml index bd6bbd48e..35ec04535 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,7 @@ maqs-appium maqs-webservices maqs-database + maqs-nosql maqs-cucumber maqs-accessibility maqs-playwright @@ -331,6 +332,11 @@ maqs-webservices ${project.version} + + com.cognizantsoftvision.maqs.nosql + maqs-nosql + ${project.version} + com.cognizantsoftvision.maqs.playwright maqs-playwright