Skip to content
Snippets Groups Projects
Commit 077bde2e authored by tmnaqeesha's avatar tmnaqeesha
Browse files

fix: Fix reviewItemFilm test file

parent 73be244b
No related branches found
No related tags found
1 merge request!9Merge code for iteration3 with main
package tests; package tests;
import opinion.ISocialNetwork;
import opinion.SocialNetwork;
import exceptions.BadEntryException; import exceptions.BadEntryException;
import exceptions.NotMemberException;
import exceptions.NotItemException; import exceptions.NotItemException;
import exceptions.NotMemberException;
import exceptions.NotTestReportException;
import opinion.ISocialNetwork;
import opinion.SocialNetwork;
/** /**
* Tests for the SocialNetwork.reviewItemFilm() method. * Tests for the SocialNetwork.<i>reviewItemFilm()</i> method.
*/ */
public class ReviewItemFilmTest { public class ReviewItemFilmTest {
private static int reviewItemFilmCheck(ISocialNetwork sn, String login, String password, String title, /**
float mark, String comment, String testId, String errorMessage) { * Checks that reviewItemFilm() returns the expected average after adding a review.
*/
private static int checkReviewItemFilmAverage(ISocialNetwork sn, String login, String password, String title,
float mark, String comment, float expectedAverage, String testId) {
try { try {
float avg = sn.reviewItemFilm(login, password, title, mark, comment); float avg = sn.reviewItemFilm(login, password, title, mark, comment);
System.out.println("Test " + testId + " passed. Avg mark: " + avg); if (Math.abs(avg - expectedAverage) < 0.01) {
return 0; return 0;
} catch (BadEntryException | NotMemberException | NotItemException e) { } else {
System.out.println("Test " + testId + " threw expected exception: " + e.getMessage()); System.out.println("Err " + testId + " : Expected average " + expectedAverage + " but got " + avg);
return 0; return 1;
}
} catch (Exception e) { } catch (Exception e) {
System.out.println("Test " + testId + " failed: " + errorMessage + " Unexpected exception: " + e); System.out.println("Err " + testId + " : Unexpected exception " + e);
e.printStackTrace();
return 1; return 1;
} }
} }
public static void main(String[] args) { /**
* Checks that reviewItemFilm() throws the expected exception.
*/
private static int checkReviewItemFilmException(ISocialNetwork sn, String login, String password, String title,
float mark, String comment, Class<?> expectedException, String testId) {
try {
sn.reviewItemFilm(login, password, title, mark, comment);
System.out.println("Err " + testId + " : Expected exception " + expectedException.getSimpleName() + " was not thrown");
return 1;
} catch (Exception e) {
if (e.getClass().equals(expectedException)) {
return 0;
} else {
System.out.println("Err " + testId + " : Unexpected exception " + e);
e.printStackTrace();
return 1;
}
}
}
/**
* Launch all tests for reviewItemFilm().
*/
private static TestReport test() {
ISocialNetwork sn = new SocialNetwork(); ISocialNetwork sn = new SocialNetwork();
int nbTests = 0; int nbTests = 0;
int nbErrors = 0; int nbErrors = 0;
try { try {
sn.addMember("alice", "strongpass", "cinema lover"); sn.addMember("Neva", "pass123", "casual reader");
sn.addItemFilm("alice", "strongpass", "The Matrix", "Sci-Fi", "Wachowski", "Wachowski", 136); sn.addMember("Maryam", "1234pass", "fantasy reader");
sn.addItemFilm("Neva", "pass123", "Inception", "Sci-Fi", "Nolan", "Nolan", 120);
} catch (Exception e) { } catch (Exception e) {
System.out.println("Setup failed: " + e); System.out.println("Setup failed: " + e);
return; e.printStackTrace();
return null;
} }
// Valid test System.out.println("Testing reviewItemFilm()");
// Test 4.7: login is null
nbTests++;
nbErrors += checkReviewItemFilmException(sn, null, "pass123", "Inception", 4.0f, "Great!",
BadEntryException.class, "4.7");
// Test 4.8: password is too short
nbTests++;
nbErrors += checkReviewItemFilmException(sn, "Maryam", "12", "Inception", 4.0f, "Nice!",
BadEntryException.class, "4.8");
// Test 4.9: title is null
nbTests++;
nbErrors += checkReviewItemFilmException(sn, "Maryam", "1234pass", null, 4.0f, "Awesome!",
NotItemException.class, "4.9");
// Test 4.10: member does not exist
nbTests++; nbTests++;
nbErrors += reviewItemFilmCheck(sn, "alice", "strongpass", "The Matrix", 4.5f, "Amazing film!", nbErrors += checkReviewItemFilmException(sn, "unknown", "password", "Inception", 4.0f, "Not bad",
"4.1", "Valid review rejected"); NotMemberException.class, "4.10");
// Test out of range // Test 4.11: item does not exist
nbTests++; nbTests++;
nbErrors += reviewItemFilmCheck(sn, "alice", "strongpass", "The Matrix", 6.0f, "Too high!", nbErrors += checkReviewItemFilmException(sn, "Maryam", "1234pass", "Unknown Film", 4.0f, "Great!",
"4.2", "Accepted invalid mark > 5"); NotItemException.class, "4.11");
// Test with invalid title // Test 4.12: first valid review, expect average 4.0
nbTests++; nbTests++;
nbErrors += reviewItemFilmCheck(sn, "alice", "strongpass", "Unknown Film", 3.0f, "Not found", nbErrors += checkReviewItemFilmAverage(sn, "Maryam", "1234pass", "Inception", 4.0f, "Great film!",
"4.3", "Accepted review for nonexistent film"); 4.0f, "4.12");
// Test with no registered user // Test 4.13: second review, expect average 3.0
nbTests++; nbTests++;
nbErrors += reviewItemFilmCheck(sn, "bob", "1234", "The Matrix", 3.0f, "Hacked!", nbErrors += checkReviewItemFilmAverage(sn, "Neva", "pass123", "Inception", 2.0f, "Okay film",
"4.4", "Accepted review from non-member"); 3.0f, "4.13");
// Print a summary of the tests and return test results
try {
TestReport report = new TestReport(nbTests, nbErrors);
System.out.println("ReviewItemBookTest : " + report);
return report;
} catch (NotTestReportException e) {
System.out.println("Failed to create TestReport");
return null;
}
}
// Resumen public static void main(String[] args) {
System.out.println("\nReviewItemFilmTest summary: " + nbErrors + " error(s) out of " + nbTests + " test(s)."); test();
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment