Skip to content

Commit

Permalink
Addressing feedbacks, fix testing
Browse files Browse the repository at this point in the history
  • Loading branch information
CDimonaco committed Mar 25, 2024
1 parent 9a48d5d commit bbc3eec
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Trento.Infrastructure.Commanded.EventHandlers.DatabaseDeregistrationEv
%DatabaseDeregistered{database_id: database_id, deregistered_at: deregistered_at},
_metadata
) do
database =
%{sap_systems: sap_systems} =
Repo.one!(
from(d in DatabaseReadModel,
where: d.id == ^database_id,
Expand All @@ -30,10 +30,8 @@ defmodule Trento.Infrastructure.Commanded.EventHandlers.DatabaseDeregistrationEv
)
)

for %{id: sap_system_id} <- database.sap_systems do
Logger.info(
"Deregistering sap system: #{sap_system_id} attached to database: #{database_id}"
)
for %{id: sap_system_id, sid: sid} <- sap_systems do
Logger.info("Deregistering sap system #{sid} attached to database #{database_id}")

commanded().dispatch(
%DeregisterSapSystem{sap_system_id: sap_system_id, deregistered_at: deregistered_at},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Trento.Infrastructure.Commanded.EventHandlers.DatabaseRestoreEventHand
%DatabaseRestored{database_id: database_id},
_metadata
) do
database =
%{sap_systems: sap_systems} =
Repo.one!(
from(d in DatabaseReadModel,
where: d.id == ^database_id,
Expand All @@ -30,8 +30,8 @@ defmodule Trento.Infrastructure.Commanded.EventHandlers.DatabaseRestoreEventHand
)
)

for %{id: sap_system_id, tenant: tenant, db_host: db_host} <- database.sap_systems do
Logger.info("Restoring sap system: #{sap_system_id} attached to database: #{database_id}")
for %{id: sap_system_id, tenant: tenant, db_host: db_host, sid: sid} <- sap_systems do
Logger.info("Restoring sap system #{sid} attached to database #{database_id}")

commanded().dispatch(
%RestoreSapSystem{sap_system_id: sap_system_id, db_host: db_host, tenant: tenant},
Expand Down
2 changes: 1 addition & 1 deletion lib/trento/sap_systems/commands/deregister_sap_system.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Trento.SapSystems.Commands.DeregisterSapSystem do
@moduledoc """
Deregister (decommission) a SapSystem instance from the monitoring system.
Deregister (decommission) a SAP System
"""

@required_fields :all
Expand Down
3 changes: 2 additions & 1 deletion test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,8 @@ defmodule Trento.Factory do
db_host: Faker.Internet.ip_v4_address(),
health: Health.unknown(),
ensa_version: EnsaVersion.ensa1(),
deregistered_at: nil
deregistered_at: nil,
database_id: Faker.UUID.v4()
}
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Trento.Infrastructure.Commanded.EventHandlers.DatabaseDeregistrationEventHandlerTest do
use ExUnit.Case
use Trento.EventStoreCase
use Trento.DataCase

import Mox
import Trento.Factory
Expand All @@ -13,13 +13,23 @@ defmodule Trento.Infrastructure.Commanded.EventHandlers.DatabaseDeregistrationEv

test "should dispatch DeregisterSapSystem commands when a database is deregistered" do
%{id: database_id} = insert(:database)
%{id: sap_system_id} = insert(:sap_system, database_id: database_id)

[%{id: first_sap_system_id}, %{id: second_sap_system_id}] =
insert_list(2, :sap_system, database_id: database_id)

deregistered_at = DateTime.utc_now()
event = %DatabaseDeregistered{database_id: database_id, deregistered_at: deregistered_at}

expect(Trento.Commanded.Mock, :dispatch, fn %DeregisterSapSystem{
sap_system_id: ^sap_system_id,
sap_system_id: ^first_sap_system_id,
deregistered_at: ^deregistered_at
},
_ ->
:ok
end)

