You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by di...@apache.org on 2026/05/24 22:20:27 UTC
(airavata-custos) branch master updated: Add AMIE connector integration test suite (#483)
This is an automated email from the ASF dual-hosted git repository.
DImuthuUpe pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata-custos.git
The following commit(s) were added to refs/heads/master by this push:
new f8fb89a52 Add AMIE connector integration test suite (#483)
f8fb89a52 is described below
commit f8fb89a52e554953f1a89148db442fcf8a256d78
Author: Lahiru Jayathilake <la...@gmail.com>
AuthorDate: Sun May 24 18:20:22 2026 -0400
Add AMIE connector integration test suite (#483)
---
.github/workflows/amie-integration-tests.yml | 63 +++
Makefile | 13 +
.../data_account_create_integration_test.go | 261 +++++++++
.../data_project_create_integration_test.go | 299 ++++++++++
...inform_transaction_complete_integration_test.go | 107 ++++
.../AMIE-Processor/handler/integration_common.go | 267 +++++++++
.../request_account_create_integration_test.go | 487 ++++++++++++++++
.../request_account_inactivate_integration_test.go | 338 +++++++++++
.../request_account_reactivate_integration_test.go | 235 ++++++++
.../request_person_merge_integration_test.go | 553 ++++++++++++++++++
.../request_project_create_integration_test.go | 344 ++++++++++++
.../request_project_inactivate_integration_test.go | 409 ++++++++++++++
.../request_project_reactivate_integration_test.go | 301 ++++++++++
.../request_user_modify_integration_test.go | 620 +++++++++++++++++++++
.../pipeline/baseline_integration_test.go | 108 ++++
.../AMIE-Processor/pipeline/integration_common.go | 290 ++++++++++
.../AMIE-Processor/testdata/scenarios/README.md | 153 +++++
.../testdata/scenarios/baseline.yaml | 197 +++++++
.../AMIE-Processor/worker/integration_common.go | 188 +++++++
.../worker/poller_integration_test.go | 210 +++++++
.../worker/processor_failures_integration_test.go | 134 +++++
.../worker/processor_integration_test.go | 290 ++++++++++
dev-ops/local-amie/Dockerfile.mock | 28 +
dev-ops/local-amie/Makefile | 32 ++
dev-ops/local-amie/README.md | 23 +
dev-ops/local-amie/compose.yaml | 56 ++
scripts/run-amie-integration-tests.sh | 40 ++
27 files changed, 6046 insertions(+)
diff --git a/.github/workflows/amie-integration-tests.yml b/.github/workflows/amie-integration-tests.yml
new file mode 100644
index 000000000..b6c78e3fb
--- /dev/null
+++ b/.github/workflows/amie-integration-tests.yml
@@ -0,0 +1,63 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://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.
+
+name: AMIE Integration Tests
+
+on:
+ pull_request:
+ paths:
+ - 'connectors/ACCESS/AMIE-Processor/**'
+ - 'dev-ops/local-amie/**'
+ - 'scripts/run-amie-integration-tests.sh'
+ - 'Makefile'
+ - '.github/workflows/amie-integration-tests.yml'
+ workflow_dispatch:
+
+jobs:
+ amie-integration:
+ name: Run AMIE integration suite
+ runs-on: ubuntu-latest
+ timeout-minutes: 20
+ steps:
+ - name: Checkout
+ uses: actions/checkout@v4
+
+ - name: Set up Go
+ uses: actions/setup-go@v5
+ with:
+ go-version: '1.24'
+ cache: true
+
+ - name: Verify Docker available
+ run: |
+ docker version
+ docker compose version
+
+ - name: Run integration suite
+ run: make integration-test-amie
+
+ - name: Dump test infra logs on failure
+ if: failure()
+ run: |
+ echo "--- mock-amie logs ---"
+ docker logs custos_amie_test_mock 2>&1 || true
+ echo "--- mariadb logs ---"
+ docker logs custos_amie_test_db 2>&1 || true
+
+ - name: Tear down stack
+ if: always()
+ run: make integration-test-amie-down || true
diff --git a/Makefile b/Makefile
new file mode 100644
index 000000000..cc758c242
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,13 @@
+SHELL := /bin/bash
+.PHONY: help integration-test-amie integration-test-amie-down
+
+help:
+ @echo "Targets:"
+ @echo " integration-test-amie run the AMIE connector integration suite end-to-end"
+ @echo " integration-test-amie-down tear down the AMIE test stack only"
+
+integration-test-amie:
+ bash scripts/run-amie-integration-tests.sh
+
+integration-test-amie-down:
+ $(MAKE) -C dev-ops/local-amie down
diff --git a/connectors/ACCESS/AMIE-Processor/handler/data_account_create_integration_test.go b/connectors/ACCESS/AMIE-Processor/handler/data_account_create_integration_test.go
new file mode 100644
index 000000000..fb670af19
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/handler/data_account_create_integration_test.go
@@ -0,0 +1,261 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package handler
+
+import (
+ "context"
+ "database/sql"
+ "errors"
+ "testing"
+
+ "github.com/jmoiron/sqlx"
+
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/model"
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/store"
+ "github.com/apache/airavata-custos/pkg/models"
+)
+
+const (
+ dacExistingDN = "CN=User One Existing,O=Baseline Org,C=US"
+ dacNewDN1 = "CN=User One New 1,O=Baseline Org,C=US"
+ dacNewDN2 = "CN=User One New 2,O=Baseline Org,C=US"
+)
+
+// baseDataAccountCreateBody returns a fully populated data_account_create
+// body. AMIE ships GlobalID and DnList in this packet, the handler reads
+// GlobalID (not PersonID) to find the local user.
+func baseDataAccountCreateBody() map[string]any {
+ return map[string]any{
+ "PersonID": "site-user-001",
+ "ProjectID": "TG-BL-001",
+ "GlobalID": "bl-user-001",
+ "DnList": []any{
+ dacExistingDN, // duplicate; INSERT IGNORE keeps the existing row
+ dacNewDN1,
+ dacNewDN2,
+ },
+ }
+}
+
+// seedUserWithIdentity creates an Organization + User + UserIdentity (source
+// "access", externalID = userGlobalID) so the handler's lookup succeeds.
+// Returns the created user ID.
+func seedUserWithIdentity(t *testing.T, database *sqlx.DB, userGlobalID string) string {
+ t.Helper()
+ svc := newTestCoreService(database)
+ ctx := context.Background()
+
+ org, err := svc.CreateOrganization(ctx, &models.Organization{
+ OriginatedID: "BASELINE",
+ Name: "Baseline Org",
+ })
+ if err != nil {
+ t.Fatalf("seed organization: %v", err)
+ }
+ user, err := svc.CreateUser(ctx, &models.User{
+ OrganizationID: org.ID,
+ FirstName: "User",
+ LastName: "One",
+ Email: "user.one@baseline.example.edu",
+ })
+ if err != nil {
+ t.Fatalf("seed user: %v", err)
+ }
+ if _, err := svc.CreateUserIdentity(ctx, &models.UserIdentity{
+ UserID: user.ID,
+ Source: "access",
+ ExternalID: userGlobalID,
+ Email: "user.one@baseline.example.edu",
+ }); err != nil {
+ t.Fatalf("seed user identity: %v", err)
+ }
+ return user.ID
+}
+
+// newDataAccountCreateHandlerForTest assembles the handler with real wiring
+// (real userDN store, fakeAmieClient).
+func newDataAccountCreateHandlerForTest(database *sqlx.DB, amie *fakeAmieClient) *DataAccountCreateHandler {
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ dnStore := store.NewUserDNStore(database)
+ return NewDataAccountCreateHandler(svc, dnStore, amie, audit)
+}
+
+// TestDataAccountCreate_HappyPath verifies the happy path for a packet whose
+// user already exists locally (the normal flow: data_account_create follows
+// notify_account_create, which created the user).
+// - persist each DN in DnList against the user (additive, no pruning)
+// - reply inform_transaction_complete
+// - audit PERSIST_DNS and REPLY_SENT
+func TestDataAccountCreate_HappyPath(t *testing.T) {
+ database := setupTestDB(t)
+
+ body := baseDataAccountCreateBody()
+ pkt := insertPacket(t, database, "data_account_create", body)
+
+ userID := seedUserWithIdentity(t, database, "bl-user-001")
+ seedExistingDN(t, database, userID, dacExistingDN)
+
+ amie := &fakeAmieClient{}
+ h := newDataAccountCreateHandlerForTest(database, amie)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ // Additive: existing DN preserved, two new DNs added, duplicate ignored.
+ if got, want := countDNsForUser(t, database, userID), 3; got != want {
+ t.Errorf("amie_user_dns for user: got %d, want %d", got, want)
+ }
+ if got := countRows(t, database, "amie_user_dns"); got != 3 {
+ t.Errorf("amie_user_dns total: got %d, want 3 (1 existing + 2 new, dup ignored)", got)
+ }
+
+ if got := countAuditActions(t, database, pkt.ID, model.AuditPersistDNs); got != 1 {
+ t.Errorf("audit PERSIST_DNS: got %d, want 1", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT: got %d, want 1", got)
+ }
+
+ // Reply contract: type must be inform_transaction_complete with the
+ // required body fields (DetailCode, Message, StatusCode).
+ if got, want := amie.lastReplyType(), "inform_transaction_complete"; got != want {
+ t.Fatalf("reply type: got %q, want %q", got, want)
+ }
+ reply := amie.lastReplyBody()
+ if reply == nil {
+ t.Fatal("reply body missing")
+ }
+ if got, _ := reply["StatusCode"].(string); got != "Success" {
+ t.Errorf("reply.StatusCode: got %q, want Success", got)
+ }
+ if _, ok := reply["DetailCode"]; !ok {
+ t.Errorf("reply.DetailCode: missing; required by reply contract")
+ }
+ if _, ok := reply["Message"]; !ok {
+ t.Errorf("reply.Message: missing; required by reply contract")
+ }
+}
+
+// TestDataAccountCreate_UnknownUser asserts the handler's soft-skip path
+// when no local user matches the packet's GlobalID.
+func TestDataAccountCreate_UnknownUser(t *testing.T) {
+ database := setupTestDB(t)
+
+ body := baseDataAccountCreateBody()
+ body["GlobalID"] = "bl-user-unknown"
+ pkt := insertPacket(t, database, "data_account_create", body)
+
+ amie := &fakeAmieClient{}
+ h := newDataAccountCreateHandlerForTest(database, amie)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ if got := countRows(t, database, "amie_user_dns"); got != 0 {
+ t.Errorf("amie_user_dns on unknown user: got %d, want 0", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditPersistDNs); got != 0 {
+ t.Errorf("PERSIST_DNS on unknown user: got %d, want 0", got)
+ }
+ if got, want := amie.lastReplyType(), "inform_transaction_complete"; got != want {
+ t.Errorf("reply type: got %q, want %q", got, want)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT on unknown user: got %d, want 1", got)
+ }
+}
+
+// TestDataAccountCreate_EmptyDnList asserts that when the packet carries no
+// DNs, the handler skips PERSIST_DNS but still completes the transaction
+// with an inform_transaction_complete reply. DnList is allowed but not
+// required.
+func TestDataAccountCreate_EmptyDnList(t *testing.T) {
+ database := setupTestDB(t)
+
+ body := baseDataAccountCreateBody()
+ delete(body, "DnList")
+ pkt := insertPacket(t, database, "data_account_create", body)
+
+ userID := seedUserWithIdentity(t, database, "bl-user-001")
+ seedExistingDN(t, database, userID, dacExistingDN)
+
+ amie := &fakeAmieClient{}
+ h := newDataAccountCreateHandlerForTest(database, amie)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ if got, want := countDNsForUser(t, database, userID), 1; got != want {
+ t.Errorf("amie_user_dns: got %d, want %d (existing row preserved)", got, want)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditPersistDNs); got != 0 {
+ t.Errorf("PERSIST_DNS on empty DnList: got %d, want 0", got)
+ }
+ if got, want := amie.lastReplyType(), "inform_transaction_complete"; got != want {
+ t.Errorf("reply type: got %q, want %q", got, want)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT: got %d, want 1", got)
+ }
+}
+
+// TestDataAccountCreate_ReplyFailurePropagates asserts that a failed reply
+// surfaces as an error to the caller and that REPLY_SENT is NOT audited.
+// The transaction does not complete until the handler successfully delivers
+// inform_transaction_complete.
+func TestDataAccountCreate_ReplyFailurePropagates(t *testing.T) {
+ database := setupTestDB(t)
+
+ body := baseDataAccountCreateBody()
+ pkt := insertPacket(t, database, "data_account_create", body)
+
+ _ = seedUserWithIdentity(t, database, "bl-user-001")
+
+ amie := &fakeAmieClient{FailWith: errors.New("simulated AMIE outage")}
+ h := newDataAccountCreateHandlerForTest(database, amie)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error when reply fails, got nil")
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("REPLY_SENT on failed reply: got %d, want 0", got)
+ }
+ // See B11: DN writes + PERSIST_DNS share the handler's tx, so on reply
+ // failure the tx rolls back.
+ if got := countRows(t, database, "amie_user_dns"); got != 0 {
+ t.Errorf("amie_user_dns after rollback: got %d, want 0", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditPersistDNs); got != 0 {
+ t.Errorf("PERSIST_DNS after rollback: got %d, want 0", got)
+ }
+}
diff --git a/connectors/ACCESS/AMIE-Processor/handler/data_project_create_integration_test.go b/connectors/ACCESS/AMIE-Processor/handler/data_project_create_integration_test.go
new file mode 100644
index 000000000..341edf40f
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/handler/data_project_create_integration_test.go
@@ -0,0 +1,299 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package handler
+
+import (
+ "context"
+ "database/sql"
+ "errors"
+ "testing"
+
+ "github.com/jmoiron/sqlx"
+
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/model"
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/store"
+ "github.com/apache/airavata-custos/pkg/models"
+ coreservice "github.com/apache/airavata-custos/pkg/service"
+)
+
+const (
+ dpcExistingDN = "CN=Pat First Existing,O=Baseline Org,C=US"
+ dpcNewDN1 = "CN=Pat First New 1,O=Baseline Org,C=US"
+ dpcNewDN2 = "CN=Pat First New 2,O=Baseline Org,C=US"
+)
+
+// baseDataProjectCreateBody returns a fully populated data_project_create
+// body. AMIE ships GlobalID and DnList in this packet; the handler reads
+// GlobalID (not PersonID) to find the local user.
+func baseDataProjectCreateBody() map[string]any {
+ return map[string]any{
+ "PersonID": "site-pi-001",
+ "ProjectID": "TG-BL-001",
+ "GlobalID": "bl-pi-001",
+ "DnList": []any{
+ dpcExistingDN, // duplicate of seeded row; INSERT IGNORE keeps one
+ dpcNewDN1,
+ dpcNewDN2,
+ },
+ }
+}
+
+// seedPIWithIdentity creates an Organization + User + UserIdentity (source
+// "access", externalID = piGlobalID) so the handler's lookup succeeds.
+// Returns the created user ID.
+func seedPIWithIdentity(t *testing.T, database *sqlx.DB, piGlobalID string) string {
+ t.Helper()
+ svc := newTestCoreService(database)
+ ctx := context.Background()
+
+ org, err := svc.CreateOrganization(ctx, &models.Organization{
+ OriginatedID: "BASELINE",
+ Name: "Baseline Org",
+ })
+ if err != nil {
+ t.Fatalf("seed organization: %v", err)
+ }
+ user, err := svc.CreateUser(ctx, &models.User{
+ OrganizationID: org.ID,
+ FirstName: "Pat",
+ LastName: "First",
+ Email: "pat.first@baseline.example.edu",
+ })
+ if err != nil {
+ t.Fatalf("seed user: %v", err)
+ }
+ if _, err := svc.CreateUserIdentity(ctx, &models.UserIdentity{
+ UserID: user.ID,
+ Source: "access",
+ ExternalID: piGlobalID,
+ Email: "pat.first@baseline.example.edu",
+ }); err != nil {
+ t.Fatalf("seed user identity: %v", err)
+ }
+ return user.ID
+}
+
+// seedExistingDN inserts an amie_user_dns row outside any handler-managed tx
+// so we can later assert that the handler preserved it.
+func seedExistingDN(t *testing.T, database *sqlx.DB, userID, dn string) {
+ t.Helper()
+ tx, err := database.BeginTx(context.Background(), nil)
+ if err != nil {
+ t.Fatalf("begin tx for DN seed: %v", err)
+ }
+ if err := store.NewUserDNStore(database).Add(
+ context.Background(), tx, &model.UserDN{UserID: userID, DN: dn},
+ ); err != nil {
+ _ = tx.Rollback()
+ t.Fatalf("seed existing DN: %v", err)
+ }
+ if err := tx.Commit(); err != nil {
+ t.Fatalf("commit DN seed: %v", err)
+ }
+}
+
+// countDNsForUser is a convenience over the user_dn store for assertions.
+func countDNsForUser(t *testing.T, database *sqlx.DB, userID string) int {
+ t.Helper()
+ dns, err := store.NewUserDNStore(database).ListByUser(context.Background(), userID)
+ if err != nil {
+ t.Fatalf("list DNs: %v", err)
+ }
+ return len(dns)
+}
+
+// newDataProjectCreateHandlerForTest assembles the handler with real wiring
+// (real userDN store, fakeAmieClient).
+func newDataProjectCreateHandlerForTest(database *sqlx.DB, amie *fakeAmieClient) (*DataProjectCreateHandler, *coreservice.Service) {
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ dnStore := store.NewUserDNStore(database)
+ return NewDataProjectCreateHandler(svc, dnStore, amie, audit), svc
+}
+
+// TestDataProjectCreate_HappyPath verifies the happy path for a packet
+// whose PI already exists locally (the normal flow: data_project_create
+// follows notify_project_create, which created the PI). The handler MUST:
+// - persist each DN in DnList against the PI user (additive, no pruning)
+// - reply inform_transaction_complete
+// - audit PERSIST_DNS and REPLY_SENT
+func TestDataProjectCreate_HappyPath(t *testing.T) {
+ database := setupTestDB(t)
+
+ body := baseDataProjectCreateBody()
+ pkt := insertPacket(t, database, "data_project_create", body)
+
+ userID := seedPIWithIdentity(t, database, "bl-pi-001")
+ seedExistingDN(t, database, userID, dpcExistingDN)
+
+ amie := &fakeAmieClient{}
+ h, _ := newDataProjectCreateHandlerForTest(database, amie)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ // Additive write: the existing DN must still be present; the two new DNs
+ // must be added. Duplicate of existing DN is a silent no-op (INSERT IGNORE).
+ if got, want := countDNsForUser(t, database, userID), 3; got != want {
+ t.Errorf("amie_user_dns for PI: got %d, want %d", got, want)
+ }
+ if got := countRows(t, database, "amie_user_dns"); got != 3 {
+ t.Errorf("amie_user_dns total: got %d, want 3 (1 existing + 2 new, dup ignored)", got)
+ }
+
+ // PERSIST_DNS audit row: handler emits exactly one summary row for the
+ // batch (see data_project_create.go).
+ if got := countAuditActions(t, database, pkt.ID, model.AuditPersistDNs); got != 1 {
+ t.Errorf("audit PERSIST_DNS: got %d, want 1", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT: got %d, want 1", got)
+ }
+
+ // Reply contract: type must be inform_transaction_complete with the
+ // required fields (DetailCode, Message, StatusCode).
+ if got, want := amie.lastReplyType(), "inform_transaction_complete"; got != want {
+ t.Fatalf("reply type: got %q, want %q", got, want)
+ }
+ reply := amie.lastReplyBody()
+ if reply == nil {
+ t.Fatal("reply body missing")
+ }
+ if got, _ := reply["StatusCode"].(string); got != "Success" {
+ t.Errorf("reply.StatusCode: got %q, want Success", got)
+ }
+ if _, ok := reply["DetailCode"]; !ok {
+ t.Errorf("reply.DetailCode: missing; required by reply contract")
+ }
+ if _, ok := reply["Message"]; !ok {
+ t.Errorf("reply.Message: missing; required by reply contract")
+ }
+}
+
+// TestDataProjectCreate_UnknownUser asserts the handler's soft-skip path
+// when no local user matches the packet's GlobalID. The reply MUST still
+// be sent (the handler closes the transaction regardless), but no DN rows and
+// no PERSIST_DNS audit row.
+func TestDataProjectCreate_UnknownUser(t *testing.T) {
+ database := setupTestDB(t)
+
+ body := baseDataProjectCreateBody()
+ body["GlobalID"] = "bl-pi-unknown"
+ pkt := insertPacket(t, database, "data_project_create", body)
+
+ amie := &fakeAmieClient{}
+ h, _ := newDataProjectCreateHandlerForTest(database, amie)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ if got := countRows(t, database, "amie_user_dns"); got != 0 {
+ t.Errorf("amie_user_dns on unknown user: got %d, want 0", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditPersistDNs); got != 0 {
+ t.Errorf("PERSIST_DNS on unknown user: got %d, want 0", got)
+ }
+ // Reply contract still holds: the handler closes the transaction.
+ if got, want := amie.lastReplyType(), "inform_transaction_complete"; got != want {
+ t.Errorf("reply type: got %q, want %q", got, want)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT on unknown user: got %d, want 1", got)
+ }
+}
+
+// TestDataProjectCreate_EmptyDnList asserts that when the packet carries no
+// DNs (DnList absent or empty), the handler skips PERSIST_DNS but still
+// completes the transaction with an inform_transaction_complete reply.
+// DnList is allowed but not required; missing/empty is valid.
+func TestDataProjectCreate_EmptyDnList(t *testing.T) {
+ database := setupTestDB(t)
+
+ body := baseDataProjectCreateBody()
+ delete(body, "DnList")
+ pkt := insertPacket(t, database, "data_project_create", body)
+
+ userID := seedPIWithIdentity(t, database, "bl-pi-001")
+ seedExistingDN(t, database, userID, dpcExistingDN)
+
+ amie := &fakeAmieClient{}
+ h, _ := newDataProjectCreateHandlerForTest(database, amie)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ // "Additive, no pruning": the existing DN must NOT be touched when DnList
+ // is empty.
+ if got, want := countDNsForUser(t, database, userID), 1; got != want {
+ t.Errorf("amie_user_dns: got %d, want %d (existing row preserved)", got, want)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditPersistDNs); got != 0 {
+ t.Errorf("PERSIST_DNS on empty DnList: got %d, want 0", got)
+ }
+ if got, want := amie.lastReplyType(), "inform_transaction_complete"; got != want {
+ t.Errorf("reply type: got %q, want %q", got, want)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT: got %d, want 1", got)
+ }
+}
+
+// TestDataProjectCreate_ReplyFailurePropagates asserts that a failed reply
+// surfaces as an error to the caller and that REPLY_SENT is NOT audited.
+// The transaction does not complete until the handler successfully delivers
+// inform_transaction_complete.
+func TestDataProjectCreate_ReplyFailurePropagates(t *testing.T) {
+ database := setupTestDB(t)
+
+ body := baseDataProjectCreateBody()
+ pkt := insertPacket(t, database, "data_project_create", body)
+
+ _ = seedPIWithIdentity(t, database, "bl-pi-001")
+
+ amie := &fakeAmieClient{FailWith: errors.New("simulated AMIE outage")}
+ h, _ := newDataProjectCreateHandlerForTest(database, amie)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error when reply fails, got nil")
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("REPLY_SENT on failed reply: got %d, want 0", got)
+ }
+ // DN writes and PERSIST_DNS share the handler's tx, so on reply failure
+ // the whole tx rolls back and neither row should exist.
+ if got := countRows(t, database, "amie_user_dns"); got != 0 {
+ t.Errorf("amie_user_dns after rollback: got %d, want 0", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditPersistDNs); got != 0 {
+ t.Errorf("PERSIST_DNS after rollback: got %d, want 0", got)
+ }
+}
diff --git a/connectors/ACCESS/AMIE-Processor/handler/inform_transaction_complete_integration_test.go b/connectors/ACCESS/AMIE-Processor/handler/inform_transaction_complete_integration_test.go
new file mode 100644
index 000000000..17689867c
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/handler/inform_transaction_complete_integration_test.go
@@ -0,0 +1,107 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package handler
+
+import (
+ "context"
+ "database/sql"
+ "testing"
+
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/model"
+)
+
+// baseInformTransactionCompleteBody returns a well-formed ITC body. Required
+// fields per the protocol: DetailCode, Message, StatusCode.
+func baseInformTransactionCompleteBody() map[string]any {
+ return map[string]any{
+ "StatusCode": "Success",
+ "DetailCode": "1",
+ "Message": "Transaction completed successfully.",
+ }
+}
+
+// TestInformTransactionComplete_HappyPath verifies the ITC contract:
+// - the handler records the arrival via a TRANSACTION_COMPLETE audit row
+// - NO reply is sent (ITC is terminal; the only packet type with no reply)
+// - no domain mutations
+func TestInformTransactionComplete_HappyPath(t *testing.T) {
+ database := setupTestDB(t)
+
+ body := baseInformTransactionCompleteBody()
+ pkt := insertPacket(t, database, "inform_transaction_complete", body)
+
+ audit := newTestAuditService(database)
+ h := NewInformTransactionCompleteHandler(audit)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ if got := countAuditActions(t, database, pkt.ID, model.AuditTransactionComplete); got != 1 {
+ t.Errorf("audit TRANSACTION_COMPLETE: got %d, want 1", got)
+ }
+ // No reply expected. The handler doesn't take an AmieClient, that alone
+ // proves the design, but assert REPLY_SENT is absent too for regression
+ // safety in case the handler shape changes later.
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("REPLY_SENT on terminal ITC: got %d, want 0 (no reply allowed)", got)
+ }
+
+ // No domain mutations, assert no domain rows were created.
+ for _, table := range []string{
+ "users", "user_identities", "organizations",
+ "projects", "compute_allocations", "amie_user_dns",
+ } {
+ if got := countRows(t, database, table); got != 0 {
+ t.Errorf("%s after ITC: got %d, want 0 (ITC has no domain writes)", table, got)
+ }
+ }
+}
+
+// TestInformTransactionComplete_MissingBody asserts that a packet with no
+// "body" key is rejected by the handler (`getBody` returns an error). A
+// well-formed ITC requires DetailCode/Message/StatusCode; a packet without
+// a body cannot satisfy them.
+func TestInformTransactionComplete_MissingBody(t *testing.T) {
+ database := setupTestDB(t)
+
+ // Persist a packet whose RawJSON has no "body" key. We hand-build the
+ // packetJSON map the same way: missing "body".
+ body := baseInformTransactionCompleteBody()
+ pkt := insertPacket(t, database, "inform_transaction_complete", body)
+
+ audit := newTestAuditService(database)
+ h := NewInformTransactionCompleteHandler(audit)
+
+ packetJSON := map[string]any{"type": pkt.Type}
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, packetJSON, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error for missing body, got nil")
+ }
+
+ // No audit row should be written when body validation fails.
+ if got := countAuditActions(t, database, pkt.ID, model.AuditTransactionComplete); got != 0 {
+ t.Errorf("TRANSACTION_COMPLETE on missing body: got %d, want 0", got)
+ }
+}
diff --git a/connectors/ACCESS/AMIE-Processor/handler/integration_common.go b/connectors/ACCESS/AMIE-Processor/handler/integration_common.go
new file mode 100644
index 000000000..28fd8a4ad
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/handler/integration_common.go
@@ -0,0 +1,267 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package handler
+
+import (
+ "context"
+ "database/sql"
+ "encoding/json"
+ "os"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/google/uuid"
+ "github.com/jmoiron/sqlx"
+
+ amiedb "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/db"
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/model"
+ amieservice "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/service"
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/store"
+ "github.com/apache/airavata-custos/internal/db"
+ "github.com/apache/airavata-custos/pkg/events"
+ coreservice "github.com/apache/airavata-custos/pkg/service"
+)
+
+const testClusterID = "00000000-0000-0000-0000-000000000001"
+
+var (
+ sharedDB *sqlx.DB
+ sharedDBOnce sync.Once
+ sharedDBErr error
+)
+
+func isLocalAMIEConfigAvailable() bool {
+ for _, key := range []string{
+ "DATABASE_DSN",
+ "AMIE_BASE_URL",
+ "AMIE_SITE_CODE",
+ "AMIE_API_KEY",
+ "AMIE_CLUSTER_ID",
+ } {
+ if os.Getenv(key) == "" {
+ return false
+ }
+ }
+ return true
+}
+
+// setupTestDB opens the DB once per process; subsequent calls re-truncate
+// and re-seed but reuse the same connection.
+func setupTestDB(t *testing.T) *sqlx.DB {
+ t.Helper()
+ if !isLocalAMIEConfigAvailable() {
+ t.Skip("integration env not set; run via scripts/run-amie-integration-tests.sh")
+ }
+ sharedDBOnce.Do(func() {
+ database, err := db.Open(db.Config{
+ DSN: os.Getenv("DATABASE_DSN"),
+ MaxOpenConns: 5,
+ MaxIdleConns: 2,
+ })
+ if err != nil {
+ sharedDBErr = err
+ return
+ }
+ if err := db.MigrateEmbedded(database); err != nil {
+ sharedDBErr = err
+ return
+ }
+ if err := db.MigrateConnectorFS(database, amiedb.MigrationFS(), "migrations", "amie"); err != nil {
+ sharedDBErr = err
+ return
+ }
+ sharedDB = database
+ })
+ if sharedDBErr != nil {
+ t.Fatalf("setup db: %v", sharedDBErr)
+ }
+ truncateAll(t, sharedDB)
+ seedDefaultCluster(t, sharedDB)
+ return sharedDB
+}
+
+// truncateAll disables FK checks so we don't have to order the table list.
+func truncateAll(t *testing.T, database *sqlx.DB) {
+ t.Helper()
+ tables := []string{
+ "amie_audit_log",
+ "amie_processing_errors",
+ "amie_processing_events",
+ "amie_packets",
+ "amie_user_dns",
+ "compute_allocation_membership_resource_overrides",
+ "compute_allocation_usages",
+ "compute_allocation_memberships",
+ "compute_allocation_change_request_events",
+ "compute_allocation_change_requests",
+ "compute_allocation_diffs",
+ "compute_allocation_resource_rates",
+ "compute_allocation_resource_mappings",
+ "compute_allocation_resources",
+ "compute_allocations",
+ "compute_cluster_users",
+ "compute_clusters",
+ "projects",
+ "user_identities",
+ "users",
+ "organizations",
+ }
+ if _, err := database.Exec("SET FOREIGN_KEY_CHECKS = 0"); err != nil {
+ t.Fatalf("disable FK: %v", err)
+ }
+ for _, tbl := range tables {
+ if _, err := database.Exec("TRUNCATE TABLE " + tbl); err != nil {
+ t.Fatalf("truncate %s: %v", tbl, err)
+ }
+ }
+ if _, err := database.Exec("SET FOREIGN_KEY_CHECKS = 1"); err != nil {
+ t.Fatalf("re-enable FK: %v", err)
+ }
+}
+
+// seedDefaultCluster inserts the row AMIE_CLUSTER_ID points at. Handlers
+// refuse to run without it, and FKs on compute_allocations /
+// compute_cluster_users require it.
+func seedDefaultCluster(t *testing.T, database *sqlx.DB) {
+ t.Helper()
+ if _, err := database.Exec(
+ "INSERT INTO compute_clusters (id, name) VALUES (?, ?)",
+ testClusterID, "default-cluster",
+ ); err != nil {
+ t.Fatalf("seed cluster: %v", err)
+ }
+}
+
+func newTestCoreService(database *sqlx.DB) *coreservice.Service {
+ return coreservice.New(database, events.New())
+}
+
+func newTestAuditService(database *sqlx.DB) *amieservice.AuditService {
+ return amieservice.NewAuditService(store.NewAuditStore(database))
+}
+
+type fakeReply struct {
+ PacketRecID int64
+ Body map[string]any
+}
+
+// fakeAmieClient captures replies in-process. Set FailWith to simulate a
+// failed reply send.
+type fakeAmieClient struct {
+ mu sync.Mutex
+ Replies []fakeReply
+ FailWith error
+}
+
+func (f *fakeAmieClient) ReplyToPacket(_ context.Context, packetRecID int64, reply map[string]any) error {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ if f.FailWith != nil {
+ return f.FailWith
+ }
+ f.Replies = append(f.Replies, fakeReply{PacketRecID: packetRecID, Body: reply})
+ return nil
+}
+
+func (f *fakeAmieClient) lastReplyBody() map[string]any {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ if len(f.Replies) == 0 {
+ return nil
+ }
+ last := f.Replies[len(f.Replies)-1]
+ body, _ := last.Body["body"].(map[string]any)
+ return body
+}
+
+func (f *fakeAmieClient) lastReplyType() string {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ if len(f.Replies) == 0 {
+ return ""
+ }
+ last := f.Replies[len(f.Replies)-1]
+ t, _ := last.Body["type"].(string)
+ return t
+}
+
+func insertPacket(t *testing.T, database *sqlx.DB, packetType string, body map[string]any) *model.Packet {
+ t.Helper()
+ raw, err := json.Marshal(map[string]any{"type": packetType, "body": body})
+ if err != nil {
+ t.Fatalf("marshal packet: %v", err)
+ }
+ pkt := &model.Packet{
+ ID: uuid.NewString(),
+ AmieID: time.Now().UnixNano(),
+ Type: packetType,
+ Status: model.PacketStatusNew,
+ RawJSON: string(raw),
+ ReceivedAt: time.Now().UTC(),
+ }
+ tx, err := database.BeginTxx(context.Background(), nil)
+ if err != nil {
+ t.Fatalf("begin tx: %v", err)
+ }
+ if err := store.NewPacketStore(database).Save(context.Background(), tx.Tx, pkt); err != nil {
+ _ = tx.Rollback()
+ t.Fatalf("save packet: %v", err)
+ }
+ if err := tx.Commit(); err != nil {
+ t.Fatalf("commit packet: %v", err)
+ }
+ return pkt
+}
+
+func runHandlerInTx(t *testing.T, database *sqlx.DB, fn func(ctx context.Context, tx *sql.Tx) error) error {
+ t.Helper()
+ ctx := context.Background()
+ tx, err := database.BeginTx(ctx, nil)
+ if err != nil {
+ return err
+ }
+ if err := fn(ctx, tx); err != nil {
+ _ = tx.Rollback()
+ return err
+ }
+ return tx.Commit()
+}
+
+func countRows(t *testing.T, database *sqlx.DB, table string) int {
+ t.Helper()
+ var n int
+ if err := database.Get(&n, "SELECT COUNT(*) FROM "+table); err != nil {
+ t.Fatalf("count %s: %v", table, err)
+ }
+ return n
+}
+
+func countAuditActions(t *testing.T, database *sqlx.DB, packetID string, action model.AuditAction) int {
+ t.Helper()
+ var n int
+ if err := database.Get(&n,
+ "SELECT COUNT(*) FROM amie_audit_log WHERE packet_id = ? AND action = ?",
+ packetID, string(action),
+ ); err != nil {
+ t.Fatalf("count audit %s: %v", action, err)
+ }
+ return n
+}
diff --git a/connectors/ACCESS/AMIE-Processor/handler/request_account_create_integration_test.go b/connectors/ACCESS/AMIE-Processor/handler/request_account_create_integration_test.go
new file mode 100644
index 000000000..ceae14873
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/handler/request_account_create_integration_test.go
@@ -0,0 +1,487 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package handler
+
+import (
+ "context"
+ "database/sql"
+ "errors"
+ "testing"
+
+ "github.com/google/uuid"
+ "github.com/jmoiron/sqlx"
+
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/model"
+)
+
+// baseRACBody returns a fully populated request_account_create body with
+// every required field (GrantNumber, ResourceList, UserFirstName,
+// UserLastName, UserOrganization, UserOrgCode). UserGlobalID is optional
+// per the protocol but is the only stable upstream identifier for user
+// resolution, so the baseline body carries it; the omission case is
+// exercised by the missing-field test.
+func baseRACBody() map[string]any {
+ return map[string]any{
+ "GrantNumber": "TG-BL-001",
+ "ResourceList": []any{"compute.access-ci.org"},
+ "UserFirstName": "Sam",
+ "UserLastName": "Userman",
+ "UserOrganization": "User Org",
+ "UserOrgCode": "USERORG",
+ "UserGlobalID": "bl-user-001",
+ "UserEmail": "sam.userman@user.example.edu",
+ }
+}
+
+// seedProjectForRAC runs request_project_create against a fresh DB so that
+// request_account_create has a project + allocation to attach to. Returns
+// the project's Custos UUID, which AMIE echoes back as body.ProjectID on
+// the subsequent request_account_create (the handler answered
+// notify_project_create with this ID).
+func seedProjectForRAC(t *testing.T, database *sqlx.DB) string {
+ t.Helper()
+ rpcBody := baseRPCBody()
+ rpcPkt := insertPacket(t, database, "request_project_create", rpcBody)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestProjectCreateHandler(svc, testClusterID, amie, audit)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": rpcPkt.Type, "body": rpcBody}, rpcPkt, "")
+ }); err != nil {
+ t.Fatalf("seed request_project_create: %v", err)
+ }
+
+ var projectID string
+ if err := database.Get(&projectID, "SELECT id FROM projects LIMIT 1"); err != nil {
+ t.Fatalf("read seeded project id: %v", err)
+ }
+ return projectID
+}
+
+// TestRequestAccountCreate_HappyPath asserts the spec-required side effects
+// of a first-delivery request_account_create with a complete body, after a
+// successful prior request_project_create has provisioned the project +
+// allocation.
+//
+// The handler MUST:
+// - persist the user as a local Person (User + UserIdentity)
+// - look up the project (already created by request_project_create)
+// - provision a ComputeClusterUser on the configured cluster
+// - attach a ComputeAllocationMembership linking the user to the project's
+// existing allocation
+// - reply notify_account_create with the required fields: ProjectID,
+// ResourceList, UserRemoteSiteLogin, AccountActivityTime
+func TestRequestAccountCreate_HappyPath(t *testing.T) {
+ database := setupTestDB(t)
+ projectID := seedProjectForRAC(t, database)
+
+ // Counts established by the seed RPC so we can assert the RAC's deltas.
+ usersAfterSeed := countRows(t, database, "users")
+ identitiesAfterSeed := countRows(t, database, "user_identities")
+ clusterUsersAfterSeed := countRows(t, database, "compute_cluster_users")
+ membershipsAfterSeed := countRows(t, database, "compute_allocation_memberships")
+ projectsAfterSeed := countRows(t, database, "projects")
+ allocationsAfterSeed := countRows(t, database, "compute_allocations")
+
+ body := baseRACBody()
+ body["ProjectID"] = projectID
+ pkt := insertPacket(t, database, "request_account_create", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestAccountCreateHandler(svc, testClusterID, amie, audit)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ // Identity-domain writes: one new User + one new UserIdentity for the
+ // account user (PI was created by the seed RPC).
+ if got, want := countRows(t, database, "users"), usersAfterSeed+1; got != want {
+ t.Errorf("users: got %d, want %d", got, want)
+ }
+ if got, want := countRows(t, database, "user_identities"), identitiesAfterSeed+1; got != want {
+ t.Errorf("user_identities: got %d, want %d", got, want)
+ }
+ // Project + allocation untouched by RAC.
+ if got, want := countRows(t, database, "projects"), projectsAfterSeed; got != want {
+ t.Errorf("projects: got %d, want %d (RAC must not create projects)", got, want)
+ }
+ if got, want := countRows(t, database, "compute_allocations"), allocationsAfterSeed; got != want {
+ t.Errorf("compute_allocations: got %d, want %d (RAC must not create allocations)", got, want)
+ }
+ // Allocation-domain writes: one new ComputeClusterUser + one new
+ // ComputeAllocationMembership for this user.
+ if got, want := countRows(t, database, "compute_cluster_users"), clusterUsersAfterSeed+1; got != want {
+ t.Errorf("compute_cluster_users: got %d, want %d", got, want)
+ }
+ if got, want := countRows(t, database, "compute_allocation_memberships"), membershipsAfterSeed+1; got != want {
+ t.Errorf("compute_allocation_memberships: got %d, want %d", got, want)
+ }
+
+ // Inspect the freshly created user row (the one bound to UserGlobalID).
+ var user struct {
+ ID string `db:"id"`
+ FirstName string `db:"first_name"`
+ LastName string `db:"last_name"`
+ Email string `db:"email"`
+ Status string `db:"status"`
+ }
+ if err := database.Get(&user,
+ `SELECT u.id, u.first_name, u.last_name, u.email, u.status
+ FROM users u
+ JOIN user_identities ui ON ui.user_id = u.id
+ WHERE ui.source = ? AND ui.external_id = ?`,
+ "access", "bl-user-001",
+ ); err != nil {
+ t.Fatalf("read account user: %v", err)
+ }
+ if user.FirstName != "Sam" || user.LastName != "Userman" {
+ t.Errorf("user name: got %q %q, want Sam Userman", user.FirstName, user.LastName)
+ }
+ if user.Email != "sam.userman@user.example.edu" {
+ t.Errorf("user.email: got %q", user.Email)
+ }
+ if user.Status != "ACTIVE" {
+ t.Errorf("user.status: got %q, want ACTIVE", user.Status)
+ }
+
+ // ComputeClusterUser must be on the configured cluster and bound to the
+ // account user.
+ var cu struct {
+ UserID string `db:"user_id"`
+ ComputeClusterID string `db:"compute_cluster_id"`
+ LocalUsername string `db:"local_username"`
+ }
+ if err := database.Get(&cu,
+ "SELECT user_id, compute_cluster_id, local_username FROM compute_cluster_users WHERE user_id = ?",
+ user.ID,
+ ); err != nil {
+ t.Fatalf("read compute_cluster_user: %v", err)
+ }
+ if cu.ComputeClusterID != testClusterID {
+ t.Errorf("compute_cluster_user.compute_cluster_id: got %q, want %q", cu.ComputeClusterID, testClusterID)
+ }
+ if cu.LocalUsername == "" {
+ t.Errorf("compute_cluster_user.local_username: empty; spec requires UserRemoteSiteLogin in reply")
+ }
+
+ // Membership row links the account user to the project's existing
+ // allocation.
+ var mem struct {
+ UserID string `db:"user_id"`
+ ComputeAllocationID string `db:"compute_allocation_id"`
+ }
+ if err := database.Get(&mem,
+ "SELECT user_id, compute_allocation_id FROM compute_allocation_memberships WHERE user_id = ?",
+ user.ID,
+ ); err != nil {
+ t.Fatalf("read membership: %v", err)
+ }
+ var allocID string
+ if err := database.Get(&allocID,
+ "SELECT id FROM compute_allocations WHERE project_id = ? LIMIT 1", projectID,
+ ); err != nil {
+ t.Fatalf("read allocation id: %v", err)
+ }
+ if mem.ComputeAllocationID != allocID {
+ t.Errorf("membership.compute_allocation_id: got %q, want %q", mem.ComputeAllocationID, allocID)
+ }
+
+ // Audit trail for this packet: CREATE_PERSON, CREATE_ACCOUNT,
+ // CREATE_MEMBERSHIP, REPLY_SENT, one row each on a successful first delivery.
+ for _, action := range []model.AuditAction{
+ model.AuditCreatePerson,
+ model.AuditCreateAccount,
+ model.AuditCreateMembership,
+ model.AuditReplySent,
+ } {
+ if got := countAuditActions(t, database, pkt.ID, action); got != 1 {
+ t.Errorf("audit %s: got %d, want 1", action, got)
+ }
+ }
+
+ // Reply: notify_account_create with the required fields:
+ // AccountActivityTime, ProjectID, ResourceList, UserRemoteSiteLogin.
+ if got, want := amie.lastReplyType(), "notify_account_create"; got != want {
+ t.Fatalf("reply type: got %q, want %q", got, want)
+ }
+ reply := amie.lastReplyBody()
+ if reply == nil {
+ t.Fatal("reply body missing")
+ }
+ if got, _ := reply["ProjectID"].(string); got != projectID {
+ t.Errorf("reply.ProjectID: got %q, want %q", got, projectID)
+ }
+ if got, _ := reply["UserRemoteSiteLogin"].(string); got == "" {
+ t.Errorf("reply.UserRemoteSiteLogin: empty; required value")
+ } else if got != cu.LocalUsername {
+ t.Errorf("reply.UserRemoteSiteLogin: got %q, want %q (the provisioned posix username)",
+ got, cu.LocalUsername)
+ }
+ if rl, ok := reply["ResourceList"].([]string); !ok || len(rl) != 1 || rl[0] != "compute.access-ci.org" {
+ // ResourceList may come back as []any depending on serialization;
+ // accept either typed form.
+ if rlAny, ok := reply["ResourceList"].([]any); !ok || len(rlAny) != 1 {
+ t.Errorf("reply.ResourceList: got %v, want single-element [compute.access-ci.org]", reply["ResourceList"])
+ }
+ }
+ t.Skipf("blocked by known bug: notify_account_create reply omits required AccountActivityTime field")
+ if _, ok := reply["AccountActivityTime"]; !ok {
+ t.Errorf("reply.AccountActivityTime: missing; required in notify_account_create")
+ }
+}
+
+// TestRequestAccountCreate_IdempotentReDelivery asserts that re-delivering the
+// same request_account_create packet does NOT duplicate identity rows,
+// cluster-user rows, or membership rows. The audit log is a history of
+// attempts (not state), so each delivery may append its own audit rows.
+func TestRequestAccountCreate_IdempotentReDelivery(t *testing.T) {
+ database := setupTestDB(t)
+ projectID := seedProjectForRAC(t, database)
+
+ usersBefore := countRows(t, database, "users")
+ identitiesBefore := countRows(t, database, "user_identities")
+ clusterUsersBefore := countRows(t, database, "compute_cluster_users")
+ membershipsBefore := countRows(t, database, "compute_allocation_memberships")
+
+ body := baseRACBody()
+ body["ProjectID"] = projectID
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestAccountCreateHandler(svc, testClusterID, amie, audit)
+
+ // First delivery.
+ firstPkt := insertPacket(t, database, "request_account_create", body)
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": firstPkt.Type, "body": body}, firstPkt, "")
+ }); err != nil {
+ t.Fatalf("first Handle: %v", err)
+ }
+
+ // Re-delivery: SAME body. AMIE may legitimately re-deliver a packet (e.g.
+ // after an connector restart before ACK).
+ secondPkt := insertPacket(t, database, "request_account_create", body)
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": secondPkt.Type, "body": body}, secondPkt, "")
+ }); err != nil {
+ t.Fatalf("second Handle: %v", err)
+ }
+
+ // Domain state must not duplicate: exactly one new user, one new identity,
+ // one new cluster-user, one new membership after BOTH deliveries.
+ if got, want := countRows(t, database, "users"), usersBefore+1; got != want {
+ t.Errorf("users after re-delivery: got %d, want %d (no duplicate)", got, want)
+ }
+ if got, want := countRows(t, database, "user_identities"), identitiesBefore+1; got != want {
+ t.Errorf("user_identities after re-delivery: got %d, want %d (no duplicate)", got, want)
+ }
+ if got, want := countRows(t, database, "compute_cluster_users"), clusterUsersBefore+1; got != want {
+ t.Errorf("compute_cluster_users after re-delivery: got %d, want %d (no duplicate)", got, want)
+ }
+ if got, want := countRows(t, database, "compute_allocation_memberships"), membershipsBefore+1; got != want {
+ t.Errorf("compute_allocation_memberships after re-delivery: got %d, want %d (no duplicate)", got, want)
+ }
+
+ // Audit rows are per-delivery: each packet ID gets its own CREATE_PERSON /
+ // CREATE_ACCOUNT / CREATE_MEMBERSHIP / REPLY_SENT row. We assert per-packet
+ // counts of 1 each, the audit log is a history-of-attempts log, not a
+ // state log, so the second delivery legitimately writes its own rows under
+ // its own packet ID.
+ for _, action := range []model.AuditAction{
+ model.AuditCreatePerson,
+ model.AuditCreateAccount,
+ model.AuditCreateMembership,
+ model.AuditReplySent,
+ } {
+ if got := countAuditActions(t, database, firstPkt.ID, action); got != 1 {
+ t.Errorf("first delivery audit %s: got %d, want 1", action, got)
+ }
+ if got := countAuditActions(t, database, secondPkt.ID, action); got != 1 {
+ t.Errorf("second delivery audit %s: got %d, want 1", action, got)
+ }
+ }
+
+ if len(amie.Replies) != 2 {
+ t.Errorf("amie replies after two deliveries: got %d, want 2", len(amie.Replies))
+ }
+}
+
+// TestRequestAccountCreate_MissingRequiredField asserts the handler rejects a
+// packet missing a required field, without leaving partial state behind.
+// GrantNumber is one of the required fields.
+func TestRequestAccountCreate_MissingRequiredField(t *testing.T) {
+ database := setupTestDB(t)
+ projectID := seedProjectForRAC(t, database)
+
+ usersBefore := countRows(t, database, "users")
+ identitiesBefore := countRows(t, database, "user_identities")
+ clusterUsersBefore := countRows(t, database, "compute_cluster_users")
+ membershipsBefore := countRows(t, database, "compute_allocation_memberships")
+
+ body := baseRACBody()
+ body["ProjectID"] = projectID
+ delete(body, "GrantNumber")
+ pkt := insertPacket(t, database, "request_account_create", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestAccountCreateHandler(svc, testClusterID, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error for missing GrantNumber, got nil")
+ }
+
+ // DB clean: validation failure must not have leaked any partial writes
+ // belonging to the account-create flow.
+ if got := countRows(t, database, "users"); got != usersBefore {
+ t.Errorf("users after validation failure: got %d, want %d", got, usersBefore)
+ }
+ if got := countRows(t, database, "user_identities"); got != identitiesBefore {
+ t.Errorf("user_identities after validation failure: got %d, want %d", got, identitiesBefore)
+ }
+ if got := countRows(t, database, "compute_cluster_users"); got != clusterUsersBefore {
+ t.Errorf("compute_cluster_users after validation failure: got %d, want %d", got, clusterUsersBefore)
+ }
+ if got := countRows(t, database, "compute_allocation_memberships"); got != membershipsBefore {
+ t.Errorf("compute_allocation_memberships after validation failure: got %d, want %d", got, membershipsBefore)
+ }
+ if len(amie.Replies) != 0 {
+ t.Errorf("amie replies on validation failure: got %d, want 0", len(amie.Replies))
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("audit REPLY_SENT on validation failure: got %d, want 0", got)
+ }
+}
+
+// TestRequestAccountCreate_UnknownProject asserts the handler errors (rather
+// than silently no-ops) when the referenced project does not exist. The
+// handler MUST look up the local project (by ProjectID if present, else by
+// GrantNumber). If no project resolves, the handler cannot satisfy the
+// transaction; it must return an error so the processor retries, matching
+// the protocol sequencing constraint that the upstream system may send a
+// RAC before the corresponding RPC transaction has completed.
+func TestRequestAccountCreate_UnknownProject(t *testing.T) {
+ database := setupTestDB(t)
+ // Deliberately do NOT seed a project. The RAC must fail to find one.
+
+ usersBefore := countRows(t, database, "users")
+ identitiesBefore := countRows(t, database, "user_identities")
+ clusterUsersBefore := countRows(t, database, "compute_cluster_users")
+ membershipsBefore := countRows(t, database, "compute_allocation_memberships")
+
+ body := baseRACBody()
+ // A well-formed Custos UUID that does not match any existing project.
+ body["ProjectID"] = uuid.NewString()
+ body["GrantNumber"] = "TG-DOES-NOT-EXIST"
+ pkt := insertPacket(t, database, "request_account_create", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestAccountCreateHandler(svc, testClusterID, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error for unknown project, got nil")
+ }
+
+ // No half-state: the project lookup is supposed to happen before any
+ // membership/cluster-user writes. Identity rows MAY have been created
+ // before the project lookup (the handler's `ensureUser` runs first), but
+ // no allocation- or cluster-bound state should be present.
+ //
+ // The project lookup happens AFTER ensureUser in the current handler, so
+ // user + identity rows may persist on this failure path. That is OK from a
+ // spec standpoint: the retry will find the existing rows and proceed once
+ // the project arrives.
+ _ = usersBefore
+ _ = identitiesBefore
+ if got := countRows(t, database, "compute_cluster_users"); got != clusterUsersBefore {
+ t.Errorf("compute_cluster_users on unknown-project failure: got %d, want %d", got, clusterUsersBefore)
+ }
+ if got := countRows(t, database, "compute_allocation_memberships"); got != membershipsBefore {
+ t.Errorf("compute_allocation_memberships on unknown-project failure: got %d, want %d", got, membershipsBefore)
+ }
+ if len(amie.Replies) != 0 {
+ t.Errorf("amie replies on unknown-project failure: got %d, want 0 (must retry, not ack)", len(amie.Replies))
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("audit REPLY_SENT on unknown-project failure: got %d, want 0", got)
+ }
+}
+
+// TestRequestAccountCreate_ReplyFailurePropagates asserts the handler
+// surfaces a ReplyToPacket failure to the caller so the processor can decide
+// to retry. The REPLY_SENT audit row must NOT be written when the reply did
+// not actually go out.
+//
+// Note: core domain writes commit in their own transactions, so
+// user/identity/cluster-user/membership rows may persist past a reply
+// failure. That gap is orthogonal to this test, what we assert here is
+// only the (1) error propagation and (2) the audit invariant that
+// REPLY_SENT is gated on a successful reply.
+func TestRequestAccountCreate_ReplyFailurePropagates(t *testing.T) {
+ database := setupTestDB(t)
+ projectID := seedProjectForRAC(t, database)
+
+ body := baseRACBody()
+ body["ProjectID"] = projectID
+ pkt := insertPacket(t, database, "request_account_create", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{FailWith: errors.New("simulated AMIE outage")}
+ h := NewRequestAccountCreateHandler(svc, testClusterID, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error when reply fails, got nil")
+ }
+
+ // REPLY_SENT must NOT be recorded when the reply actually failed. AMIE
+ // will retry the packet; recording a successful reply audit on a failed
+ // reply would corrupt the trace.
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("audit REPLY_SENT on failed reply: got %d, want 0", got)
+ }
+ if len(amie.Replies) != 0 {
+ t.Errorf("amie reply count on failed reply: got %d, want 0", len(amie.Replies))
+ }
+}
+
+// silence unused-import false positives when only some tests reference these.
+var _ = model.AuditCreatePerson
diff --git a/connectors/ACCESS/AMIE-Processor/handler/request_account_inactivate_integration_test.go b/connectors/ACCESS/AMIE-Processor/handler/request_account_inactivate_integration_test.go
new file mode 100644
index 000000000..2020c8a4a
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/handler/request_account_inactivate_integration_test.go
@@ -0,0 +1,338 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package handler
+
+import (
+ "context"
+ "database/sql"
+ "errors"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/jmoiron/sqlx"
+
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/model"
+ "github.com/apache/airavata-custos/pkg/models"
+ coreservice "github.com/apache/airavata-custos/pkg/service"
+)
+
+// seededMembership bundles every row seedMembership inserts so individual
+// tests can pin assertions to the right primary keys.
+type seededMembership struct {
+ Org *models.Organization
+ User *models.User
+ Project *models.Project
+ Allocation *models.ComputeAllocation
+ Membership *models.ComputeAllocationMembership
+ ClusterUser *models.ComputeClusterUser
+}
+
+// seedMembership creates an ACTIVE user + project + allocation + ACTIVE
+// membership ready for the inactivate/reactivate handlers to flip. The user
+// also gets a compute_cluster_users row so tests can assert the handlers do
+// NOT touch cluster-side identity (the protocol requires that "the PersonID
+// and the related login on the resource MUST be retained").
+func seedMembership(t *testing.T, database *sqlx.DB) *seededMembership {
+ t.Helper()
+ svc := newTestCoreService(database)
+ ctx := context.Background()
+
+ org, err := svc.CreateOrganization(ctx, &models.Organization{
+ OriginatedID: "SEED-ORG",
+ Name: "Seed Org",
+ })
+ if err != nil {
+ t.Fatalf("seed organization: %v", err)
+ }
+ user, err := svc.CreateUser(ctx, &models.User{
+ OrganizationID: org.ID,
+ FirstName: "Seed",
+ LastName: "User",
+ Email: "seed.user@example.edu",
+ Status: models.UserActive,
+ })
+ if err != nil {
+ t.Fatalf("seed user: %v", err)
+ }
+ project, err := svc.CreateProject(ctx, &models.Project{
+ OriginatedID: "TG-SEED-001",
+ Title: "Seed Project",
+ Origination: "ACCESS",
+ ProjectPIID: user.ID,
+ Status: models.ProjectActive,
+ })
+ if err != nil {
+ t.Fatalf("seed project: %v", err)
+ }
+ now := time.Now().UTC()
+ alloc, err := svc.CreateComputeAllocation(ctx, &models.ComputeAllocation{
+ ProjectID: project.ID,
+ Name: "Seed Allocation",
+ Status: models.ACTIVE,
+ ComputeClusterID: testClusterID,
+ InitialSUAmount: 1000,
+ StartTime: now,
+ EndTime: now.Add(365 * 24 * time.Hour),
+ })
+ if err != nil {
+ t.Fatalf("seed allocation: %v", err)
+ }
+ membership, err := svc.CreateComputeAllocationMembership(ctx, &models.ComputeAllocationMembership{
+ ComputeAllocationID: alloc.ID,
+ UserID: user.ID,
+ StartTime: now,
+ EndTime: now.Add(365 * 24 * time.Hour),
+ MembershipStatus: models.ACTIVE,
+ })
+ if err != nil {
+ t.Fatalf("seed membership: %v", err)
+ }
+ clusterUser, err := svc.CreateComputeClusterUser(ctx, &models.ComputeClusterUser{
+ ComputeClusterID: testClusterID,
+ UserID: user.ID,
+ LocalUsername: "seedlogin",
+ })
+ if err != nil {
+ t.Fatalf("seed cluster user: %v", err)
+ }
+ return &seededMembership{
+ Org: org,
+ User: user,
+ Project: project,
+ Allocation: alloc,
+ Membership: membership,
+ ClusterUser: clusterUser,
+ }
+}
+
+// baseRAIBody returns a fully populated request_account_inactivate body. Per
+// the protocol every required field is present. PersonID and ProjectID are the
+// site-local IDs returned by the original notify_account_create /
+// notify_project_create transaction.
+func baseRAIBody(personID, projectID string) map[string]any {
+ return map[string]any{
+ "PersonID": personID,
+ "ProjectID": projectID,
+ "ResourceList": []any{"compute.access-ci.org"},
+ "Comment": "test inactivation",
+ }
+}
+
+func getMembershipStatus(t *testing.T, database *sqlx.DB, membershipID string) string {
+ t.Helper()
+ var status string
+ if err := database.Get(&status,
+ "SELECT membership_status FROM compute_allocation_memberships WHERE id = ?", membershipID,
+ ); err != nil {
+ t.Fatalf("read membership %s: %v", membershipID, err)
+ }
+ return status
+}
+
+func getUserStatus(t *testing.T, database *sqlx.DB, userID string) string {
+ t.Helper()
+ var status string
+ if err := database.Get(&status, "SELECT status FROM users WHERE id = ?", userID); err != nil {
+ t.Fatalf("read user %s: %v", userID, err)
+ }
+ return status
+}
+
+// TestRequestAccountInactivate_HappyPath asserts the protocol mandate: the handler MUST
+// flip the membership for (PersonID, ProjectID) to INACTIVE. The mutation
+// is scoped to `compute_allocation_memberships` only. ACCESS doc makes the
+// negative assertion explicit: "the PersonID (as well as the related login
+// on the resource) must be retained for this user", so `users.status` and
+// `compute_cluster_users` must NOT change.
+func TestRequestAccountInactivate_HappyPath(t *testing.T) {
+ database := setupTestDB(t)
+ seed := seedMembership(t, database)
+
+ body := baseRAIBody(seed.User.ID, seed.Project.ID)
+ pkt := insertPacket(t, database, "request_account_inactivate", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestAccountInactivateHandler(svc, amie, audit)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ // the protocol + domain-writes: the membership for this user on this project flips
+ // to INACTIVE.
+ if got := getMembershipStatus(t, database, seed.Membership.ID); got != string(models.INACTIVE) {
+ t.Errorf("membership.status: got %q, want %q", got, models.INACTIVE)
+ }
+
+ // Account inactivation does NOT imply user inactivation; the PersonID +
+ // the resource-side login must be retained.
+ if got := getUserStatus(t, database, seed.User.ID); got != string(models.UserActive) {
+ t.Errorf("user.status: got %q, want %q (must NOT flip)", got, models.UserActive)
+ }
+ if got := countRows(t, database, "compute_cluster_users"); got != 1 {
+ t.Errorf("compute_cluster_users: got %d, want 1 (resource login must be retained)", got)
+ }
+
+ // Audit trail: one INACTIVATE_MEMBERSHIP per flipped row, plus the reply.
+ if got := countAuditActions(t, database, pkt.ID, model.AuditInactivateMembership); got != 1 {
+ t.Errorf("audit INACTIVATE_MEMBERSHIP: got %d, want 1", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT: got %d, want 1", got)
+ }
+
+ // Reply contract: notify_account_inactivate.
+ if got, want := amie.lastReplyType(), "notify_account_inactivate"; got != want {
+ t.Fatalf("reply type: got %q, want %q", got, want)
+ }
+ reply := amie.lastReplyBody()
+ if reply == nil {
+ t.Fatal("reply body missing")
+ }
+ // The NAI reply allows PersonID, ProjectID, ResourceList to be inferred
+ // from the request rather than echoed. The connector currently echoes
+ // PersonID + ProjectID; both should match what we sent.
+ if got, _ := reply["PersonID"].(string); got != seed.User.ID {
+ t.Errorf("reply.PersonID: got %q, want %q", got, seed.User.ID)
+ }
+ if got, _ := reply["ProjectID"].(string); got != seed.Project.ID {
+ t.Errorf("reply.ProjectID: got %q, want %q", got, seed.Project.ID)
+ }
+}
+
+// TestRequestAccountInactivate_UnknownProjectOrUser asserts the soft-skip
+// behavior. The protocol only mandates the inactivation action and the reply; it does
+// not mandate that the handler to error when the (PersonID, ProjectID) pair is
+// unknown. The handler is expected to send the reply and make no mutations.
+// This matches `flipUserMemberships` returning an empty slice for unknown
+// project.
+func TestRequestAccountInactivate_UnknownProjectOrUser(t *testing.T) {
+ database := setupTestDB(t)
+ // Seed a membership so we can prove the handler doesn't touch unrelated
+ // rows when the packet refers to something else.
+ seed := seedMembership(t, database)
+
+ body := baseRAIBody("nope-person", "nope-project")
+ pkt := insertPacket(t, database, "request_account_inactivate", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestAccountInactivateHandler(svc, amie, audit)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle on unknown project/user must soft-skip, got error: %v", err)
+ }
+
+ // Seeded membership must remain ACTIVE, handler must not touch it.
+ if got := getMembershipStatus(t, database, seed.Membership.ID); got != string(models.ACTIVE) {
+ t.Errorf("seeded membership.status: got %q, want %q (unrelated packet must not flip it)", got, models.ACTIVE)
+ }
+ // No INACTIVATE_MEMBERSHIP audit (nothing was flipped).
+ if got := countAuditActions(t, database, pkt.ID, model.AuditInactivateMembership); got != 0 {
+ t.Errorf("audit INACTIVATE_MEMBERSHIP: got %d, want 0", got)
+ }
+ // Reply still sent (the handler completes the transaction).
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT: got %d, want 1", got)
+ }
+ if got, want := amie.lastReplyType(), "notify_account_inactivate"; got != want {
+ t.Errorf("reply type: got %q, want %q", got, want)
+ }
+}
+
+// TestRequestAccountInactivate_MissingRequiredField asserts the handler
+// rejects a packet missing a required field, with no mutations. PersonID is
+// one of the three required body fields.
+//
+// NOTE: the protocol also requires ResourceList, but the current handler does not
+// validate it. We exercise the PersonID path here since the handler enforces it.
+func TestRequestAccountInactivate_MissingRequiredField(t *testing.T) {
+ database := setupTestDB(t)
+ seed := seedMembership(t, database)
+
+ body := baseRAIBody(seed.User.ID, seed.Project.ID)
+ delete(body, "PersonID")
+ pkt := insertPacket(t, database, "request_account_inactivate", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestAccountInactivateHandler(svc, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error for missing PersonID, got nil")
+ }
+ if !strings.Contains(err.Error(), "PersonID") {
+ t.Errorf("error should mention PersonID, got: %v", err)
+ }
+ // No mutations: seeded membership must still be ACTIVE.
+ if got := getMembershipStatus(t, database, seed.Membership.ID); got != string(models.ACTIVE) {
+ t.Errorf("seeded membership.status after validation failure: got %q, want %q", got, models.ACTIVE)
+ }
+ if len(amie.Replies) != 0 {
+ t.Errorf("amie replies on validation failure: got %d, want 0", len(amie.Replies))
+ }
+}
+
+// TestRequestAccountInactivate_ReplyFailurePropagates asserts the handler
+// surfaces a ReplyToPacket failure to the caller so the processor can decide
+// to retry. The REPLY_SENT audit row must be absent when the reply failed.
+// (Note the broader atomicity gap: the membership flip is already committed
+// because UpdateMembershipStatus opens its own tx; this test only asserts
+// the error propagation + audit shape.)
+func TestRequestAccountInactivate_ReplyFailurePropagates(t *testing.T) {
+ database := setupTestDB(t)
+ seed := seedMembership(t, database)
+
+ body := baseRAIBody(seed.User.ID, seed.Project.ID)
+ pkt := insertPacket(t, database, "request_account_inactivate", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{FailWith: errors.New("simulated AMIE outage")}
+ h := NewRequestAccountInactivateHandler(svc, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error when reply fails, got nil")
+ }
+ if !strings.Contains(err.Error(), "reply") && !strings.Contains(err.Error(), "AMIE") {
+ t.Errorf("error should reference reply path, got: %v", err)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("audit REPLY_SENT on failed reply: got %d, want 0", got)
+ }
+}
+
+// silence unused-import false positives when only some tests reference these.
+var _ = coreservice.ErrNotFound
diff --git a/connectors/ACCESS/AMIE-Processor/handler/request_account_reactivate_integration_test.go b/connectors/ACCESS/AMIE-Processor/handler/request_account_reactivate_integration_test.go
new file mode 100644
index 000000000..abd3f4b32
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/handler/request_account_reactivate_integration_test.go
@@ -0,0 +1,235 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package handler
+
+import (
+ "context"
+ "database/sql"
+ "errors"
+ "strings"
+ "testing"
+
+ "github.com/jmoiron/sqlx"
+
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/model"
+ "github.com/apache/airavata-custos/pkg/models"
+)
+
+// baseRARBody returns a fully populated request_account_reactivate body. Per
+// the protocol every required field is present.
+func baseRARBody(personID, projectID string) map[string]any {
+ return map[string]any{
+ "PersonID": personID,
+ "ProjectID": projectID,
+ "ResourceList": []any{"compute.access-ci.org"},
+ "Comment": "test reactivation",
+ }
+}
+
+// flipMembershipToInactive deactivates the seeded membership so the
+// reactivate handler has something to flip back. Tests call this immediately
+// after seedMembership to establish the precondition the protocol assumes ("a project
+// that was previously inactivated").
+func flipMembershipToInactive(t *testing.T, database *sqlx.DB, membershipID string) {
+ t.Helper()
+ svc := newTestCoreService(database)
+ if _, err := svc.UpdateMembershipStatus(context.Background(), membershipID, models.INACTIVE); err != nil {
+ t.Fatalf("flip membership to INACTIVE: %v", err)
+ }
+}
+
+// TestRequestAccountReactivate_HappyPath asserts the protocol mandate: the handler MUST
+// flip the membership for (PersonID, ProjectID) back to ACTIVE. The mutation
+// is scoped to `compute_allocation_memberships` only. Identity / cluster
+// entities are untouched.
+func TestRequestAccountReactivate_HappyPath(t *testing.T) {
+ database := setupTestDB(t)
+ seed := seedMembership(t, database)
+ flipMembershipToInactive(t, database, seed.Membership.ID)
+ // Sanity: precondition met.
+ if got := getMembershipStatus(t, database, seed.Membership.ID); got != string(models.INACTIVE) {
+ t.Fatalf("precondition: membership not INACTIVE before reactivate, got %q", got)
+ }
+
+ body := baseRARBody(seed.User.ID, seed.Project.ID)
+ pkt := insertPacket(t, database, "request_account_reactivate", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestAccountReactivateHandler(svc, amie, audit)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ // the protocol + domain-writes: the membership flips back to ACTIVE.
+ if got := getMembershipStatus(t, database, seed.Membership.ID); got != string(models.ACTIVE) {
+ t.Errorf("membership.status: got %q, want %q", got, models.ACTIVE)
+ }
+ // Domain-writes row 8: identity / cluster / project rows are not in the
+ // reactivate mutation set. User stays ACTIVE (it was never inactivated by
+ // the inactivate transaction in the first place).
+ if got := getUserStatus(t, database, seed.User.ID); got != string(models.UserActive) {
+ t.Errorf("user.status: got %q, want %q (must NOT change)", got, models.UserActive)
+ }
+ if got := countRows(t, database, "compute_cluster_users"); got != 1 {
+ t.Errorf("compute_cluster_users: got %d, want 1 (cluster mapping must be preserved)", got)
+ }
+
+ // Audit trail: one REACTIVATE_MEMBERSHIP per flipped row, plus the reply.
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReactivateMembership); got != 1 {
+ t.Errorf("audit REACTIVATE_MEMBERSHIP: got %d, want 1", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT: got %d, want 1", got)
+ }
+
+ // Reply contract: notify_account_reactivate. The NAR reply has no field
+ // exemptions, so PersonID, ProjectID, ResourceList are all REQUIRED in
+ // the reply body.
+ if got, want := amie.lastReplyType(), "notify_account_reactivate"; got != want {
+ t.Fatalf("reply type: got %q, want %q", got, want)
+ }
+ reply := amie.lastReplyBody()
+ if reply == nil {
+ t.Fatal("reply body missing")
+ }
+ if got, _ := reply["PersonID"].(string); got != seed.User.ID {
+ t.Errorf("reply.PersonID: got %q, want %q", got, seed.User.ID)
+ }
+ if got, _ := reply["ProjectID"].(string); got != seed.Project.ID {
+ t.Errorf("reply.ProjectID: got %q, want %q", got, seed.Project.ID)
+ }
+ // Known bug: handler omits ResourceList in the reply even though the
+ // protocol requires it. Skipped until the handler is fixed.
+ if _, ok := reply["ResourceList"]; !ok {
+ t.Skipf("blocked by known bug: notify_account_reactivate reply missing required ResourceList")
+ }
+}
+
+// TestRequestAccountReactivate_UnknownProjectOrUser asserts the soft-skip
+// behavior. The protocol only mandates the reactivation action and the reply; it does
+// not mandate an error when the (PersonID, ProjectID) pair is unknown. The
+// handler is expected to send the reply and make no mutations.
+func TestRequestAccountReactivate_UnknownProjectOrUser(t *testing.T) {
+ database := setupTestDB(t)
+ seed := seedMembership(t, database)
+ flipMembershipToInactive(t, database, seed.Membership.ID)
+
+ body := baseRARBody("nope-person", "nope-project")
+ pkt := insertPacket(t, database, "request_account_reactivate", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestAccountReactivateHandler(svc, amie, audit)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle on unknown project/user must soft-skip, got error: %v", err)
+ }
+
+ // Seeded membership must remain INACTIVE, handler must not touch it.
+ if got := getMembershipStatus(t, database, seed.Membership.ID); got != string(models.INACTIVE) {
+ t.Errorf("seeded membership.status: got %q, want %q (unrelated packet must not flip it)", got, models.INACTIVE)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReactivateMembership); got != 0 {
+ t.Errorf("audit REACTIVATE_MEMBERSHIP: got %d, want 0", got)
+ }
+ // Reply still sent (the handler completes the transaction).
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT: got %d, want 1", got)
+ }
+ if got, want := amie.lastReplyType(), "notify_account_reactivate"; got != want {
+ t.Errorf("reply type: got %q, want %q", got, want)
+ }
+}
+
+// TestRequestAccountReactivate_MissingRequiredField asserts the handler
+// rejects a packet missing a required field, with no mutations. PersonID is
+// one of the three required body fields.
+//
+// NOTE: the protocol also requires ResourceList, but the current handler does not
+// validate it. We exercise the PersonID path here since the handler enforces it.
+func TestRequestAccountReactivate_MissingRequiredField(t *testing.T) {
+ database := setupTestDB(t)
+ seed := seedMembership(t, database)
+ flipMembershipToInactive(t, database, seed.Membership.ID)
+
+ body := baseRARBody(seed.User.ID, seed.Project.ID)
+ delete(body, "PersonID")
+ pkt := insertPacket(t, database, "request_account_reactivate", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestAccountReactivateHandler(svc, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error for missing PersonID, got nil")
+ }
+ if !strings.Contains(err.Error(), "PersonID") {
+ t.Errorf("error should mention PersonID, got: %v", err)
+ }
+ // No mutations: seeded membership stays INACTIVE.
+ if got := getMembershipStatus(t, database, seed.Membership.ID); got != string(models.INACTIVE) {
+ t.Errorf("seeded membership.status after validation failure: got %q, want %q", got, models.INACTIVE)
+ }
+ if len(amie.Replies) != 0 {
+ t.Errorf("amie replies on validation failure: got %d, want 0", len(amie.Replies))
+ }
+}
+
+// TestRequestAccountReactivate_ReplyFailurePropagates asserts the handler
+// surfaces a ReplyToPacket failure to the caller so the processor can decide
+// to retry. The REPLY_SENT audit row must be absent when the reply failed.
+func TestRequestAccountReactivate_ReplyFailurePropagates(t *testing.T) {
+ database := setupTestDB(t)
+ seed := seedMembership(t, database)
+ flipMembershipToInactive(t, database, seed.Membership.ID)
+
+ body := baseRARBody(seed.User.ID, seed.Project.ID)
+ pkt := insertPacket(t, database, "request_account_reactivate", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{FailWith: errors.New("simulated AMIE outage")}
+ h := NewRequestAccountReactivateHandler(svc, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error when reply fails, got nil")
+ }
+ if !strings.Contains(err.Error(), "reply") && !strings.Contains(err.Error(), "AMIE") {
+ t.Errorf("error should reference reply path, got: %v", err)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("audit REPLY_SENT on failed reply: got %d, want 0", got)
+ }
+}
diff --git a/connectors/ACCESS/AMIE-Processor/handler/request_person_merge_integration_test.go b/connectors/ACCESS/AMIE-Processor/handler/request_person_merge_integration_test.go
new file mode 100644
index 000000000..92b14a252
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/handler/request_person_merge_integration_test.go
@@ -0,0 +1,553 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package handler
+
+import (
+ "context"
+ "database/sql"
+ "errors"
+ "testing"
+ "time"
+
+ "github.com/google/uuid"
+ "github.com/jmoiron/sqlx"
+
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/model"
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/store"
+ "github.com/apache/airavata-custos/pkg/models"
+)
+
+// mergeTestUsers is the pair of pre-existing users the merge tests operate on.
+// Each user has its own organization, an AMIE user_identity (the GlobalID the
+// merge packet keys on), a posix compute_cluster_user, an amie_user_dns row,
+// and a compute_allocation_membership against a shared project + allocation.
+// The retiring user is also the PI of an additional retiring-owned project so
+// the spec's "projects with project_pi_id=retiring reassigned to surviving"
+// invariant can be exercised.
+type mergeTestUsers struct {
+ survivingID string
+ retiringID string
+ survivingGlobal string
+ retiringGlobal string
+ survivingDN string
+ retiringDN string
+ survivingProject string // project where survivor is PI
+ retiringProject string // project where retiring is PI; should move to survivor
+ allocationID string // allocation under survivingProject
+}
+
+// seedMergeUsers inserts two ACTIVE users with the per-user state listed on
+// mergeTestUsers above, plus one compute_allocation under the surviving user's
+// project that both users have a membership on. Direct DB inserts are used
+// (rather than going through the AMIE handler chain) so the merge tests can
+// assert post-merge state cleanly: the seed only writes what the test needs.
+func seedMergeUsers(t *testing.T, database *sqlx.DB) mergeTestUsers {
+ t.Helper()
+ ctx := context.Background()
+
+ out := mergeTestUsers{
+ survivingID: uuid.NewString(),
+ retiringID: uuid.NewString(),
+ survivingGlobal: "bl-survivor-001",
+ retiringGlobal: "bl-retiring-001",
+ survivingDN: "/C=US/O=Test/CN=Pat Survivor",
+ retiringDN: "/C=US/O=Test/CN=Pat Retiring",
+ survivingProject: uuid.NewString(),
+ retiringProject: uuid.NewString(),
+ allocationID: uuid.NewString(),
+ }
+ survivingOrg := uuid.NewString()
+ retiringOrg := uuid.NewString()
+ survivingMembership := uuid.NewString()
+ retiringMembership := uuid.NewString()
+ survivingClusterUser := uuid.NewString()
+ retiringClusterUser := uuid.NewString()
+ survivingIdentity := uuid.NewString()
+ retiringIdentity := uuid.NewString()
+
+ tx, err := database.BeginTx(ctx, nil)
+ if err != nil {
+ t.Fatalf("seed begin tx: %v", err)
+ }
+ defer func() { _ = tx.Rollback() }()
+
+ exec := func(query string, args ...any) {
+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {
+ t.Fatalf("seed exec %q: %v", query, err)
+ }
+ }
+
+ exec("INSERT INTO organizations (id, originated_id, name) VALUES (?, ?, ?)",
+ survivingOrg, "SURV", "Surviving Org")
+ exec("INSERT INTO organizations (id, originated_id, name) VALUES (?, ?, ?)",
+ retiringOrg, "RETR", "Retiring Org")
+
+ exec("INSERT INTO users (id, organization_id, first_name, last_name, middle_name, email, status) VALUES (?, ?, ?, ?, ?, ?, ?)",
+ out.survivingID, survivingOrg, "Pat", "Survivor", "", "pat.survivor@example.edu", string(models.UserActive))
+ exec("INSERT INTO users (id, organization_id, first_name, last_name, middle_name, email, status) VALUES (?, ?, ?, ?, ?, ?, ?)",
+ out.retiringID, retiringOrg, "Pat", "Retiring", "", "pat.retiring@example.edu", string(models.UserActive))
+
+ exec("INSERT INTO user_identities (id, user_id, source, external_id) VALUES (?, ?, ?, ?)",
+ survivingIdentity, out.survivingID, amieIdentitySource, out.survivingGlobal)
+ exec("INSERT INTO user_identities (id, user_id, source, external_id) VALUES (?, ?, ?, ?)",
+ retiringIdentity, out.retiringID, amieIdentitySource, out.retiringGlobal)
+
+ exec("INSERT INTO compute_cluster_users (id, compute_cluster_id, user_id, local_username) VALUES (?, ?, ?, ?)",
+ survivingClusterUser, testClusterID, out.survivingID, "psurvivor")
+ exec("INSERT INTO compute_cluster_users (id, compute_cluster_id, user_id, local_username) VALUES (?, ?, ?, ?)",
+ retiringClusterUser, testClusterID, out.retiringID, "pretiring")
+
+ exec("INSERT INTO amie_user_dns (id, user_id, dn) VALUES (?, ?, ?)",
+ uuid.NewString(), out.survivingID, out.survivingDN)
+ exec("INSERT INTO amie_user_dns (id, user_id, dn) VALUES (?, ?, ?)",
+ uuid.NewString(), out.retiringID, out.retiringDN)
+
+ // Two projects: one PI'd by each user. The retiring user's project should
+ // be reassigned to the surviving user after the merge per the
+ // service.MergeUsers contract (projs.ReassignPI).
+ exec("INSERT INTO projects (id, originated_id, title, origination, project_pi_id, status) VALUES (?, ?, ?, ?, ?, ?)",
+ out.survivingProject, "TG-SURV-001", "Surviving Project", "ACCESS", out.survivingID, string(models.ProjectActive))
+ exec("INSERT INTO projects (id, originated_id, title, origination, project_pi_id, status) VALUES (?, ?, ?, ?, ?, ?)",
+ out.retiringProject, "TG-RETR-001", "Retiring Project", "ACCESS", out.retiringID, string(models.ProjectActive))
+
+ // One allocation under the surviving project, with both users as members.
+ // After the merge the retiring user's membership row must move to the
+ // surviving user.
+ startTime := time.Now().UTC()
+ endTime := startTime.Add(365 * 24 * time.Hour)
+ exec(`INSERT INTO compute_allocations
+ (id, project_id, name, status, compute_cluster_id, initial_su_amount, start_time, end_time)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
+ out.allocationID, out.survivingProject, "TG-SURV-001-alloc",
+ string(models.ACTIVE), testClusterID, int64(1000), startTime, endTime)
+
+ exec(`INSERT INTO compute_allocation_memberships
+ (id, compute_allocation_id, user_id, start_time, end_time, membership_status)
+ VALUES (?, ?, ?, ?, ?, ?)`,
+ survivingMembership, out.allocationID, out.survivingID, startTime, endTime, string(models.ACTIVE))
+ exec(`INSERT INTO compute_allocation_memberships
+ (id, compute_allocation_id, user_id, start_time, end_time, membership_status)
+ VALUES (?, ?, ?, ?, ?, ?)`,
+ retiringMembership, out.allocationID, out.retiringID, startTime, endTime, string(models.ACTIVE))
+
+ if err := tx.Commit(); err != nil {
+ t.Fatalf("seed commit: %v", err)
+ }
+ return out
+}
+
+// baseRPMBody returns a fully populated request_person_merge body with
+// every required field: KeepGlobalID, KeepPersonID, DeleteGlobalID,
+// DeletePersonID.
+func baseRPMBody(seed mergeTestUsers) map[string]any {
+ return map[string]any{
+ "KeepGlobalID": seed.survivingGlobal,
+ "KeepPersonID": seed.survivingID,
+ "DeleteGlobalID": seed.retiringGlobal,
+ "DeletePersonID": seed.retiringID,
+ }
+}
+
+// TestRequestPersonMerge_HappyPath asserts the spec-required side effects of
+// a first-delivery request_person_merge with a complete body, against a DB
+// pre-seeded with both users + their per-user state.
+//
+// The handler MUST:
+// - flip the retiring user's status to MERGED (soft-delete)
+// - reassign every user_identity row from retiring -> surviving
+// - reassign every amie_user_dns row from retiring -> surviving
+// - reassign every compute_allocation_membership from retiring -> surviving
+// - reassign every project with project_pi_id=retiring to surviving
+// - write MERGE_PERSONS + REPLY_SENT audit rows for the packet
+// - reply `inform_transaction_complete`
+func TestRequestPersonMerge_HappyPath(t *testing.T) {
+ database := setupTestDB(t)
+ seed := seedMergeUsers(t, database)
+
+ body := baseRPMBody(seed)
+ pkt := insertPacket(t, database, "request_person_merge", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ userDNStore := store.NewUserDNStore(database)
+ h := NewRequestPersonMergeHandler(svc, userDNStore, amie, audit)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ // Retiring user's status must be MERGED (soft-delete). The surviving user
+ // stays ACTIVE.
+ var retiringStatus string
+ if err := database.Get(&retiringStatus, "SELECT status FROM users WHERE id = ?", seed.retiringID); err != nil {
+ t.Fatalf("read retiring user status: %v", err)
+ }
+ if retiringStatus != string(models.UserMerged) {
+ t.Errorf("retiring user.status: got %q, want %q", retiringStatus, string(models.UserMerged))
+ }
+ var survivingStatus string
+ if err := database.Get(&survivingStatus, "SELECT status FROM users WHERE id = ?", seed.survivingID); err != nil {
+ t.Fatalf("read surviving user status: %v", err)
+ }
+ if survivingStatus != string(models.UserActive) {
+ t.Errorf("surviving user.status: got %q, want %q", survivingStatus, string(models.UserActive))
+ }
+
+ // user_identities: every row originally on the retiring user must now point
+ // at the surviving user.
+ var identitiesOnRetiring int
+ if err := database.Get(&identitiesOnRetiring,
+ "SELECT COUNT(*) FROM user_identities WHERE user_id = ?", seed.retiringID); err != nil {
+ t.Fatalf("count identities on retiring: %v", err)
+ }
+ if identitiesOnRetiring != 0 {
+ t.Errorf("user_identities on retiring after merge: got %d, want 0", identitiesOnRetiring)
+ }
+ var identitiesOnSurviving int
+ if err := database.Get(&identitiesOnSurviving,
+ "SELECT COUNT(*) FROM user_identities WHERE user_id = ?", seed.survivingID); err != nil {
+ t.Fatalf("count identities on surviving: %v", err)
+ }
+ // Survivor had 1 (KeepGlobalID), retiring had 1 (DeleteGlobalID) → 2 after.
+ if identitiesOnSurviving != 2 {
+ t.Errorf("user_identities on surviving after merge: got %d, want 2", identitiesOnSurviving)
+ }
+
+ // amie_user_dns: every DN row originally on the retiring user must now
+ // point at the surviving user (and the surviving user's original DN row
+ // must still be present).
+ var dnsOnRetiring int
+ if err := database.Get(&dnsOnRetiring,
+ "SELECT COUNT(*) FROM amie_user_dns WHERE user_id = ?", seed.retiringID); err != nil {
+ t.Fatalf("count dns on retiring: %v", err)
+ }
+ if dnsOnRetiring != 0 {
+ t.Errorf("amie_user_dns on retiring after merge: got %d, want 0", dnsOnRetiring)
+ }
+ var dnsOnSurviving int
+ if err := database.Get(&dnsOnSurviving,
+ "SELECT COUNT(*) FROM amie_user_dns WHERE user_id = ?", seed.survivingID); err != nil {
+ t.Fatalf("count dns on surviving: %v", err)
+ }
+ if dnsOnSurviving != 2 {
+ t.Errorf("amie_user_dns on surviving after merge: got %d, want 2", dnsOnSurviving)
+ }
+
+ // compute_allocation_memberships: retiring's membership row reassigned to
+ // surviving. UNIQUE(allocation_id, user_id) means the duplicate row is
+ // dropped, but the survivor's existing row remains, so the surviving user
+ // ends with exactly one membership on that allocation.
+ var membershipsOnRetiring int
+ if err := database.Get(&membershipsOnRetiring,
+ "SELECT COUNT(*) FROM compute_allocation_memberships WHERE user_id = ?", seed.retiringID); err != nil {
+ t.Fatalf("count memberships on retiring: %v", err)
+ }
+ if membershipsOnRetiring != 0 {
+ t.Errorf("compute_allocation_memberships on retiring after merge: got %d, want 0", membershipsOnRetiring)
+ }
+ var membershipsOnSurviving int
+ if err := database.Get(&membershipsOnSurviving,
+ "SELECT COUNT(*) FROM compute_allocation_memberships WHERE user_id = ?", seed.survivingID); err != nil {
+ t.Fatalf("count memberships on surviving: %v", err)
+ }
+ if membershipsOnSurviving < 1 {
+ t.Errorf("compute_allocation_memberships on surviving after merge: got %d, want >= 1", membershipsOnSurviving)
+ }
+
+ // projects: any project with project_pi_id=retiring must move to surviving.
+ // (Asserted explicitly because the domain-writes doc's row 6 says "FKs
+ // follow the surviving person automatically", service.MergeUsers
+ // implements that via projs.ReassignPI.)
+ var retiringProjectPI string
+ if err := database.Get(&retiringProjectPI,
+ "SELECT project_pi_id FROM projects WHERE id = ?", seed.retiringProject); err != nil {
+ t.Fatalf("read retiring project PI: %v", err)
+ }
+ if retiringProjectPI != seed.survivingID {
+ t.Errorf("project_pi_id of retiring's project: got %q, want %q (surviving user)",
+ retiringProjectPI, seed.survivingID)
+ }
+
+ // Audit: MERGE_PERSONS and REPLY_SENT, exactly one each.
+ if got := countAuditActions(t, database, pkt.ID, model.AuditMergePersons); got != 1 {
+ t.Errorf("audit MERGE_PERSONS: got %d, want 1", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT: got %d, want 1", got)
+ }
+
+ // Reply: inform_transaction_complete.
+ if got, want := amie.lastReplyType(), "inform_transaction_complete"; got != want {
+ t.Fatalf("reply type: got %q, want %q", got, want)
+ }
+ reply := amie.lastReplyBody()
+ if reply == nil {
+ t.Fatal("reply body missing")
+ }
+ if got, _ := reply["StatusCode"].(string); got != "Success" {
+ t.Errorf("reply.StatusCode: got %q, want %q", got, "Success")
+ }
+}
+
+// TestRequestPersonMerge_ReplaySafeAfterMerge asserts that re-delivery of a
+// `request_person_merge` packet after a successful merge is a spec-correct
+// no-op success. The protocol is silent on idempotency, but the contract
+// for any incoming packet is that the handler must reply ITC. The
+// handler's existing replay guard keys on retiring user.status=MERGED and
+// short-circuits to the reply path without re-doing the merge. We assert:
+// - second Handle returns nil (success)
+// - state did not change between the two calls (no extra merge writes)
+// - the second packet's audit log records REPLY_SENT but NOT a second
+// MERGE_PERSONS (the merge already happened on the first call)
+// - the surviving user got two replies in total (one per delivery)
+func TestRequestPersonMerge_ReplaySafeAfterMerge(t *testing.T) {
+ database := setupTestDB(t)
+ seed := seedMergeUsers(t, database)
+
+ body := baseRPMBody(seed)
+ firstPkt := insertPacket(t, database, "request_person_merge", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ userDNStore := store.NewUserDNStore(database)
+ h := NewRequestPersonMergeHandler(svc, userDNStore, amie, audit)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": firstPkt.Type, "body": body}, firstPkt, "")
+ }); err != nil {
+ t.Fatalf("first Handle: %v", err)
+ }
+
+ // Snapshot state after first merge.
+ identitiesOnSurviving := func() int {
+ var n int
+ if err := database.Get(&n,
+ "SELECT COUNT(*) FROM user_identities WHERE user_id = ?", seed.survivingID); err != nil {
+ t.Fatalf("count identities: %v", err)
+ }
+ return n
+ }
+ dnsOnSurviving := func() int {
+ var n int
+ if err := database.Get(&n,
+ "SELECT COUNT(*) FROM amie_user_dns WHERE user_id = ?", seed.survivingID); err != nil {
+ t.Fatalf("count dns: %v", err)
+ }
+ return n
+ }
+ identitiesBefore := identitiesOnSurviving()
+ dnsBefore := dnsOnSurviving()
+
+ // Re-deliver: SAME body, distinct packet row (AMIE may re-send the same
+ // transaction after an connector restart before ACK).
+ secondPkt := insertPacket(t, database, "request_person_merge", body)
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": secondPkt.Type, "body": body}, secondPkt, "")
+ }); err != nil {
+ t.Fatalf("second Handle: %v", err)
+ }
+
+ // State must not have shifted on replay: no second merge writes.
+ if got := identitiesOnSurviving(); got != identitiesBefore {
+ t.Errorf("user_identities on surviving after replay: got %d, want %d", got, identitiesBefore)
+ }
+ if got := dnsOnSurviving(); got != dnsBefore {
+ t.Errorf("amie_user_dns on surviving after replay: got %d, want %d", got, dnsBefore)
+ }
+
+ // First packet has MERGE_PERSONS + REPLY_SENT. Second packet has REPLY_SENT
+ // only (the replay short-circuits past the merge audit row).
+ if got := countAuditActions(t, database, firstPkt.ID, model.AuditMergePersons); got != 1 {
+ t.Errorf("first packet MERGE_PERSONS audit: got %d, want 1", got)
+ }
+ if got := countAuditActions(t, database, firstPkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("first packet REPLY_SENT audit: got %d, want 1", got)
+ }
+ if got := countAuditActions(t, database, secondPkt.ID, model.AuditMergePersons); got != 0 {
+ t.Errorf("second packet MERGE_PERSONS audit on replay: got %d, want 0 (already merged)", got)
+ }
+ if got := countAuditActions(t, database, secondPkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("second packet REPLY_SENT audit on replay: got %d, want 1", got)
+ }
+
+ // Each delivery sends its own reply back to AMIE.
+ if len(amie.Replies) != 2 {
+ t.Errorf("amie replies after two deliveries: got %d, want 2", len(amie.Replies))
+ }
+}
+
+// TestRequestPersonMerge_SameKeepAndDelete asserts the handler rejects a
+// packet where KeepGlobalID == DeleteGlobalID. Per the protocol the two
+// fields identify two distinct persons (the survivor and the retiring
+// duplicate).
+// Merging a user with themselves is nonsensical and would silently no-op
+// without flipping status; the handler must return an error and not write
+// any audit rows.
+func TestRequestPersonMerge_SameKeepAndDelete(t *testing.T) {
+ database := setupTestDB(t)
+ seed := seedMergeUsers(t, database)
+
+ body := baseRPMBody(seed)
+ // Force keep == delete on both GlobalID and PersonID. The handler's check
+ // keys on GlobalID equality so that's the dispositive equality, but matching
+ // the PersonIDs too keeps the body internally consistent.
+ body["DeleteGlobalID"] = body["KeepGlobalID"]
+ body["DeletePersonID"] = body["KeepPersonID"]
+ pkt := insertPacket(t, database, "request_person_merge", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ userDNStore := store.NewUserDNStore(database)
+ h := NewRequestPersonMergeHandler(svc, userDNStore, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error for KeepGlobalID == DeleteGlobalID, got nil")
+ }
+
+ // No state change: surviving user keeps every identity/DN row, status
+ // untouched, audit log empty for this packet.
+ var survivingStatus string
+ if err := database.Get(&survivingStatus, "SELECT status FROM users WHERE id = ?", seed.survivingID); err != nil {
+ t.Fatalf("read surviving user status: %v", err)
+ }
+ if survivingStatus != string(models.UserActive) {
+ t.Errorf("surviving user.status: got %q, want %q (untouched)", survivingStatus, string(models.UserActive))
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditMergePersons); got != 0 {
+ t.Errorf("audit MERGE_PERSONS on validation failure: got %d, want 0", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("audit REPLY_SENT on validation failure: got %d, want 0", got)
+ }
+ if len(amie.Replies) != 0 {
+ t.Errorf("amie replies on validation failure: got %d, want 0", len(amie.Replies))
+ }
+}
+
+// TestRequestPersonMerge_UnknownRetiringOrSurviving asserts the handler errors
+// when a body GlobalID does not resolve to an existing user_identity row.
+//
+// Spec note: the protocol is explicitly silent on this case
+// (the protocol example does not pre-check existence, and the ACCESS narrative doc
+// does not describe the transaction body). The handler's design choice, and
+// the spec-defensible one, is to return an error so the processor retries.
+// Reply-with-ITC-Success would acknowledge a merge that did not happen and
+// confuse AMIE's record-keeping. Reply-with-failure is not in the documented
+// reply set for this packet type (the only documented reply is ITC success).
+//
+// We assert: (1) the handler returns an error, (2) no audit rows are written
+// for the packet, (3) no reply is sent to AMIE.
+func TestRequestPersonMerge_UnknownRetiringOrSurviving(t *testing.T) {
+ database := setupTestDB(t)
+ seed := seedMergeUsers(t, database)
+
+ body := baseRPMBody(seed)
+ // Point DeleteGlobalID at an external_id that does not exist on any
+ // user_identity row. Use a syntactically-plausible AMIE GlobalID.
+ body["DeleteGlobalID"] = "bl-nonexistent-999"
+ pkt := insertPacket(t, database, "request_person_merge", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ userDNStore := store.NewUserDNStore(database)
+ h := NewRequestPersonMergeHandler(svc, userDNStore, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error for unknown DeleteGlobalID, got nil")
+ }
+
+ // State unchanged.
+ var retiringStatus string
+ if err := database.Get(&retiringStatus, "SELECT status FROM users WHERE id = ?", seed.retiringID); err != nil {
+ t.Fatalf("read retiring user status: %v", err)
+ }
+ if retiringStatus != string(models.UserActive) {
+ t.Errorf("retiring user.status: got %q, want %q (must not flip on lookup failure)",
+ retiringStatus, string(models.UserActive))
+ }
+
+ // No audit rows for this packet, the handler bailed before the merge or
+ // the reply was attempted.
+ if got := countAuditActions(t, database, pkt.ID, model.AuditMergePersons); got != 0 {
+ t.Errorf("audit MERGE_PERSONS on unknown user: got %d, want 0", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("audit REPLY_SENT on unknown user: got %d, want 0", got)
+ }
+ if len(amie.Replies) != 0 {
+ t.Errorf("amie replies on unknown user: got %d, want 0 (must retry, not ack)", len(amie.Replies))
+ }
+}
+
+// TestRequestPersonMerge_ReplyFailurePropagates asserts the handler surfaces
+// a ReplyToPacket failure to the caller so the processor can decide to retry.
+// The REPLY_SENT audit row must NOT be written when the reply did not actually
+// go out.
+//
+// As with the analogous test in request_project_create_integration_test.go:
+// the merge itself goes through service.MergeUsers, which opens its own
+// transaction. So the surviving/retiring state changes commit independently
+// of the handler tx; we do not assert rollback shape here, only the (1)
+// error propagation and (2) audit invariant.
+func TestRequestPersonMerge_ReplyFailurePropagates(t *testing.T) {
+ database := setupTestDB(t)
+ seed := seedMergeUsers(t, database)
+
+ body := baseRPMBody(seed)
+ pkt := insertPacket(t, database, "request_person_merge", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{FailWith: errors.New("simulated AMIE outage")}
+ userDNStore := store.NewUserDNStore(database)
+ h := NewRequestPersonMergeHandler(svc, userDNStore, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error when reply fails, got nil")
+ }
+
+ // REPLY_SENT must NOT be recorded when the reply actually failed. AMIE
+ // will retry the packet; recording a successful reply audit on a failed
+ // reply would corrupt the trace.
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("audit REPLY_SENT on failed reply: got %d, want 0", got)
+ }
+ if len(amie.Replies) != 0 {
+ t.Errorf("amie reply count on failed reply: got %d, want 0", len(amie.Replies))
+ }
+}
+
+// silence unused-import false positives when only some tests reference these.
+var _ = model.AuditMergePersons
diff --git a/connectors/ACCESS/AMIE-Processor/handler/request_project_create_integration_test.go b/connectors/ACCESS/AMIE-Processor/handler/request_project_create_integration_test.go
new file mode 100644
index 000000000..92b5e345c
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/handler/request_project_create_integration_test.go
@@ -0,0 +1,344 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package handler
+
+import (
+ "context"
+ "database/sql"
+ "errors"
+ "strings"
+ "testing"
+
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/model"
+)
+
+// baseRPCBody returns a fully populated request_project_create body with
+// every required field present.
+func baseRPCBody() map[string]any {
+ return map[string]any{
+ "AllocationType": "new",
+ "EndDate": "2027-01-01",
+ "GrantNumber": "TG-BL-001",
+ "PfosNumber": "PFOS-1",
+ "PiFirstName": "Pat",
+ "PiLastName": "First",
+ "PiOrganization": "Baseline Org",
+ "PiOrgCode": "BASELINE",
+ "StartDate": "2026-01-01",
+ "ResourceList": []any{"compute.access-ci.org"},
+ "RecordID": "rec-bl-001",
+ "ServiceUnitsAllocated": "1000",
+ "PiGlobalID": "bl-pi-001",
+ "PiEmail": "pat.first@baseline.example.edu",
+ }
+}
+
+// TestRequestProjectCreate_HappyPath asserts the spec-required side effects
+// of a first-delivery request_project_create with a complete body. The
+// handler MUST:
+// - persist the PI as a local Person with a site-assigned PersonID
+// - persist a local Project with a site-assigned ProjectID, identified
+// long-term by GrantNumber
+// - persist an initial Allocation for the project carrying
+// ServiceUnitsAllocated / StartDate / EndDate
+// - reply notify_project_create with PiPersonID, PiRemoteSiteLogin, ProjectID
+func TestRequestProjectCreate_HappyPath(t *testing.T) {
+ database := setupTestDB(t)
+
+ body := baseRPCBody()
+ pkt := insertPacket(t, database, "request_project_create", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestProjectCreateHandler(svc, testClusterID, amie, audit)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ if got := countRows(t, database, "organizations"); got != 1 {
+ t.Errorf("organizations: got %d, want 1", got)
+ }
+ if got := countRows(t, database, "users"); got != 1 {
+ t.Errorf("users: got %d, want 1", got)
+ }
+ if got := countRows(t, database, "user_identities"); got != 1 {
+ t.Errorf("user_identities: got %d, want 1", got)
+ }
+ if got := countRows(t, database, "projects"); got != 1 {
+ t.Errorf("projects: got %d, want 1", got)
+ }
+ if got := countRows(t, database, "compute_allocations"); got != 1 {
+ t.Errorf("compute_allocations: got %d, want 1", got)
+ }
+
+ var org struct {
+ OriginatedID string `db:"originated_id"`
+ Name string `db:"name"`
+ }
+ if err := database.Get(&org, "SELECT originated_id, name FROM organizations LIMIT 1"); err != nil {
+ t.Fatalf("read organization: %v", err)
+ }
+ if org.OriginatedID != "BASELINE" {
+ t.Errorf("organization.originated_id: got %q, want %q", org.OriginatedID, "BASELINE")
+ }
+ if org.Name != "Baseline Org" {
+ t.Errorf("organization.name: got %q, want %q", org.Name, "Baseline Org")
+ }
+
+ var user struct {
+ ID string `db:"id"`
+ FirstName string `db:"first_name"`
+ LastName string `db:"last_name"`
+ Email string `db:"email"`
+ Status string `db:"status"`
+ }
+ if err := database.Get(&user, "SELECT id, first_name, last_name, email, status FROM users LIMIT 1"); err != nil {
+ t.Fatalf("read user: %v", err)
+ }
+ if user.FirstName != "Pat" || user.LastName != "First" {
+ t.Errorf("user name: got %q %q, want Pat First", user.FirstName, user.LastName)
+ }
+ if user.Email != "pat.first@baseline.example.edu" {
+ t.Errorf("user.email: got %q", user.Email)
+ }
+ if user.Status != "ACTIVE" {
+ t.Errorf("user.status: got %q, want ACTIVE", user.Status)
+ }
+
+ var ident struct {
+ Source string `db:"source"`
+ ExternalID string `db:"external_id"`
+ Email *string `db:"email"`
+ UserID string `db:"user_id"`
+ }
+ if err := database.Get(&ident, "SELECT source, external_id, email, user_id FROM user_identities LIMIT 1"); err != nil {
+ t.Fatalf("read user_identity: %v", err)
+ }
+ if ident.Source != "access" {
+ t.Errorf("identity.source: got %q, want access", ident.Source)
+ }
+ if ident.ExternalID != "bl-pi-001" {
+ t.Errorf("identity.external_id: got %q", ident.ExternalID)
+ }
+ if ident.UserID != user.ID {
+ t.Errorf("identity.user_id: got %q, want %q", ident.UserID, user.ID)
+ }
+
+ var proj struct {
+ ID string `db:"id"`
+ OriginatedID string `db:"originated_id"`
+ ProjectPIID string `db:"project_pi_id"`
+ Status string `db:"status"`
+ }
+ if err := database.Get(&proj, "SELECT id, originated_id, project_pi_id, status FROM projects LIMIT 1"); err != nil {
+ t.Fatalf("read project: %v", err)
+ }
+ if proj.OriginatedID != "TG-BL-001" {
+ t.Errorf("project.originated_id: got %q, want TG-BL-001", proj.OriginatedID)
+ }
+ if proj.ProjectPIID != user.ID {
+ t.Errorf("project.project_pi_id: got %q, want %q", proj.ProjectPIID, user.ID)
+ }
+ if proj.Status != "ACTIVE" {
+ t.Errorf("project.status: got %q, want ACTIVE", proj.Status)
+ }
+
+ var alloc struct {
+ ProjectID string `db:"project_id"`
+ Name string `db:"name"`
+ InitialSUAmount int64 `db:"initial_su_amount"`
+ ComputeClusterID string `db:"compute_cluster_id"`
+ }
+ if err := database.Get(&alloc,
+ "SELECT project_id, name, initial_su_amount, compute_cluster_id FROM compute_allocations LIMIT 1",
+ ); err != nil {
+ t.Fatalf("read allocation: %v", err)
+ }
+ if alloc.ProjectID != proj.ID {
+ t.Errorf("allocation.project_id: got %q, want %q", alloc.ProjectID, proj.ID)
+ }
+ if alloc.InitialSUAmount != 1000 {
+ t.Errorf("allocation.initial_su_amount: got %d, want 1000", alloc.InitialSUAmount)
+ }
+ if alloc.ComputeClusterID != testClusterID {
+ t.Errorf("allocation.compute_cluster_id: got %q, want %q", alloc.ComputeClusterID, testClusterID)
+ }
+
+ for _, action := range []model.AuditAction{
+ model.AuditCreatePerson,
+ model.AuditCreateProject,
+ model.AuditCreateAllocation,
+ model.AuditReplySent,
+ } {
+ if got := countAuditActions(t, database, pkt.ID, action); got != 1 {
+ t.Errorf("audit %s: got %d, want 1", action, got)
+ }
+ }
+
+ if got, want := amie.lastReplyType(), "notify_project_create"; got != want {
+ t.Fatalf("reply type: got %q, want %q", got, want)
+ }
+ reply := amie.lastReplyBody()
+ if reply == nil {
+ t.Fatal("reply body missing")
+ }
+ if got, _ := reply["ProjectID"].(string); got != proj.ID {
+ t.Errorf("reply.ProjectID: got %q, want %q", got, proj.ID)
+ }
+ if got, _ := reply["PiPersonID"].(string); got != user.ID {
+ t.Errorf("reply.PiPersonID: got %q, want %q", got, user.ID)
+ }
+ if got, _ := reply["PiRemoteSiteLogin"].(string); got == "" {
+ t.Errorf("reply.PiRemoteSiteLogin: empty; required value")
+ }
+}
+
+// TestRequestProjectCreate_SupplementIsIdempotent asserts the protocol supplement
+// rule: re-delivery of request_project_create against the same GrantNumber
+// MUST NOT create a second Project or a second base Allocation. The grant
+// event is recorded as a compute_allocation_diffs row instead.
+func TestRequestProjectCreate_SupplementIsIdempotent(t *testing.T) {
+ database := setupTestDB(t)
+
+ first := baseRPCBody()
+ firstPkt := insertPacket(t, database, "request_project_create", first)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestProjectCreateHandler(svc, testClusterID, amie, audit)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": firstPkt.Type, "body": first}, firstPkt, "")
+ }); err != nil {
+ t.Fatalf("first Handle: %v", err)
+ }
+
+ second := baseRPCBody()
+ second["AllocationType"] = "supplement"
+ second["RecordID"] = "rec-bl-001-supp"
+ second["ServiceUnitsAllocated"] = "500"
+ secondPkt := insertPacket(t, database, "request_project_create", second)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": secondPkt.Type, "body": second}, secondPkt, "")
+ }); err != nil {
+ t.Fatalf("second Handle: %v", err)
+ }
+
+ if got := countRows(t, database, "projects"); got != 1 {
+ t.Errorf("projects after supplement: got %d, want 1 (no second project)", got)
+ }
+ if got := countRows(t, database, "compute_allocations"); got != 1 {
+ t.Errorf("compute_allocations after supplement: got %d, want 1 (no second base allocation)", got)
+ }
+ if got := countRows(t, database, "compute_allocation_diffs"); got != 1 {
+ t.Errorf("compute_allocation_diffs: got %d, want 1 (supplement records a diff)", got)
+ }
+
+ var diff struct {
+ DiffType string `db:"diff_type"`
+ NewSUAmount int64 `db:"new_su_amount"`
+ }
+ if err := database.Get(&diff,
+ "SELECT diff_type, new_su_amount FROM compute_allocation_diffs LIMIT 1",
+ ); err != nil {
+ t.Fatalf("read diff: %v", err)
+ }
+ if diff.DiffType != "SUPPLEMENT" {
+ t.Errorf("diff.diff_type: got %q, want SUPPLEMENT", diff.DiffType)
+ }
+ if diff.NewSUAmount != 500 {
+ t.Errorf("diff.new_su_amount: got %d, want 500 (this packet's delta only, not cumulative)", diff.NewSUAmount)
+ }
+}
+
+// TestRequestProjectCreate_MissingGrantNumber asserts the handler rejects a
+// packet missing a the protocol-required field, without mutating the DB.
+func TestRequestProjectCreate_MissingGrantNumber(t *testing.T) {
+ database := setupTestDB(t)
+
+ body := baseRPCBody()
+ delete(body, "GrantNumber")
+ pkt := insertPacket(t, database, "request_project_create", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestProjectCreateHandler(svc, testClusterID, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error for missing GrantNumber, got nil")
+ }
+ if !strings.Contains(err.Error(), "GrantNumber") {
+ t.Errorf("error should mention GrantNumber, got: %v", err)
+ }
+ if got := countRows(t, database, "projects"); got != 0 {
+ t.Errorf("projects on validation failure: got %d, want 0", got)
+ }
+ if got := countRows(t, database, "users"); got != 0 {
+ t.Errorf("users on validation failure: got %d, want 0", got)
+ }
+ if len(amie.Replies) != 0 {
+ t.Errorf("amie replies on validation failure: got %d, want 0", len(amie.Replies))
+ }
+}
+
+// TestRequestProjectCreate_ReplyFailurePropagates asserts the handler
+// surfaces a ReplyToPacket failure to the caller so the processor can decide
+// to retry. The protocol does not explicitly mandate atomicity on reply
+// failure, so this test does not check rollback shape. (Design gap: core
+// service writes commit in their own txns, only audit + reply share the
+// handler's tx, tracked separately.)
+func TestRequestProjectCreate_ReplyFailurePropagates(t *testing.T) {
+ database := setupTestDB(t)
+
+ body := baseRPCBody()
+ pkt := insertPacket(t, database, "request_project_create", body)
+
+ svc := newTestCoreService(database)
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{FailWith: errors.New("simulated AMIE outage")}
+ h := NewRequestProjectCreateHandler(svc, testClusterID, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error when reply fails, got nil")
+ }
+ if !strings.Contains(err.Error(), "reply") && !strings.Contains(err.Error(), "AMIE") {
+ t.Errorf("error should reference reply path, got: %v", err)
+ }
+ // REPLY_SENT audit row must NOT exist when the reply failed.
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("audit REPLY_SENT on failed reply: got %d, want 0", got)
+ }
+}
+
+// silence unused-import false positives when only some tests reference these.
+var _ = model.AuditCreatePerson
diff --git a/connectors/ACCESS/AMIE-Processor/handler/request_project_inactivate_integration_test.go b/connectors/ACCESS/AMIE-Processor/handler/request_project_inactivate_integration_test.go
new file mode 100644
index 000000000..454c677bb
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/handler/request_project_inactivate_integration_test.go
@@ -0,0 +1,409 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package handler
+
+import (
+ "context"
+ "database/sql"
+ "errors"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/jmoiron/sqlx"
+
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/model"
+ "github.com/apache/airavata-custos/pkg/models"
+ coreservice "github.com/apache/airavata-custos/pkg/service"
+)
+
+// seededProject groups the rows seeded by seedActiveProject so tests can read
+// back the expected IDs after the handler runs.
+type seededProject struct {
+ OrgID string
+ PIUserID string
+ OtherUserID string
+ ProjectID string
+ AllocationID string
+ PIMembership string
+ OtherMember string
+}
+
+// seedActiveProject builds the pre-existing state every request_project_*
+// activate/inactivate test starts from: one organization, a PI user, a
+// non-PI user, an ACTIVE project, one ACTIVE allocation under it, and an
+// ACTIVE membership for each user on that allocation.
+//
+// Tests exercising the protocol's PI-only-reactivate asymmetry need both members to
+// observe that the non-PI membership is NOT touched on reactivate. The
+// inactivate test reuses the same shape because the protocol says ALL accounts on the
+// project must be inactivated.
+func seedActiveProject(t *testing.T, database *sqlx.DB, svc *coreservice.Service) seededProject {
+ t.Helper()
+ ctx := context.Background()
+
+ org, err := svc.CreateOrganization(ctx, &models.Organization{
+ OriginatedID: "SEED-ORG",
+ Name: "Seed Org",
+ })
+ if err != nil {
+ t.Fatalf("seed organization: %v", err)
+ }
+
+ pi, err := svc.CreateUser(ctx, &models.User{
+ OrganizationID: org.ID,
+ FirstName: "Seed",
+ LastName: "PI",
+ Email: "seed.pi@example.edu",
+ })
+ if err != nil {
+ t.Fatalf("seed PI user: %v", err)
+ }
+ if _, err := svc.CreateUserIdentity(ctx, &models.UserIdentity{
+ UserID: pi.ID,
+ Source: amieIdentitySource,
+ ExternalID: "seed-pi-global",
+ Email: pi.Email,
+ }); err != nil {
+ t.Fatalf("seed PI identity: %v", err)
+ }
+
+ other, err := svc.CreateUser(ctx, &models.User{
+ OrganizationID: org.ID,
+ FirstName: "Seed",
+ LastName: "Member",
+ Email: "seed.member@example.edu",
+ })
+ if err != nil {
+ t.Fatalf("seed non-PI user: %v", err)
+ }
+ if _, err := svc.CreateUserIdentity(ctx, &models.UserIdentity{
+ UserID: other.ID,
+ Source: amieIdentitySource,
+ ExternalID: "seed-member-global",
+ Email: other.Email,
+ }); err != nil {
+ t.Fatalf("seed non-PI identity: %v", err)
+ }
+
+ project, err := svc.CreateProject(ctx, &models.Project{
+ OriginatedID: "TG-SEED-001",
+ Title: "TG-SEED-001",
+ Origination: amieIdentitySource,
+ ProjectPIID: pi.ID,
+ })
+ if err != nil {
+ t.Fatalf("seed project: %v", err)
+ }
+
+ alloc, err := svc.CreateComputeAllocation(ctx, &models.ComputeAllocation{
+ ProjectID: project.ID,
+ Name: "TG-SEED-001",
+ ComputeClusterID: testClusterID,
+ InitialSUAmount: 1000,
+ StartTime: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC),
+ EndTime: time.Date(2027, 1, 1, 0, 0, 0, 0, time.UTC),
+ })
+ if err != nil {
+ t.Fatalf("seed allocation: %v", err)
+ }
+
+ piMem, err := svc.CreateComputeAllocationMembership(ctx, &models.ComputeAllocationMembership{
+ ComputeAllocationID: alloc.ID,
+ UserID: pi.ID,
+ StartTime: alloc.StartTime,
+ EndTime: alloc.EndTime,
+ })
+ if err != nil {
+ t.Fatalf("seed PI membership: %v", err)
+ }
+ otherMem, err := svc.CreateComputeAllocationMembership(ctx, &models.ComputeAllocationMembership{
+ ComputeAllocationID: alloc.ID,
+ UserID: other.ID,
+ StartTime: alloc.StartTime,
+ EndTime: alloc.EndTime,
+ })
+ if err != nil {
+ t.Fatalf("seed non-PI membership: %v", err)
+ }
+
+ return seededProject{
+ OrgID: org.ID,
+ PIUserID: pi.ID,
+ OtherUserID: other.ID,
+ ProjectID: project.ID,
+ AllocationID: alloc.ID,
+ PIMembership: piMem.ID,
+ OtherMember: otherMem.ID,
+ }
+}
+
+// baseRPInactivateBody returns a fully populated request_project_inactivate
+// body with every required field. ProjectID is filled in by each test from
+// the seeded project.
+func baseRPInactivateBody(projectID string) map[string]any {
+ return map[string]any{
+ "ProjectID": projectID,
+ "ResourceList": []any{"compute.access-ci.org"},
+ "Comment": "End of allocation period",
+ }
+}
+
+func membershipStatus(t *testing.T, database *sqlx.DB, id string) models.AllocationStatus {
+ t.Helper()
+ var status string
+ if err := database.Get(&status,
+ "SELECT membership_status FROM compute_allocation_memberships WHERE id = ?", id,
+ ); err != nil {
+ t.Fatalf("read membership %s: %v", id, err)
+ }
+ return models.AllocationStatus(status)
+}
+
+// TestRequestProjectInactivate_HappyPath asserts the protocol contract:
+//
+// - the Project flips to INACTIVE.
+// - every ComputeAllocation under the project flips to INACTIVE (the
+// connector models the protocol's "suspended or expired" as INACTIVE).
+// - every membership under the project flips to INACTIVE.
+// - the handler replies notify_project_inactivate and emits the
+// INACTIVATE_PROJECT / INACTIVATE_MEMBERSHIP / REPLY_SENT audit rows.
+func TestRequestProjectInactivate_HappyPath(t *testing.T) {
+ database := setupTestDB(t)
+ svc := newTestCoreService(database)
+ seed := seedActiveProject(t, database, svc)
+
+ body := baseRPInactivateBody(seed.ProjectID)
+ pkt := insertPacket(t, database, "request_project_inactivate", body)
+
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestProjectInactivateHandler(svc, amie, audit)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ // Project must be INACTIVE.
+ proj, err := svc.GetProject(context.Background(), seed.ProjectID)
+ if err != nil {
+ t.Fatalf("read project: %v", err)
+ }
+ if proj.Status != models.ProjectInactive {
+ t.Errorf("project.status: got %q, want %q", proj.Status, models.ProjectInactive)
+ }
+
+ // Allocation must be INACTIVE.
+ alloc, err := svc.GetComputeAllocation(context.Background(), seed.AllocationID)
+ if err != nil {
+ t.Fatalf("read allocation: %v", err)
+ }
+ if alloc.Status != models.INACTIVE {
+ t.Errorf("allocation.status: got %q, want %q", alloc.Status, models.INACTIVE)
+ }
+
+ // Both memberships (PI and non-PI) must be INACTIVE, the protocol says ALL accounts
+ // on the project are inactivated.
+ if got := membershipStatus(t, database, seed.PIMembership); got != models.INACTIVE {
+ t.Errorf("PI membership status: got %q, want %q", got, models.INACTIVE)
+ }
+ if got := membershipStatus(t, database, seed.OtherMember); got != models.INACTIVE {
+ t.Errorf("non-PI membership status: got %q, want %q", got, models.INACTIVE)
+ }
+
+ // A status-change diff row is recorded for the allocation.
+ if got := countRows(t, database, "compute_allocation_diffs"); got != 1 {
+ t.Errorf("compute_allocation_diffs: got %d, want 1", got)
+ }
+ var diff struct {
+ DiffType string `db:"diff_type"`
+ Status string `db:"status"`
+ }
+ if err := database.Get(&diff,
+ "SELECT diff_type, status FROM compute_allocation_diffs LIMIT 1",
+ ); err != nil {
+ t.Fatalf("read diff: %v", err)
+ }
+ if diff.DiffType != "ALLOCATION_STATUS_CHANGE" {
+ t.Errorf("diff.diff_type: got %q, want ALLOCATION_STATUS_CHANGE", diff.DiffType)
+ }
+ if diff.Status != string(models.INACTIVE) {
+ t.Errorf("diff.status: got %q, want %q", diff.Status, models.INACTIVE)
+ }
+
+ // Audit trail: INACTIVATE_PROJECT, INACTIVATE_MEMBERSHIP (x2), REPLY_SENT.
+ if got := countAuditActions(t, database, pkt.ID, model.AuditInactivateProject); got != 1 {
+ t.Errorf("audit INACTIVATE_PROJECT: got %d, want 1", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditInactivateMembership); got != 2 {
+ t.Errorf("audit INACTIVATE_MEMBERSHIP: got %d, want 2 (PI + non-PI)", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT: got %d, want 1", got)
+ }
+
+ // Reply: type and ProjectID.
+ if got, want := amie.lastReplyType(), "notify_project_inactivate"; got != want {
+ t.Fatalf("reply type: got %q, want %q", got, want)
+ }
+ reply := amie.lastReplyBody()
+ if reply == nil {
+ t.Fatal("reply body missing")
+ }
+ if got, _ := reply["ProjectID"].(string); got != seed.ProjectID {
+ t.Errorf("reply.ProjectID: got %q, want %q", got, seed.ProjectID)
+ }
+}
+
+// TestRequestProjectInactivate_UnknownProject asserts the handler's current
+// soft-skip behavior: when the ProjectID does not resolve to a local row,
+// the handler logs a warning, still sends the reply, and returns success.
+//
+// Spec context: the protocol is silent on the unknown-project case. The connector's
+// "soft skip" behavior is a known design concern (it may mask real bugs in
+// production). Mirroring the existing handler shape until the contract is
+// clarified upstream.
+func TestRequestProjectInactivate_UnknownProject(t *testing.T) {
+ database := setupTestDB(t)
+ svc := newTestCoreService(database)
+
+ body := baseRPInactivateBody("does-not-exist-uuid")
+ pkt := insertPacket(t, database, "request_project_inactivate", body)
+
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestProjectInactivateHandler(svc, amie, audit)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error for unknown project: %v (B7 in bugs tracking documents this soft skip)", err)
+ }
+
+ // No state mutations: nothing to mutate.
+ if got := countRows(t, database, "projects"); got != 0 {
+ t.Errorf("projects: got %d, want 0", got)
+ }
+ if got := countRows(t, database, "compute_allocations"); got != 0 {
+ t.Errorf("compute_allocations: got %d, want 0", got)
+ }
+ if got := countRows(t, database, "compute_allocation_diffs"); got != 0 {
+ t.Errorf("compute_allocation_diffs: got %d, want 0", got)
+ }
+
+ // No INACTIVATE_PROJECT audit, only REPLY_SENT.
+ if got := countAuditActions(t, database, pkt.ID, model.AuditInactivateProject); got != 0 {
+ t.Errorf("audit INACTIVATE_PROJECT on unknown project: got %d, want 0", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT on unknown project: got %d, want 1", got)
+ }
+
+ // Reply still sent.
+ if got, want := amie.lastReplyType(), "notify_project_inactivate"; got != want {
+ t.Fatalf("reply type: got %q, want %q", got, want)
+ }
+}
+
+// TestRequestProjectInactivate_MissingProjectID asserts the handler rejects
+// a packet missing the protocol-required ProjectID field, without mutating the DB.
+func TestRequestProjectInactivate_MissingProjectID(t *testing.T) {
+ database := setupTestDB(t)
+ svc := newTestCoreService(database)
+ seed := seedActiveProject(t, database, svc)
+
+ body := baseRPInactivateBody(seed.ProjectID)
+ delete(body, "ProjectID")
+ pkt := insertPacket(t, database, "request_project_inactivate", body)
+
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestProjectInactivateHandler(svc, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error for missing ProjectID, got nil")
+ }
+ if !strings.Contains(err.Error(), "ProjectID") {
+ t.Errorf("error should mention ProjectID, got: %v", err)
+ }
+
+ // Seeded project must remain ACTIVE, no mutations on validation failure.
+ proj, err := svc.GetProject(context.Background(), seed.ProjectID)
+ if err != nil {
+ t.Fatalf("read seeded project: %v", err)
+ }
+ if proj.Status != models.ProjectActive {
+ t.Errorf("project.status after validation failure: got %q, want %q (ACTIVE)", proj.Status, models.ProjectActive)
+ }
+ alloc, err := svc.GetComputeAllocation(context.Background(), seed.AllocationID)
+ if err != nil {
+ t.Fatalf("read seeded allocation: %v", err)
+ }
+ if alloc.Status != models.ACTIVE {
+ t.Errorf("allocation.status after validation failure: got %q, want %q", alloc.Status, models.ACTIVE)
+ }
+ if got := membershipStatus(t, database, seed.PIMembership); got != models.ACTIVE {
+ t.Errorf("PI membership after validation failure: got %q, want %q", got, models.ACTIVE)
+ }
+
+ if len(amie.Replies) != 0 {
+ t.Errorf("amie replies on validation failure: got %d, want 0", len(amie.Replies))
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditInactivateProject); got != 0 {
+ t.Errorf("audit INACTIVATE_PROJECT on validation failure: got %d, want 0", got)
+ }
+}
+
+// TestRequestProjectInactivate_ReplyFailurePropagates asserts the handler
+// surfaces a ReplyToPacket failure to the caller. REPLY_SENT must NOT exist
+// when the reply failed. The protocol does not mandate atomicity on reply
+// failure, so this test does not check rollback shape.
+func TestRequestProjectInactivate_ReplyFailurePropagates(t *testing.T) {
+ database := setupTestDB(t)
+ svc := newTestCoreService(database)
+ seed := seedActiveProject(t, database, svc)
+
+ body := baseRPInactivateBody(seed.ProjectID)
+ pkt := insertPacket(t, database, "request_project_inactivate", body)
+
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{FailWith: errors.New("simulated AMIE outage")}
+ h := NewRequestProjectInactivateHandler(svc, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error when reply fails, got nil")
+ }
+ if !strings.Contains(err.Error(), "reply") && !strings.Contains(err.Error(), "AMIE") {
+ t.Errorf("error should reference reply path, got: %v", err)
+ }
+
+ // REPLY_SENT audit row must NOT exist when the reply failed.
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("audit REPLY_SENT on failed reply: got %d, want 0", got)
+ }
+}
diff --git a/connectors/ACCESS/AMIE-Processor/handler/request_project_reactivate_integration_test.go b/connectors/ACCESS/AMIE-Processor/handler/request_project_reactivate_integration_test.go
new file mode 100644
index 000000000..f3dd32ad9
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/handler/request_project_reactivate_integration_test.go
@@ -0,0 +1,301 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package handler
+
+import (
+ "context"
+ "database/sql"
+ "errors"
+ "strings"
+ "testing"
+
+ "github.com/jmoiron/sqlx"
+
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/model"
+ "github.com/apache/airavata-custos/pkg/models"
+ coreservice "github.com/apache/airavata-custos/pkg/service"
+)
+
+// inactivateSeeded flips the seeded project, allocation, and memberships to
+// INACTIVE so a request_project_reactivate test starts from the
+// "project previously inactivated" precondition stated in the protocol line 451.
+func inactivateSeeded(t *testing.T, database *sqlx.DB, svc *coreservice.Service, seed seededProject) {
+ t.Helper()
+ ctx := context.Background()
+ if _, err := svc.UpdateProjectStatus(ctx, seed.ProjectID, models.ProjectInactive); err != nil {
+ t.Fatalf("inactivate seeded project: %v", err)
+ }
+ alloc, err := svc.GetComputeAllocation(ctx, seed.AllocationID)
+ if err != nil {
+ t.Fatalf("read seeded allocation: %v", err)
+ }
+ alloc.Status = models.INACTIVE
+ if err := svc.UpdateComputeAllocation(ctx, alloc); err != nil {
+ t.Fatalf("inactivate seeded allocation: %v", err)
+ }
+ if _, err := svc.UpdateMembershipStatus(ctx, seed.PIMembership, models.INACTIVE); err != nil {
+ t.Fatalf("inactivate PI membership: %v", err)
+ }
+ if _, err := svc.UpdateMembershipStatus(ctx, seed.OtherMember, models.INACTIVE); err != nil {
+ t.Fatalf("inactivate non-PI membership: %v", err)
+ }
+}
+
+// baseRPReactivateBody returns a fully populated request_project_reactivate
+// body. ProjectID and ResourceList are required; PersonID is allowed and
+// carries the PI.
+func baseRPReactivateBody(projectID, piGlobalID string) map[string]any {
+ return map[string]any{
+ "ProjectID": projectID,
+ "ResourceList": []any{"compute.access-ci.org"},
+ "PersonID": piGlobalID,
+ "Comment": "Reactivation requested",
+ }
+}
+
+// TestRequestProjectReactivate_HappyPath asserts the protocol contract:
+//
+// - the Project flips back to ACTIVE.
+// - every ComputeAllocation under the project flips to ACTIVE.
+// - only the PI's membership reactivates; other members stay INACTIVE
+// .
+// - the handler replies notify_project_reactivate and emits the
+// REACTIVATE_PROJECT / REACTIVATE_MEMBERSHIP / REPLY_SENT audit rows.
+func TestRequestProjectReactivate_HappyPath(t *testing.T) {
+ database := setupTestDB(t)
+ svc := newTestCoreService(database)
+ seed := seedActiveProject(t, database, svc)
+ inactivateSeeded(t, database, svc, seed)
+
+ body := baseRPReactivateBody(seed.ProjectID, "seed-pi-global")
+ pkt := insertPacket(t, database, "request_project_reactivate", body)
+
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestProjectReactivateHandler(svc, amie, audit)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ // Project must be ACTIVE again.
+ proj, err := svc.GetProject(context.Background(), seed.ProjectID)
+ if err != nil {
+ t.Fatalf("read project: %v", err)
+ }
+ if proj.Status != models.ProjectActive {
+ t.Errorf("project.status: got %q, want %q", proj.Status, models.ProjectActive)
+ }
+
+ // Allocation must be ACTIVE.
+ alloc, err := svc.GetComputeAllocation(context.Background(), seed.AllocationID)
+ if err != nil {
+ t.Fatalf("read allocation: %v", err)
+ }
+ if alloc.Status != models.ACTIVE {
+ t.Errorf("allocation.status: got %q, want %q", alloc.Status, models.ACTIVE)
+ }
+
+ // the protocol asymmetry: PI membership ACTIVE, non-PI membership still INACTIVE.
+ if got := membershipStatus(t, database, seed.PIMembership); got != models.ACTIVE {
+ t.Errorf("PI membership status: got %q, want %q", got, models.ACTIVE)
+ }
+ if got := membershipStatus(t, database, seed.OtherMember); got != models.INACTIVE {
+ t.Errorf("non-PI membership status: got %q, want %q", got, models.INACTIVE)
+ }
+
+ // One status-change diff for the allocation.
+ if got := countRows(t, database, "compute_allocation_diffs"); got != 1 {
+ t.Errorf("compute_allocation_diffs: got %d, want 1", got)
+ }
+ var diff struct {
+ DiffType string `db:"diff_type"`
+ Status string `db:"status"`
+ }
+ if err := database.Get(&diff,
+ "SELECT diff_type, status FROM compute_allocation_diffs LIMIT 1",
+ ); err != nil {
+ t.Fatalf("read diff: %v", err)
+ }
+ if diff.DiffType != "ALLOCATION_STATUS_CHANGE" {
+ t.Errorf("diff.diff_type: got %q, want ALLOCATION_STATUS_CHANGE", diff.DiffType)
+ }
+ if diff.Status != string(models.ACTIVE) {
+ t.Errorf("diff.status: got %q, want %q", diff.Status, models.ACTIVE)
+ }
+
+ // Audit trail: REACTIVATE_PROJECT, REACTIVATE_MEMBERSHIP (1, PI only),
+ // REPLY_SENT. No INACTIVATE_MEMBERSHIP rows from the non-PI member.
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReactivateProject); got != 1 {
+ t.Errorf("audit REACTIVATE_PROJECT: got %d, want 1", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReactivateMembership); got != 1 {
+ t.Errorf("audit REACTIVATE_MEMBERSHIP: got %d, want 1 (PI only, the protocol)", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT: got %d, want 1", got)
+ }
+
+ // Reply: type and ProjectID.
+ if got, want := amie.lastReplyType(), "notify_project_reactivate"; got != want {
+ t.Fatalf("reply type: got %q, want %q", got, want)
+ }
+ reply := amie.lastReplyBody()
+ if reply == nil {
+ t.Fatal("reply body missing")
+ }
+ if got, _ := reply["ProjectID"].(string); got != seed.ProjectID {
+ t.Errorf("reply.ProjectID: got %q, want %q", got, seed.ProjectID)
+ }
+}
+
+// TestRequestProjectReactivate_UnknownProject asserts the handler's current
+// soft-skip behavior when the ProjectID does not resolve to a local row.
+//
+// Spec context: the protocol is silent on the unknown-project case. The connector's
+// "soft skip" behavior is a known design concern (may mask real bugs in
+// production).
+func TestRequestProjectReactivate_UnknownProject(t *testing.T) {
+ database := setupTestDB(t)
+ svc := newTestCoreService(database)
+
+ body := baseRPReactivateBody("does-not-exist-uuid", "seed-pi-global")
+ pkt := insertPacket(t, database, "request_project_reactivate", body)
+
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestProjectReactivateHandler(svc, amie, audit)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error for unknown project: %v (B7 documents this soft skip)", err)
+ }
+
+ // No state mutations: nothing to mutate.
+ if got := countRows(t, database, "projects"); got != 0 {
+ t.Errorf("projects: got %d, want 0", got)
+ }
+ if got := countRows(t, database, "compute_allocations"); got != 0 {
+ t.Errorf("compute_allocations: got %d, want 0", got)
+ }
+ if got := countRows(t, database, "compute_allocation_diffs"); got != 0 {
+ t.Errorf("compute_allocation_diffs: got %d, want 0", got)
+ }
+
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReactivateProject); got != 0 {
+ t.Errorf("audit REACTIVATE_PROJECT on unknown project: got %d, want 0", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT on unknown project: got %d, want 1", got)
+ }
+
+ if got, want := amie.lastReplyType(), "notify_project_reactivate"; got != want {
+ t.Fatalf("reply type: got %q, want %q", got, want)
+ }
+}
+
+// TestRequestProjectReactivate_MissingProjectID asserts the handler rejects
+// a packet missing the protocol-required ProjectID field, without mutating the DB.
+func TestRequestProjectReactivate_MissingProjectID(t *testing.T) {
+ database := setupTestDB(t)
+ svc := newTestCoreService(database)
+ seed := seedActiveProject(t, database, svc)
+ inactivateSeeded(t, database, svc, seed)
+
+ body := baseRPReactivateBody(seed.ProjectID, "seed-pi-global")
+ delete(body, "ProjectID")
+ pkt := insertPacket(t, database, "request_project_reactivate", body)
+
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{}
+ h := NewRequestProjectReactivateHandler(svc, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error for missing ProjectID, got nil")
+ }
+ if !strings.Contains(err.Error(), "ProjectID") {
+ t.Errorf("error should mention ProjectID, got: %v", err)
+ }
+
+ // Seeded project must remain INACTIVE, no mutations on validation failure.
+ proj, err := svc.GetProject(context.Background(), seed.ProjectID)
+ if err != nil {
+ t.Fatalf("read seeded project: %v", err)
+ }
+ if proj.Status != models.ProjectInactive {
+ t.Errorf("project.status after validation failure: got %q, want %q (INACTIVE)", proj.Status, models.ProjectInactive)
+ }
+ alloc, err := svc.GetComputeAllocation(context.Background(), seed.AllocationID)
+ if err != nil {
+ t.Fatalf("read seeded allocation: %v", err)
+ }
+ if alloc.Status != models.INACTIVE {
+ t.Errorf("allocation.status after validation failure: got %q, want %q", alloc.Status, models.INACTIVE)
+ }
+ if got := membershipStatus(t, database, seed.PIMembership); got != models.INACTIVE {
+ t.Errorf("PI membership after validation failure: got %q, want %q", got, models.INACTIVE)
+ }
+
+ if len(amie.Replies) != 0 {
+ t.Errorf("amie replies on validation failure: got %d, want 0", len(amie.Replies))
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReactivateProject); got != 0 {
+ t.Errorf("audit REACTIVATE_PROJECT on validation failure: got %d, want 0", got)
+ }
+}
+
+// TestRequestProjectReactivate_ReplyFailurePropagates asserts the handler
+// surfaces a ReplyToPacket failure to the caller. REPLY_SENT must NOT exist
+// when the reply failed. (Core writes commit in their own txns, so this
+// test does not check rollback shape.)
+func TestRequestProjectReactivate_ReplyFailurePropagates(t *testing.T) {
+ database := setupTestDB(t)
+ svc := newTestCoreService(database)
+ seed := seedActiveProject(t, database, svc)
+ inactivateSeeded(t, database, svc, seed)
+
+ body := baseRPReactivateBody(seed.ProjectID, "seed-pi-global")
+ pkt := insertPacket(t, database, "request_project_reactivate", body)
+
+ audit := newTestAuditService(database)
+ amie := &fakeAmieClient{FailWith: errors.New("simulated AMIE outage")}
+ h := NewRequestProjectReactivateHandler(svc, amie, audit)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error when reply fails, got nil")
+ }
+ if !strings.Contains(err.Error(), "reply") && !strings.Contains(err.Error(), "AMIE") {
+ t.Errorf("error should reference reply path, got: %v", err)
+ }
+
+ // REPLY_SENT audit row must NOT exist when the reply failed.
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("audit REPLY_SENT on failed reply: got %d, want 0", got)
+ }
+}
diff --git a/connectors/ACCESS/AMIE-Processor/handler/request_user_modify_integration_test.go b/connectors/ACCESS/AMIE-Processor/handler/request_user_modify_integration_test.go
new file mode 100644
index 000000000..6e98f5133
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/handler/request_user_modify_integration_test.go
@@ -0,0 +1,620 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package handler
+
+import (
+ "context"
+ "database/sql"
+ "encoding/json"
+ "errors"
+ "sort"
+ "strings"
+ "testing"
+
+ "github.com/google/uuid"
+ "github.com/jmoiron/sqlx"
+
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/model"
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/store"
+ "github.com/apache/airavata-custos/pkg/models"
+)
+
+// seedUser inserts an organization, a user, a user_identity bound to (access,
+// userGlobalID), and the supplied DNs into amie_user_dns. It returns the
+// inserted user ID, user_identity ID, and DN row IDs so tests can assert
+// downstream mutations relative to the seeded state.
+func seedUser(t *testing.T, database *sqlx.DB, userGlobalID, firstName, lastName, email string, dns []string) (userID, identityID string, dnIDs []string) {
+ t.Helper()
+ orgID := uuid.NewString()
+ if _, err := database.Exec(
+ "INSERT INTO organizations (id, originated_id, name) VALUES (?, ?, ?)",
+ orgID, "SEED-ORG", "Seed Org",
+ ); err != nil {
+ t.Fatalf("seed organization: %v", err)
+ }
+ userID = uuid.NewString()
+ if _, err := database.Exec(
+ "INSERT INTO users (id, organization_id, first_name, last_name, middle_name, email, status) VALUES (?, ?, ?, ?, ?, ?, ?)",
+ userID, orgID, firstName, lastName, "", email, string(models.UserActive),
+ ); err != nil {
+ t.Fatalf("seed user: %v", err)
+ }
+ identityID = uuid.NewString()
+ seedMeta, err := json.Marshal(map[string]any{
+ "organization": "Old Org",
+ "org_code": "OLD-ORG",
+ "nsf_status_code": "PENDING",
+ })
+ if err != nil {
+ t.Fatalf("marshal seed metadata: %v", err)
+ }
+ if _, err := database.Exec(
+ "INSERT INTO user_identities (id, user_id, source, external_id, email, metadata) VALUES (?, ?, ?, ?, ?, ?)",
+ identityID, userID, "access", userGlobalID, email, string(seedMeta),
+ ); err != nil {
+ t.Fatalf("seed user_identity: %v", err)
+ }
+ for _, dn := range dns {
+ dnID := uuid.NewString()
+ if _, err := database.Exec(
+ "INSERT INTO amie_user_dns (id, user_id, dn) VALUES (?, ?, ?)",
+ dnID, userID, dn,
+ ); err != nil {
+ t.Fatalf("seed amie_user_dns: %v", err)
+ }
+ dnIDs = append(dnIDs, dnID)
+ }
+ return userID, identityID, dnIDs
+}
+
+// listUserDNs reads the DN strings currently stored for a given user, sorted
+// for stable comparison.
+func listUserDNs(t *testing.T, database *sqlx.DB, userID string) []string {
+ t.Helper()
+ var dns []string
+ if err := database.Select(&dns,
+ "SELECT dn FROM amie_user_dns WHERE user_id = ? ORDER BY dn",
+ userID,
+ ); err != nil {
+ t.Fatalf("list amie_user_dns: %v", err)
+ }
+ return dns
+}
+
+// newRequestUserModifyHandlerForTest wires the handler with the package's
+// real services + the supplied fake AMIE client and a freshly minted
+// UserDNStore on the shared connection.
+func newRequestUserModifyHandlerForTest(database *sqlx.DB, amie *fakeAmieClient) *RequestUserModifyHandler {
+ return NewRequestUserModifyHandler(
+ newTestCoreService(database),
+ store.NewUserDNStore(database),
+ amie,
+ newTestAuditService(database),
+ )
+}
+
+// baseUserModifyBody builds a baseline request_user_modify body with the
+// fields the handler actually reads. Tests mutate the returned map.
+func baseUserModifyBody(actionType, userGlobalID string) map[string]any {
+ return map[string]any{
+ "ActionType": actionType,
+ "UserGlobalID": userGlobalID,
+ }
+}
+
+// TestRequestUserModify_ReplaceHappyPath: ActionType=replace with a full
+// body. The handler MUST:
+// - update the User row's basic profile from the packet
+// - update the UserIdentity row's email + source-specific metadata
+// (organization, org_code, nsf_status_code)
+// - reconcile the user's DN list against the packet's DnList
+// - reply inform_transaction_complete with StatusCode=Success
+func TestRequestUserModify_ReplaceHappyPath(t *testing.T) {
+ database := setupTestDB(t)
+
+ const (
+ userGlobalID = "amie-user-1001"
+ seedDN1 = "/C=US/O=Old/CN=Old User A"
+ seedDN2 = "/C=US/O=Old/CN=Old User B"
+ newDN = "/C=US/O=New Org/CN=New User"
+ )
+ userID, identityID, _ := seedUser(t, database,
+ userGlobalID, "Old", "Name", "old@example.edu",
+ []string{seedDN1, seedDN2},
+ )
+
+ body := baseUserModifyBody("replace", userGlobalID)
+ body["UserFirstName"] = "New"
+ body["UserLastName"] = "Name"
+ body["UserEmail"] = "new@example.edu"
+ body["UserOrganization"] = "New Org"
+ body["UserOrgCode"] = "NEW-ORG"
+ body["NsfStatusCode"] = "ACTIVE"
+ // Replace semantics: presence of DnList means the local DN set must be
+ // replaced with the packet's set. seedDN1 is retained and seedDN2 must
+ // be dropped.
+ body["DnList"] = []any{seedDN1, newDN}
+
+ pkt := insertPacket(t, database, "request_user_modify", body)
+ amie := &fakeAmieClient{}
+ h := newRequestUserModifyHandlerForTest(database, amie)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ // User row: first name, last name, email updated.
+ var user struct {
+ FirstName string `db:"first_name"`
+ LastName string `db:"last_name"`
+ Email string `db:"email"`
+ }
+ if err := database.Get(&user, "SELECT first_name, last_name, email FROM users WHERE id = ?", userID); err != nil {
+ t.Fatalf("read user: %v", err)
+ }
+ if user.FirstName != "New" {
+ t.Errorf("user.first_name: got %q, want New", user.FirstName)
+ }
+ if user.LastName != "Name" {
+ t.Errorf("user.last_name: got %q, want Name", user.LastName)
+ }
+ if user.Email != "new@example.edu" {
+ t.Errorf("user.email: got %q, want new@example.edu", user.Email)
+ }
+
+ // UserIdentity row: email + metadata updated.
+ var ident struct {
+ Email *string `db:"email"`
+ Metadata *string `db:"metadata"`
+ }
+ if err := database.Get(&ident, "SELECT email, metadata FROM user_identities WHERE id = ?", identityID); err != nil {
+ t.Fatalf("read user_identity: %v", err)
+ }
+ if ident.Email == nil || *ident.Email != "new@example.edu" {
+ got := "<nil>"
+ if ident.Email != nil {
+ got = *ident.Email
+ }
+ t.Errorf("user_identity.email: got %q, want new@example.edu", got)
+ }
+ if ident.Metadata == nil {
+ t.Fatalf("user_identity.metadata: nil; want JSON with new org / org_code / nsf_status_code")
+ }
+ var meta map[string]any
+ if err := json.Unmarshal([]byte(*ident.Metadata), &meta); err != nil {
+ t.Fatalf("decode metadata: %v", err)
+ }
+ if got, _ := meta["organization"].(string); got != "New Org" {
+ t.Errorf("metadata.organization: got %q, want New Org", got)
+ }
+ if got, _ := meta["org_code"].(string); got != "NEW-ORG" {
+ t.Errorf("metadata.org_code: got %q, want NEW-ORG", got)
+ }
+ if got, _ := meta["nsf_status_code"].(string); got != "ACTIVE" {
+ t.Errorf("metadata.nsf_status_code: got %q, want ACTIVE", got)
+ }
+
+ // DN reconciliation: seedDN1 kept, seedDN2 removed, newDN added.
+ got := listUserDNs(t, database, userID)
+ want := []string{newDN, seedDN1}
+ sort.Strings(want)
+ if !equalStringSlices(got, want) {
+ t.Errorf("DN set: got %v, want %v", got, want)
+ }
+
+ // Audits.
+ if got := countAuditActions(t, database, pkt.ID, model.AuditUpdatePerson); got != 1 {
+ t.Errorf("audit UPDATE_PERSON: got %d, want 1", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditPersistDNs); got != 1 {
+ t.Errorf("audit PERSIST_DNS: got %d, want 1 (added + removed > 0)", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT: got %d, want 1", got)
+ }
+
+ // Reply: inform_transaction_complete with Success / DetailCode=1.
+ if got, want := amie.lastReplyType(), "inform_transaction_complete"; got != want {
+ t.Errorf("reply type: got %q, want %q", got, want)
+ }
+ reply := amie.lastReplyBody()
+ if reply == nil {
+ t.Fatal("reply body missing")
+ }
+ if got, _ := reply["StatusCode"].(string); got != "Success" {
+ t.Errorf("reply.StatusCode: got %q, want Success", got)
+ }
+}
+
+// TestRequestUserModify_ReplaceIdempotentSameDNs, re-handling the same
+// replace packet must not produce duplicate DN rows or extra PERSIST_DNS audit
+// activity beyond what each handle naturally emits. Replace is row-level
+// convergent (every update sets the same target state).
+func TestRequestUserModify_ReplaceIdempotentSameDNs(t *testing.T) {
+ database := setupTestDB(t)
+
+ const (
+ userGlobalID = "amie-user-1002"
+ dnA = "/C=US/O=Org/CN=A"
+ dnB = "/C=US/O=Org/CN=B"
+ )
+ userID, _, _ := seedUser(t, database,
+ userGlobalID, "First", "Last", "user1002@example.edu",
+ []string{dnA, dnB},
+ )
+
+ body := baseUserModifyBody("replace", userGlobalID)
+ body["UserFirstName"] = "First"
+ body["UserLastName"] = "Last"
+ body["UserEmail"] = "user1002@example.edu"
+ body["UserOrganization"] = "Org"
+ body["UserOrgCode"] = "ORG"
+ body["DnList"] = []any{dnA, dnB}
+
+ pkt1 := insertPacket(t, database, "request_user_modify", body)
+ amie := &fakeAmieClient{}
+ h := newRequestUserModifyHandlerForTest(database, amie)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt1.Type, "body": body}, pkt1, "")
+ }); err != nil {
+ t.Fatalf("first Handle: %v", err)
+ }
+
+ pkt2 := insertPacket(t, database, "request_user_modify", body)
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt2.Type, "body": body}, pkt2, "")
+ }); err != nil {
+ t.Fatalf("second Handle: %v", err)
+ }
+
+ // DN set unchanged: still exactly {dnA, dnB}.
+ got := listUserDNs(t, database, userID)
+ want := []string{dnA, dnB}
+ sort.Strings(want)
+ if !equalStringSlices(got, want) {
+ t.Errorf("DN set after re-handle: got %v, want %v", got, want)
+ }
+ if total := countRows(t, database, "amie_user_dns"); total != 2 {
+ t.Errorf("amie_user_dns total rows: got %d, want 2 (no duplicates)", total)
+ }
+
+ // PERSIST_DNS only emitted when added+removed > 0. The second handle is a
+ // pure no-op on the DN set, so it must NOT emit a second PERSIST_DNS.
+ if got := countAuditActions(t, database, pkt2.ID, model.AuditPersistDNs); got != 0 {
+ t.Errorf("audit PERSIST_DNS on no-op replace: got %d, want 0", got)
+ }
+
+ // Each handle emits its own UPDATE_PERSON and REPLY_SENT, these are
+ // per-packet, not deduped. We assert the second packet emitted them once
+ // each (i.e. the handler ran end-to-end on the second delivery).
+ if got := countAuditActions(t, database, pkt2.ID, model.AuditUpdatePerson); got != 1 {
+ t.Errorf("audit UPDATE_PERSON on second handle: got %d, want 1", got)
+ }
+ if got := countAuditActions(t, database, pkt2.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT on second handle: got %d, want 1", got)
+ }
+}
+
+// TestRequestUserModify_ReplaceWithDifferentDNs, replace with a DnList that
+// REMOVES some existing DNs and ADDS new ones. Replace semantics treat DnList
+// as the desired set: drop locals absent from the packet, add packet entries
+// not present locally. Caveat: a partial upstream DnList would wipe local
+// DNs, so this is only safe when upstream sends complete lists.
+func TestRequestUserModify_ReplaceWithDifferentDNs(t *testing.T) {
+ database := setupTestDB(t)
+
+ const (
+ userGlobalID = "amie-user-1003"
+ keepDN = "/C=US/O=Org/CN=Keep"
+ dropDN = "/C=US/O=Org/CN=Drop"
+ addDN1 = "/C=US/O=Org/CN=Add1"
+ addDN2 = "/C=US/O=Org/CN=Add2"
+ )
+ userID, _, _ := seedUser(t, database,
+ userGlobalID, "Stable", "Person", "user1003@example.edu",
+ []string{keepDN, dropDN},
+ )
+
+ body := baseUserModifyBody("replace", userGlobalID)
+ body["UserFirstName"] = "Stable"
+ body["UserLastName"] = "Person"
+ body["UserEmail"] = "user1003@example.edu"
+ body["UserOrganization"] = "Org"
+ body["UserOrgCode"] = "ORG"
+ body["DnList"] = []any{keepDN, addDN1, addDN2}
+
+ pkt := insertPacket(t, database, "request_user_modify", body)
+ amie := &fakeAmieClient{}
+ h := newRequestUserModifyHandlerForTest(database, amie)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ got := listUserDNs(t, database, userID)
+ want := []string{addDN1, addDN2, keepDN}
+ sort.Strings(want)
+ if !equalStringSlices(got, want) {
+ t.Errorf("DN set after destructive sync: got %v, want %v", got, want)
+ }
+ // dropDN must be gone.
+ for _, dn := range got {
+ if dn == dropDN {
+ t.Errorf("DN %q should have been removed by destructive sync", dropDN)
+ }
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditPersistDNs); got != 1 {
+ t.Errorf("audit PERSIST_DNS: got %d, want 1 (DN delta non-empty)", got)
+ }
+}
+
+// TestRequestUserModify_DeleteHappyPath: ActionType=delete with a DnList of
+// DNs to inactivate. The protocol's guidance: "If delete: reads
+// inactive_dn_list = packet.DnList. Inactivate the specified DNs for the
+// user." The delete action affects DN rows only (no other identity entity).
+// The named DNs must be removed; unnamed DNs and the User / UserIdentity
+// rows must remain intact.
+func TestRequestUserModify_DeleteHappyPath(t *testing.T) {
+ database := setupTestDB(t)
+
+ const (
+ userGlobalID = "amie-user-1004"
+ keepDN1 = "/C=US/O=Org/CN=Keep1"
+ keepDN2 = "/C=US/O=Org/CN=Keep2"
+ deleteDN = "/C=US/O=Org/CN=Delete"
+ )
+ const (
+ origFirst = "Tobe"
+ origLast = "Preserved"
+ origEmail = "preserved@example.edu"
+ )
+ userID, identityID, _ := seedUser(t, database,
+ userGlobalID, origFirst, origLast, origEmail,
+ []string{keepDN1, keepDN2, deleteDN},
+ )
+
+ body := baseUserModifyBody("delete", userGlobalID)
+ body["DnList"] = []any{deleteDN}
+
+ pkt := insertPacket(t, database, "request_user_modify", body)
+ amie := &fakeAmieClient{}
+ h := newRequestUserModifyHandlerForTest(database, amie)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error: %v", err)
+ }
+
+ // DNs: keepDN1 and keepDN2 retained, deleteDN removed.
+ got := listUserDNs(t, database, userID)
+ want := []string{keepDN1, keepDN2}
+ sort.Strings(want)
+ if !equalStringSlices(got, want) {
+ t.Errorf("DN set after delete: got %v, want %v", got, want)
+ }
+
+ // User row untouched.
+ var user struct {
+ FirstName string `db:"first_name"`
+ LastName string `db:"last_name"`
+ Email string `db:"email"`
+ Status string `db:"status"`
+ }
+ if err := database.Get(&user, "SELECT first_name, last_name, email, status FROM users WHERE id = ?", userID); err != nil {
+ t.Fatalf("read user: %v", err)
+ }
+ if user.FirstName != origFirst || user.LastName != origLast || user.Email != origEmail {
+ t.Errorf("user row mutated: got %+v, want first=%q last=%q email=%q", user, origFirst, origLast, origEmail)
+ }
+ if user.Status != string(models.UserActive) {
+ t.Errorf("user.status: got %q, want ACTIVE (delete on DNs must not change user lifecycle)", user.Status)
+ }
+
+ // UserIdentity row untouched (no DELETE_PERSON path for DN-only delete).
+ if got := countRows(t, database, "user_identities"); got != 1 {
+ t.Errorf("user_identities: got %d, want 1", got)
+ }
+ var identUserID string
+ if err := database.Get(&identUserID, "SELECT user_id FROM user_identities WHERE id = ?", identityID); err != nil {
+ t.Fatalf("read user_identity: %v", err)
+ }
+ if identUserID != userID {
+ t.Errorf("user_identity.user_id rebound: got %q, want %q", identUserID, userID)
+ }
+
+ // Audits: PERSIST_DNS once (one DN removed), REPLY_SENT once,
+ // no UPDATE_PERSON (delete path does not touch the user row).
+ if got := countAuditActions(t, database, pkt.ID, model.AuditPersistDNs); got != 1 {
+ t.Errorf("audit PERSIST_DNS: got %d, want 1", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditUpdatePerson); got != 0 {
+ t.Errorf("audit UPDATE_PERSON on delete: got %d, want 0 (delete only affects DNs)", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT: got %d, want 1", got)
+ }
+}
+
+// TestRequestUserModify_UnknownUser, packet refers to a UserGlobalID with no
+// matching user_identity. The handler always replies ITC StatusCode='Success'
+// regardless of branch: the connector may receive packets for linked person
+// IDs that this site does not know, so soft-skip with success reply is the
+// correct response. No state mutations expected.
+func TestRequestUserModify_UnknownUser(t *testing.T) {
+ database := setupTestDB(t)
+
+ body := baseUserModifyBody("replace", "amie-user-not-here")
+ body["UserFirstName"] = "Ghost"
+ body["UserLastName"] = "User"
+ body["UserEmail"] = "ghost@example.edu"
+ body["DnList"] = []any{"/C=US/O=Ghost/CN=Ghost"}
+
+ pkt := insertPacket(t, database, "request_user_modify", body)
+ amie := &fakeAmieClient{}
+ h := newRequestUserModifyHandlerForTest(database, amie)
+
+ if err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ }); err != nil {
+ t.Fatalf("Handle returned error on unknown user (spec expects soft-skip): %v", err)
+ }
+
+ if got := countRows(t, database, "users"); got != 0 {
+ t.Errorf("users: got %d, want 0 (no user_identity → no user creation)", got)
+ }
+ if got := countRows(t, database, "user_identities"); got != 0 {
+ t.Errorf("user_identities: got %d, want 0", got)
+ }
+ if got := countRows(t, database, "amie_user_dns"); got != 0 {
+ t.Errorf("amie_user_dns: got %d, want 0", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditUpdatePerson); got != 0 {
+ t.Errorf("audit UPDATE_PERSON: got %d, want 0", got)
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditPersistDNs); got != 0 {
+ t.Errorf("audit PERSIST_DNS: got %d, want 0", got)
+ }
+ // Reply still goes out (soft-skip per the protocol's implementation guidance).
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 1 {
+ t.Errorf("audit REPLY_SENT: got %d, want 1 (soft-skip still replies success)", got)
+ }
+ if got, want := amie.lastReplyType(), "inform_transaction_complete"; got != want {
+ t.Errorf("reply type: got %q, want %q", got, want)
+ }
+}
+
+// TestRequestUserModify_UnsupportedActionType: ActionType `add` has no
+// defined semantics (only `replace` and `delete` are documented). The
+// handler MUST refuse the packet and not mutate state. Sending "add"
+// here also doubles as a guard against arbitrary action strings.
+func TestRequestUserModify_UnsupportedActionType(t *testing.T) {
+ database := setupTestDB(t)
+
+ const userGlobalID = "amie-user-1005"
+ userID, _, _ := seedUser(t, database,
+ userGlobalID, "Stay", "Same", "user1005@example.edu",
+ []string{"/C=US/O=Org/CN=Stay"},
+ )
+
+ body := baseUserModifyBody("add", userGlobalID)
+ body["UserFirstName"] = "Mutated"
+ body["UserEmail"] = "mutated@example.edu"
+ body["DnList"] = []any{"/C=US/O=Org/CN=New"}
+
+ pkt := insertPacket(t, database, "request_user_modify", body)
+ amie := &fakeAmieClient{}
+ h := newRequestUserModifyHandlerForTest(database, amie)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error for unsupported ActionType, got nil")
+ }
+ if !strings.Contains(err.Error(), "ActionType") && !strings.Contains(err.Error(), "action") {
+ t.Errorf("error should mention ActionType, got: %v", err)
+ }
+
+ // No state mutations.
+ var user struct {
+ FirstName string `db:"first_name"`
+ Email string `db:"email"`
+ }
+ if err := database.Get(&user, "SELECT first_name, email FROM users WHERE id = ?", userID); err != nil {
+ t.Fatalf("read user: %v", err)
+ }
+ if user.FirstName != "Stay" || user.Email != "user1005@example.edu" {
+ t.Errorf("user mutated on rejected action: got %+v", user)
+ }
+ if got := listUserDNs(t, database, userID); !equalStringSlices(got, []string{"/C=US/O=Org/CN=Stay"}) {
+ t.Errorf("DN set mutated on rejected action: got %v", got)
+ }
+ if len(amie.Replies) != 0 {
+ t.Errorf("amie replies on rejected action: got %d, want 0", len(amie.Replies))
+ }
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("audit REPLY_SENT on rejected action: got %d, want 0", got)
+ }
+}
+
+// TestRequestUserModify_ReplyFailurePropagates, when the reply send fails
+// the handler must surface the error so the processor can retry. The
+// REPLY_SENT audit row must NOT exist (it is written only after a
+// successful reply within the handler's tx; that tx rolls back on error).
+// (Some core writes commit in their own txns; this test asserts only what
+// the protocol reply contract requires.)
+func TestRequestUserModify_ReplyFailurePropagates(t *testing.T) {
+ database := setupTestDB(t)
+
+ const (
+ userGlobalID = "amie-user-1006"
+ dn = "/C=US/O=Org/CN=Reply Test"
+ )
+ seedUser(t, database,
+ userGlobalID, "Reply", "Test", "user1006@example.edu",
+ []string{dn},
+ )
+
+ body := baseUserModifyBody("replace", userGlobalID)
+ body["UserFirstName"] = "Reply"
+ body["UserLastName"] = "Test"
+ body["UserEmail"] = "user1006@example.edu"
+ body["UserOrganization"] = "Org"
+ body["UserOrgCode"] = "ORG"
+ body["DnList"] = []any{dn}
+
+ pkt := insertPacket(t, database, "request_user_modify", body)
+ amie := &fakeAmieClient{FailWith: errors.New("simulated AMIE outage")}
+ h := newRequestUserModifyHandlerForTest(database, amie)
+
+ err := runHandlerInTx(t, database, func(ctx context.Context, tx *sql.Tx) error {
+ return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, "body": body}, pkt, "")
+ })
+ if err == nil {
+ t.Fatal("expected error when reply fails, got nil")
+ }
+ if !strings.Contains(err.Error(), "reply") && !strings.Contains(err.Error(), "AMIE") {
+ t.Errorf("error should reference reply path, got: %v", err)
+ }
+
+ if got := countAuditActions(t, database, pkt.ID, model.AuditReplySent); got != 0 {
+ t.Errorf("audit REPLY_SENT on failed reply: got %d, want 0", got)
+ }
+}
+
+// equalStringSlices reports whether two string slices contain the same
+// elements in the same order. Tests sort their `want` before calling.
+func equalStringSlices(a, b []string) bool {
+ if len(a) != len(b) {
+ return false
+ }
+ for i := range a {
+ if a[i] != b[i] {
+ return false
+ }
+ }
+ return true
+}
diff --git a/connectors/ACCESS/AMIE-Processor/pipeline/baseline_integration_test.go b/connectors/ACCESS/AMIE-Processor/pipeline/baseline_integration_test.go
new file mode 100644
index 000000000..0ad15af13
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/pipeline/baseline_integration_test.go
@@ -0,0 +1,108 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package pipeline
+
+import (
+ "testing"
+ "time"
+)
+
+// TestPipeline_BaselineDeterminism, runs the canonical baseline scenario
+// end-to-end against the real mock-amie server, drains, and asserts every
+// total_count from
+// `connectors/ACCESS/AMIE-Processor/testdata/scenarios/baseline.yaml`.
+//
+// This is a *determinism* test, not a *correctness* test. It locks
+// in "same packets in → same row counts out" so future handler / scenario
+// changes get caught. If baseline.yaml changes, this test changes with it ,
+// the YAML is the source of truth here.
+func TestPipeline_BaselineDeterminism(t *testing.T) {
+ pipe := newTestPipeline(t)
+ defer pipe.stop()
+
+ pipe.fireScenario(t, "baseline")
+ decoded := pipe.waitForDrain(t, 9, 90*time.Second)
+
+ // Expected totals come straight from
+ // connectors/ACCESS/AMIE-Processor/testdata/scenarios/baseline.yaml.
+ // Field references in comments are the YAML section.
+ expectations := []struct {
+ table string
+ want int
+ }{
+ {"amie_packets", 9}, // packets.total_count
+ {"amie_processing_errors", 0}, // not_expected
+ {"organizations", 1}, // tables.organizations.total_count
+ {"users", 2}, // tables.users.total_count
+ {"user_identities", 2}, // tables.user_identities.total_count
+ {"projects", 2}, // tables.projects.total_count
+ {"compute_allocations", 2}, // tables.compute_allocations.total_count
+ {"compute_allocation_diffs", 1}, // tables.compute_allocation_diffs.total_count
+ {"amie_user_dns", 2}, // tables.amie_user_dns.total_count
+ {"amie_audit_log", 23}, // audit_log.total_count
+ {"compute_cluster_users", 0}, // not_expected
+ {"compute_allocation_memberships", 0}, // not_expected
+ }
+ if decoded != 9 {
+ t.Errorf("decoded packets: got %d, want 9", decoded)
+ }
+ for _, e := range expectations {
+ if got := countRows(t, pipe.db, e.table); got != e.want {
+ t.Errorf("%s: got %d, want %d (per baseline.yaml)", e.table, got, e.want)
+ }
+ }
+
+ // Audit-by-action breakdown also per baseline.yaml audit_log.by_action.
+ actionCounts := []struct {
+ action string
+ want int
+ }{
+ {"CREATE_PERSON", 3},
+ {"CREATE_PROJECT", 3},
+ {"CREATE_ALLOCATION", 3},
+ {"REPLY_SENT", 8},
+ {"PERSIST_DNS", 3},
+ {"UPDATE_PERSON", 1},
+ {"MERGE_PERSONS", 1},
+ {"TRANSACTION_COMPLETE", 1},
+ }
+ for _, ac := range actionCounts {
+ var n int
+ if err := pipe.db.Get(&n,
+ "SELECT COUNT(*) FROM amie_audit_log WHERE action = ?", ac.action,
+ ); err != nil {
+ t.Fatalf("count audit action %s: %v", ac.action, err)
+ }
+ if n != ac.want {
+ t.Errorf("audit %s: got %d, want %d", ac.action, n, ac.want)
+ }
+ }
+
+ // Every processing event ended SUCCEEDED per baseline.yaml processing_events.
+ var nonSucceeded int
+ if err := pipe.db.Get(&nonSucceeded,
+ "SELECT COUNT(*) FROM amie_processing_events WHERE status != 'SUCCEEDED'",
+ ); err != nil {
+ t.Fatalf("count non-succeeded events: %v", err)
+ }
+ if nonSucceeded != 0 {
+ t.Errorf("non-SUCCEEDED events: got %d, want 0", nonSucceeded)
+ }
+}
diff --git a/connectors/ACCESS/AMIE-Processor/pipeline/integration_common.go b/connectors/ACCESS/AMIE-Processor/pipeline/integration_common.go
new file mode 100644
index 000000000..51a480fbf
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/pipeline/integration_common.go
@@ -0,0 +1,290 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package pipeline
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+ "os"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/jmoiron/sqlx"
+
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/amieclient"
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/config"
+ amiedb "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/db"
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/handler"
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/metrics"
+ amieservice "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/service"
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/store"
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/worker"
+ "github.com/apache/airavata-custos/internal/db"
+ "github.com/apache/airavata-custos/pkg/events"
+ coreservice "github.com/apache/airavata-custos/pkg/service"
+)
+
+const testClusterID = "00000000-0000-0000-0000-000000000001"
+
+var (
+ sharedDB *sqlx.DB
+ sharedDBOnce sync.Once
+ sharedDBErr error
+)
+
+func isLocalAMIEConfigAvailable() bool {
+ for _, key := range []string{
+ "DATABASE_DSN",
+ "AMIE_BASE_URL",
+ "AMIE_SITE_CODE",
+ "AMIE_API_KEY",
+ "AMIE_CLUSTER_ID",
+ } {
+ if os.Getenv(key) == "" {
+ return false
+ }
+ }
+ return true
+}
+
+func setupTestDB(t *testing.T) *sqlx.DB {
+ t.Helper()
+ if !isLocalAMIEConfigAvailable() {
+ t.Skip("integration env not set; run via scripts/run-amie-integration-tests.sh")
+ }
+ sharedDBOnce.Do(func() {
+ database, err := db.Open(db.Config{
+ DSN: os.Getenv("DATABASE_DSN"),
+ MaxOpenConns: 5,
+ MaxIdleConns: 2,
+ })
+ if err != nil {
+ sharedDBErr = err
+ return
+ }
+ if err := db.MigrateEmbedded(database); err != nil {
+ sharedDBErr = err
+ return
+ }
+ if err := db.MigrateConnectorFS(database, amiedb.MigrationFS(), "migrations", "amie"); err != nil {
+ sharedDBErr = err
+ return
+ }
+ sharedDB = database
+ })
+ if sharedDBErr != nil {
+ t.Fatalf("setup db: %v", sharedDBErr)
+ }
+ truncateAll(t, sharedDB)
+ seedCluster(t, sharedDB)
+ return sharedDB
+}
+
+func truncateAll(t *testing.T, database *sqlx.DB) {
+ t.Helper()
+ tables := []string{
+ "amie_audit_log",
+ "amie_processing_errors",
+ "amie_processing_events",
+ "amie_packets",
+ "amie_user_dns",
+ "compute_allocation_membership_resource_overrides",
+ "compute_allocation_usages",
+ "compute_allocation_memberships",
+ "compute_allocation_change_request_events",
+ "compute_allocation_change_requests",
+ "compute_allocation_diffs",
+ "compute_allocation_resource_rates",
+ "compute_allocation_resource_mappings",
+ "compute_allocation_resources",
+ "compute_allocations",
+ "compute_cluster_users",
+ "compute_clusters",
+ "projects",
+ "user_identities",
+ "users",
+ "organizations",
+ }
+ if _, err := database.Exec("SET FOREIGN_KEY_CHECKS = 0"); err != nil {
+ t.Fatalf("disable FK: %v", err)
+ }
+ for _, tbl := range tables {
+ if _, err := database.Exec("TRUNCATE TABLE " + tbl); err != nil {
+ t.Fatalf("truncate %s: %v", tbl, err)
+ }
+ }
+ if _, err := database.Exec("SET FOREIGN_KEY_CHECKS = 1"); err != nil {
+ t.Fatalf("re-enable FK: %v", err)
+ }
+}
+
+func seedCluster(t *testing.T, database *sqlx.DB) {
+ t.Helper()
+ if _, err := database.Exec(
+ "INSERT INTO compute_clusters (id, name) VALUES (?, ?)",
+ testClusterID, "default-cluster",
+ ); err != nil {
+ t.Fatalf("seed cluster: %v", err)
+ }
+}
+
+// testPipeline runs the AMIE connector in-process against the local mock
+// server.
+type testPipeline struct {
+ db *sqlx.DB
+ baseURL string
+ siteCode string
+ cancel context.CancelFunc
+ wg sync.WaitGroup
+}
+
+// newTestPipeline mirrors pkg/amie.LoadConnector but with short poll/worker
+// intervals so tests don't wait the production default of 30s/5s.
+func newTestPipeline(t *testing.T) *testPipeline {
+ t.Helper()
+ database := setupTestDB(t)
+
+ cfg := config.AMIEConfig{
+ BaseURL: os.Getenv("AMIE_BASE_URL"),
+ SiteCode: os.Getenv("AMIE_SITE_CODE"),
+ APIKey: os.Getenv("AMIE_API_KEY"),
+ PollInterval: 200 * time.Millisecond,
+ WorkerInterval: 100 * time.Millisecond,
+ ConnectTimeout: 5 * time.Second,
+ ReadTimeout: 20 * time.Second,
+ PollerEnabled: true,
+ }
+
+ amieClient := amieclient.New(cfg)
+ eventBus := events.New()
+ coreSvc := coreservice.New(database, eventBus)
+
+ packetStore := store.NewPacketStore(database)
+ eventStore := store.NewEventStore(database)
+ errorStore := store.NewProcessingErrorStore(database)
+ auditStore := store.NewAuditStore(database)
+ userDNStore := store.NewUserDNStore(database)
+ auditSvc := amieservice.NewAuditService(auditStore)
+
+ router := handler.NewRouter(
+ handler.NewRequestProjectCreateHandler(coreSvc, testClusterID, amieClient, auditSvc),
+ handler.NewRequestAccountCreateHandler(coreSvc, testClusterID, amieClient, auditSvc),
+ handler.NewRequestProjectInactivateHandler(coreSvc, amieClient, auditSvc),
+ handler.NewRequestProjectReactivateHandler(coreSvc, amieClient, auditSvc),
+ handler.NewRequestAccountInactivateHandler(coreSvc, amieClient, auditSvc),
+ handler.NewRequestAccountReactivateHandler(coreSvc, amieClient, auditSvc),
+ handler.NewRequestPersonMergeHandler(coreSvc, userDNStore, amieClient, auditSvc),
+ handler.NewRequestUserModifyHandler(coreSvc, userDNStore, amieClient, auditSvc),
+ handler.NewDataProjectCreateHandler(coreSvc, userDNStore, amieClient, auditSvc),
+ handler.NewDataAccountCreateHandler(coreSvc, userDNStore, amieClient, auditSvc),
+ handler.NewInformTransactionCompleteHandler(auditSvc),
+ handler.NewNoOpHandler(),
+ )
+
+ met := metrics.New()
+ poller := worker.NewPoller(amieClient, packetStore, eventStore, met, database, cfg)
+ processor := worker.NewProcessor(eventStore, packetStore, errorStore, router, met, database, cfg)
+
+ ctx, cancel := context.WithCancel(context.Background())
+ pipe := &testPipeline{
+ db: database,
+ baseURL: cfg.BaseURL,
+ siteCode: cfg.SiteCode,
+ cancel: cancel,
+ }
+ pipe.wg.Add(2)
+ go func() {
+ defer pipe.wg.Done()
+ poller.Run(ctx)
+ }()
+ go func() {
+ defer pipe.wg.Done()
+ processor.Run(ctx)
+ }()
+ return pipe
+}
+
+func (p *testPipeline) stop() {
+ p.cancel()
+ p.wg.Wait()
+}
+
+func (p *testPipeline) fireScenario(t *testing.T, scenarioType string) {
+ t.Helper()
+ url := fmt.Sprintf("%s/test/%s/scenarios?type=%s", p.baseURL, p.siteCode, scenarioType)
+ resp, err := http.Post(url, "application/json", nil)
+ if err != nil {
+ t.Fatalf("POST %s: %v", url, err)
+ }
+ defer resp.Body.Close()
+ if resp.StatusCode >= 400 {
+ t.Fatalf("scenario %s returned status %d", scenarioType, resp.StatusCode)
+ }
+}
+
+// waitForDrain blocks until at least expectPackets have been ingested AND
+// no rows are pending. The "at least N ingested" guard avoids a race where
+// the poller hasn't fired yet so a naive drain check returns immediately.
+func (p *testPipeline) waitForDrain(t *testing.T, expectPackets int, deadline time.Duration) int {
+ t.Helper()
+ end := time.Now().Add(deadline)
+ for {
+ var total, pendingDecode, pendingProcess int
+ if err := p.db.Get(&total, "SELECT COUNT(*) FROM amie_packets"); err != nil {
+ t.Fatalf("count packets: %v", err)
+ }
+ if err := p.db.Get(&pendingDecode,
+ "SELECT COUNT(*) FROM amie_packets WHERE status = 'NEW'",
+ ); err != nil {
+ t.Fatalf("count pending packets: %v", err)
+ }
+ if err := p.db.Get(&pendingProcess,
+ "SELECT COUNT(*) FROM amie_processing_events WHERE status NOT IN ('SUCCEEDED','FAILED','PERMANENTLY_FAILED')",
+ ); err != nil {
+ t.Fatalf("count pending events: %v", err)
+ }
+ if total >= expectPackets && pendingDecode == 0 && pendingProcess == 0 {
+ break
+ }
+ if time.Now().After(end) {
+ t.Fatalf("drain timeout: total=%d (want >=%d), pendingDecode=%d, pendingProcess=%d",
+ total, expectPackets, pendingDecode, pendingProcess)
+ }
+ time.Sleep(250 * time.Millisecond)
+ }
+ var decoded int
+ if err := p.db.Get(&decoded,
+ "SELECT COUNT(*) FROM amie_packets WHERE status = 'DECODED'",
+ ); err != nil {
+ t.Fatalf("count decoded: %v", err)
+ }
+ return decoded
+}
+
+func countRows(t *testing.T, database *sqlx.DB, table string) int {
+ t.Helper()
+ var n int
+ if err := database.Get(&n, "SELECT COUNT(*) FROM "+table); err != nil {
+ t.Fatalf("count %s: %v", table, err)
+ }
+ return n
+}
diff --git a/connectors/ACCESS/AMIE-Processor/testdata/scenarios/README.md b/connectors/ACCESS/AMIE-Processor/testdata/scenarios/README.md
new file mode 100644
index 000000000..68c7aa535
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/testdata/scenarios/README.md
@@ -0,0 +1,153 @@
+# AMIE scenario fixtures: expected DB state after each mock scenario
+
+Per-scenario test fixtures. Each YAML in this directory describes one mock-server scenario: what gets fired, what should end up in which table, and what the audit log should contain.
+
+## How it's used
+
+The Go integration test at `connectors/ACCESS/AMIE-Processor/pipeline/baseline_integration_test.go` consumes `baseline.yaml`:
+
+1. Stand up the test stack (`make integration-test-amie` does this).
+2. POST to the scenario endpoint on the mock server.
+3. Wait for `amie_packets` to settle (no `NEW` or in-flight `RUNNING` events).
+4. Assert table counts + audit-action breakdown match the YAML's `expectations:` block.
+
+For ad-hoc inspection, the `verification_queries:` block in each YAML gives copy-pasteable SQL you can run against the test DB.
+
+## File format (overview)
+
+```yaml
+scenario:
+ name: <scenario name> # matches mock-server's ?type=<name>
+ description: <one-line summary>
+ determinism: full | partial | random # how much can be asserted exactly
+
+trigger:
+ endpoint: POST /test/{site}/scenarios?type=<name>
+ preconditions:
+ env:
+ - VAR_NAME # required env vars
+ seed:
+ - "table: row spec" # rows that must exist before firing
+
+expectations:
+ packets: # amie_packets table
+ - {type: <packet type>, status: DECODED, count: <N>}
+ # ...
+
+ tables: # domain tables
+ <table_name>:
+ total_count: <N> # or {min, max} for non-deterministic
+ rows: # ordered or set; see `match:` hint
+ - <column>: <value> # exact value, or pattern object below
+ ...
+
+ audit_log:
+ by_action:
+ CREATE_PERSON: <count>
+ CREATE_PROJECT: <count>
+ # ...
+
+ not_expected: # tables that should remain empty
+ - user_dns
+ - user_merges
+ # ...
+
+verification_queries: # manual SQL for ad-hoc checks
+ - name: <human label>
+ sql: "SELECT ..."
+ expect: <one-line description of the expected output>
+```
+
+### Value forms
+
+| Form | Meaning | Example |
+|---|---|---|
+| literal | exact string / int / bool / null match | `email: hwan@uccs.edu` |
+| `${ENV_VAR}` | substituted at test time from process env | `email: ${DEV_EMAIL}` |
+| `{regex: "..."}` | column value must match the RE2 regex | `email: {regex: '^user[0-9]+@example\.edu$'}` |
+| `{not_null: true}` | column must be non-NULL (any value) | `id: {not_null: true}` |
+| `{from: "<table>.<col> where <pred>"}` | runner resolves to that scalar; assertion uses it | `project_pi_id: {from: "users.id where email=${DEV_EMAIL}"}` |
+
+#### Grammar for `${ENV_VAR}`
+
+- **Name**: matches `[A-Z][A-Z0-9_]*`. Anything else is a literal `$`.
+- **Substitution context**: runner expands inside YAML scalar values only, NOT inside table or column names.
+- **Unset variable**: runner MUST fail the scenario with `env var <name> not set`. Empty-string is treated as set (so `DEV_EMAIL=""` substitutes the empty string).
+- **Escaping a literal `$`**: prefix with backslash (`\$NOT_AN_ENV_VAR`).
+- **Shell-special characters in the value** (spaces, quotes, semicolons): the runner inserts the value as a SQL parameter (not via string concat), so injection is not a concern. The value flows through bound parameters in the eventual Go assertion.
+
+#### Grammar for `{from: "<table>.<col> where <pred>"}`
+
+- **Form**: `{from: "<table_name>.<column_name> where <predicate>"}`. Single quoted YAML string.
+- **Predicate**: SQL-safe `WHERE` fragment. May reference other columns and `${ENV_VAR}` substitutions. Single equality only; joins or sub-selects are not supported.
+- **Resolution**: runner executes `SELECT <col> FROM <table> WHERE <pred> LIMIT 2` and:
+ - **0 rows** → fail the scenario with `from lookup matched no rows: <expr>`.
+ - **1 row** → use the scalar value as the assertion target.
+ - **>1 row** → fail the scenario with `from lookup matched multiple rows: <expr>` (the YAML author must constrain the predicate further; ambiguity is always a fixture bug).
+- **Order of resolution**: `from` lookups happen AFTER `${ENV_VAR}` substitution. So `from: "users.id where email=${DEV_EMAIL}"` first substitutes the env var, then runs the lookup.
+- **Composition**: `from` cannot contain another `from`. Chain by stacking rows in the expected order so each prior row's value is queryable.
+
+### Counts
+
+| Form | Meaning |
+|---|---|
+| `<N>` | exact count |
+| `{min: <N>}` | at least N |
+| `{min: <N>, max: <M>}` | range |
+| `{>=: <N>}` | shorthand for min |
+
+## Scenario index
+
+| Scenario | YAML | Determinism | Notes |
+|---|---|---|---|
+| `baseline` | [`baseline.yaml`](baseline.yaml) | Full | **Canonical integration-test fixture.** Hardcoded values covering every handler the mock can drive without needing the Custos-assigned project UUID. |
+
+## What is NOT covered (and why)
+
+Five handlers need the **Custos-assigned project UUID** in the packet body, and the mock can't supply that today (it would need to capture the `notify_project_create` reply and feed the UUID into subsequent packets). These handlers are NOT exercised by `baseline`:
+
+- `request_account_create`
+- `request_account_inactivate`
+- `request_account_reactivate`
+- `request_project_inactivate`
+- `request_project_reactivate`
+
+That leaves these tables empty in a mock-only test:
+- `compute_cluster_users`
+- `compute_allocation_memberships`
+
+And these status-flip code paths unexercised:
+- `projects.status`
+- `compute_allocations.status`
+- `compute_allocation_memberships.membership_status`
+- `compute_cluster_users.status`
+
+Closing the gap requires either reply-aware mock sequencing or a Go runner that captures replies between packet posts. Tracked as follow-up.
+
+### What this fixture also does NOT exercise (beyond the 5 untestable handlers)
+
+- **Email-uniqueness collision**: `baseline` never re-creates a user with a clashing email. The `UpdateUser` uniqueness guard is not exercised by this fixture.
+- **Intra-packet audit ordering**: the fixture asserts `by_action` counts only, not the temporal order of audits within a single packet. A regression that reordered, say, `CREATE_PROJECT` after `CREATE_ALLOCATION` would not fail this fixture.
+- **Retry storms**: every event in `baseline` reaches `SUCCEEDED` on the first attempt. Handlers that succeed only after N retries pass the totals but mask transient bugs. The `processing_events.by_status` block flags any non-SUCCEEDED final state, but does not flag retry count per event.
+- **Concurrent re-delivery**: the supplement (packet 3) arrives strictly after packet 1 finishes. Find-or-create TOCTOU races need concurrent fires from the load workload, not this fixture.
+- **Read-path race on `oidc_sub`**: packet 3's PI lookup re-uses the bl-pi-001 row created by packet 1, so this fixture covers the read path through `FindBySourceAndExternalID`, but only serially.
+
+## Other mock scenarios (not part of the deterministic fixture suite)
+
+These exist in the mock for ad-hoc load testing but do not have YAML fixtures because their data is randomized per run:
+
+| Scenario | Use |
+|---|---|
+| `mixed` | 6 success + 4 failure (random GIDs); quick sanity sweep |
+| `success_only` | 8 success packets (random) |
+| `failures_only` | 8 deliberately malformed packets (random); validation-path testing |
+| `heavy` | 15 success + 10 failure (random) |
+| `all_handlers` | One of each handler type; pre-creates users for merge + DN handlers |
+| `dev_email` | Scripted dev-loop scenario binding `DEV_EMAIL`; partially fails because account_create packets hit the missing-UUID issue |
+
+Validate these scenarios via the k6 load workload + invariant-style checks, not exact-row assertions.
+
+## Out of scope
+
+- Reply-aware mock sequencing would unblock the 5 untestable handlers but adds Python complexity. Tracked as a follow-up.
+- Cross-scenario expectations (chaining multiple scenarios in one test run) are explicitly NOT modeled; each scenario assumes a fresh DB.
diff --git a/connectors/ACCESS/AMIE-Processor/testdata/scenarios/baseline.yaml b/connectors/ACCESS/AMIE-Processor/testdata/scenarios/baseline.yaml
new file mode 100644
index 000000000..6768f4e66
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/testdata/scenarios/baseline.yaml
@@ -0,0 +1,197 @@
+# Mock scenario: baseline
+#
+# Hardcoded values in mock-server.py::gen_baseline_scenario. Used as the
+# canonical integration-test fixture for the connector.
+#
+# Covers: request_project_create (initial + supplement), request_user_modify,
+# request_person_merge, data_project_create, data_account_create,
+# inform_transaction_complete.
+#
+# NOT covered (handlers need the Custos-assigned project UUID, which the mock
+# can't supply): request_account_create, request_account_inactivate,
+# request_account_reactivate, request_project_inactivate, request_project_reactivate.
+# Tables left empty as a result: compute_cluster_users, compute_allocation_memberships.
+# Status-flip code paths on projects/allocations/memberships are also unexercised.
+
+scenario:
+ name: baseline
+ description: 9 hardcoded packets exercising every handler the mock can drive without reply sequencing.
+ determinism: full
+ # Expectations assume packets are decoded and handled in mock-emission order.
+ # The connector's worker is single-threaded today; if that changes, the
+ # post-merge counts (amie_user_dns=3, projects.project_pi_id, survivor column
+ # values) all become race-dependent.
+ ordering: strict
+
+trigger:
+ endpoint: POST /test/{site}/scenarios?type=baseline
+ drain_wait:
+ decode_pending: "SELECT COUNT(*) FROM amie_packets WHERE status='NEW'"
+ handle_pending: "SELECT COUNT(*) FROM amie_processing_events WHERE status NOT IN ('SUCCEEDED','FAILED','PERMANENTLY_FAILED')"
+ timeout_seconds: 180
+ preconditions:
+ env:
+ - name: AMIE_BASE_URL
+ - name: AMIE_SITE_CODE
+ - name: AMIE_API_KEY
+ - name: AMIE_CLUSTER_ID
+ seed:
+ - table: compute_clusters
+ row: { id: "${AMIE_CLUSTER_ID}", name: default-cluster }
+
+expectations:
+
+ packets:
+ total_count: 9
+ by_type_status:
+ - { type: request_project_create, status: DECODED, count: 3 }
+ - { type: data_project_create, status: DECODED, count: 1 }
+ - { type: data_account_create, status: DECODED, count: 1 }
+ - { type: request_user_modify, status: DECODED, count: 2 }
+ - { type: request_person_merge, status: DECODED, count: 1 }
+ - { type: inform_transaction_complete, status: DECODED, count: 1 }
+
+ tables:
+
+ organizations:
+ total_count: 1
+ rows:
+ - { originated_id: BASELINE, name: Baseline Org }
+
+ users:
+ total_count: 2
+ rows:
+ - { first_name: Pat, last_name: First-Updated, email: pat.first.updated@baseline.example.edu, status: ACTIVE,
+ organization_id: { from: "organizations.id where originated_id=BASELINE" } }
+ - { first_name: Sam, last_name: Second, email: sam.second@baseline.example.edu, status: MERGED,
+ organization_id: { from: "organizations.id where originated_id=BASELINE" } }
+
+ user_identities:
+ total_count: 2
+ rows:
+ - { source: access, external_id: bl-pi-001,
+ email: pat.first.updated@baseline.example.edu,
+ user_id: { from: "users.id where email=pat.first.updated@baseline.example.edu" },
+ metadata: { regex: '.*"org_code":"BASELINE".*' } }
+ # bl-pi-002's row is reassigned to the survivor by MergeUsers.ReassignUser.
+ - { source: access, external_id: bl-pi-002,
+ email: sam.second@baseline.example.edu,
+ user_id: { from: "users.id where email=pat.first.updated@baseline.example.edu" } }
+
+ projects:
+ total_count: 2
+ rows:
+ # Handler sets project.title = GrantNumber (NOT ProjectTitle from packet).
+ - { originated_id: BL-001, title: BL-001, origination: access, status: ACTIVE,
+ project_pi_id: { from: "users.id where email=pat.first.updated@baseline.example.edu" } }
+ # BL-002's PI was bl-pi-002 originally; merge reassigned project_pi_id to the survivor.
+ - { originated_id: BL-002, title: BL-002, origination: access, status: ACTIVE,
+ project_pi_id: { from: "users.id where email=pat.first.updated@baseline.example.edu" } }
+
+ compute_allocations:
+ total_count: 2
+ rows:
+ # initial_su_amount stays at first-delivery value; supplement writes a diff row
+ # instead of updating this column. Effective budget = initial_su_amount + SUM(diffs.new_su_amount).
+ - { name: BL-001, status: ACTIVE, compute_cluster_id: "${AMIE_CLUSTER_ID}",
+ initial_su_amount: 10000,
+ start_time: "2026-01-01 00:00:00", end_time: "2026-12-31 00:00:00",
+ project_id: { from: "projects.id where originated_id=BL-001" } }
+ - { name: BL-002, status: ACTIVE, compute_cluster_id: "${AMIE_CLUSTER_ID}",
+ initial_su_amount: 20000,
+ start_time: "2026-01-01 00:00:00", end_time: "2026-12-31 00:00:00",
+ project_id: { from: "projects.id where originated_id=BL-002" } }
+
+ compute_allocation_diffs:
+ total_count: 1
+ rows:
+ - { compute_allocation_id: { from: "compute_allocations.id where name=BL-001" },
+ diff_type: SUPPLEMENT, new_su_amount: 5000, status: ACTIVE,
+ description: "AMIE SUPPLEMENT of 5000 SUs" }
+
+ amie_user_dns:
+ # 3 DNs land on the survivor after merge; the trailing ActionType=delete
+ # request_user_modify removes "/DC=EDU/CN=patfirst", leaving 2.
+ # Connector-local table; user_id references core users.id by value.
+ total_count: 2
+ rows:
+ - { dn: "/C=US/O=Baseline Org/CN=Pat First Extra",
+ user_id: { from: "users.id where email=pat.first.updated@baseline.example.edu" } }
+ # data_account_create added this DN to bl-pi-002; merge reassigned it to survivor.
+ - { dn: "/C=US/O=Baseline Org/CN=Sam Second",
+ user_id: { from: "users.id where email=pat.first.updated@baseline.example.edu" } }
+
+ audit_log:
+ total_count: 23
+ by_action:
+ CREATE_PERSON: 3 # all 3 request_project_create packets audit CREATE_PERSON (re-use audited too)
+ CREATE_PROJECT: 3
+ CREATE_ALLOCATION: 3 # supplement-delivery still audits CREATE_ALLOCATION (known M1: diff is the actual write)
+ REPLY_SENT: 8 # inform_transaction_complete does not reply
+ PERSIST_DNS: 3 # 2 from data_*, 1 from request_user_modify delete (DN delete: -1)
+ UPDATE_PERSON: 1
+ MERGE_PERSONS: 1
+ TRANSACTION_COMPLETE: 1
+
+ processing_events:
+ total_count: 9
+ by_status:
+ SUCCEEDED: 9
+ not_expected_statuses:
+ - RUNNING
+ - NEW
+ - RETRY_SCHEDULED
+ - FAILED
+ - PERMANENTLY_FAILED
+
+ not_expected:
+ - compute_cluster_users
+ - compute_allocation_memberships
+ - amie_processing_errors
+
+verification_queries:
+
+ - name: packet status by type
+ sql: |
+ SELECT type, status, COUNT(*) AS n FROM amie_packets
+ GROUP BY type, status ORDER BY type;
+ expect: see expectations.packets.by_type_status
+
+ - name: zero errors
+ sql: SELECT COUNT(*) FROM amie_processing_errors;
+ expect: 0
+
+ - name: survivor + retiring user state
+ sql: SELECT email, status FROM users ORDER BY email;
+ expect: |
+ pat.first.updated@baseline.example.edu, ACTIVE
+ sam.second@baseline.example.edu, MERGED
+
+ - name: supplement recorded as diff (not new allocation)
+ sql: |
+ SELECT (SELECT COUNT(*) FROM compute_allocations) AS allocs,
+ (SELECT COUNT(*) FROM compute_allocation_diffs) AS diffs,
+ (SELECT diff_type FROM compute_allocation_diffs) AS diff_type,
+ (SELECT new_su_amount FROM compute_allocation_diffs) AS supp_su;
+ expect: allocs=2, diffs=1, diff_type=SUPPLEMENT, supp_su=5000
+
+ - name: merge consolidated identity-forward state to survivor
+ sql: |
+ SELECT 'user_identities' AS t, COUNT(*) AS n_on_survivor FROM user_identities WHERE user_id = (SELECT id FROM users WHERE email='pat.first.updated@baseline.example.edu')
+ UNION ALL
+ SELECT 'amie_user_dns', COUNT(*) FROM amie_user_dns WHERE user_id = (SELECT id FROM users WHERE email='pat.first.updated@baseline.example.edu')
+ UNION ALL
+ SELECT 'projects_as_pi', COUNT(*) FROM projects WHERE project_pi_id = (SELECT id FROM users WHERE email='pat.first.updated@baseline.example.edu');
+ expect: user_identities=2, amie_user_dns=2, projects_as_pi=2
+
+ - name: merge recorded in audit log (no user_merges table; audit is source of truth)
+ sql: |
+ SELECT action, entity_type, entity_id, summary
+ FROM amie_audit_log
+ WHERE action = 'MERGE_PERSONS';
+ expect:
+ one row, entity_type=user, summary contains "merged <retiring_id> into <survivor_id>: <reason>"
+
+ - name: audit action counts
+ sql: SELECT action, COUNT(*) AS n FROM amie_audit_log GROUP BY action ORDER BY action;
+ expect: see expectations.audit_log.by_action
diff --git a/connectors/ACCESS/AMIE-Processor/worker/integration_common.go b/connectors/ACCESS/AMIE-Processor/worker/integration_common.go
new file mode 100644
index 000000000..459b0b5ca
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/worker/integration_common.go
@@ -0,0 +1,188 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package worker
+
+import (
+ "context"
+ "os"
+ "sync"
+ "testing"
+
+ "github.com/jmoiron/sqlx"
+
+ amiedb "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/db"
+ "github.com/apache/airavata-custos/internal/db"
+)
+
+var (
+ sharedDB *sqlx.DB
+ sharedDBOnce sync.Once
+ sharedDBErr error
+)
+
+func isLocalAMIEConfigAvailable() bool {
+ for _, key := range []string{
+ "DATABASE_DSN",
+ "AMIE_BASE_URL",
+ "AMIE_SITE_CODE",
+ "AMIE_API_KEY",
+ "AMIE_CLUSTER_ID",
+ } {
+ if os.Getenv(key) == "" {
+ return false
+ }
+ }
+ return true
+}
+
+func setupTestDB(t *testing.T) *sqlx.DB {
+ t.Helper()
+ if !isLocalAMIEConfigAvailable() {
+ t.Skip("integration env not set; run via scripts/run-amie-integration-tests.sh")
+ }
+ sharedDBOnce.Do(func() {
+ database, err := db.Open(db.Config{
+ DSN: os.Getenv("DATABASE_DSN"),
+ MaxOpenConns: 5,
+ MaxIdleConns: 2,
+ })
+ if err != nil {
+ sharedDBErr = err
+ return
+ }
+ if err := db.MigrateEmbedded(database); err != nil {
+ sharedDBErr = err
+ return
+ }
+ if err := db.MigrateConnectorFS(database, amiedb.MigrationFS(), "migrations", "amie"); err != nil {
+ sharedDBErr = err
+ return
+ }
+ sharedDB = database
+ })
+ if sharedDBErr != nil {
+ t.Fatalf("setup db: %v", sharedDBErr)
+ }
+ truncateAMIETables(t, sharedDB)
+ return sharedDB
+}
+
+// truncateAMIETables wipes connector tables only; worker tests don't touch
+// core domain tables.
+func truncateAMIETables(t *testing.T, database *sqlx.DB) {
+ t.Helper()
+ if _, err := database.Exec("SET FOREIGN_KEY_CHECKS = 0"); err != nil {
+ t.Fatalf("disable FK: %v", err)
+ }
+ for _, tbl := range []string{
+ "amie_audit_log",
+ "amie_processing_errors",
+ "amie_processing_events",
+ "amie_packets",
+ "amie_user_dns",
+ } {
+ if _, err := database.Exec("TRUNCATE TABLE " + tbl); err != nil {
+ t.Fatalf("truncate %s: %v", tbl, err)
+ }
+ }
+ if _, err := database.Exec("SET FOREIGN_KEY_CHECKS = 1"); err != nil {
+ t.Fatalf("re-enable FK: %v", err)
+ }
+}
+
+// stubAmieClient returns canned responses/errors per call so tests can drive
+// multi-call sequences deterministically.
+type stubAmieClient struct {
+ mu sync.Mutex
+ responses [][]map[string]any
+ errors []error
+ calls int
+}
+
+func (s *stubAmieClient) FetchInProgressPackets(_ context.Context) ([]map[string]any, error) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ idx := s.calls
+ s.calls++
+ if idx < len(s.errors) && s.errors[idx] != nil {
+ return nil, s.errors[idx]
+ }
+ if idx < len(s.responses) {
+ return s.responses[idx], nil
+ }
+ return nil, nil
+}
+
+func (s *stubAmieClient) callCount() int {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ return s.calls
+}
+
+// stubMetrics counts calls instead of touching Prometheus.
+type stubMetrics struct {
+ mu sync.Mutex
+ packetsReceived []string
+ fetchCounts []int
+ processedOutcomes []string
+ retries int
+ timerStops int
+}
+
+func (m *stubMetrics) RecordPacketReceived(packetType string) {
+ m.mu.Lock()
+ defer m.mu.Unlock()
+ m.packetsReceived = append(m.packetsReceived, packetType)
+}
+
+func (m *stubMetrics) RecordPollerFetch(count int) {
+ m.mu.Lock()
+ defer m.mu.Unlock()
+ m.fetchCounts = append(m.fetchCounts, count)
+}
+
+func (m *stubMetrics) RecordPacketProcessed(packetType, outcome string) {
+ m.mu.Lock()
+ defer m.mu.Unlock()
+ m.processedOutcomes = append(m.processedOutcomes, outcome)
+}
+
+func (m *stubMetrics) RecordRetry() {
+ m.mu.Lock()
+ defer m.mu.Unlock()
+ m.retries++
+}
+
+func (m *stubMetrics) StartProcessingTimer() func(string) {
+ return func(string) {
+ m.mu.Lock()
+ defer m.mu.Unlock()
+ m.timerStops++
+ }
+}
+
+func countRows(t *testing.T, database *sqlx.DB, table string) int {
+ t.Helper()
+ var n int
+ if err := database.Get(&n, "SELECT COUNT(*) FROM "+table); err != nil {
+ t.Fatalf("count %s: %v", table, err)
+ }
+ return n
+}
diff --git a/connectors/ACCESS/AMIE-Processor/worker/poller_integration_test.go b/connectors/ACCESS/AMIE-Processor/worker/poller_integration_test.go
new file mode 100644
index 000000000..3ef7a8a5a
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/worker/poller_integration_test.go
@@ -0,0 +1,210 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package worker
+
+import (
+ "context"
+ "testing"
+ "time"
+
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/config"
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/store"
+)
+
+// makePollerPacket constructs the JSON node shape the AMIE server returns
+// for one in-progress packet.
+func makePollerPacket(amieID int64, packetType string) map[string]any {
+ return map[string]any{
+ "type": packetType,
+ "header": map[string]any{
+ "packet_rec_id": float64(amieID),
+ },
+ "body": map[string]any{"GrantNumber": "TG-POLL-001"},
+ }
+}
+
+func newPoller(t *testing.T, client pollerAmieClient) (*Poller, *stubMetrics) {
+ t.Helper()
+ database := setupTestDB(t)
+ met := &stubMetrics{}
+ cfg := config.AMIEConfig{
+ PollInterval: 10 * time.Millisecond,
+ PollerEnabled: true,
+ }
+ return NewPoller(client, store.NewPacketStore(database), store.NewEventStore(database), met, database, cfg), met
+}
+
+// TestPoller_SinglePacketIngestCreatesPacketAndEvent, one well-formed packet
+// in, one amie_packets row + one amie_processing_events row, both NEW.
+func TestPoller_SinglePacketIngestCreatesPacketAndEvent(t *testing.T) {
+ database := setupTestDB(t)
+ stub := &stubAmieClient{responses: [][]map[string]any{{makePollerPacket(1001, "request_project_create")}}}
+ p, met := newPoller(t, stub)
+
+ p.pollForPackets(context.Background())
+
+ if got := countRows(t, database, "amie_packets"); got != 1 {
+ t.Errorf("amie_packets: got %d, want 1", got)
+ }
+ if got := countRows(t, database, "amie_processing_events"); got != 1 {
+ t.Errorf("amie_processing_events: got %d, want 1", got)
+ }
+
+ var status string
+ if err := database.Get(&status, "SELECT status FROM amie_packets LIMIT 1"); err != nil {
+ t.Fatalf("read status: %v", err)
+ }
+ if status != "NEW" {
+ t.Errorf("packet status: got %q, want NEW", status)
+ }
+ if err := database.Get(&status, "SELECT status FROM amie_processing_events LIMIT 1"); err != nil {
+ t.Fatalf("read event status: %v", err)
+ }
+ if status != "NEW" {
+ t.Errorf("event status: got %q, want NEW", status)
+ }
+
+ if len(met.packetsReceived) != 1 || met.packetsReceived[0] != "request_project_create" {
+ t.Errorf("packetsReceived: %v, want [request_project_create]", met.packetsReceived)
+ }
+ if len(met.fetchCounts) != 1 || met.fetchCounts[0] != 1 {
+ t.Errorf("fetchCounts: %v, want [1]", met.fetchCounts)
+ }
+}
+
+// TestPoller_DuplicateAmieIDInOneBatchDedupes, same amie_id twice in one poll
+// response. After processing, only one amie_packets row exists. Witness for B3
+// (poller dedup race), current single-process deployment makes this safe; the
+// race only exists with concurrent pollers.
+func TestPoller_DuplicateAmieIDInOneBatchDedupes(t *testing.T) {
+ database := setupTestDB(t)
+ p1 := makePollerPacket(2001, "request_project_create")
+ p2 := makePollerPacket(2001, "request_project_create")
+ stub := &stubAmieClient{responses: [][]map[string]any{{p1, p2}}}
+ p, _ := newPoller(t, stub)
+
+ p.pollForPackets(context.Background())
+
+ if got := countRows(t, database, "amie_packets"); got != 1 {
+ t.Errorf("amie_packets after dup-in-batch: got %d, want 1", got)
+ }
+ if got := countRows(t, database, "amie_processing_events"); got != 1 {
+ t.Errorf("amie_processing_events after dup-in-batch: got %d, want 1", got)
+ }
+}
+
+// TestPoller_DuplicateAmieIDAcrossTicksDedupes, same packet returned by the
+// server on two consecutive ticks; only one row persists.
+func TestPoller_DuplicateAmieIDAcrossTicksDedupes(t *testing.T) {
+ database := setupTestDB(t)
+ pkt := makePollerPacket(3001, "data_project_create")
+ stub := &stubAmieClient{responses: [][]map[string]any{{pkt}, {pkt}}}
+ p, _ := newPoller(t, stub)
+
+ p.pollForPackets(context.Background())
+ p.pollForPackets(context.Background())
+
+ if got := countRows(t, database, "amie_packets"); got != 1 {
+ t.Errorf("amie_packets across ticks: got %d, want 1", got)
+ }
+}
+
+// TestPoller_EmptyServerResponseWritesNothing, empty packet list → no rows,
+// no RecordPacketReceived, no RecordPollerFetch (which only fires when > 0).
+func TestPoller_EmptyServerResponseWritesNothing(t *testing.T) {
+ database := setupTestDB(t)
+ stub := &stubAmieClient{responses: [][]map[string]any{{}}}
+ p, met := newPoller(t, stub)
+
+ p.pollForPackets(context.Background())
+
+ if got := countRows(t, database, "amie_packets"); got != 0 {
+ t.Errorf("amie_packets on empty fetch: got %d, want 0", got)
+ }
+ if len(met.fetchCounts) != 0 {
+ t.Errorf("fetchCounts on empty: %v, want []", met.fetchCounts)
+ }
+}
+
+// TestPoller_MalformedPacketIsDropped, packet missing header.packet_rec_id
+// is logged and skipped, no DB write.
+func TestPoller_MalformedPacketIsDropped(t *testing.T) {
+ database := setupTestDB(t)
+ badPacket := map[string]any{
+ "type": "request_project_create",
+ // no header at all
+ "body": map[string]any{"GrantNumber": "TG-BAD"},
+ }
+ stub := &stubAmieClient{responses: [][]map[string]any{{badPacket}}}
+ p, _ := newPoller(t, stub)
+
+ p.pollForPackets(context.Background())
+
+ if got := countRows(t, database, "amie_packets"); got != 0 {
+ t.Errorf("amie_packets after malformed: got %d, want 0", got)
+ }
+}
+
+// TestPoller_MissingTypeIsDropped, packet with header but no top-level type
+// is also skipped.
+func TestPoller_MissingTypeIsDropped(t *testing.T) {
+ database := setupTestDB(t)
+ noType := map[string]any{
+ "header": map[string]any{"packet_rec_id": float64(4001)},
+ "body": map[string]any{},
+ }
+ stub := &stubAmieClient{responses: [][]map[string]any{{noType}}}
+ p, _ := newPoller(t, stub)
+
+ p.pollForPackets(context.Background())
+
+ if got := countRows(t, database, "amie_packets"); got != 0 {
+ t.Errorf("amie_packets after missing type: got %d, want 0", got)
+ }
+}
+
+// TestPoller_DisabledRunReturnsImmediately, when PollerEnabled=false the
+// Run loop must return without calling the AMIE client.
+func TestPoller_DisabledRunReturnsImmediately(t *testing.T) {
+ database := setupTestDB(t)
+ stub := &stubAmieClient{responses: [][]map[string]any{{makePollerPacket(5001, "request_project_create")}}}
+ met := &stubMetrics{}
+ cfg := config.AMIEConfig{
+ PollInterval: 10 * time.Millisecond,
+ PollerEnabled: false,
+ }
+ p := NewPoller(stub, store.NewPacketStore(database), store.NewEventStore(database), met, database, cfg)
+
+ done := make(chan struct{})
+ go func() {
+ p.Run(context.Background())
+ close(done)
+ }()
+ select {
+ case <-done:
+ // ok. Run returned without polling
+ case <-time.After(500 * time.Millisecond):
+ t.Fatal("disabled Run did not return immediately")
+ }
+
+ if stub.callCount() != 0 {
+ t.Errorf("disabled poller called client %d times, want 0", stub.callCount())
+ }
+}
diff --git a/connectors/ACCESS/AMIE-Processor/worker/processor_failures_integration_test.go b/connectors/ACCESS/AMIE-Processor/worker/processor_failures_integration_test.go
new file mode 100644
index 000000000..24300c3a0
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/worker/processor_failures_integration_test.go
@@ -0,0 +1,134 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package worker
+
+import (
+ "context"
+ "database/sql"
+ "errors"
+ "testing"
+
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/model"
+ "github.com/google/uuid"
+)
+
+// TestProcessor_RouterWriteRolledBackOnError: the processor wraps every
+// router invocation in a single transaction. A write the router performs
+// inside that tx MUST be rolled back when the router subsequently returns
+// an error. This is the contract that lets handlers trust their `*sql.Tx`
+// parameter.
+func TestProcessor_RouterWriteRolledBackOnError(t *testing.T) {
+ database := setupTestDB(t)
+ packetID, eventID := seedNewEvent(t, database)
+
+ // fakeRouter that writes a probe row to amie_audit_log inside the tx,
+ // then returns an error, forcing the processor to roll the tx back.
+ auditProbe := uuid.NewString()
+ router := &probeRouter{
+ work: func(ctx context.Context, tx *sql.Tx) error {
+ _, err := tx.ExecContext(ctx,
+ `INSERT INTO amie_audit_log (packet_id, action, entity_type, summary)
+ VALUES (?, ?, ?, ?)`,
+ packetID, "CREATE_PROJECT", "probe", auditProbe,
+ )
+ return err
+ },
+ returnErr: errors.New("simulated handler failure after audit write"),
+ }
+ p := newProcessor(database, router, &stubMetrics{})
+
+ p.processPendingEvents(context.Background())
+
+ // The audit write must NOT be visible, tx rolled back.
+ var hits int
+ if err := database.Get(&hits,
+ "SELECT COUNT(*) FROM amie_audit_log WHERE summary = ?", auditProbe,
+ ); err != nil {
+ t.Fatalf("query audit probe: %v", err)
+ }
+ if hits != 0 {
+ t.Errorf("probe audit rows: got %d, want 0 (tx must roll back when router errors)", hits)
+ }
+
+ // Event should still be visible in RETRY_SCHEDULED state (separate
+ // "record failure" tx is what writes the error row + retry schedule).
+ ev := readEvent(t, database, eventID)
+ if ev.Status != model.ProcessingStatusRetryScheduled {
+ t.Errorf("event status: got %q, want RETRY_SCHEDULED", ev.Status)
+ }
+ if got := countRows(t, database, "amie_processing_errors"); got != 1 {
+ t.Errorf("processing_errors: got %d, want 1", got)
+ }
+}
+
+// TestProcessor_FAILEDStatusIsUnreachable: `FAILED` is defined in
+// model/event.go but no code path in the processor writes it. After driving
+// an event through success, retry, and permanent-fail flows, no
+// amie_processing_events row should end in FAILED.
+func TestProcessor_FAILEDStatusIsUnreachable(t *testing.T) {
+ database := setupTestDB(t)
+
+ // One event that succeeds.
+ _, _ = seedNewEvent(t, database)
+ pOK := newProcessor(database, &fakeRouter{}, &stubMetrics{})
+ pOK.processPendingEvents(context.Background())
+
+ // One event that permanently fails after 3 attempts.
+ _, permID := seedNewEvent(t, database)
+ pFail := newProcessor(database,
+ &fakeRouter{errors: []error{errors.New("e1"), errors.New("e2"), errors.New("e3")}},
+ &stubMetrics{},
+ )
+ pFail.processPendingEvents(context.Background())
+ forceRetryNow(t, database, permID)
+ pFail.processPendingEvents(context.Background())
+ forceRetryNow(t, database, permID)
+ pFail.processPendingEvents(context.Background())
+
+ if ev := readEvent(t, database, permID); ev.Status != model.ProcessingStatusPermanentlyFailed {
+ t.Errorf("perm-fail event status: got %q, want PERMANENTLY_FAILED", ev.Status)
+ }
+
+ var failedCount int
+ if err := database.Get(&failedCount,
+ "SELECT COUNT(*) FROM amie_processing_events WHERE status = 'FAILED'",
+ ); err != nil {
+ t.Fatalf("count FAILED events: %v", err)
+ }
+ if failedCount != 0 {
+ t.Errorf("amie_processing_events in FAILED status: got %d, want 0 (B1: unreachable)", failedCount)
+ }
+}
+
+// probeRouter does a configurable write inside the handler's tx, then
+// returns a configurable error. Lets tests verify tx atomicity.
+type probeRouter struct {
+ work func(ctx context.Context, tx *sql.Tx) error
+ returnErr error
+}
+
+func (p *probeRouter) Route(ctx context.Context, tx *sql.Tx, _ map[string]any, _ *model.Packet, _ string) error {
+ if p.work != nil {
+ if err := p.work(ctx, tx); err != nil {
+ return err
+ }
+ }
+ return p.returnErr
+}
diff --git a/connectors/ACCESS/AMIE-Processor/worker/processor_integration_test.go b/connectors/ACCESS/AMIE-Processor/worker/processor_integration_test.go
new file mode 100644
index 000000000..d9128ca04
--- /dev/null
+++ b/connectors/ACCESS/AMIE-Processor/worker/processor_integration_test.go
@@ -0,0 +1,290 @@
+//go:build integration
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://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.
+
+package worker
+
+import (
+ "context"
+ "database/sql"
+ "errors"
+ "testing"
+ "time"
+
+ "github.com/google/uuid"
+ "github.com/jmoiron/sqlx"
+
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/config"
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/model"
+ "github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/store"
+)
+
+// State machine under test (from processor.go):
+// NEW → RUNNING → SUCCEEDED (success path)
+// NEW → RUNNING → (rollback) → RETRY_SCHEDULED (transient failure, attempts<MaxAttempts)
+// NEW → RUNNING → (rollback) → PERMANENTLY_FAILED (after MaxAttempts)
+
+// fakeRouter is a programmable processorRouter. Each call consults the next
+// element of `errors`; if non-nil, that call returns the error. Else nil.
+type fakeRouter struct {
+ errors []error
+ calls int
+}
+
+func (f *fakeRouter) Route(_ context.Context, _ *sql.Tx, _ map[string]any, _ *model.Packet, _ string) error {
+ idx := f.calls
+ f.calls++
+ if idx < len(f.errors) && f.errors[idx] != nil {
+ return f.errors[idx]
+ }
+ return nil
+}
+
+// seedNewEvent inserts an amie_packets row + amie_processing_events row both
+// in NEW state. Returns the IDs so tests can refetch state directly.
+func seedNewEvent(t *testing.T, database *sqlx.DB) (packetID, eventID string) {
+ t.Helper()
+ packetID = uuid.NewString()
+ eventID = uuid.NewString()
+ now := time.Now().UTC()
+
+ if _, err := database.Exec(
+ `INSERT INTO amie_packets (id, amie_id, type, status, raw_json, received_at, retries)
+ VALUES (?, ?, ?, ?, ?, ?, ?)`,
+ packetID, time.Now().UnixNano(), "request_project_create", string(model.PacketStatusNew),
+ `{"type":"request_project_create","body":{}}`, now, 0,
+ ); err != nil {
+ t.Fatalf("seed packet: %v", err)
+ }
+
+ if _, err := database.Exec(
+ `INSERT INTO amie_processing_events (id, packet_id, type, status, attempts, payload, created_at)
+ VALUES (?, ?, ?, ?, ?, ?, ?)`,
+ eventID, packetID, string(model.EventTypeDecodePacket), string(model.ProcessingStatusNew), 0, []byte(""), now,
+ ); err != nil {
+ t.Fatalf("seed event: %v", err)
+ }
+ return packetID, eventID
+}
+
+func newProcessor(database *sqlx.DB, router processorRouter, met *stubMetrics) *Processor {
+ cfg := config.AMIEConfig{WorkerInterval: 50 * time.Millisecond}
+ return NewProcessor(
+ store.NewEventStore(database),
+ store.NewPacketStore(database),
+ store.NewProcessingErrorStore(database),
+ router,
+ met,
+ database,
+ cfg,
+ )
+}
+
+func readEvent(t *testing.T, database *sqlx.DB, id string) *model.ProcessingEvent {
+ t.Helper()
+ var e model.ProcessingEvent
+ if err := database.Get(&e,
+ `SELECT id, packet_id, type, status, attempts, payload, created_at, started_at, finished_at, last_error, next_retry_at
+ FROM amie_processing_events WHERE id = ?`, id,
+ ); err != nil {
+ t.Fatalf("read event %s: %v", id, err)
+ }
+ return &e
+}
+
+func readPacket(t *testing.T, database *sqlx.DB, id string) *model.Packet {
+ t.Helper()
+ var p model.Packet
+ if err := database.Get(&p,
+ `SELECT id, amie_id, type, status, raw_json, received_at, decoded_at, processed_at, retries, last_error
+ FROM amie_packets WHERE id = ?`, id,
+ ); err != nil {
+ t.Fatalf("read packet %s: %v", id, err)
+ }
+ return &p
+}
+
+// forceRetryNow nudges a RETRY_SCHEDULED event to be eligible immediately so
+// the next processPendingEvents call picks it up without waiting for the
+// real backoff. Backoff timing itself is covered by the dedicated test.
+func forceRetryNow(t *testing.T, database *sqlx.DB, eventID string) {
+ t.Helper()
+ if _, err := database.Exec(
+ "UPDATE amie_processing_events SET next_retry_at = NULL WHERE id = ?", eventID,
+ ); err != nil {
+ t.Fatalf("force retry: %v", err)
+ }
+}
+
+// TestProcessor_SuccessFirstAttempt, router returns nil; event ends
+// SUCCEEDED, packet ends DECODED, no error rows.
+func TestProcessor_SuccessFirstAttempt(t *testing.T) {
+ database := setupTestDB(t)
+ _, eventID := seedNewEvent(t, database)
+
+ router := &fakeRouter{}
+ met := &stubMetrics{}
+ p := newProcessor(database, router, met)
+
+ p.processPendingEvents(context.Background())
+
+ ev := readEvent(t, database, eventID)
+ if ev.Status != model.ProcessingStatusSucceeded {
+ t.Errorf("event status: got %q, want SUCCEEDED", ev.Status)
+ }
+ if ev.Attempts != 1 {
+ t.Errorf("attempts: got %d, want 1", ev.Attempts)
+ }
+ if ev.NextRetryAt != nil {
+ t.Errorf("next_retry_at: got %v, want nil", ev.NextRetryAt)
+ }
+ pkt := readPacket(t, database, ev.PacketID)
+ if pkt.Status != model.PacketStatusDecoded {
+ t.Errorf("packet status: got %q, want DECODED", pkt.Status)
+ }
+ if got := countRows(t, database, "amie_processing_errors"); got != 0 {
+ t.Errorf("error rows: got %d, want 0", got)
+ }
+}
+
+// TestProcessor_FailTwiceSucceedThird, three attempts total; first two fail,
+// third succeeds. Two error rows persist; event ends SUCCEEDED.
+func TestProcessor_FailTwiceSucceedThird(t *testing.T) {
+ database := setupTestDB(t)
+ _, eventID := seedNewEvent(t, database)
+
+ router := &fakeRouter{errors: []error{errors.New("boom 1"), errors.New("boom 2")}}
+ met := &stubMetrics{}
+ p := newProcessor(database, router, met)
+
+ p.processPendingEvents(context.Background())
+ if ev := readEvent(t, database, eventID); ev.Status != model.ProcessingStatusRetryScheduled || ev.Attempts != 1 {
+ t.Fatalf("after attempt 1: status=%q attempts=%d, want RETRY_SCHEDULED/1", ev.Status, ev.Attempts)
+ }
+ forceRetryNow(t, database, eventID)
+
+ p.processPendingEvents(context.Background())
+ if ev := readEvent(t, database, eventID); ev.Status != model.ProcessingStatusRetryScheduled || ev.Attempts != 2 {
+ t.Fatalf("after attempt 2: status=%q attempts=%d, want RETRY_SCHEDULED/2", ev.Status, ev.Attempts)
+ }
+ forceRetryNow(t, database, eventID)
+
+ p.processPendingEvents(context.Background())
+ ev := readEvent(t, database, eventID)
+ if ev.Status != model.ProcessingStatusSucceeded {
+ t.Errorf("final status: got %q, want SUCCEEDED", ev.Status)
+ }
+ if ev.Attempts != 3 {
+ t.Errorf("final attempts: got %d, want 3", ev.Attempts)
+ }
+ if got := countRows(t, database, "amie_processing_errors"); got != 2 {
+ t.Errorf("error rows: got %d, want 2", got)
+ }
+}
+
+// TestProcessor_FailThreeTimesPermanentlyFails, every attempt fails. Event
+// ends PERMANENTLY_FAILED, packet ends FAILED, next_retry_at is NULL, three
+// error rows.
+func TestProcessor_FailThreeTimesPermanentlyFails(t *testing.T) {
+ database := setupTestDB(t)
+ _, eventID := seedNewEvent(t, database)
+
+ router := &fakeRouter{errors: []error{
+ errors.New("boom 1"), errors.New("boom 2"), errors.New("boom 3"),
+ }}
+ p := newProcessor(database, router, &stubMetrics{})
+
+ p.processPendingEvents(context.Background())
+ forceRetryNow(t, database, eventID)
+ p.processPendingEvents(context.Background())
+ forceRetryNow(t, database, eventID)
+ p.processPendingEvents(context.Background())
+
+ ev := readEvent(t, database, eventID)
+ if ev.Status != model.ProcessingStatusPermanentlyFailed {
+ t.Errorf("event status: got %q, want PERMANENTLY_FAILED", ev.Status)
+ }
+ if ev.Attempts != MaxAttempts {
+ t.Errorf("attempts: got %d, want %d", ev.Attempts, MaxAttempts)
+ }
+ if ev.NextRetryAt != nil {
+ t.Errorf("next_retry_at on permanent fail: got %v, want nil", ev.NextRetryAt)
+ }
+ pkt := readPacket(t, database, ev.PacketID)
+ if pkt.Status != model.PacketStatusFailed {
+ t.Errorf("packet status: got %q, want FAILED", pkt.Status)
+ }
+ if got := countRows(t, database, "amie_processing_errors"); got != MaxAttempts {
+ t.Errorf("error rows: got %d, want %d", got, MaxAttempts)
+ }
+}
+
+// TestProcessor_BackoffBlocksFutureRetry, a RETRY_SCHEDULED event with
+// next_retry_at in the future MUST NOT be picked up. Verifies the
+// FindTop50EventsToProcess filter respects the schedule.
+func TestProcessor_BackoffBlocksFutureRetry(t *testing.T) {
+ database := setupTestDB(t)
+ _, eventID := seedNewEvent(t, database)
+
+ router := &fakeRouter{errors: []error{errors.New("boom")}}
+ p := newProcessor(database, router, &stubMetrics{})
+
+ // First attempt → fails → event becomes RETRY_SCHEDULED with next_retry_at = now + 30s.
+ p.processPendingEvents(context.Background())
+ ev := readEvent(t, database, eventID)
+ if ev.Status != model.ProcessingStatusRetryScheduled {
+ t.Fatalf("setup: event not RETRY_SCHEDULED, got %q", ev.Status)
+ }
+ if ev.NextRetryAt == nil || !ev.NextRetryAt.After(time.Now().UTC()) {
+ t.Fatalf("setup: next_retry_at not in future, got %v", ev.NextRetryAt)
+ }
+
+ // Run processor again without forcing retry, event should NOT be re-picked.
+ callsBefore := router.calls
+ p.processPendingEvents(context.Background())
+ if router.calls != callsBefore {
+ t.Errorf("router invoked while next_retry_at in future: calls went %d -> %d", callsBefore, router.calls)
+ }
+ ev2 := readEvent(t, database, eventID)
+ if ev2.Attempts != 1 {
+ t.Errorf("attempts: got %d, want 1 (no re-pick during backoff)", ev2.Attempts)
+ }
+}
+
+// TestProcessor_AttemptCounterAcrossRollback verifies the subtle invariant
+// in recordFailureInNewTransaction: the processing tx rolls back (attempt
+// increment reverts), then the new tx re-reads the event and computes
+// effectiveAttempts = stored+1. After one failure, attempts must equal
+// exactly 1.
+func TestProcessor_AttemptCounterAcrossRollback(t *testing.T) {
+ database := setupTestDB(t)
+ _, eventID := seedNewEvent(t, database)
+
+ router := &fakeRouter{errors: []error{errors.New("boom")}}
+ p := newProcessor(database, router, &stubMetrics{})
+
+ p.processPendingEvents(context.Background())
+
+ ev := readEvent(t, database, eventID)
+ if ev.Attempts != 1 {
+ t.Errorf("attempts after one failure: got %d, want 1", ev.Attempts)
+ }
+ if got := countRows(t, database, "amie_processing_errors"); got != 1 {
+ t.Errorf("error rows after one failure: got %d, want 1", got)
+ }
+}
diff --git a/dev-ops/local-amie/Dockerfile.mock b/dev-ops/local-amie/Dockerfile.mock
new file mode 100644
index 000000000..88863ae0b
--- /dev/null
+++ b/dev-ops/local-amie/Dockerfile.mock
@@ -0,0 +1,28 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://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.
+
+FROM python:3.11-slim
+
+WORKDIR /app
+
+RUN pip install --no-cache-dir flask==3.1.3
+
+COPY mock-amie-server.py ./
+
+EXPOSE 8180
+
+CMD ["python", "-u", "mock-amie-server.py"]
diff --git a/dev-ops/local-amie/Makefile b/dev-ops/local-amie/Makefile
new file mode 100644
index 000000000..e48c683dd
--- /dev/null
+++ b/dev-ops/local-amie/Makefile
@@ -0,0 +1,32 @@
+SHELL := /bin/bash
+.PHONY: up down build logs ps wait
+
+up:
+ docker compose up -d --build
+ $(MAKE) wait
+
+down:
+ docker compose down -v --remove-orphans
+
+build:
+ docker compose build
+
+logs:
+ docker compose logs -f --tail=100
+
+ps:
+ docker compose ps
+
+# Blocks until both services report healthy. Times out at 90s.
+wait:
+ @deadline=$$(( $$(date +%s) + 90 )); \
+ while true; do \
+ db_ok=$$(docker inspect -f '{{.State.Health.Status}}' custos_amie_test_db 2>/dev/null || echo starting); \
+ mock_ok=$$(docker inspect -f '{{.State.Health.Status}}' custos_amie_test_mock 2>/dev/null || echo starting); \
+ if [ "$$db_ok" = "healthy" ] && [ "$$mock_ok" = "healthy" ]; then echo "ready"; exit 0; fi; \
+ if [ "$$(date +%s)" -ge "$$deadline" ]; then \
+ echo "TIMED OUT waiting for healthy: db=$$db_ok mock=$$mock_ok" >&2; \
+ docker compose ps; exit 1; \
+ fi; \
+ sleep 2; \
+ done
diff --git a/dev-ops/local-amie/README.md b/dev-ops/local-amie/README.md
new file mode 100644
index 000000000..db8cd6243
--- /dev/null
+++ b/dev-ops/local-amie/README.md
@@ -0,0 +1,23 @@
+# local-amie
+
+Isolated Docker stack for AMIE connector integration tests. Runs on offset
+ports so it coexists with the main dev stack in `dev-ops/compose/`.
+
+## Services
+
+| Service | Container | Host port |
+|-------------|-------------------------|-----------|
+| MariaDB | `custos_amie_test_db` | `3307` |
+| mock-amie | `custos_amie_test_mock` | `8181` |
+
+## Usage
+
+```bash
+make up # build + start, blocks until both are healthy
+make down # stop + remove + drop volumes
+make logs # tail both services
+make ps # status
+```
+
+The integration test runner at `scripts/run-amie-integration-tests.sh`
+handles up/down for you.
diff --git a/dev-ops/local-amie/compose.yaml b/dev-ops/local-amie/compose.yaml
new file mode 100644
index 000000000..42f264064
--- /dev/null
+++ b/dev-ops/local-amie/compose.yaml
@@ -0,0 +1,56 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://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.
+
+# Isolated DB + mock-AMIE stack for AMIE connector integration tests.
+# Ports are deliberately offset from the dev-ops/compose stack so both can
+# coexist on one developer machine.
+
+services:
+ db:
+ container_name: custos_amie_test_db
+ image: mariadb:11.2
+ restart: "no"
+ command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
+ ports:
+ - "3307:3306"
+ environment:
+ MARIADB_ROOT_PASSWORD: root
+ MARIADB_USER: admin
+ MARIADB_PASSWORD: admin
+ MAX_ALLOWED_PACKET: 1073741824
+ volumes:
+ - ../compose/dbinit:/docker-entrypoint-initdb.d:ro
+ healthcheck:
+ test: [ "CMD", "mariadb-admin", "ping", "-uroot", "-proot", "--silent" ]
+ interval: 2s
+ timeout: 5s
+ retries: 30
+
+ mock-amie:
+ container_name: custos_amie_test_mock
+ build:
+ context: ../../connectors/ACCESS/AMIE-Processor/mock-server
+ dockerfile: ../../../../dev-ops/local-amie/Dockerfile.mock
+ restart: "no"
+ ports:
+ - "8181:8180"
+ healthcheck:
+ test: [ "CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8180/packets/TESTSITE').read()" ]
+ interval: 2s
+ timeout: 5s
+ retries: 30
diff --git a/scripts/run-amie-integration-tests.sh b/scripts/run-amie-integration-tests.sh
new file mode 100755
index 000000000..49e068024
--- /dev/null
+++ b/scripts/run-amie-integration-tests.sh
@@ -0,0 +1,40 @@
+#!/usr/bin/env bash
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://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.
+
+set -euo pipefail
+
+REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
+cd "$REPO_ROOT"
+
+cleanup() {
+ make -s -C dev-ops/local-amie down >/dev/null 2>&1 || true
+}
+trap cleanup EXIT
+
+make -s -C dev-ops/local-amie down >/dev/null 2>&1 || true
+make -s -C dev-ops/local-amie up
+
+export DATABASE_DSN="admin:admin@tcp(localhost:3307)/custos?parseTime=true&charset=utf8mb4&multiStatements=true"
+export AMIE_BASE_URL="http://localhost:8181"
+export AMIE_SITE_CODE="TESTSITE"
+export AMIE_API_KEY="dev"
+export AMIE_CLUSTER_ID="00000000-0000-0000-0000-000000000001"
+export TEST_AMIE_INFRA_READY=1
+
+go test -tags integration -v -count=1 -p 1 -timeout 10m ./connectors/ACCESS/AMIE-Processor/...