diff --git a/src/main/java/com/github/archarithms/App.java b/src/main/java/com/github/archarithms/App.java index dcb6723..dd13e52 100644 --- a/src/main/java/com/github/archarithms/App.java +++ b/src/main/java/com/github/archarithms/App.java @@ -1,15 +1,45 @@ package com.github.archarithms; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; +/** + * Arcarithm Coding Interview + * Christopher Eastridge + * 7/20/2022 + */ public class App { - public static String convertToTitleCase(final String inpStr) - { - return inpStr; - } - - public static String convertUnixToDateString(final Long inpUnixSeconds) - { - return String.valueOf(inpUnixSeconds); - } + /** + * Converts a string to UPPER-CASE + * + * @param inpStr The string you want to UPPER-CASE + * @return The string as UPPER-CASE + */ + public static String convertToTitleCase(final String inpStr) + { + return inpStr.toUpperCase(); + } + + /** + * Converts Unix timestamp to a human-readable format + * + * @param inpUnixSeconds seconds since epoch + * @param timeZone the time zone for the expected conversion + * @return a human-readable date format + */ + public static String convertUnixToDateString(final Long inpUnixSeconds, String timeZone) + { + // Convert seconds to milliseconds + Date date = new Date(inpUnixSeconds * 1000L); + + // Provide the format we want the resulting string in + SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy"); + + // Get this date in EST + dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone)); + + return dateFormat.format(date); + } } diff --git a/src/test/java/com/github/archarithms/test/DateStringTests.java b/src/test/java/com/github/archarithms/test/DateStringTests.java index 04e9943..e84afba 100644 --- a/src/test/java/com/github/archarithms/test/DateStringTests.java +++ b/src/test/java/com/github/archarithms/test/DateStringTests.java @@ -1,36 +1,44 @@ package com.github.archarithms.test; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - +import com.github.archarithms.App; import org.junit.jupiter.api.Test; -import com.github.archarithms.App; +import static org.junit.jupiter.api.Assertions.*; /** * DateStringTests unit tests for simple App. */ public class DateStringTests { - - /* - * UNCOMMENT THE UNIT TESTS TO RUN THEM. FEEL FREE TO ADD MORE! - */ - /** * Test the testConvertUnixToDateString method */ @Test - public void testConvertUnixToDateString() { + public void testConvertUnixToDateString_Nominal() + { String testStr = "July 4, 2017"; - assertTrue(testStr.equals(App.convertUnixToDateString(1499144400L))); + assertEquals(testStr, App.convertUnixToDateString(1499144400L, "GMT-5")); + } + + /** + * Test the testConvertUnixToDateString method for mismatched time zone + */ + @Test + public void testConvertUnixToDateString_TimeZoneMismatch() + { + String pstTimeStr = "July 3, 2017"; + String estTimeStr = "July 4, 2017"; + String convertedStr = App.convertUnixToDateString(1499144400L, "GMT-8"); + assertEquals(pstTimeStr, convertedStr); + assertNotEquals(estTimeStr, convertedStr); } /** - * Test the testNullCase method + * Test the testConvertUnixToDateString method throws an exception for null cases */ @Test - public void testNullCase() { - assertThrows(Exception.class, () -> App.convertUnixToDateString(null)); + public void testConvertUnixToDateString_Null_ThrowsException() + { + assertThrows(Exception.class, () -> App.convertUnixToDateString(null, "GMT-5")); } } diff --git a/src/test/java/com/github/archarithms/test/TitleCaseTests.java b/src/test/java/com/github/archarithms/test/TitleCaseTests.java index bd3bb2d..2a4d236 100644 --- a/src/test/java/com/github/archarithms/test/TitleCaseTests.java +++ b/src/test/java/com/github/archarithms/test/TitleCaseTests.java @@ -1,45 +1,77 @@ package com.github.archarithms.test; -import static org.junit.jupiter.api.Assertions.assertTrue; - +import com.github.archarithms.App; import org.junit.jupiter.api.Test; -import com.github.archarithms.App; +import static org.junit.jupiter.api.Assertions.*; /** * TitleCaseTests unit tests for simple App. */ public class TitleCaseTests { - - /* - * UNCOMMENT THE UNIT TESTS TO RUN THEM. FEEL FREE TO ADD MORE! - */ - /** * Test the testConvertToTitleCase method */ @Test - public void testConvertToTitleCase() { + public void testConvertToTitleCase_Nominal() + { String testStr = "Title Case"; - assertTrue(testStr.equals(App.convertToTitleCase("TITLE_CASE"))); + assertEquals("TITLE CASE", App.convertToTitleCase(testStr)); } /** - * Test the testNumbers method + * Test that upper-casing works with number literals */ @Test - public void testNumbers() { + public void testConvertToTitleCase_Numbers() + { String testStr = "Number 3"; - assertTrue(testStr.equals(App.convertToTitleCase("NUMBER_3"))); + assertEquals("NUMBER 3", App.convertToTitleCase(testStr)); + + } + + /** + * Test that upper-casing works with different characters + */ + @Test + public void testConvertToTitleCase_OtherChars() + { + String testStr = "Truth-Track"; + assertEquals("TRUTH-TRACK", App.convertToTitleCase(testStr)); + + } + + /** + * Test that upper-casing will preserve whitespace. + */ + @Test + public void testConvertToTitleCase_WhiteSpace() + { + String testStr = " test "; + assertEquals(" TEST ", App.convertToTitleCase(testStr)); + + // Verify equals isn't trimming or eliminating whitespace + assertNotEquals("TEST", App.convertToTitleCase(testStr)); + } + + /** + * Test that upper-casing works with empty String. + */ + @Test + public void testConvertToTitleCase_Empty() + { + String testStr = ""; + assertEquals("", App.convertToTitleCase(testStr)); } /** - * Test the testOtherChars method + * Test that an exception is thrown for null values. */ @Test - public void testOtherChars() { - String testStr = "Truth Track"; - assertTrue(testStr.equals(App.convertToTitleCase("TRUTH-TRACK"))); + public void testConvertToTitleCase_Null_ThrowsException() + { + String testStr = ""; + assertThrows(Exception.class, () -> App.convertToTitleCase(null)); } }