Skip to content

Commit

Permalink
Add test for initSocket method in main
Browse files Browse the repository at this point in the history
  • Loading branch information
robinalfengard committed Feb 5, 2024
1 parent ba80c92 commit 00dd4b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/main/java/org/fungover/haze/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ private static void whileServerOpen(HazeList hazeList, HazeDatabase hazeDatabase
while (serverOpen) {
var client = serverSocket.accept();
logger.info("Application started: serverSocket.accept()");

runThread(hazeList, hazeDatabase, auth, isPasswordSet, client);
}
}
Expand Down Expand Up @@ -63,7 +62,6 @@ private static void createThread(HazeList hazeList, HazeDatabase hazeDatabase, A
private static void handleThread(HazeList hazeList, HazeDatabase hazeDatabase, Socket client, List<String> inputList) throws IOException {
controlCommand(hazeList, hazeDatabase, client, inputList);
printThreadDebug();

inputList.clear();
}

Expand All @@ -81,7 +79,7 @@ public static List<String> getInputList(BufferedReader input) throws IOException
return inputList;
}

private static void initSocket(Initialize initialize, ServerSocket serverSocket) throws IOException {
public static void initSocket(Initialize initialize, ServerSocket serverSocket) throws IOException {
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(initialize.getPort()));
}
Expand Down Expand Up @@ -164,7 +162,7 @@ public static void initializeServer(String[] args, Initialize initialize, Auth a
auth.setPassword(initialize.getPassword());
}

private static boolean authenticateClient(Auth auth, boolean isPasswordSet, Socket client, List<String> inputList, boolean clientAuthenticated) throws IOException {
public static boolean authenticateClient(Auth auth, boolean isPasswordSet, Socket client, List<String> inputList, boolean clientAuthenticated) throws IOException {
if (authCommandReceived(isPasswordSet, inputList, clientAuthenticated))
return auth.authenticate(inputList.get(1), client);

Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/fungover/haze/MainTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package org.fungover.haze;
import org.apache.logging.log4j.core.jmx.Server;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -12,10 +13,12 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.in;
import static org.mockito.Mockito.*;

class MainTest {
Expand Down Expand Up @@ -205,6 +208,25 @@ void callToShutDownClientIfNotAuthenticatedShouldNotShutDownOutputForAuthenticat
assertThat(clientSocket.isOutputShutdown()).isEqualTo(false);
}

@Test
@DisplayName("Call to initSocket should set reuseAddress to true")
void callToInitSocketShouldSetReuseAddressToTrue() throws IOException {
Initialize initialize = new Initialize();
ServerSocket ss = new ServerSocket();
Main.initSocket(initialize, ss);
assertThat(ss.getReuseAddress()).isTrue();
}

@Test
@DisplayName("Call to initSocket should bind serversocket port to same as initialize")
void callToInitSocketShouldBindServerSocketPortToSameAsInitialize() throws IOException {
Initialize initialize = new Initialize();
ServerSocket ss = new ServerSocket();
Main.initSocket(initialize, ss);
int initializePort = initialize.getPort();
int serverSocketPort = ss.getLocalPort();
assertThat(initializePort).isEqualTo(serverSocketPort);
}



Expand Down

0 comments on commit 00dd4b8

Please sign in to comment.