From 3b085ee7b47352683fae2bb7fa3230784e2d0bdf Mon Sep 17 00:00:00 2001 From: jmanuelosunamoreno Date: Wed, 14 Aug 2024 17:39:11 -0400 Subject: [PATCH] add unit test --- .../edu/ohio/ais/rundeck/HttpBuilderTest.java | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/test/java/edu/ohio/ais/rundeck/HttpBuilderTest.java diff --git a/src/test/java/edu/ohio/ais/rundeck/HttpBuilderTest.java b/src/test/java/edu/ohio/ais/rundeck/HttpBuilderTest.java new file mode 100644 index 0000000..91189a6 --- /dev/null +++ b/src/test/java/edu/ohio/ais/rundeck/HttpBuilderTest.java @@ -0,0 +1,78 @@ +package edu.ohio.ais.rundeck; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static edu.ohio.ais.rundeck.HttpBuilder.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class HttpBuilderTest { + + @Test + public void testGetStringOption() { + Map options = new HashMap<>(); + + String result = getStringOption(options, "missingKey"); + assertNull("Expected null when key is missing", result); + + options.put("nullKey", null); + result = getStringOption(options, "nullKey"); + assertNull("Expected null when value is null", result); + + options.put("key", "value"); + result = getStringOption(options, "key"); + assertEquals("Expected the value associated with the key", "value", result); + } + + @Test + public void testGetStringOptionWithDefault() { + Map options = new HashMap<>(); + + String result = getStringOption(options, "missingKey", "defaultValue"); + assertEquals("Expected the default value when key is missing", "defaultValue", result); + + options.put("nullKey", null); + result = getStringOption(options, "nullKey", "defaultValue"); + assertEquals("Expected the default value when value is null", "defaultValue", result); + + options.put("key", "value"); + result = getStringOption(options, "key", "defaultValue"); + assertEquals("Expected the value associated with the key", "value", result); + } + + @Test + public void testGetIntOption() { + Map options = new HashMap<>(); + + Integer result = getIntOption(options, "missingKey", 42); + assertEquals("Expected the default value when key is missing", Integer.valueOf(42), result); + + options.put("nullKey", null); + result = getIntOption(options, "nullKey", 42); + assertEquals("Expected the default value when value is null", Integer.valueOf(42), result); + + options.put("key", 99); + result = getIntOption(options, "key", 42); + assertEquals("Expected the value associated with the key", Integer.valueOf(99), result); + } + + @Test + public void testGetBooleanOption() { + Map options = new HashMap<>(); + + Boolean result = getBooleanOption(options, "missingKey", true); + assertEquals("Expected the default value when key is missing", Boolean.TRUE, result); + + options.put("nullKey", null); + result = getBooleanOption(options, "nullKey", true); + assertEquals("Expected the default value when value is null", Boolean.TRUE, result); + + options.put("key", false); + result = getBooleanOption(options, "key", true); + assertEquals("Expected the value associated with the key", Boolean.FALSE, result); + } + +}