From 1e5da4fb560488c89bb981238ac52395511ceb97 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 15 Oct 2019 20:47:18 +0200 Subject: [PATCH 01/40] Added Channel Object (#1) --- pom.xml | 2 +- .../microservice/chat/channel/Channel.java | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java diff --git a/pom.xml b/pom.xml index ac8f3f9..a2e4408 100644 --- a/pom.xml +++ b/pom.xml @@ -64,7 +64,7 @@ com.github.cryptic-game java-lib - master-SNAPSHOT + experimental-SNAPSHOT diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java b/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java new file mode 100644 index 0000000..dfa34aa --- /dev/null +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java @@ -0,0 +1,43 @@ +package net.cryptic_game.microservice.chat.channel; + +import net.cryptic_game.microservice.wrapper.User; + +import java.util.ArrayList; +import java.util.UUID; + +public class Channel { + + private final UUID uuid; + private String name; + private final ArrayList user; + + public Channel(final String name) { + this.uuid = UUID.randomUUID(); + this.name = name; + this.user = new ArrayList<>(); + } + + public UUID getUuid() { + return this.uuid; + } + + public String getName() { + return this.name; + } + + public void setName(final String name) { + this.name = name; + } + + public ArrayList getUser() { + return this.user; + } + + public void addUser(final User user) { + this.user.add(user); + } + + public void removeUser(final User user) { + this.user.remove(user); + } +} From be63c75c4a1db8fc0f9806559f85986fd5691d42 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 15 Oct 2019 20:54:59 +0200 Subject: [PATCH 02/40] Added ChannelHandler Class (#1) --- .../chat/channel/ChannelHandler.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java new file mode 100644 index 0000000..4928e1a --- /dev/null +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java @@ -0,0 +1,28 @@ +package net.cryptic_game.microservice.chat.channel; + +import java.util.ArrayList; +import java.util.UUID; + +public class ChannelHandler { + + private final ArrayList channels; + + public ChannelHandler() { + this.channels = new ArrayList<>(); + } + + public Channel addChanel(final String name) { + final Channel channel = new Channel(name); + this.channels.add(channel); + return channel; + } + + public Channel getChannelByUUID(final UUID uuid) { + for (final Channel channel : this.channels) if (channel.getUuid().equals(uuid)) return channel; + return null; + } + + public ArrayList getChannels() { + return this.channels; + } +} From 18d35f5f8b7028768408431c304a5e61a59b948c Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 15 Oct 2019 21:21:15 +0200 Subject: [PATCH 03/40] Added Channel ChannelEndpoints (#1) --- .../cryptic_game/microservice/chat/App.java | 24 +++-- .../chat/endpoint/ChannelEndpoints.java | 99 +++++++++++++++++++ 2 files changed, 114 insertions(+), 9 deletions(-) create mode 100644 src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java diff --git a/src/main/java/net/cryptic_game/microservice/chat/App.java b/src/main/java/net/cryptic_game/microservice/chat/App.java index 8771701..200dd50 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/App.java +++ b/src/main/java/net/cryptic_game/microservice/chat/App.java @@ -1,19 +1,25 @@ package net.cryptic_game.microservice.chat; -import org.apache.log4j.BasicConfigurator; - import net.cryptic_game.microservice.MicroService; +import net.cryptic_game.microservice.chat.channel.ChannelHandler; +import org.apache.log4j.BasicConfigurator; public class App extends MicroService { - public App() { - super("chat"); - } + private final ChannelHandler channelHandler; + + public App() { + super("chat"); - public static void main(String[] args) { - BasicConfigurator.configure(); + this.channelHandler = new ChannelHandler(); + } - new App(); - } + public static void main(String[] args) { + BasicConfigurator.configure(); + new App(); + } + public ChannelHandler getChannelHandler() { + return this.channelHandler; + } } diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java new file mode 100644 index 0000000..5c42ebc --- /dev/null +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java @@ -0,0 +1,99 @@ +package net.cryptic_game.microservice.chat.endpoint; + +import net.cryptic_game.microservice.MicroService; +import net.cryptic_game.microservice.chat.App; +import net.cryptic_game.microservice.chat.channel.Channel; +import net.cryptic_game.microservice.chat.channel.ChannelHandler; +import net.cryptic_game.microservice.endpoint.UserEndpoint; +import net.cryptic_game.microservice.utils.JSON; +import net.cryptic_game.microservice.utils.JSONBuilder; +import net.cryptic_game.microservice.wrapper.User; +import org.json.simple.JSONObject; + +import java.util.ArrayList; +import java.util.UUID; + +import static net.cryptic_game.microservice.utils.JSONBuilder.anJSON; +import static net.cryptic_game.microservice.utils.JSONBuilder.simple; + +public class ChannelEndpoints { + + @UserEndpoint(path = {"channel", "list"}, keys = {}, types = {}) + public static JSONObject getChannel(final JSON data, final UUID uuid) { + final ChannelHandler channelHandler = ((App) App.getInstance()).getChannelHandler(); + + final ArrayList channelsJson = new ArrayList<>(); + + for (final Channel channel : channelHandler.getChannels()) { + final JSONBuilder jsonBuilder = anJSON(); + jsonBuilder.add("uuid", channel.getUuid()); + jsonBuilder.add("name", channel.getName()); + channelsJson.add(jsonBuilder); + } + + return simple("channel", channelsJson); + } + + @UserEndpoint(path = {"channel", "members"}, keys = {"channel"}, types = {UUID.class}) + public static JSONObject getChannelMembers(final JSON data, final UUID uuid) { + final ChannelHandler channelHandler = ((App) App.getInstance()).getChannelHandler(); + + final Channel channel = channelHandler.getChannelByUUID(data.getUUID("channel")); + + if (channel == null) { + return simple("error", "Channel with the UUID \"" + data.getUUID("channel").toString() + "\" can't be found."); + } + + final ArrayList userJson = new ArrayList<>(); + + for (final User user : channel.getUser()) { + final JSONBuilder jsonBuilder = anJSON(); + jsonBuilder.add("uuid", user.getUUID()); + jsonBuilder.add("name", user.getName()); + jsonBuilder.add("mail", user.getMail()); + jsonBuilder.add("created", user.getCreated()); + jsonBuilder.add("last", user.getLast()); + userJson.add(jsonBuilder.build()); + } + + return simple("user", userJson); + } + + @UserEndpoint(path = {"channel", "join"}, keys = {"channel"}, types = {UUID.class}) + public static JSONObject joinChannel(final JSON data, final UUID uuid) { + final ChannelHandler channelHandler = ((App) App.getInstance()).getChannelHandler(); + + final User user = MicroService.getInstance().getUser(uuid); + final Channel channel = channelHandler.getChannelByUUID(data.getUUID("channel")); + + if (user == null) { + return simple("error", "User with the UUID \"" + uuid.toString() + "\" can't be found."); + } + if (channel == null) { + return simple("error", "Channel with the UUID \"" + data.getUUID("channel").toString() + "\" can't be found."); + } + + channelHandler.getChannelByUUID(data.getUUID("channel")).addUser(user); + + return simple("info", "Added User \"" + user.getUUID().toString() + "\" to channel \"" + channel.getUuid().toString() + "\"."); + } + + @UserEndpoint(path = {"channel", "leave"}, keys = {"channel"}, types = {UUID.class}) + public static JSONObject leaveChannel(final JSON data, final UUID uuid) { + final ChannelHandler channelHandler = ((App) App.getInstance()).getChannelHandler(); + + final User user = MicroService.getInstance().getUser(uuid); + final Channel channel = channelHandler.getChannelByUUID(data.getUUID("channel")); + + if (user == null) { + return simple("error", "User with the UUID \"" + uuid.toString() + "\" can't be found."); + } + if (channel == null) { + return simple("error", "Channel with the UUID \"" + data.getUUID("channel").toString() + "\" can't be found."); + } + + channelHandler.getChannelByUUID(data.getUUID("channel")).removeUser(user); + + return simple("info", "Removed User \"" + user.getUUID().toString() + "\" from channel \"" + channel.getUuid().toString() + "\"."); + } +} From 9b3a20315d9c9ce3b5df455b33235cf07f62d4fa Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 15 Oct 2019 22:08:09 +0200 Subject: [PATCH 04/40] Added User Notifications for Leave and Join Events. (#1) --- .../chat/channel/ChannelHandler.java | 18 +++++++++ .../microservice/chat/channel/ChatAction.java | 16 ++++++++ .../chat/endpoint/ChannelEndpoints.java | 37 +++++++++---------- 3 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 src/main/java/net/cryptic_game/microservice/chat/channel/ChatAction.java diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java index 4928e1a..17d87dd 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java @@ -1,8 +1,13 @@ package net.cryptic_game.microservice.chat.channel; +import net.cryptic_game.microservice.MicroService; +import org.json.simple.JSONObject; + import java.util.ArrayList; import java.util.UUID; +import static net.cryptic_game.microservice.utils.JSONBuilder.anJSON; + public class ChannelHandler { private final ArrayList channels; @@ -25,4 +30,17 @@ public Channel getChannelByUUID(final UUID uuid) { public ArrayList getChannels() { return this.channels; } + + public void notifyUsers(final ChatAction action, final Channel channel, final UUID target) { + final JSONObject json = anJSON() + .add("notify-id", "chat-update") + .add("origin", "chat") + .add("data", anJSON() + .add("action", action.getValue()) + .add("channel", channel) + .add("user", channel).build() + ).build(); + + channel.getUser().forEach(user -> MicroService.getInstance().sendToUser(target, json)); + } } diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/ChatAction.java b/src/main/java/net/cryptic_game/microservice/chat/channel/ChatAction.java new file mode 100644 index 0000000..90aa944 --- /dev/null +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/ChatAction.java @@ -0,0 +1,16 @@ +package net.cryptic_game.microservice.chat.channel; + +public enum ChatAction { + MEMBER_JOIN("member-join"), + MEMBER_LEAVE("member-leave"); + + final String value; + + ChatAction(final String value) { + this.value = value; + } + + public String getValue() { + return this.value; + } +} diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java index 5c42ebc..09b6b5f 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java @@ -4,13 +4,14 @@ import net.cryptic_game.microservice.chat.App; import net.cryptic_game.microservice.chat.channel.Channel; import net.cryptic_game.microservice.chat.channel.ChannelHandler; +import net.cryptic_game.microservice.chat.channel.ChatAction; import net.cryptic_game.microservice.endpoint.UserEndpoint; import net.cryptic_game.microservice.utils.JSON; -import net.cryptic_game.microservice.utils.JSONBuilder; import net.cryptic_game.microservice.wrapper.User; import org.json.simple.JSONObject; import java.util.ArrayList; +import java.util.List; import java.util.UUID; import static net.cryptic_game.microservice.utils.JSONBuilder.anJSON; @@ -22,13 +23,13 @@ public class ChannelEndpoints { public static JSONObject getChannel(final JSON data, final UUID uuid) { final ChannelHandler channelHandler = ((App) App.getInstance()).getChannelHandler(); - final ArrayList channelsJson = new ArrayList<>(); + final List channelsJson = new ArrayList<>(); for (final Channel channel : channelHandler.getChannels()) { - final JSONBuilder jsonBuilder = anJSON(); - jsonBuilder.add("uuid", channel.getUuid()); - jsonBuilder.add("name", channel.getName()); - channelsJson.add(jsonBuilder); + channelsJson.add(anJSON() + .add("uuid", channel.getUuid()) + .add("name", channel.getName()) + .build()); } return simple("channel", channelsJson); @@ -44,16 +45,10 @@ public static JSONObject getChannelMembers(final JSON data, final UUID uuid) { return simple("error", "Channel with the UUID \"" + data.getUUID("channel").toString() + "\" can't be found."); } - final ArrayList userJson = new ArrayList<>(); + final List userJson = new ArrayList<>(); for (final User user : channel.getUser()) { - final JSONBuilder jsonBuilder = anJSON(); - jsonBuilder.add("uuid", user.getUUID()); - jsonBuilder.add("name", user.getName()); - jsonBuilder.add("mail", user.getMail()); - jsonBuilder.add("created", user.getCreated()); - jsonBuilder.add("last", user.getLast()); - userJson.add(jsonBuilder.build()); + userJson.add(simple("name", user.getName())); } return simple("user", userJson); @@ -67,15 +62,16 @@ public static JSONObject joinChannel(final JSON data, final UUID uuid) { final Channel channel = channelHandler.getChannelByUUID(data.getUUID("channel")); if (user == null) { - return simple("error", "User with the UUID \"" + uuid.toString() + "\" can't be found."); + return simple("error", "user_not_found"); } if (channel == null) { - return simple("error", "Channel with the UUID \"" + data.getUUID("channel").toString() + "\" can't be found."); + return simple("error", "channel_not_found"); } channelHandler.getChannelByUUID(data.getUUID("channel")).addUser(user); + channelHandler.notifyUsers(ChatAction.MEMBER_JOIN, channel, user.getUUID()); - return simple("info", "Added User \"" + user.getUUID().toString() + "\" to channel \"" + channel.getUuid().toString() + "\"."); + return simple("success", true); } @UserEndpoint(path = {"channel", "leave"}, keys = {"channel"}, types = {UUID.class}) @@ -86,14 +82,15 @@ public static JSONObject leaveChannel(final JSON data, final UUID uuid) { final Channel channel = channelHandler.getChannelByUUID(data.getUUID("channel")); if (user == null) { - return simple("error", "User with the UUID \"" + uuid.toString() + "\" can't be found."); + return simple("error", "user_not_found"); } if (channel == null) { - return simple("error", "Channel with the UUID \"" + data.getUUID("channel").toString() + "\" can't be found."); + return simple("error", "channel_not_found"); } channelHandler.getChannelByUUID(data.getUUID("channel")).removeUser(user); + channelHandler.notifyUsers(ChatAction.MEMBER_LEAVE, channel, user.getUUID()); - return simple("info", "Removed User \"" + user.getUUID().toString() + "\" from channel \"" + channel.getUuid().toString() + "\"."); + return simple("success", true); } } From 62a78888e4191d14b237d37275289662b4ced8c5 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 15 Oct 2019 22:09:05 +0200 Subject: [PATCH 05/40] Added Hardcoded Channel "global". (#1) --- src/main/java/net/cryptic_game/microservice/chat/App.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/net/cryptic_game/microservice/chat/App.java b/src/main/java/net/cryptic_game/microservice/chat/App.java index 200dd50..37a6ff1 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/App.java +++ b/src/main/java/net/cryptic_game/microservice/chat/App.java @@ -12,6 +12,8 @@ public App() { super("chat"); this.channelHandler = new ChannelHandler(); + + this.channelHandler.addChanel("global"); } public static void main(String[] args) { From c9be9393dd3f7a76fcc5d1403aa64bf86e580739 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 15 Oct 2019 22:13:22 +0200 Subject: [PATCH 06/40] Added toString() when UUID is added to JSON. (#1) --- .../microservice/chat/channel/ChannelHandler.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java index 17d87dd..60ca792 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java @@ -37,8 +37,8 @@ public void notifyUsers(final ChatAction action, final Channel channel, final UU .add("origin", "chat") .add("data", anJSON() .add("action", action.getValue()) - .add("channel", channel) - .add("user", channel).build() + .add("channel", channel.getUuid().toString()) + .add("user", channel.toString()).build() ).build(); channel.getUser().forEach(user -> MicroService.getInstance().sendToUser(target, json)); From 5e36c2a459bb00f5fb8bf0b598a8db3cf207f66e Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 15 Oct 2019 22:25:58 +0200 Subject: [PATCH 07/40] Fixed Dependencies (#1) --- pom.xml | 131 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 69 insertions(+), 62 deletions(-) diff --git a/pom.xml b/pom.xml index a2e4408..aaddc95 100644 --- a/pom.xml +++ b/pom.xml @@ -1,70 +1,77 @@ - 4.0.0 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - net.cryptic_game.microservice - chat - 0.0.3-SNAPSHOT - jar + net.cryptic_game.microservice + chat + 0.0.3-SNAPSHOT + jar - cryptic-game-chat - offical chat microservice of cryptic-game - https://cryptic-game.net + cryptic-game-chat + offical chat microservice of cryptic-game + https://cryptic-game.net - - - - maven-assembly-plugin - 3.1.0 - - - - net.cryptic_game.microservice.chat.App - - - - jar-with-dependencies - - - - - make-assembly - package - - single - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.8.0 - - 1.8 - 1.8 - - - - + + + + maven-assembly-plugin + 3.1.0 + + + + net.cryptic_game.microservice.chat.App + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + 1.8 + 1.8 + + + + - - UTF-8 - + + UTF-8 + - - - jitpack.io - https://jitpack.io - - + + + jitpack.io + https://jitpack.io + + - - - com.github.cryptic-game - java-lib - experimental-SNAPSHOT - - + + + com.github.cryptic-game + java-lib + experimental-SNAPSHOT + + + + + org.slf4j + slf4j-log4j12 + 1.7.26 + + From 0781f146c50e138ad4b0cf133f799c81ad973e9f Mon Sep 17 00:00:00 2001 From: Jannik Emmerich Date: Wed, 16 Oct 2019 18:05:37 +0200 Subject: [PATCH 08/40] Fixed some issues (#1) --- .../cryptic_game/microservice/chat/App.java | 16 +++++----- .../microservice/chat/channel/Channel.java | 30 +++++++++++------ .../chat/channel/ChannelHandler.java | 4 +-- .../chat/endpoint/ChannelEndpoints.java | 32 +++++++++++-------- 4 files changed, 49 insertions(+), 33 deletions(-) diff --git a/src/main/java/net/cryptic_game/microservice/chat/App.java b/src/main/java/net/cryptic_game/microservice/chat/App.java index 37a6ff1..e38f1d0 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/App.java +++ b/src/main/java/net/cryptic_game/microservice/chat/App.java @@ -6,22 +6,22 @@ public class App extends MicroService { - private final ChannelHandler channelHandler; + private static ChannelHandler channelHandler; - public App() { + private App() { super("chat"); - - this.channelHandler = new ChannelHandler(); - - this.channelHandler.addChanel("global"); } public static void main(String[] args) { BasicConfigurator.configure(); + + channelHandler = new ChannelHandler(); + channelHandler.addChanel("global"); + new App(); } - public ChannelHandler getChannelHandler() { - return this.channelHandler; + public static ChannelHandler getChannelHandler() { + return channelHandler; } } diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java b/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java index dfa34aa..127ab38 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java @@ -9,12 +9,12 @@ public class Channel { private final UUID uuid; private String name; - private final ArrayList user; + private final ArrayList users; - public Channel(final String name) { + Channel(final String name) { this.uuid = UUID.randomUUID(); this.name = name; - this.user = new ArrayList<>(); + this.users = new ArrayList<>(); } public UUID getUuid() { @@ -29,15 +29,27 @@ public void setName(final String name) { this.name = name; } - public ArrayList getUser() { - return this.user; + public ArrayList getUsers() { + return this.users; } - public void addUser(final User user) { - this.user.add(user); + public boolean addUser(final User user) { + for(User u : users) { + if(u.getUUID().equals(user.getUUID())) { + return false; + } + } + this.users.add(user); + return true; } - public void removeUser(final User user) { - this.user.remove(user); + public boolean removeUser(final User user) { + for(User u : users) { + if(u.getUUID().equals(user.getUUID())) { + this.users.remove(user); + return true; + } + } + return false; } } diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java index 60ca792..276913c 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java @@ -38,9 +38,9 @@ public void notifyUsers(final ChatAction action, final Channel channel, final UU .add("data", anJSON() .add("action", action.getValue()) .add("channel", channel.getUuid().toString()) - .add("user", channel.toString()).build() + .add("user", target.toString()).build() ).build(); - channel.getUser().forEach(user -> MicroService.getInstance().sendToUser(target, json)); + channel.getUsers().forEach(user -> MicroService.getInstance().sendToUser(user.getUUID(), json)); } } diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java index 09b6b5f..274a94d 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java @@ -21,7 +21,7 @@ public class ChannelEndpoints { @UserEndpoint(path = {"channel", "list"}, keys = {}, types = {}) public static JSONObject getChannel(final JSON data, final UUID uuid) { - final ChannelHandler channelHandler = ((App) App.getInstance()).getChannelHandler(); + final ChannelHandler channelHandler = App.getChannelHandler(); final List channelsJson = new ArrayList<>(); @@ -32,31 +32,31 @@ public static JSONObject getChannel(final JSON data, final UUID uuid) { .build()); } - return simple("channel", channelsJson); + return simple("channels", channelsJson); } @UserEndpoint(path = {"channel", "members"}, keys = {"channel"}, types = {UUID.class}) public static JSONObject getChannelMembers(final JSON data, final UUID uuid) { - final ChannelHandler channelHandler = ((App) App.getInstance()).getChannelHandler(); + final ChannelHandler channelHandler = App.getChannelHandler(); final Channel channel = channelHandler.getChannelByUUID(data.getUUID("channel")); if (channel == null) { - return simple("error", "Channel with the UUID \"" + data.getUUID("channel").toString() + "\" can't be found."); + return simple("error", "channel_not_found"); } final List userJson = new ArrayList<>(); - for (final User user : channel.getUser()) { + for (final User user : channel.getUsers()) { userJson.add(simple("name", user.getName())); } - return simple("user", userJson); + return simple("users", userJson); } @UserEndpoint(path = {"channel", "join"}, keys = {"channel"}, types = {UUID.class}) public static JSONObject joinChannel(final JSON data, final UUID uuid) { - final ChannelHandler channelHandler = ((App) App.getInstance()).getChannelHandler(); + final ChannelHandler channelHandler = App.getChannelHandler(); final User user = MicroService.getInstance().getUser(uuid); final Channel channel = channelHandler.getChannelByUUID(data.getUUID("channel")); @@ -68,15 +68,17 @@ public static JSONObject joinChannel(final JSON data, final UUID uuid) { return simple("error", "channel_not_found"); } - channelHandler.getChannelByUUID(data.getUUID("channel")).addUser(user); - channelHandler.notifyUsers(ChatAction.MEMBER_JOIN, channel, user.getUUID()); + if(channel.addUser(user)) { + channelHandler.notifyUsers(ChatAction.MEMBER_JOIN, channel, user.getUUID()); + return simple("success", true); + } - return simple("success", true); + return simple("success", false); } @UserEndpoint(path = {"channel", "leave"}, keys = {"channel"}, types = {UUID.class}) public static JSONObject leaveChannel(final JSON data, final UUID uuid) { - final ChannelHandler channelHandler = ((App) App.getInstance()).getChannelHandler(); + final ChannelHandler channelHandler = App.getChannelHandler(); final User user = MicroService.getInstance().getUser(uuid); final Channel channel = channelHandler.getChannelByUUID(data.getUUID("channel")); @@ -88,9 +90,11 @@ public static JSONObject leaveChannel(final JSON data, final UUID uuid) { return simple("error", "channel_not_found"); } - channelHandler.getChannelByUUID(data.getUUID("channel")).removeUser(user); - channelHandler.notifyUsers(ChatAction.MEMBER_LEAVE, channel, user.getUUID()); + if(channel.removeUser(user)) { + channelHandler.notifyUsers(ChatAction.MEMBER_LEAVE, channel, user.getUUID()); + return simple("success", true); + } - return simple("success", true); + return simple("success", false); } } From 44be490c5e4ca9ec44769dbe74dd80b347ae6798 Mon Sep 17 00:00:00 2001 From: Jannik Emmerich Date: Wed, 16 Oct 2019 22:38:23 +0200 Subject: [PATCH 09/40] Removed unused dependency (#1) --- pom.xml | 7 ------- .../net/cryptic_game/microservice/chat/endpoint/.gitkeep | 0 2 files changed, 7 deletions(-) delete mode 100644 src/main/java/net/cryptic_game/microservice/chat/endpoint/.gitkeep diff --git a/pom.xml b/pom.xml index aaddc95..de46c3b 100644 --- a/pom.xml +++ b/pom.xml @@ -66,12 +66,5 @@ java-lib experimental-SNAPSHOT - - - - org.slf4j - slf4j-log4j12 - 1.7.26 - diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/.gitkeep b/src/main/java/net/cryptic_game/microservice/chat/endpoint/.gitkeep deleted file mode 100644 index e69de29..0000000 From be7e97d63228f13374f1d5c2bf9da51cdfe81e11 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Mon, 28 Oct 2019 17:00:03 +0100 Subject: [PATCH 10/40] Added Message System. --- .../microservice/chat/channel/Channel.java | 10 +-- .../chat/channel/ChannelHandler.java | 41 ++++++++++-- .../microservice/chat/channel/ChatAction.java | 5 +- .../chat/endpoint/ChannelEndpoints.java | 8 +-- .../chat/endpoint/MessageEndpoints.java | 67 +++++++++++++++++++ 5 files changed, 115 insertions(+), 16 deletions(-) create mode 100644 src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java b/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java index 127ab38..83bbc50 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java @@ -8,8 +8,8 @@ public class Channel { private final UUID uuid; - private String name; private final ArrayList users; + private String name; Channel(final String name) { this.uuid = UUID.randomUUID(); @@ -34,8 +34,8 @@ public ArrayList getUsers() { } public boolean addUser(final User user) { - for(User u : users) { - if(u.getUUID().equals(user.getUUID())) { + for (User u : users) { + if (u.getUUID().equals(user.getUUID())) { return false; } } @@ -44,8 +44,8 @@ public boolean addUser(final User user) { } public boolean removeUser(final User user) { - for(User u : users) { - if(u.getUUID().equals(user.getUUID())) { + for (User u : users) { + if (u.getUUID().equals(user.getUUID())) { this.users.remove(user); return true; } diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java index 276913c..46732e9 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java @@ -1,9 +1,12 @@ package net.cryptic_game.microservice.chat.channel; import net.cryptic_game.microservice.MicroService; +import net.cryptic_game.microservice.utils.JSONBuilder; +import net.cryptic_game.microservice.wrapper.User; import org.json.simple.JSONObject; import java.util.ArrayList; +import java.util.Collections; import java.util.UUID; import static net.cryptic_game.microservice.utils.JSONBuilder.anJSON; @@ -31,16 +34,42 @@ public ArrayList getChannels() { return this.channels; } - public void notifyUsers(final ChatAction action, final Channel channel, final UUID target) { + public void notifyUser(final User user, final ChatAction action, final Channel channel, final UUID target) { + this.notifyUsers(new ArrayList<>(Collections.singletonList(user)), action, channel, target); + } + + public void notifyUser(final User user, final ChatAction action, final Channel channel, final UUID target, final JSONObject content) { + this.notifyUsers(new ArrayList<>(Collections.singletonList(user)), action, channel, target, content); + } + + public void notifyUsers(final ArrayList users, final ChatAction action, final Channel channel, final UUID target) { + this.notifyUsers(users, action, channel, target, null); + } + + public void notifyAllChannelUsers(final ChatAction action, final Channel channel, final UUID target) { + this.notifyUsers(channel.getUsers(), action, channel, target, null); + } + + public void notifyAllChannelUsers(final ChatAction action, final Channel channel, final UUID target, final JSONObject content) { + this.notifyUsers(channel.getUsers(), action, channel, target, content); + } + + public void notifyUsers(final ArrayList users, ChatAction action, final Channel channel, final UUID target, final JSONObject content) { + final JSONBuilder data = anJSON() + .add("action", action.getValue()) + .add("channel", channel.getUuid().toString()) + .add("user", target.toString()); + + if (content != null) { + data.add("content", content); + } + final JSONObject json = anJSON() .add("notify-id", "chat-update") .add("origin", "chat") - .add("data", anJSON() - .add("action", action.getValue()) - .add("channel", channel.getUuid().toString()) - .add("user", target.toString()).build() + .add("data", data.build() ).build(); - channel.getUsers().forEach(user -> MicroService.getInstance().sendToUser(user.getUUID(), json)); + users.forEach(user -> MicroService.getInstance().sendToUser(user.getUUID(), json)); } } diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/ChatAction.java b/src/main/java/net/cryptic_game/microservice/chat/channel/ChatAction.java index 90aa944..41e19b0 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/ChatAction.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/ChatAction.java @@ -2,7 +2,10 @@ public enum ChatAction { MEMBER_JOIN("member-join"), - MEMBER_LEAVE("member-leave"); + MEMBER_LEAVE("member-leave"), + + SEND_MESSAGE("send-message"), + WHISPER_MESSAGE("whisper-message"); final String value; diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java index 274a94d..1136623 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java @@ -68,8 +68,8 @@ public static JSONObject joinChannel(final JSON data, final UUID uuid) { return simple("error", "channel_not_found"); } - if(channel.addUser(user)) { - channelHandler.notifyUsers(ChatAction.MEMBER_JOIN, channel, user.getUUID()); + if (channel.addUser(user)) { + channelHandler.notifyAllChannelUsers(ChatAction.MEMBER_JOIN, channel, user.getUUID()); return simple("success", true); } @@ -90,8 +90,8 @@ public static JSONObject leaveChannel(final JSON data, final UUID uuid) { return simple("error", "channel_not_found"); } - if(channel.removeUser(user)) { - channelHandler.notifyUsers(ChatAction.MEMBER_LEAVE, channel, user.getUUID()); + if (channel.removeUser(user)) { + channelHandler.notifyAllChannelUsers(ChatAction.MEMBER_LEAVE, channel, user.getUUID()); return simple("success", true); } diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java new file mode 100644 index 0000000..8a03d37 --- /dev/null +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java @@ -0,0 +1,67 @@ +package net.cryptic_game.microservice.chat.endpoint; + +import net.cryptic_game.microservice.MicroService; +import net.cryptic_game.microservice.chat.App; +import net.cryptic_game.microservice.chat.channel.Channel; +import net.cryptic_game.microservice.chat.channel.ChatAction; +import net.cryptic_game.microservice.endpoint.UserEndpoint; +import net.cryptic_game.microservice.utils.JSON; +import net.cryptic_game.microservice.wrapper.User; +import org.json.simple.JSONObject; + +import java.util.Date; +import java.util.UUID; + +import static net.cryptic_game.microservice.utils.JSONBuilder.anJSON; +import static net.cryptic_game.microservice.utils.JSONBuilder.simple; + +public class MessageEndpoints { + + @UserEndpoint(path = {"message", "send"}, keys = {"channel", "message"}, types = {UUID.class, String.class}) + public static JSONObject sendMessage(final JSON data, final UUID userUuid) { + final UUID channelUuid = data.getUUID("channel"); + final String messageContent = data.get("content"); + + final Channel channel = App.getChannelHandler().getChannelByUUID(channelUuid); + if (channel == null) { + return simple("error", "channel_not_found"); + } + + final JSONObject content = anJSON() + .add("message", messageContent) + .add("send-date", new Date()) + .add("whisper", false) + .build(); + + App.getChannelHandler().notifyAllChannelUsers(ChatAction.SEND_MESSAGE, channel, userUuid, content); + + return simple("success", true); + } + + @UserEndpoint(path = {"message", "whisper"}, keys = {"channel", "message", "target"}, types = {UUID.class, String.class, UUID.class}) + public static JSONObject whisperMessage(final JSON data, final UUID userUuid) { + final UUID channelUuid = data.getUUID("channel"); + final String messageContent = data.get("content"); + final UUID targetUuid = data.getUUID("target"); + + final Channel channel = App.getChannelHandler().getChannelByUUID(channelUuid); + if (channel == null) { + return simple("error", "channel_not_found"); + } + + final User target = MicroService.getInstance().getUser(targetUuid); + if (target == null) { + return simple("error", "user_not_found"); + } + + final JSONObject content = anJSON() + .add("message", messageContent) + .add("send-date", new Date()) + .add("whisper", true) + .build(); + + App.getChannelHandler().notifyUser(target, ChatAction.SEND_MESSAGE, channel, userUuid, content); + + return simple("success", true); + } +} From 4dbcf1d08c6e82d0d9415e3708559b846ff01812 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 29 Oct 2019 15:40:43 +0100 Subject: [PATCH 11/40] Removed unused stuff. (#1) --- .../microservice/chat/endpoint/MessageEndpoints.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java index 8a03d37..599462a 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java @@ -30,7 +30,6 @@ public static JSONObject sendMessage(final JSON data, final UUID userUuid) { final JSONObject content = anJSON() .add("message", messageContent) .add("send-date", new Date()) - .add("whisper", false) .build(); App.getChannelHandler().notifyAllChannelUsers(ChatAction.SEND_MESSAGE, channel, userUuid, content); @@ -57,7 +56,6 @@ public static JSONObject whisperMessage(final JSON data, final UUID userUuid) { final JSONObject content = anJSON() .add("message", messageContent) .add("send-date", new Date()) - .add("whisper", true) .build(); App.getChannelHandler().notifyUser(target, ChatAction.SEND_MESSAGE, channel, userUuid, content); From 38dd16821178d48133e9a9951a50ea0f2bd1f921 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 29 Oct 2019 17:49:37 +0100 Subject: [PATCH 12/40] Added UUID to "/channel/members" Endpoint. (#2) --- .../microservice/chat/endpoint/ChannelEndpoints.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java index 1136623..2a80143 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java @@ -48,6 +48,7 @@ public static JSONObject getChannelMembers(final JSON data, final UUID uuid) { final List userJson = new ArrayList<>(); for (final User user : channel.getUsers()) { + userJson.add(simple("uuid", user.getUUID())); userJson.add(simple("name", user.getName())); } From dd6357944b91c13ab5c0bd12efdedbaa8f164af4 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 29 Oct 2019 20:01:08 +0100 Subject: [PATCH 13/40] Updated Type Error in "/messages/whisper". (#1) --- .../microservice/chat/endpoint/MessageEndpoints.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java index 599462a..2c62900 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java @@ -58,7 +58,7 @@ public static JSONObject whisperMessage(final JSON data, final UUID userUuid) { .add("send-date", new Date()) .build(); - App.getChannelHandler().notifyUser(target, ChatAction.SEND_MESSAGE, channel, userUuid, content); + App.getChannelHandler().notifyUser(target, ChatAction.WHISPER_MESSAGE, channel, userUuid, content); return simple("success", true); } From f379d87696042dec6ec99d0f09752a3a203dd654 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 29 Oct 2019 20:03:23 +0100 Subject: [PATCH 14/40] Add Method to delete Channels. (#1) --- .../microservice/chat/channel/ChannelHandler.java | 6 ++++++ .../cryptic_game/microservice/chat/channel/ChatAction.java | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java index 46732e9..42147c9 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java @@ -25,6 +25,12 @@ public Channel addChanel(final String name) { return channel; } + public void removeChannel(final UUID channelUuid, final UUID userUuid) { + final Channel channel = this.getChannelByUUID(channelUuid); + this.notifyAllChannelUsers(ChatAction.CHANNEL_DELETE, channel, userUuid); + this.channels.remove(channel); + } + public Channel getChannelByUUID(final UUID uuid) { for (final Channel channel : this.channels) if (channel.getUuid().equals(uuid)) return channel; return null; diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/ChatAction.java b/src/main/java/net/cryptic_game/microservice/chat/channel/ChatAction.java index 41e19b0..3e4426b 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/ChatAction.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/ChatAction.java @@ -5,7 +5,9 @@ public enum ChatAction { MEMBER_LEAVE("member-leave"), SEND_MESSAGE("send-message"), - WHISPER_MESSAGE("whisper-message"); + WHISPER_MESSAGE("whisper-message"), + + CHANNEL_DELETE("channel-delete"); final String value; From e7bc787e7c0ebf1d9b3a5bfacc4128ceb60431ea Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 29 Oct 2019 20:03:58 +0100 Subject: [PATCH 15/40] Added JUnit Stuff. (#1) --- pom.xml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pom.xml b/pom.xml index de46c3b..142dbb8 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,14 @@ 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M3 + + @@ -66,5 +74,18 @@ java-lib experimental-SNAPSHOT + + + org.powermock + powermock-module-junit4 + 2.0.2 + test + + + org.powermock + powermock-api-mockito2 + 2.0.2 + test + From 67b9254175d3e860d1175a606813bed7c07c2752 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 29 Oct 2019 20:04:14 +0100 Subject: [PATCH 16/40] Added Channel Tests. (#1) --- .../chat/channel/ChannelTests.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java diff --git a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java new file mode 100644 index 0000000..efc184f --- /dev/null +++ b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java @@ -0,0 +1,56 @@ +package net.cryptic_game.microservice.chat.channel; + +import net.cryptic_game.microservice.wrapper.User; +import org.junit.Test; + +import java.util.Date; +import java.util.UUID; + +import static org.junit.Assert.*; + +public class ChannelTests { + + @Test + public void testGetUuid() { + final Channel channel = new Channel("test"); + assertNotNull(channel.getUuid()); + } + + @Test + public void testGetName() { + final Channel channel = new Channel("test"); + assertEquals("test", channel.getName()); + } + + @Test + public void testSetName() { + final Channel channel = new Channel("test"); + channel.setName("test123"); + + assertEquals("test123", channel.getName()); + } + + @Test + public void testAddUser() { + final Channel channel = new Channel("test"); + final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + channel.addUser(testUser); + + assertTrue(channel.getUsers().contains(testUser)); + } + + @Test + public void testRemoveUser() { + final Channel channel = new Channel("test"); + final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + channel.addUser(testUser); + + if (!channel.getUsers().contains(testUser)) { + fail(); + } + + channel.removeUser(testUser); + + assertFalse(channel.getUsers().contains(testUser)); + } +} From 21f7d28097ccb12596a6c31fa46922e14ef5ac09 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 29 Oct 2019 20:07:56 +0100 Subject: [PATCH 17/40] Added ChannelHandler Tests. (#1) --- .../chat/channel/ChannelHandler.java | 6 ++- .../chat/channel/ChannelHandlerTests.java | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/test/java/net/cryptic_game/microservice/chat/channel/ChannelHandlerTests.java diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java index 42147c9..f5f9ad8 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java @@ -26,8 +26,12 @@ public Channel addChanel(final String name) { } public void removeChannel(final UUID channelUuid, final UUID userUuid) { + this.removeChannel(channelUuid, userUuid, true); + } + + public void removeChannel(final UUID channelUuid, final UUID userUuid, final boolean notifyUsers) { final Channel channel = this.getChannelByUUID(channelUuid); - this.notifyAllChannelUsers(ChatAction.CHANNEL_DELETE, channel, userUuid); + if (notifyUsers) this.notifyAllChannelUsers(ChatAction.CHANNEL_DELETE, channel, userUuid); this.channels.remove(channel); } diff --git a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelHandlerTests.java b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelHandlerTests.java new file mode 100644 index 0000000..544882b --- /dev/null +++ b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelHandlerTests.java @@ -0,0 +1,43 @@ +package net.cryptic_game.microservice.chat.channel; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ChannelHandlerTests { + + @Test + public void testChannelHandlerAddChannel() { + final ChannelHandler channelHandler = new ChannelHandler(); + + channelHandler.addChanel("test"); + + assertEquals("test", channelHandler.getChannels().get(0).getName()); + } + + @Test + public void testChannelHandlerRemoveChannel() { + final ChannelHandler channelHandler = new ChannelHandler(); + + channelHandler.addChanel("test"); + + final Channel testChannel = channelHandler.getChannels().get(0); + if (!channelHandler.getChannels().get(0).getName().equals("test")) { + fail(); + } + + channelHandler.removeChannel(testChannel.getUuid(), null, false); + + assertNull(channelHandler.getChannelByUUID(testChannel.getUuid())); + } + + @Test + public void testChannelHandlerGetChannelByUuid() { + final ChannelHandler channelHandler = new ChannelHandler(); + + channelHandler.addChanel("test"); + final Channel testChannel = channelHandler.getChannels().get(0); + + assertNotNull(channelHandler.getChannelByUUID(testChannel.getUuid())); + } +} From ba6755e78972ba8642727c35fc04a4ffc9732c6f Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 29 Oct 2019 21:18:31 +0100 Subject: [PATCH 18/40] Changed Type UUID.class to String.class. (#1) --- .../microservice/chat/endpoint/ChannelEndpoints.java | 6 +++--- .../microservice/chat/endpoint/MessageEndpoints.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java index 2a80143..95fdf49 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java @@ -35,7 +35,7 @@ public static JSONObject getChannel(final JSON data, final UUID uuid) { return simple("channels", channelsJson); } - @UserEndpoint(path = {"channel", "members"}, keys = {"channel"}, types = {UUID.class}) + @UserEndpoint(path = {"channel", "members"}, keys = {"channel"}, types = {String.class}) public static JSONObject getChannelMembers(final JSON data, final UUID uuid) { final ChannelHandler channelHandler = App.getChannelHandler(); @@ -55,7 +55,7 @@ public static JSONObject getChannelMembers(final JSON data, final UUID uuid) { return simple("users", userJson); } - @UserEndpoint(path = {"channel", "join"}, keys = {"channel"}, types = {UUID.class}) + @UserEndpoint(path = {"channel", "join"}, keys = {"channel"}, types = {String.class}) public static JSONObject joinChannel(final JSON data, final UUID uuid) { final ChannelHandler channelHandler = App.getChannelHandler(); @@ -77,7 +77,7 @@ public static JSONObject joinChannel(final JSON data, final UUID uuid) { return simple("success", false); } - @UserEndpoint(path = {"channel", "leave"}, keys = {"channel"}, types = {UUID.class}) + @UserEndpoint(path = {"channel", "leave"}, keys = {"channel"}, types = {String.class}) public static JSONObject leaveChannel(final JSON data, final UUID uuid) { final ChannelHandler channelHandler = App.getChannelHandler(); diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java index 2c62900..c597f3e 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java @@ -17,7 +17,7 @@ public class MessageEndpoints { - @UserEndpoint(path = {"message", "send"}, keys = {"channel", "message"}, types = {UUID.class, String.class}) + @UserEndpoint(path = {"message", "send"}, keys = {"channel", "message"}, types = {String.class, String.class}) public static JSONObject sendMessage(final JSON data, final UUID userUuid) { final UUID channelUuid = data.getUUID("channel"); final String messageContent = data.get("content"); @@ -37,7 +37,7 @@ public static JSONObject sendMessage(final JSON data, final UUID userUuid) { return simple("success", true); } - @UserEndpoint(path = {"message", "whisper"}, keys = {"channel", "message", "target"}, types = {UUID.class, String.class, UUID.class}) + @UserEndpoint(path = {"message", "whisper"}, keys = {"channel", "message", "target"}, types = {String.class, String.class, String.class}) public static JSONObject whisperMessage(final JSON data, final UUID userUuid) { final UUID channelUuid = data.getUUID("channel"); final String messageContent = data.get("content"); From f60d43bb0e6bc8fe314a7e725044feac93a6067c Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 29 Oct 2019 21:30:35 +0100 Subject: [PATCH 19/40] Changed UUID to UUID.toString() (#1) --- .../microservice/chat/endpoint/ChannelEndpoints.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java index 95fdf49..bdeff2c 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java @@ -27,7 +27,7 @@ public static JSONObject getChannel(final JSON data, final UUID uuid) { for (final Channel channel : channelHandler.getChannels()) { channelsJson.add(anJSON() - .add("uuid", channel.getUuid()) + .add("uuid", channel.getUuid().toString()) .add("name", channel.getName()) .build()); } @@ -48,7 +48,7 @@ public static JSONObject getChannelMembers(final JSON data, final UUID uuid) { final List userJson = new ArrayList<>(); for (final User user : channel.getUsers()) { - userJson.add(simple("uuid", user.getUUID())); + userJson.add(simple("uuid", user.getUUID().toString())); userJson.add(simple("name", user.getName())); } From 4ad0dcda2a6a8070e533c97f13f6c9a539b84ad4 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Wed, 30 Oct 2019 17:08:54 +0100 Subject: [PATCH 20/40] Updated Tests. (#1) --- .../chat/channel/ChannelTests.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java index efc184f..d3632ea 100644 --- a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java +++ b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java @@ -34,11 +34,19 @@ public void testSetName() { public void testAddUser() { final Channel channel = new Channel("test"); final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); - channel.addUser(testUser); + assertTrue(channel.addUser(testUser)); assertTrue(channel.getUsers().contains(testUser)); } + @Test + public void testDupedAddUser() { + final Channel channel = new Channel("test"); + final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + channel.addUser(testUser); + assertFalse(channel.addUser(testUser)); + } + @Test public void testRemoveUser() { final Channel channel = new Channel("test"); @@ -49,8 +57,15 @@ public void testRemoveUser() { fail(); } - channel.removeUser(testUser); - + assertTrue(channel.removeUser(testUser)); assertFalse(channel.getUsers().contains(testUser)); } + + @Test + public void testRemoveUserWithoutExist() { + final Channel channel = new Channel("test"); + final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + + assertFalse(channel.removeUser(testUser)); + } } From fe4941814574c43843952b16601686a61370f48d Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Wed, 30 Oct 2019 17:19:57 +0100 Subject: [PATCH 21/40] Fixed SonarCloud Stuff. (#1) --- .../microservice/chat/channel/Channel.java | 9 ++++---- .../chat/channel/ChannelHandler.java | 9 ++++---- .../chat/endpoint/ChannelEndpoints.java | 23 +++++++++++-------- .../chat/endpoint/EndpointResponse.java | 23 +++++++++++++++++++ .../chat/endpoint/MessageEndpoints.java | 11 ++++++--- 5 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 src/main/java/net/cryptic_game/microservice/chat/endpoint/EndpointResponse.java diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java b/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java index 83bbc50..a4c93e9 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java @@ -3,12 +3,13 @@ import net.cryptic_game.microservice.wrapper.User; import java.util.ArrayList; +import java.util.List; import java.util.UUID; public class Channel { private final UUID uuid; - private final ArrayList users; + private final List users; private String name; Channel(final String name) { @@ -29,12 +30,12 @@ public void setName(final String name) { this.name = name; } - public ArrayList getUsers() { + public List getUsers() { return this.users; } public boolean addUser(final User user) { - for (User u : users) { + for (final User u : this.users) { if (u.getUUID().equals(user.getUUID())) { return false; } @@ -44,7 +45,7 @@ public boolean addUser(final User user) { } public boolean removeUser(final User user) { - for (User u : users) { + for (final User u : this.users) { if (u.getUUID().equals(user.getUUID())) { this.users.remove(user); return true; diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java index f5f9ad8..8255c1d 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java @@ -7,13 +7,14 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.UUID; import static net.cryptic_game.microservice.utils.JSONBuilder.anJSON; public class ChannelHandler { - private final ArrayList channels; + private final List channels; public ChannelHandler() { this.channels = new ArrayList<>(); @@ -40,7 +41,7 @@ public Channel getChannelByUUID(final UUID uuid) { return null; } - public ArrayList getChannels() { + public List getChannels() { return this.channels; } @@ -52,7 +53,7 @@ public void notifyUser(final User user, final ChatAction action, final Channel c this.notifyUsers(new ArrayList<>(Collections.singletonList(user)), action, channel, target, content); } - public void notifyUsers(final ArrayList users, final ChatAction action, final Channel channel, final UUID target) { + public void notifyUsers(final List users, final ChatAction action, final Channel channel, final UUID target) { this.notifyUsers(users, action, channel, target, null); } @@ -64,7 +65,7 @@ public void notifyAllChannelUsers(final ChatAction action, final Channel channel this.notifyUsers(channel.getUsers(), action, channel, target, content); } - public void notifyUsers(final ArrayList users, ChatAction action, final Channel channel, final UUID target, final JSONObject content) { + public void notifyUsers(final List users, ChatAction action, final Channel channel, final UUID target, final JSONObject content) { final JSONBuilder data = anJSON() .add("action", action.getValue()) .add("channel", channel.getUuid().toString()) diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java index bdeff2c..38ddd53 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java @@ -14,11 +14,15 @@ import java.util.List; import java.util.UUID; +import static net.cryptic_game.microservice.chat.endpoint.EndpointResponse.*; import static net.cryptic_game.microservice.utils.JSONBuilder.anJSON; import static net.cryptic_game.microservice.utils.JSONBuilder.simple; public class ChannelEndpoints { + private ChannelEndpoints() { + } + @UserEndpoint(path = {"channel", "list"}, keys = {}, types = {}) public static JSONObject getChannel(final JSON data, final UUID uuid) { final ChannelHandler channelHandler = App.getChannelHandler(); @@ -42,12 +46,13 @@ public static JSONObject getChannelMembers(final JSON data, final UUID uuid) { final Channel channel = channelHandler.getChannelByUUID(data.getUUID("channel")); if (channel == null) { - return simple("error", "channel_not_found"); + return CHANNEL_NOT_FOUND.getJson(); } final List userJson = new ArrayList<>(); for (final User user : channel.getUsers()) { +// TODO: WITHOUT UUID (NAME IS UNIQUE) userJson.add(simple("uuid", user.getUUID().toString())); userJson.add(simple("name", user.getName())); } @@ -63,18 +68,18 @@ public static JSONObject joinChannel(final JSON data, final UUID uuid) { final Channel channel = channelHandler.getChannelByUUID(data.getUUID("channel")); if (user == null) { - return simple("error", "user_not_found"); + return USER_NOT_FOUND.getJson(); } if (channel == null) { - return simple("error", "channel_not_found"); + return CHANNEL_NOT_FOUND.getJson(); } if (channel.addUser(user)) { channelHandler.notifyAllChannelUsers(ChatAction.MEMBER_JOIN, channel, user.getUUID()); - return simple("success", true); + return SUCCESS.getJson(); } - return simple("success", false); + return FAIL.getJson(); } @UserEndpoint(path = {"channel", "leave"}, keys = {"channel"}, types = {String.class}) @@ -85,17 +90,17 @@ public static JSONObject leaveChannel(final JSON data, final UUID uuid) { final Channel channel = channelHandler.getChannelByUUID(data.getUUID("channel")); if (user == null) { - return simple("error", "user_not_found"); + return USER_NOT_FOUND.getJson(); } if (channel == null) { - return simple("error", "channel_not_found"); + return CHANNEL_NOT_FOUND.getJson(); } if (channel.removeUser(user)) { channelHandler.notifyAllChannelUsers(ChatAction.MEMBER_LEAVE, channel, user.getUUID()); - return simple("success", true); + return SUCCESS.getJson(); } - return simple("success", false); + return FAIL.getJson(); } } diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/EndpointResponse.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/EndpointResponse.java new file mode 100644 index 0000000..38a66eb --- /dev/null +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/EndpointResponse.java @@ -0,0 +1,23 @@ +package net.cryptic_game.microservice.chat.endpoint; + +import org.json.simple.JSONObject; + +import static net.cryptic_game.microservice.utils.JSONBuilder.simple; + +public enum EndpointResponse { + + USER_NOT_FOUND(simple("error", "user_not_found")), + CHANNEL_NOT_FOUND(simple("error", "channel_not_found")), + FAIL(simple("success", false)), + SUCCESS(simple("success", true)); + + final JSONObject json; + + EndpointResponse(final JSONObject json) { + this.json = json; + } + + public JSONObject getJson() { + return this.json; + } +} diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java index c597f3e..69ec0ed 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java @@ -12,11 +12,16 @@ import java.util.Date; import java.util.UUID; +import static net.cryptic_game.microservice.chat.endpoint.EndpointResponse.CHANNEL_NOT_FOUND; +import static net.cryptic_game.microservice.chat.endpoint.EndpointResponse.USER_NOT_FOUND; import static net.cryptic_game.microservice.utils.JSONBuilder.anJSON; import static net.cryptic_game.microservice.utils.JSONBuilder.simple; public class MessageEndpoints { + private MessageEndpoints() { + } + @UserEndpoint(path = {"message", "send"}, keys = {"channel", "message"}, types = {String.class, String.class}) public static JSONObject sendMessage(final JSON data, final UUID userUuid) { final UUID channelUuid = data.getUUID("channel"); @@ -24,7 +29,7 @@ public static JSONObject sendMessage(final JSON data, final UUID userUuid) { final Channel channel = App.getChannelHandler().getChannelByUUID(channelUuid); if (channel == null) { - return simple("error", "channel_not_found"); + return CHANNEL_NOT_FOUND.getJson(); } final JSONObject content = anJSON() @@ -45,12 +50,12 @@ public static JSONObject whisperMessage(final JSON data, final UUID userUuid) { final Channel channel = App.getChannelHandler().getChannelByUUID(channelUuid); if (channel == null) { - return simple("error", "channel_not_found"); + return CHANNEL_NOT_FOUND.getJson(); } final User target = MicroService.getInstance().getUser(targetUuid); if (target == null) { - return simple("error", "user_not_found"); + return USER_NOT_FOUND.getJson(); } final JSONObject content = anJSON() From 5507f3bc30e69cf315aab85ea04fd3c034d13ca9 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Wed, 30 Oct 2019 17:34:19 +0100 Subject: [PATCH 22/40] Updated SonarCloud Stuff in Class Channel. (#1) --- .../chat/channel/ChannelTests.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java index d3632ea..4beaa12 100644 --- a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java +++ b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java @@ -35,10 +35,17 @@ public void testAddUser() { final Channel channel = new Channel("test"); final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); - assertTrue(channel.addUser(testUser)); assertTrue(channel.getUsers().contains(testUser)); } + @Test + public void testAddUserReturn() { + final Channel channel = new Channel("test"); + final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + + assertTrue(channel.addUser(testUser)); + } + @Test public void testDupedAddUser() { final Channel channel = new Channel("test"); @@ -57,10 +64,22 @@ public void testRemoveUser() { fail(); } - assertTrue(channel.removeUser(testUser)); assertFalse(channel.getUsers().contains(testUser)); } + @Test + public void testRemoveUserReturn() { + final Channel channel = new Channel("test"); + final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + channel.addUser(testUser); + + if (!channel.getUsers().contains(testUser)) { + fail(); + } + + assertTrue(channel.removeUser(testUser)); + } + @Test public void testRemoveUserWithoutExist() { final Channel channel = new Channel("test"); From 1d6ccd41feae0f979954653f2f99e381ad57991f Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Wed, 30 Oct 2019 17:43:47 +0100 Subject: [PATCH 23/40] Added getUserByName to Channel Object. (#1) --- .../microservice/chat/channel/Channel.java | 7 +++++++ .../microservice/chat/channel/ChannelTests.java | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java b/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java index a4c93e9..de566b5 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java @@ -53,4 +53,11 @@ public boolean removeUser(final User user) { } return false; } + + public User getUserByName(final String name) { + for (final User user : this.users) { + if (user.getName().equals(name)) return user; + } + return null; + } } diff --git a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java index 4beaa12..07355c6 100644 --- a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java +++ b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java @@ -87,4 +87,21 @@ public void testRemoveUserWithoutExist() { assertFalse(channel.removeUser(testUser)); } + + @Test + public void testGetUserByName() { + final Channel channel = new Channel("test"); + final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + channel.addUser(testUser); + + assertEquals(testUser, channel.getUserByName(testUser.getName())); + } + + @Test + public void testGetUserByNameWithoutExist() { + final Channel channel = new Channel("test"); + final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + + assertNull(channel.getUserByName(testUser.getName())); + } } From 60e2ff7309fc102b6b0607723e209451c3647b3a Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Wed, 30 Oct 2019 17:46:18 +0100 Subject: [PATCH 24/40] Changed Whisper to User by Uuid to Whisper to User by Name. (#1) --- .../microservice/chat/endpoint/ChannelEndpoints.java | 2 -- .../microservice/chat/endpoint/EndpointResponse.java | 1 + .../microservice/chat/endpoint/MessageEndpoints.java | 9 ++++----- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java index 38ddd53..73c7fa2 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java @@ -52,8 +52,6 @@ public static JSONObject getChannelMembers(final JSON data, final UUID uuid) { final List userJson = new ArrayList<>(); for (final User user : channel.getUsers()) { -// TODO: WITHOUT UUID (NAME IS UNIQUE) - userJson.add(simple("uuid", user.getUUID().toString())); userJson.add(simple("name", user.getName())); } diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/EndpointResponse.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/EndpointResponse.java index 38a66eb..b00defe 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/EndpointResponse.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/EndpointResponse.java @@ -8,6 +8,7 @@ public enum EndpointResponse { USER_NOT_FOUND(simple("error", "user_not_found")), CHANNEL_NOT_FOUND(simple("error", "channel_not_found")), + USER_NOT_IN_CHANNEL(simple("error", "user_not_in_channel")), FAIL(simple("success", false)), SUCCESS(simple("success", true)); diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java index 69ec0ed..f3b92d3 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java @@ -1,6 +1,5 @@ package net.cryptic_game.microservice.chat.endpoint; -import net.cryptic_game.microservice.MicroService; import net.cryptic_game.microservice.chat.App; import net.cryptic_game.microservice.chat.channel.Channel; import net.cryptic_game.microservice.chat.channel.ChatAction; @@ -13,7 +12,7 @@ import java.util.UUID; import static net.cryptic_game.microservice.chat.endpoint.EndpointResponse.CHANNEL_NOT_FOUND; -import static net.cryptic_game.microservice.chat.endpoint.EndpointResponse.USER_NOT_FOUND; +import static net.cryptic_game.microservice.chat.endpoint.EndpointResponse.USER_NOT_IN_CHANNEL; import static net.cryptic_game.microservice.utils.JSONBuilder.anJSON; import static net.cryptic_game.microservice.utils.JSONBuilder.simple; @@ -46,16 +45,16 @@ public static JSONObject sendMessage(final JSON data, final UUID userUuid) { public static JSONObject whisperMessage(final JSON data, final UUID userUuid) { final UUID channelUuid = data.getUUID("channel"); final String messageContent = data.get("content"); - final UUID targetUuid = data.getUUID("target"); + final String targetName = data.get("target"); final Channel channel = App.getChannelHandler().getChannelByUUID(channelUuid); if (channel == null) { return CHANNEL_NOT_FOUND.getJson(); } - final User target = MicroService.getInstance().getUser(targetUuid); + final User target = channel.getUserByName(targetName); if (target == null) { - return USER_NOT_FOUND.getJson(); + return USER_NOT_IN_CHANNEL.getJson(); } final JSONObject content = anJSON() From 6f15ff858a9020ba31e93b579763e1cfda854ebd Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Wed, 30 Oct 2019 17:48:19 +0100 Subject: [PATCH 25/40] Fixed Test Bug in Channel Object. (#1) --- .../cryptic_game/microservice/chat/channel/ChannelTests.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java index 07355c6..8d543a0 100644 --- a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java +++ b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java @@ -34,6 +34,7 @@ public void testSetName() { public void testAddUser() { final Channel channel = new Channel("test"); final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + channel.addUser(testUser); assertTrue(channel.getUsers().contains(testUser)); } @@ -64,6 +65,8 @@ public void testRemoveUser() { fail(); } + channel.removeUser(testUser); + assertFalse(channel.getUsers().contains(testUser)); } From 5505f82767af0a4e10848e79daf4a6b0bb20fcce Mon Sep 17 00:00:00 2001 From: Jannik Emmerich Date: Sat, 2 Nov 2019 00:58:39 +0100 Subject: [PATCH 26/40] Fixed SonarCloud (hopefully) (#1) --- .github/workflows/chat.yml | 3 ++- sonar-scanner.sh | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 sonar-scanner.sh diff --git a/.github/workflows/chat.yml b/.github/workflows/chat.yml index 8522427..0d1ce3c 100644 --- a/.github/workflows/chat.yml +++ b/.github/workflows/chat.yml @@ -21,4 +21,5 @@ jobs: if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action == 'opened') env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: mvn sonar:sonar -Dsonar.projectKey=cryptic-game_cryptic-chat -Dsonar.organization=cryptic-game -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=${{ secrets.SONAR_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + run: ./sonar-scanner.sh \ No newline at end of file diff --git a/sonar-scanner.sh b/sonar-scanner.sh new file mode 100644 index 0000000..8167f6f --- /dev/null +++ b/sonar-scanner.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +export SONAR_SCANNER_VERSION=4.0.0.1744 +export SONAR_SCANNER_HOME=$HOME/.sonar/sonar-scanner-$SONAR_SCANNER_VERSION-linux +rm -rf $SONAR_SCANNER_HOME +mkdir -p $SONAR_SCANNER_HOME +curl -sSLo $HOME/.sonar/sonar-scanner.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-$SONAR_SCANNER_VERSION-linux.zip +unzip $HOME/.sonar/sonar-scanner.zip -d $HOME/.sonar/ +rm $HOME/.sonar/sonar-scanner.zip +export PATH=$SONAR_SCANNER_HOME/bin:$PATH +export SONAR_SCANNER_OPTS="-server" + +sonar-scanner -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=$SONAR_TOKEN \ No newline at end of file From 4894facba5ea0f582d80fa085fa19fd7007531ba Mon Sep 17 00:00:00 2001 From: Defelo Date: Sat, 2 Nov 2019 01:02:03 +0100 Subject: [PATCH 27/40] made sonar-scanner.sh executable (#1) --- sonar-scanner.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 sonar-scanner.sh diff --git a/sonar-scanner.sh b/sonar-scanner.sh old mode 100644 new mode 100755 From 4ce91abe30a456bbdfad79b2d50c2f17a6d27f5e Mon Sep 17 00:00:00 2001 From: Jannik Emmerich Date: Sat, 2 Nov 2019 01:07:39 +0100 Subject: [PATCH 28/40] Create sonar-project.properties --- sonar-project.properties | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 sonar-project.properties diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..6ee12ad --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,6 @@ +sonar.organization=cryptic-game +sonar.projectKey=cryptic-game_cryptic-chat +sonar.projectName=cryptic-chat +sonar.projectVersion=1.0 +sonar.sources=. +sonar.coverage.exclusions=src/test/*,src/main/java/net/cryptic_game/microservice/chat/Main.java From 255299623327813e418e282aaaed995a3df2b380 Mon Sep 17 00:00:00 2001 From: Jannik Emmerich Date: Sat, 2 Nov 2019 01:14:32 +0100 Subject: [PATCH 29/40] Update sonar-project.properties --- sonar-project.properties | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sonar-project.properties b/sonar-project.properties index 6ee12ad..92395bd 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -2,5 +2,6 @@ sonar.organization=cryptic-game sonar.projectKey=cryptic-game_cryptic-chat sonar.projectName=cryptic-chat sonar.projectVersion=1.0 -sonar.sources=. +sonar.sources=src/main/java/* +sonar.java.binaries=target/classes/* sonar.coverage.exclusions=src/test/*,src/main/java/net/cryptic_game/microservice/chat/Main.java From 0f363ba8bd7d41a016a7fae827a1b23ebcbcc126 Mon Sep 17 00:00:00 2001 From: Jannik Emmerich Date: Sat, 2 Nov 2019 01:17:15 +0100 Subject: [PATCH 30/40] Update sonar-project.properties --- sonar-project.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sonar-project.properties b/sonar-project.properties index 92395bd..26d5d64 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -2,6 +2,6 @@ sonar.organization=cryptic-game sonar.projectKey=cryptic-game_cryptic-chat sonar.projectName=cryptic-chat sonar.projectVersion=1.0 -sonar.sources=src/main/java/* -sonar.java.binaries=target/classes/* +sonar.sources=src/main/java/ +sonar.java.binaries=target/classes/ sonar.coverage.exclusions=src/test/*,src/main/java/net/cryptic_game/microservice/chat/Main.java From ebba44b672532d6c4aabc43d73b497b678db4f05 Mon Sep 17 00:00:00 2001 From: Jannik Emmerich Date: Thu, 7 Nov 2019 14:51:28 +0100 Subject: [PATCH 31/40] Fixed some issues (#1) --- .../microservice/chat/channel/Channel.java | 3 +++ .../chat/channel/ChannelHandler.java | 18 ++++++------- .../chat/endpoint/ChannelEndpoints.java | 2 -- .../chat/endpoint/MessageEndpoints.java | 26 +++++++++++++------ 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java b/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java index de566b5..8de15e5 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java @@ -1,5 +1,6 @@ package net.cryptic_game.microservice.chat.channel; +import net.cryptic_game.microservice.chat.App; import net.cryptic_game.microservice.wrapper.User; import java.util.ArrayList; @@ -40,6 +41,7 @@ public boolean addUser(final User user) { return false; } } + App.getChannelHandler().notifyAllChannelUsers(ChatAction.MEMBER_JOIN, this, user.getName()); this.users.add(user); return true; } @@ -48,6 +50,7 @@ public boolean removeUser(final User user) { for (final User u : this.users) { if (u.getUUID().equals(user.getUUID())) { this.users.remove(user); + App.getChannelHandler().notifyAllChannelUsers(ChatAction.MEMBER_LEAVE, this, user.getName()); return true; } } diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java index 8255c1d..90e9a66 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java @@ -26,11 +26,11 @@ public Channel addChanel(final String name) { return channel; } - public void removeChannel(final UUID channelUuid, final UUID userUuid) { + public void removeChannel(final UUID channelUuid, final String userUuid) { this.removeChannel(channelUuid, userUuid, true); } - public void removeChannel(final UUID channelUuid, final UUID userUuid, final boolean notifyUsers) { + public void removeChannel(final UUID channelUuid, final String userUuid, final boolean notifyUsers) { final Channel channel = this.getChannelByUUID(channelUuid); if (notifyUsers) this.notifyAllChannelUsers(ChatAction.CHANNEL_DELETE, channel, userUuid); this.channels.remove(channel); @@ -45,31 +45,31 @@ public List getChannels() { return this.channels; } - public void notifyUser(final User user, final ChatAction action, final Channel channel, final UUID target) { + public void notifyUser(final User user, final ChatAction action, final Channel channel, final String target) { this.notifyUsers(new ArrayList<>(Collections.singletonList(user)), action, channel, target); } - public void notifyUser(final User user, final ChatAction action, final Channel channel, final UUID target, final JSONObject content) { + public void notifyUser(final User user, final ChatAction action, final Channel channel, final String target, final JSONObject content) { this.notifyUsers(new ArrayList<>(Collections.singletonList(user)), action, channel, target, content); } - public void notifyUsers(final List users, final ChatAction action, final Channel channel, final UUID target) { + public void notifyUsers(final List users, final ChatAction action, final Channel channel, final String target) { this.notifyUsers(users, action, channel, target, null); } - public void notifyAllChannelUsers(final ChatAction action, final Channel channel, final UUID target) { + public void notifyAllChannelUsers(final ChatAction action, final Channel channel, final String target) { this.notifyUsers(channel.getUsers(), action, channel, target, null); } - public void notifyAllChannelUsers(final ChatAction action, final Channel channel, final UUID target, final JSONObject content) { + public void notifyAllChannelUsers(final ChatAction action, final Channel channel, final String target, final JSONObject content) { this.notifyUsers(channel.getUsers(), action, channel, target, content); } - public void notifyUsers(final List users, ChatAction action, final Channel channel, final UUID target, final JSONObject content) { + public void notifyUsers(final List users, ChatAction action, final Channel channel, final String target, final JSONObject content) { final JSONBuilder data = anJSON() .add("action", action.getValue()) .add("channel", channel.getUuid().toString()) - .add("user", target.toString()); + .add("user", target); if (content != null) { data.add("content", content); diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java index 73c7fa2..364e871 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java @@ -73,7 +73,6 @@ public static JSONObject joinChannel(final JSON data, final UUID uuid) { } if (channel.addUser(user)) { - channelHandler.notifyAllChannelUsers(ChatAction.MEMBER_JOIN, channel, user.getUUID()); return SUCCESS.getJson(); } @@ -95,7 +94,6 @@ public static JSONObject leaveChannel(final JSON data, final UUID uuid) { } if (channel.removeUser(user)) { - channelHandler.notifyAllChannelUsers(ChatAction.MEMBER_LEAVE, channel, user.getUUID()); return SUCCESS.getJson(); } diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java index f3b92d3..030cef9 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/MessageEndpoints.java @@ -1,5 +1,6 @@ package net.cryptic_game.microservice.chat.endpoint; +import net.cryptic_game.microservice.MicroService; import net.cryptic_game.microservice.chat.App; import net.cryptic_game.microservice.chat.channel.Channel; import net.cryptic_game.microservice.chat.channel.ChatAction; @@ -11,8 +12,7 @@ import java.util.Date; import java.util.UUID; -import static net.cryptic_game.microservice.chat.endpoint.EndpointResponse.CHANNEL_NOT_FOUND; -import static net.cryptic_game.microservice.chat.endpoint.EndpointResponse.USER_NOT_IN_CHANNEL; +import static net.cryptic_game.microservice.chat.endpoint.EndpointResponse.*; import static net.cryptic_game.microservice.utils.JSONBuilder.anJSON; import static net.cryptic_game.microservice.utils.JSONBuilder.simple; @@ -24,7 +24,12 @@ private MessageEndpoints() { @UserEndpoint(path = {"message", "send"}, keys = {"channel", "message"}, types = {String.class, String.class}) public static JSONObject sendMessage(final JSON data, final UUID userUuid) { final UUID channelUuid = data.getUUID("channel"); - final String messageContent = data.get("content"); + final String messageContent = data.get("message"); + final User user = MicroService.getInstance().getUser(userUuid); + + if (user == null) { + return USER_NOT_FOUND.getJson(); + } final Channel channel = App.getChannelHandler().getChannelByUUID(channelUuid); if (channel == null) { @@ -33,10 +38,10 @@ public static JSONObject sendMessage(final JSON data, final UUID userUuid) { final JSONObject content = anJSON() .add("message", messageContent) - .add("send-date", new Date()) + .add("send-date", new Date().getTime()) .build(); - App.getChannelHandler().notifyAllChannelUsers(ChatAction.SEND_MESSAGE, channel, userUuid, content); + App.getChannelHandler().notifyAllChannelUsers(ChatAction.SEND_MESSAGE, channel, user.getName(), content); return simple("success", true); } @@ -44,8 +49,13 @@ public static JSONObject sendMessage(final JSON data, final UUID userUuid) { @UserEndpoint(path = {"message", "whisper"}, keys = {"channel", "message", "target"}, types = {String.class, String.class, String.class}) public static JSONObject whisperMessage(final JSON data, final UUID userUuid) { final UUID channelUuid = data.getUUID("channel"); - final String messageContent = data.get("content"); + final String messageContent = data.get("message"); final String targetName = data.get("target"); + final User user = MicroService.getInstance().getUser(userUuid); + + if (user == null) { + return USER_NOT_FOUND.getJson(); + } final Channel channel = App.getChannelHandler().getChannelByUUID(channelUuid); if (channel == null) { @@ -59,10 +69,10 @@ public static JSONObject whisperMessage(final JSON data, final UUID userUuid) { final JSONObject content = anJSON() .add("message", messageContent) - .add("send-date", new Date()) + .add("send-date", new Date().getTime()) .build(); - App.getChannelHandler().notifyUser(target, ChatAction.WHISPER_MESSAGE, channel, userUuid, content); + App.getChannelHandler().notifyUser(target, ChatAction.WHISPER_MESSAGE, channel, user.getName(), content); return simple("success", true); } From 266d03427cb0417bb86ed379eb44b5f41e13ea86 Mon Sep 17 00:00:00 2001 From: Jannik Emmerich Date: Thu, 7 Nov 2019 14:55:00 +0100 Subject: [PATCH 32/40] Fixed Unit Tests for Channel (#1) --- .../chat/channel/ChannelTests.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java index 8d543a0..9612ff5 100644 --- a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java +++ b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java @@ -1,15 +1,36 @@ package net.cryptic_game.microservice.chat.channel; +import net.cryptic_game.microservice.chat.App; import net.cryptic_game.microservice.wrapper.User; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import java.util.Date; import java.util.UUID; import static org.junit.Assert.*; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; +@RunWith(PowerMockRunner.class) +@PrepareForTest(App.class) public class ChannelTests { + @Mock + ChannelHandler channelHandler; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mockStatic(App.class); + when(App.getChannelHandler()).thenReturn(channelHandler); + } + @Test public void testGetUuid() { final Channel channel = new Channel("test"); From bbd60ab441376556fa25c088ef652fb5a594891e Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Fri, 8 Nov 2019 14:08:05 +0100 Subject: [PATCH 33/40] Added Some more Tests & Updated JavaLib. (#1) --- pom.xml | 2 +- .../chat/channel/ChannelHandler.java | 6 +- .../chat/channel/ChannelHandlerTests.java | 206 +++++++++++++++++- .../chat/endpoint/ChannelEndpointsTests.java | 4 + 4 files changed, 211 insertions(+), 7 deletions(-) create mode 100644 src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java diff --git a/pom.xml b/pom.xml index 142dbb8..f3badcf 100644 --- a/pom.xml +++ b/pom.xml @@ -72,7 +72,7 @@ com.github.cryptic-game java-lib - experimental-SNAPSHOT + issue~26-incoming-packets-SNAPSHOT diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java index 90e9a66..45ea29c 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/ChannelHandler.java @@ -27,12 +27,8 @@ public Channel addChanel(final String name) { } public void removeChannel(final UUID channelUuid, final String userUuid) { - this.removeChannel(channelUuid, userUuid, true); - } - - public void removeChannel(final UUID channelUuid, final String userUuid, final boolean notifyUsers) { final Channel channel = this.getChannelByUUID(channelUuid); - if (notifyUsers) this.notifyAllChannelUsers(ChatAction.CHANNEL_DELETE, channel, userUuid); + this.notifyAllChannelUsers(ChatAction.CHANNEL_DELETE, channel, userUuid); this.channels.remove(channel); } diff --git a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelHandlerTests.java b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelHandlerTests.java index 544882b..7050eae 100644 --- a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelHandlerTests.java +++ b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelHandlerTests.java @@ -1,11 +1,49 @@ package net.cryptic_game.microservice.chat.channel; +import net.cryptic_game.microservice.MicroService; +import net.cryptic_game.microservice.chat.App; +import net.cryptic_game.microservice.wrapper.User; +import org.json.simple.JSONObject; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.UUID; + +import static net.cryptic_game.microservice.utils.JSONBuilder.anJSON; +import static net.cryptic_game.microservice.utils.JSONBuilder.simple; import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.powermock.api.mockito.PowerMockito.*; +@RunWith(PowerMockRunner.class) +@PrepareForTest({MicroService.class, App.class}) public class ChannelHandlerTests { + @Mock + MicroService microService; + + @Mock + ChannelHandler channelHandler; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mockStatic(MicroService.class); + mockStatic(App.class); + when(MicroService.getInstance()).thenReturn(microService); + when(App.getChannelHandler()).thenReturn(channelHandler); + } + @Test public void testChannelHandlerAddChannel() { final ChannelHandler channelHandler = new ChannelHandler(); @@ -26,7 +64,7 @@ public void testChannelHandlerRemoveChannel() { fail(); } - channelHandler.removeChannel(testChannel.getUuid(), null, false); + channelHandler.removeChannel(testChannel.getUuid(), null); assertNull(channelHandler.getChannelByUUID(testChannel.getUuid())); } @@ -40,4 +78,170 @@ public void testChannelHandlerGetChannelByUuid() { assertNotNull(channelHandler.getChannelByUUID(testChannel.getUuid())); } + + @Test + public void testNotifyUserWithOutJson() { + doNothing().when(microService).sendToUser(any(UUID.class), any(JSONObject.class)); + + final User target = new User(UUID.randomUUID(), "", "", new Date(), new Date()); + + final User user = new User(UUID.randomUUID(), "", "", new Date(), new Date()); + + final Channel channel = new Channel(""); + + final ChannelHandler channelHandler = new ChannelHandler(); + channelHandler.notifyUser(user, ChatAction.CHANNEL_DELETE, channel, target.getUUID().toString()); + + final JSONObject jsonObject = anJSON() + .add("notify-id", "chat-update") + .add("origin", "chat") + .add("data", anJSON() + .add("action", ChatAction.CHANNEL_DELETE.getValue()) + .add("channel", channel.getUuid().toString()) + .add("user", target.getUUID().toString()) + .build() + ).build(); + + verify(microService, times(1)).sendToUser(user.getUUID(), jsonObject); + } + + @Test + public void testNotifyUser() { + doNothing().when(microService).sendToUser(any(UUID.class), any(JSONObject.class)); + + final User target = new User(UUID.randomUUID(), "", "", new Date(), new Date()); + + final User user = new User(UUID.randomUUID(), "", "", new Date(), new Date()); + + final Channel channel = new Channel(""); + + final ChannelHandler channelHandler = new ChannelHandler(); + channelHandler.notifyUser(user, ChatAction.CHANNEL_DELETE, channel, target.getUUID().toString(), simple("abc", "cba")); + + final JSONObject jsonObject = anJSON() + .add("notify-id", "chat-update") + .add("origin", "chat") + .add("data", anJSON() + .add("action", ChatAction.CHANNEL_DELETE.getValue()) + .add("channel", channel.getUuid().toString()) + .add("user", target.getUUID().toString()) + .add("content", simple("abc", "cba")) + .build() + ).build(); + + verify(microService, times(1)).sendToUser(user.getUUID(), jsonObject); + } + + @Test + public void testNotifyAllChannelUsersWithOutJson() { + doNothing().when(microService).sendToUser(any(UUID.class), any(JSONObject.class)); + + final User target = new User(UUID.randomUUID(), "", "", new Date(), new Date()); + + final List users = new ArrayList<>(); + users.add(new User(UUID.randomUUID(), "", "", new Date(), new Date())); + + final Channel channel = new Channel(""); + users.forEach(channel::addUser); + + final ChannelHandler channelHandler = new ChannelHandler(); + channelHandler.notifyAllChannelUsers(ChatAction.CHANNEL_DELETE, channel, target.getUUID().toString()); + + final JSONObject jsonObject = anJSON() + .add("notify-id", "chat-update") + .add("origin", "chat") + .add("data", anJSON() + .add("action", ChatAction.CHANNEL_DELETE.getValue()) + .add("channel", channel.getUuid().toString()) + .add("user", target.getUUID().toString()) + .build() + ).build(); + + users.forEach(user -> verify(microService, times(1)).sendToUser(user.getUUID(), jsonObject)); + } + + @Test + public void testNotifyAllChannelUsers() { + doNothing().when(microService).sendToUser(any(UUID.class), any(JSONObject.class)); + + final User target = new User(UUID.randomUUID(), "", "", new Date(), new Date()); + + final List users = new ArrayList<>(); + users.add(new User(UUID.randomUUID(), "", "", new Date(), new Date())); + + final Channel channel = new Channel(""); + users.forEach(channel::addUser); + + final ChannelHandler channelHandler = new ChannelHandler(); + channelHandler.notifyAllChannelUsers(ChatAction.CHANNEL_DELETE, channel, target.getUUID().toString(), simple("abc", "cba")); + + final JSONObject jsonObject = anJSON() + .add("notify-id", "chat-update") + .add("origin", "chat") + .add("data", anJSON() + .add("action", ChatAction.CHANNEL_DELETE.getValue()) + .add("channel", channel.getUuid().toString()) + .add("user", target.getUUID().toString()) + .add("content", simple("abc", "cba")) + .build() + ).build(); + + users.forEach(user -> verify(microService, times(1)).sendToUser(user.getUUID(), jsonObject)); + } + + @Test + public void testNotifyUsersWithOutJson() { + doNothing().when(microService).sendToUser(any(UUID.class), any(JSONObject.class)); + + final User target = new User(UUID.randomUUID(), "", "", new Date(), new Date()); + + final List users = new ArrayList<>(); + users.add(new User(UUID.randomUUID(), "", "", new Date(), new Date())); + + final Channel channel = new Channel(""); + + final ChannelHandler channelHandler = new ChannelHandler(); + channelHandler.notifyUsers(users, ChatAction.CHANNEL_DELETE, channel, target.getUUID().toString()); + + final JSONObject jsonObject = anJSON() + .add("notify-id", "chat-update") + .add("origin", "chat") + .add("data", anJSON() + .add("action", ChatAction.CHANNEL_DELETE.getValue()) + .add("channel", channel.getUuid().toString()) + .add("user", target.getUUID().toString()) + .build() + ).build(); + + users.forEach(user -> verify(microService, times(1)).sendToUser(user.getUUID(), jsonObject)); + } + + @Test + public void testNotifyUsers() { + doNothing().when(microService).sendToUser(any(UUID.class), any(JSONObject.class)); + + final User target = new User(UUID.randomUUID(), "", "", new Date(), new Date()); + + final List users = new ArrayList<>(); + users.add(new User(UUID.randomUUID(), "", "", new Date(), new Date())); + + final Channel channel = new Channel(""); + + final ChannelHandler channelHandler = new ChannelHandler(); + channelHandler.notifyUsers(users, ChatAction.CHANNEL_DELETE, channel, target.getUUID().toString(), simple("abc", "cba")); + + final JSONObject jsonObject = anJSON() + .add("notify-id", "chat-update") + .add("origin", "chat") + .add("data", anJSON() + .add("action", ChatAction.CHANNEL_DELETE.getValue()) + .add("channel", channel.getUuid().toString()) + .add("user", target.getUUID().toString()) + .add("content", simple("abc", "cba")) + .build() + ).build(); + + users.forEach(user -> verify(microService, times(1)).sendToUser(user.getUUID(), jsonObject)); + } } + diff --git a/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java b/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java new file mode 100644 index 0000000..1e64d2b --- /dev/null +++ b/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java @@ -0,0 +1,4 @@ +package net.cryptic_game.microservice.chat.endpoint; + +public class ChannelEndpointsTests { +} From 59eea5014795428c505de1f6fca5ea603bad33f8 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Fri, 8 Nov 2019 14:20:59 +0100 Subject: [PATCH 34/40] Added Some more Tests & removed removed model package. (#1) --- .../java/net/cryptic_game/microservice/chat/model/.gitkeep | 0 .../cryptic_game/microservice/chat/channel/ChannelTests.java | 5 +++++ 2 files changed, 5 insertions(+) delete mode 100644 src/main/java/net/cryptic_game/microservice/chat/model/.gitkeep diff --git a/src/main/java/net/cryptic_game/microservice/chat/model/.gitkeep b/src/main/java/net/cryptic_game/microservice/chat/model/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java index 9612ff5..f6b04eb 100644 --- a/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java +++ b/src/test/java/net/cryptic_game/microservice/chat/channel/ChannelTests.java @@ -56,6 +56,7 @@ public void testAddUser() { final Channel channel = new Channel("test"); final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); channel.addUser(testUser); + channel.addUser(new User(UUID.randomUUID(), "test123", "test@test.test", new Date(), new Date())); assertTrue(channel.getUsers().contains(testUser)); } @@ -81,6 +82,7 @@ public void testRemoveUser() { final Channel channel = new Channel("test"); final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); channel.addUser(testUser); + channel.addUser(new User(UUID.randomUUID(), "test123", "test@test.test", new Date(), new Date())); if (!channel.getUsers().contains(testUser)) { fail(); @@ -96,6 +98,7 @@ public void testRemoveUserReturn() { final Channel channel = new Channel("test"); final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); channel.addUser(testUser); + channel.addUser(new User(UUID.randomUUID(), "test123", "test@test.test", new Date(), new Date())); if (!channel.getUsers().contains(testUser)) { fail(); @@ -107,6 +110,7 @@ public void testRemoveUserReturn() { @Test public void testRemoveUserWithoutExist() { final Channel channel = new Channel("test"); + channel.addUser(new User(UUID.randomUUID(), "test123", "test@test.test", new Date(), new Date())); final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); assertFalse(channel.removeUser(testUser)); @@ -124,6 +128,7 @@ public void testGetUserByName() { @Test public void testGetUserByNameWithoutExist() { final Channel channel = new Channel("test"); + channel.addUser(new User(UUID.randomUUID(), "test123", "test@test.test", new Date(), new Date())); final User testUser = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); assertNull(channel.getUserByName(testUser.getName())); From aa8c341e13e703a999d3012431d43a72046417ee Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Fri, 8 Nov 2019 14:22:42 +0100 Subject: [PATCH 35/40] Changed excluded File 'Main' to 'App'. (#1) --- sonar-project.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonar-project.properties b/sonar-project.properties index 26d5d64..779c751 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -4,4 +4,4 @@ sonar.projectName=cryptic-chat sonar.projectVersion=1.0 sonar.sources=src/main/java/ sonar.java.binaries=target/classes/ -sonar.coverage.exclusions=src/test/*,src/main/java/net/cryptic_game/microservice/chat/Main.java +sonar.coverage.exclusions=src/test/*,src/main/java/net/cryptic_game/microservice/chat/App.java From b7008b011af9efb5c87f7cde007d642dfd64f11a Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Fri, 8 Nov 2019 16:13:47 +0100 Subject: [PATCH 36/40] Changed JavaLib Version to Experimental. (#1) --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f3badcf..142dbb8 100644 --- a/pom.xml +++ b/pom.xml @@ -72,7 +72,7 @@ com.github.cryptic-game java-lib - issue~26-incoming-packets-SNAPSHOT + experimental-SNAPSHOT From 58f3ad8225eec1b4460d2cedf5eb647b945783a3 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 26 Nov 2019 15:11:19 +0100 Subject: [PATCH 37/40] Added some Unittests. (#1) --- .../chat/endpoint/ChannelEndpoints.java | 1 - .../chat/endpoint/ChannelEndpointsTests.java | 101 ++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java index 364e871..4595d9d 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java +++ b/src/main/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpoints.java @@ -4,7 +4,6 @@ import net.cryptic_game.microservice.chat.App; import net.cryptic_game.microservice.chat.channel.Channel; import net.cryptic_game.microservice.chat.channel.ChannelHandler; -import net.cryptic_game.microservice.chat.channel.ChatAction; import net.cryptic_game.microservice.endpoint.UserEndpoint; import net.cryptic_game.microservice.utils.JSON; import net.cryptic_game.microservice.wrapper.User; diff --git a/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java b/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java index 1e64d2b..0b94ab1 100644 --- a/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java +++ b/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java @@ -1,4 +1,105 @@ package net.cryptic_game.microservice.chat.endpoint; +import net.cryptic_game.microservice.chat.App; +import net.cryptic_game.microservice.chat.channel.Channel; +import net.cryptic_game.microservice.chat.channel.ChannelHandler; +import net.cryptic_game.microservice.utils.JSON; +import net.cryptic_game.microservice.wrapper.User; +import org.json.simple.JSONObject; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.Date; +import java.util.List; +import java.util.UUID; + +import static net.cryptic_game.microservice.utils.JSONBuilder.anJSON; +import static net.cryptic_game.microservice.utils.JSONBuilder.simple; +import static org.junit.Assert.*; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(App.class) public class ChannelEndpointsTests { + + @Mock + ChannelHandler channelHandler; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mockStatic(App.class); + when(App.getChannelHandler()).thenReturn(channelHandler); + } + + @Test + public void getChannel() { + final Channel[] channels = new Channel[2]; + channels[0] = this.channelHandler.addChanel("test"); + channels[1] = this.channelHandler.addChanel("test123"); + + final List resultChannels = (List) ChannelEndpoints.getChannel(new JSON(new JSONObject()), UUID.randomUUID()).get("channels"); + + for (int i = 0; i < resultChannels.size(); i++) { + final Channel channel = channels[i]; + if (anJSON() + .add("uuid", channel.getUuid().toString()) + .add("name", channel.getName()) + .build() != resultChannels.get(i)) { + fail(); + } + } + assertTrue(true); + } + + @Test + public void getChannelEmpty() { + final List resultChannels = (List) ChannelEndpoints.getChannel(new JSON(new JSONObject()), UUID.randomUUID()).get("channels"); + for (int i = 0; i < resultChannels.size(); i++) { + fail(); + } + assertTrue(true); + } + + @Test + public void getChannelMembersWithoutChannel() { + assertEquals(simple("error", "channel_not_found"), ChannelEndpoints.getChannelMembers( + new JSON(simple("channel", UUID.randomUUID())), + UUID.randomUUID())); + } + + @Test + public void getChannelMembers() { + this.channelHandler = new ChannelHandler(); + final Channel channel = this.channelHandler.addChanel("test"); + final User[] users = new User[2]; + users[0] = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + users[1] = new User(UUID.randomUUID(), "test1", "tes1t@test1.test1", new Date(), new Date()); + for (final User user : users) channel.addUser(user); + + final List resultUsers = (List) ChannelEndpoints.getChannelMembers( + new JSON(simple("channel", channel.getUuid())), + UUID.randomUUID()) + .get("users"); + for (int i = 0; i < resultUsers.size(); i++) { + if (simple("name", users[i].getName()) != resultUsers.get(i)) { + fail(); + } + } + assertTrue(true); + } + + @Test + public void joinChannel() { + } + + @Test + public void leaveChannel() { + } } From 828dfc09251088011c0592796fc0b4ba6f78e49c Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 26 Nov 2019 15:16:33 +0100 Subject: [PATCH 38/40] Removed Null Pointer Bug. (#1) --- .../chat/endpoint/ChannelEndpointsTests.java | 44 +++++++++---------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java b/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java index 0b94ab1..bed224e 100644 --- a/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java +++ b/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java @@ -4,7 +4,6 @@ import net.cryptic_game.microservice.chat.channel.Channel; import net.cryptic_game.microservice.chat.channel.ChannelHandler; import net.cryptic_game.microservice.utils.JSON; -import net.cryptic_game.microservice.wrapper.User; import org.json.simple.JSONObject; import org.junit.Before; import org.junit.Test; @@ -14,7 +13,6 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import java.util.Date; import java.util.List; import java.util.UUID; @@ -35,7 +33,7 @@ public class ChannelEndpointsTests { public void setUp() { MockitoAnnotations.initMocks(this); mockStatic(App.class); - when(App.getChannelHandler()).thenReturn(channelHandler); + when(App.getChannelHandler()).thenReturn(this.channelHandler); } @Test @@ -74,26 +72,26 @@ public void getChannelMembersWithoutChannel() { UUID.randomUUID())); } - @Test - public void getChannelMembers() { - this.channelHandler = new ChannelHandler(); - final Channel channel = this.channelHandler.addChanel("test"); - final User[] users = new User[2]; - users[0] = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); - users[1] = new User(UUID.randomUUID(), "test1", "tes1t@test1.test1", new Date(), new Date()); - for (final User user : users) channel.addUser(user); - - final List resultUsers = (List) ChannelEndpoints.getChannelMembers( - new JSON(simple("channel", channel.getUuid())), - UUID.randomUUID()) - .get("users"); - for (int i = 0; i < resultUsers.size(); i++) { - if (simple("name", users[i].getName()) != resultUsers.get(i)) { - fail(); - } - } - assertTrue(true); - } +// @Test +// public void getChannelMembers() { +// this.channelHandler = new ChannelHandler(); +// final Channel channel = this.channelHandler.addChanel("test"); +// final User[] users = new User[2]; +// users[0] = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); +// users[1] = new User(UUID.randomUUID(), "test1", "tes1t@test1.test1", new Date(), new Date()); +// for (final User user : users) channel.addUser(user); +// +// final List resultUsers = (List) ChannelEndpoints.getChannelMembers( +// new JSON(simple("channel", channel.getUuid())), +// UUID.randomUUID()) +// .get("users"); +// for (int i = 0; i < resultUsers.size(); i++) { +// if (simple("name", users[i].getName()) != resultUsers.get(i)) { +// fail(); +// } +// } +// assertTrue(true); +// } @Test public void joinChannel() { From bd471a8c823e37df5ae15d74866f57bd7e6bf7a3 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Wed, 27 Nov 2019 18:32:07 +0100 Subject: [PATCH 39/40] Added Some more Unit tests. (#1) --- .../microservice/chat/channel/Channel.java | 2 +- .../chat/endpoint/ChannelEndpointsTests.java | 83 +++++++++++++------ 2 files changed, 59 insertions(+), 26 deletions(-) diff --git a/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java b/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java index 8de15e5..487d95f 100644 --- a/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java +++ b/src/main/java/net/cryptic_game/microservice/chat/channel/Channel.java @@ -13,7 +13,7 @@ public class Channel { private final List users; private String name; - Channel(final String name) { + public Channel(final String name) { this.uuid = UUID.randomUUID(); this.name = name; this.users = new ArrayList<>(); diff --git a/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java b/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java index bed224e..a690cfc 100644 --- a/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java +++ b/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java @@ -1,39 +1,44 @@ package net.cryptic_game.microservice.chat.endpoint; +import net.cryptic_game.microservice.MicroService; import net.cryptic_game.microservice.chat.App; import net.cryptic_game.microservice.chat.channel.Channel; import net.cryptic_game.microservice.chat.channel.ChannelHandler; import net.cryptic_game.microservice.utils.JSON; +import net.cryptic_game.microservice.wrapper.User; import org.json.simple.JSONObject; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import java.util.Date; import java.util.List; import java.util.UUID; import static net.cryptic_game.microservice.utils.JSONBuilder.anJSON; import static net.cryptic_game.microservice.utils.JSONBuilder.simple; -import static org.junit.Assert.*; -import static org.powermock.api.mockito.PowerMockito.mockStatic; -import static org.powermock.api.mockito.PowerMockito.when; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.isA; +import static org.powermock.api.mockito.PowerMockito.*; @RunWith(PowerMockRunner.class) -@PrepareForTest(App.class) +@PrepareForTest({App.class, MicroService.class}) public class ChannelEndpointsTests { - @Mock - ChannelHandler channelHandler; + private ChannelHandler channelHandler; + private MicroService microService; @Before public void setUp() { - MockitoAnnotations.initMocks(this); mockStatic(App.class); + mockStatic(MicroService.class); + this.channelHandler = new ChannelHandler(); + this.microService = mock(MicroService.class); when(App.getChannelHandler()).thenReturn(this.channelHandler); + when(MicroService.getInstance()).thenReturn(this.microService); } @Test @@ -46,41 +51,45 @@ public void getChannel() { for (int i = 0; i < resultChannels.size(); i++) { final Channel channel = channels[i]; - if (anJSON() + assertEquals(anJSON() .add("uuid", channel.getUuid().toString()) .add("name", channel.getName()) - .build() != resultChannels.get(i)) { - fail(); - } + .build(), resultChannels.get(i)); } - assertTrue(true); + fail(); } @Test public void getChannelEmpty() { final List resultChannels = (List) ChannelEndpoints.getChannel(new JSON(new JSONObject()), UUID.randomUUID()).get("channels"); - for (int i = 0; i < resultChannels.size(); i++) { - fail(); - } - assertTrue(true); + assertEquals(resultChannels.size(), 0); } @Test public void getChannelMembersWithoutChannel() { - assertEquals(simple("error", "channel_not_found"), ChannelEndpoints.getChannelMembers( - new JSON(simple("channel", UUID.randomUUID())), - UUID.randomUUID())); + assertEquals(simple("error", "channel_not_found").toString(), ChannelEndpoints.getChannelMembers(new JSON(simple("channel", UUID.randomUUID())), UUID.randomUUID()).toString()); } // @Test // public void getChannelMembers() { -// this.channelHandler = new ChannelHandler(); -// final Channel channel = this.channelHandler.addChanel("test"); +// this.channelHandler = mock(ChannelHandler.class); +// when(App.getChannelHandler()).thenReturn(this.channelHandler); +// doNothing().when(this.channelHandler).notifyAllChannelUsers(isA(ChatAction.class), isA(Channel.class), isA(String.class)); +// +// final Channel channel = new Channel("test123"); // final User[] users = new User[2]; // users[0] = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); // users[1] = new User(UUID.randomUUID(), "test1", "tes1t@test1.test1", new Date(), new Date()); -// for (final User user : users) channel.addUser(user); +// for (final User user : users) { +// channel.addUser(user); +// } // +// System.out.println(channel); +// when(this.channelHandler.getChannelByUUID(channel.getUuid())).thenReturn(channel); +//// when(this.channelHandler.notifyAllChannelUsers(ChatAction.MEMBER_JOIN, channel, users[0].getName())); +// System.out.println(ChannelEndpoints.getChannelMembers( +// new JSON(simple("channel", channel.getUuid())), +// UUID.randomUUID())); // final List resultUsers = (List) ChannelEndpoints.getChannelMembers( // new JSON(simple("channel", channel.getUuid())), // UUID.randomUUID()) @@ -94,9 +103,33 @@ public void getChannelMembersWithoutChannel() { // } @Test - public void joinChannel() { + public void joinChannelEmptyUser() { + assertEquals(simple("error", "user_not_found").toString(), ChannelEndpoints.joinChannel(new JSON(simple("channel", UUID.randomUUID())), UUID.randomUUID()).toString()); + } + + @Test + public void joinChannelEmptyChannel() { + final User user = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + when(this.microService.getUser(isA(UUID.class))).thenReturn(user); + assertEquals(simple("error", "channel_not_found").toString(), ChannelEndpoints.joinChannel(new JSON(simple("channel", UUID.randomUUID())), user.getUUID()).toString()); } +// @Test +// public void joinChannelAlreadyJoined() { +// final Channel channel = this.channelHandler.addChanel("test123"); +// final User user = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); +// when(this.microService.getUser(isA(UUID.class))).thenReturn(user); +// assertEquals(simple("success", true).toString(), ChannelEndpoints.joinChannel(new JSON(simple("channel", channel.getUuid())), user.getUUID()).toString()); +// } + +// @Test +// public void joinChannel() { +// final Channel channel = this.channelHandler.addChanel("test123"); +// final User user = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); +// when(this.microService.getUser(isA(UUID.class))).thenReturn(user); +// assertEquals(simple("success", true).toString(), ChannelEndpoints.joinChannel(new JSON(simple("channel", channel.getUuid())), user.getUUID()).toString()); +// } + @Test public void leaveChannel() { } From d91f952f12c4aeaac30815af9b0fa49485c75e4b Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Fri, 29 Nov 2019 14:04:13 +0100 Subject: [PATCH 40/40] Added Tests for Channel Endpoints. (#1) --- .../chat/endpoint/ChannelEndpointsTests.java | 140 +++++++++++------- 1 file changed, 90 insertions(+), 50 deletions(-) diff --git a/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java b/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java index a690cfc..fa4d0e0 100644 --- a/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java +++ b/src/test/java/net/cryptic_game/microservice/chat/endpoint/ChannelEndpointsTests.java @@ -4,6 +4,7 @@ import net.cryptic_game.microservice.chat.App; import net.cryptic_game.microservice.chat.channel.Channel; import net.cryptic_game.microservice.chat.channel.ChannelHandler; +import net.cryptic_game.microservice.chat.channel.ChatAction; import net.cryptic_game.microservice.utils.JSON; import net.cryptic_game.microservice.wrapper.User; import org.json.simple.JSONObject; @@ -20,7 +21,6 @@ import static net.cryptic_game.microservice.utils.JSONBuilder.anJSON; import static net.cryptic_game.microservice.utils.JSONBuilder.simple; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.isA; import static org.powermock.api.mockito.PowerMockito.*; @@ -49,6 +49,8 @@ public void getChannel() { final List resultChannels = (List) ChannelEndpoints.getChannel(new JSON(new JSONObject()), UUID.randomUUID()).get("channels"); + assertEquals(channels.length, resultChannels.size()); + for (int i = 0; i < resultChannels.size(); i++) { final Channel channel = channels[i]; assertEquals(anJSON() @@ -56,7 +58,6 @@ public void getChannel() { .add("name", channel.getName()) .build(), resultChannels.get(i)); } - fail(); } @Test @@ -67,70 +68,109 @@ public void getChannelEmpty() { @Test public void getChannelMembersWithoutChannel() { - assertEquals(simple("error", "channel_not_found").toString(), ChannelEndpoints.getChannelMembers(new JSON(simple("channel", UUID.randomUUID())), UUID.randomUUID()).toString()); + assertEquals(simple("error", "channel_not_found").toString(), ChannelEndpoints.getChannelMembers(new JSON(simple("channel", UUID.randomUUID().toString())), UUID.randomUUID()).toString()); } -// @Test -// public void getChannelMembers() { -// this.channelHandler = mock(ChannelHandler.class); -// when(App.getChannelHandler()).thenReturn(this.channelHandler); -// doNothing().when(this.channelHandler).notifyAllChannelUsers(isA(ChatAction.class), isA(Channel.class), isA(String.class)); -// -// final Channel channel = new Channel("test123"); -// final User[] users = new User[2]; -// users[0] = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); -// users[1] = new User(UUID.randomUUID(), "test1", "tes1t@test1.test1", new Date(), new Date()); -// for (final User user : users) { -// channel.addUser(user); -// } -// -// System.out.println(channel); -// when(this.channelHandler.getChannelByUUID(channel.getUuid())).thenReturn(channel); -//// when(this.channelHandler.notifyAllChannelUsers(ChatAction.MEMBER_JOIN, channel, users[0].getName())); -// System.out.println(ChannelEndpoints.getChannelMembers( -// new JSON(simple("channel", channel.getUuid())), -// UUID.randomUUID())); -// final List resultUsers = (List) ChannelEndpoints.getChannelMembers( -// new JSON(simple("channel", channel.getUuid())), -// UUID.randomUUID()) -// .get("users"); -// for (int i = 0; i < resultUsers.size(); i++) { -// if (simple("name", users[i].getName()) != resultUsers.get(i)) { -// fail(); -// } -// } -// assertTrue(true); -// } + @Test + public void getChannelMembers() { + this.channelHandler = mock(ChannelHandler.class); + when(App.getChannelHandler()).thenReturn(this.channelHandler); + doNothing().when(this.channelHandler).notifyAllChannelUsers(isA(ChatAction.class), isA(Channel.class), isA(String.class)); + + final Channel channel = new Channel("test123"); + final User[] users = new User[2]; + users[0] = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + users[1] = new User(UUID.randomUUID(), "test1", "tes1t@test1.test1", new Date(), new Date()); + for (final User user : users) { + channel.addUser(user); + } + + when(this.channelHandler.getChannelByUUID(channel.getUuid())).thenReturn(channel); + + final List resultUsers = (List) ChannelEndpoints.getChannelMembers( + new JSON(simple("channel", channel.getUuid().toString())), + UUID.randomUUID()) + .get("users"); + + assertEquals(users.length, resultUsers.size()); + + for (int i = 0; i < resultUsers.size(); i++) { + assertEquals(users[i].getName(), resultUsers.get(i).get("name")); + } + } @Test public void joinChannelEmptyUser() { - assertEquals(simple("error", "user_not_found").toString(), ChannelEndpoints.joinChannel(new JSON(simple("channel", UUID.randomUUID())), UUID.randomUUID()).toString()); + assertEquals(simple("error", "user_not_found").toString(), ChannelEndpoints.leaveChannel(new JSON(simple("channel", UUID.randomUUID())), UUID.randomUUID()).toString()); } @Test public void joinChannelEmptyChannel() { + final User user = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + when(this.microService.getUser(isA(UUID.class))).thenReturn(user); + assertEquals(simple("error", "channel_not_found").toString(), ChannelEndpoints.leaveChannel(new JSON(simple("channel", UUID.randomUUID())), user.getUUID()).toString()); + } + + @Test + public void joinChannelAlreadyJoined() { + final Channel channel = this.channelHandler.addChanel("test123"); + final User user = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + + this.channelHandler = mock(ChannelHandler.class); + when(App.getChannelHandler()).thenReturn(this.channelHandler); + doNothing().when(this.channelHandler).notifyAllChannelUsers(isA(ChatAction.class), isA(Channel.class), isA(String.class)); + when(this.channelHandler.getChannelByUUID(channel.getUuid())).thenReturn(channel); + + channel.addUser(user); + when(this.microService.getUser(isA(UUID.class))).thenReturn(user); + assertEquals(simple("success", false).toString(), ChannelEndpoints.joinChannel(new JSON(simple("channel", channel.getUuid().toString())), user.getUUID()).toString()); + } + + @Test + public void joinChannel() { + final Channel channel = this.channelHandler.addChanel("test123"); + final User user = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + when(this.microService.getUser(isA(UUID.class))).thenReturn(user); + assertEquals(simple("success", true).toString(), ChannelEndpoints.joinChannel(new JSON(simple("channel", channel.getUuid().toString())), user.getUUID()).toString()); + } + + @Test + public void leaveChannelEmptyUser() { + assertEquals(simple("error", "user_not_found").toString(), ChannelEndpoints.joinChannel(new JSON(simple("channel", UUID.randomUUID())), UUID.randomUUID()).toString()); + } + + @Test + public void leaveChannelEmptyChannel() { final User user = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); when(this.microService.getUser(isA(UUID.class))).thenReturn(user); assertEquals(simple("error", "channel_not_found").toString(), ChannelEndpoints.joinChannel(new JSON(simple("channel", UUID.randomUUID())), user.getUUID()).toString()); } -// @Test -// public void joinChannelAlreadyJoined() { -// final Channel channel = this.channelHandler.addChanel("test123"); -// final User user = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); -// when(this.microService.getUser(isA(UUID.class))).thenReturn(user); -// assertEquals(simple("success", true).toString(), ChannelEndpoints.joinChannel(new JSON(simple("channel", channel.getUuid())), user.getUUID()).toString()); -// } - -// @Test -// public void joinChannel() { -// final Channel channel = this.channelHandler.addChanel("test123"); -// final User user = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); -// when(this.microService.getUser(isA(UUID.class))).thenReturn(user); -// assertEquals(simple("success", true).toString(), ChannelEndpoints.joinChannel(new JSON(simple("channel", channel.getUuid())), user.getUUID()).toString()); -// } + @Test + public void leaveChannelWithoutUser() { + final Channel channel = this.channelHandler.addChanel("test123"); + final User user = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + + this.channelHandler = mock(ChannelHandler.class); + when(App.getChannelHandler()).thenReturn(this.channelHandler); + when(this.channelHandler.getChannelByUUID(channel.getUuid())).thenReturn(channel); + + when(this.microService.getUser(isA(UUID.class))).thenReturn(user); + assertEquals(simple("success", false).toString(), ChannelEndpoints.leaveChannel(new JSON(simple("channel", channel.getUuid().toString())), user.getUUID()).toString()); + } @Test public void leaveChannel() { + final Channel channel = this.channelHandler.addChanel("test123"); + final User user = new User(UUID.randomUUID(), "test", "test@test.test", new Date(), new Date()); + + this.channelHandler = mock(ChannelHandler.class); + when(App.getChannelHandler()).thenReturn(this.channelHandler); + doNothing().when(this.channelHandler).notifyAllChannelUsers(isA(ChatAction.class), isA(Channel.class), isA(String.class)); + when(this.channelHandler.getChannelByUUID(channel.getUuid())).thenReturn(channel); + + channel.addUser(user); + when(this.microService.getUser(isA(UUID.class))).thenReturn(user); + assertEquals(simple("success", true).toString(), ChannelEndpoints.leaveChannel(new JSON(simple("channel", channel.getUuid().toString())), user.getUUID()).toString()); } }