Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/185-refractor-init-methods-from-…
Browse files Browse the repository at this point in the history
…main-to-initialize' into 185-refractor-init-methods-from-main-to-initialize

# Conflicts:
#	src/test/java/org/fungover/haze/AuthTest.java
#	src/test/java/org/fungover/haze/MainTest.java
  • Loading branch information
cmatlak committed Feb 12, 2024
2 parents 9175b5c + 058ce05 commit 7c4d5e3
Show file tree
Hide file tree
Showing 16 changed files with 376 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
Java_Version=$(mvn help:evaluate "-Dexpression=maven.compiler.release" -q -DforceStdout | sed -e 's/^1\./1.0./')
echo "Java_Version=$Java_Version" >> $GITHUB_ENV
- name: Set up JDK 19
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
java-version: ${{ env.Java_Version }}
distribution: 'temurin'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-latest
steps:

- uses: release-drafter/release-drafter@v5
- uses: release-drafter/release-drafter@v6
with:
config-name: release-drafter-config.yml
env:
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
Java_Version=$(mvn help:evaluate "-Dexpression=maven.compiler.release" -q -DforceStdout | sed -e 's/^1\./1.0./')
echo "Java_Version=$Java_Version" >> $GITHUB_ENV
- name: Set up JDK 19
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
java-version: ${{ env.Java_Version }}
distribution: 'temurin'
Expand All @@ -35,21 +35,21 @@ jobs:
uses: docker/setup-buildx-action@v2

- name: Login to Docker Hub
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Docker meta
id: meta
uses: docker/metadata-action@v4
uses: docker/metadata-action@v5
with:
images: |
fungover/haze
Expand All @@ -62,10 +62,10 @@ jobs:
type=sha
- name: Build and push
uses: docker/build-push-action@v4
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64,linux/arm/v7
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
4 changes: 2 additions & 2 deletions .github/workflows/sonar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ jobs:
Java_Version=$(mvn help:evaluate "-Dexpression=maven.compiler.release" -q -DforceStdout | sed -e 's/^1\./1.0./')
echo "Java_Version=$Java_Version" >> $GITHUB_ENV
- name: Set up JDK 19
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
java-version: ${{ env.Java_Version }}
distribution: 'temurin'
cache: maven
- name: Cache SonarCloud packages
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
Expand Down
16 changes: 5 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,13 @@
<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>
<version>5.9.2</version>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.3.1</version>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -63,13 +57,13 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.17.6</version>
<version>1.19.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.36</version>
<version>2.0.12</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down Expand Up @@ -143,7 +137,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.2.1</version>
<version>3.2.5</version>
<executions>
<execution>
<goals>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/fungover/haze/Command.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.fungover.haze;

public enum Command {
SET, GET, DEL, PING, SETNX, EXISTS, SAVE, RPUSH, LPUSH, LPOP, RPOP, LLEN, LMOVE, LTRIM, AUTH
SET, GET, DEL, PING, SETNX, EXISTS, SAVE, RPUSH, LPUSH, LPOP, RPOP, LLEN, LMOVE, LTRIM, AUTH, LINDEX, INCR, DECR

}
56 changes: 55 additions & 1 deletion src/main/java/org/fungover/haze/HazeDatabase.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package org.fungover.haze;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -46,6 +45,9 @@ public String get(List<String> inputList) {
}

