CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (push) Canceled after 0s
CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (pull_request) Canceled after 0s
CI / Core-Modul (Build/Vet/Test) (push) Failing after 57s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (push) Successful in 5s
CI / Connect Security-Test-Gate (QA-01 AC4, Merge-Gate) (push) Successful in 5s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (push) Failing after 22s
CI / Web (dms-browser) (push) Failing after 1m23s
CI / Web (pdf-viewer) (push) Failing after 55s
CI / Web (shl) (push) Failing after 13s
Connect-Schnittstellen-Vertragstests / contract-tests (push) Successful in 10m42s
Supply-Chain-Scan (Go) / govulncheck (push) Failing after 1m10s
CI / Core-Modul (Build/Vet/Test) (pull_request) Failing after 58s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (pull_request) Successful in 4s
CI / Connect Security-Test-Gate (QA-01 AC4, Merge-Gate) (pull_request) Successful in 4s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (pull_request) Failing after 24s
CI / Web (dms-browser) (pull_request) Failing after 1m25s
CI / Web (pdf-viewer) (pull_request) Failing after 54s
CI / Web (shl) (pull_request) Failing after 11s
Connect-Schnittstellen-Vertragstests / contract-tests (pull_request) Successful in 12m3s
Core-Schnittstellen-Vertragstests / contract-tests (pull_request) Successful in 11m40s
Supply-Chain-Scan (Go) / govulncheck (pull_request) Failing after 1m11s
Supply-Chain-Scan (npm) / npm-audit (pull_request) Failing after 1m49s
Core-Pflichttest-Gate / pflichttest-gate (pull_request) Successful in 3m23s
RBAC-Rollenhierarchie-Pflichtgate / rbac-policy-invariant (pull_request) Successful in 11m49s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (push) Failing after 3s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (pull_request) Failing after 3s
DB-Integrationstests / db-tests (pull_request) Canceled after 0s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
212 lines
6.0 KiB
Go
212 lines
6.0 KiB
Go
package fieldmapping
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"gitea.perlbach24.de/scripte/nexarch/common/testdb"
|
|
"gitea.perlbach24.de/scripte/nexarch/connect/internal/migrate"
|
|
)
|
|
|
|
func newStore(t *testing.T) *Store {
|
|
t.Helper()
|
|
dsn := os.Getenv("TEST_TENANT_DSN")
|
|
if dsn == "" {
|
|
t.Skip("TEST_TENANT_DSN nicht gesetzt, Integrationstest uebersprungen")
|
|
}
|
|
pool := testdb.Pool(t, dsn)
|
|
|
|
root, err := filepath.Abs("../../migrations")
|
|
if err != nil {
|
|
t.Fatalf("migrationsverzeichnis: %v", err)
|
|
}
|
|
migrations, err := migrate.Load(root)
|
|
if err != nil {
|
|
t.Fatalf("migrationen laden: %v", err)
|
|
}
|
|
if _, err := migrate.Up(context.Background(), pool, migrations); err != nil {
|
|
t.Fatalf("migrationen anwenden: %v", err)
|
|
}
|
|
return NewStore(pool)
|
|
}
|
|
|
|
func testRef(t *testing.T) Ref {
|
|
t.Helper()
|
|
return Ref{TenantSlug: "acme", ConnectorName: "testconnector", ConnectorInstanceID: t.Name()}
|
|
}
|
|
|
|
func testSchema() []FieldDef {
|
|
return []FieldDef{
|
|
{NexarchField: "title", Required: true},
|
|
{NexarchField: "created_by", Required: true},
|
|
{NexarchField: "tags", Required: false},
|
|
}
|
|
}
|
|
|
|
// AC1: Feld-Mapping ist ohne Codeänderung konfigurierbar.
|
|
func TestSave_ConfigurableWithoutCodeChange(t *testing.T) {
|
|
s := newStore(t)
|
|
ctx := context.Background()
|
|
|
|
m, err := s.Save(ctx, testRef(t), []Entry{
|
|
{NexarchField: "title", ExternalField: "Subject"},
|
|
{NexarchField: "created_by", ExternalField: "Author"},
|
|
}, testSchema())
|
|
if err != nil {
|
|
t.Fatalf("save: %v", err)
|
|
}
|
|
if m.Version != 1 {
|
|
t.Fatalf("version = %d, want 1", m.Version)
|
|
}
|
|
|
|
got, err := s.GetActive(ctx, testRef(t))
|
|
if err != nil {
|
|
t.Fatalf("get active: %v", err)
|
|
}
|
|
if len(got.Fields) != 2 {
|
|
t.Fatalf("fields = %d, want 2", len(got.Fields))
|
|
}
|
|
}
|
|
|
|
// AC2 / Prüfung 1: ungültiges Mapping wird beim Speichern abgewiesen.
|
|
func TestSave_RejectsInvalidMapping(t *testing.T) {
|
|
s := newStore(t)
|
|
ctx := context.Background()
|
|
|
|
cases := []struct {
|
|
name string
|
|
fields []Entry
|
|
}{
|
|
{"leer", nil},
|
|
{"unbekanntes feld", []Entry{{NexarchField: "not_in_schema", ExternalField: "X"}}},
|
|
{"leerer external-name", []Entry{{NexarchField: "title", ExternalField: ""}}},
|
|
{"leerer nexarch-name", []Entry{{NexarchField: "", ExternalField: "X"}}},
|
|
{"doppeltes nexarch-feld", []Entry{
|
|
{NexarchField: "title", ExternalField: "Subject"},
|
|
{NexarchField: "title", ExternalField: "OtherSubject"},
|
|
}},
|
|
{"doppeltes ziel", []Entry{
|
|
{NexarchField: "title", ExternalField: "Subject"},
|
|
{NexarchField: "created_by", ExternalField: "Subject"},
|
|
}},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
ref := Ref{TenantSlug: "acme", ConnectorName: "testconnector", ConnectorInstanceID: t.Name()}
|
|
if _, err := s.Save(ctx, ref, c.fields, testSchema()); !errors.Is(err, ErrInvalidConfig) {
|
|
t.Fatalf("err = %v, want ErrInvalidConfig", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// AC3 / Prüfung 2: Pflichtfeld ohne Zuordnung wird vor Ausführung gemeldet.
|
|
func TestValidateForExecution_ReportsMissingRequiredFields(t *testing.T) {
|
|
s := newStore(t)
|
|
ctx := context.Background()
|
|
|
|
// Nur "title" gemappt, "created_by" (required) fehlt.
|
|
m, err := s.Save(ctx, testRef(t), []Entry{
|
|
{NexarchField: "title", ExternalField: "Subject"},
|
|
}, testSchema())
|
|
if err != nil {
|
|
t.Fatalf("save: %v", err)
|
|
}
|
|
|
|
err = ValidateForExecution(m, testSchema())
|
|
if !errors.Is(err, ErrMissingFields) {
|
|
t.Fatalf("err = %v, want ErrMissingFields", err)
|
|
}
|
|
if !contains(err.Error(), "created_by") {
|
|
t.Fatalf("error %q does not name the missing field", err.Error())
|
|
}
|
|
|
|
// Vollständig gemappt: kein Fehler mehr.
|
|
m2, err := s.Save(ctx, testRef(t), []Entry{
|
|
{NexarchField: "title", ExternalField: "Subject"},
|
|
{NexarchField: "created_by", ExternalField: "Author"},
|
|
}, testSchema())
|
|
if err != nil {
|
|
t.Fatalf("save (complete): %v", err)
|
|
}
|
|
if err := ValidateForExecution(m2, testSchema()); err != nil {
|
|
t.Fatalf("validate for execution (complete mapping): %v", err)
|
|
}
|
|
}
|
|
|
|
// Prüfung 3: geändertes Mapping wirkt erst auf nachfolgende Läufe. Eine
|
|
// bereits erfasste Mapping-Version (wie sie ein laufender Sync beim Start
|
|
// festhalten würde) bleibt unverändert lesbar, auch nachdem eine neue
|
|
// Version gespeichert wurde.
|
|
func TestGetVersion_ChangedMappingOnlyAffectsSubsequentRuns(t *testing.T) {
|
|
s := newStore(t)
|
|
ctx := context.Background()
|
|
ref := testRef(t)
|
|
|
|
v1, err := s.Save(ctx, ref, []Entry{
|
|
{NexarchField: "title", ExternalField: "Subject"},
|
|
{NexarchField: "created_by", ExternalField: "Author"},
|
|
}, testSchema())
|
|
if err != nil {
|
|
t.Fatalf("save v1: %v", err)
|
|
}
|
|
|
|
// Ein "laufender Sync" erfasst v1 zu Beginn.
|
|
inFlight, err := s.GetVersion(ctx, ref, v1.Version)
|
|
if err != nil {
|
|
t.Fatalf("get version v1: %v", err)
|
|
}
|
|
|
|
// Konfiguration ändert sich während der in-flight-Sync noch läuft.
|
|
v2, err := s.Save(ctx, ref, []Entry{
|
|
{NexarchField: "title", ExternalField: "Headline"},
|
|
{NexarchField: "created_by", ExternalField: "Author"},
|
|
}, testSchema())
|
|
if err != nil {
|
|
t.Fatalf("save v2: %v", err)
|
|
}
|
|
if v2.Version != v1.Version+1 {
|
|
t.Fatalf("v2.Version = %d, want %d", v2.Version, v1.Version+1)
|
|
}
|
|
|
|
// Die bereits erfasste Snapshot-Version bleibt unverändert.
|
|
if inFlight.Fields[0].ExternalField != "Subject" {
|
|
t.Fatalf("in-flight snapshot wurde veraendert: %+v", inFlight.Fields)
|
|
}
|
|
reread, err := s.GetVersion(ctx, ref, v1.Version)
|
|
if err != nil {
|
|
t.Fatalf("get version v1 again: %v", err)
|
|
}
|
|
if reread.Fields[0].ExternalField != "Subject" {
|
|
t.Fatalf("gespeicherte v1 wurde veraendert: %+v", reread.Fields)
|
|
}
|
|
|
|
// Neue Läufe sehen die neue Version.
|
|
active, err := s.GetActive(ctx, ref)
|
|
if err != nil {
|
|
t.Fatalf("get active: %v", err)
|
|
}
|
|
if active.Version != v2.Version || active.Fields[0].ExternalField != "Headline" {
|
|
t.Fatalf("active mapping = %+v, want v2 mit Headline", active)
|
|
}
|
|
}
|
|
|
|
func TestGetActive_UnknownRef(t *testing.T) {
|
|
s := newStore(t)
|
|
if _, err := s.GetActive(context.Background(), testRef(t)); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("err = %v, want ErrNotFound", err)
|
|
}
|
|
}
|
|
|
|
func contains(s, sub string) bool {
|
|
for i := 0; i+len(sub) <= len(s); i++ {
|
|
if s[i:i+len(sub)] == sub {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|