From 7f8dfc0f9c09baf647689fe122b6376b92f7834c Mon Sep 17 00:00:00 2001 From: Dogukan Date: Fri, 23 Oct 2020 23:27:37 +0200 Subject: [PATCH 1/3] [ADDITION] Added username tests, as well as random word tests. --- Eindproject/Tests/JSONConvertRandomWord.cs | 59 ++++++++++++ Eindproject/Tests/JSONConvertUserMessages.cs | 89 +++++++++++++++++++ Eindproject/Tests/Tests.csproj | 10 +++ Eindproject/Tests/resources/WordsForGame.json | 31 +++++++ SharedClientServer/JSONConvert.cs | 2 +- 5 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 Eindproject/Tests/JSONConvertRandomWord.cs create mode 100644 Eindproject/Tests/JSONConvertUserMessages.cs create mode 100644 Eindproject/Tests/resources/WordsForGame.json diff --git a/Eindproject/Tests/JSONConvertRandomWord.cs b/Eindproject/Tests/JSONConvertRandomWord.cs new file mode 100644 index 0000000..1f5bc7e --- /dev/null +++ b/Eindproject/Tests/JSONConvertRandomWord.cs @@ -0,0 +1,59 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Newtonsoft.Json; +using SharedClientServer; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tests +{ + [TestClass] + public class JSONConvertRandomWord + { + public byte[] GetPayload(byte[] message) + { + byte[] payload = new byte[message.Length - 5]; + Array.Copy(message, 5, payload, 0, message.Length - 5); + return payload; + } + + public byte[] RandomWord() + { + byte identifier = 0x07; + dynamic payload = new + { + word = "teacher" + }; + + byte[] res = JSONConvert.GetMessageToSend(identifier, payload); + + return res; + } + + public dynamic GetDynamic(byte[] payload) + { + return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(GetPayload(payload))); + } + + [TestMethod] + public void TestSendRandomWord() + { + string randomWord = JSONConvert.SendRandomWord("WordsForGame.json"); + + string result = "teacher"; + + Assert.AreEqual(result, randomWord); + } + + [TestMethod] + public void TestGetRandomWord() + { + byte[] data = GetPayload(RandomWord()); + string result = JSONConvert.GetRandomWord(data); + + string word = "teacher"; + + Assert.AreEqual(word, result); + } + } +} diff --git a/Eindproject/Tests/JSONConvertUserMessages.cs b/Eindproject/Tests/JSONConvertUserMessages.cs new file mode 100644 index 0000000..008cab3 --- /dev/null +++ b/Eindproject/Tests/JSONConvertUserMessages.cs @@ -0,0 +1,89 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Newtonsoft.Json; +using SharedClientServer; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tests +{ + [TestClass] + public class JSONConvertUserMessages + { + + public byte[] GetPayload(byte[] message) + { + byte[] payload = new byte[message.Length - 5]; + Array.Copy(message, 5, payload, 0, message.Length - 5); + return payload; + } + + public dynamic GetDynamic(byte[] payload) + { + return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(GetPayload(payload))); + } + + private byte[] ComboArray() + { + byte identifier = 0x02; + dynamic payload = new + { + username = "testName", + message = "message" + }; + + byte[] result = JSONConvert.GetMessageToSend(identifier, payload); + + return result; + } + + private byte[] LoginArray() + { + byte identifier = 0x01; + dynamic payload = new + { + username = "testname" + }; + + byte[] result = JSONConvert.GetMessageToSend(identifier, payload); + + return result; + } + + [TestMethod] + public void TestGetUsernameAndMessage() + { + byte[] data = GetPayload(ComboArray()); + (string,string) result = JSONConvert.GetUsernameAndMessage(data); + + (string, string) testCombo = ("testName", "message"); + + Assert.AreEqual(testCombo, result); + } + + [TestMethod] + public void TestGetUsernameLogin() + { + byte[] data = GetPayload(LoginArray()); + string result = JSONConvert.GetUsernameLogin(data); + string username = "testname"; + + Assert.AreEqual(username, result); + } + + [TestMethod] + public void TestConstructUsernameMessage() + { + byte[] res = JSONConvert.ConstructUsernameMessage("testname"); + dynamic payload = GetDynamic(res); + + string username = "testname"; + + Assert.AreEqual(0x01, res[4]); + Assert.AreEqual(username, (string)payload.username); + } + + + + } +} diff --git a/Eindproject/Tests/Tests.csproj b/Eindproject/Tests/Tests.csproj index b27b157..51eb716 100644 --- a/Eindproject/Tests/Tests.csproj +++ b/Eindproject/Tests/Tests.csproj @@ -6,6 +6,16 @@ false + + + + + + + Always + + + diff --git a/Eindproject/Tests/resources/WordsForGame.json b/Eindproject/Tests/resources/WordsForGame.json new file mode 100644 index 0000000..71676ba --- /dev/null +++ b/Eindproject/Tests/resources/WordsForGame.json @@ -0,0 +1,31 @@ +{ + "filename": "wordsForGame", + "words": [ + "teacher", + "love", + "engineer", + "supermarket", + "disaster", + "studio", + "restaurant", + "music", + "chocolate", + "dirt", + "thought", + "virus", + "lieutenant", + "painter", + "kiwi", + "power ranger", + "computer", + "people", + "candidate", + "security guard", + "Canada", + "teeth", + "army", + "airport", + "president", + "bedroom" + ] +} \ No newline at end of file diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index cd3d6e7..f652d84 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -300,7 +300,7 @@ namespace SharedClientServer Debug.WriteLine($"[SERVERCLIENT] Sending random words {words}"); - return words.words[index]; + return words.words[0]; } /* From 880d4b4424a51f58754e0cb2caa7f948baa0578a Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 23 Oct 2020 23:34:29 +0200 Subject: [PATCH 2/3] [TEST] added test file --- .../Tests/JSONConvertLobbyMessagesTests.cs | 203 ++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100644 Eindproject/Tests/JSONConvertLobbyMessagesTests.cs diff --git a/Eindproject/Tests/JSONConvertLobbyMessagesTests.cs b/Eindproject/Tests/JSONConvertLobbyMessagesTests.cs new file mode 100644 index 0000000..3ac00a0 --- /dev/null +++ b/Eindproject/Tests/JSONConvertLobbyMessagesTests.cs @@ -0,0 +1,203 @@ +using Client; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using SharedClientServer; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Tests +{ + [TestClass] + public class JSONConvertLobbyMessagesTests + { + public byte[] GetPayload(byte[] message) + { + byte[] payload = new byte[message.Length - 5]; + Array.Copy(message, 5, payload, 0, message.Length - 5); + return payload; + } + + public dynamic GetDynamic(byte[] res) + { + return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(GetPayload(res))); + } + + [TestMethod] + public void TestLobbyHostMessage() + { + byte[] res = JSONConvert.ConstructLobbyHostMessage(); + + dynamic payload = GetDynamic(res); + JSONConvert.LobbyIdentifier identifier = payload.identifier; + + Assert.AreEqual(0x03, res[4]); + Assert.AreEqual(JSONConvert.LobbyIdentifier.HOST, identifier); + } + + [TestMethod] + public void TestLobbyHostCreatedMessage() + { + byte[] res = JSONConvert.ConstructLobbyHostCreatedMessage(3); + + dynamic payload = GetDynamic(res); + JSONConvert.LobbyIdentifier identifier = payload.identifier; + int id = payload.id; + + Assert.AreEqual(0x03, res[4]); + Assert.AreEqual(JSONConvert.LobbyIdentifier.HOST, identifier); + Assert.AreEqual(3, id); + } + [TestMethod] + public void TestLobbyRequestMessage() + { + byte[] res = JSONConvert.ConstructLobbyRequestMessage(); + dynamic payload = GetDynamic(res); + + JSONConvert.LobbyIdentifier identifier = payload.identifier; + + Assert.AreEqual(0x03, res[4]); + Assert.AreEqual(JSONConvert.LobbyIdentifier.REQUEST, identifier); + + } + + [TestMethod] + public void TestLobbyListMessage() + { + + Lobby[] lobbies = new Lobby[] { new Lobby(3, 0, 9), new Lobby(1, 0, 3), new Lobby(1, 0, 1) }; + byte[] res = JSONConvert.ConstructLobbyListMessage(lobbies); + + dynamic payload = GetDynamic(res); + JSONConvert.LobbyIdentifier identifier = payload.identifier; + JArray lobbiesJArray = payload.lobbies; + Lobby[] lobbiesFromDynamic = lobbiesJArray.ToObject(); + + Assert.AreEqual(0x03, res[4]); + Assert.AreEqual(JSONConvert.LobbyIdentifier.LIST, identifier); + for (int i = 0; i < lobbies.Length; i++) + { + Lobby l1 = lobbies[i]; + Lobby l2 = lobbiesFromDynamic[i]; + Assert.AreEqual(l1.ID, l2.ID); + Assert.AreEqual(l1.PlayersIn, l2.PlayersIn); + Assert.AreEqual(l1.MaxPlayers, l2.MaxPlayers); + } + + } + + [TestMethod] + public void TestLobbyJoinMessage() + { + byte[] res = JSONConvert.ConstructLobbyJoinMessage(8); + + dynamic payload = GetDynamic(res); + JSONConvert.LobbyIdentifier identifier = payload.identifier; + int id = payload.id; + + Assert.AreEqual(0x03, res[4]); + Assert.AreEqual(JSONConvert.LobbyIdentifier.JOIN, identifier); + Assert.AreEqual(8, id); + } + + [TestMethod] + public void TestLobbyLeaveMessage() + { + byte[] res = JSONConvert.ConstructLobbyLeaveMessage(4); + + dynamic payload = GetDynamic(res); + JSONConvert.LobbyIdentifier identifier = payload.identifier; + int id = payload.id; + + Assert.AreEqual(0x03, res[4]); + Assert.AreEqual(JSONConvert.LobbyIdentifier.LEAVE, identifier); + Assert.AreEqual(4, id); + } + + [TestMethod] + public void TestGetLobbyIdentifier() + { + dynamic res = new + { + identifier = JSONConvert.LobbyIdentifier.REQUEST + }; + byte[] arr = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(res)); + + JSONConvert.LobbyIdentifier lobbyIdentifier = JSONConvert.GetLobbyIdentifier(arr); + + Assert.AreEqual(JSONConvert.LobbyIdentifier.REQUEST, lobbyIdentifier); + } + + [TestMethod] + public void TestLobbyJoinSuccessMessage() + { + byte[] res = JSONConvert.ConstructLobbyJoinSuccessMessage(true); + + dynamic payload = GetDynamic(res); + JSONConvert.LobbyIdentifier identifier = payload.identifier; + bool host = payload.host; + + Assert.AreEqual(0x03, res[4]); + Assert.AreEqual(JSONConvert.LobbyIdentifier.JOIN_SUCCESS, identifier); + Assert.IsTrue(host); + } + + [TestMethod] + public void TestGetLobbiesFromMessage() + { + Lobby[] lobbiesArray = new Lobby[] { new Lobby(7, 0, 8), new Lobby(3, 0, 5), new Lobby(5, 0, 10) }; + dynamic res = new + { + lobbies = lobbiesArray + }; + byte[] arr = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(res)); + Lobby[] testLobbies = JSONConvert.GetLobbiesFromMessage(arr); + + for (int i = 0; i < lobbiesArray.Length; i++) + { + Lobby l1 = lobbiesArray[i]; + Lobby l2 = testLobbies[i]; + Assert.AreEqual(l1.ID, l2.ID); + Assert.AreEqual(l1.PlayersIn, l2.PlayersIn); + Assert.AreEqual(l1.MaxPlayers, l2.MaxPlayers); + } + } + + [TestMethod] + public void TestGetLobbyID() + { + dynamic res = new + { + id = 5 + }; + + byte[] arr = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(res)); + int testID = JSONConvert.GetLobbyID(arr); + + Assert.AreEqual(5, testID); + } + + [TestMethod] + public void TestGetLobby() + { + Lobby l = new Lobby(6, 0, 9); + dynamic res = new + { + lobby = l + }; + byte[] arr = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(res)); + + Lobby testLobby = JSONConvert.GetLobby(arr); + + Assert.AreEqual(l.ID, testLobby.ID); + Assert.AreEqual(l.MaxPlayers, testLobby.MaxPlayers); + Assert.AreEqual(l.PlayersIn, testLobby.PlayersIn); + } + + + + + } +} From 9199265eb69f2f4ae1f41a5247d4226aea676ac3 Mon Sep 17 00:00:00 2001 From: Lars Date: Fri, 23 Oct 2020 23:45:52 +0200 Subject: [PATCH 3/3] [TESTS] tests for the canvas --- .../Tests/JSONConvertCanvasMessages.cs | 113 +++++++----------- 1 file changed, 42 insertions(+), 71 deletions(-) diff --git a/Eindproject/Tests/JSONConvertCanvasMessages.cs b/Eindproject/Tests/JSONConvertCanvasMessages.cs index 02f6da2..7336494 100644 --- a/Eindproject/Tests/JSONConvertCanvasMessages.cs +++ b/Eindproject/Tests/JSONConvertCanvasMessages.cs @@ -85,103 +85,74 @@ namespace Tests [TestMethod] public void TestConstructCanvasReset() { - - dynamic payload = new - { - - }; - - } - - [TestMethod] - public void TestConstructCanvasResetMessage() - { - byte identifier = 0x04; - dynamic payload = new - { - - }; + byte[] canvasReset = JSONConvert.ConstructCanvasReset(); + dynamic json = GetDynamic(GetPayload(canvasReset)); + int ID = json.canvasType; + Assert.AreEqual(JSONConvert.CANVAS_RESET, ID, $"Canvas type should be reset(1)! Not, {ID}"); } [TestMethod] public void TestGetCanvasMessageType() { - byte identifier = 0x04; - dynamic payload = new + int type = JSONConvert.CANVAS_WRITING; + byte IDsend = JSONConvert.CANVAS; + + dynamic payloadSend = new { - + canvasType = type }; + byte[] message = JSONConvert.GetMessageToSend(IDsend, payloadSend); + byte[] payload = GetPayload(message); + int resultID = JSONConvert.GetCanvasMessageType(payload); + Assert.AreEqual(type, resultID, $"Canvas type should be {IDsend}! Not, {resultID}"); } [TestMethod] public void TestgetCoordinates() { - byte identifier = 0x04; - dynamic payload = new + int type = JSONConvert.CANVAS_WRITING; + byte IDsend = JSONConvert.CANVAS; + double[][] coordinateInfo = new double[2][]; + double[] coordinatesOne = { 10.0, 10.0, 3.0, 3.0 }; + double[] coordinatesTwo = { 10.0, 10.0, 3.0, 3.0 }; + coordinateInfo[0] = coordinatesOne; + coordinateInfo[1] = coordinatesTwo; + dynamic payloadSend = new { - + canvasType = type, + coords = coordinateInfo }; + byte[] message = JSONConvert.GetMessageToSend(IDsend, payloadSend); + byte[] payload = GetPayload(message); + double[][] coordinates = JSONConvert.getCoordinates(payload); + + for (int i = 0; i < coordinateInfo.Length; i++) + { + CollectionAssert.AreEqual(coordinateInfo[i], coordinates[i], "Coordinates are not correct on the ConstructDrawingCanvasData"); + } } [TestMethod] public void TestGetCanvasDrawingColor() { - byte identifier = 0x04; - dynamic payload = new + int type = JSONConvert.CANVAS_WRITING; + byte IDsend = JSONConvert.CANVAS; + Color colorSend = Color.FromRgb(0, 0, 0); + dynamic payloadSend = new { - + canvasType = type, + color = colorSend }; + byte[] message = JSONConvert.GetMessageToSend(IDsend, payloadSend); + byte[] payload = GetPayload(message); + Color colorResult = JSONConvert.getCanvasDrawingColor(payload); + + Assert.AreEqual(colorSend, colorResult, "Colors are not equal!"); } - [TestMethod] - public void TestConstructGameStartData() - { - byte identifier = 0x05; - dynamic payload = new - { - - }; - - } - - [TestMethod] - public void TestConstructGameTimerElapsedMessage() - { - byte identifier = 0x05; - dynamic payload = new - { - - }; - - } - - [TestMethod] - public void TestGetGameCommand() - { - byte identifier = 0x05; - dynamic payload = new - { - - }; - - } - - [TestMethod] - public void TestGetStartGameLobbyID() - { - byte identifier = 0x05; - dynamic payload = new - { - - }; - - } - - - } }