diff --git a/src/tests/AddItemFilmTest.java b/src/tests/AddItemFilmTest.java index bdd811f5cb1f6b51a3780e111e43941ebf13e0aa..ebe188f98d239647329b6bbd2ba434ea759af2d4 100644 --- a/src/tests/AddItemFilmTest.java +++ b/src/tests/AddItemFilmTest.java @@ -1,62 +1,138 @@ package tests; -import opinion.ISocialNetwork; -import opinion.SocialNetwork; import exceptions.BadEntryException; -import exceptions.MemberAlreadyExistsException; -import exceptions.NotMemberException; import exceptions.ItemFilmAlreadyExistsException; +import exceptions.MemberAlreadyExistsException; +import exceptions.NotTestReportException; +import opinion.ISocialNetwork; +import opinion.SocialNetwork; /** - * Tests for the SocialNetwork.addItemFilm() method. + * Tests for the SocialNetwork.<i>addItemFilm()</i> method. */ public class AddItemFilmTest { - private static int addItemFilmCheck(ISocialNetwork sn, String login, String password, String title, - String kind, String director, String scenarist, int duration, - String testId, String errorMessage) { + /** + * Check that the method addItemFilm() throws the expected exception. + * + * @param sn the <i>ISocialNetwork</i> to test + * @param login login of the member + * @param password password of the member + * @param title title of the film + * @param kind kind of the film + * @param director director of the film + * @param scenarist scenarist of the film + * @param duration duration of the film (in minutes) + * @param testId id of the test + * @param expectedException error message to display if test fails + * @return 0 if the exception was correctly thrown, 1 otherwise + */ + private static int checkAddItemFilmException(ISocialNetwork sn, String login, String password, String title, String kind, String director, String scenarist, int duration, String testId, Class<?> expectedException) { + try { + sn.addItemFilm(login, password, title, kind, director, scenarist, duration); + 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; + } + } + + } + + /** + * Check that the method addItemFilm() works in a nominal case. + * + * @param sn the <i>ISocialNetwork</i> to test + * @param login login of the member + * @param password password of the member + * @param title title of the film + * @param kind kind of the film + * @param director director of the film + * @param scenarist scenarist of the film + * @param duration duration of the film (in minutes) + * @param testId id of the test + * @return 0 if film was correctly added, 1 otherwise + */ + private static int checkAddItemFilmSuccess(ISocialNetwork sn, String login, String password, String title, + String kind, String director, String scenarist, int duration, + String testId) { try { sn.addItemFilm(login, password, title, kind, director, scenarist, duration); - System.out.println("Test " + testId + " passed."); - return 0; - } catch (BadEntryException | NotMemberException | ItemFilmAlreadyExistsException e) { - System.out.println("Test " + testId + " threw exception: " + e.getMessage()); return 0; } catch (Exception e) { - System.out.println("Test " + testId + " failed: " + errorMessage + " Unexpected exception: " + e); + System.out.println("Err " + testId + " : Unexpected exception " + e); return 1; } } - public static void main(String[] args) { + /** + * Main test for addItemFilm: + * Checks if the method throws BadEntryException or itemFilmAlreadyExistsException in non-nominal conditions + * Checks if the method works correctly in nominal conditions + * + * @return a summary of the performed tests + */ + public static TestReport test() { + ISocialNetwork sn = new SocialNetwork(); int nbTests = 0; int nbErrors = 0; - // Primero registramos un miembro try { - sn.addMember("john", "password123", "film critic"); - } catch (Exception e) { - System.out.println("Error setting up test member: " + e); - return; + sn.addMember("taylorSwift", "password", "profile2"); + } catch (BadEntryException | MemberAlreadyExistsException e) { + System.out.println("Err : Unexpected exception " + e); + e.printStackTrace(); } - // Test válido + // Non-nominal scenarios except 4.5 + + // Test 4.1: Login is null + nbTests++; + nbErrors += checkAddItemFilmException(sn, null, "password", "Inception", "Sci-Fi", "Nolan", "Nolan", 120, + "4.1", BadEntryException.class); + + // Test 4.2: Password is too short + nbTests++; + nbErrors += checkAddItemFilmException(sn, "taylorSwift", "123", "Inception", "Sci-Fi", "Nolan", "Nolan", 120, + "4.2", BadEntryException.class); + + // Test 4.3: Title is null nbTests++; - nbErrors += addItemFilmCheck(sn, "john", "password123", "Inception", "Sci-Fi", "Christopher Nolan", "Christopher Nolan", 148, - "1.1", "Failed to add valid film"); + nbErrors += checkAddItemFilmException(sn, "taylorSwift", "password", null, "Sci-Fi", "Nolan", "Nolan", 120, + "4.3", BadEntryException.class); - // Test con duración inválida + // Test 4.4: Duration is negative nbTests++; - nbErrors += addItemFilmCheck(sn, "john", "password123", "Matrix", "Action", "Wachowskis", "Wachowskis", -120, - "1.2", "Negative duration accepted"); + nbErrors += checkAddItemFilmException(sn, "taylorSwift", "password", "Inception", "Sci-Fi", "Nolan", "Nolan", -10, + "4.4", BadEntryException.class); - // Test con login incorrecto + // Test 4.5: Valid film creation (Nominal) nbTests++; - nbErrors += addItemFilmCheck(sn, "unknown", "password123", "Avatar", "Fantasy", "James Cameron", "James Cameron", 162, - "1.3", "Unregistered member accepted"); + nbErrors += checkAddItemFilmSuccess(sn, "taylorSwift", "password", "Inception", "Sci-Fi", "Nolan", "Nolan", 120, + "4.5"); - // Resumen - System.out.println("\nAddItemFilmTest summary: " + nbErrors + " error(s) out of " + nbTests + " test(s)."); + // Test 4.6: Duplicate film creation + nbTests++; + nbErrors += checkAddItemFilmException(sn, "taylorSwift", "password", "Inception", "Sci-Fi", "Nolan", "Nolan", 120, + "4.6", ItemFilmAlreadyExistsException.class); + + try { + TestReport tr = new TestReport(nbTests, nbErrors); + System.out.println("AddItemBookTest : " + tr); + return tr; + } catch (NotTestReportException e) { + System.out.println("Unexpected error in AddItemBookTest test code - Can't return valuable test results"); + return null; + } + } + + public static void main(String[] args) { + test(); } -} +} \ No newline at end of file