-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add messaging method to retrieve distinct origin_host from topics * Add rescuer client and adapter to list undelivered messages * Implement delete method on rescuer client
- Loading branch information
Showing
7 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule Postoffice.Rescuer.Adapters.Http do | ||
require Logger | ||
|
||
@behaviour Postoffice.Rescuer.Adapters.Impl | ||
|
||
@impl true | ||
def list(host) do | ||
Logger.info("Listing undelivered messages from #{host}") | ||
HTTPoison.get(host) | ||
end | ||
|
||
@impl true | ||
def delete(host, message_id) do | ||
build_message_path(host, message_id) | ||
|> HTTPoison.delete() | ||
end | ||
|
||
defp build_message_path(host, message_id) do | ||
host <> message_id <> "/" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
defmodule Postoffice.Rescuer.Adapters.Impl do | ||
@moduledoc false | ||
|
||
@callback list(host :: String) :: {:ok, list} | {:error, reason :: String} | ||
@callback delete(host :: String, message_id :: Integer) :: {:ok, status :: Atom } | {:error, reason :: String} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule Postoffice.Rescuer.Client do | ||
require Logger | ||
|
||
alias Postoffice.Rescuer.Adapters.Http | ||
|
||
def list(host) do | ||
case impl().list(host) do | ||
{:ok, %HTTPoison.Response{status_code: status_code, body: body}} | ||
when status_code in 200..299 -> | ||
Logger.info("Succesfully listed pending messages from #{host}") | ||
{:ok, Poison.decode!(body)} | ||
|
||
{:ok, %HTTPoison.Response{status_code: status_code, body: body}} -> | ||
Logger.info( | ||
"Non successful response list pending messages from #{host} with status code: #{ | ||
status_code | ||
}" | ||
) | ||
|
||
{:error, []} | ||
|
||
{:error, %HTTPoison.Error{reason: reason}} -> | ||
Logger.info("Error trying to list pending messages #{reason}") | ||
{:error, []} | ||
end | ||
end | ||
|
||
def delete(host, message_id) do | ||
case impl().delete(host, message_id) do | ||
{:ok, %HTTPoison.Response{status_code: status_code}} | ||
when status_code in 200..299 -> | ||
Logger.info("Successfully deleted message #{message_id} from #{host}") | ||
{:ok, :deleted} | ||
{:ok, %HTTPoison.Response{status_code: status_code, body: body}} -> | ||
Logger.info( | ||
"Non successful response deleting message from #{host} with status code: #{ | ||
status_code | ||
}" | ||
) | ||
|
||
{:error, "Request status code #{status_code}"} | ||
|
||
{:error, %HTTPoison.Error{reason: reason}} -> | ||
Logger.info("Error trying to delete message #{host}: #{reason}") | ||
{:error, reason} | ||
end | ||
end | ||
|
||
defp impl do | ||
Application.get_env(:postoffice, :rescuer_client, Http) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
defmodule Postoffice.Rescuer.ClientTest do | ||
use ExUnit.Case | ||
|
||
import Mox | ||
|
||
alias Postoffice.Rescuer.Adapters.HttpMock | ||
alias Postoffice.Rescuer.Client | ||
|
||
@origin_host "http://fake_origin.host" | ||
@wrong_message_id 9999 | ||
@external_message_id 1 | ||
@one_message_response "[{\"id\": 1, \"topic\": \"test\", \"payload\": {\"products\": [{\"code\": \"1234\"}, {\"code\": 2345}], \"reference\": 1234}, \"attributes\": null}]" | ||
@two_messages_response "[{\"id\": 1, \"topic\": \"test\", \"payload\": {\"products\": [{\"code\": \"1234\"}, {\"code\": 2345}], \"reference\": 1234}, \"attributes\": null}, {\"id\": 2, \"topic\": \"test2\", \"payload\": {\"products\": [{\"code\": \"1234\"}, {\"code\": 2345}], \"reference\": 1234}, \"attributes\": null}]" | ||
|
||
setup [:set_mox_from_context, :verify_on_exit!] | ||
|
||
setup do | ||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Postoffice.Repo) | ||
end | ||
|
||
describe "list undelivered messages" do | ||
test "non successful response from origin host" do | ||
expect(HttpMock, :list, fn @origin_host -> | ||
{:ok, %HTTPoison.Response{status_code: 300}} | ||
end) | ||
|
||
{:error, pending_messages} = Client.list(@origin_host) | ||
assert pending_messages == [] | ||
end | ||
|
||
test "origin host returns an error" do | ||
expect(HttpMock, :list, fn @origin_host -> | ||
{:error, %HTTPoison.Error{reason: "test error"}} | ||
end) | ||
|
||
{:error, pending_messages} = Client.list(@origin_host) | ||
assert pending_messages == [] | ||
end | ||
|
||
test "no pending messages returned" do | ||
expect(HttpMock, :list, fn @origin_host -> | ||
{:ok, %HTTPoison.Response{status_code: 200, body: "[]"}} | ||
end) | ||
|
||
{:ok, pending_messages} = Client.list(@origin_host) | ||
assert pending_messages == [] | ||
end | ||
|
||
test "one pending message received" do | ||
expect(HttpMock, :list, fn @origin_host -> | ||
{:ok, %HTTPoison.Response{status_code: 200, body: @one_message_response}} | ||
end) | ||
|
||
{:ok, pending_messages} = Client.list(@origin_host) | ||
assert Kernel.length(pending_messages) == 1 | ||
end | ||
|
||
test "two pending messages received" do | ||
expect(HttpMock, :list, fn @origin_host -> | ||
{:ok, %HTTPoison.Response{status_code: 200, body: @two_messages_response}} | ||
end) | ||
|
||
{:ok, pending_messages} = Client.list(@origin_host) | ||
assert Kernel.length(pending_messages) == 2 | ||
end | ||
end | ||
|
||
describe "delete undelivered messages" do | ||
test "trying to delete non existing message" do | ||
expect(HttpMock, :delete, fn @origin_host, @wrong_message_id -> | ||
{:ok, %HTTPoison.Response{status_code: 404, body: ""}} | ||
end) | ||
|
||
{:error, reason} = Client.delete(@origin_host, @wrong_message_id) | ||
assert reason == "Request status code 404" | ||
end | ||
|
||
test "errors are handled from our side" do | ||
expect(HttpMock, :delete, fn @origin_host, @wrong_message_id -> | ||
{:error, %HTTPoison.Error{reason: "Something weird happened"}} | ||
end) | ||
|
||
{:error, reason} = Client.delete(@origin_host, @wrong_message_id) | ||
assert reason == "Something weird happened" | ||
end | ||
|
||
test "messages are successfuly deleted" do | ||
expect(HttpMock, :delete, fn @origin_host, @external_message_id -> | ||
{:ok, %HTTPoison.Response{status_code: 204, body: ""}} | ||
end) | ||
|
||
{:ok, :deleted} = Client.delete(@origin_host, @external_message_id) | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters