Skip to content

Commit

Permalink
Restore a SAP system only when the database is present (#1706)
Browse files Browse the repository at this point in the history
  • Loading branch information
CDimonaco authored Aug 10, 2023
1 parent 000a46e commit 42eb982
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/trento/domain/sap_system/sap_system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{
Expand Down
122 changes: 122 additions & 0 deletions test/trento/domain/sap_system/sap_system_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 42eb982

Please sign in to comment.