Add Project to my Repo
parent
91a5939d9f
commit
1394257c0b
|
@ -0,0 +1,23 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>MyLocalChat</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
|
@ -0,0 +1,7 @@
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.github.ObaiAlbek</groupId>
|
||||||
|
<artifactId>MyLocalChat</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>LocalChat</name>
|
||||||
|
</project>
|
|
@ -0,0 +1,14 @@
|
||||||
|
package app;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import domain.*;
|
||||||
|
import fassade.ChatService;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,88 @@
|
||||||
|
package domain;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ChatRoom {
|
||||||
|
private static int nextRoomId = 1000;
|
||||||
|
private int roomId;
|
||||||
|
private String roomName;
|
||||||
|
private List<Message> messages;
|
||||||
|
private final LocalDateTime createdAt;
|
||||||
|
private List<User> participants;
|
||||||
|
|
||||||
|
public ChatRoom(String roomName, 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
messages = new ArrayList<>();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getCreatedAt() {
|
||||||
|
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())
|
||||||
|
return new ArrayList<>();
|
||||||
|
|
||||||
|
return messages.stream()
|
||||||
|
.map(Message::toString)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,55 @@
|
||||||
|
package domain;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
|
||||||
|
public class Message {
|
||||||
|
private User sender;
|
||||||
|
private String content;
|
||||||
|
private LocalDateTime timestamp;
|
||||||
|
//private MessageType type;
|
||||||
|
|
||||||
|
public Message(User sender, String content) {
|
||||||
|
super();
|
||||||
|
this.sender = sender;
|
||||||
|
this.content = content;
|
||||||
|
this.timestamp = LocalDateTime.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public User getSender() {
|
||||||
|
return sender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSender(User sender) {
|
||||||
|
this.sender = sender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(String content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getTimestamp() {
|
||||||
|
return timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTimestamp(LocalDateTime timestamp) {
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Message [sender=" + sender.getUsername() + ", content=" + content + ", timestamp="
|
||||||
|
+ timestamp + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package domain;
|
||||||
|
|
||||||
|
public enum MessageType {
|
||||||
|
TEXT,LINK;
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
package domain;
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
private static int nextUserId = 1000;
|
||||||
|
private int userId;
|
||||||
|
private final String username;
|
||||||
|
private boolean isOnline;
|
||||||
|
private UserInfo userInfo;
|
||||||
|
private UserContacts userContacts;
|
||||||
|
|
||||||
|
public User(String username) {
|
||||||
|
this.userId = nextUserId++;
|
||||||
|
this.username = username;
|
||||||
|
this.isOnline = true;
|
||||||
|
this.userInfo = UserInfo.VERFÜGBAR;
|
||||||
|
this.userContacts = new UserContacts();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isOnline() {
|
||||||
|
return isOnline;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOnline(boolean isOnline) {
|
||||||
|
this.isOnline = isOnline;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserInfo getUserInfo() {
|
||||||
|
return userInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserInfo(UserInfo userInfo) {
|
||||||
|
this.userInfo = userInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserContacts getUserContacts() {
|
||||||
|
return userContacts;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User [userId=" + userId + ", username=" + username + ", isOnline=" + isOnline + ", userInfo=" + userInfo
|
||||||
|
+ "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
package domain;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class UserContacts {
|
||||||
|
private List<User> contacts;
|
||||||
|
|
||||||
|
public UserContacts() {
|
||||||
|
this.contacts = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean addContact(User user) {
|
||||||
|
if (user == null)
|
||||||
|
throw new NullPointerException("User darf nicht null sein");
|
||||||
|
|
||||||
|
if (contacts.contains(user))
|
||||||
|
throw new IllegalArgumentException("User existiert bereits in der Kontakt Liste");
|
||||||
|
|
||||||
|
return contacts.add(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean removeContact(User user) {
|
||||||
|
if (user == null)
|
||||||
|
throw new NullPointerException("User darf nicht null sein");
|
||||||
|
return contacts.remove(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasContact(User user) {
|
||||||
|
if (user == null)
|
||||||
|
throw new NullPointerException("User darf nicht null sein");
|
||||||
|
return contacts.contains(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> showAllContacts() {
|
||||||
|
if (contacts == null || contacts.isEmpty()) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
return contacts.stream()
|
||||||
|
.map(User::toString)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<User> getContacts() {
|
||||||
|
return new ArrayList<>(contacts);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getContactCount() {
|
||||||
|
return contacts.size();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
package domain;
|
||||||
|
|
||||||
|
public enum UserInfo {
|
||||||
|
VERFÜGBAR,BESCHÄFTIGT,IN_DER_SCHULE,IM_KINO,BEI_DER_ARBEIT,AKKU_FAST_LEER,SCHLAFE;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,133 @@
|
||||||
|
package fassade;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import domain.*;
|
||||||
|
|
||||||
|
public class ChatService {
|
||||||
|
private Map<Integer, ChatRoom> chatrooms;
|
||||||
|
private Map<String, User> users;
|
||||||
|
|
||||||
|
public ChatService() {
|
||||||
|
chatrooms = new HashMap<>();
|
||||||
|
users = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean createUser(String userName) {
|
||||||
|
if (users.containsKey(userName))
|
||||||
|
throw new IllegalArgumentException("User existiert bereits: " + userName);
|
||||||
|
|
||||||
|
User tempUser = new User(userName);
|
||||||
|
users.put(userName, tempUser);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private User getUser(String userName) {
|
||||||
|
return users.get(userName);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public int createChatRoom(String roomName, 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);
|
||||||
|
chatrooms.put(tempChatRoom.getRoomId(), 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);
|
||||||
|
if (room == null)
|
||||||
|
throw new IllegalArgumentException("Chatroom existiert nicht: " + roomId);
|
||||||
|
|
||||||
|
User sender = getUser(senderName);
|
||||||
|
if (sender == null) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> showMessages(int roomId) {
|
||||||
|
ChatRoom room = chatrooms.get(roomId);
|
||||||
|
if (room == null)
|
||||||
|
throw new IllegalArgumentException("Chatroom existiert nicht: " + roomId);
|
||||||
|
|
||||||
|
return room.showMessages();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean addContact(String contactUserName, String currentUserName) {
|
||||||
|
if (!users.containsKey(contactUserName))
|
||||||
|
throw new IllegalArgumentException("User existiert nicht: " + contactUserName);
|
||||||
|
|
||||||
|
if (!users.containsKey(currentUserName))
|
||||||
|
throw new IllegalArgumentException("User existiert nicht: " + currentUserName);
|
||||||
|
|
||||||
|
User contactUser = users.get(contactUserName);
|
||||||
|
User currentUser = users.get(currentUserName);
|
||||||
|
|
||||||
|
return currentUser.getUserContacts().addContact(contactUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getUserContacts(String userName) {
|
||||||
|
if (!users.containsKey(userName))
|
||||||
|
throw new IllegalArgumentException("User existiert nicht: " + userName);
|
||||||
|
|
||||||
|
User user = users.get(userName);
|
||||||
|
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);
|
||||||
|
if (room == null)
|
||||||
|
throw new IllegalArgumentException("Chatroom existiert nicht: " + roomId);
|
||||||
|
|
||||||
|
return room.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package ui;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.border.EmptyBorder;
|
||||||
|
|
||||||
|
public class Chat extends JFrame {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private JPanel contentPane;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Chat() {
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setBounds(100, 100, 567, 400);
|
||||||
|
contentPane = new JPanel();
|
||||||
|
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||||
|
setContentPane(contentPane);
|
||||||
|
contentPane.setLayout(null);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
package test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import domain.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ChatRoomTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testChatRoomCreation() {
|
||||||
|
User user1 = new User("User1");
|
||||||
|
User user2 = new User("User2");
|
||||||
|
ChatRoom room = new ChatRoom("TestRoom", 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddMessageToChatRoom() {
|
||||||
|
User user1 = new User("User1");
|
||||||
|
User user2 = new User("User2");
|
||||||
|
ChatRoom room = new ChatRoom("TestRoom", user1, user2);
|
||||||
|
|
||||||
|
Message message = new Message(user1, "Hallo!");
|
||||||
|
room.addMessage(message);
|
||||||
|
|
||||||
|
assertEquals(1, room.getMessages().size());
|
||||||
|
assertEquals(message, room.getMessages().get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testShowMessages() {
|
||||||
|
User user1 = new User("User1");
|
||||||
|
User user2 = new User("User2");
|
||||||
|
ChatRoom room = new ChatRoom("TestRoom", user1, user2);
|
||||||
|
|
||||||
|
room.addMessage(new Message(user1, "Nachricht 1"));
|
||||||
|
room.addMessage(new Message(user2, "Nachricht 2"));
|
||||||
|
|
||||||
|
List<String> messages = room.showMessages();
|
||||||
|
assertEquals(2, messages.size());
|
||||||
|
assertTrue(messages.get(0).contains("Nachricht 1"));
|
||||||
|
assertTrue(messages.get(1).contains("Nachricht 2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddParticipant() {
|
||||||
|
User user1 = new User("User1");
|
||||||
|
User user2 = new User("User2");
|
||||||
|
User user3 = new User("User3");
|
||||||
|
ChatRoom room = new ChatRoom("TestRoom", 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);
|
||||||
|
|
||||||
|
assertFalse(room.addParticipant(user1)); // Bereits vorhanden
|
||||||
|
assertEquals(2, room.getParticipants().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,117 @@
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import domain.*;
|
||||||
|
|
||||||
|
|
||||||
|
public class MessageTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMessageCreation() {
|
||||||
|
User sender = new User("Sender");
|
||||||
|
Message message = new Message(sender, "Testnachricht");
|
||||||
|
|
||||||
|
assertEquals(sender, message.getSender());
|
||||||
|
assertEquals("Testnachricht", message.getContent());
|
||||||
|
assertNotNull(message.getTimestamp());
|
||||||
|
assertTrue(message.getTimestamp().isBefore(LocalDateTime.now().plusSeconds(1)));
|
||||||
|
assertTrue(message.getTimestamp().isAfter(LocalDateTime.now().minusSeconds(1)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMessageSetters() {
|
||||||
|
User sender1 = new User("Sender1");
|
||||||
|
User sender2 = new User("Sender2");
|
||||||
|
Message message = new Message(sender1, "Original");
|
||||||
|
|
||||||
|
message.setSender(sender2);
|
||||||
|
message.setContent("Geändert");
|
||||||
|
|
||||||
|
assertEquals(sender2, message.getSender());
|
||||||
|
assertEquals("Geändert", message.getContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMessageToString() {
|
||||||
|
User sender = new User("TestSender");
|
||||||
|
Message message = new Message(sender, "Testinhalt");
|
||||||
|
|
||||||
|
String toString = message.toString();
|
||||||
|
assertTrue(toString.contains("TestSender"));
|
||||||
|
assertTrue(toString.contains("Testinhalt"));
|
||||||
|
assertTrue(toString.contains("timestamp"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testMessageWithNullSender() {
|
||||||
|
new Message(null, "Test");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
package test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import domain.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
|
||||||
|
public class UserTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUserCreation() {
|
||||||
|
User user = new User("TestUser");
|
||||||
|
assertEquals("TestUser", user.getUsername());
|
||||||
|
assertTrue(user.isOnline());
|
||||||
|
assertEquals(UserInfo.VERFÜGBAR, user.getUserInfo());
|
||||||
|
assertTrue(user.getUserId() >= 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUserStatusChange() {
|
||||||
|
User user = new User("TestUser");
|
||||||
|
user.setOnline(false);
|
||||||
|
assertFalse(user.isOnline());
|
||||||
|
|
||||||
|
user.setUserInfo(UserInfo.BESCHÄFTIGT);
|
||||||
|
assertEquals(UserInfo.BESCHÄFTIGT, user.getUserInfo());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUserIdUniqueness() {
|
||||||
|
User user1 = new User("User1");
|
||||||
|
User user2 = new User("User2");
|
||||||
|
User user3 = new User("User3");
|
||||||
|
|
||||||
|
assertNotEquals(user1.getUserId(), user2.getUserId());
|
||||||
|
assertNotEquals(user2.getUserId(), user3.getUserId());
|
||||||
|
assertNotEquals(user1.getUserId(), user3.getUserId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUserToString() {
|
||||||
|
User user = new User("TestUser");
|
||||||
|
String toString = user.toString();
|
||||||
|
assertTrue(toString.contains("TestUser"));
|
||||||
|
assertTrue(toString.contains("VERFÜGBAR"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue