Skip to content

Commit

Permalink
Revert back the policy usage in the processor
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Nov 4, 2022
1 parent 339a03a commit a6b3b2f
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 101 deletions.
2 changes: 0 additions & 2 deletions config/wanda.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ config :trento, :messaging, adapter: Trento.Messaging.Adapters.AMQP

config :trento, Trento.Integration.Checks, adapter: Trento.Integration.Checks.Wanda

config :trento, Trento.Integration.Checks.Wanda, policy: Trento.Integration.Checks.Wanda.Policy

config :trento, Trento.Messaging.Publisher, adapter: Trento.Messaging.Adapters.AMQP

config :trento, Trento.Messaging.Adapters.AMQP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,45 @@ defmodule Trento.Integration.Checks.Wanda.Messaging.AMQP.Processor do

alias Trento.Contracts

alias Trento.Checks.V1.ExecutionCompleted
alias Trento.Domain.Commands.CompleteChecksExecutionWanda

require Logger
require Trento.Domain.Enums.Health, as: Health

def process(%GenRMQ.Message{payload: payload} = message) do
Logger.debug("Received message: #{inspect(message)}")

with {:ok, event} <- Contracts.from_event(payload),
{:ok, command, opts} <- adapter().handle(event) do
{:ok, {command, opts}} <- handle(event) do
commanded().dispatch(command, opts)
else
{:error, reason} ->
{:error, reason}
end
end

defp adapter,
do: Application.fetch_env!(:trento, Trento.Integration.Checks.Wanda)[:policy]
def handle(%ExecutionCompleted{
execution_id: execution_id,
group_id: group_id,
result: result
}) do
case CompleteChecksExecutionWanda.new(%{
cluster_id: group_id,
health: map_health(result)
}) do
{:ok, command} ->
opts = [correlation_id: execution_id]
{:ok, {command, opts}}

error ->
error
end
end

defp map_health(:CRITICAL), do: Health.critical()
defp map_health(:WARNING), do: Health.warning()
defp map_health(:PASSING), do: Health.passing()

defp commanded,
do: Application.fetch_env!(:trento, Trento.Commanded)[:adapter]
Expand Down

This file was deleted.

This file was deleted.

8 changes: 0 additions & 8 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ Application.put_env(:trento, Trento.Integration.Prometheus,
Mox.defmock(Trento.Messaging.Adapters.Mock, for: Trento.Messaging.Adapters.Behaviour)
Application.put_env(:trento, :messaging, adapter: Trento.Messaging.Adapters.Mock)

Mox.defmock(Trento.Integration.Checks.Wanda.Policy.Mock,
for: Trento.Integration.Checks.Wanda.Behaviour
)

Application.put_env(:trento, Trento.Integration.Checks.Wanda,
policy: Trento.Integration.Checks.Wanda.Policy.Mock
)

Mox.defmock(GenRMQ.Processor.Mock, for: GenRMQ.Processor)

Mox.defmock(Trento.Support.DateService.Mock, for: Trento.Support.DateService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,31 @@ defmodule Trento.Integration.Checks.Wanda.Messaging.AMQP.ProcessorTest do

alias Trento.Checks.V1.ExecutionCompleted
alias Trento.Contracts
alias Trento.Domain.Commands.CompleteChecksExecutionWanda

require Trento.Domain.Enums.Health, as: Health

describe "process" do
test "should process valid event and dispatch command" do
test "should process ExecutionCompleted and dispatch command" do
execution_id = UUID.uuid4()
group_id = UUID.uuid4()

execution_completed =
Contracts.to_event(%ExecutionCompleted{
execution_id: UUID.uuid4(),
group_id: UUID.uuid4(),
execution_id: execution_id,
group_id: group_id,
result: :PASSING
})

message = %GenRMQ.Message{payload: execution_completed, attributes: %{}, channel: nil}
command = "some-command"
opts = [correlation_id: UUID.uuid4()]

expect(Trento.Integration.Checks.Wanda.Policy.Mock, :handle, fn _ ->
{:ok, command, opts}
end)
expect(Trento.Commanded.Mock, :dispatch, fn command, opts ->
assert %CompleteChecksExecutionWanda{
cluster_id: ^group_id,
health: Health.passing()
} = command

expect(Trento.Commanded.Mock, :dispatch, fn expected_command, expected_opts ->
assert ^expected_command = command
assert ^expected_opts = opts
assert [correlation_id: ^execution_id] = opts
:ok
end)

Expand All @@ -40,17 +44,13 @@ defmodule Trento.Integration.Checks.Wanda.Messaging.AMQP.ProcessorTest do
execution_completed =
Contracts.to_event(%ExecutionCompleted{
execution_id: UUID.uuid4(),
group_id: UUID.uuid4(),
group_id: "invalid-id",
result: :PASSING
})

message = %GenRMQ.Message{payload: execution_completed, attributes: %{}, channel: nil}

expect(Trento.Integration.Checks.Wanda.Policy.Mock, :handle, fn _ ->
{:error, :handling_error}
end)

assert {:error, :handling_error} = Processor.process(message)
assert {:error, %{cluster_id: ["is invalid"]}} = Processor.process(message)
end

@tag capture_log: true
Expand Down

This file was deleted.

0 comments on commit a6b3b2f

Please sign in to comment.