diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index a8288221..c38aa8ac 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -19,21 +19,6 @@ modules = [ {org = "ballerina", packageName = "crypto", moduleName = "crypto"} ] -[[package]] -org = "ballerina" -name = "file" -version = "1.9.0" -scope = "testOnly" -dependencies = [ - {org = "ballerina", name = "io"}, - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "os"}, - {org = "ballerina", name = "time"} -] -modules = [ - {org = "ballerina", packageName = "file", moduleName = "file"} -] - [[package]] org = "ballerina" name = "io" @@ -134,7 +119,6 @@ name = "mongodb" version = "4.3.0" dependencies = [ {org = "ballerina", name = "crypto"}, - {org = "ballerina", name = "file"}, {org = "ballerina", name = "jballerina.java"}, {org = "ballerina", name = "log"}, {org = "ballerina", name = "os"}, diff --git a/ballerina/build.gradle b/ballerina/build.gradle index 455e0d91..d8f9bf6d 100644 --- a/ballerina/build.gradle +++ b/ballerina/build.gradle @@ -89,10 +89,41 @@ task commitTomlFiles { } } +task startMongodbServer() { + doLast { + println("Starting mongodb server...") + exec { + if (Os.isFamily(Os.FAMILY_WINDOWS)) { + commandLine 'cmd', '/c', "docker-compose -f tests/resources/docker/docker-compose.yml up && exit %%ERRORLEVEL%%" + } else { + commandLine 'sh', '-c', "docker-compose -f tests/resources/docker/docker-compose.yml up -d" + } + } + } +} + +task stopMongodbServer() { + doLast { + println("Stopping mongodb server...") + exec { + if (Os.isFamily(Os.FAMILY_WINDOWS)) { + commandLine 'cmd', '/c', "docker-compose -f tests/resources/docker/docker-compose.yml down && exit %%ERRORLEVEL%%" + } else { + commandLine 'sh', '-c', "docker-compose -f tests/resources/docker/docker-compose.yml down" + } + } + } +} + clean { delete 'build' } build.dependsOn copyToLib build.dependsOn ":${packageName}-native:build" +build.dependsOn startMongodbServer +build.finalizedBy stopMongodbServer + test.dependsOn ":${packageName}-native:build" +test.dependsOn startMongodbServer +test.finalizedBy stopMongodbServer diff --git a/ballerina/tests/main_test.bal b/ballerina/tests/main_test.bal index 187ff7b4..9aa6a4a1 100644 --- a/ballerina/tests/main_test.bal +++ b/ballerina/tests/main_test.bal @@ -15,20 +15,19 @@ // under the License. import ballerina/log; -import ballerina/os; import ballerina/test; -string testHostName = os:getEnv("MONGODB_HOST") != "" ? os:getEnv("MONGODB_HOST") : "localhost"; -string testUser = os:getEnv("MONGODB_USER") != "" ? os:getEnv("MONGODB_USER") : "admin"; -string testPass = os:getEnv("MONGODB_PASSWORD") != "" ? os:getEnv("MONGODB_PASSWORD") : "admin"; +string testHostName = "localhost"; +string testUser = "admin"; +string testPass = "admin"; -const DATABASE_NAME = "moviecollection"; -const COLLECTION_NAME = "moviedetails"; +const DATABASE_NAME = "moviesDB"; +const COLLECTION_NAME = "movies"; ConnectionConfig mongoConfig = { connection: { host: testHostName, - port: 27011, + port: 27017, auth: { username: testUser, password: testPass @@ -43,7 +42,7 @@ ConnectionConfig mongoConfig = { ConnectionConfig mongoConfigError = { connection: { - url: "asdakjdk" + url: "invalidDB" }, databaseName: "MyDb" }; @@ -68,7 +67,6 @@ public function initializeInvalidClient() { groups: ["mongodb"] } public function testListDatabaseNames() returns error? { - log:printInfo("----------------- List Databases------------------"); string[] dbNames = check mongoClient->getDatabasesNames(); log:printInfo("Database Names: " + dbNames.toString()); } @@ -78,7 +76,6 @@ public function testListDatabaseNames() returns error? { groups: ["mongodb"] } public function testListCollections() returns error? { - log:printInfo("----------------- List Collections------------------"); string[] collectionNames = check mongoClient->getCollectionNames("admin"); log:printInfo("Collection Names: " + collectionNames.toString()); } @@ -88,7 +85,6 @@ public function testListCollections() returns error? { groups: ["mongodb"] } public function testInsertData() returns error? { - log:printInfo("------------------ Inserting Data ------------------"); map document1 = {name: "The Lion King", year: "2019", rating: 8}; map ducument2 = {name: "Black Panther", year: "2018", rating: 7}; @@ -104,7 +100,6 @@ public function testInsertData() returns error? { groups: ["mongodb"] } public function testInsertDataWithDbName() returns error? { - log:printInfo("------------------ Inserting Data With Second Database ------------------"); map document1 = {name: "The Lion King", year: "2019", rating: 8}; map document2 = {name: "Black Panther", year: "2018", rating: 7}; @@ -120,8 +115,6 @@ public function testInsertDataWithDbName() returns error? { groups: ["mongodb"] } public function testCountDocuments() returns error? { - log:printInfo("----------------- Count Documents------------------"); - int documentCount = check mongoClient->countDocuments(COLLECTION_NAME, (),()); log:printInfo("Documents counted successfully. Count: " + documentCount.toString()); test:assertEquals(2, documentCount); @@ -138,7 +131,6 @@ public function testCountDocuments() returns error? { groups: ["mongodb"] } public function testListIndices() returns error? { - log:printInfo("----------------- List Indices------------------"); stream returned = check mongoClient->listIndices(COLLECTION_NAME); check returned.forEach(function(Index data){ log:printInfo(data.ns); @@ -151,8 +143,6 @@ public function testListIndices() returns error? { groups: ["mongodb"] } public function testFindData() returns error? { - log:printInfo("----------------- Querying Data ----------------"); - map document1 = {name: "Joker", year: "2019", rating: 7}; map document2 = {name: "Black Panther", year: "2018", rating: 7}; @@ -190,8 +180,6 @@ public function testFindData() returns error? { groups: ["mongodb"] } public function testFindDataWithProjection() returns error? { - log:printInfo("----------------- Querying Data with Projection ----------------"); - map findDoc = {year: "2019"}; map projectionDoc = {name: 1, year: true}; stream result = check mongoClient->find(COLLECTION_NAME, projection = projectionDoc, @@ -225,7 +213,6 @@ public function testFindDataWithProjection() returns error? { groups: ["mongodb"] } function testFindDistinctValues() returns error? { - log:printInfo("----------------- Querying Distinct Values ----------------"); string[] distictiveValues = []; stream result = check mongoClient->'distinct(COLLECTION_NAME, "year"); check result.forEach(function(string data){ @@ -242,8 +229,6 @@ function testFindDistinctValues() returns error? { groups: ["mongodb"] } function testUpdateDocument() returns error? { - log:printInfo("------------------ Updating Data -------------------"); - map replaceFilter = {name: "The Lion King"}; map replaceDoc = { "$set": {name: "The Lion King", year: "2019", rating: 6}}; @@ -264,8 +249,6 @@ function testUpdateDocument() returns error? { groups: ["mongodb"] } function testUpdateDocumentUpsertTrue() returns error? { - log:printInfo("------------------ Updating Data (Upsert) -------------------"); - map replaceFilter = {name: "The Lion King 2"}; map replaceDoc = { "$set": {name: "The Lion King 2", year: "2019", rating: 7}}; @@ -279,8 +262,6 @@ function testUpdateDocumentUpsertTrue() returns error? { groups: ["mongodb"] } function testDelete() returns error? { - log:printInfo("------------------ Deleting Data -------------------"); - map deleteFilter = {"rating": 9}; int deleteDocCount = check mongoClient->delete(COLLECTION_NAME, (), deleteFilter, true); @@ -291,7 +272,6 @@ function testDelete() returns error? { log:printInfo("Deleted count: " + deleteDocCount.toString()); test:assertEquals(deleteDocCount, 2, msg = "Document deletion failed"); - log:printInfo("------------------ Deleting Data From Second Database-------------------"); deleteDocCount = check mongoClient->delete(COLLECTION_NAME, "anothermoviecollection", (), true); log:printInfo("Deleted count: " + deleteDocCount.toString()); test:assertEquals(deleteDocCount, 2, msg = "Document deletion failed"); diff --git a/ballerina/tests/resources/docker/certs/mongodb-CA.pem b/ballerina/tests/resources/docker/certs/mongodb-CA.pem new file mode 100644 index 00000000..d93b5e3e --- /dev/null +++ b/ballerina/tests/resources/docker/certs/mongodb-CA.pem @@ -0,0 +1,50 @@ +-----BEGIN PRIVATE KEY----- +MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCOTt8cJP70dLZi +iIvk4BwNtXB0hs8Io0jeL9Kz4UyzLTRfPvKL7cL4cS57HOOjfWnlzdOdcaYxjapT +6HSaF/4R/mRMD3g7X7LcczGJDBV5N8FdmPrwuvL7kDttt8bC+nINFQvGoFlSXZc8 +cx+bejxLs6EtanPFfqX+aOYWvAjJF9cTs3N7ZP7pmax59dB+tAhPQnKy+4QornSe +npLxUjiQHAxg90yru0up1OPDPMIJthO1V7eW84aqqce0y7KwUs8xAR3Qneh88XYP +9v7uKyEOcZgzKcG69Geee9OXMIHXh+TB87y0R8twrGT8lHT7V3Oi19YeNfDjJBy7 +vMgMoqZ5AgMBAAECggEAGIYjENiXT01luOlLInA0pXAlJw+sUhLBlfir2O/0pdQe +eGOC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+AQEAPrHmFUjofdVJL9AXcL3Ey5BSw+8Kq8DzSiQ0p9jgxZzMd7zTBItqtKiL9/Ph +qIyftGHcJfNUMWiEXH9R9MBkv1N43DRkDvChXdZCTgZ0lDBc9V64yUQZ/J7rvECF +fIDvwh+U4/FV1hzh/+5rWlU8nfwky8xzSKOgdZDle1+xRrSaGztYvyGpCPDjyCuu +yW9bha3ICPSB0fe/V4WGZYOUN9su+t+O4Xj7I9xJPVysPkdO9Ik0lyuPe2+briRP +O5zY/ach9MbJ6/nH3z5fl6MQKQ4c3f+Z6klwqbRQBC0aAUfAmDetbQi0W1cAvKY8 +banL+FYfkeVVxgTk3ilqtWQ+rw== +-----END CERTIFICATE----- diff --git a/ballerina/tests/resources/docker/certs/mongodb-client.jks b/ballerina/tests/resources/docker/certs/mongodb-client.jks new file mode 100644 index 00000000..8701d77f Binary files /dev/null and b/ballerina/tests/resources/docker/certs/mongodb-client.jks differ diff --git a/ballerina/tests/resources/docker/certs/mongodb-client.pem b/ballerina/tests/resources/docker/certs/mongodb-client.pem new file mode 100644 index 00000000..9efb5aa9 --- /dev/null +++ b/ballerina/tests/resources/docker/certs/mongodb-client.pem @@ -0,0 +1,48 @@ +-----BEGIN PRIVATE KEY----- +MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCrul6TzDjh55FT +OuVnPJU4NDWC58+tCsfv8LiTFtmk6F1Q7hQ28fEwG5R9EFYkSARqCn5J8Nmwe6SQ +GQ7N2WHzF65XkwcYMh9GqMiAMYNGcL+2yhkuacahCo+r5I2jumSaf5Fn8YTkhW1H +EFcZeo/YqNpLkJ+m4XaivUvPlx/IePz5Z5hnecbU8GuChUYUrDXBCbE4Jwd7f6J2 +GELokebmaKNXa4kG2hmmyrSUTk3ZKVrbxIgBZfteyMWiunSzhM4I2m3PEzcvDJcN +OzprO0XMHovi0rF5smDJUxKcLi7iMuAxSwVpggB2ITu/ueBXu33rprsvDDpwpe+y +cxd6vq9lAgMBAAECg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k9LsSR01Klkrhw7zNkTCvGGXq6FcRWZTQfjK0w6YicEuhsB8Vdr6E +PoLTpG6QIKwLiUpZEX4J9SqYfyrlOkFLAPS3eiLHZ41XAwFa5Fboh3822MYGoDuX +RgoU1eeD099L6Swm5kOq8KOXdLHtYpoQ3FLt+YSTTr/nwbe7W4RoM1MXIcQsJ0nX +WJRwh+mXvaPoo21BeXBHyL0BC+i826mn54wcOlrELVmJOuOgsOfmq/WeeP3BckMV +qCo2CLuz4Zt9GTXcTLVYW2wA6Ni5/i01tvhaCuHzpw== +-----END CERTIFICATE----- diff --git a/ballerina/tests/resources/docker/certs/mongodb-server.pem b/ballerina/tests/resources/docker/certs/mongodb-server.pem new file mode 100644 index 00000000..569bbca3 --- /dev/null +++ b/ballerina/tests/resources/docker/certs/mongodb-server.pem @@ -0,0 +1,48 @@ +-----BEGIN PRIVATE KEY----- +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC2YbOLff66HOaC +0/y2pwcJEvjCmPW8nbmI+DgE9rxhBgOPaSPk0QoqnHcJyfOvfnpgY+7i7a4NMoa/ +wyTONTrbiSa2jtwKgGKrK+SMJyouNGGWjYf44XzHD4AerJ4y5boQ0JVW5cBMUBzI +I7a5rs/RSINdg0svh/DTfPbtzr36cI/HKRQ0mAQgOR926zFHxZY04T/wv/dmDhUD +e31/aElB7hcEeHdJtNWCoUtroVdlVysuAf6J9sS01FYwRt0lt75Ln2BQu5D6dwD2 +HIgC88SsmsmbqOq4WxVcd9r9gFQa2sIo0Ze4cSmpEP6qwJr8uXXlfWXRgVNYEAR0 +qViUo2/zAgMBAAECggEAFCnSDUp1NoNKcs1YA6OMshKgYPdDN8KiSAPVQas7oWhm +TV0c5Ee+HX7l5DWpFM5hXjW+K+WGGBxtbFb5Qzj8VFgh6Q37Qsf14KG+J0aPMcOF +xHzKOxhG0MUkb3+7D7p5KzG8SPqmm2lB/ViBc57hh+xB5b1FuzAGzoLi0+DG/SBz +x+BDwEc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0lev +vKEC7OXxmvwekUYkX4+XPLjEU44gH8nlATSjXUGWFqLVHGp0arLCDzZBtLIQCSPi +alX/ZSoXwpRSx+8Go+E1Lu+ZC3VG2Gfl8Ldu1RT+8XA= +-----END CERTIFICATE----- diff --git a/ballerina/tests/resources/docker/docker-compose.yml b/ballerina/tests/resources/docker/docker-compose.yml new file mode 100644 index 00000000..9f422e5e --- /dev/null +++ b/ballerina/tests/resources/docker/docker-compose.yml @@ -0,0 +1,32 @@ +version: '3.8' + +services: + mongo-no-ssl: + image: mongo:4.2 + restart: always + ports: + - 27017:27017 + volumes: + - ./init-scripts/no-ssl.js:/docker-entrypoint-initdb.d/init-mongo.js:ro + healthcheck: + test: echo 'db.runCommand("ping").ok' | mongo localhost:27017/test --quiet + interval: 30s + timeout: 20s + retries: 3 + environment: + MONGO_INITDB_ROOT_USERNAME: admin + MONGO_INITDB_ROOT_PASSWORD: admin + + mongo-with-ssl: + image: mongo:4.2 + restart: always + ports: + - 27018:27018 + volumes: + - ./init-scripts/ssl.js:/docker-entrypoint-initdb.d/init-mongo.js:ro + - ./certs/mongodb-server.pem:/etc/ssl/mongodb.pem:ro + - ./certs/mongodb-CA.pem:/etc/ssl/mongodb-ca.pem:ro + depends_on: + mongo-no-ssl: + condition: service_healthy + command: --port 27018 --tlsMode requireTLS --tlsCertificateKeyFile /etc/ssl/mongodb.pem --tlsCAFile /etc/ssl/mongodb-ca.pem diff --git a/ballerina/tests/resources/docker/init-scripts/no-ssl.js b/ballerina/tests/resources/docker/init-scripts/no-ssl.js new file mode 100644 index 00000000..b262fc1c --- /dev/null +++ b/ballerina/tests/resources/docker/init-scripts/no-ssl.js @@ -0,0 +1,7 @@ +db.createUser( + { + user: "admin", + pwd: "admin", + roles: [{ role: "userAdminAnyDatabase", db: "admin" }] + } +) diff --git a/ballerina/tests/resources/docker/init-scripts/ssl.js b/ballerina/tests/resources/docker/init-scripts/ssl.js new file mode 100644 index 00000000..6f40e2f7 --- /dev/null +++ b/ballerina/tests/resources/docker/init-scripts/ssl.js @@ -0,0 +1,17 @@ +db.createUser( + { + user: "admin", + pwd: "admin", + roles: [{ role: "userAdminAnyDatabase", db: "admin" }] + } +) + +db.getSiblingDB("$external").runCommand( + { + createUser: "C=LK,ST=Western,L=Colombo,O=WSO2,OU=Ballerina,CN=admin", + roles: [ + { role: "root", db: "admin" }, + { role: "readWrite", db: "moviesDB" } + ] + } +); diff --git a/ballerina/tests/resources/mongodb-client.jks b/ballerina/tests/resources/mongodb-client.jks deleted file mode 100644 index b13d0fd4..00000000 Binary files a/ballerina/tests/resources/mongodb-client.jks and /dev/null differ diff --git a/ballerina/tests/ssl_connection_test.bal b/ballerina/tests/ssl_connection_test.bal index 1e485399..c2b110a8 100644 --- a/ballerina/tests/ssl_connection_test.bal +++ b/ballerina/tests/ssl_connection_test.bal @@ -14,14 +14,14 @@ // specific language governing permissions and limitations // under the License. -import ballerina/file; import ballerina/log; import ballerina/test; -string jksFilePath = check file:getAbsolutePath("ballerina/tests/resources/mongodb-client.jks"); +string keystorePath = "./tests/resources/docker/certs/mongodb-client.jks"; +string x509Username = "C=LK,ST=Western,L=Colombo,O=WSO2,OU=Ballerina,CN=admin"; X509Credential x509Credential = { - username: testUser + username: x509Username }; ConnectionConfig mongoConfigInvalid = { @@ -38,22 +38,21 @@ ConnectionConfig sslMongoConfig = { connection: { host: testHostName, auth: x509Credential, - port: 27012, + port: 27018, options: { socketTimeout: 10000, authMechanism: "MONGODB-X509", sslEnabled: true, - sslInvalidHostNameAllowed: true, secureSocket: { trustStore: { - path: jksFilePath, + path: keystorePath, password: "123456" }, keyStore: { - path: jksFilePath, + path: keystorePath, password: "123456" }, - protocol:"TLS" + protocol: "TLS" } } }, @@ -61,26 +60,25 @@ ConnectionConfig sslMongoConfig = { }; @test:Config { - groups: ["mongodb-ssl"] + groups: ["mongodb-ssl"] } public function initializeInValidConfig() { - log:printInfo("Start initialization test failure"); - Client|Error mongoClient = new (mongoConfigInvalid); - if (mongoClient is ApplicationError) { - log:printInfo("Creating client failed '" + mongoClient.message() + "'."); - } else { - test:assertFail("Error expected when url is invalid."); - } + Client|Error mongoClient = new (mongoConfigInvalid); + if (mongoClient is ApplicationError) { + log:printInfo("Creating client failed '" + mongoClient.message() + "'."); + } else { + test:assertFail("Expected an error for invalid client configurations"); + } } @test:Config { - dependsOn: [ initializeInValidConfig ], - groups: ["mongodb-ssl"] + groups: ["mongodb-ssl"] } public function testSSLConnection() returns error? { - log:printInfo("------------------ Inserting Data on SSL Connection ------------------"); - map document = {name: "The Lion King", year: "2019", rating: 8}; - - Client mongoClient = check new (sslMongoConfig); - check mongoClient->insert(document, "test"); + map document = {name: "The Lion King", year: "2019", rating: 8}; + Client|Error mongoClient = new (sslMongoConfig); + if mongoClient is Error { + return mongoClient; + } + check mongoClient->insert(document, "test"); } diff --git a/gradle.properties b/gradle.properties index 463e40e9..87160bd7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ org.gradle.caching=true group=org.ballerinalang.mongodb -version=4.3.0 +version=5.0.0-SNAPSHOT ballerinaLangVersion=2201.8.2 checkstylePluginVersion=10.12.0