feat(event): ContactWithEventTest3.java: test passé (2.6.3)

This commit is contained in:
bouclyma 2025-01-07 08:41:51 +01:00
parent df7442f7a6
commit 8b3a0e5b21
4 changed files with 158 additions and 5 deletions

View file

@ -1,6 +1,8 @@
package rtgre.modeles; package rtgre.modeles;
import org.json.JSONObject;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
@ -101,6 +103,20 @@ public class Contact {
return 0; return 0;
} }
public JSONObject toJsonObject() {
return new JSONObject()
.put("login", this.login)
.put("connected", this.connected);
}
public String toJson() {
return toJsonObject().toString();
}
public static Contact fromJSON(JSONObject jsonObject, File banque_avatars) {
return new Contact(jsonObject.getString("login"), jsonObject.getBoolean("connected"), banque_avatars);
}
public static BufferedImage avatarFromLogin(File fichier, String login) throws IOException { public static BufferedImage avatarFromLogin(File fichier, String login) throws IOException {
/** /**
* Renvoie une sous-image en fonction d'une banque d'image et d'un login. * Renvoie une sous-image en fonction d'une banque d'image et d'un login.

View file

@ -110,6 +110,35 @@ public class ChatServer {
//client.echoLoop(); //client.echoLoop();
} }
public ChatClientHandler findClient(Contact contact) {
for (ChatClientHandler user: clientList) {
if (user.user.equals(contact)) {
return user;
}
}
return null;
}
public void sendEventToContact(Contact contact, Event event) {
ChatClientHandler user = findClient(contact);
if (!(user == null)) {
try {
user.send(event.toString());
} catch (Exception e) {
LOGGER.warning("!!Erreur de l'envoi d'Event à %s, fermeture de la connexion".formatted(user.user.getLogin()));
user.close();
}
}
}
public void sendEventToAllContacts(Event event) {
for (Contact contact: contactMap.values()) {
if (contact.isConnected()) {
sendEventToContact(contact, event);
}
}
}
public ContactMap getContactMap() { public ContactMap getContactMap() {
return contactMap; return contactMap;
} }
@ -133,6 +162,8 @@ public class ChatServer {
*/ */
private String ipPort; private String ipPort;
private Contact user;
/** /**
* Initialise les attributs {@link #sock} (socket connecté au client), * Initialise les attributs {@link #sock} (socket connecté au client),
* {@link #out} (flux de caractères UTF-8 en sortie) et * {@link #out} (flux de caractères UTF-8 en sortie) et
@ -220,14 +251,10 @@ public class ChatServer {
} else { } else {
LOGGER.info("Connexion de " + login); LOGGER.info("Connexion de " + login);
contactMap.getContact(login).setConnected(true); contactMap.getContact(login).setConnected(true);
this.user = contactMap.getContact(login);
} }
} }
public ChatClientHandler findClient(Contact contact) {
String login = contact.getLogin();
Contact contactRes = contactMap.get()
}
public void send(String message) throws IOException { public void send(String message) throws IOException {
LOGGER.finest("send: %s".formatted(message)); LOGGER.finest("send: %s".formatted(message));
out.println(message); out.println(message);

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

View file

@ -0,0 +1,110 @@
package rtgre.modeles;
import org.json.JSONObject;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.jupiter.params.provider.Arguments.arguments;
/** Tests unitaires du modèle de base de Contact (étape 1) */
class ContactWithEventTest3 {
static Class<?> classe = Contact.class;
static String module = "rtgre.modeles";
@DisplayName("01-Structure de la classe Contact")
@Nested
class StructureTest {
static List<String> constructeursSignatures;
static List<String> methodesSignatures;
@BeforeAll
static void init() {
Constructor<?>[] constructeurs = classe.getConstructors();
constructeursSignatures = Arrays.stream(constructeurs).map(Constructor::toString).collect(Collectors.toList());
Method[] methodes = classe.getDeclaredMethods();
methodesSignatures = Arrays.stream(methodes).map(Method::toString).collect(Collectors.toList());
}
static Stream<Arguments> methodesProvider3() {
return Stream.of(
arguments("toJsonObject", "public org.json.JSONObject %s.Contact.toJsonObject()"),
arguments("toJson", "public java.lang.String %s.Contact.toJson()"),
arguments("fromJSON", "public static %s.Contact %s.Contact.fromJSON(org.json.JSONObject,java.io.File)")
);
}
// @Disabled("Jusqu'à ce que soit codé les events")
@DisplayName("Déclaration des méthodes (avec event et JSON)")
@ParameterizedTest
@MethodSource("methodesProvider3")
void testDeclarationMethodes3(String nom, String signature) {
Assertions.assertTrue(methodesSignatures.contains(String.format(signature, module, module)),
String.format("Méthode non déclarée : doit être %s\nalors que sont déclarés %s",
signature, methodesSignatures));
}
}
@DisplayName("07-Représentation JSON")
@Nested
class JSONTest {
@Test
@DisplayName("JSONObject")
void TestToJSONObject() {
String erreur = "Représentation JSON erronée";
Contact fifi = new Contact("fifi", true, (Image) null);
JSONObject json = fifi.toJsonObject();
Assertions.assertTrue(json.has("login"), erreur);
Assertions.assertEquals("fifi", json.get("login"), erreur);
Assertions.assertTrue(json.has("connected"), erreur);
Assertions.assertEquals(true, json.get("connected"), erreur);
}
@Test
@DisplayName("Sérialisation JSON")
void TestToJSON() {
String erreur = "Sérialisation de la représentation JSON erronée";
Contact riri = new Contact("riri", true, (Image) null);
String json = riri.toJson();
Assertions.assertTrue(json.contains("\"login\":\"riri\""), erreur);
Assertions.assertTrue(json.contains("\"connected\":true"), erreur);
}
@Test
@DisplayName("Contact à partir d'un JSON")
void TestConstructeur() throws IOException {
String work_dir = System.getProperty("user.dir");
Assertions.assertTrue(work_dir.endsWith("chat"),
"Le working dir doit être <projet>/chat/ et non : " + work_dir);
File f = new File("src/main/resources/rtgre/chat/banque_avatars.png");
String json = "{\"login\":\"riri\",\"connected\":true}";
Contact toto = Contact.fromJSON(new JSONObject(json), f);
Assertions.assertEquals("riri", toto.getLogin(), "Login erroné");
Assertions.assertEquals(true, toto.isConnected(), "Connected erroné");
Assertions.assertNotNull(toto.getAvatar(), "Avatar non chargé");
}
}
}