diff --git a/build.gradle b/build.gradle index aa26ed057..07b34e07e 100644 --- a/build.gradle +++ b/build.gradle @@ -63,6 +63,7 @@ dependencies { implementation "org.codehaus.groovy:groovy-all:3.0.19" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" testImplementation "org.junit.jupiter:junit-jupiter:5.10.2" + testImplementation "org.assertj:assertj-core:3.26.3" } test { diff --git a/src/main/java/core/file/xml/XMLArenaParser.java b/src/main/java/core/file/xml/XMLArenaParser.java index c5c73ee26..2265c2866 100644 --- a/src/main/java/core/file/xml/XMLArenaParser.java +++ b/src/main/java/core/file/xml/XMLArenaParser.java @@ -1,127 +1,167 @@ -// %2675225597:de.hattrickorganizer.logik.xml% -/* - * XMLArenaParser.java - * - * Created on 5. Juni 2004, 15:40 - */ package core.file.xml; import core.util.HOLogger; - -import java.util.Map; - import org.w3c.dom.Document; import org.w3c.dom.Element; -/** - * - * @author thetom - */ +import java.util.Map; + public class XMLArenaParser { - /** - * Utility class - private constructor enforces noninstantiability. - */ - private XMLArenaParser() { - } - - public static Map parseArenaFromString(String str) { - return parseDetails(XMLManager.parseString(str)); - } - - private static Map parseDetails(Document doc) { - Map map = new SafeInsertMap(); - - if (doc == null) { - return map; - } - - try { - Element root = doc.getDocumentElement(); - Element ele = (Element) root.getElementsByTagName("FetchedDate") - .item(0); - map.put("FetchedDate", (XMLManager.getFirstChildNodeValue(ele))); - - // Root wechseln - root = (Element) root.getElementsByTagName("Arena").item(0); - ele = (Element) root.getElementsByTagName("ArenaID").item(0); - map.put("ArenaID", (XMLManager.getFirstChildNodeValue(ele))); - ele = (Element) root.getElementsByTagName("ArenaName").item(0); - map.put("ArenaName", (XMLManager.getFirstChildNodeValue(ele))); - - Element tmpRoot = (Element) root.getElementsByTagName("Team").item( - 0); - ele = (Element) tmpRoot.getElementsByTagName("TeamID").item(0); - map.put("TeamID", (XMLManager.getFirstChildNodeValue(ele))); - ele = (Element) tmpRoot.getElementsByTagName("TeamName").item(0); - map.put("TeamName", (XMLManager.getFirstChildNodeValue(ele))); - - tmpRoot = (Element) root.getElementsByTagName("League").item(0); - ele = (Element) tmpRoot.getElementsByTagName("LeagueID").item(0); - map.put("LeagueID", (XMLManager.getFirstChildNodeValue(ele))); - ele = (Element) tmpRoot.getElementsByTagName("LeagueName").item(0); - map.put("LeagueName", (XMLManager.getFirstChildNodeValue(ele))); - - tmpRoot = (Element) root.getElementsByTagName("Region").item(0); - ele = (Element) tmpRoot.getElementsByTagName("RegionID").item(0); - map.put("RegionID", (XMLManager.getFirstChildNodeValue(ele))); - ele = (Element) tmpRoot.getElementsByTagName("RegionName").item(0); - map.put("RegionName", (XMLManager.getFirstChildNodeValue(ele))); - - tmpRoot = (Element) root.getElementsByTagName("CurrentCapacity") - .item(0); - ele = (Element) tmpRoot.getElementsByTagName("RebuiltDate").item(0); - - if (XMLManager.getAttributeValue(ele, "Available").trim() - .equalsIgnoreCase("true")) { - map.put("RebuiltDate", (XMLManager.getFirstChildNodeValue(ele))); - } - - ele = (Element) tmpRoot.getElementsByTagName("Terraces").item(0); - map.put("Terraces", (XMLManager.getFirstChildNodeValue(ele))); - ele = (Element) tmpRoot.getElementsByTagName("Basic").item(0); - map.put("Basic", (XMLManager.getFirstChildNodeValue(ele))); - ele = (Element) tmpRoot.getElementsByTagName("Roof").item(0); - map.put("Roof", (XMLManager.getFirstChildNodeValue(ele))); - ele = (Element) tmpRoot.getElementsByTagName("VIP").item(0); - map.put("VIP", (XMLManager.getFirstChildNodeValue(ele))); - ele = (Element) tmpRoot.getElementsByTagName("Total").item(0); - map.put("Total", (XMLManager.getFirstChildNodeValue(ele))); - - tmpRoot = (Element) root.getElementsByTagName("ExpandedCapacity") - .item(0); - - if (XMLManager.getAttributeValue(ele, "Available").trim() - .equalsIgnoreCase("true")) { - map.put("isExpanding", "1"); - ele = (Element) tmpRoot.getElementsByTagName("ExpansionDate") - .item(0); - map.put("ExpansionDate", - (XMLManager.getFirstChildNodeValue(ele))); - ele = (Element) tmpRoot.getElementsByTagName("Terraces") - .item(0); - map.put("ExTerraces", (XMLManager.getFirstChildNodeValue(ele))); - ele = (Element) tmpRoot.getElementsByTagName("Basic").item(0); - map.put("ExBasic", (XMLManager.getFirstChildNodeValue(ele))); - ele = (Element) tmpRoot.getElementsByTagName("Roof").item(0); - map.put("ExRoof", (XMLManager.getFirstChildNodeValue(ele))); - ele = (Element) tmpRoot.getElementsByTagName("VIP").item(0); - map.put("ExVIP", (XMLManager.getFirstChildNodeValue(ele))); - ele = (Element) tmpRoot.getElementsByTagName("Total").item(0); - map.put("ExTotal", (XMLManager.getFirstChildNodeValue(ele))); - } else { - map.put("isExpanding", "0"); - map.put("ExpansionDate", "0"); - map.put("ExTerraces", "0"); - map.put("ExBasic", "0"); - map.put("ExRoof", "0"); - map.put("ExVIP", "0"); - map.put("ExTotal", "0"); - } - } catch (Exception e) { - HOLogger.instance().log(XMLArenaParser.class, e); - } - - return map; - } + private static final String ELEMENT_NAME_FETCHED_DATE = "FetchedDate"; + private static final String ELEMENT_NAME_ARENA = "Arena"; + private static final String ELEMENT_NAME_ARENA_ID = "ArenaID"; + private static final String ELEMENT_NAME_ARENA_NAME = "ArenaName"; + private static final String ELEMENT_NAME_TEAM = "Team"; + private static final String ELEMENT_NAME_TEAM_ID = "TeamID"; + private static final String ELEMENT_NAME_TEAM_NAME = "TeamName"; + private static final String ELEMENT_NAME_LEAGUE = "League"; + private static final String ELEMENT_NAME_LEAGUE_ID = "LeagueID"; + private static final String ELEMENT_NAME_LEAGUE_NAME = "LeagueName"; + private static final String ELEMENT_NAME_REGION = "Region"; + private static final String ELEMENT_NAME_REGION_ID = "RegionID"; + private static final String ELEMENT_NAME_REGION_NAME = "RegionName"; + private static final String ELEMENT_NAME_CURRENT_CAPACITY = "CurrentCapacity"; + private static final String ELEMENT_NAME_CURRENT_CAPACITY_REBUILT_DATE = "RebuiltDate"; + + private static final String ELEMENT_NAME_EXPANDED_CAPACITY = "ExpandedCapacity"; + private static final String ELEMENT_NAME_EXPANDED_CAPACITY_EXPANSION_DATE = "ExpansionDate"; + + private static final String ATTRIBUTE_NAME_CAPACITY_AVAILABLE = "Available"; + private static final String ELEMENT_NAME_CAPACITY_TERRACES = "Terraces"; + private static final String ELEMENT_NAME_CAPACITY_BASIC = "Basic"; + private static final String ELEMENT_NAME_CAPACITY_ROOF = "Roof"; + private static final String ELEMENT_NAME_CAPACITY_VIP = "VIP"; + private static final String ELEMENT_NAME_CAPACITY_TOTAL = "Total"; + + private static final String PROPERTY_NAME_FETCHED_DATE = "FetchedDate"; + private static final String PROPERTY_NAME_ARENA_ID = "ArenaID"; + private static final String PROPERTY_NAME_ARENA_NAME = "ArenaName"; + private static final String PROPERTY_NAME_TEAM_ID = "TeamID"; + private static final String PROPERTY_NAME_TEAM_NAME = "TeamName"; + private static final String PROPERTY_NAME_LEAGUE_ID = "LeagueID"; + private static final String PROPERTY_NAME_LEAGUE_NAME = "LeagueName"; + private static final String PROPERTY_NAME_REGION_ID = "RegionID"; + private static final String PROPERTY_NAME_REGION_NAME = "RegionName"; + + private static final String PROPERTY_NAME_CURRENT_CAPACITY_REBUILT_DATE = "RebuiltDate"; + private static final String PROPERTY_NAME_CURRENT_CAPACITY_TERRACES = "Terraces"; + private static final String PROPERTY_NAME_CURRENT_CAPACITY_BASIC = "Basic"; + private static final String PROPERTY_NAME_CURRENT_CAPACITY_ROOF = "Roof"; + private static final String PROPERTY_NAME_CURRENT_CAPACITY_VIP = "VIP"; + private static final String PROPERTY_NAME_CURRENT_CAPACITY_TOTAL = "Total"; + + private static final String PROPERTY_NAME_EXPANDING_CAPACITY_IS_EXPANDING = "isExpanding"; + private static final String PROPERTY_NAME_EXPANDING_CAPACITY_EXPANSION_DATE = "ExpansionDate"; + private static final String PROPERTY_NAME_EXPANDING_CAPACITY_TERRACES = "ExTerraces"; + private static final String PROPERTY_NAME_EXPANDING_CAPACITY_BASIC = "ExBasic"; + private static final String PROPERTY_NAME_EXPANDING_CAPACITY_ROOF = "ExRoof"; + private static final String PROPERTY_NAME_EXPANDING_CAPACITY_VIP = "ExVIP"; + private static final String PROPERTY_NAME_EXPANDING_CAPACITY_TOTAL = "ExTotal"; + + private static final String PROPERTY_VALUE_ZERO = "0"; + private static final String PROPERTY_VALUE_ONE = "1"; + + private XMLArenaParser() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + } + + public static Map parseArenaFromString(String str) { + return parseDetails(XMLManager.parseString(str)); + } + + private static Map parseDetails(Document doc) { + Map map = new SafeInsertMap(); + + if (doc == null) { + return map; + } + + try { + Element root = doc.getDocumentElement(); + Element element = (Element) root.getElementsByTagName(ELEMENT_NAME_FETCHED_DATE).item(0); + map.put(PROPERTY_NAME_FETCHED_DATE, XMLManager.getFirstChildNodeValue(element)); + + // Root wechseln + root = (Element) root.getElementsByTagName(ELEMENT_NAME_ARENA).item(0); + element = (Element) root.getElementsByTagName(ELEMENT_NAME_ARENA_ID).item(0); + map.put(PROPERTY_NAME_ARENA_ID, XMLManager.getFirstChildNodeValue(element)); + element = (Element) root.getElementsByTagName(ELEMENT_NAME_ARENA_NAME).item(0); + map.put(PROPERTY_NAME_ARENA_NAME, XMLManager.getFirstChildNodeValue(element)); + + Element tmpRoot = (Element) root.getElementsByTagName(ELEMENT_NAME_TEAM).item( + 0); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_TEAM_ID).item(0); + map.put(PROPERTY_NAME_TEAM_ID, XMLManager.getFirstChildNodeValue(element)); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_TEAM_NAME).item(0); + map.put(PROPERTY_NAME_TEAM_NAME, XMLManager.getFirstChildNodeValue(element)); + + tmpRoot = (Element) root.getElementsByTagName(ELEMENT_NAME_LEAGUE).item(0); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_LEAGUE_ID).item(0); + map.put(PROPERTY_NAME_LEAGUE_ID, XMLManager.getFirstChildNodeValue(element)); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_LEAGUE_NAME).item(0); + map.put(PROPERTY_NAME_LEAGUE_NAME, XMLManager.getFirstChildNodeValue(element)); + + tmpRoot = (Element) root.getElementsByTagName(ELEMENT_NAME_REGION).item(0); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_REGION_ID).item(0); + map.put(PROPERTY_NAME_REGION_ID, XMLManager.getFirstChildNodeValue(element)); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_REGION_NAME).item(0); + map.put(PROPERTY_NAME_REGION_NAME, XMLManager.getFirstChildNodeValue(element)); + + tmpRoot = (Element) root.getElementsByTagName(ELEMENT_NAME_CURRENT_CAPACITY).item(0); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_CURRENT_CAPACITY_REBUILT_DATE).item(0); + + final boolean rebuiltDateAvailable = getXmlAttributeAsBoolean(element, ATTRIBUTE_NAME_CAPACITY_AVAILABLE); + if (rebuiltDateAvailable) { + map.put(PROPERTY_NAME_CURRENT_CAPACITY_REBUILT_DATE, XMLManager.getFirstChildNodeValue(element)); + } + + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_CAPACITY_TERRACES).item(0); + map.put(PROPERTY_NAME_CURRENT_CAPACITY_TERRACES, XMLManager.getFirstChildNodeValue(element)); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_CAPACITY_BASIC).item(0); + map.put(PROPERTY_NAME_CURRENT_CAPACITY_BASIC, XMLManager.getFirstChildNodeValue(element)); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_CAPACITY_ROOF).item(0); + map.put(PROPERTY_NAME_CURRENT_CAPACITY_ROOF, XMLManager.getFirstChildNodeValue(element)); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_CAPACITY_VIP).item(0); + map.put(PROPERTY_NAME_CURRENT_CAPACITY_VIP, XMLManager.getFirstChildNodeValue(element)); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_CAPACITY_TOTAL).item(0); + map.put(PROPERTY_NAME_CURRENT_CAPACITY_TOTAL, XMLManager.getFirstChildNodeValue(element)); + + tmpRoot = (Element) root.getElementsByTagName(ELEMENT_NAME_EXPANDED_CAPACITY).item(0); + + final boolean expandedCapacityAvailable = getXmlAttributeAsBoolean(tmpRoot, ATTRIBUTE_NAME_CAPACITY_AVAILABLE); + if (expandedCapacityAvailable) { + map.put(PROPERTY_NAME_EXPANDING_CAPACITY_IS_EXPANDING, PROPERTY_VALUE_ONE); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_EXPANDED_CAPACITY_EXPANSION_DATE).item(0); + map.put(PROPERTY_NAME_EXPANDING_CAPACITY_EXPANSION_DATE, XMLManager.getFirstChildNodeValue(element)); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_CAPACITY_TERRACES).item(0); + map.put(PROPERTY_NAME_EXPANDING_CAPACITY_TERRACES, XMLManager.getFirstChildNodeValue(element)); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_CAPACITY_BASIC).item(0); + map.put(PROPERTY_NAME_EXPANDING_CAPACITY_BASIC, XMLManager.getFirstChildNodeValue(element)); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_CAPACITY_ROOF).item(0); + map.put(PROPERTY_NAME_EXPANDING_CAPACITY_ROOF, XMLManager.getFirstChildNodeValue(element)); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_CAPACITY_VIP).item(0); + map.put(PROPERTY_NAME_EXPANDING_CAPACITY_VIP, XMLManager.getFirstChildNodeValue(element)); + element = (Element) tmpRoot.getElementsByTagName(ELEMENT_NAME_CAPACITY_TOTAL).item(0); + map.put(PROPERTY_NAME_EXPANDING_CAPACITY_TOTAL, XMLManager.getFirstChildNodeValue(element)); + } else { + map.put(PROPERTY_NAME_EXPANDING_CAPACITY_IS_EXPANDING, PROPERTY_VALUE_ZERO); + map.put(PROPERTY_NAME_EXPANDING_CAPACITY_EXPANSION_DATE, PROPERTY_VALUE_ZERO); + map.put(PROPERTY_NAME_EXPANDING_CAPACITY_TERRACES, PROPERTY_VALUE_ZERO); + map.put(PROPERTY_NAME_EXPANDING_CAPACITY_BASIC, PROPERTY_VALUE_ZERO); + map.put(PROPERTY_NAME_EXPANDING_CAPACITY_ROOF, PROPERTY_VALUE_ZERO); + map.put(PROPERTY_NAME_EXPANDING_CAPACITY_VIP, PROPERTY_VALUE_ZERO); + map.put(PROPERTY_NAME_EXPANDING_CAPACITY_TOTAL, PROPERTY_VALUE_ZERO); + } + } catch (Exception e) { + HOLogger.instance().log(XMLArenaParser.class, e); + } + + return map; + } + + @SuppressWarnings(value = "SameParameterValue") + private static boolean getXmlAttributeAsBoolean(Element element, String attributeName) { + return Boolean.parseBoolean(XMLManager.getAttributeValue(element, attributeName).trim()); + } } diff --git a/src/main/java/core/util/ResourceUtils.java b/src/main/java/core/util/ResourceUtils.java new file mode 100644 index 000000000..f9f6741c9 --- /dev/null +++ b/src/main/java/core/util/ResourceUtils.java @@ -0,0 +1,25 @@ +package core.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.stream.Collectors; + +public final class ResourceUtils { + + private ResourceUtils() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + } + + public static String getResourceFileAsString(String fileName) throws IOException { + ClassLoader classLoader = ClassLoader.getSystemClassLoader(); + try (InputStream is = classLoader.getResourceAsStream(fileName)) { + if (is == null) return null; + try (InputStreamReader isr = new InputStreamReader(is); + BufferedReader reader = new BufferedReader(isr)) { + return reader.lines().collect(Collectors.joining(System.lineSeparator())); + } + } + } +} \ No newline at end of file diff --git a/src/test/java/core/file/xml/XMLArenaParserTest.java b/src/test/java/core/file/xml/XMLArenaParserTest.java new file mode 100644 index 000000000..ba177d3f8 --- /dev/null +++ b/src/test/java/core/file/xml/XMLArenaParserTest.java @@ -0,0 +1,85 @@ +package core.file.xml; + +import core.util.ResourceUtils; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +class XMLArenaParserTest { + + @Test + void parseArenaFromString_noExpansion() throws IOException { + // given + final var content = ResourceUtils.getResourceFileAsString("arenaDetails_noExpansion.xml"); + + // FileName, Version and UserID are currently not read + final var expected = Map.ofEntries( + Map.entry("FetchedDate", "2024-08-21 01:13:12"), + Map.entry("ArenaID", "2345678"), + Map.entry("ArenaName", "ArenaName"), + Map.entry("TeamID", "3456789"), + Map.entry("TeamName", "TeamName"), + Map.entry("LeagueID", "3"), + Map.entry("LeagueName", "LeagueName"), + Map.entry("RegionID", "227"), + Map.entry("RegionName", "RegionName"), + Map.entry("RebuiltDate", "2024-08-13 00:05:26"), + Map.entry("Terraces", "8000"), + Map.entry("Basic", "3000"), + Map.entry("Roof", "1792"), + Map.entry("VIP", "282"), + Map.entry("Total", "13074"), + Map.entry("isExpanding", "0"), + Map.entry("ExpansionDate", "0"), + Map.entry("ExTerraces", "0"), + Map.entry("ExBasic", "0"), + Map.entry("ExRoof", "0"), + Map.entry("ExVIP", "0"), + Map.entry("ExTotal", "0")); + + // when + final var result = XMLArenaParser.parseArenaFromString(content); + + // then + assertThat(result).isEqualTo(expected); + } + + @Test + void parseArenaFromString_withExpansion() throws IOException { + // given + final var content = ResourceUtils.getResourceFileAsString("arenaDetails_withExpansion.xml"); + + // FileName, Version and UserID are currently not read + final var expected = Map.ofEntries( + Map.entry("FetchedDate", "2024-08-21 11:08:22"), + Map.entry("ArenaID", "2345678"), + Map.entry("ArenaName", "ArenaName"), + Map.entry("TeamID", "3456789"), + Map.entry("TeamName", "TeamName"), + Map.entry("LeagueID", "3"), + Map.entry("LeagueName", "LeagueName"), + Map.entry("RegionID", "227"), + Map.entry("RegionName", "RegionName"), + Map.entry("Terraces", "8000"), + Map.entry("Basic", "3000"), + Map.entry("Roof", "1792"), + Map.entry("VIP", "282"), + Map.entry("Total", "13074"), + Map.entry("isExpanding", "1"), + Map.entry("ExpansionDate", "2024-08-30 11:07:17"), + Map.entry("ExTerraces", "226"), + Map.entry("ExBasic", "234"), + Map.entry("ExRoof", "1138"), + Map.entry("ExVIP", "78"), + Map.entry("ExTotal", "1676")); + + // when + final var result = XMLArenaParser.parseArenaFromString(content); + + // then + assertThat(result).isEqualTo(expected).doesNotContainKey("RebuiltDate"); + } +} \ No newline at end of file diff --git a/src/test/resources/arenaDetails_noExpansion.xml b/src/test/resources/arenaDetails_noExpansion.xml new file mode 100644 index 000000000..bffebc156 --- /dev/null +++ b/src/test/resources/arenaDetails_noExpansion.xml @@ -0,0 +1,32 @@ + + + arenadetails.xml + 1.3 + 1234567 + 2024-08-21 01:13:12 + + 2345678 + ArenaName + + 3456789 + TeamName + + + 3 + LeagueName + + + 227 + RegionName + + + 2024-08-13 00:05:26 + 8000 + 3000 + 1792 + 282 + 13074 + + + + \ No newline at end of file diff --git a/src/test/resources/arenaDetails_withExpansion.xml b/src/test/resources/arenaDetails_withExpansion.xml new file mode 100644 index 000000000..89387725c --- /dev/null +++ b/src/test/resources/arenaDetails_withExpansion.xml @@ -0,0 +1,39 @@ + + + arenadetails.xml + 1.3 + 1234567 + 2024-08-21 11:08:22 + + 2345678 + ArenaName + + 3456789 + TeamName + + + 3 + LeagueName + + + 227 + RegionName + + + + 8000 + 3000 + 1792 + 282 + 13074 + + + 2024-08-30 11:07:17 + 226 + 234 + 1138 + 78 + 1676 + + + \ No newline at end of file