diff --git a/pom.xml b/pom.xml index 4f37f2aaef6c006c1cdabb857ae7787128d71766..a1fe802d25b92f3c95961e9719ba2f9c6b28a779 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,13 @@ <scope>test</scope> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-junit-jupiter</artifactId> + <version>5.8.0</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> @@ -75,6 +82,12 @@ <artifactId>gson</artifactId> <version>2.10.1</version> </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>RELEASE</version> + <scope>test</scope> + </dependency> </dependencies> diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/GsonHandler.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/GsonHandler.java index 0ad125eb6de25b18fc0e19a56be6dd587b34d974..308b553a68422590c8a0619baa336e91ee513bb2 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/GsonHandler.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/GsonHandler.java @@ -42,8 +42,8 @@ public class GsonHandler { } catch (Exception e) { log.info(e); if(type == MapType.COREMAP){ - log.info("GSON - Loading Community-Maps from JSON failed!"); - throw new GSONException("Loading Community-Maps from JSON failed!"); + log.info("GSON - Loading Core-Maps from JSON failed!"); + throw new GSONException("Loading Core-Maps from JSON failed!"); } else{ log.info("GSON - Loading Community-Maps from JSON failed!"); diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Persistence.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Persistence.java index 1614d296ebe37c87a3177c4cb758c1944dcc1c61..5ced7271a2692745c102175fb9295869041bd9dd 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Persistence.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Persistence.java @@ -16,7 +16,7 @@ public class Persistence { private static final Logger log = LogManager.getLogger(Persistence.class); private static final Persistence persistenceSingleton = new Persistence(); private final GsonHandler gsonHandler = new GsonHandler(); - private OracleDB db = new OracleDB(); //evtl. Methoden von OracleDB static machen und von GSON Handler + protected OracleDB db = new OracleDB(); //evtl. Methoden von OracleDB static machen und von GSON Handler ; "protected" for testing purposes to showcase Mockito tests private ArrayList<MapData> coreMaps; private ArrayList<MapData> communityMaps; private PlayerStatistics statistics; @@ -34,10 +34,30 @@ public class Persistence { return persistenceSingleton; } + public ArrayList<MapData> getCoreMaps() { + return coreMaps; + } + + public ArrayList<MapData> getCommunityMaps() { + return communityMaps; + } + public PlayerStatistics getStatistics() { return statistics; } + public PlayerAccount getAccount() { + return account; + } + + public AppSettings getSettings() { + return settings; + } + + public ArrayList<MapInfo> getCommunityMapsListRemote() { + return communityMapsListRemote; + } + public void loadCoreMaps() throws DatabaseException { try { coreMaps = gsonHandler.loadMaps(MapType.COREMAP); diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/Parser.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/Parser.java index 1c3337c5195038b6962de8d14d018d26f1391182..a98f5bc2a3e7a345b72ba6633b7e328733e02212 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/Parser.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/Parser.java @@ -323,7 +323,7 @@ public class Parser { } } - public void playerStatsValid(PlayerStatistics stats) throws ParserException{ + public static void playerStatsValid(PlayerStatistics stats) throws ParserException{ if(stats.getGamesLost() < 0){ throw new ParserException("Player statistics data corrupted - Minimum value for lost games is 0!"); diff --git a/src/test/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/PersistenceTest.java b/src/test/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/PersistenceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..79b599da6c201b66ffe5d1c21293f44b9ee244e4 --- /dev/null +++ b/src/test/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/PersistenceTest.java @@ -0,0 +1,48 @@ +package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes; + +import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Exceptions.DatabaseException; +import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Exceptions.SQLException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.Before; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class PersistenceTest { + + private static final Logger log = LogManager.getLogger(PersistenceTest.class); + Persistence persistenceSingleton; + + OracleDB inst = new OracleDB(); + + @Before + public void setup() throws SQLException { + persistenceSingleton = Persistence.getInstance(); + persistenceSingleton.db = inst; + ArrayList<MapData> maps = new ArrayList<MapData>(); + maps.add(new MapData("09a02b54d05b5b7ebc29a4383ca12d3dda846b72", "Arena1", 18, 18, "12 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 12")); + maps.add(new MapData("0ab15557ab6dc4be60dfe6a9b0288bac3036bd97", "Arena2", 18, 18, "12 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 12 12 1 1 1 1 1 1 1 5 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 12")); + when(inst.getCoreMaps()).thenReturn(maps); + } + + @Test + void updateCoreMaps() throws RuntimeException, DatabaseException { + persistenceSingleton.updateCoreMaps(); + assertEquals("09a02b54d05b5b7ebc29a4383ca12d3dda846b72", persistenceSingleton.getCoreMaps().get(0).getMapID()); + assertEquals("0ab15557ab6dc4be60dfe6a9b0288bac3036bd97", persistenceSingleton.getCoreMaps().get(1).getMapID()); + } + + @Test + void loadPlayerStatistics() { + } + + + + +} \ No newline at end of file diff --git a/src/test/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/ParserTest.java b/src/test/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/ParserTest.java index 264966b68ee8797695bbc7b92d95b0dbd4edc2cf..0ffdfca98e607df423dd54c2abcc281146e293b4 100644 --- a/src/test/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/ParserTest.java +++ b/src/test/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/ParserTest.java @@ -1,6 +1,7 @@ package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Utilities; import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Exceptions.ParserException; +import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.PlayerStatistics; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -12,12 +13,12 @@ class ParserTest { @ValueSource(strings = {"Player1", "Player2", "0123456789012345678901234567890123456789"}) - void usernameValidCorrect(String test){ + void usernameValid(String test){ assertDoesNotThrow(() -> Parser.usernameValid(test)); } @Test - void usernameValidIncorrect() { + void usernameInvalid() { ParserException testMe = assertThrows(ParserException.class, () -> Parser.usernameValid("lol")); assertTrue(testMe.getMessage().contains("Username too short! Min length is 4 characters.")); @@ -32,12 +33,12 @@ class ParserTest { @ValueSource(strings = {"pw123456", "Passwort", "0123456789012345678901234567890123456789"}) - void passwordValidCorrect(String test){ + void passwordValid(String test){ assertDoesNotThrow(() -> Parser.passwordValid(test)); } @Test - void passwordValidIncorrect() { + void passwordInvalid() { ParserException testMe = assertThrows(ParserException.class, () -> Parser.passwordValid("lol")); assertTrue(testMe.getMessage().contains("Password too short! Min length is 4 characters.")); @@ -52,12 +53,12 @@ class ParserTest { @ValueSource(strings = {"012345678901234567890123456789", "de_dust", "Ziba-Tower"}) - void mapNameValidCorrect(String test){ + void mapNameValid(String test){ assertDoesNotThrow(() -> Parser.mapNameValid(test)); } @Test - void mapNameValidIncorrect() { + void mapNameInvalid() { ParserException testMe = assertThrows(ParserException.class, () -> Parser.mapNameValid("lol")); assertTrue(testMe.getMessage().contains("Map-Name too short! Min length is 4 characters.")); @@ -72,12 +73,12 @@ class ParserTest { @ValueSource(strings = {"a593cafd1d061f0f463a2d2051bf4718aaaf5c48", "e559d8fbb53b333f5839cb3c6c0c515395afe344", "a593cafd1d061f0f463a2d2051bf4718aaaf5c48"}) - void mapIDValidCorrect(String test){ + void mapIDValid(String test){ assertDoesNotThrow(() -> Parser.sha1HexHashValid(test)); } @Test - void mapIDValidIncorrect() { + void mapIDInvalid() { ParserException testMe = assertThrows(ParserException.class, () -> Parser.sha1HexHashValid("01234567890123456789012345678901234567890")); assertTrue(testMe.getMessage().contains("Map-ID length not correct. Must have length of 40 characters!")); @@ -94,12 +95,12 @@ class ParserTest { "0.0.0.0", "12.12.12.1", "localhost"}) - void ipAddressValidCorrect(String test){ + void ipAddressValid(String test){ assertDoesNotThrow(() -> Parser.ipAddressValid(test)); } @Test - void ipAddressValidIncorrect() { + void ipAddressInvalid() { ParserException testMe = assertThrows(ParserException.class, () -> Parser.ipAddressValid("255.255.255.2555")); assertTrue(testMe.getMessage().contains("IP-Address too long. Must have maximum length of 15 characters including octet dividers (.) !")); @@ -142,12 +143,12 @@ class ParserTest { @ValueSource(strings = {"12 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1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1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 12", "12 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 12 12 1 1 1 1 1 1 1 8 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 12 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 12"}) - void mapDataValidCorrect(String test){ + void mapDataValid(String test){ assertDoesNotThrow(() -> Parser.mapDataValid(test)); } @Test - void mapDataValidIncorrect() { + void mapDataInvalid() { ParserException testMe = assertThrows(ParserException.class, () -> Parser.mapDataValid("12 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 12 12 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 12")); assertTrue(testMe.getMessage().contains("Map-Data corrupted - mapData String must not exceed length of 967 chars including spaces for 18x18 Tiles map!")); @@ -223,4 +224,31 @@ class ParserTest { assertTrue(testMe.getMessage().contains("Map-Data corrupted - Player spawn bottom right must use walkable tile!")); } + @Test + void statsValid(){ + PlayerStatistics stats = new PlayerStatistics(0, 0, 0, 0, 0, 0); + assertDoesNotThrow(() -> Parser.playerStatsValid(stats)); + } + + @Test + void statsInvalid(){ + ParserException testMe = assertThrows(ParserException.class, () -> Parser.playerStatsValid(new PlayerStatistics(-1, 0, 0, 0, 0, 0))); + assertTrue(testMe.getMessage().contains("Player statistics data corrupted - Minimum value for lost games is 0!")); + + testMe = assertThrows(ParserException.class, () -> Parser.playerStatsValid(new PlayerStatistics(0, -1, 0, 0, 0, 0))); + assertTrue(testMe.getMessage().contains("Player statistics data corrupted - Minimum value for won games is 0!")); + + testMe = assertThrows(ParserException.class, () -> Parser.playerStatsValid(new PlayerStatistics(0, 0, -1, 0, 0, 0))); + assertTrue(testMe.getMessage().contains("Player statistics data corrupted - Minimum value for kills is 0!")); + + testMe = assertThrows(ParserException.class, () -> Parser.playerStatsValid(new PlayerStatistics(0, 0, 0, -1, 0, 0))); + assertTrue(testMe.getMessage().contains("Player statistics data corrupted - Minimum value for deaths is 0!")); + + testMe = assertThrows(ParserException.class, () -> Parser.playerStatsValid(new PlayerStatistics(0, 0, 0, 0, -1, 0))); + assertTrue(testMe.getMessage().contains("Player statistics data corrupted - Minimum value for destroyed blocks is 0!")); + + testMe = assertThrows(ParserException.class, () -> Parser.playerStatsValid(new PlayerStatistics(0, 0, 0, 0, 0, -1))); + assertTrue(testMe.getMessage().contains("Player statistics data corrupted - Minimum value for ingame time is 0 seconds!")); + } + } \ No newline at end of file