Skip to content
Snippets Groups Projects
Commit 248c744e authored by MASSY FERNANDEZ Neva Aracely's avatar MASSY FERNANDEZ Neva Aracely
Browse files

fix: ConsultITemFilmTest

parent f052e54b
No related branches found
No related tags found
1 merge request!9Merge code for iteration3 with main
No preview for this file type
......@@ -304,13 +304,24 @@ public class SocialNetwork implements ISocialNetwork {
if (title == null || title.trim().length() <= 1) {
throw new BadEntryException("Error: Title is null or too short");
}
String formattedTitle = title.trim().toLowerCase();
LinkedList<String> result = new LinkedList<>();
for (Book b : listBook) {
if (b.getTitle().toLowerCase().contains(formattedTitle)) {
result.add(b.toString());
}
}
for (Film f : listFilm) {
if (f.getTitle().toLowerCase().contains(formattedTitle)
|| f.getDirector().toLowerCase().contains(formattedTitle)
|| f.getScenarist().toLowerCase().contains(formattedTitle)) {
result.add(f.toString());
}
}
return result;
}
......
package tests;
import exceptions.*;
import opinion.ISocialNetwork;
import opinion.SocialNetwork;
import java.util.LinkedList;
public class ConsultItemFilmTest {
private static int checkConsultItemFilmException(ISocialNetwork sn, String title, Class<?> expectedException, String testId) {
try {
sn.consultItems(title);
System.out.println("Err " + testId + " : Expected exception " + expectedException.getSimpleName() + " was not thrown");
return 1;
} catch (Exception e) {
if (e.getClass().equals(expectedException)) {
System.out.println("Test " + testId + " passed (caught expected " + expectedException.getSimpleName() + ")");
return 0;
} else {
System.out.println("Err " + testId + " : Unexpected exception " + e);
e.printStackTrace();
return 1;
}
}
}
private static int checkConsultItemFilmResult(ISocialNetwork sn, String title, int expectedSize, String testId) {
try {
LinkedList<String> results = sn.consultItems(title);
if (results.size() == expectedSize) {
System.out.println("Test " + testId + " passed.");
return 0;
} else {
System.out.println("Err " + testId + " : Expected " + expectedSize + " result(s) but got " + results.size());
return 1;
}
} catch (Exception e) {
System.out.println("Err " + testId + " : Unexpected exception " + e);
e.printStackTrace();
return 1;
}
}
public static void main(String[] args) {
ISocialNetwork sn = new SocialNetwork();
int nbTests = 0;
int nbErrors = 0;
try {
sn.addMember("cinefan", "securepass", "film fan");
sn.addItemFilm("cinefan", "securepass", "Interstellar", "Sci-Fi", "Christopher Nolan", "Jonathan Nolan", 169);
sn.addItemFilm("cinefan", "securepass", "Inception", "Thriller", "Christopher Nolan", "Christopher Nolan", 148);
} catch (Exception e) {
System.out.println("Setup failed: " + e);
return;
}
// Nominal tests
nbTests++;
nbErrors += checkConsultItemFilmResult(sn, "Inception", 1, "3.1");
nbTests++;
nbErrors += checkConsultItemFilmResult(sn, "nolan", 2, "3.2");
nbTests++;
nbErrors += checkConsultItemFilmResult(sn, "Unknown Film", 0, "3.3");
// Non-nominal test (expects exception)
nbTests++;
nbErrors += checkConsultItemFilmException(sn, "", BadEntryException.class, "3.4");
System.out.println("\nConsultItemFilmTest summary: " + nbErrors + " error(s) out of " + nbTests + " test(s).");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment