Skip to content

Commit

Permalink
Improved testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
FabriDeCastelli committed Aug 10, 2023
1 parent 91812d8 commit a9d9b0d
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/main/java/server/conf/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
"f": {
"username": "f",
"passwordHash": "JS8QyDYQ68oaBZwLroJV66L5W+TR17z6idckioLZ8RE\u003d"
},
"ciao": {
"username": "ciao",
"passwordHash": "sTOgwOm+474gFj0q0x1iSNspKqbcse4IeiqlDg/HWuI\u003d"
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/server/CommandTests.java
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server;
package server.controller;

import java.io.IOException;
import java.net.ServerSocket;
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand Down
87 changes: 87 additions & 0 deletions src/test/java/server/service/LoginCommandTests.java
Original file line number Diff line number Diff line change
@@ -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));

}





}

0 comments on commit a9d9b0d

Please sign in to comment.