Skip to content

Commit

Permalink
feat: added CreateMessages command
Browse files Browse the repository at this point in the history
some required refactorings, too.
  • Loading branch information
ericbrunner committed Nov 11, 2024
1 parent 6f21e7c commit e1de2a1
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public async Task<List<DomainIdentity>> Handle(Command request, CancellationToke
{
var challenge = (await sdkClient.Challenges.CreateChallenge()).Result;


if (challenge is null) continue;

identitiesWithChallenge.Challenges.Add(challenge);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public async Task<List<DomainIdentity>> Handle(Command request, CancellationToke
identityConfiguration.NumberOfDevices,
identityConfiguration.NumberOfRelationshipTemplates,
identityConfiguration.IdentityPoolType,
identityConfiguration.NumberOfChallenges);
identityConfiguration.NumberOfChallenges,
identityConfiguration.PoolAlias);

identities.Add(createdIdentity);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Backbone.ConsumerApi.Sdk;
using Backbone.ConsumerApi.Sdk.Authentication;
using Backbone.ConsumerApi.Sdk.Endpoints.Messages.Types.Requests;
using Backbone.ConsumerApi.Tests.Performance.SnapshotCreator.V2.Features.Shared.Models;
using Backbone.Crypto;
using MediatR;

namespace Backbone.ConsumerApi.Tests.Performance.SnapshotCreator.V2.Features.Create;

public record CreateMessages
{
public record Command(
List<DomainIdentity> Identities,
List<RelationshipAndMessages> RelationshipAndMessages,
string BaseUrlAddress,
ClientCredentials ClientCredentials) : IRequest<List<DomainIdentity>>;

// ReSharper disable once UnusedMember.Global - Invoked via IMediator
public record CommandHandler : IRequestHandler<Command, List<DomainIdentity>>
{
public async Task<List<DomainIdentity>> Handle(Command request, CancellationToken cancellationToken)
{
foreach (var senderIdentity in request.Identities)
{
var recipientsRelationshipIds = request.RelationshipAndMessages
.Where(relationship =>
senderIdentity.PoolAlias == relationship.SenderPoolAlias &&
senderIdentity.ConfigurationIdentityAddress == relationship.SenderIdentityAddress)
.Select(r =>
(
r.RecipientIdentityAddress,
r.RecipientPoolAlias
))
.ToList();


var recipientIdentities = request.Identities
.Where(recipient => recipientsRelationshipIds.Any(relationship =>
relationship.RecipientPoolAlias == recipient.PoolAlias &&
relationship.RecipientIdentityAddress == recipient.ConfigurationIdentityAddress))
.ToList();


foreach (var recipientIdentity in recipientIdentities)
{
var sdkClient = Client.CreateForExistingIdentity(request.BaseUrlAddress, request.ClientCredentials, senderIdentity.UserCredentials);

var messageResponse = await sdkClient.Messages.SendMessage(new SendMessageRequest
{
Recipients =
[
new SendMessageRequestRecipientInformation
{
Address = recipientIdentity.IdentityAddress,
EncryptedKey = ConvertibleString.FromUtf8(new string('A', 152)).BytesRepresentation
}
],
Attachments = [],
Body = ConvertibleString.FromUtf8("Message body").BytesRepresentation
});

if (messageResponse.Result is null) continue;

senderIdentity.Messages.Add((messageResponse.Result.Id, recipientIdentity));
}
}

return request.Identities;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Backbone.ConsumerApi.Tests.Performance.SnapshotCreator.V2.Features.Cre

public record CreateRelationships
{
public record Command(List<RelationshipAndMessages> RelationshipAndMessages, List<DomainIdentity> Identities, string BaseUrlAddress, ClientCredentials ClientCredentials)
public record Command(List<DomainIdentity> Identities, List<RelationshipAndMessages> RelationshipAndMessages, string BaseUrlAddress, ClientCredentials ClientCredentials)
: IRequest<List<DomainIdentity>>;

// ReSharper disable once UnusedMember.Global - Invoked via IMediator
Expand All @@ -28,23 +28,36 @@ public async Task<List<DomainIdentity>> Handle(Command request, CancellationToke
foreach (var appIdentity in appIdentities)
{
var connectorRecipientIds = request.RelationshipAndMessages
.Where(r => r.RecipientIdentityPoolType == IdentityPoolType.Connector)
.Where(r => r.SenderIdentityAddress == appIdentity.IdentityConfigurationAddress)
.Select(r => r.RecipientIdentityAddress)
.Where(r =>
r.RecipientIdentityPoolType == IdentityPoolType.Connector &&
r.SenderIdentityAddress == appIdentity.ConfigurationIdentityAddress)
.Select(r =>
(
r.RecipientIdentityAddress,
r.RecipientPoolAlias
))
.ToList();

var connectorIdentityToEstablishRelationshipWith = connectorIdentities
.Where(c => connectorRecipientIds.Contains(c.IdentityConfigurationAddress))
.Where(c => connectorRecipientIds.Any(cr =>
cr.RecipientPoolAlias == c.PoolAlias &&
cr.RecipientIdentityAddress == c.ConfigurationIdentityAddress))
.ToList();

foreach (var connectorIdentity in connectorIdentityToEstablishRelationshipWith)
{
var appIdentitySdkClient = Client.CreateForExistingIdentity(request.BaseUrlAddress, request.ClientCredentials, appIdentity.UserCredentials);
var connectorIdentitySdkClient = Client.CreateForExistingIdentity(request.BaseUrlAddress, request.ClientCredentials, connectorIdentity.UserCredentials);

var nextRelationshipTemplate = connectorIdentity.RelationshipTemplates.First();
var index = connectorIdentity.RelationshipTemplates.IndexOf(nextRelationshipTemplate);
connectorIdentity.RelationshipTemplates.RemoveAt(index);
var nextRelationshipTemplate = connectorIdentity.RelationshipTemplates.FirstOrDefault();

if (nextRelationshipTemplate is null)

Check failure on line 54 in Applications/ConsumerApi/test/ConsumerApi.Tests.Performance/tools/snapshot-creator-v2/Features/Create/CreateRelationships.cs

View workflow job for this annotation

GitHub Actions / Check Formatting

Null check can be simplified
{
throw new InvalidOperationException(
$"Connector Identity {connectorIdentity.IdentityAddress}/{connectorIdentity.ConfigurationIdentityAddress}/{connectorIdentity.IdentityPoolType} [IdentityAddress/ConfigAddress/Pool] has no further RelationshipTemplates.");
}

connectorIdentity.RelationshipTemplates.Remove(nextRelationshipTemplate);

var createRelationshipResponse = await appIdentitySdkClient.Relationships.CreateRelationship(
new CreateRelationshipRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ public async Task<StatusMessage> Handle(Command request, CancellationToken cance
identities = await Mediator.Send(new CreateRelationshipTemplates.Command(identities, request.BaseAddress, clientCredentials), cancellationToken);
Logger.LogInformation("Relationship templates created");

identities = await Mediator.Send(new CreateRelationships.Command(poolConfig.RelationshipAndMessages, identities, request.BaseAddress, clientCredentials), cancellationToken);
identities = await Mediator.Send(new CreateRelationships.Command(identities, poolConfig.RelationshipAndMessages, request.BaseAddress, clientCredentials), cancellationToken);
Logger.LogInformation("Relationships created");

identities = await Mediator.Send(new CreateChallenges.Command(identities, request.BaseAddress, clientCredentials), cancellationToken);
Logger.LogInformation("Challenges created");

// Create Messages
identities = await Mediator.Send(new CreateMessages.Command(identities, poolConfig.RelationshipAndMessages, request.BaseAddress, clientCredentials), cancellationToken);
Logger.LogInformation("Messages created");

// Create DatawalletModifications

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public RelationshipAndMessages[] Generate(PerformanceTestConfiguration poolConfi
}

var relationshipAndMessages = new RelationshipAndMessages(
SenderPool: appIdentity.PoolAlias,
SenderPoolAlias: appIdentity.PoolAlias,
SenderIdentityAddress: appIdentity.Address,
RecipientPool: recipientConnectorIdentityConfiguration.PoolAlias,
RecipientPoolAlias: recipientConnectorIdentityConfiguration.PoolAlias,
RecipientIdentityAddress: recipientConnectorIdentityConfiguration.Address);

appIdentity.RelationshipAndMessages.Add(relationshipAndMessages);
Expand All @@ -72,9 +72,9 @@ public RelationshipAndMessages[] Generate(PerformanceTestConfiguration poolConfi


var reverseRelationshipAndMessages = new RelationshipAndMessages(
SenderPool: recipientConnectorIdentityConfiguration.PoolAlias,
SenderPoolAlias: recipientConnectorIdentityConfiguration.PoolAlias,
SenderIdentityAddress: recipientConnectorIdentityConfiguration.Address,
RecipientPool: appIdentity.PoolAlias,
RecipientPoolAlias: appIdentity.PoolAlias,
RecipientIdentityAddress: appIdentity.Address);

recipientConnectorIdentityConfiguration.RelationshipAndMessages.Add(reverseRelationshipAndMessages);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ public record DomainIdentity(
string IdentityAddress, // the real identity address returned by sdk
string DeviceId,
IdentityPoolConfiguration IdentityPoolConfiguration,
int IdentityConfigurationAddress, // the address from pool-config json
int ConfigurationIdentityAddress, // the address from pool-config json
int NumberOfDevices,
int NumberOfRelationshipTemplates,
IdentityPoolType IdentityPoolType,
int NumberOfChallenges)
int NumberOfChallenges,
string PoolAlias)
{
public List<string> DeviceIds = [];

Expand All @@ -23,6 +24,8 @@ public record DomainIdentity(
public Dictionary<string, DomainIdentity> EstablishedRelationshipsById { get; } = [];
public List<Challenge> Challenges { get; set; } = [];

public HashSet<(string messageId, DomainIdentity recipient)> Messages { get; private set; } = [];

public void AddDevice(string deviceId)
{
DeviceIds.Add(deviceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
namespace Backbone.ConsumerApi.Tests.Performance.SnapshotCreator.V2.Features.Shared.Models;

public record RelationshipAndMessages(
string SenderPool,
string SenderPoolAlias,
int SenderIdentityAddress,
string RecipientPool,
string RecipientPoolAlias,
int RecipientIdentityAddress)
{
public long NumberOfSentMessages { get; set; }

[Ignore, JsonIgnore]
public IdentityPoolType RecipientIdentityPoolType => RecipientPool.FirstOrDefault() switch
public IdentityPoolType RecipientIdentityPoolType => RecipientPoolAlias.FirstOrDefault() switch
{
'n' => IdentityPoolType.Never,
'a' => IdentityPoolType.App,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,47 @@ The number of messages and relationships is defined in the pool-config.*.json fi

## 1. JIRA Issue:

https://sprind-mb-team.atlassian.net/browse/ABL-221
https://sprind-mb-team.atlassian.net/browse/ABL-244

READ Notes: https://js-soft.atlassian.net/wiki/spaces/JSSNMSHDD/pages/3892871169/Performance+Test

##2. Feature Branch
## 2. Feature Branch

https://github.com/nmshd/backbone/tree/abl-10-fill-db-for-perftests-tool

## 3. Dev-ToDos:
Current branch vector:

```
main --> abl-10 --> abl-10-fill-db-for-perftests-tool
```

PR (Intermediate Review) Branch: abl-10-fill-db-for-perftests-tool
PR (Intermediate Review): https://github.com/nmshd/backbone/pull/921

### 3.1 CLI Commands:
## 3. CLI Commands Usage:

#### 3.1.1 Verify JSON Pool Config
### 3.1 Verify JSON Pool Config

> verifies the json pool-config-<worksheet-name>.json files against the source PerformanceTestData.xls for validity
```shell
verify-config --source PerformanceTestData.xlsx --worksheet <worksheet-name> --pool-config pool-config.<worksheet-name>.json
```

##### 3.1.1.1 Methods and Tests:

- [x] Deserialize from JSON
- [x] Deserialize from Excel Calculation (PerformanceTestData.xlsx)
- [x] Verify JSON configs against Excel Calculation (PerformanceTestData.xlsx)
- [x] Verify JSON Pool Config Command


#### 3.1.2 Generate Json Pool Configs with Relationships And Message Command
### 3.2 Generate Json Pool Config

> generates the pool-config.<worksheet-name>.json
```shell
generate-config --source PerformanceTestData.xlsx --worksheet <worksheet-name>
```

##### 3.1.2.1 Methods and Tests:

- [x] Generate JSON configs from Excel Calculation (PerformanceTestData.xlsx)
- [x] Generate Relationships and Messages
- [x] Distribute JSON configs with Relationships and Messages (pool-config.<worksheet-name>.json)
- [x] Generate RelationshipsAndMessagePoolConfigs Excel Command

#### 3.1.3 Create Identity relationships and messages Command
### 3.3 Create Database Snapshot

> Applie the pools configs including their relationships and messages in the Database
```shell
create-snapshot --baseAddress http://localhost:8081 --clientId test --clientSecret test --pool-config pool-config.<worksheet-name>.json
```

##### 3.1.3.1 Methods and Tests:

- [ ] Apply pools configs including their relationships and messages in the Database (Create Database Snapshot)
- [ ] Apply RelationshipsAndMessagePoolConfigs Excel in Database Command

## 3.2 Console Application (CLI)

Command Line Parser Nuget we want to use: McMaster.Extensions.CommandLineUtils

nuget: https://www.nuget.org/packages/McMaster.Extensions.CommandLineUtils/

Current branch vector:

```
main --> abl-10 --> abl-10-fill-db-for-perftests-tool
```

PR (Intermediate Review) Branch: abl-10-fill-db-for-perftests-tool
PR (Intermediate Review): https://github.com/nmshd/backbone/pull/921


0 comments on commit e1de2a1

Please sign in to comment.