diff --git a/Eindproject/Tests/JSONConvertCanvasMessages.cs b/Eindproject/Tests/JSONConvertCanvasMessages.cs index d8e6493..fe219b0 100644 --- a/Eindproject/Tests/JSONConvertCanvasMessages.cs +++ b/Eindproject/Tests/JSONConvertCanvasMessages.cs @@ -1,140 +1,161 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using SharedClientServer; -using System; -using System.Collections.Generic; -using System.Runtime.CompilerServices; -using System.Text; -using System.Windows.Media; - -namespace Tests -{ - [TestClass] - public class JSONConvertCanvasMessages - { - //Helper method for the tests - 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) - { - dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload)); - return json; - } - - [TestMethod] - public void TestConstructCanvasDataSend() - { - int type = JSONConvert.CANVAS_WRITING; - 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; - Color color = Color.FromRgb(0, 0, 0); - - byte[] message = JSONConvert.ConstructCanvasDataSend(type, coordinateInfo, color); - byte[] payload = GetPayload(message); - dynamic json = GetDynamic(payload); - int ID = json.canvasType; - JArray coorArray = json.coords; - double[][] coordinates = coorArray.ToObject(); - Color colorResult = json.color; - - Assert.AreEqual(0x04, message[4]); - Assert.AreEqual(type, ID, "The canvas type message is not correct on the ConstructDrawingCanvasData"); - for (int i = 0; i < coordinateInfo.Length; i++) - { - CollectionAssert.AreEqual(coordinateInfo[i], coordinates[i], "Coordinates are not correct on the ConstructDrawingCanvasData"); - } - Assert.AreEqual(color, colorResult, "color is not correct on the ConstructDrawingCanvasData"); - } - - [TestMethod] - public void TestConstructDrawingCanvasData() - { - 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; - Color color = Color.FromRgb(0, 0, 0); - - byte[] message = JSONConvert.ConstructDrawingCanvasData(coordinateInfo, color); - byte[] payload = GetPayload(message); - dynamic json = GetDynamic(payload); - int ID = json.canvasType; - JArray coorArray = json.coords; - double[][] coordinates = coorArray.ToObject(); - Color colorResult = json.color; - - Assert.AreEqual(0x04, message[4]); - Assert.AreEqual(JSONConvert.CANVAS_WRITING, ID, "The canvas type message is not correct on the ConstructDrawingCanvasData"); - for (int i = 0; i < coordinateInfo.Length; i++) - { - CollectionAssert.AreEqual(coordinateInfo[i], coordinates[i], "Coordinates are not correct on the ConstructDrawingCanvasData"); - } - Assert.AreEqual(color, colorResult, "color is not correct on the ConstructDrawingCanvasData"); - } - - [TestMethod] - public void TestConstructCanvasReset() - { - - dynamic payload = new - { - - }; - - } - - [TestMethod] - public void TestConstructCanvasResetMessage() - { - byte identifier = 0x04; - dynamic payload = new - { - - }; - - } - - [TestMethod] - public void TestGetCanvasMessageType() - { - byte identifier = 0x04; - dynamic payload = new - { - - }; - - } - - [TestMethod] - public void TestgetCoordinates() - { - byte identifier = 0x04; - dynamic payload = new - { - - }; - - } - - [TestMethod] - public void TestGetCanvasDrawingColor() - { - byte identifier = 0x04; - dynamic payload = new - { - - }; - - } - } -} +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using SharedClientServer; +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Text; +using System.Windows.Media; + +namespace Tests +{ + [TestClass] + public class JSONConvertCanvasMessages + { + //Helper method for the tests + 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) + { + dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload)); + return json; + } + + [TestMethod] + public void TestConstructCanvasDataSend() + { + int type = JSONConvert.CANVAS_WRITING; + 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; + Color color = Color.FromRgb(0, 0, 0); + + byte[] message = JSONConvert.ConstructCanvasDataSend(type, coordinateInfo, color); + byte[] payload = GetPayload(message); + dynamic json = GetDynamic(payload); + int ID = json.canvasType; + JArray coorArray = json.coords; + double[][] coordinates = coorArray.ToObject(); + Color colorResult = json.color; + + Assert.AreEqual(0x04, message[4]); + Assert.AreEqual(type, ID, "The canvas type message is not correct on the ConstructDrawingCanvasData"); + for (int i = 0; i < coordinateInfo.Length; i++) + { + CollectionAssert.AreEqual(coordinateInfo[i], coordinates[i], "Coordinates are not correct on the ConstructDrawingCanvasData"); + } + Assert.AreEqual(color, colorResult, "color is not correct on the ConstructDrawingCanvasData"); + } + + [TestMethod] + public void TestConstructDrawingCanvasData() + { + 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; + Color color = Color.FromRgb(0, 0, 0); + + byte[] message = JSONConvert.ConstructDrawingCanvasData(coordinateInfo, color); + byte[] payload = GetPayload(message); + dynamic json = GetDynamic(payload); + int ID = json.canvasType; + JArray coorArray = json.coords; + double[][] coordinates = coorArray.ToObject(); + Color colorResult = json.color; + + Assert.AreEqual(0x04, message[4]); + Assert.AreEqual(JSONConvert.CANVAS_WRITING, ID, "The canvas type message is not correct on the ConstructDrawingCanvasData"); + for (int i = 0; i < coordinateInfo.Length; i++) + { + CollectionAssert.AreEqual(coordinateInfo[i], coordinates[i], "Coordinates are not correct on the ConstructDrawingCanvasData"); + } + Assert.AreEqual(color, colorResult, "color is not correct on the ConstructDrawingCanvasData"); + } + + [TestMethod] + public void TestConstructCanvasReset() + { + 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() + { + 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() + { + + 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() + { + + 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!"); + } + + } +} 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); + } + + + + + } +} 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]; } /*