Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add commands in executeCommands #134

Merged
merged 4 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/org/fungover/haze/HazeDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.concurrent.locks.ReentrantLock;

public class HazeDatabase {

private static final String ARGUMENT_ERROR = "-ERR wrong number of arguments for command\r\n";
private final Map<String, String> database;
private final Lock lock;

Expand All @@ -17,23 +17,27 @@ public HazeDatabase() {
this.lock = new ReentrantLock();
}

public String set(String key, String value) {
public String set(List<String> inputList) {
if (inputList.size() != 3)
return ARGUMENT_ERROR;
String key = inputList.get(1);
String value = inputList.get(2);
lock.lock();
try {
//add code for setting value when key exists
database.put(key, value);

} finally {
lock.unlock();
}
return "+OK\r\n";
}

public String get(String key) {
public String get(List<String> inputList) {
if (inputList.size() != 2)
return ARGUMENT_ERROR;
lock.lock();
try {
if (database.containsKey(key)) {
var value = database.get(key);
if (database.containsKey(inputList.get(1))) {
var value = database.get(inputList.get(1));
return "$" + value.length() + "\r\n" + value + "\r\n";
} else return "$-1\r\n";
} finally {
Expand Down Expand Up @@ -68,14 +72,13 @@ public String exists(List<String> keys) {
}
} finally {
lock.unlock();

}
return ":" + numberOfKeys + "\r\n";
}

public String setNX(List<String> inputList) {
if (inputList.size() != 3)
return "-ERR wrong number of arguments for command\r\n";
return ARGUMENT_ERROR;
String key = inputList.get(1);
String value = inputList.get(2);

Expand Down Expand Up @@ -104,9 +107,8 @@ public Map<String, String> copy() {
}
return shallowCopy;
}

public String ping(List<String> messageList) {

public String ping(List<String> messageList) {
if (messageList.size() == 1)
return "+PONG\r\n";
else return "$" + (messageList.get(1)).length() + "\r\n" + messageList.get(1) + "\r\n";
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/fungover/haze/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ public static String executeCommand(HazeDatabase hazeDatabase, List<String> inpu
String command = inputList.get(0).toUpperCase();

return switch (command) {
case "SET" -> hazeDatabase.set(inputList);
case "GET" -> hazeDatabase.get(inputList);
case "DEL" -> hazeDatabase.delete(inputList.subList(1, inputList.size()));
case "PING" -> hazeDatabase.ping(inputList);
case "SETNX" -> hazeDatabase.setNX(inputList);
case "EXISTS" -> hazeDatabase.exists(inputList.subList(1, inputList.size()));
case "SAVE" -> SaveFile.writeOnFile(hazeDatabase.copy());
case "DEL" -> hazeDatabase.delete(inputList.subList(1, inputList.size()));
default -> "-ERR unknown command\r\n";
};
}


private static void readInputStream(BufferedReader input, List<String> inputList, String firstReading) throws
IOException {
Log4j2.debug("readInputStream: " + input + " " + inputList + " " + firstReading);
Expand Down
36 changes: 26 additions & 10 deletions src/test/java/org/fungover/haze/HazeDatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -29,13 +30,13 @@ void callingDeleteReturnsNumberOfSuccessfullyRemovedKeys() {
void callingDeleteRemovesTheSpecifiedKey() {
testDatabase.setNX(List.of("SETNX", "1", "thisWillBeRemoved"));
testDatabase.delete(Collections.singletonList("1"));
assertThat(testDatabase.get("1")).isEqualTo("$-1\r\n");
assertThat(testDatabase.get(List.of("", "1"))).isEqualTo("$-1\r\n");
}

@Test
void callingGetReturnsTheCorrectValueIfItExists() {
testDatabase.setNX(List.of("SETNX", "someKey", "someValue"));
assertThat(testDatabase.get("someKey")).isEqualTo("$9\r\nsomeValue\r\n");
assertThat(testDatabase.get(List.of("", "someKey"))).isEqualTo("$9\r\nsomeValue\r\n");
}

@Test
Expand Down Expand Up @@ -93,41 +94,56 @@ void testPingResponseShouldBeSameAsValue() {

@Test
void testSetWithValidKeyValuePair() {
String result = testDatabase.set("key", "value");
String result = testDatabase.set(List.of("", "key", "value"));
assertEquals("+OK\r\n", result);
}

@Test
void testSetWithNullValue() {
String result = testDatabase.set("key", null);
String result = testDatabase.set(Arrays.asList("", "key", null));
assertEquals("+OK\r\n", result);
}

@Test
void testGetWithValidKey() {
testDatabase.set("key", "value");
String result = testDatabase.get("key");
testDatabase.set(List.of("", "key", "value"));
String result = testDatabase.get(List.of("", "key"));
assertEquals("$5\r\nvalue\r\n", result);
}

@Test
void testGetWithInvalidKey() {
String result = testDatabase.get("invalidKey");
String result = testDatabase.get(List.of("", "invalidKey"));
assertEquals("$-1\r\n", result);
}

@Test
void testGetWithNullKey() {
String result = testDatabase.get(null);
String result = testDatabase.get(Arrays.asList("", null));
assertEquals("$-1\r\n", result);
}

@Test
void testThatIfYouPutKeyAndValueYouGetOutAMap() {
testDatabase.set("1", "test");
testDatabase.set("2", "hast");
testDatabase.set(List.of("", "1", "test"));
testDatabase.set(List.of("", "2", "hast"));
assertThat(testDatabase.copy())
.containsEntry("1", "test")
.containsEntry("2", "hast");
}

@Test
void callingSetWithWrongNumberOfArgumentsResultsInErrorMessage() {
assertThat(testDatabase.set(List.of("", "key"))).isEqualTo("-ERR wrong number of arguments for command\r\n");
}

@Test
void callingSetNXWithWrongNumberOfArgumentsResultsInErrorMessage() {
assertThat(testDatabase.setNX(List.of("", "key"))).isEqualTo("-ERR wrong number of arguments for command\r\n");
}

@Test
void callingGetWithWrongNumberOfArgumentsResultsInErrorMessage() {
assertThat(testDatabase.get(List.of(""))).isEqualTo("-ERR wrong number of arguments for command\r\n");
}
}
39 changes: 36 additions & 3 deletions src/test/java/org/fungover/haze/MainTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.fungover.haze;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;


class MainTest {
HazeDatabase database = new HazeDatabase();

Expand All @@ -31,8 +34,38 @@ void callExecuteCommandWithAnEmptyPingShouldReturnPong() {
}

@Test
void callExecuteCommandWithPingAndMessageShouldReturnTheMessage() {
assertThat(Main.executeCommand(database, List.of("Ping", "test message")))
.isEqualTo("$12\r\ntest message\r\n");
void callExecuteCommandWithPingAndMessageShouldReturnTheMessageAsBulkString() {
assertThat(Main.executeCommand(database, List.of("Ping", "test message"))).isEqualTo("$12\r\ntest message\r\n");
}

@Test
void callExecuteCommandWithGetAndKeyShouldReturnTheValueAsBulkString() {
Main.executeCommand(database, List.of("SET", "theKey", "theValue"));
assertThat(Main.executeCommand(database, List.of("GET", "theKey"))).isEqualTo("$8\r\ntheValue\r\n");
}

@Test
void callExecuteCommandWithDelAndExistingKeyShouldReturnOne() {
Main.executeCommand(database, List.of("SET", "theKey", "theValue"));
assertThat(Main.executeCommand(database, List.of("Del", "theKey"))).isEqualTo(":1\r\n");
}

@Test
void callExecuteCommandWithExistsAndOneExistingKeyShouldReturnOne() {
Main.executeCommand(database, List.of("SET", "theKey", "theValue"));
assertThat(Main.executeCommand(database, List.of("Exists", "theKey", "secondKey"))).isEqualTo(":1\r\n");
}

@Test
void callExecuteCommandWithSaveShouldReturnOK() {
Main.executeCommand(database, List.of("SAVE"));
assertThat(SaveFile.writeOnFile(database.copy())).isEqualTo("+OK\r\n");
}

@ParameterizedTest
@ValueSource(strings = {"SET", "SETNX"})
void callingExecuteCommandWithWrongNumberOfArgumentsResultsInErrorMessage(String command) {
assertThat(Main.executeCommand(database, List.of(command, "key")))
.isEqualTo("-ERR wrong number of arguments for command\r\n");
}
}