diff --git a/src/main/java/server/conf/users.json b/src/main/java/server/conf/users.json index 95e267d..49c1ef5 100644 --- a/src/main/java/server/conf/users.json +++ b/src/main/java/server/conf/users.json @@ -6,5 +6,9 @@ "f": { "username": "f", "passwordHash": "JS8QyDYQ68oaBZwLroJV66L5W+TR17z6idckioLZ8RE\u003d" + }, + "ciao": { + "username": "ciao", + "passwordHash": "sTOgwOm+474gFj0q0x1iSNspKqbcse4IeiqlDg/HWuI\u003d" } } \ No newline at end of file diff --git a/src/test/java/service/PasswordHashingServiceTests.java b/src/test/java/client/service/PasswordHashingServiceTests.java similarity index 95% rename from src/test/java/service/PasswordHashingServiceTests.java rename to src/test/java/client/service/PasswordHashingServiceTests.java index 41f1e0a..e839e44 100644 --- a/src/test/java/service/PasswordHashingServiceTests.java +++ b/src/test/java/client/service/PasswordHashingServiceTests.java @@ -1,9 +1,8 @@ -package service; +package client.service; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import client.service.PasswordHashingService; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/server/CommandTests.java b/src/test/java/server/CommandTests.java new file mode 100644 index 0000000..69ae151 --- /dev/null +++ b/src/test/java/server/CommandTests.java @@ -0,0 +1,30 @@ +package server; + +import static org.mockito.Mockito.mock; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import server.model.Command; + +/** + * Tests for the Command interface. + */ +public class CommandTests { + + private final Command command = mock(Command.class); + + + + @Test + @DisplayName(" can correctly send a response") + public void testSendResponse() { + + // TODO + } + + @Test + @DisplayName(" throws an exception when the response cannot be sent") + public void testSendResponseThrowsException() { + // TODO + } +} diff --git a/src/test/java/server/RequestHandlerTests.java b/src/test/java/server/controller/RequestHandlerTests.java similarity index 79% rename from src/test/java/server/RequestHandlerTests.java rename to src/test/java/server/controller/RequestHandlerTests.java index 79bdfe0..65f04cc 100644 --- a/src/test/java/server/RequestHandlerTests.java +++ b/src/test/java/server/controller/RequestHandlerTests.java @@ -1,8 +1,7 @@ -package server; +package server.controller; import static org.mockito.Mockito.mock; -import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; @@ -11,7 +10,6 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.mockito.Mock; -import server.controller.RequestHandler; /** * RequestHandlerTests is the test suite for RequestHandler. @@ -47,14 +45,10 @@ public void testHandleUserRequest() { /** * Tears down the test suite. - * - * @throws IOException if an I/O error occurs when closing resources. */ @AfterEach - public void tearDown() throws IOException { - in.close(); - out.close(); - socket.close(); + public void tearDown() { + // TODO } } diff --git a/src/test/java/server/TerminationHandlerTests.java b/src/test/java/server/controller/TerminationHandlerTests.java similarity index 95% rename from src/test/java/server/TerminationHandlerTests.java rename to src/test/java/server/controller/TerminationHandlerTests.java index 4a25c00..8706af9 100644 --- a/src/test/java/server/TerminationHandlerTests.java +++ b/src/test/java/server/controller/TerminationHandlerTests.java @@ -1,4 +1,4 @@ -package server; +package server.controller; import java.io.IOException; import java.net.ServerSocket; @@ -8,7 +8,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import server.controller.TerminationHandler; /** * TerminationHandlerTests is the test suite for TerminationHandler. diff --git a/src/test/java/service/AuthenticationServiceTests.java b/src/test/java/server/service/AuthenticationServiceTests.java similarity index 98% rename from src/test/java/service/AuthenticationServiceTests.java rename to src/test/java/server/service/AuthenticationServiceTests.java index 561f3ba..0c8b8fb 100644 --- a/src/test/java/service/AuthenticationServiceTests.java +++ b/src/test/java/server/service/AuthenticationServiceTests.java @@ -1,4 +1,4 @@ -package service; +package server.service; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -10,8 +10,6 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import server.service.AuthenticationService; - /** * Tests for the AuthenticationService. diff --git a/src/test/java/server/service/LoginCommandTests.java b/src/test/java/server/service/LoginCommandTests.java new file mode 100644 index 0000000..79b995c --- /dev/null +++ b/src/test/java/server/service/LoginCommandTests.java @@ -0,0 +1,87 @@ +package server.service; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.util.Optional; +import model.User; +import model.UserRequest; +import model.enums.Request; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import server.model.Command; + +/** + * Tests for the LoginCommand. + */ +@DisplayName("The LoginCommand tests ") +public class LoginCommandTests { + + @Mock + private static final Command command = mock(Command.class); + private static UserRequest userRequest; + private static LoginCommand loginCommand; + private static ObjectOutputStream out; + @Mock + private static AuthenticationService authenticationService; + private static User user; + + /** + * Sets up the test suite. + * + * @throws IOException if an I/O error occurs when closing resources. + */ + @BeforeAll + public static void setUp() throws IOException { + authenticationService = mock(AuthenticationService.class); + out = new ObjectOutputStream(System.out); + loginCommand = new LoginCommand(authenticationService, out); + user = new User("testUser", "testPassword"); + userRequest = new UserRequest(Request.LOGIN, user); + when(command.sendResponse(any(), any())).thenReturn(true); + + } + + @Test + @DisplayName(" can correctly handle a login command") + public void testHandle() { + assertTrue(loginCommand.handle(userRequest)); + } + + @Test + @DisplayName(" successfully writes the response when the user is not registered") + public void testHandleNotRegisteredUser() { + when(authenticationService.getUserByUsername(any())).thenReturn(Optional.empty()); + assertTrue(loginCommand.handle(userRequest)); + + } + + @Test + @DisplayName(" successfully writes the response when the user is registered") + public void testHandleRegisteredUser() { + when(authenticationService.getUserByUsername(any())).thenReturn(Optional.of(user)); + assertTrue(loginCommand.handle(userRequest)); + + } + + @Test + @DisplayName(" successfully writes the response " + + "when the user is registered but the password is wrong") + public void testHandleRegisteredUserWrongPassword() { + final User wrongUser = new User("testUser", "wrongPassword"); + when(authenticationService.getUserByUsername(any())).thenReturn(Optional.of(wrongUser)); + assertTrue(loginCommand.handle(userRequest)); + + } + + + + + +}