Skip to content
Snippets Groups Projects
Commit 9849bdb0 authored by cristiano's avatar cristiano
Browse files

Merge branch 'master' of gitlab.inf.unibz.it:TTST1920/clar

parents 9b9da26a e27c19d0
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,6 @@
| Method | Static Test | Dynamic Test | Parameterized test |
| :------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | ----------------------------------------------------- |
| login | 1 working (right credentials) - 1 not working (wrong credentials) | | empty case, wrong mail |
| becomeSeller | | | empty case, wrong format, right format |
| searchItem | 1 working (item existent) - 1 not working (item not existent) | | title (empty, numbers, length), category not selected |
| searchAuction | 1 working (auction existent) - 1 not working (auction not existent) | date (start before end, malformation), price (wrong format, below zero) | title (empty, numbers, length), category not selected |
......
......@@ -15,9 +15,10 @@
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -157,7 +157,7 @@ public class User {
public ArrayList<Item> searchItem(String title, String category)
throws CategoryInvalidException, TitleInvalidException {
if (!title.matches("^[a-zA-Z]+$") || title.length() > 30 || title.length() < 1) {
if (!title.matches("^[a-zA-Z\\s]+$") || title.length() > 30 || title.length() < 1) {
throw new TitleInvalidException();
......@@ -175,7 +175,7 @@ public class User {
for (Item i : Item.items) {
if (i.getTitle().equals(title) || i.getCategory().equals(c)) {
if (i.getTitle().equals(title) && i.getCategory().equals(c)) {
toReturn.add(i);
......
......@@ -82,4 +82,6 @@ public class ItemUnitTest {
}
}
package unitTest;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
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.Iterator;
import java.util.Random;
import java.util.function.Function;
import java.util.stream.Stream;
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 org.junit.jupiter.api.function.ThrowingConsumer;
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 {
public void loginTest()
throws UsernameEmailEmptyException, PasswordInvalidException, MailInvalidException, LoginInvalidException {
String username = "mario", password = "password", email = "test@gmail.com";
User u = new User();
......@@ -36,9 +35,7 @@ public class UserUnitTest {
u.setPassword(password);
assertTrue(User.login(username, email, password));
assertThrows(LoginInvalidException.class, () -> {
User.login(username, email, "wrong password");
});
assertThrows(LoginInvalidException.class, () -> User.login(username, email, "wrong password"));
}
......@@ -50,9 +47,6 @@ public class UserUnitTest {
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> errorMessages = new ArrayList<String>(Arrays.asList(
"Error: you should provide an username or a password", "Error: email not in the right format",
"Error: password blank", "Error: login invalid", "Error: login invalid"));
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>>(
......@@ -66,9 +60,68 @@ public class UserUnitTest {
String us = username.get(i);
String em = email.get(i);
String pw = password.get(i);
String err = errorMessages.get(i);
Executable exec = () -> assertThrows(exceptions.get(i), User.login(us, em, pw));
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);
......@@ -78,5 +131,72 @@ public class UserUnitTest {
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;
}
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment