diff --git a/lib/ex_chat/application.ex b/lib/ex_chat/application.ex index 100e59a..5d703e1 100644 --- a/lib/ex_chat/application.ex +++ b/lib/ex_chat/application.ex @@ -12,9 +12,9 @@ defmodule ExChat.Application do def init(:ok) do children = [ - {Registry, keys: :unique, name: ExChat.ChatRoomRegistry}, + {Registry, keys: :unique, name: ExChat.RoomRegistry}, {Registry, keys: :unique, name: ExChat.UserSessionRegistry}, - ExChat.ChatRooms, + ExChat.Rooms, ExChat.UserSessions, ExChat.AccessTokenRepository, ExChat.Setup, diff --git a/lib/ex_chat/chat_room.ex b/lib/ex_chat/room.ex similarity index 78% rename from lib/ex_chat/chat_room.ex rename to lib/ex_chat/room.ex index bde6f6e..7867feb 100644 --- a/lib/ex_chat/chat_room.ex +++ b/lib/ex_chat/room.ex @@ -1,15 +1,15 @@ -defmodule ExChat.ChatRoom do +defmodule ExChat.Room do use GenServer alias ExChat.UserSessions defstruct session_ids: [], name: nil - def create(name = {:via, Registry, {_registry_name, chatroom_name}}) do - GenServer.start_link(__MODULE__, %__MODULE__{name: chatroom_name}, name: name) + def create(name = {:via, Registry, {_registry_name, room_name}}) do + GenServer.start_link(__MODULE__, %__MODULE__{name: room_name}, name: name) end - def create(chatroom_name) do - GenServer.start_link(__MODULE__, %__MODULE__{name: chatroom_name}, name: String.to_atom(chatroom_name)) + def create(room_name) do + GenServer.start_link(__MODULE__, %__MODULE__{name: room_name}, name: String.to_atom(room_name)) end def start_link(name), do: create(name) diff --git a/lib/ex_chat/chat_rooms.ex b/lib/ex_chat/rooms.ex similarity index 62% rename from lib/ex_chat/chat_rooms.ex rename to lib/ex_chat/rooms.ex index 9c4cd06..6f66d0a 100644 --- a/lib/ex_chat/chat_rooms.ex +++ b/lib/ex_chat/rooms.ex @@ -1,7 +1,7 @@ -defmodule ExChat.ChatRooms do +defmodule ExChat.Rooms do use DynamicSupervisor - alias ExChat.{ChatRoom, ChatRoomRegistry} + alias ExChat.{Room, RoomRegistry} ############## # Client API # @@ -20,7 +20,7 @@ defmodule ExChat.ChatRooms do def join(room, [as: session_id]) do case find(room) do {:ok, pid} -> - try_join_chatroom(pid, session_id) + try_join_room(pid, session_id) {:error, :unexisting_room} -> {:error, :unexisting_room} end @@ -28,13 +28,13 @@ defmodule ExChat.ChatRooms do def send(message, [to: room, as: session_id]) do case find(room) do - {:ok, pid} -> ChatRoom.send(pid, message, as: session_id) + {:ok, pid} -> Room.send(pid, message, as: session_id) error -> error end end - defp try_join_chatroom(chatroom_pid, session_id) do - case ChatRoom.join(chatroom_pid, session_id) do + defp try_join_room(room_pid, session_id) do + case Room.join(room_pid, session_id) do :ok -> :ok {:error, :already_joined} -> @@ -43,7 +43,7 @@ defmodule ExChat.ChatRooms do end defp find(room) do - case Registry.lookup(ChatRoomRegistry, room) do + case Registry.lookup(RoomRegistry, room) do [] -> {:error, :unexisting_room} [{pid, nil}] -> {:ok, pid} end @@ -54,16 +54,16 @@ defmodule ExChat.ChatRooms do #################### def start_link(_opts) do - DynamicSupervisor.start_link(__MODULE__, [], name: :chatroom_supervisor) + DynamicSupervisor.start_link(__MODULE__, [], name: :room_supervisor) end def init(_) do DynamicSupervisor.init(strategy: :one_for_one) end - defp start(chatroom_name) do - name = {:via, Registry, {ChatRoomRegistry, chatroom_name}} + defp start(room_name) do + name = {:via, Registry, {RoomRegistry, room_name}} - DynamicSupervisor.start_child(:chatroom_supervisor, {ChatRoom, name}) + DynamicSupervisor.start_child(:room_supervisor, {Room, name}) end end diff --git a/lib/ex_chat/setup.ex b/lib/ex_chat/setup.ex index dcc79a5..b75b075 100644 --- a/lib/ex_chat/setup.ex +++ b/lib/ex_chat/setup.ex @@ -1,13 +1,13 @@ defmodule ExChat.Setup do use Task, restart: :transient - alias ExChat.ChatRooms + alias ExChat.Rooms def start_link(_args) do Task.start_link(__MODULE__, :run, []) end def run() do - ChatRooms.create("default") + Rooms.create("default") end end diff --git a/lib/ex_chat/use_cases/create_chat_room.ex b/lib/ex_chat/use_cases/create_room.ex similarity index 62% rename from lib/ex_chat/use_cases/create_chat_room.ex rename to lib/ex_chat/use_cases/create_room.ex index b1a312b..6cbaebe 100644 --- a/lib/ex_chat/use_cases/create_chat_room.ex +++ b/lib/ex_chat/use_cases/create_room.ex @@ -1,9 +1,9 @@ -defmodule ExChat.UseCases.CreateChatRoom do +defmodule ExChat.UseCases.CreateRoom do - alias ExChat.ChatRooms + alias ExChat.Rooms def on(room) do - case ChatRooms.create(room) do + case Rooms.create(room) do :ok -> {:ok, "#{room} has been created!"} {:error, :already_exists} -> diff --git a/lib/ex_chat/use_cases/join_chat_room.ex b/lib/ex_chat/use_cases/join_room.ex similarity index 73% rename from lib/ex_chat/use_cases/join_chat_room.ex rename to lib/ex_chat/use_cases/join_room.ex index 713d744..f9727cf 100644 --- a/lib/ex_chat/use_cases/join_chat_room.ex +++ b/lib/ex_chat/use_cases/join_room.ex @@ -1,9 +1,9 @@ -defmodule ExChat.UseCases.JoinChatRoom do +defmodule ExChat.UseCases.JoinRoom do - alias ExChat.{ChatRooms, UserSessions} + alias ExChat.{Rooms, UserSessions} def on(room, user_id) do - case ChatRooms.join(room, as: user_id) do + case Rooms.join(room, as: user_id) do :ok -> UserSessions.notify(%{room: room, message: "welcome to the #{room} chat room, #{user_id}!"}, to: user_id) :ok diff --git a/lib/ex_chat/use_cases/send_message_to_chat_room.ex b/lib/ex_chat/use_cases/send_message_to_room.ex similarity index 54% rename from lib/ex_chat/use_cases/send_message_to_chat_room.ex rename to lib/ex_chat/use_cases/send_message_to_room.ex index 0d69348..26419d3 100644 --- a/lib/ex_chat/use_cases/send_message_to_chat_room.ex +++ b/lib/ex_chat/use_cases/send_message_to_room.ex @@ -1,9 +1,9 @@ -defmodule ExChat.UseCases.SendMessageToChatRoom do +defmodule ExChat.UseCases.SendMessageToRoom do - alias ExChat.ChatRooms + alias ExChat.Rooms def on(message, room, user_id) do - case ChatRooms.send(message, to: room, as: user_id) do + case Rooms.send(message, to: room, as: user_id) do :ok -> :ok {:error, :unexisting_room} -> diff --git a/lib/ex_chat/web/websocket_controller.ex b/lib/ex_chat/web/websocket_controller.ex index cf1a795..db1300e 100644 --- a/lib/ex_chat/web/websocket_controller.ex +++ b/lib/ex_chat/web/websocket_controller.ex @@ -3,8 +3,8 @@ defmodule ExChat.Web.WebSocketController do @behaviour :cowboy_websocket end - alias ExChat.UseCases.{ValidateAccessToken, SendMessageToChatRoom, - CreateChatRoom, JoinChatRoom, SubscribeToUserSession} + alias ExChat.UseCases.{ValidateAccessToken, SendMessageToRoom, + CreateRoom, JoinRoom, SubscribeToUserSession} @default_ping_interval 30_000 @default_idle_timeout 60_000 @@ -48,7 +48,7 @@ defmodule ExChat.Web.WebSocketController do end defp handle(%{"command" => "join", "room" => room}, session_id) do - case JoinChatRoom.on(room, session_id) do + case JoinRoom.on(room, session_id) do :ok -> {:ok, session_id} {:error, message} -> @@ -61,7 +61,7 @@ defmodule ExChat.Web.WebSocketController do end defp handle(%{"room" => room, "message" => message}, session_id) do - case SendMessageToChatRoom.on(message, room, session_id) do + case SendMessageToRoom.on(message, room, session_id) do {:error, message} -> {:reply, {:text, to_json(%{ error: message })}, session_id} :ok -> @@ -70,7 +70,7 @@ defmodule ExChat.Web.WebSocketController do end defp handle(%{"command" => "create", "room" => room}, session_id) do - response = case CreateChatRoom.on(room) do + response = case CreateRoom.on(room) do {:ok, message} -> %{success: message} {:error, message} -> %{error: message} end diff --git a/test/ex_chat/chat_room_test.exs b/test/ex_chat/room_test.exs similarity index 67% rename from test/ex_chat/chat_room_test.exs rename to test/ex_chat/room_test.exs index 5a4354c..9ae4780 100644 --- a/test/ex_chat/chat_room_test.exs +++ b/test/ex_chat/room_test.exs @@ -1,14 +1,14 @@ -defmodule ExChat.ChatRoomTest do +defmodule ExChat.RoomTest do use ExUnit.Case, async: false import Mock alias ExChat.UserSessions - alias ExChat.ChatRoom + alias ExChat.Room test "notify subscribed user session when message is received" do - {:ok, chatroom} = ChatRoom.create("room_name") - ChatRoom.join(chatroom, "a-user-session-id") + {:ok, room} = Room.create("room_name") + Room.join(room, "a-user-session-id") with_mock UserSessions, [notify: fn(_message, [to: _user_session_id]) -> :ok end] do expected_message = %{ @@ -17,7 +17,7 @@ defmodule ExChat.ChatRoomTest do message: "a message" } - ChatRoom.send(chatroom, "a message", as: "another-user-session-id") + Room.send(room, "a message", as: "another-user-session-id") assert called UserSessions.notify(expected_message, to: "a-user-session-id") end diff --git a/test/ex_chat/use_cases/create_chat_room_test.exs b/test/ex_chat/use_cases/create_chat_room_test.exs deleted file mode 100644 index 1fa303f..0000000 --- a/test/ex_chat/use_cases/create_chat_room_test.exs +++ /dev/null @@ -1,26 +0,0 @@ -defmodule ExChat.UseCases.CreateChatRoomTest do - use ExUnit.Case, async: true - - import Mock - - alias ExChat.ChatRooms - alias ExChat.UseCases.CreateChatRoom - - test "return an error message when the room already exists" do - with_mock(ChatRooms, create: fn(_) -> {:error, :already_exists} end) do - result = CreateChatRoom.on("a room") - - assert result == {:error, "a room already exists"} - assert called ChatRooms.create("a room") - end - end - - test "return an successful message when create a room" do - with_mock(ChatRooms, create: fn(_) -> :ok end) do - result = CreateChatRoom.on("a room") - - assert result == {:ok, "a room has been created!"} - assert called ChatRooms.create("a room") - end - end -end \ No newline at end of file diff --git a/test/ex_chat/use_cases/create_room_test.exs b/test/ex_chat/use_cases/create_room_test.exs new file mode 100644 index 0000000..8f3846c --- /dev/null +++ b/test/ex_chat/use_cases/create_room_test.exs @@ -0,0 +1,26 @@ +defmodule ExChat.UseCases.CreateRoomTest do + use ExUnit.Case, async: true + + import Mock + + alias ExChat.Rooms + alias ExChat.UseCases.CreateRoom + + test "return an error message when the room already exists" do + with_mock(Rooms, create: fn(_) -> {:error, :already_exists} end) do + result = CreateRoom.on("a room") + + assert result == {:error, "a room already exists"} + assert called Rooms.create("a room") + end + end + + test "return an successful message when create a room" do + with_mock(Rooms, create: fn(_) -> :ok end) do + result = CreateRoom.on("a room") + + assert result == {:ok, "a room has been created!"} + assert called Rooms.create("a room") + end + end +end \ No newline at end of file diff --git a/test/ex_chat/use_cases/join_chat_room_test.exs b/test/ex_chat/use_cases/join_room_test.exs similarity index 69% rename from test/ex_chat/use_cases/join_chat_room_test.exs rename to test/ex_chat/use_cases/join_room_test.exs index c3ef2b6..87363dd 100644 --- a/test/ex_chat/use_cases/join_chat_room_test.exs +++ b/test/ex_chat/use_cases/join_room_test.exs @@ -1,17 +1,17 @@ -defmodule ExChat.UseCases.JoinChatRoomTest do +defmodule ExChat.UseCases.JoinRoomTest do use ExUnit.Case, async: true import Mock - alias ExChat.{ChatRooms, UserSessions} - alias ExChat.UseCases.JoinChatRoom + alias ExChat.{Rooms, UserSessions} + alias ExChat.UseCases.JoinRoom test "return an error message when the chat room does not exists" do with_mocks([ - {ChatRooms, [], join: fn(_, _) -> {:error, :unexisting_room} end}, + {Rooms, [], join: fn(_, _) -> {:error, :unexisting_room} end}, {UserSessions, [], notify: fn(_, _) -> nil end} ]) do - result = JoinChatRoom.on("a room", "a user id") + result = JoinRoom.on("a room", "a user id") assert result == {:error, "a room does not exists"} refute called UserSessions.notify(%{room: "a room", message: "welcome to the a room chat room, a user id!"}, to: "a user id") @@ -20,10 +20,10 @@ defmodule ExChat.UseCases.JoinChatRoomTest do test "return an error message when already joined the chat room" do with_mocks([ - {ChatRooms, [], join: fn(_, _) -> {:error, :already_joined} end}, + {Rooms, [], join: fn(_, _) -> {:error, :already_joined} end}, {UserSessions, [], notify: fn(_, _) -> nil end} ]) do - result = JoinChatRoom.on("a room", "a user id") + result = JoinRoom.on("a room", "a user id") assert result == {:error, "you already joined the a room room!"} refute called UserSessions.notify(%{room: "a room", message: "welcome to the a room chat room, a user id!"}, to: "a user id") @@ -32,10 +32,10 @@ defmodule ExChat.UseCases.JoinChatRoomTest do test "notifies user sessions when joining a chat room" do with_mocks([ - {ChatRooms, [], join: fn(_, _) -> :ok end}, + {Rooms, [], join: fn(_, _) -> :ok end}, {UserSessions, [], notify: fn(_, _) -> nil end} ]) do - result = JoinChatRoom.on("a room", "a user id") + result = JoinRoom.on("a room", "a user id") assert result == :ok assert called UserSessions.notify(%{room: "a room", message: "welcome to the a room chat room, a user id!"}, to: "a user id") diff --git a/test/ex_chat/use_cases/send_message_to_chat_room_test.exs b/test/ex_chat/use_cases/send_message_to_chat_room_test.exs deleted file mode 100644 index 6305349..0000000 --- a/test/ex_chat/use_cases/send_message_to_chat_room_test.exs +++ /dev/null @@ -1,26 +0,0 @@ -defmodule ExChat.UseCases.SendMessageToChatRoomTest do - use ExUnit.Case, async: true - - import Mock - - alias ExChat.ChatRooms - alias ExChat.UseCases.SendMessageToChatRoom - - test "return an error message when the room does not exists" do - with_mock(ChatRooms, send: fn(_, _) -> {:error, :unexisting_room} end) do - result = SendMessageToChatRoom.on("a message", "a room", "a user id") - - assert result == {:error, "a room does not exists"} - assert called ChatRooms.send("a message", to: "a room", as: "a user id") - end - end - - test "return ok when send a message" do - with_mock(ChatRooms, send: fn(_, _) -> :ok end) do - result = SendMessageToChatRoom.on("a message", "a room", "a user id") - - assert result == :ok - assert called ChatRooms.send("a message", to: "a room", as: "a user id") - end - end -end \ No newline at end of file diff --git a/test/ex_chat/use_cases/send_message_to_room_test.exs b/test/ex_chat/use_cases/send_message_to_room_test.exs new file mode 100644 index 0000000..e42fe09 --- /dev/null +++ b/test/ex_chat/use_cases/send_message_to_room_test.exs @@ -0,0 +1,26 @@ +defmodule ExChat.UseCases.SendMessageToRoomTest do + use ExUnit.Case, async: true + + import Mock + + alias ExChat.Rooms + alias ExChat.UseCases.SendMessageToRoom + + test "return an error message when the room does not exists" do + with_mock(Rooms, send: fn(_, _) -> {:error, :unexisting_room} end) do + result = SendMessageToRoom.on("a message", "a room", "a user id") + + assert result == {:error, "a room does not exists"} + assert called Rooms.send("a message", to: "a room", as: "a user id") + end + end + + test "return ok when send a message" do + with_mock(Rooms, send: fn(_, _) -> :ok end) do + result = SendMessageToRoom.on("a message", "a room", "a user id") + + assert result == :ok + assert called Rooms.send("a message", to: "a room", as: "a user id") + end + end +end \ No newline at end of file diff --git a/test/ex_chat/web/websocket_acceptance_test.exs b/test/ex_chat/web/websocket_acceptance_test.exs index 4b457a9..073ee1d 100644 --- a/test/ex_chat/web/websocket_acceptance_test.exs +++ b/test/ex_chat/web/websocket_acceptance_test.exs @@ -88,10 +88,10 @@ defmodule ExChat.Web.WebSocketAcceptanceTest do setup :connect_as_a_user test "I receive an error message if the room already exist", %{client: client} do - send_as_text(client, "{\"command\":\"create\",\"room\":\"a_chat_room\"}") - send_as_text(client, "{\"command\":\"create\",\"room\":\"a_chat_room\"}") + send_as_text(client, "{\"command\":\"create\",\"room\":\"a_room\"}") + send_as_text(client, "{\"command\":\"create\",\"room\":\"a_room\"}") - assert_receive "{\"error\":\"a_chat_room already exists\"}" + assert_receive "{\"error\":\"a_room already exists\"}" end test "I receive a successful message", %{client: client} do @@ -105,10 +105,10 @@ defmodule ExChat.Web.WebSocketAcceptanceTest do setup :connect_as_a_user test "I want to receive a welcome message that contain my name and the chat room name", %{client: client} do - send_as_text(client, "{\"command\":\"create\",\"room\":\"a_chat_room\"}") - send_as_text(client, "{\"command\":\"join\",\"room\":\"a_chat_room\"}") + send_as_text(client, "{\"command\":\"create\",\"room\":\"a_room\"}") + send_as_text(client, "{\"command\":\"join\",\"room\":\"a_room\"}") - assert_receive "{\"room\":\"a_chat_room\",\"message\":\"welcome to the a_chat_room chat room, a-user!\"}" + assert_receive "{\"room\":\"a_room\",\"message\":\"welcome to the a_room chat room, a-user!\"}" end end