Skip to content

Commit

Permalink
feat(seeding): add initial wallet seeding
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil91 committed Sep 26, 2024
1 parent d166faa commit e97b540
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 52 deletions.
60 changes: 60 additions & 0 deletions charts/dim/templates/configmap-seeding-testdata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{{- /*
* Copyright (c) 2024 BMW Group AG
* Copyright 2024 SAP SE or an SAP affiliate company and ssi-dim-middle-layer contributors.
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/}}

{{- if .Values.migrations.seeding.testData.useDefault -}}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "dim.fullname" . }}-default-testdata
namespace: {{ .Release.Namespace }}
data:
tenants.defaulttest.json: |-
[
{
"id": "5c9a4f56-0609-49a5-ab86-dd8f93dfd3fa",
"company_name": "Catena-X",
"bpn": "{{ .Values.dim.bpn }}",
"did_document_location": "{{ .Values.dim.didDocumentLocationAddress }}",
"process_id": "dd371565-9489-4907-a2e4-b8cbfe7a8cd1",
"is_issuer": true,
"operator_id": "a7444c10-5b07-430d-a3c1-c52675c4fa5a"
}
]
processes.defaulttest.json: |-
[
{
"id": "dd371565-9489-4907-a2e4-b8cbfe7a8cd1",
"process_type_id" : 1,
"lock_expiry_date" : "2023-03-01 00:00:00.000000 +00:00",
"version" : "deadbeef-dead-beef-dead-beefdeadbeef"
}
]
process_steps.defaulttest.json: |-
[
{
"id": "5c9a4f56-0609-49a5-ab86-dd8f93dfd3fa",
"process_step_type_id": 1,
"process_step_status_id": 1,
"process_id": "dd371565-9489-4907-a2e4-b8cbfe7a8cd1",
"date_created": "2023-02-21 08:15:20.479000 +00:00",
"date_last_changed": null
}
]
{{- end -}}
46 changes: 44 additions & 2 deletions charts/dim/templates/job-migrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,25 @@ spec:
- name: "CONNECTIONSTRINGS__DIMDB"
value: "Server={{ .Values.externalDatabase.host }};Database={{ .Values.externalDatabase.database }};Port={{ .Values.externalDatabase.port }};User Id={{ .Values.externalDatabase.username }};Password=$(DIM_PASSWORD);Ssl Mode={{ .Values.dbConnection.sslMode }};"
{{- end }}
{{- if (.Values.migrations.seeding.testData.useDefault) }}
- name: "SEEDING__DATAPATHS__0"
value: "Seeder/Data/import/default-test-data"
- name: "SEEDING__TESTDATAENVIRONMENTS__0"
value: "{{ .Values.migrations.seeding.testDataEnvironments }}"
value: "defaulttest"
{{- end }}
{{- if and (.Values.migrations.seeding.testData.useOwnConfigMap.configMap) (.Values.migrations.seeding.testData.useOwnConfigMap.filename) }}
{{- if (.Values.migrations.seeding.testData.useDefault) }}
- name: "SEEDING__DATAPATHS__1"
value: "Seeder/Data/import/custom-test-data"
- name: "SEEDING__TESTDATAENVIRONMENTS__1"
value: "{{ .Values.migrations.seeding.testData.useOwnConfigMap.filename }}"
{{ else }}
- name: "SEEDING__DATAPATHS__0"
value: "{{ .Values.migrations.seeding.testDataPaths }}"
value: "Seeder/Data/import/custom-test-data"
- name: "SEEDING__TESTDATAENVIRONMENTS__0"
value: "{{ .Values.migrations.seeding.testData.useOwnConfigMap.filename }}"
{{- end }}
{{- end }}
- name: "SERILOG__MINIMUMLEVEL__Default"
value: "{{ .Values.migrations.logging.default }}"
ports:
Expand All @@ -76,3 +91,30 @@ spec:
protocol: TCP
resources:
{{- toYaml .Values.migrations.resources | nindent 10 }}
{{- if or (.Values.migrations.seeding.testData.useDefault) (and (.Values.migrations.seeding.testData.useOwnConfigMap.configMap) (.Values.migrations.seeding.testData.useOwnConfigMap.filename)) }}
volumeMounts:
{{- if (.Values.migrations.seeding.testData.useDefault) }}
- name: test-data-default
mountPath: /migrations/Seeder/Data/import/default-test-data
{{- end }}
{{- if and (.Values.migrations.seeding.testData.useOwnConfigMap.configMap) (.Values.migrations.seeding.testData.useOwnConfigMap.filename) }}
- name: test-data-custom
mountPath: /migrations/Seeder/Data/import/custom-test-data
{{- end }}
{{- end }}
{{- if or (.Values.migrations.seeding.testData.useDefault) (and (.Values.migrations.seeding.testData.useOwnConfigMap.configMap) (.Values.migrations.seeding.testData.useOwnConfigMap.filename)) }}
volumes:
{{- if (.Values.migrations.seeding.testData.useDefault) }}
- name: test-data-default
configMap:
name: "{{ include "dim.fullname" . }}-default-testdata"
optional: true
{{- end }}
{{- if and (.Values.migrations.seeding.testData.useOwnConfigMap.configMap) (.Values.migrations.seeding.testData.useOwnConfigMap.filename) }}
- name: test-data-custom
configMap:
name: "{{ .Values.migrations.seeding.testData.useOwnConfigMap.configMap }}"
optional: true
{{- end }}
{{- end }}

12 changes: 11 additions & 1 deletion charts/dim/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ dim:
path: "/ready"
swaggerEnabled: false
operatorId: "00000000-0000-0000-0000-000000000000"
# -- bpn of the operating company
bpn: "BPNL00000003CRHK"
didDocumentLocationAddress: "https://portal-backend.int.catena-x.net/api/administration/staticdata/did/BPNL00000003CRHK/did.json"

migrations:
name: "migrations"
Expand All @@ -60,7 +63,14 @@ migrations:
memory: 200M
seeding:
testDataEnvironments: ""
testDataPaths: "Seeder/Data"
testData:
# -- If set to true the data configured in the config map 'configmap-seeding-testdata.yaml' will be taken to insert the default test data
useDefault: true
useOwnConfigMap:
# -- ConfigMap containing json files for the tables to seed, e.g. technical_users.test.json, tenants.test.json, etc.
configMap: ""
# -- Filename identifying the test data files e.g. for tenants.test.json the value would be "test"
filename: ""
logging:
default: "Information"

Expand Down
7 changes: 0 additions & 7 deletions src/Dim.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{CB1B7D43
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Processes.Library.Tests", "..\tests\processes\Processes.Library.Tests\Processes.Library.Tests.csproj", "{AA29EECC-CD77-4452-B48E-CC038E4FBB69}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Processes.Worker.Library.Tests", "..\tests\processes\Processes.Worker.Library.Tests\Processes.Worker.Library.Tests.csproj", "{9888C703-CC13-47EB-89F0-B8A34D64BD07}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.Shared", "..\tests\shared\Tests.Shared\Tests.Shared.csproj", "{0D288AF0-1CE5-4B2B-9F80-532040F24BCF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DimProcess.Executor.Tests", "..\tests\processes\DimProcess.Executor.Tests\DimProcess.Executor.Tests.csproj", "{A44447B0-794D-451A-A571-E3B761174B48}"
Expand Down Expand Up @@ -96,10 +94,6 @@ Global
{AA29EECC-CD77-4452-B48E-CC038E4FBB69}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA29EECC-CD77-4452-B48E-CC038E4FBB69}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA29EECC-CD77-4452-B48E-CC038E4FBB69}.Release|Any CPU.Build.0 = Release|Any CPU
{9888C703-CC13-47EB-89F0-B8A34D64BD07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9888C703-CC13-47EB-89F0-B8A34D64BD07}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9888C703-CC13-47EB-89F0-B8A34D64BD07}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9888C703-CC13-47EB-89F0-B8A34D64BD07}.Release|Any CPU.Build.0 = Release|Any CPU
{0D288AF0-1CE5-4B2B-9F80-532040F24BCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0D288AF0-1CE5-4B2B-9F80-532040F24BCF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0D288AF0-1CE5-4B2B-9F80-532040F24BCF}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -137,7 +131,6 @@ Global
{1621A06B-9AF2-421C-BB48-08028560E108} = {4A305F87-23B4-4929-B138-4B7D2710526F}
{1B50DC13-DB8A-41CF-9D40-A8740B068AC2} = {D20B95EF-D7D9-45BA-B5A4-C8D3B64AC745}
{AA29EECC-CD77-4452-B48E-CC038E4FBB69} = {CB1B7D43-9AFC-47EF-8915-A547F7F553AB}
{9888C703-CC13-47EB-89F0-B8A34D64BD07} = {CB1B7D43-9AFC-47EF-8915-A547F7F553AB}
{0D288AF0-1CE5-4B2B-9F80-532040F24BCF} = {CB1B7D43-9AFC-47EF-8915-A547F7F553AB}
{A44447B0-794D-451A-A571-E3B761174B48} = {CB1B7D43-9AFC-47EF-8915-A547F7F553AB}
{85D316A0-17BE-4983-AB06-5C72365ABD9B} = {CB1B7D43-9AFC-47EF-8915-A547F7F553AB}
Expand Down
3 changes: 3 additions & 0 deletions src/database/Dim.Migrations/Dim.Migrations.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

<None Remove="Seeder\Data" />
<Content Include="Seeder\Data\**" LinkBase="Seeder\Data" CopyToOutputDirectory="Always" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,15 @@
using Microsoft.Extensions.Options;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Seeding;

namespace Dim.DbAccess.Tests.Seeder;
namespace Dim.Migrations.Seeder;

/// <summary>
/// Seeder to seed the base entities (those with an id as primary key)
/// </summary>
public class BatchInsertSeeder : ICustomSeeder
public class BatchInsertSeeder(DimDbContext context, ILogger<BatchInsertSeeder> logger, IOptions<SeederSettings> options)
: ICustomSeeder
{
private readonly DimDbContext _context;
private readonly ILogger<BatchInsertSeeder> _logger;
private readonly SeederSettings _settings;

/// <summary>
/// Constructor
/// </summary>
/// <param name="context">The database context</param>
/// <param name="logger">The logger</param>
/// <param name="options">The options</param>
public BatchInsertSeeder(DimDbContext context, ILogger<BatchInsertSeeder> logger, IOptions<SeederSettings> options)
{
_context = context;
_logger = logger;
_settings = options.Value;
}
private readonly SeederSettings _settings = options.Value;

/// <inheritdoc />
public int Order => 1;
Expand All @@ -56,37 +42,36 @@ public async Task ExecuteAsync(CancellationToken cancellationToken)
{
if (!_settings.DataPaths.Any())
{
_logger.LogInformation("There a no data paths configured, therefore the {SeederName} will be skipped", nameof(BatchInsertSeeder));
logger.LogInformation("There a no data paths configured, therefore the {SeederName} will be skipped", nameof(BatchInsertSeeder));
return;
}

_logger.LogInformation("Start BaseEntityBatch Seeder");
logger.LogInformation("Start BaseEntityBatch Seeder");
await SeedTable<Tenant>("tenants", x => x.Id, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
await SeedTable<ProcessStep>("process_steps", x => x.Id, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
await SeedTable<Process>("processes", x => x.Id, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
await SeedTable<TechnicalUser>("technical_users", x => x.Id, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);

await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
_logger.LogInformation("Finished BaseEntityBatch Seeder");
await context.SaveChangesAsync(cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
logger.LogInformation("Finished BaseEntityBatch Seeder");
}

private async Task SeedTable<T>(string fileName, Func<T, object> keySelector, CancellationToken cancellationToken) where T : class
{
_logger.LogInformation("Start seeding {Filename}", fileName);
logger.LogInformation("Start seeding {Filename}", fileName);
var additionalEnvironments = _settings.TestDataEnvironments ?? Enumerable.Empty<string>();
var data = await SeederHelper.GetSeedData<T>(_logger, fileName, _settings.DataPaths, cancellationToken, additionalEnvironments.ToArray()).ConfigureAwait(ConfigureAwaitOptions.None);
_logger.LogInformation("Found {ElementCount} data", data.Count);
var data = await SeederHelper.GetSeedData<T>(logger, fileName, _settings.DataPaths, cancellationToken, additionalEnvironments.ToArray()).ConfigureAwait(ConfigureAwaitOptions.None);
logger.LogInformation("Found {ElementCount} data", data.Count);
if (data.Any())
{
var typeName = typeof(T).Name;
_logger.LogInformation("Started to Seed {TableName}", typeName);
data = data.GroupJoin(_context.Set<T>(), keySelector, keySelector, (d, dbEntry) => new { d, dbEntry })
logger.LogInformation("Started to Seed {TableName}", typeName);
data = data.GroupJoin(context.Set<T>(), keySelector, keySelector, (d, dbEntry) => new { d, dbEntry })
.SelectMany(t => t.dbEntry.DefaultIfEmpty(), (t, x) => new { t, x })
.Where(t => t.x == null)
.Select(t => t.t.d).ToList();
_logger.LogInformation("Seeding {DataCount} {TableName}", data.Count, typeName);
await _context.Set<T>().AddRangeAsync(data, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
_logger.LogInformation("Seeded {TableName}", typeName);
logger.LogInformation("Seeding {DataCount} {TableName}", data.Count, typeName);
await context.Set<T>().AddRangeAsync(data, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
}
}
}
10 changes: 10 additions & 0 deletions src/database/Dim.Migrations/Seeder/Data/process_steps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"id": "5c9a4f56-0609-49a5-ab86-dd8f93dfd3fa",
"process_step_type_id": 1,
"process_step_status_id": 1,
"process_id": "dd371565-9489-4907-a2e4-b8cbfe7a8cd1",
"date_created": "2023-02-21 08:15:20.479000 +00:00",
"date_last_changed": null
}
]
8 changes: 8 additions & 0 deletions src/database/Dim.Migrations/Seeder/Data/processes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"id": "dd371565-9489-4907-a2e4-b8cbfe7a8cd1",
"process_type_id" : 1,
"lock_expiry_date" : "2023-03-01 00:00:00.000000 +00:00",
"version" : "deadbeef-dead-beef-dead-beefdeadbeef"
}
]
11 changes: 11 additions & 0 deletions src/database/Dim.Migrations/Seeder/Data/tenants.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"id": "5c9a4f56-0609-49a5-ab86-dd8f93dfd3fa",
"company_name": "Catena-X",
"bpn": "BPNL00000003CRHK",
"did_document_location": "https://portal-backend.int.catena-x.net/api/administration/staticdata/did/BPNL00000003CRHK/did.json",
"process_id": "dd371565-9489-4907-a2e4-b8cbfe7a8cd1",
"is_issuer": true,
"operator_id": "a7444c10-5b07-430d-a3c1-c52675c4fa5a"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,7 @@
"is_issuer": true,
"operator_id": "a7444c10-5b07-430d-a3c1-c52675c4fa5a",
"operation_id": "6dcac248-57ab-4309-9477-ee21586b3738",
"company_id": "6dcac248-57ab-4309-9477-ee21586b3666",
"company_id": null,
"base_url": "https://example.org/base"
},
{
"id": "5ced499f-7e7a-4e3c-aee6-41ae96df6d40",
"company_name": "test corp",
"bpn": "BPNL0000001CORP",
"did_document_location": "https://example.org/BPNL0000001CORP",
"process_id": "dd371565-9489-4907-a2e4-b8cbfe7a8cd2",
"is_issuer": false,
"operator_id": "a7444c10-5b07-430d-a3c1-c52675c4fa5a"
}
]
4 changes: 2 additions & 2 deletions tests/database/Dim.DbAccess.Tests/Setup/TestDbFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Dim.DbAccess.Tests.Seeder;
using Dim.Entities;
using Dim.Migrations.Migrations;
using Dim.Migrations.Seeder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -90,7 +90,7 @@ await _container.StartAsync()

var seederOptions = Options.Create(new SeederSettings
{
TestDataEnvironments = Enumerable.Empty<string>(),
TestDataEnvironments = Enumerable.Repeat("unittests", 1),
DataPaths = new[] { "Seeder/Data" }
});
var insertSeeder = new BatchInsertSeeder(context,
Expand Down

0 comments on commit e97b540

Please sign in to comment.