diff --git a/src/main/java/org/fungover/haze/Main.java b/src/main/java/org/fungover/haze/Main.java index 64f300d9..500e27d3 100644 --- a/src/main/java/org/fungover/haze/Main.java +++ b/src/main/java/org/fungover/haze/Main.java @@ -10,14 +10,20 @@ import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; + import java.util.Arrays; import java.util.List; + + public class Main { static boolean serverOpen = true; static Logger logger = LogManager.getLogger(Main.class); + public static void main(String[] args) { + + Initialize initialize = new Initialize(); initialize.importCliOptions(args); HazeDatabase hazeDatabase = new HazeDatabase(); @@ -45,7 +51,8 @@ public static void main(String[] args) { List inputList = new ArrayList<>(); String firstReading = input.readLine(); - readInputStream(input, inputList, firstReading); + + readInputStream(input,inputList,firstReading); clientAuthenticated = authenticateClient(auth, isPasswordSet, client, inputList, clientAuthenticated); @@ -75,9 +82,11 @@ private static void shutdown(HazeDatabase hazeDatabase) { logger.info("Shutting down...."); } - private static void printThreadDebug() { - logger.debug("ThreadID {}", () -> Thread.currentThread().threadId()); // Only for Debug - logger.debug("Is virtual Thread {}", () -> Thread.currentThread().isVirtual()); // Only for Debug + + public static void printThreadDebug() { + logger.debug("ThreadID " + Thread.currentThread().threadId()); // Only for Debug + logger.debug("Is virtual Thread " + Thread.currentThread().isVirtual()); // Only for Debug + } public static String executeCommand(HazeDatabase hazeDatabase, List inputList, HazeList hazeList) { @@ -116,6 +125,7 @@ public static String executeCommand(HazeDatabase hazeDatabase, List inpu }; } + private static void readInputStream(BufferedReader input, List inputList, String firstReading) throws IOException { logger.debug("readInputStream: {} {} {}", () -> input, () -> inputList, () -> firstReading); diff --git a/src/main/java/org/fungover/haze/RespInputParser.java b/src/main/java/org/fungover/haze/RespInputParser.java new file mode 100644 index 00000000..13ccdcff --- /dev/null +++ b/src/main/java/org/fungover/haze/RespInputParser.java @@ -0,0 +1,32 @@ +package org.fungover.haze; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +public class RespInputParser { + + RespInputParser() { + } + private static final Logger logger = LogManager.getLogger(RespInputParser.class); + + public static void readInputStream(BufferedReader input, List inputList, String firstReading) throws IOException { + if (firstReading.startsWith("*")) { + + logger.debug("readInputStream: {} {} {}", input, inputList, firstReading); + int size = Integer.parseInt(firstReading.substring(1)) * 2; + for (int i = 0; i < size; i++) { + String temp = input.readLine(); + if (!temp.contains("$")) + inputList.add(temp); + } + } else { + String[] separated = firstReading.split("\\s"); + inputList.addAll(Arrays.asList(separated)); + } + } +} diff --git a/src/test/java/org/fungover/haze/MainTest.java b/src/test/java/org/fungover/haze/MainTest.java index 67933883..b7e5d380 100644 --- a/src/test/java/org/fungover/haze/MainTest.java +++ b/src/test/java/org/fungover/haze/MainTest.java @@ -5,15 +5,21 @@ import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.ArrayList; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.fungover.haze.Main.printThreadDebug; +import static org.junit.jupiter.api.Assertions.*; class MainTest { HazeDatabase database = new HazeDatabase(); HazeList hazeList = new HazeList(database); + @Test void callingExecuteCommandWithValidNonExistingInputReturnsColonOne() { assertThat(Main.executeCommand(database, List.of("SETNX", "1", "This is a value"), hazeList)).isEqualTo(":1\r\n"); @@ -102,4 +108,15 @@ void callExecuteCommandWithLMOVEShouldReturnErrorMessageWhenListIsEmpty() { void callExecuteCommandWithLTRIMShouldReturnErrorMessageWhenKeyDoesNotExist() { assertThat(Main.executeCommand(database, List.of("LTRIM", "key", "2", "3"), hazeList)).isEqualTo("-The key is not present in the database.\r\n"); } + + @Test + void testPrintThreadDebug() { + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + + printThreadDebug(); + + assertFalse(outContent.toString().contains("ThreadID")); + assertFalse(outContent.toString().contains("Is virtual Thread")); + } } diff --git a/src/test/java/org/fungover/haze/RespInputParserTest.java b/src/test/java/org/fungover/haze/RespInputParserTest.java new file mode 100644 index 00000000..df644290 --- /dev/null +++ b/src/test/java/org/fungover/haze/RespInputParserTest.java @@ -0,0 +1,52 @@ +package org.fungover.haze; + +import org.junit.jupiter.api.Test; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class RespInputParserTest { + RespInputParser respInputParser = new RespInputParser(); + @Test + void testReadInputStreamBulk() throws IOException { + String inputString = "*3\r\n$5\r\nHello\r\n$5\r\nWorld\r\n$5\r\nRedis\r\n"; + List expectedOutput = Arrays.asList("Hello", "World", "Redis"); + + BufferedReader input = new BufferedReader(new StringReader(inputString)); + List actualOutput = new ArrayList<>(); + respInputParser.readInputStream(input, actualOutput, input.readLine()); + + assertEquals(expectedOutput, actualOutput); + } + + @Test + void testReadInputStreamSimple() throws IOException { + String inputString = "+OK\r\n"; + List expectedOutput = Arrays.asList("+OK"); + + BufferedReader input = new BufferedReader(new StringReader(inputString)); + List actualOutput = new ArrayList<>(); + respInputParser.readInputStream(input, actualOutput, input.readLine()); + + assertEquals(expectedOutput, actualOutput); + } + + @Test + void testReadInputStreamError() throws IOException { + String inputString = "-ERR unknown command\r\n"; + List expectedOutput = Arrays.asList("-ERR", "unknown", "command"); + + BufferedReader input = new BufferedReader(new StringReader(inputString)); + List actualOutput = new ArrayList<>(); + respInputParser.readInputStream(input, actualOutput, input.readLine()); + + assertEquals(expectedOutput, actualOutput); + } + +}