Skip to content

Commit

Permalink
Break out readInputStream from Main.java to RespInputParser.java #99 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kappsegla authored Feb 6, 2024
2 parents 1c250a1 + e3c700a commit bf4a184
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/main/java/org/fungover/haze/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -45,7 +51,8 @@ public static void main(String[] args) {
List<String> inputList = new ArrayList<>();

String firstReading = input.readLine();
readInputStream(input, inputList, firstReading);

readInputStream(input,inputList,firstReading);

clientAuthenticated = authenticateClient(auth, isPasswordSet, client, inputList, clientAuthenticated);

Expand Down Expand Up @@ -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<String> inputList, HazeList hazeList) {
Expand Down Expand Up @@ -116,6 +125,7 @@ public static String executeCommand(HazeDatabase hazeDatabase, List<String> inpu
};
}


private static void readInputStream(BufferedReader input, List<String> inputList, String firstReading) throws
IOException {
logger.debug("readInputStream: {} {} {}", () -> input, () -> inputList, () -> firstReading);
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/fungover/haze/RespInputParser.java
Original file line number Diff line number Diff line change
@@ -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<String> 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));
}
}
}
17 changes: 17 additions & 0 deletions src/test/java/org/fungover/haze/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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"));
}
}
52 changes: 52 additions & 0 deletions src/test/java/org/fungover/haze/RespInputParserTest.java
Original file line number Diff line number Diff line change
@@ -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<String> expectedOutput = Arrays.asList("Hello", "World", "Redis");

BufferedReader input = new BufferedReader(new StringReader(inputString));
List<String> actualOutput = new ArrayList<>();
respInputParser.readInputStream(input, actualOutput, input.readLine());

assertEquals(expectedOutput, actualOutput);
}

@Test
void testReadInputStreamSimple() throws IOException {
String inputString = "+OK\r\n";
List<String> expectedOutput = Arrays.asList("+OK");

BufferedReader input = new BufferedReader(new StringReader(inputString));
List<String> 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<String> expectedOutput = Arrays.asList("-ERR", "unknown", "command");

BufferedReader input = new BufferedReader(new StringReader(inputString));
List<String> actualOutput = new ArrayList<>();
respInputParser.readInputStream(input, actualOutput, input.readLine());

assertEquals(expectedOutput, actualOutput);
}

}

0 comments on commit bf4a184

Please sign in to comment.