From 077bde2e80e8b8bae92dc4da254e3fbc63c1bd7b Mon Sep 17 00:00:00 2001
From: tmnaqeesha <tmnaqeesha@users.noreply.github.com>
Date: Mon, 26 May 2025 03:18:39 +0200
Subject: [PATCH] fix: Fix reviewItemFilm test file

---
 src/tests/ReviewItemFilmTest.java | 119 ++++++++++++++++++++++--------
 1 file changed, 89 insertions(+), 30 deletions(-)

diff --git a/src/tests/ReviewItemFilmTest.java b/src/tests/ReviewItemFilmTest.java
index 43c3711..c54a6c4 100644
--- a/src/tests/ReviewItemFilmTest.java
+++ b/src/tests/ReviewItemFilmTest.java
@@ -1,65 +1,124 @@
 package tests;
 
-import opinion.ISocialNetwork;
-import opinion.SocialNetwork;
 import exceptions.BadEntryException;
-import exceptions.NotMemberException;
 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 {
 
-    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 {
             float avg = sn.reviewItemFilm(login, password, title, mark, comment);
-            System.out.println("Test " + testId + " passed. Avg mark: " + avg);
-            return 0;
-        } catch (BadEntryException | NotMemberException | NotItemException e) {
-            System.out.println("Test " + testId + " threw expected exception: " + e.getMessage());
-            return 0;
+            if (Math.abs(avg - expectedAverage) < 0.01) {
+                return 0;
+            } else {
+                System.out.println("Err " + testId + " : Expected average " + expectedAverage + " but got " + avg);
+                return 1;
+            }
         } 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;
         }
     }
 
-    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();
         int nbTests = 0;
         int nbErrors = 0;
 
         try {
-            sn.addMember("alice", "strongpass", "cinema lover");
-            sn.addItemFilm("alice", "strongpass", "The Matrix", "Sci-Fi", "Wachowski", "Wachowski", 136);
+            sn.addMember("Neva", "pass123", "casual reader");
+            sn.addMember("Maryam", "1234pass", "fantasy reader");
+            sn.addItemFilm("Neva", "pass123", "Inception", "Sci-Fi", "Nolan", "Nolan", 120);
         } catch (Exception 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++;
-        nbErrors += reviewItemFilmCheck(sn, "alice", "strongpass", "The Matrix", 4.5f, "Amazing film!",
-                "4.1", "Valid review rejected");
+        nbErrors += checkReviewItemFilmException(sn, "unknown", "password", "Inception", 4.0f, "Not bad",
+                NotMemberException.class, "4.10");
 
-        // Test out of range
+        // Test 4.11: item does not exist
         nbTests++;
-        nbErrors += reviewItemFilmCheck(sn, "alice", "strongpass", "The Matrix", 6.0f, "Too high!",
-                "4.2", "Accepted invalid mark > 5");
+        nbErrors += checkReviewItemFilmException(sn, "Maryam", "1234pass", "Unknown Film", 4.0f, "Great!",
+                NotItemException.class, "4.11");
 
-        // Test with invalid title
+        // Test 4.12: first valid review, expect average 4.0
         nbTests++;
-        nbErrors += reviewItemFilmCheck(sn, "alice", "strongpass", "Unknown Film", 3.0f, "Not found",
-                "4.3", "Accepted review for nonexistent film");
+        nbErrors += checkReviewItemFilmAverage(sn, "Maryam", "1234pass", "Inception", 4.0f, "Great film!",
+                4.0f, "4.12");
 
-        // Test with no registered user
+        // Test 4.13: second review, expect average 3.0
         nbTests++;
-        nbErrors += reviewItemFilmCheck(sn, "bob", "1234", "The Matrix", 3.0f, "Hacked!",
-                "4.4", "Accepted review from non-member");
+        nbErrors += checkReviewItemFilmAverage(sn, "Neva", "pass123", "Inception", 2.0f, "Okay film",
+                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
-        System.out.println("\nReviewItemFilmTest summary: " + nbErrors + " error(s) out of " + nbTests + " test(s).");
+    public static void main(String[] args) {
+        test();
     }
 }
-- 
GitLab