diff --git a/config/prod.exs b/config/prod.exs index 11476f9..398d584 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -26,8 +26,8 @@ config :poa_agent, identifier: "elixirNodeJSIntegration", # Authentication parameters - user: "rUN7afCO", - password: "_3IC09xfMtAW4Hr", + user: "AhvK0DSj", + password: "EkiuUkyOD6KLas8", token_url: "https://localhost:4003/session" ] } @@ -41,5 +41,5 @@ config :poa_agent, {:eth_stats, [:rest_transfer]}, {:eth_pending, [:rest_transfer]}, {:eth_information, [:rest_transfer]}, - {:system_collector, []} + {:system_collector, [:rest_transfer]} ] diff --git a/lib/poa_agent/configuration.ex b/lib/poa_agent/configuration.ex index bcae8de..38d3a91 100644 --- a/lib/poa_agent/configuration.ex +++ b/lib/poa_agent/configuration.ex @@ -24,21 +24,21 @@ defmodule POAAgent.Configuration do defp config(overlay, default) do overlay |> Kernel.get_in(["POAAgent", "transfers"]) - |> Kernel.hd() |> merge_overlay_into_config(default) end - defp merge_overlay_into_config(overlay, [{id, module_name, default}]) do - keys = [ - "address", - "identifier", - "secret" - ] - ^id = String.to_existing_atom(Map.fetch!(overlay, "id")) - restricted = Map.take(overlay, keys) - want = POAAgent.Configuration.to_keyword(restricted) - final = Keyword.merge(default, want) - [{id, module_name, final}] + defp merge_overlay_into_config(overlay, default) do + Enum.flat_map(default, + fn {id, module_name, args} -> + case POAAgent.Configuration.search(overlay, Atom.to_string(id)) do + [] -> + [] + overlay_args -> + overlay_args = POAAgent.Configuration.to_keyword(overlay_args) + final = Keyword.merge(args, overlay_args) + [{id, module_name, final}] + end + end) end end @@ -65,28 +65,35 @@ defmodule POAAgent.Configuration do defp config(overlay, default) do overlay |> Kernel.get_in(["POAAgent", "collectors"]) - |> Kernel.hd() |> merge_overlay_into_config(default) end - defp merge_overlay_into_config(overlay, [{id, module_name, freq, tag, default} | rest]) do - keys = [ - "url", - "name", - "contact" - ] - ^id = String.to_existing_atom(Map.fetch!(overlay, "id")) - restricted = Map.take(overlay, keys) - want = POAAgent.Configuration.to_keyword(restricted) - final = Keyword.merge(default, want) - [{id, module_name, freq, tag, final}] ++ rest + defp merge_overlay_into_config(overlay, default) do + Enum.flat_map(default, + fn {id, module_name, freq, tag, args} -> + case POAAgent.Configuration.search(overlay, Atom.to_string(id)) do + [] -> + [] + overlay_args -> + overlay_args = POAAgent.Configuration.to_keyword(overlay_args) + final = Keyword.merge(args, overlay_args) + [{id, module_name, freq, tag, final}] + end + end) end end + def to_keyword(nil), do: [] def to_keyword(x) do f = fn {k, v} -> - {String.to_existing_atom(k), v} + {String.to_atom(k), v} end Enum.into(x, [], f) end + + def search(list, key) do + Enum.find(list, fn(map) -> + Map.get(map, "id") == key + end) + end end diff --git a/test/poa_agent/config_overlay.json b/test/poa_agent/config_overlay.json index 80a3c7d..2cb80af 100644 --- a/test/poa_agent/config_overlay.json +++ b/test/poa_agent/config_overlay.json @@ -1,6 +1,10 @@ { "POAAgent":{ "collectors":[ + { + "id": "other_collector", + "myfield": "myvalue" + }, { "id": "eth_information", "url": "http://localhost:8546", @@ -9,11 +13,17 @@ } ], "transfers":[ + { + "id": "transfer1", + "somefield": "somevalue", + "somefield2": "somevalue2" + }, { "id": "rest_transfer", "address": "http://localhost:4003", "identifier": "NewIdentifier", - "secret": "newsecret" + "user": "user1", + "password": "password1" } ] } diff --git a/test/poa_agent/configuration_test.exs b/test/poa_agent/configuration_test.exs index 3349cdd..37863b2 100644 --- a/test/poa_agent/configuration_test.exs +++ b/test/poa_agent/configuration_test.exs @@ -6,11 +6,13 @@ defmodule POAAgent.ConfigurationTest do transfers = [ {:rest_transfer, POAAgent.Plugins.Transfers.HTTP.REST, [ - address: "http://localhost:4002", - identifier: "elixirNodeJSIntegration", - secret: "mysecret" - ] - } + address: "http://localhost:4002", + identifier: "elixirNodeJSIntegration", + user: "AhvK0DSj", + password: "EkiuUkyOD6KLas8", + token_url: "https://localhost:4003/session" + ] + } ] :ok = Application.put_env(:poa_agent, :transfers, transfers) @@ -20,13 +22,59 @@ defmodule POAAgent.ConfigurationTest do # those values are in the test/poa_agent/config_overlay.json file new_address = "http://localhost:4003" new_identifier = "NewIdentifier" - new_secret = "newsecret" + new_user = "user1" + new_password = "password1" [{:rest_transfer, POAAgent.Plugins.Transfers.HTTP.REST, new_args}] = POAAgent.Configuration.Transfers.config() assert new_address == new_args[:address] assert new_identifier == new_args[:identifier] - assert new_secret == new_args[:secret] + assert new_user == new_args[:user] + assert new_password == new_args[:password] + assert "https://localhost:4003/session" == new_args[:token_url] + + # putting back the original value + :ok = Application.put_env(:poa_agent, :transfers, original_transfers) + + assert original_transfers == Application.fetch_env!(:poa_agent, :transfers) + end + + test "Transfers overlay doesn't exist in default" do + original_transfers = Application.fetch_env!(:poa_agent, :transfers) + + transfers = [ + {:rest_transfer2, POAAgent.Plugins.Transfers.HTTP.REST, [ + address: "http://localhost:4002", + identifier: "elixirNodeJSIntegration", + user: "AhvK0DSj", + password: "EkiuUkyOD6KLas8", + token_url: "https://localhost:4003/session" + ] + } + ] + + :ok = Application.put_env(:poa_agent, :transfers, transfers) + + assert transfers == Application.fetch_env!(:poa_agent, :transfers) + + assert transfers == POAAgent.Configuration.Transfers.config() + + # putting back the original value + :ok = Application.put_env(:poa_agent, :transfers, original_transfers) + + assert original_transfers == Application.fetch_env!(:poa_agent, :transfers) + end + + test "Transfers overlay, default is an empty list" do + original_transfers = Application.fetch_env!(:poa_agent, :transfers) + + transfers = [] + + :ok = Application.put_env(:poa_agent, :transfers, transfers) + + assert transfers == Application.fetch_env!(:poa_agent, :transfers) + + assert transfers == POAAgent.Configuration.Transfers.config() # putting back the original value :ok = Application.put_env(:poa_agent, :transfers, original_transfers) @@ -50,7 +98,7 @@ defmodule POAAgent.ConfigurationTest do assert collectors == Application.fetch_env!(:poa_agent, :collectors) - # # those values are in the test/poa_agent/config_overlay.json file + # those values are in the test/poa_agent/config_overlay.json file new_url = "http://localhost:8546" new_name = "NewNodeName" new_contact = "mynewemail@gmail.com" @@ -89,7 +137,7 @@ defmodule POAAgent.ConfigurationTest do assert collectors == Application.fetch_env!(:poa_agent, :collectors) - # # those values are in the test/poa_agent/config_overlay.json file + # those values are in the test/poa_agent/config_overlay.json file new_url = "http://localhost:8546" new_name = "NewNodeName" new_contact = "mynewemail@gmail.com" @@ -105,4 +153,59 @@ defmodule POAAgent.ConfigurationTest do assert original_collectors == Application.fetch_env!(:poa_agent, :collectors) end + + test "test coverage for collectors" do + original_collectors = Application.fetch_env!(:poa_agent, :collectors) + original_config_overlay = Application.fetch_env!(:poa_agent, :config_overlay) + + collectors = [ + {:eth_information, POAAgent.Plugins.Collectors.Eth.Information, 60_000, :eth_information, [ + url: "http://localhost:8545", + name: "Elixir-NodeJS-Integration", + contact: "myemail@gmail.com" + ] + }, + {:other_collector, POAAgent.Plugins.Collectors.Eth.Information, 60_000, :eth_information, [ + url: "http://localhost:8545", + name: "Elixir-NodeJS-Integration", + contact: "myemail@gmail.com" + ] + } + ] + + :ok = Application.put_env(:poa_agent, :collectors, collectors) + + assert collectors == Application.fetch_env!(:poa_agent, :collectors) + + :ok = Application.delete_env(:poa_agent, :config_overlay) + + assert collectors == POAAgent.Configuration.Collectors.config() + + # putting back the original value + :ok = Application.put_env(:poa_agent, :collectors, original_collectors) + :ok = Application.put_env(:poa_agent, :config_overlay, original_config_overlay) + + assert original_collectors == Application.fetch_env!(:poa_agent, :collectors) + end + + test "test coverage for transfers" do + original_transfers = Application.fetch_env!(:poa_agent, :transfers) + original_config_overlay = Application.fetch_env!(:poa_agent, :config_overlay) + + transfers = [] + + :ok = Application.put_env(:poa_agent, :transfers, transfers) + + assert transfers == Application.fetch_env!(:poa_agent, :transfers) + + :ok = Application.delete_env(:poa_agent, :config_overlay) + + assert transfers == POAAgent.Configuration.Transfers.config() + + # putting back the original value + :ok = Application.put_env(:poa_agent, :transfers, original_transfers) + + assert original_transfers == Application.fetch_env!(:poa_agent, :transfers) + :ok = Application.put_env(:poa_agent, :config_overlay, original_config_overlay) + end end