Add ChatRooms to User Class
parent
1394257c0b
commit
ef2c82aa46
|
@ -10,25 +10,18 @@ public class ChatRoom {
|
|||
private String roomName;
|
||||
private List<Message> messages;
|
||||
private final LocalDateTime createdAt;
|
||||
private List<User> participants;
|
||||
private User user1;
|
||||
private User user2;
|
||||
|
||||
public ChatRoom(String roomName, User user1, User user2) {
|
||||
public ChatRoom(User user1, User user2) {
|
||||
this.roomId = nextRoomId++;
|
||||
this.roomName = roomName;
|
||||
this.messages = new ArrayList<>();
|
||||
this.createdAt = LocalDateTime.now();
|
||||
this.participants = new ArrayList<>();
|
||||
this.participants.add(user1);
|
||||
this.participants.add(user2);
|
||||
this.user1 = user1;
|
||||
this.user2 = user2;
|
||||
}
|
||||
|
||||
public ChatRoom(String roomName, List<User> participants) {
|
||||
this.roomId = nextRoomId++;
|
||||
this.roomName = roomName;
|
||||
this.messages = new ArrayList<>();
|
||||
this.createdAt = LocalDateTime.now();
|
||||
this.participants = new ArrayList<>(participants);
|
||||
}
|
||||
|
||||
|
||||
public void addMessage(Message message) {
|
||||
if (messages == null)
|
||||
|
@ -37,26 +30,12 @@ public class ChatRoom {
|
|||
messages.add(message);
|
||||
}
|
||||
|
||||
public boolean addParticipant(User user) {
|
||||
if (!participants.contains(user)) {
|
||||
participants.add(user);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Getter und Setter
|
||||
public int getRoomId() {
|
||||
return roomId;
|
||||
}
|
||||
|
||||
public String getRoomName() {
|
||||
return roomName;
|
||||
}
|
||||
|
||||
public void setRoomName(String roomName) {
|
||||
this.roomName = roomName;
|
||||
}
|
||||
|
||||
public List<Message> getMessages() {
|
||||
return messages;
|
||||
|
@ -66,16 +45,7 @@ public class ChatRoom {
|
|||
return createdAt;
|
||||
}
|
||||
|
||||
public List<User> getParticipants() {
|
||||
return participants;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ChatRoom [roomId=" + roomId + ", roomName=" + roomName +
|
||||
", createdAt=" + createdAt + ", participants=" +
|
||||
participants.stream().map(User::getUsername).toList() + "]";
|
||||
}
|
||||
|
||||
|
||||
public List<String> showMessages(){
|
||||
if (messages == null || messages.isEmpty())
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package domain;
|
||||
|
||||
public class GruppenRoom {
|
||||
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package domain;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class User {
|
||||
private static int nextUserId = 1000;
|
||||
private int userId;
|
||||
|
@ -7,6 +9,8 @@ public class User {
|
|||
private boolean isOnline;
|
||||
private UserInfo userInfo;
|
||||
private UserContacts userContacts;
|
||||
private UserChatRoom chatRoom;
|
||||
private List<GruppenRoom> userGruppen;
|
||||
|
||||
public User(String username) {
|
||||
this.userId = nextUserId++;
|
||||
|
@ -14,6 +18,8 @@ public class User {
|
|||
this.isOnline = true;
|
||||
this.userInfo = UserInfo.VERFÜGBAR;
|
||||
this.userContacts = new UserContacts();
|
||||
this.chatRoom = new UserChatRoom();
|
||||
|
||||
}
|
||||
|
||||
public int getUserId() {
|
||||
|
@ -43,6 +49,10 @@ public class User {
|
|||
public UserContacts getUserContacts() {
|
||||
return userContacts;
|
||||
}
|
||||
|
||||
public UserChatRoom getUserChatRoom() {
|
||||
return chatRoom;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package domain;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class UserChatRoom {
|
||||
|
||||
private List<ChatRoom> chatsRooms;
|
||||
|
||||
public UserChatRoom() {
|
||||
this.chatsRooms = new ArrayList<>();
|
||||
}
|
||||
|
||||
public boolean addChat(ChatRoom chatRoom) {
|
||||
if (chatRoom == null)
|
||||
throw new IllegalArgumentException("ChatRoom soll nicht null sein");
|
||||
|
||||
if (chatsRooms.contains(chatRoom))
|
||||
throw new IllegalArgumentException("chatRoom ist vorhanden");
|
||||
|
||||
return chatsRooms.add(chatRoom);
|
||||
}
|
||||
|
||||
public boolean removeChat(ChatRoom chatRoom) {
|
||||
if (chatRoom == null)
|
||||
throw new IllegalArgumentException("ChatRoom soll nicht null sein");
|
||||
|
||||
if (!chatsRooms.contains(chatRoom))
|
||||
throw new IllegalArgumentException("chatRoom nicht gefunden");
|
||||
|
||||
return chatsRooms.remove(chatRoom);
|
||||
}
|
||||
|
||||
public List<ChatRoom> getAllChatsRooms() {
|
||||
return new ArrayList<>(chatsRooms);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
package fassade;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -30,36 +29,22 @@ public class ChatService {
|
|||
|
||||
|
||||
|
||||
public int createChatRoom(String roomName, String user1Name, String user2Name) {
|
||||
public int createChatRoom(String user1Name, String user2Name) {
|
||||
User user1 = getUser(user1Name);
|
||||
User user2 = getUser(user2Name);
|
||||
|
||||
if (user1 == null || user2 == null)
|
||||
throw new IllegalArgumentException("User existiert nicht");
|
||||
|
||||
ChatRoom tempChatRoom = new ChatRoom(roomName, user1, user2);
|
||||
ChatRoom tempChatRoom = new ChatRoom(user1, user2);
|
||||
chatrooms.put(tempChatRoom.getRoomId(), tempChatRoom);
|
||||
user1.getUserChatRoom().addChat(tempChatRoom);
|
||||
user2.getUserChatRoom().addChat(tempChatRoom);
|
||||
|
||||
return tempChatRoom.getRoomId();
|
||||
}
|
||||
|
||||
public int createGroupChat(String roomName, List<String> participantNames) {
|
||||
if (participantNames == null || participantNames.size() < 2) {
|
||||
throw new IllegalArgumentException("Mindestens 2 Teilnehmer benötigt");
|
||||
}
|
||||
|
||||
List<User> participants = new ArrayList<>();
|
||||
for (String name : participantNames) {
|
||||
User user = getUser(name);
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException("User existiert nicht: " + name);
|
||||
}
|
||||
participants.add(user);
|
||||
}
|
||||
|
||||
ChatRoom tempChatRoom = new ChatRoom(roomName, participants);
|
||||
chatrooms.put(tempChatRoom.getRoomId(), tempChatRoom);
|
||||
return tempChatRoom.getRoomId();
|
||||
}
|
||||
|
||||
|
||||
public void sendMessage(int roomId, String senderName, String content) {
|
||||
ChatRoom room = chatrooms.get(roomId);
|
||||
|
@ -71,8 +56,7 @@ public class ChatService {
|
|||
throw new IllegalArgumentException("Sender existiert nicht: " + senderName);
|
||||
}
|
||||
|
||||
if (!room.getParticipants().contains(sender))
|
||||
throw new IllegalArgumentException("Sender ist nicht Teil des Chatrooms");
|
||||
|
||||
|
||||
Message message = new Message(sender, content);
|
||||
room.addMessage(message);
|
||||
|
@ -107,21 +91,7 @@ public class ChatService {
|
|||
return user.getUserContacts().showAllContacts();
|
||||
}
|
||||
|
||||
public List<Integer> getUserChatRoomIds(String userName) {
|
||||
if (!users.containsKey(userName))
|
||||
throw new IllegalArgumentException("User existiert nicht: " + userName);
|
||||
|
||||
User user = users.get(userName);
|
||||
List<Integer> roomIds = new ArrayList<>();
|
||||
|
||||
for (ChatRoom room : chatrooms.values()) {
|
||||
if (room.getParticipants().contains(user)) {
|
||||
roomIds.add(room.getRoomId());
|
||||
}
|
||||
}
|
||||
|
||||
return roomIds;
|
||||
}
|
||||
|
||||
|
||||
public String getChatRoomInfo(int roomId) {
|
||||
ChatRoom room = chatrooms.get(roomId);
|
||||
|
|
|
@ -13,12 +13,10 @@ public class ChatRoomTest {
|
|||
public void testChatRoomCreation() {
|
||||
User user1 = new User("User1");
|
||||
User user2 = new User("User2");
|
||||
ChatRoom room = new ChatRoom("TestRoom", user1, user2);
|
||||
ChatRoom room = new ChatRoom(user1, user2);
|
||||
|
||||
assertEquals("TestRoom", room.getRoomName());
|
||||
assertEquals(2, room.getParticipants().size());
|
||||
assertTrue(room.getParticipants().contains(user1));
|
||||
assertTrue(room.getParticipants().contains(user2));
|
||||
|
||||
|
||||
assertNotNull(room.getCreatedAt());
|
||||
assertTrue(room.getRoomId() >= 1000);
|
||||
}
|
||||
|
@ -27,7 +25,7 @@ public class ChatRoomTest {
|
|||
public void testAddMessageToChatRoom() {
|
||||
User user1 = new User("User1");
|
||||
User user2 = new User("User2");
|
||||
ChatRoom room = new ChatRoom("TestRoom", user1, user2);
|
||||
ChatRoom room = new ChatRoom(user1, user2);
|
||||
|
||||
Message message = new Message(user1, "Hallo!");
|
||||
room.addMessage(message);
|
||||
|
@ -40,7 +38,7 @@ public class ChatRoomTest {
|
|||
public void testShowMessages() {
|
||||
User user1 = new User("User1");
|
||||
User user2 = new User("User2");
|
||||
ChatRoom room = new ChatRoom("TestRoom", user1, user2);
|
||||
ChatRoom room = new ChatRoom(user1, user2);
|
||||
|
||||
room.addMessage(new Message(user1, "Nachricht 1"));
|
||||
room.addMessage(new Message(user2, "Nachricht 2"));
|
||||
|
@ -56,21 +54,18 @@ public class ChatRoomTest {
|
|||
User user1 = new User("User1");
|
||||
User user2 = new User("User2");
|
||||
User user3 = new User("User3");
|
||||
ChatRoom room = new ChatRoom("TestRoom", user1, user2);
|
||||
ChatRoom room = new ChatRoom(user1, user2);
|
||||
|
||||
assertTrue(room.addParticipant(user3));
|
||||
assertEquals(3, room.getParticipants().size());
|
||||
assertTrue(room.getParticipants().contains(user3));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddDuplicateParticipant() {
|
||||
User user1 = new User("User1");
|
||||
User user2 = new User("User2");
|
||||
ChatRoom room = new ChatRoom("TestRoom", user1, user2);
|
||||
ChatRoom room = new ChatRoom(user1, user2);
|
||||
|
||||
|
||||
assertFalse(room.addParticipant(user1)); // Bereits vorhanden
|
||||
assertEquals(2, room.getParticipants().size());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,117 +0,0 @@
|
|||
package test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.Before;
|
||||
import fassade.ChatService;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ChatServiceTest {
|
||||
|
||||
private ChatService chatService;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
chatService = new ChatService();
|
||||
chatService.createUser("User1");
|
||||
chatService.createUser("User2");
|
||||
chatService.createUser("User3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateUser() {
|
||||
boolean result = chatService.createUser("NewUser");
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCreateDuplicateUser() {
|
||||
chatService.createUser("User1"); // Bereits existiert
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateChatRoom() {
|
||||
int roomId = chatService.createChatRoom("TestRoom", "User1", "User2");
|
||||
assertTrue(roomId >= 1000);
|
||||
|
||||
String roomInfo = chatService.getChatRoomInfo(roomId);
|
||||
assertTrue(roomInfo.contains("TestRoom"));
|
||||
assertTrue(roomInfo.contains("User1"));
|
||||
assertTrue(roomInfo.contains("User2"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCreateChatRoomWithNonExistentUser() {
|
||||
chatService.createChatRoom("TestRoom", "NonExistent", "User2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendAndRetrieveMessages() {
|
||||
int roomId = chatService.createChatRoom("TestRoom", "User1", "User2");
|
||||
|
||||
chatService.sendMessage(roomId, "User1", "Hallo User2!");
|
||||
chatService.sendMessage(roomId, "User2", "Hallo User1!");
|
||||
|
||||
List<String> messages = chatService.showMessages(roomId);
|
||||
assertEquals(2, messages.size());
|
||||
assertTrue(messages.get(0).contains("Hallo User2!"));
|
||||
assertTrue(messages.get(1).contains("Hallo User1!"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSendMessageToNonExistentRoom() {
|
||||
chatService.sendMessage(9999, "User1", "Test");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSendMessageFromNonParticipant() {
|
||||
int roomId = chatService.createChatRoom("TestRoom", "User1", "User2");
|
||||
chatService.sendMessage(roomId, "User3", "Ich bin nicht dabei!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUserChatRooms() {
|
||||
int room1 = chatService.createChatRoom("Room1", "User1", "User2");
|
||||
int room2 = chatService.createChatRoom("Room2", "User1", "User3");
|
||||
|
||||
List<Integer> user1Rooms = chatService.getUserChatRoomIds("User1");
|
||||
assertEquals(2, user1Rooms.size());
|
||||
|
||||
List<Integer> user2Rooms = chatService.getUserChatRoomIds("User2");
|
||||
assertEquals(1, user2Rooms.size());
|
||||
assertEquals((Integer) room1, user2Rooms.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateGroupChat() {
|
||||
List<String> participants = Arrays.asList("User1", "User2", "User3");
|
||||
int roomId = chatService.createGroupChat("Gruppenchat", participants);
|
||||
|
||||
assertTrue(roomId >= 1000);
|
||||
String roomInfo = chatService.getChatRoomInfo(roomId);
|
||||
assertTrue(roomInfo.contains("Gruppenchat"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCreateGroupChatWithTooFewParticipants() {
|
||||
List<String> participants = Arrays.asList("User1");
|
||||
chatService.createGroupChat("UngültigerChat", participants);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddContact() {
|
||||
boolean result = chatService.addContact("User2", "User1");
|
||||
assertTrue(result);
|
||||
|
||||
List<String> contacts = chatService.getUserContacts("User1");
|
||||
assertEquals(1, contacts.size());
|
||||
assertTrue(contacts.get(0).contains("User2"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testAddContactNonExistentUser() {
|
||||
chatService.addContact("NonExistent", "User1");
|
||||
}
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
package test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import fassade.ChatService;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class IntegrationTest {
|
||||
|
||||
@Test
|
||||
public void testCompleteChatFlow() {
|
||||
ChatService chatService = new ChatService();
|
||||
|
||||
// Benutzer erstellen
|
||||
chatService.createUser("Alice");
|
||||
chatService.createUser("Bob");
|
||||
|
||||
// Chatroom erstellen
|
||||
int roomId = chatService.createChatRoom("Alice-Bob-Chat", "Alice", "Bob");
|
||||
|
||||
// Nachrichten austauschen
|
||||
chatService.sendMessage(roomId, "Alice", "Hallo Bob, wie geht's?");
|
||||
chatService.sendMessage(roomId, "Bob", "Hallo Alice! Mir geht's gut, danke!");
|
||||
chatService.sendMessage(roomId, "Alice", "Lust auf Kaffee?");
|
||||
|
||||
// Nachrichten überprüfen
|
||||
List<String> messages = chatService.showMessages(roomId);
|
||||
assertEquals(3, messages.size());
|
||||
assertTrue(messages.get(0).contains("Hallo Bob, wie geht's?"));
|
||||
assertTrue(messages.get(1).contains("Mir geht's gut, danke!"));
|
||||
assertTrue(messages.get(2).contains("Lust auf Kaffee?"));
|
||||
|
||||
// Chaträume der Benutzer überprüfen
|
||||
assertEquals(1, chatService.getUserChatRoomIds("Alice").size());
|
||||
assertEquals(1, chatService.getUserChatRoomIds("Bob").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContactsWorkflow() {
|
||||
ChatService chatService = new ChatService();
|
||||
|
||||
chatService.createUser("Alice");
|
||||
chatService.createUser("Bob");
|
||||
chatService.createUser("Charlie");
|
||||
|
||||
// Kontakte hinzufügen
|
||||
chatService.addContact("Bob", "Alice");
|
||||
chatService.addContact("Charlie", "Alice");
|
||||
|
||||
// Kontakte überprüfen
|
||||
List<String> contacts = chatService.getUserContacts("Alice");
|
||||
assertEquals(2, contacts.size());
|
||||
|
||||
// Gruppenchat mit Kontakten erstellen
|
||||
List<String> participants = Arrays.asList("Alice", "Bob", "Charlie");
|
||||
int roomId = chatService.createGroupChat("Freunde-Chat", participants);
|
||||
|
||||
// Chat nutzen
|
||||
chatService.sendMessage(roomId, "Alice", "Hallo zusammen!");
|
||||
chatService.sendMessage(roomId, "Bob", "Hallo Alice!");
|
||||
|
||||
List<String> messages = chatService.showMessages(roomId);
|
||||
assertEquals(2, messages.size());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue