diff --git a/src/opinion/SocialNetwork.java b/src/opinion/SocialNetwork.java index 11431320dc72a126ae6fae45b34217efd1fe9668..281bc465e6d00bc4ecdb764079382ff13eb74129 100644 --- a/src/opinion/SocialNetwork.java +++ b/src/opinion/SocialNetwork.java @@ -92,7 +92,7 @@ public class SocialNetwork implements ISocialNetwork { if(login == null || login.length() <= 1){ throw new BadEntryException("login is null or empty"); } - if(password == null || password.length() <= 1 ){ + if(password == null || password.length() <=8 ){ throw new BadEntryException("password is null or empty"); } return true; diff --git a/src/tests/AddItemBookTest.java b/src/tests/AddItemBookTest.java index 9c1e0f02f480a7ebc4f47916472c1bc52a7a9d7e..9766796421c74f787a7fd0657912a72f5de5a46c 100644 --- a/src/tests/AddItemBookTest.java +++ b/src/tests/AddItemBookTest.java @@ -1,319 +1,171 @@ package tests; + +import opinion.ISocialNetwork; +import opinion.SocialNetwork; import exceptions.BadEntryException; +import exceptions.MemberAlreadyExistsException; +import exceptions.NotMemberException; import exceptions.ItemBookAlreadyExistsException; import exceptions.NotTestReportException; -import exceptions.NotMemberException; -import opinion.ISocialNetwork; -import opinion.SocialNetwork; +/** + * Tests for the SocialNetwork.<i>addItemBook()</i> method. + */ public class AddItemBookTest { /** - * Function to test if we catch correctly bad entries - * @param sn - * @param login - * @param pwd - * @param title - * @param kind - * @param author - * @param nbPages - * @param testId - * @param errorMessage - * @return - */ - private static int addItemBookBadEntryTest(ISocialNetwork sn, String login, - String pwd, String title, String kind, String author, int nbPages, String testId, String errorMessage) { - - int nbBooks = sn.nbBooks(); // Number of books when starting to run this method - try { - sn.addItemBook(login, pwd, title, kind, author, nbPages); // Try to add this book - System.out.println("Err " + testId + " : " + errorMessage); // display error - return 1; // and return the "error" value - } catch (BadEntryException e) { // BadEntry exception was thrown - // check if 'sn' was not impacted - if (sn.nbBooks() != nbBooks) { - System.out - .println("Err " - + testId - + " : BadEntry was thrown but the number of books was changed"); - // Display error for the number of books - return 1; // return "error" value - } else - // The number of books hasn't changed,'sn' not modified - return 0; // everything seems OK - } catch (Exception e) { // An exception was thrown by addItemBook(), Unexpected exception - System.out.println("Err " + testId + " : unexpected exception. " - + e); // Display a specific error message - e.printStackTrace(); - return 1; // return error value - } - } - - /** - * Function to test if the method send a error when the book already exist + * Check that the method addItemBook throws BadEntryException or NotMemberException correctly. + * If OK, the method just returns 0. If not OK, displays an error message and returns 1. + * * @param sn + * - the <i>ISocialNetwork</i> * @param login - * @param pwd + * - login of the member + * @param password + * - password of the member * @param title + * - title of the book * @param kind + * - kind of the book * @param author + * - author of the book * @param nbPages + * - number of pages of the book + * @param expectedException + * - the expected exception class * @param testId - * @param errorMessage - * @return + * - the test ID that will prefix any error message displayed by this method + * @return 0 if the test is OK, 1 if not */ - private static int addItemBookAlreadyExistsTest(ISocialNetwork sn, - String login, String pwd, String title, String kind, String author, int nbPages, String testId, - String errorMessage) { - int nbBooks = sn.nbBooks(); // Number of members when starting to process this method + private static int checkAddItemBookException(ISocialNetwork sn, String login, String password, String title, String kind, String author, int nbPages, Class<?> expectedException, String testId) { try { - sn.addItemBook(login, pwd, title, kind, author, nbPages); // Try to add this book that exists - System.out.println("Err " + testId + " : " + errorMessage); // display error message - return 1; // and return the "error" value - } catch ( ItemBookAlreadyExistsException e) {// AlreadyExists exception was launched - //checking sn changes - if (sn.nbBooks() != nbBooks) { - System.out - .println("Err " - + testId - + " : ItemBookAlreadyExists was thrown, but the number of books was changed"); // Display - // error message - return 1;// error value assigned - } else - return 0; // OK value - // display - } catch (Exception e) { // An exception was thrown by addMember() - System.out.println("Err " + testId + " : unexpected exception. " + e); // Display unexpected exception - e.printStackTrace(); // Display contextual info about what happened - return 1; // return error value + sn.addItemBook(login, password, title, kind, author, nbPages); + 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; + } } - } /** - * Function to test if the method work correctly + * Check that the method addItemBook succeeds. + * If OK, the method just returns 0. If not OK, displays an error message and returns 1. + * * @param sn + * - the <i>ISocialNetwork</i> * @param login - * @param pwd + * - login of the member + * @param password + * - password of the member * @param title + * - title of the book * @param kind + * - kind of the book * @param author + * - author of the book * @param nbPages + * - number of pages of the book * @param testId - * @return + * - the test ID that will prefix any error message displayed by this method + * @return 0 if the test is OK, 1 if not */ - private static int addItemBookOKTest(ISocialNetwork sn, String login, - String pwd, String title, String kind, String author, int nbPages, String testId) { - int nbBooks = sn.nbBooks(); // Number of books + private static int checkAddItemBookSuccess(ISocialNetwork sn, String login, String password, String title, String kind, String author, int nbPages, String testId) { try { - sn.addItemBook(login, pwd, title, kind, author, nbPages); // Try to add this book - if (sn.nbBooks() != nbBooks + 1) { // Number does not changed - System.out.println("Err " + testId - + " : the number of books (" + nbBooks - + ") was not incremented"); // Error message displayed - return 1; // return error code - } else - return 0; // return OK code - // display - } catch (Exception e) {// An exception was thrown by addMember() - System.out - .println("Err " + testId + " : unexpected exception " + e); // Error message - e.printStackTrace(); // Display contextual info about what happened - return 1; // return error code + sn.addItemBook(login, password, title, kind, author, nbPages); + return 0; + } catch (Exception e) { + System.out.println("Err " + testId + " : Unexpected exception " + e); + e.printStackTrace(); + return 1; } } /** - * Function to test if the method send a error the member is not registerd - * @param sn - * @param login - * @param pwd - * @param title - * @param kind - * @param author - * @param nbPages - * @param testId - * @param errorMessage - * @return + * Main test for addItemBook: + * <ul> + * <li>check if the method throws BadEntryException or NotMemberException in non-nominal conditions</li> + * <li>check if the method works correctly in nominal conditions</li> + * </ul> + * + * @return a summary of the performed tests */ - private static int NotMemberTest(ISocialNetwork sn, - String login, String pwd, String title, String kind, String author, int nbPages, String testId, - String errorMessage) { - int nbBooks = sn.nbBooks(); // Number of members when starting to - // process this method - try { - sn.addItemBook(login, pwd, title, kind, author, nbPages); // Try to add this book - // Reaching this point means that no exception was thrown by addItemBook() - System.out.println("Err " + testId + " : " + errorMessage); // display error message - return 1; // and return the "error" value - } catch ( NotMemberException e) {// AlreadyExists exception was thrown by addItemBook() : - //check if 'sn' was not impacted - if (sn.nbBooks() != nbBooks) { - System.out - .println("Err " - + testId - + " : NotMember was thrown, but the number of books was changed"); // Display - // a error message launched - return 1;// and return the "error" value - } else - return 0; // return OK value - } catch (Exception e) { // An exception was thrown by addMember(), but - // it was not the expected exception - // AlreadyExists - System.out.println("Err " + testId + " : unexpected exception. " - + e); // Display a specific error message - e.printStackTrace(); // Display contextual info about what happened - return 1; // return error value - } + public static TestReport test() { - } - - /** - * Function to test if the method add some members correctly - * @param sn - * @param login - * @param pwd - * @param profile - * @param testId - * @return - */ - private static int addMemberOKTest(ISocialNetwork sn, String login, - String pwd, String profile, String testId) { - int nbMembers = sn.nbMembers(); // Number of members when starting to - // process this method + ISocialNetwork sn = new SocialNetwork(); try { - sn.addMember(login, pwd, profile); // Try to add this member - // No exception was thrown. That's a good start ! - if (sn.nbMembers() != nbMembers + 1) { // But the number of members - // hasn't changed - // accordingly - System.out.println("Err " + testId - + " : the number of members (" + nbMembers - + ") was not incremented"); // Error message displayed - return 1; // return error code - } else - return 0; // return success code - } catch (Exception e) {// An exception was thrown by addMember() : error case - System.out - .println("Err " + testId + " : unexpected exception " + e); // Error message - e.printStackTrace(); // Display contextual info about what happened - return 1; // return error code + sn.addMember("usertest", "passwordtest1", "profile1"); + } catch (BadEntryException | MemberAlreadyExistsException e) { + System.out.println("Err : Unexpected exception " + e); + e.printStackTrace(); } - } - - - public static TestReport test(){ - - ISocialNetwork sn = new SocialNetwork(); - - int nbBooks = sn.nbBooks(); // number of books in 'sn' (should be 0 - - int nbTests = 0; // total number of performed tests - int nbErrors = 0; // total number of failed tests + int nbTests = 0; + int nbErrors = 0; System.out.println("Testing addItemBook()"); - // <=> test n°1 - - // check if incorrect parameters cause addItemBook() to throw BadEntry exception - - //populate 'sn' with 3 members - nbErrors += addMemberOKTest(sn,"Paul","paul","Student","1.0a"); - nbErrors += addMemberOKTest(sn,"Antoine","antoine","Philosopher", "1.0b"); - nbErrors += addMemberOKTest(sn,"Alice", "alice","F1 driver", "1.0c"); + // Non-nominal scenarios + // Scenario 1: Invalid login (empty) nbTests++; - nbErrors += addItemBookBadEntryTest(sn, null, "password1.1", "test1.1","drame","eleve",10, "1.1", - "addItemBook() doesn't reject null logins"); - nbTests++; - nbErrors += addItemBookBadEntryTest(sn," ","password1.2","test1.2","drame","eleve",10,"1.2", - "addItemBook() doesn't reject logins that don't contain at least one character other than space"); - nbTests++; - nbErrors += addItemBookBadEntryTest(sn, "B", null,"test1.3","drame","eleve",10, "1.3", - "addItemBook() doesn't reject null passwords"); - nbTests++; - nbErrors += addItemBookBadEntryTest(sn,"B"," qwd ","test1.4","drame","eleve",10,"1.4", - "addItemBook() doesn't reject passwords that don't contain at least 4 characters (not taking into account leading or trailing blanks)"); - nbTests++; - nbErrors += addItemBookBadEntryTest(sn,"BB", "password1.5", null,"drame","eleve",10, "1.5", - "addItemBook() doesn't reject null title"); - nbTests++; - nbErrors += addItemBookBadEntryTest(sn,"A","password1.6"," ","drame","eleve",10,"1.6", - "addItemBook() doesn't reject title that don't contain at least one character other than space"); - nbTests++; - nbErrors += addItemBookBadEntryTest(sn, "AA", "password1.7", "test1.7",null,"eleve",10, "1.7", - "addItemBook() doesn't reject null kind"); - nbTests++; - nbErrors += addItemBookBadEntryTest(sn, "AAA", "password1.8", "test1.8","drame",null,10, "1.8", - "addItemBook() doesn't reject null author"); - nbTests++; - nbErrors += addItemBookBadEntryTest(sn, "AAA", "password1.8", "test1.8","drame","eleve",-10, "1.8", - "addItemBook() doesn't reject negative number of pages"); - - // <=> test n°2 - - + nbErrors += checkAddItemBookException(sn, "", "passwordtest1", "New Book", "Comedy", "Juan Perez", 300, BadEntryException.class, "2.1"); - // populate 'sn' with 3 books - - nbTests++; - nbErrors += addItemBookOKTest(sn,"Paul","paul","Germinal","Roman","Emile Zola",462,"2.1a"); - nbTests++; - nbErrors += addItemBookOKTest(sn,"Antoine","antoine","Les miserables","Roman","Victor Hugo",2598, "2.1b"); + // Scenario 2: Invalid password(less than 8 characters) nbTests++; - nbErrors += addItemBookOKTest(sn,"Alice", "alice","Les fleurs du mal","Poesie","Charles Baudelaire",151, "2.1c"); - - // try to add already registered books + nbErrors += checkAddItemBookException(sn, "usertest", "abc", "New Book", "Comedy", "Juan Perez", 300, BadEntryException.class, "2.2"); + // Scenario 3: Invalid title (empty) nbTests++; - nbErrors += addItemBookAlreadyExistsTest(sn, "Paul","paul",new String("Germinal"),"Roman", "Emile Zola",462, "2.2", - "The first book with the same title's name was accepted as a new book "); + nbErrors += checkAddItemBookException(sn, "usertest", "passwordtest1", "", "Comedy", "Juan Perez", 300, BadEntryException.class, "2.3"); + // Scenario 4: Uninstantiated kind nbTests++; - nbErrors += addItemBookAlreadyExistsTest(sn, "Alice", "alice",new String("Les fleurs du mal"),"Poesie","Charles Baudelaire",151, "2.3", - "The last book was accepted as a new book"); + nbErrors += checkAddItemBookException(sn, "usertest", "passwordtest1", "New Book", null, "Juan Perez", 300, BadEntryException.class, "2.4"); + // Scenario 5: Uninstantiated author nbTests++; - nbErrors += addItemBookAlreadyExistsTest(sn,"Antoine","antoine",new String("lEs miSerabLes"),"Roman","Victor Hugo",2598,"2.4", - "An already registered book, but with different case, was accepted as login for a new member"); - nbTests++; - nbErrors += addItemBookAlreadyExistsTest(sn,"Antoine","antoine",new String(" Les miserables "),"Roman","Victor Hugo",2598,"2.5", - "An already registered book title, surrounded by leading/trailing blanks, was accepted as title for a new book"); + nbErrors += checkAddItemBookException(sn, "usertest", "passwordtest1", "New Book", "Comedy", null, 300, BadEntryException.class, "2.5"); + // Scenario 6: Invalid number of pages (not strictly positive) + nbTests++; + nbErrors += checkAddItemBookException(sn, "usertest", "passwordtest1", "New Book", "Comedy", "Juan Perez", 0, BadEntryException.class, "2.6"); - //try to enter a book with no register login + // Scenario 7: Non-existent member nbTests++; - nbErrors += NotMemberTest(sn, new String("Clementine"),"bloblo","Le Cygne","Poesie","Charles Baudelaire", 374,"3.1","The login used is not registered"); + nbErrors += checkAddItemBookException(sn, "nonExistentUser", "passwordtest1", "New Book", "Comedy", "Juan Perez", 300, NotMemberException.class, "2.7"); - //try to enter wrong password + // Scenario 8: Invalid password(null) nbTests++; - nbErrors += NotMemberTest(sn,"Paul",new String("blublu"),"Jamais Plus","Roman", "Colleen Hoover", 445,"3.2","You're password is incorrect"); + nbErrors += checkAddItemBookException(sn, "usertest", "", "New Book", "Comedy", "Juan Perez", 300, BadEntryException.class, "2.8"); + + // Nominal scenarios - //try to enter a new book with a good login + // Scenario 1: Valid book addition nbTests++; - nbErrors += addItemBookOKTest(sn,"Alice","alice","Le Cygne","Poesie","Charles Baudelaire", 374,"3.3"); + nbErrors += checkAddItemBookSuccess(sn, "usertest", "passwordtest1", "Effective Java", "Programming", "Joshua Bloch", 416, "2.9"); - // Display final state of 'sn' - System.out.println("Final state of the social network : " + sn); + // Scenario 2: Adding an existing book + nbTests++; + nbErrors += checkAddItemBookException(sn, "usertest", "passwordtest1", "Effective Java", "Programming", "Jose Pablo", 416, ItemBookAlreadyExistsException.class, "2.10"); // Print a summary of the tests and return test results - try{ + try { TestReport tr = new TestReport(nbTests, nbErrors); System.out.println("AddItemBookTest : " + tr); return tr; - } - catch (NotTestReportException e){ - System.out.println("Unexpected error in AddMemberTest test code - Can't return valuable test results"); + } catch (NotTestReportException e) { // This shouldn't happen + System.out.println("Unexpected error in AddItemBookTest test code - Can't return valuable test results"); return null; } - } public static void main(String[] args) { test(); - } - } -