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

142-additional-tests-to-increase-code-coverage #207

Merged
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
2 changes: 1 addition & 1 deletion src/main/java/org/fungover/haze/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private static void shutdownClientIfNotAuthenticated(Socket client, boolean clie
}
}

private static boolean authCommandReceived(boolean isPasswordSet, List<String> inputList, boolean clientAuthenticated) {
static boolean authCommandReceived(boolean isPasswordSet, List<String> inputList, boolean clientAuthenticated) {
return isPasswordSet && !clientAuthenticated && inputList.size() == 2 && inputList.getFirst().equals("AUTH");
}
}
66 changes: 66 additions & 0 deletions src/test/java/org/fungover/haze/HazeDatabaseTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.fungover.haze;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand All @@ -8,6 +9,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class HazeDatabaseTest {

Expand All @@ -33,6 +35,14 @@ void callingDeleteRemovesTheSpecifiedKey() {
assertThat(testDatabase.get(List.of("", "1"))).isEqualTo("$-1\r\n");
}

@Test
@DisplayName("Calling delete throw IllegalArgumentException for empty key")
void callingDeleteThrowIllegalArgumentExceptionForEmptyKey() {
assertThrows(IllegalArgumentException.class, () -> {
testDatabase.delete(Collections.emptyList());
}, "No keys provided");
}

@Test
void callingGetReturnsTheCorrectValueIfItExists() {
testDatabase.setNX(List.of("SETNX", "someKey", "someValue"));
Expand Down Expand Up @@ -92,6 +102,33 @@ void testPingResponseShouldBeSameAsValue() {
assertThat(testDatabase.ping(List.of("PING", "test message"))).isEqualTo("$12\r\ntest message\r\n");
}

@Test
@DisplayName("testPing throw Exception for null message list")
void testPingThrowExceptionForNullMessageList() {

assertThrows(IllegalArgumentException.class, () -> {
testDatabase.ping(null);
}, "No message provided");
}

@Test
@DisplayName("testPing throw Exception for Empty message list")
void testPingThrowExceptionForEmptyMessageList() {

assertThrows(IllegalArgumentException.class, () -> {
testDatabase.ping(List.of());
}, "No message provided");
}

@Test
@DisplayName("testPing throw exception for too many arguments")
void testPingThrowExceptionForTooManyArguments() {

assertThrows(IllegalArgumentException.class, () -> {
testDatabase.ping(List.of("arg1", "arg2", "arg3"));
}, "Too many arguments for PING command");
}

@Test
void testSetWithValidKeyValuePair() {
String result = testDatabase.set(List.of("", "key", "value"));
Expand Down Expand Up @@ -182,8 +219,37 @@ void callingDecreaseWithKeyWithIntegerShouldDecreaseValueBy1(){
String increaseResult = testDatabase.decreaseValue(List.of("DECR","key1"));
assertThat(increaseResult).isEqualTo(":0\r\n");
assertThat(testDatabase.getValue("key1")).isEqualTo("0");
}

@Test
@DisplayName("increaseValue should return ERR message when key does not exist")
void increaseValueShouldReturnErrMessageWhenKeyDoesNotExist() {

String nonExistentKey = "nonExistentKey";
List<String> inputList = List.of("INCR", nonExistentKey);

assertThat(testDatabase.increaseValue(inputList)).isEqualTo("-ERR no such key\r\n");
}

@Test
@DisplayName("decreaseValue should return ERR message when key does not exist")
void decreaseValueShouldReturnErrMessageWhenKeyDoesNotExist() {

String nonExistentKey = "nonExistentKey";
List<String> inputList = List.of("DECR", nonExistentKey);

assertThat(testDatabase.decreaseValue(inputList)).isEqualTo("-ERR no such key\r\n");
}

@Test
@DisplayName("decreaseValue should return WRONGTYPE message when value is not Integer")
void decreaseValueShouldReturnWrongtypeMessageWhenValueIsNotInteger() {
String key = "key";
testDatabase.addValue(key, "notInteger");

assertThat(testDatabase.decreaseValue(List.of("DECR", key)))
.isEqualTo("-WRONGTYPE value is not an integer or out of range\r\n");

}

}
18 changes: 18 additions & 0 deletions src/test/java/org/fungover/haze/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.LinkedList;
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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;


Expand Down Expand Up @@ -130,4 +132,20 @@ void testPrintThreadDebug() {
assertFalse(outContent.toString().contains("ThreadID"));
assertFalse(outContent.toString().contains("Is virtual Thread"));
}

@ParameterizedTest
@CsvSource({
"true, AUTH, password, false, true",
"false, AUTH, password, false, false",
"true, SET, password, false, false",
"true, AUTH, password, true, false",
"false, AUTH, password, true, false"
})
void authCommandReceivedTest(boolean isPasswordSet, String command, String password, boolean clientAuthenticated, boolean expected) {
List<String> inputList = new LinkedList<>(List.of(command, password));

boolean result = Main.authCommandReceived(isPasswordSet, inputList, clientAuthenticated);

assertEquals(expected, result);
}
}
12 changes: 12 additions & 0 deletions src/test/java/org/fungover/haze/SaveFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,16 @@ private int compareLastModified(Path p1, Path p2) {
throw new RuntimeException(e);
}
}

@Test
@DisplayName("File already exists returns +OK\\r\\n")
void fileAlreadyExistsReturnsOK(@TempDir Path tempDir) {
System.setProperty("user.home", tempDir.toString());
var map = Map.of("key", "value");

SaveFile.createFile();

assertThat(SaveFile.writeOnFile(map)).isEqualTo("+OK\r\n");
}
}

Loading