package unitTest; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Date; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestFactory; import org.junit.jupiter.api.function.Executable; import exceptions.*; import project.Auction; import project.Category; import project.Item; import project.User; public class UserUnitTest { @Test public void loginTest() throws UsernameEmailEmptyException, PasswordInvalidException, MailInvalidException, LoginInvalidException { String username = "mario", password = "password", email = "test@gmail.com"; User u = new User(); u.setUsername(username); u.setEmail(email); u.setPassword(password); assertTrue(User.login(username, email, password)); assertThrows(LoginInvalidException.class, () -> User.login(username, email, "wrong password")); } @TestFactory public Collection<DynamicTest> loginAcceptanceTest() { ArrayList<String> username = new ArrayList<String>(Arrays.asList("", "", "mario", "mario", "")); ArrayList<String> email = new ArrayList<String>( Arrays.asList("", "an.gmail.it", "mario@gmail.com", "", "mario@gmail.com")); ArrayList<String> password = new ArrayList<String>( Arrays.asList("test", "test", "", "wrong password", "wrong password")); ArrayList<String> testNames = new ArrayList<String>(Arrays.asList("username and mail empty", "wrong mail format", "password empty", "login invalid with username", "login invalid with email")); ArrayList<Class<? extends Exception>> exceptions = new ArrayList<Class<? extends Exception>>( Arrays.asList(UsernameEmailEmptyException.class, MailInvalidException.class, PasswordInvalidException.class, LoginInvalidException.class, LoginInvalidException.class)); Collection<DynamicTest> dynamicTests = new ArrayList<DynamicTest>(); for (int i = 0; i < username.size(); i++) { String us = username.get(i); String em = email.get(i); String pw = password.get(i); final int j = i; Executable exec = () -> assertThrows(exceptions.get(j), () -> User.login(us, em, pw)); DynamicTest dTest = DynamicTest.dynamicTest(testNames.get(i), exec); dynamicTests.add(dTest); } return dynamicTests; } @Test public void searchItemTest() throws CategoryInvalidException, TitleInvalidException { String title = "Red bike", category = "Sport and Outdoor", category2 = "Cars and Motorbikes"; User u = User.getInstance(); Category c = new Category(category2); ArrayList<Item> result = new ArrayList<Item>(); ArrayList<Item> emptyArray = new ArrayList<Item>(); Item i = new Item(); i.setTitle(title); i.setCategory(new Category(category)); result.add(i); assertEquals(u.searchItem(title, category), result); assertEquals(u.searchItem("ferrari", category), emptyArray); assertEquals(u.searchItem(title, category2), emptyArray); assertEquals(u.searchItem("ferrari", category2), emptyArray); } @TestFactory public Collection<DynamicTest> searchItemAcceptanceTest() { ArrayList<String> title = new ArrayList<String>( Arrays.asList("", "9999", "t-#\\", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "title")); ArrayList<String> category = new ArrayList<String>( Arrays.asList("camera", "camera", "camera", "camera", "bike")); ArrayList<String> testNames = new ArrayList<String>(Arrays.asList("title empty", "numerical title", "symbols present in the title", "title longer than 30 characters", "cateogry not correct")); ArrayList<Class<? extends Exception>> exceptions = new ArrayList<Class<? extends Exception>>( Arrays.asList(TitleInvalidException.class, TitleInvalidException.class, TitleInvalidException.class, TitleInvalidException.class, CategoryInvalidException.class)); Collection<DynamicTest> dynamicTests = new ArrayList<DynamicTest>(); Category c = new Category("camera"); User u = User.getInstance(); for (int i = 0; i < title.size(); i++) { Item item = Item.getInstance(); item.setTitle(title.get(i)); final int j = i; Executable exec = () -> assertThrows(exceptions.get(j), () -> u.searchItem(title.get(j), category.get(j))); DynamicTest dTest = DynamicTest.dynamicTest(testNames.get(i), exec); dynamicTests.add(dTest); } return dynamicTests; } @Test public void searchAuctionTest() throws CategoryInvalidException, TitleInvalidException { String format = "dd/MM/yyyy hh:mm:ss"; SimpleDateFormat sdf = new SimpleDateFormat(format); String title = "Red bike", category = "Sport and Outdoor", category2 = "Cars and Motorbikes"; Date start = sdf.parse("22/06/2020"); Date end = sdf.parse("22/07/2020"); double price = 10; int bid = 5; User u = User.getInstance(); Category c = new Category(category2); ArrayList<Auction> result = new ArrayList<Auction>(); ArrayList<Auction> emptyArray = new ArrayList<Auction>(); Item i = new Item(); i.setTitle(title); i.setCategory(new Category(category)); Auction a = new Auction(i, start, end, price, bid); result.add(a); assertEquals(u.searchAuction(start, end, price, title, category), result); assertEquals(u.searchItem("ferrari", category), emptyArray); assertEquals(u.searchItem(title, category2), emptyArray); assertEquals(u.searchItem("ferrari", category2), emptyArray); } @TestFactory public Collection<DynamicTest> searchItemAcceptanceTest() { ArrayList<String> title = new ArrayList<String>( Arrays.asList("", "9999", "t-#\\", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "title")); ArrayList<String> category = new ArrayList<String>( Arrays.asList("camera", "camera", "camera", "camera", "bike")); ArrayList<String> testNames = new ArrayList<String>(Arrays.asList("title empty", "numerical title", "symbols present in the title", "title longer than 30 characters", "cateogry not correct")); ArrayList<Class<? extends Exception>> exceptions = new ArrayList<Class<? extends Exception>>( Arrays.asList(TitleInvalidException.class, TitleInvalidException.class, TitleInvalidException.class, TitleInvalidException.class, CategoryInvalidException.class)); Collection<DynamicTest> dynamicTests = new ArrayList<DynamicTest>(); Category c = new Category("camera"); User u = User.getInstance(); for (int i = 0; i < title.size(); i++) { Item item = Item.getInstance(); item.setTitle(title.get(i)); final int j = i; Executable exec = () -> assertThrows(exceptions.get(j), () -> u.searchItem(title.get(j), category.get(j))); DynamicTest dTest = DynamicTest.dynamicTest(testNames.get(i), exec); dynamicTests.add(dTest); } return dynamicTests; } }