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

add: ConsultItemBookTest

parent 26efaacb
No related branches found
No related tags found
1 merge request!7Merge code for iteration3 with main
package tests;
import opinion.SocialNetwork;
import opinion.ISocialNetwork;
import exceptions.BadEntryException;
import exceptions.ItemBookAlreadyExistsException;
import exceptions.MemberAlreadyExistsException;
import exceptions.NotMemberException;
import java.util.LinkedList;
public class ConsultItemBookTest {
private static int nbTests = 0;
private static int nbErrors = 0;
private static void displaySuccess(String message) {
System.out.println("Test passed: " + message);
}
......@@ -12,8 +22,58 @@ public class ConsultItemBookTest {
System.out.println("Test failed: " + message);
nbErrors++;
}
public static void testConsultItemBook() {
private static void testExpectedException(ISocialNetwork sn, String title, Class<?> expectedException, String testId) {
nbTests++;
try {
sn.consultItems(title);
displayError("[" + testId + "] Expected exception " + expectedException.getSimpleName() + " but none was thrown");
} catch (Exception e) {
if (e.getClass().equals(expectedException)) {
displaySuccess("[" + testId + "] Correctly threw " + expectedException.getSimpleName());
} else {
displayError("[" + testId + "] Unexpected exception: " + e);
}
}
}
private static void testConsultResult(ISocialNetwork sn, String title, int expectedSize, String testId) {
nbTests++;
try {
LinkedList<String> results = sn.consultItems(title);
if (results.size() == expectedSize) {
displaySuccess("[" + testId + "] Returned expected number of results: " + expectedSize);
} else {
displayError("[" + testId + "] Expected " + expectedSize + " but got " + results.size());
}
} catch (Exception e) {
displayError("[" + testId + "] Unexpected exception: " + e);
}
}
public static void main(String[] args) {
ISocialNetwork sn = new SocialNetwork();
try {
sn.addMember("user1", "pass1234", "profile");
sn.addItemBook("user1", "pass1234", "Don Quijote", "Novela", "Cervantes", 800);
sn.addItemBook("user1", "pass1234", "El Principito", "Fábula", "Saint-Exupéry", 100);
sn.addItemBook("user1", "pass1234", "Principios de Física", "Ciencia", "Tipler", 1200);
} catch (Exception e) {
System.out.println("Setup failed: " + e);
return;
}
// Exception cases
testExpectedException(sn, null, BadEntryException.class, "3.8");
testExpectedException(sn, " ", BadEntryException.class, "3.9");
testExpectedException(sn, "a", BadEntryException.class, "3.10");
// Success cases
testConsultResult(sn, "princip", 2, "3.11"); // It returns two books "El principito" and "Principios de la fisica"
testConsultResult(sn, "DON", 1, "3.12"); // test case-sensitive
testConsultResult(sn, "XYZ", 0, "3.13"); // no results
System.out.println("\nConsultItemBookTest: " + nbTests + " tests run, " + nbErrors + " errors.");
}
}
......@@ -60,12 +60,12 @@ public class ReviewItemBookTest {
return null;
}
// --- Casos exitosos ---
testExpectedAverage(sn, "ana", "pass123", "El Hobbit", 4.0f, "Muy bueno", 4.0f, "1.1");
testExpectedAverage(sn, "luis", "1234pass", "El Hobbit", 5.0f, "Obra maestra", 4.5f, "1.2");
testExpectedAverage(sn, "ana", "pass123", "El Hobbit", 3.0f, "Bueno pero largo", 4.0f, "1.3");
// --- Sucess cases ---
testExpectedAverage(sn, "ana", "pass123", "El Hobbit", 4.0f, "So good", 4.0f, "1.1");
testExpectedAverage(sn, "luis", "1234pass", "El Hobbit", 5.0f, "excelent!", 4.5f, "1.2");
testExpectedAverage(sn, "ana", "pass123", "El Hobbit", 3.0f, "good but long", 4.0f, "1.3");
// --- Casos de error esperados ---
// --- Cases with exception ---
testExpectedException(sn, "mario", "nopass", "El Hobbit", 4.0f, "Hola", NotMemberException.class, "3.1");
testExpectedException(sn, "ana", "pass123", "Book X", 3.0f, "Does not exist", NotItemException.class, "3.2");
testExpectedException(sn, "ana", "pass123", "El Hobbit", 6.0f, "Out of range", BadEntryException.class, "3.3");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment