Newer
Older
import static org.junit.jupiter.api.Assertions.assertEquals;

Righi Andrea (Student Com17)
committed
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Righi Andrea (Student Com17)
committed
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

Righi Andrea (Student Com17)
committed
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 project.Auction;

Righi Andrea (Student Com17)
committed
import project.Category;
import project.Item;
import project.User;
public class UserUnitTest {

Righi Andrea (Student Com17)
committed

Righi Andrea (Student Com17)
committed
public void loginTest()
throws UsernameEmailEmptyException, PasswordInvalidException, MailInvalidException, LoginInvalidException {
String username = "mario", password = "password", email = "test@gmail.com";
u.setUsername(username);
u.setEmail(email);
u.setPassword(password);
assertTrue(User.login(username, email, password));

Righi Andrea (Student Com17)
committed
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);

Righi Andrea (Student Com17)
committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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;
}

Righi Andrea (Student Com17)
committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
@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;
}