✅ Add test for “ConfigData”
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 10 Feb 2023 09:12:23 +0000 (10:12 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 10 Feb 2023 09:12:23 +0000 (10:12 +0100)
src/main/java/net/pterodactylus/fcp/ConfigData.java
src/test/java/net/pterodactylus/fcp/ConfigDataTest.java [new file with mode: 0644]

index d81b9ee..998b5aa 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.pterodactylus.fcp;
 
+import static java.lang.Boolean.parseBoolean;
+
 /**
  * A “ConfigData” message contains various aspects of the node’s configuration.
  *
@@ -110,7 +112,7 @@ public class ConfigData extends BaseMessage {
         * @return The expert flag of the option
         */
        public boolean getExpertFlag(String option) {
-               return Boolean.valueOf(getField("expertFlag." + option));
+               return parseBoolean(getField("expertFlag." + option));
        }
 
        /**
@@ -121,7 +123,7 @@ public class ConfigData extends BaseMessage {
         * @return The force-write flag of the given option
         */
        public boolean getForceWriteFlag(String option) {
-               return Boolean.valueOf(getField("forceWriteFlag." + option));
+               return parseBoolean(getField("forceWriteFlag." + option));
        }
 
 }
diff --git a/src/test/java/net/pterodactylus/fcp/ConfigDataTest.java b/src/test/java/net/pterodactylus/fcp/ConfigDataTest.java
new file mode 100644 (file)
index 0000000..ff3596f
--- /dev/null
@@ -0,0 +1,86 @@
+package net.pterodactylus.fcp;
+
+import java.util.function.Function;
+
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+
+/**
+ * Unit test for {@link ConfigData}.
+ */
+public class ConfigDataTest {
+
+       @Test
+       public void configDataCanExposeCurrentOptionValue() {
+               testExposedMethodReturningString("current", configData::getCurrent);
+       }
+
+       @Test
+       public void configDataCanExposeShortDescription() {
+               testExposedMethodReturningString("shortDescription", configData::getShortDescription);
+       }
+
+       @Test
+       public void configDataCanExposeLongDescription() {
+               testExposedMethodReturningString("longDescription", configData::getLongDescription);
+       }
+
+       @Test
+       public void configDataCanExposeDataType() {
+               testExposedMethodReturningString("dataType", configData::getDataType);
+       }
+
+       @Test
+       public void configDataCanExposeDefault() {
+               testExposedMethodReturningString("default", configData::getDefault);
+       }
+
+       @Test
+       public void configDataCanExposeSortOrder() {
+               receivedMessage.setField("sortOrder.testOption", "123");
+               assertThat(configData.getSortOrder("testOption"), equalTo(123));
+       }
+
+       @Test
+       public void sortOrderIsReturnedAsMinus1IfGivenANonNumericValue() {
+               receivedMessage.setField("sortOrder.testOption", "not-a-number");
+               assertThat(configData.getSortOrder("testOption"), equalTo(-1));
+       }
+
+       @Test
+       public void expertFlagIsReturnedCorrectly() {
+               receivedMessage.setField("expertFlag.testOption", "true");
+               assertThat(configData.getExpertFlag("testOption"), equalTo(true));
+       }
+
+       @Test
+       public void expertFlagIsFalseIfGivenANonBooleanValue() {
+               receivedMessage.setField("expertFlag.testOption", "not-a-boolean");
+               assertThat(configData.getExpertFlag("testOption"), equalTo(false));
+       }
+
+       @Test
+       public void forceWriteFlagIsReturnedCorrectly() {
+               receivedMessage.setField("forceWriteFlag.testOption", "true");
+               assertThat(configData.getForceWriteFlag("testOption"), equalTo(true));
+       }
+
+       @Test
+       public void forceWriteFlagIsFalseIfGivenANonBooleanValue() {
+               receivedMessage.setField("forceWriteFlag.testOption", "not-a-boolean");
+               assertThat(configData.getForceWriteFlag("testOption"), equalTo(false));
+       }
+
+       private void testExposedMethodReturningString(String prefixInMap, Function<String, String> getter) {
+               String option = "testOption" + Math.random();
+               String value = "testValue" + Math.random();
+               receivedMessage.setField(prefixInMap  + "." + option, value);
+               assertThat(getter.apply(option), equalTo(value));
+       }
+
+       private final FcpMessage receivedMessage = new FcpMessage("");
+       private final ConfigData configData = new ConfigData(receivedMessage);
+
+}