Test: Add test for ChatController

This commit is contained in:
Mathis Ginkel
2026-04-26 23:36:50 +02:00
parent 216c15e825
commit 5358309e1a
5 changed files with 229 additions and 11 deletions
@@ -0,0 +1,16 @@
package ch.unibas.dmi.dbis.cs108.casono.client.chat;
import java.util.List;
/**
* Interface used to create test instances of the ChatClient
*/
public interface ChatClientInterface {
List<Message> getMessages();
void sendMessage(Message message);
List<String> getUsers();
}
@@ -33,9 +33,9 @@ public class ChatController {
private volatile String username;
private final ClientService clientService;
private ClientService clientService;
private final ChatClient chatClient;
private final ChatClientInterface chatClient;
public ChatBoxController getChatBoxController() {
return chatBoxController;
@@ -112,6 +112,37 @@ public class ChatController {
REFRESH_TIME);
}
/**
* Another constructor to give the ChatClient directly instead of the Client Service used only for test purposes
* @param username
*/
public ChatController(String username, ChatClientInterface chatClient) {
this.username = username;
this.chatClient = chatClient;
chatModelMap = new LinkedHashMap<>();
localUserList = new ArrayList<>();
this.chatBoxController = new ChatBoxController(username, this);
this.logger = LogManager.getLogger(ChatController.class);
this.serverEventListener = this::handleServerEvent;
this.activeChatControllers = new HashMap<>();
this.timer = new Timer(true);
timer.schedule(
new TimerTask() {
@Override
public void run() {
try {
receiveMessage();
checkWhisperUsers();
} catch (RuntimeException e) {
logger.warn("Chat refresh failed: {}", e.getMessage());
}
}
},
0,
REFRESH_TIME);
}
private void registerAsActiveController(ClientService clientService) {
synchronized (ACTIVE_CONTROLLERS) {
ChatController oldController = ACTIVE_CONTROLLERS.get(clientService);
@@ -1,5 +1,6 @@
package ch.unibas.dmi.dbis.cs108.casono.client.network;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatClientInterface;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.Message;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.RequestParameter;
import java.util.ArrayList;
@@ -12,7 +13,7 @@ import org.apache.logging.log4j.Logger;
* from the server. It uses the ClientService to send commands and receive responses from the
* server.
*/
public class ChatClient {
public class ChatClient implements ChatClientInterface {
private final ClientService clientService;
private final Logger logger;