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

fix: Modify comments and AddItemBookTest

parent c56c013b
No related branches found
No related tags found
1 merge request!5Merge code for Iteration 2 with main
...@@ -92,7 +92,7 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -92,7 +92,7 @@ public class SocialNetwork implements ISocialNetwork {
if(login == null || login.length() <= 1){ if(login == null || login.length() <= 1){
throw new BadEntryException("login is null or empty"); 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"); throw new BadEntryException("password is null or empty");
} }
return true; return true;
......
package tests; package tests;
import opinion.ISocialNetwork;
import opinion.SocialNetwork;
import exceptions.BadEntryException; import exceptions.BadEntryException;
import exceptions.MemberAlreadyExistsException;
import exceptions.NotMemberException;
import exceptions.ItemBookAlreadyExistsException; import exceptions.ItemBookAlreadyExistsException;
import exceptions.NotTestReportException; import exceptions.NotTestReportException;
import exceptions.NotMemberException;
import opinion.ISocialNetwork;
import opinion.SocialNetwork;
/**
* Tests for the SocialNetwork.<i>addItemBook()</i> method.
*/
public class AddItemBookTest { public class AddItemBookTest {
/** /**
* Function to test if we catch correctly bad entries * Check that the method addItemBook throws BadEntryException or NotMemberException correctly.
* @param sn * If OK, the method just returns 0. If not OK, displays an error message and returns 1.
* @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
* @param sn * @param sn
* - the <i>ISocialNetwork</i>
* @param login * @param login
* @param pwd * - login of the member
* @param password
* - password of the member
* @param title * @param title
* - title of the book
* @param kind * @param kind
* - kind of the book
* @param author * @param author
* - author of the book
* @param nbPages * @param nbPages
* - number of pages of the book
* @param expectedException
* - the expected exception class
* @param testId * @param testId
* @param errorMessage * - the test ID that will prefix any error message displayed by this method
* @return * @return 0 if the test is OK, 1 if not
*/ */
private static int addItemBookAlreadyExistsTest(ISocialNetwork sn, private static int checkAddItemBookException(ISocialNetwork sn, String login, String password, String title, String kind, String author, int nbPages, Class<?> expectedException, String testId) {
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 { try {
sn.addItemBook(login, pwd, title, kind, author, nbPages); // Try to add this book that exists sn.addItemBook(login, password, title, kind, author, nbPages);
System.out.println("Err " + testId + " : " + errorMessage); // display error message System.out.println("Err " + testId + " : Expected exception " + expectedException.getSimpleName() + " was not thrown");
return 1; // and return the "error" value return 1;
} catch ( ItemBookAlreadyExistsException e) {// AlreadyExists exception was launched } catch (Exception e) {
//checking sn changes if (e.getClass().equals(expectedException)) {
if (sn.nbBooks() != nbBooks) { return 0;
System.out } else {
.println("Err " System.out.println("Err " + testId + " : Unexpected exception " + e);
+ testId e.printStackTrace();
+ " : ItemBookAlreadyExists was thrown, but the number of books was changed"); // Display return 1;
// 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
} }
} }
/** /**
* 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 * @param sn
* - the <i>ISocialNetwork</i>
* @param login * @param login
* @param pwd * - login of the member
* @param password
* - password of the member
* @param title * @param title
* - title of the book
* @param kind * @param kind
* - kind of the book
* @param author * @param author
* - author of the book
* @param nbPages * @param nbPages
* - number of pages of the book
* @param testId * @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, private static int checkAddItemBookSuccess(ISocialNetwork sn, String login, String password, String title, String kind, String author, int nbPages, String testId) {
String pwd, String title, String kind, String author, int nbPages, String testId) {
int nbBooks = sn.nbBooks(); // Number of books
try { try {
sn.addItemBook(login, pwd, title, kind, author, nbPages); // Try to add this book sn.addItemBook(login, password, title, kind, author, nbPages);
if (sn.nbBooks() != nbBooks + 1) { // Number does not changed return 0;
System.out.println("Err " + testId } catch (Exception e) {
+ " : the number of books (" + nbBooks System.out.println("Err " + testId + " : Unexpected exception " + e);
+ ") was not incremented"); // Error message displayed e.printStackTrace();
return 1; // return error code return 1;
} 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
} }
} }
/** /**
* Function to test if the method send a error the member is not registerd * Main test for addItemBook:
* @param sn * <ul>
* @param login * <li>check if the method throws BadEntryException or NotMemberException in non-nominal conditions</li>
* @param pwd * <li>check if the method works correctly in nominal conditions</li>
* @param title * </ul>
* @param kind *
* @param author * @return a summary of the performed tests
* @param nbPages
* @param testId
* @param errorMessage
* @return
*/ */
private static int NotMemberTest(ISocialNetwork sn, public static TestReport test() {
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
}
} ISocialNetwork sn = new SocialNetwork();
/**
* 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
try { try {
sn.addMember(login, pwd, profile); // Try to add this member sn.addMember("usertest", "passwordtest1", "profile1");
// No exception was thrown. That's a good start ! } catch (BadEntryException | MemberAlreadyExistsException e) {
if (sn.nbMembers() != nbMembers + 1) { // But the number of members System.out.println("Err : Unexpected exception " + e);
// hasn't changed e.printStackTrace();
// 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
} }
}
public static TestReport test(){
ISocialNetwork sn = new SocialNetwork();
int nbBooks = sn.nbBooks(); // number of books in 'sn' (should be 0
int nbTests = 0;
int nbTests = 0; // total number of performed tests int nbErrors = 0;
int nbErrors = 0; // total number of failed tests
System.out.println("Testing addItemBook()"); 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++; nbTests++;
nbErrors += addItemBookBadEntryTest(sn, null, "password1.1", "test1.1","drame","eleve",10, "1.1", nbErrors += checkAddItemBookException(sn, "", "passwordtest1", "New Book", "Comedy", "Juan Perez", 300, BadEntryException.class, "2.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
// populate 'sn' with 3 books // Scenario 2: Invalid password(less than 8 characters)
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");
nbTests++; nbTests++;
nbErrors += addItemBookOKTest(sn,"Alice", "alice","Les fleurs du mal","Poesie","Charles Baudelaire",151, "2.1c"); nbErrors += checkAddItemBookException(sn, "usertest", "abc", "New Book", "Comedy", "Juan Perez", 300, BadEntryException.class, "2.2");
// try to add already registered books
// Scenario 3: Invalid title (empty)
nbTests++; nbTests++;
nbErrors += addItemBookAlreadyExistsTest(sn, "Paul","paul",new String("Germinal"),"Roman", "Emile Zola",462, "2.2", nbErrors += checkAddItemBookException(sn, "usertest", "passwordtest1", "", "Comedy", "Juan Perez", 300, BadEntryException.class, "2.3");
"The first book with the same title's name was accepted as a new book ");
// Scenario 4: Uninstantiated kind
nbTests++; nbTests++;
nbErrors += addItemBookAlreadyExistsTest(sn, "Alice", "alice",new String("Les fleurs du mal"),"Poesie","Charles Baudelaire",151, "2.3", nbErrors += checkAddItemBookException(sn, "usertest", "passwordtest1", "New Book", null, "Juan Perez", 300, BadEntryException.class, "2.4");
"The last book was accepted as a new book");
// Scenario 5: Uninstantiated author
nbTests++; nbTests++;
nbErrors += addItemBookAlreadyExistsTest(sn,"Antoine","antoine",new String("lEs miSerabLes"),"Roman","Victor Hugo",2598,"2.4", nbErrors += checkAddItemBookException(sn, "usertest", "passwordtest1", "New Book", "Comedy", null, 300, BadEntryException.class, "2.5");
"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");
// 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++; 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++; 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++; 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' // Scenario 2: Adding an existing book
System.out.println("Final state of the social network : " + sn); 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 // Print a summary of the tests and return test results
try{ try {
TestReport tr = new TestReport(nbTests, nbErrors); TestReport tr = new TestReport(nbTests, nbErrors);
System.out.println("AddItemBookTest : " + tr); System.out.println("AddItemBookTest : " + tr);
return tr; return tr;
} } catch (NotTestReportException e) { // This shouldn't happen
catch (NotTestReportException e){ System.out.println("Unexpected error in AddItemBookTest test code - Can't return valuable test results");
System.out.println("Unexpected error in AddMemberTest test code - Can't return valuable test results");
return null; return null;
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
test(); test();
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment