diff --git a/lib/trento/domain/sap_system/sap_system.ex b/lib/trento/domain/sap_system/sap_system.ex index c27d7498bb..a1241ef10d 100644 --- a/lib/trento/domain/sap_system/sap_system.ex +++ b/lib/trento/domain/sap_system/sap_system.ex @@ -931,8 +931,12 @@ defmodule Trento.Domain.SapSystem do end end + # Restore a SAP system when the database is registered defp maybe_emit_sap_system_restored_event( - %SapSystem{application: %Application{instances: instances}}, + %SapSystem{ + application: %Application{instances: instances}, + database: %Database{deregistered_at: nil} + }, %RegisterApplicationInstance{ sap_system_id: sap_system_id, tenant: tenant, @@ -950,6 +954,9 @@ defmodule Trento.Domain.SapSystem do end end + # Do not restore SAP system if the database is not registered + defp maybe_emit_sap_system_restored_event(%SapSystem{}, %RegisterApplicationInstance{}), do: nil + defp maybe_emit_sap_system_registered_or_updated_event( %SapSystem{sid: nil, application: %Application{instances: instances}}, %RegisterApplicationInstance{ diff --git a/test/trento/domain/sap_system/sap_system_test.exs b/test/trento/domain/sap_system/sap_system_test.exs index 360904bb89..281b09c6e5 100644 --- a/test/trento/domain/sap_system/sap_system_test.exs +++ b/test/trento/domain/sap_system/sap_system_test.exs @@ -2720,6 +2720,128 @@ defmodule Trento.SapSystemTest do ) end + test "should not restore a sap system if the database is not present and messageserver/abap instance are present" do + sap_system_id = UUID.uuid4() + + primary_database_host_id = UUID.uuid4() + secondary_database_host_id = UUID.uuid4() + + deregistered_at = DateTime.utc_now() + + db_instance_number_1 = "00" + db_instance_number_2 = "01" + + db_sid = fake_sid() + application_sid = fake_sid() + + message_server_host_id = UUID.uuid4() + message_server_instance_number = "00" + abap_host_id = UUID.uuid4() + abap_instance_number = "01" + + initial_events = [ + build( + :database_registered_event, + sap_system_id: sap_system_id, + sid: db_sid + ), + build( + :database_instance_registered_event, + sap_system_id: sap_system_id, + host_id: primary_database_host_id, + sid: db_sid, + instance_number: db_instance_number_1, + system_replication: "Primary" + ), + build( + :database_instance_registered_event, + sap_system_id: sap_system_id, + host_id: secondary_database_host_id, + instance_number: db_instance_number_2, + system_replication: "Secondary", + sid: db_sid + ), + build( + :application_instance_registered_event, + sap_system_id: sap_system_id, + features: "MESSAGESERVER|ENQUE", + host_id: message_server_host_id, + instance_number: message_server_instance_number, + sid: application_sid + ), + build( + :application_instance_registered_event, + sap_system_id: sap_system_id, + features: "ABAP|GATEWAY|ICMAN|IGS", + host_id: abap_host_id, + instance_number: abap_instance_number, + sid: application_sid + ), + build( + :sap_system_registered_event, + sap_system_id: sap_system_id, + sid: application_sid + ), + build(:database_instance_deregistered_event, + sap_system_id: sap_system_id, + host_id: primary_database_host_id, + instance_number: db_instance_number_1, + deregistered_at: deregistered_at + ), + build(:database_deregistered_event, + sap_system_id: sap_system_id, + deregistered_at: deregistered_at + ), + build(:sap_system_deregistered_event, + sap_system_id: sap_system_id, + deregistered_at: deregistered_at + ), + build(:application_instance_deregistered_event, + sap_system_id: sap_system_id, + deregistered_at: deregistered_at, + instance_number: message_server_instance_number, + host_id: message_server_host_id + ) + ] + + command = + build( + :register_application_instance_command, + sap_system_id: sap_system_id, + sid: application_sid, + db_host: primary_database_host_id, + features: "MESSAGESERVER" + ) + + assert_events_and_state( + initial_events, + command, + [ + %ApplicationInstanceRegistered{ + sap_system_id: sap_system_id, + sid: application_sid, + host_id: command.host_id, + instance_number: command.instance_number, + instance_hostname: command.instance_hostname, + features: command.features, + http_port: command.http_port, + https_port: command.https_port, + start_priority: command.start_priority, + health: command.health + } + ], + fn sap_system -> + assert %SapSystem{ + deregistered_at: ^deregistered_at, + database: %Database{ + deregistered_at: ^deregistered_at, + sid: ^db_sid + } + } = sap_system + end + ) + end + test "should reject all the commands except for the registration/instance deregistration ones, when the SAP system is deregistered" do sap_system_id = UUID.uuid4()