Skip to content

Commit

Permalink
Add MongoDB engine
Browse files Browse the repository at this point in the history
  • Loading branch information
mg-diego committed Aug 20, 2023
1 parent a63ee39 commit b554545
Show file tree
Hide file tree
Showing 21 changed files with 664 additions and 1 deletion.
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
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -84,6 +88,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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -102,6 +114,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}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4F88CB24-23C8-4E0E-8B83-29023C1642B8}
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;

Check warning on line 16 in samples/TestWare.Samples.MongoDB/Hook.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Non-nullable field '_testReport' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 16 in samples/TestWare.Samples.MongoDB/Hook.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Non-nullable field '_testReport' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 16 in samples/TestWare.Samples.MongoDB/Hook.cs

View workflow job for this annotation

GitHub Actions / build (macOs-latest)

Non-nullable field '_testReport' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

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);
result[0].GetValue("name").RawValue.Should().Be(value);

Check warning on line 47 in samples/TestWare.Samples.MongoDB/StepDefinitions/DatabaseStepDefinitions.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

'BsonValue.RawValue' is obsolete: 'Use Value property of subclasses or BsonTypeMapper.MapToDotNetValue instead.'

Check warning on line 47 in samples/TestWare.Samples.MongoDB/StepDefinitions/DatabaseStepDefinitions.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

'BsonValue.RawValue' is obsolete: 'Use Value property of subclasses or BsonTypeMapper.MapToDotNetValue instead.'

Check warning on line 47 in samples/TestWare.Samples.MongoDB/StepDefinitions/DatabaseStepDefinitions.cs

View workflow job for this annotation

GitHub Actions / build (macOs-latest)

'BsonValue.RawValue' is obsolete: 'Use Value property of subclasses or BsonTypeMapper.MapToDotNetValue instead.'
}
}

[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\\"
}
36 changes: 36 additions & 0 deletions samples/TestWare.Samples.MongoDB/TestWare.Samples.MongoDB.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="SpecFlow" Version="3.9.74" />
<PackageReference Include="SpecFlow.MsTest" Version="3.9.74" />
</ItemGroup>

<ItemGroup>
<Folder Include="Features\" />
<Folder Include="Scripts\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Core\TestWare.TestReport\TestWare.ExtentReport.csproj" />
<ProjectReference Include="..\..\src\Engines\TestWare.Engines.MongoDB\TestWare.Engines.MongoDB.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="TestConfiguration.MongoDB.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading

0 comments on commit b554545

Please sign in to comment.