expect(Trento.Commanded.Mock, :dispatch, fn %DeregisterSapSystem{
sap_system_id: ^second_sap_system_id,
deregistered_at: ^deregistered_at
},
_ ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Trento.Infrastructure.Commanded.EventHandlers.DatabaseRestoreEventHandlerTest do
use ExUnit.Case
use Trento.EventStoreCase
use Trento.DataCase

import Mox
import Trento.Factory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,13 @@ defmodule Trento.SapSystems.Projections.SapSystemProjectorTest do
end

test "should restore a SAP system when SapSystemRestored is received" do
%{id: database_id} = insert(:database)

%{tenant: tenant, id: sap_system_id, sid: sid} =
insert(:sap_system, deregistered_at: DateTime.utc_now())
insert(:sap_system, deregistered_at: DateTime.utc_now(), database_id: database_id)

database_instance =
insert(:database_instance_without_host, sap_system_id: sap_system_id)
insert(:database_instance_without_host, sap_system_id: database_id)
|> Map.from_struct()
|> Map.delete(:__meta__)
|> Map.delete(:host)
Expand Down
14 changes: 0 additions & 14 deletions test/trento/sap_systems/sap_system_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1599,11 +1599,9 @@ defmodule Trento.SapSystems.SapSystemTest do

message_server_host_id = UUID.uuid4()
abap_host_id = UUID.uuid4()
enqrep_host_id = UUID.uuid4()

message_server_instance_number = "01"
abap_instance_number = "02"
enqrep_server_instance_number = "03"

application_sid = fake_sid()

Expand All @@ -1629,14 +1627,6 @@ defmodule Trento.SapSystems.SapSystemTest do
:sap_system_registered_event,
sap_system_id: sap_system_id,
sid: application_sid
),
build(
:application_instance_registered_event,
sap_system_id: sap_system_id,
host_id: enqrep_host_id,
instance_number: enqrep_server_instance_number,
sid: application_sid,
features: "ENQREP"
)
],
%DeregisterSapSystem{
Expand All @@ -1654,10 +1644,6 @@ defmodule Trento.SapSystems.SapSystemTest do
sid: ^application_sid,
deregistered_at: ^deregistered_at,
instances: [
%Instance{
host_id: ^enqrep_host_id,
instance_number: ^enqrep_server_instance_number
},
%Instance{
host_id: ^abap_host_id,
instance_number: ^abap_instance_number
Expand Down
22 changes: 14 additions & 8 deletions test/trento/sap_systems/services/health_summary_service_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ defmodule Trento.SapSystems.Services.HealthSummaryServiceTest do

%SapSystemReadModel{
id: sap_system_id,
sid: sid
sid: sid,
database_id: database_id
} = insert(:sap_system)

insert(
:database_instance_without_host,
sap_system_id: sap_system_id,
sap_system_id: database_id,
host_id: a_host_id
)

Expand Down Expand Up @@ -64,25 +65,28 @@ defmodule Trento.SapSystems.Services.HealthSummaryServiceTest do
%HostReadModel{id: app_host_id_2} =
app_host_2 = insert(:host, cluster_id: nil, heartbeat: Health.critical())

%{id: database_id} = insert(:database)

%SapSystemReadModel{
id: sap_system_id,
sid: sid
} = insert(:sap_system, health: Health.critical())
sid: sid,
database_id: database_id
} = insert(:sap_system, health: Health.critical(), database_id: database_id)

insert(:sap_system, deregistered_at: DateTime.utc_now())

database_instances = [
insert(
:database_instance,
sap_system_id: sap_system_id,
sap_system_id: database_id,
instance_number: "00",
host_id: db_host_id,
health: Health.warning(),
host: db_host
),
insert(
:database_instance,
sap_system_id: sap_system_id,
sap_system_id: database_id,
instance_number: "01",
host_id: db_host_id_2,
health: Health.passing(),
Expand Down Expand Up @@ -133,18 +137,20 @@ defmodule Trento.SapSystems.Services.HealthSummaryServiceTest do
%HostReadModel{id: app_host_id} =
app_host = insert(:host, cluster_id: nil, health: Health.passing())

%{id: database_id} = insert(:database)

%SapSystemReadModel{
id: sap_system_id,
sid: sid
} = insert(:sap_system, health: Health.critical())
} = insert(:sap_system, health: Health.critical(), database_id: database_id)

insert(:sap_system, deregistered_at: DateTime.utc_now())

database_instances =
insert_list(
1,
:database_instance,
sap_system_id: sap_system_id,
sap_system_id: database_id,
instance_number: "00",
host_id: db_host_id,
health: Health.warning(),
Expand Down
10 changes: 7 additions & 3 deletions test/trento/sap_systems_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ defmodule Trento.SapSystemsTest do

describe "sap_systems" do
test "should retrieve all the currently registered existing sap systems and the related instances" do
%{id: database_id} = insert(:database)

%SapSystemReadModel{
id: sap_system_id,
sid: sid,
tenant: tenant,
db_host: db_host
} = insert(:sap_system)
db_host: db_host,
database_id: database_id
} = insert(:sap_system, database_id: database_id)

insert(:sap_system, deregistered_at: DateTime.utc_now())

Expand All @@ -32,7 +35,7 @@ defmodule Trento.SapSystemsTest do

database_instances =
Enum.sort_by(
insert_list(5, :database_instance_without_host, sap_system_id: sap_system_id),
insert_list(5, :database_instance_without_host, sap_system_id: database_id),
&{&1.instance_number, &1.host_id}
)

Expand All @@ -42,6 +45,7 @@ defmodule Trento.SapSystemsTest do
sid: ^sid,
tenant: ^tenant,
db_host: ^db_host,
database_id: ^database_id,
application_instances: ^application_instances,
database_instances: ^database_instances
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ defmodule TrentoWeb.V1.HealthOverviewControllerTest do

%SapSystemReadModel{
id: sap_system_id,
sid: sid
sid: sid,
database_id: database_id
} = insert(:sap_system, health: Health.critical())

insert(
:database_instance_without_host,
sap_system_id: sap_system_id,
sap_system_id: database_id,
sid: "HDD",
host_id: host_1_id,
health: Health.warning()
Expand Down

0 comments on commit bbc3eec

Please sign in to comment.