public String delete(List<String> keys) {
if (keys.isEmpty())
throw new IllegalArgumentException("No keys provided");

var counter = new AtomicInteger(0);
lock.lock();
try {
Expand All @@ -62,6 +64,9 @@ public String delete(List<String> keys) {
}

public String exists(List<String> keys) {
if (keys.isEmpty())
return ":0\r\n";

lock.lock();
int numberOfKeys = 0;
try {
Expand Down Expand Up @@ -109,6 +114,12 @@ public Map<String, String> copy() {
}

public String ping(List<String> messageList) {
if (messageList == null || messageList.isEmpty()) {
throw new IllegalArgumentException("No message provided");
} else if (messageList.size() > 2) {
throw new IllegalArgumentException("Too many arguments for PING command");
}

if (messageList.size() == 1)
return "+PONG\r\n";
else return "$" + (messageList.get(1)).length() + "\r\n" + messageList.get(1) + "\r\n";
Expand Down Expand Up @@ -143,4 +154,47 @@ public void addValue(String key, String value) {
lock.unlock();
}
}

public String increaseValue(List<String> inputList) {
lock.lock();
String key = inputList.get(1);
try {
if (!database.containsKey(key)) {
return "-ERR no such key\r\n";
}
String value = database.get(key);
try {
long longValue = Long.parseLong(value);
longValue++;
database.put(key, String.valueOf(longValue));
return ":" + longValue + "\r\n";
} catch (NumberFormatException e) {
return "-WRONGTYPE value is not an integer or out of range\r\n";
}
} finally {
lock.unlock();
}
}

public String decreaseValue(List<String> inputList) {
lock.lock();
String key = inputList.get(1);
try {
if (!database.containsKey(key)) {
return "-ERR no such key\r\n";
}
String value = database.get(key);
try {
long longValue = Long.parseLong(value);
longValue--;
database.put(key, String.valueOf(longValue));
return ":" + longValue + "\r\n";
} catch (NumberFormatException e) {
return "-WRONGTYPE value is not an integer or out of range\r\n";
}
} finally {
lock.unlock();
}
}

}
27 changes: 27 additions & 0 deletions src/main/java/org/fungover/haze/HazeList.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,33 @@ public static String listValueAsString(List<String> list) {
return String.join("\r\n", list);
}

public String lIndex(List<String> inputList){
String key = getKey(inputList);
int index;
if (!hazeDatabase.containsKey(key))
return "Could not find list";
String indexStr = inputList.get(2);

try {
index = Integer.parseInt(indexStr);
} catch (NumberFormatException e) {
return "-ERR invalid index\r\n";
}

List<String> data = getValueAsList(hazeDatabase.getValue(key));
if(data.size()-1<index)
return NIL_RESPONSE;
String result;
if(index<0){
int newIndex = (data.size()) + index;
result = data.get(newIndex);
return "$" + result.length() + "\r\n" + result + "\r\n";
}

result = data.get(index);
return "$" + result.length() + "\r\n" + result + "\r\n";
}

private static String getKey(List<String> inputList) {
String key = null;
if (inputList.size() > 1)
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/org/fungover/haze/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ public static String executeCommand(HazeDatabase hazeDatabase, List<String> inpu
case LLEN -> hazeList.lLen(inputList);
case LMOVE -> hazeList.lMove(inputList);
case LTRIM -> hazeList.callLtrim(inputList);
case LINDEX -> hazeList.lIndex(inputList);
case AUTH -> "+OK\r\n";

case INCR -> hazeDatabase.increaseValue(inputList);
case DECR -> hazeDatabase.decreaseValue(inputList);
};
}

Expand All @@ -143,4 +145,16 @@ private static void readInputStream(BufferedReader input, List<String> inputList
}



private static void shutdownClientIfNotAuthenticated(Socket client, boolean clientAuthenticated, boolean isPasswordSet) throws IOException {
if (!clientAuthenticated && isPasswordSet) {
client.getOutputStream().write(Auth.printAuthError());
client.shutdownOutput();
}
}

static boolean authCommandReceived(boolean isPasswordSet, List<String> inputList, boolean clientAuthenticated) {
return isPasswordSet && !clientAuthenticated && inputList.size() == 2 && inputList.getFirst().equals("AUTH");
}

}
75 changes: 74 additions & 1 deletion src/test/java/org/fungover/haze/AuthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@



import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


class AuthTest {
Expand Down Expand Up @@ -70,7 +78,72 @@ void authCommandReceivedPasswordNotAuth() {

}

}


@Test
void authenticateShouldShutdownOutputForInvalidPassword() throws IOException {

Auth auth = new Auth();
auth.setPassword("12345");

Socket client = Mockito.mock(Socket.class);
OutputStream outputStream = Mockito.mock(OutputStream.class);
when(client.getOutputStream()).thenReturn(outputStream);

auth.authenticate("wrongPassword", client);

verify(client).shutdownOutput();
}

@Test
void authenticateClientShouldReturnTrueForValidPassword() throws IOException {

Auth auth = Mockito.mock(Auth.class);
Socket client = Mockito.mock(Socket.class);
List<String> inputList = new ArrayList<>();
inputList.add("AUTH");
inputList.add("password");

when(auth.authenticate(inputList.get(1), client)).thenReturn(true);

boolean result = Auth.authenticateClient(auth, true, client, inputList, false);

assertTrue(result);
}

@Test
void shutdownClientIfNotAuthenticatedWhenClientNotAuthenticatedAndPasswordIsSet() throws Exception {

Socket client = Mockito.mock(Socket.class);
OutputStream outputStream = Mockito.mock(OutputStream.class);
when(client.getOutputStream()).thenReturn(outputStream);

Method method = Auth.class.getDeclaredMethod("shutdownClientIfNotAuthenticated", Socket.class, boolean.class, boolean.class);
method.setAccessible(true);

method.invoke(null, client, false, true);

verify(outputStream).write(Auth.printAuthError());
verify(client).shutdownOutput();

}

@Test
void clientShouldNotBeShutdownWhenAuthenticatedOrPasswordIsNotSet() throws Exception {

Socket client = Mockito.mock(Socket.class);
OutputStream outputStream = Mockito.mock(OutputStream.class);
when(client.getOutputStream()).thenReturn(outputStream);

Method method = Auth.class.getDeclaredMethod("shutdownClientIfNotAuthenticated", Socket.class, boolean.class, boolean.class);
method.setAccessible(true);

method.invoke(null, client, true, true);
verify(outputStream, Mockito.never()).write(Auth.printAuthError());
verify(client, Mockito.never()).shutdownOutput();

method.invoke(null, client, false, false);
verify(outputStream, Mockito.never()).write(Auth.printAuthError());
verify(client, Mockito.never()).shutdownOutput();
}
}
Loading

0 comments on commit 7c4d5e3

Please sign in to comment.