Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MongoDB engine #84

Merged
merged 7 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/Sonar_Scanner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ jobs:
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x
- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 1.11
java-version: 17
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
Expand Down
14 changes: 14 additions & 0 deletions TestWare.sln
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestWare.ExtentReport", "sr
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Reporting", "Reporting", "{DCCEF363-0EBE-46EA-B02B-CD59010626F6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestWare.Engines.MongoDB", "src\Engines\TestWare.Engines.MongoDB\TestWare.Engines.MongoDB.csproj", "{4DFC2BE5-D3EF-4F39-AAD4-B8213DE92A11}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestWare.Samples.MongoDB", "samples\TestWare.Samples.MongoDB\TestWare.Samples.MongoDB.csproj", "{5378DE68-675E-440D-AAA9-7D3AF8AA680E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestWare.AllureReport", "src\Core\TestWare.AllureReport\TestWare.AllureReport.csproj", "{91F2B723-A68B-4711-969B-89897B3C6161}"
EndProject
Global
Expand Down Expand Up @@ -86,6 +90,14 @@ Global
{94025C6B-DF0D-4AC4-BBC5-A94A9FA3AB0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{94025C6B-DF0D-4AC4-BBC5-A94A9FA3AB0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{94025C6B-DF0D-4AC4-BBC5-A94A9FA3AB0A}.Release|Any CPU.Build.0 = Release|Any CPU
{4DFC2BE5-D3EF-4F39-AAD4-B8213DE92A11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4DFC2BE5-D3EF-4F39-AAD4-B8213DE92A11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4DFC2BE5-D3EF-4F39-AAD4-B8213DE92A11}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4DFC2BE5-D3EF-4F39-AAD4-B8213DE92A11}.Release|Any CPU.Build.0 = Release|Any CPU
{5378DE68-675E-440D-AAA9-7D3AF8AA680E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5378DE68-675E-440D-AAA9-7D3AF8AA680E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5378DE68-675E-440D-AAA9-7D3AF8AA680E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5378DE68-675E-440D-AAA9-7D3AF8AA680E}.Release|Any CPU.Build.0 = Release|Any CPU
{91F2B723-A68B-4711-969B-89897B3C6161}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{91F2B723-A68B-4711-969B-89897B3C6161}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91F2B723-A68B-4711-969B-89897B3C6161}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -108,6 +120,8 @@ Global
{71120454-DD7A-484C-93F2-699B4C363297} = {58B1446D-98A3-46A2-A668-305F0170B39F}
{94025C6B-DF0D-4AC4-BBC5-A94A9FA3AB0A} = {DCCEF363-0EBE-46EA-B02B-CD59010626F6}
{DCCEF363-0EBE-46EA-B02B-CD59010626F6} = {4678C707-68DB-4E06-9184-A07FB398832C}
{4DFC2BE5-D3EF-4F39-AAD4-B8213DE92A11} = {A2E4E1F7-CB61-48DC-AA38-A74F7DC74CA2}
{5378DE68-675E-440D-AAA9-7D3AF8AA680E} = {58B1446D-98A3-46A2-A668-305F0170B39F}
{91F2B723-A68B-4711-969B-89897B3C6161} = {DCCEF363-0EBE-46EA-B02B-CD59010626F6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
25 changes: 25 additions & 0 deletions samples/TestWare.Samples.MongoDB/Features/Database.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@MongoDB
Feature: Database


Scenario: Insert at database
When the following document is inserted in 'collection-example' collection at 'database-example' database
| NAME |
| Didac |
Then the following document is saved in 'collection-example' collection at 'database-example' database
| NAME |
| Didac |


Scenario: Delete at database
Given the following document is saved in 'collection-example' collection at 'database-example' database
| NAME |
| Diego |
When the following document is deleted in 'collection-example' collection at 'database-example' database
| NAME |
| Diego |
Then no documents are saved in 'collection-example' collection at 'database-example' database with values
| NAME |
| Diego |


114 changes: 114 additions & 0 deletions samples/TestWare.Samples.MongoDB/Hook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using TestWare.Reporting.ExtentReport;
using TestWare.Samples.MongoDB.Seeder;

namespace TestWare.Samples.MongoDB;

[Binding]
public sealed class Hook
{
private readonly TestContext _testContext;
private int _stepCounter;
private static readonly LifeCycle _lifeCycle = new();
private static ExtentReport? _testReport;

public Hook(TestContext testContext)
{
_testContext = testContext;
}

[BeforeFeature]
public static void BeforeFeature(FeatureContext featureContext)
{
var name = featureContext.FeatureInfo.Title;
var tags = featureContext.FeatureInfo.Tags;

_lifeCycle.BeginTestSuite(name);
_testReport?.CreateFeature(name, tags);
}

[AfterFeature]
public static void AfterFeature(FeatureContext featureContext)
{
_lifeCycle.EndTestSuite();
}

[BeforeScenario]
public void BeforeScenario(FeatureContext featureContext, ScenarioContext scenarioContext)
{
var name = scenarioContext.ScenarioInfo.Arguments.Count > 0
? $"{DateTime.UtcNow.ToString("yyy-MM-dd HH-mm-ss", CultureInfo.InvariantCulture)} - {scenarioContext.ScenarioInfo.Title}"
: scenarioContext.ScenarioInfo.Title;

var description = scenarioContext.ScenarioInfo.Description ?? "";
var scenarioTags = scenarioContext.ScenarioInfo.Tags;
_testReport?.CreateTestCase(name, description, scenarioTags);

_testContext.WriteLine("----------------------------------------- \r\n");
_testContext.WriteLine($"Feature: {featureContext.FeatureInfo.Title}");
_testContext.WriteLine($" Scenario: {scenarioContext.ScenarioInfo.Title} \r\n");

_stepCounter = 1;
var tags = GetTags(featureContext, scenarioContext);
_lifeCycle.BeginTestCase(name, tags);

var databaseSeeder = new DatabaseSeeder();
databaseSeeder.InitializeDatabase();
}

[AfterScenario]
public void AfterScenario()
{
_testReport?.SetTestcaseOutcome(_testContext.CurrentTestOutcome);
_lifeCycle.EndTestCase();
}

[BeforeTestRun]
public static void BeforeTestRun()
{
_lifeCycle.BeginTestExecution();
_testReport = new ExtentReport(_lifeCycle.GetCurrentResultsDirectory());
}

[AfterTestRun]
public static void AfterTestRun()
{
_lifeCycle.EndTestExecution();
_testReport?.CreateTestReportFile();
}

[BeforeStep]
public void BeforeStep(ScenarioContext scenarioContext)
{
var name = scenarioContext.CurrentScenarioBlock.ToString();
var description = scenarioContext.StepContext.StepInfo.Text;
_testReport?.CreateStep(name, description);

var stepId = $"{_stepCounter:00} {description}";
_stepCounter++;
_lifeCycle.BeginTestStep(stepId);
}

[AfterStep]
public void AfterStep(ScenarioContext scenarioContext)
{
_lifeCycle.EndTestStep();
var evidencesPath = _lifeCycle.GetStepEvidences();

foreach (var evidence in evidencesPath)
{
_testReport?.AddScreenshotToStep(evidence);
_testContext.AddResultFile(evidence);
}
}

private static List<string> GetTags(FeatureContext featureContext, ScenarioContext scenarioContext)
{
var tags = featureContext.FeatureInfo.Tags.ToList();
tags.AddRange(scenarioContext.ScenarioInfo.Tags.ToList());
return tags;
}
}
3 changes: 3 additions & 0 deletions samples/TestWare.Samples.MongoDB/ImplicitUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global using FluentAssertions;
global using Microsoft.VisualStudio.TestTools.UnitTesting;
global using TechTalk.SpecFlow;
36 changes: 36 additions & 0 deletions samples/TestWare.Samples.MongoDB/LifeCycle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.Reflection;
using TestWare.Core;
using TestWare.Core.Configuration;
using TestWare.Core.Interfaces;
using TestWare.Engines.MongoDB;

namespace TestWare.Samples.MongoDB;

internal class LifeCycle : AutomationLifeCycleBase
{
protected override IEnumerable<Assembly> GetTestWareComponentAssemblies()
{
IEnumerable<Assembly> assemblies = new[]
{
typeof(Hook).Assembly
};

return assemblies;
}

protected override IEnumerable<IEngineManager> GetTestWareEngines()
{
IEnumerable<IEngineManager> engines = new[]
{
new MongoDbManager()
};

return engines;
}

protected override TestConfiguration GetConfiguration()
{
return ConfigurationManager.ReadConfigurationFile("TestConfiguration.MongoDB.json");
}
}
15 changes: 15 additions & 0 deletions samples/TestWare.Samples.MongoDB/Scripts/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3.7'

services:
mongodb:
image: mongo:latest
container_name: mongodb
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: guest
MONGO_INITDB_ROOT_PASSWORD: guest
MONGO_INITDB_DATABASE: admin
ports:
- 27017:27017
volumes:
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-compose up --build -d mongodb
12 changes: 12 additions & 0 deletions samples/TestWare.Samples.MongoDB/Scripts/mongo-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
db.createUser(
{
user: "guest",
pwd: "guest",
roles: [
{
role: "readWrite",
db: "database-example"
}
]
}
);
27 changes: 27 additions & 0 deletions samples/TestWare.Samples.MongoDB/Seeder/DatabaseSeeder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using MongoDB.Bson;
using TestWare.Core;
using TestWare.Engines.MongoDB.Factory;

namespace TestWare.Samples.MongoDB.Seeder;

internal class DatabaseSeeder
{
private readonly IMongoDbClient _mongoDbClient;
private const string DATABASE_NAME = "database-example";
private const string COLLECTION_NAME = "collection-example";

public DatabaseSeeder()
{
_mongoDbClient = ContainerManager.GetTestWareComponent<IMongoDbClient>();
}

public void InitializeDatabase()
{
_mongoDbClient.DropDatabase(DATABASE_NAME);

_mongoDbClient.CreateDatabase(DATABASE_NAME);
_mongoDbClient.CreateCollection(DATABASE_NAME, COLLECTION_NAME);

_mongoDbClient.InsertOne(new BsonDocument("name", "Diego"), DATABASE_NAME, COLLECTION_NAME);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using FluentAssertions.Execution;
using MongoDB.Bson;
using TestWare.Core;
using TestWare.Engines.MongoDB.Factory;

namespace TestWare.Samples.MongoDB.StepDefinitions;

[Binding]
public class DatabaseStepDefinitions
{
private readonly IMongoDbClient _mongoDbClient;

public DatabaseStepDefinitions()
{
_mongoDbClient = ContainerManager.GetTestWareComponent<IMongoDbClient>();
}

[When(@"the following document is inserted in '([^']*)' collection at '([^']*)' database")]
public void TheFollowingDocumentIsInsertedInCollectionAtDatabase(string collectionName, string databaseName, Table table)
{
var value = table.Rows[0]["NAME"].ToString();

_mongoDbClient.InsertOne(new BsonDocument("name", value), databaseName, collectionName);
}

[When(@"the following document is deleted in '([^']*)' collection at '([^']*)' database")]
public void TheFollowingDocumentIsDeletedInCollectionAtDatabase(string collectionName, string databaseName, Table table)
{
var value = table.Rows[0]["NAME"].ToString();

_mongoDbClient.DeleteOne(new BsonDocument("name", value), databaseName, collectionName);
}


[Given(@"the following document is saved in '([^']*)' collection at '([^']*)' database")]
[Then(@"the following document is saved in '([^']*)' collection at '([^']*)' database")]
public void TheFollowingDocumentIsSavedInCollectionAtDatabase(string collectionName, string databaseName, Table table)
{
var value = table.Rows[0]["NAME"].ToString();

var result = _mongoDbClient.Find(new BsonDocument("name", value), databaseName, collectionName).Result;

using (new AssertionScope())
{
result.Should().NotBeNull();
result.Count.Should().Be(1);
BsonTypeMapper.MapToDotNetValue(result[0].GetValue("name")).ToString().Should().Be(value);
}
}

[Then(@"no documents are saved in '([^']*)' collection at '([^']*)' database with values")]
public void ThenNoDocumentsAreSavedInCollectionAtDatabaseWithValues(string collectionName, string databaseName, Table table)
{
var value = table.Rows[0]["NAME"].ToString();

var result = _mongoDbClient.Find(new BsonDocument("name", value), databaseName, collectionName).Result;

result.Count.Should().Be(0);
}

}
14 changes: 14 additions & 0 deletions samples/TestWare.Samples.MongoDB/TestConfiguration.MongoDB.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Configurations": [
{
"Tag": "MongoDB",
"Capabilities": [
{
"Name": "MongoDB",
"ConnectionString": "mongodb://guest:guest@localhost:27017/"
}
]
}
],
"TestResultPath": "C:\\workspace\\ERNI\\results\\"
}
Loading
Loading