Skip to content

Commit

Permalink
Add test for shutDownClientIfNotAuthenticated in main class
Browse files Browse the repository at this point in the history
  • Loading branch information
robinalfengard committed Feb 5, 2024
1 parent b2a7f62 commit ba80c92
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
7 changes: 0 additions & 7 deletions src/main/java/org/fungover/haze/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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.io.InputStreamReader;
Expand All @@ -12,22 +10,17 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class Main {
static boolean serverOpen = true;
static Logger logger = LogManager.getLogger(Main.class);

public static void main(String[] args) {

Initialize initialize = Initialize.getInitialize(args);

HazeDatabase hazeDatabase = new HazeDatabase();
HazeList hazeList = new HazeList(hazeDatabase);
Auth auth = new Auth();
initializeServer(args, initialize, auth);
final boolean isPasswordSet = auth.isPasswordSet();

addHook(hazeDatabase);
try (ServerSocket serverSocket = new ServerSocket()) {
initSocket(initialize, serverSocket);
Expand Down
47 changes: 47 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.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -9,14 +10,30 @@
import org.mockito.MockitoAnnotations;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

class MainTest {

private ServerSocket serverSocket;
private Socket clientSocket;

@BeforeEach
void setUp() throws IOException {
serverSocket = new ServerSocket(0);
clientSocket = new Socket("localhost", serverSocket.getLocalPort());
}

@AfterEach
void tearDown() throws IOException {
serverSocket.close();
clientSocket.close();
}

HazeDatabase database = new HazeDatabase();
HazeList hazeList = new HazeList(database);
Expand Down Expand Up @@ -151,6 +168,16 @@ void callAuthCommandReceivedWithInvalidInputShouldReturnFalse() {
assertThat(result).isFalse();
}

@Test
@DisplayName("Call AuthCommandReceived With Invalid Password Should Return False")
void callAuthCommandReceivedWithInvalidPasswordShouldReturnFalse() {
boolean isPasswordSet = false;
boolean clientAuthenticated = false;
List<String> inputList = List.of("AUTH", "password");
boolean result = Main.authCommandReceived(isPasswordSet, inputList, clientAuthenticated);
assertThat(result).isFalse();
}

@Test
void callToInitializeServerShouldSetPasswordToAuth(){
String[] testArgs = {"arg1", "arg2"};
Expand All @@ -160,6 +187,26 @@ void callToInitializeServerShouldSetPasswordToAuth(){
assertThat(auth.isPasswordSet()).isTrue();
}

@Test
@DisplayName("Call to shutdownClientIfNotAuthenticated should shut down output for non authenticated user")
void callToShutDownClientIfNotAuthenticatedShouldShutDownOutputForNonAuthenticatedUser() throws IOException {
boolean clientAuthenticated = false;
boolean isPasswordSet = true;
Main.shutdownClientIfNotAuthenticated(clientSocket, clientAuthenticated, isPasswordSet);
assertThat(clientSocket.isOutputShutdown()).isEqualTo(true);
}

@Test
@DisplayName("Call to shutdownClientIfNotAuthenticated should not shut down output for authenticated user")
void callToShutDownClientIfNotAuthenticatedShouldNotShutDownOutputForAuthenticatedUser() throws IOException {
boolean clientAuthenticated = true;
boolean isPasswordSet = true;
Main.shutdownClientIfNotAuthenticated(clientSocket, clientAuthenticated, isPasswordSet);
assertThat(clientSocket.isOutputShutdown()).isEqualTo(false);
}






Expand Down

0 comments on commit ba80c92

Please sign in to comment.