Skip to content
Snippets Groups Projects
Commit b74f1747 authored by Righi Andrea (Student Com17)'s avatar Righi Andrea (Student Com17)
Browse files

updated seller unit class

parent a38909c9
No related branches found
No related tags found
No related merge requests found
......@@ -54,7 +54,7 @@ public class AuctionManager {
public static Date parseDate(String value) {
Date date = null;
String format = "dd/MM/yyyy hh:mm:ss";
String format = "dd/MM/yyyy HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
......
package unitTest;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.function.ThrowingConsumer;
import exceptions.ImageInvalidException;
import project.Auction;
......@@ -25,7 +19,7 @@ import project.ItemPicture;
import project.User;
public class ItemPictureUnitTest {
@BeforeEach
public void reset() {
......@@ -37,40 +31,40 @@ public class ItemPictureUnitTest {
}
@TestFactory
public Collection<DynamicTest> ItemPictureUnitTest() throws ImageInvalidException {
public Collection<DynamicTest> addPictureAcceptanceTest() throws ImageInvalidException {
Item i = Item.getInstance();
ArrayList<ItemPicture> rightList = new ArrayList<ItemPicture>();
for(int x = 0; x < 10; x++) {
for (int x = 0; x < 10; x++) {
rightList.add(new ItemPicture(x));
}
i.addPicture(rightList);
ArrayList<ItemPicture> wrongList = new ArrayList<ItemPicture>();
for(int x = 10; x < 20; x++) {
for (int x = 10; x < 20; x++) {
wrongList.add(new ItemPicture(x));
}
Collection<DynamicTest> dynamicTests = new ArrayList<DynamicTest>();
for(int e = 0; e < rightList.size(); e++) {
for (int e = 0; e < rightList.size(); e++) {
ItemPicture toRemove = rightList.get(e);
Executable exec = () -> assertTrue(ItemPicture.remove(toRemove.getId(), i));
DynamicTest dTest = DynamicTest.dynamicTest("Existing images", exec);
dynamicTests.add(dTest);
}
for(int e = 0; e < wrongList.size(); e++) {
for (int e = 0; e < wrongList.size(); e++) {
ItemPicture toRemove = wrongList.get(e);
Executable exec = () -> assertFalse(ItemPicture.remove(toRemove.getId(), i));
DynamicTest dTest = DynamicTest.dynamicTest("Not existen images", exec);
dynamicTests.add(dTest);
}
return dynamicTests;
}
}
......@@ -24,7 +24,6 @@ import org.junit.jupiter.params.provider.ValueSource;
import exceptions.CategoryInvalidException;
import exceptions.DescriptionInvalidException;
import exceptions.ImageInvalidException;
import exceptions.PriceInvalidException;
import exceptions.TitleInvalidException;
import project.Auction;
import project.Category;
......@@ -33,7 +32,7 @@ import project.ItemPicture;
import project.User;
public class ItemUnitTest {
@BeforeEach
public void reset() {
......@@ -51,8 +50,7 @@ public class ItemUnitTest {
int itemId;
Random random = new Random();
@Override
public boolean hasNext() {
......@@ -112,7 +110,7 @@ public class ItemUnitTest {
}
@TestFactory
public Collection<DynamicTest> modifyTest() {
public Collection<DynamicTest> modifyAcceptanceTest() {
ArrayList<String> title = new ArrayList<String>(
Arrays.asList("", "9999", "t-#\\", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
......@@ -184,14 +182,20 @@ public class ItemUnitTest {
}
Executable exec = () -> assertTrue(item.modify("title", "description", "camera"));
DynamicTest dTest = DynamicTest.dynamicTest("Working test", exec);
dynamicTests.add(dTest);
return dynamicTests;
}
@Test
public void modifyTestTrue() throws CategoryInvalidException, DescriptionInvalidException, TitleInvalidException {
Item item = Item.getInstance();
new Category("camera");
assertTrue(item.modify("title", "description", "camera"));
}
@ParameterizedTest
@ValueSource(ints = { -1, 20 })
public void addPictureOutOfBoundTest(int arrayLength) {
......
......@@ -69,35 +69,72 @@ public class SellerUnitTest {
@TestFactory
public Collection<DynamicTest> createAuctionAcceptanceTest() {
ArrayList<Integer> items = new ArrayList<Integer>(Arrays.asList(0, 1, 1, 1, 1, 1, 1, 1));
ArrayList<String> startTimes = new ArrayList<String>(
Arrays.asList("10/11/2019 11:20:12", "11/11/2019 11:20:12", "111112019", "10/11/2019", "111112019",
"10/11/2019 11:20:12", "10/11/2019 11:20:12", "10/11/2019 11:20:12"));
ArrayList<String> endTimes = new ArrayList<String>(
Arrays.asList("11/11/2019 11:20:12", "10/11/2019 11:20:12", "10112019", "111112019", "11/11/2019",
"11/11/2019 11:20:12", "11/11/2019 11:20:12", "11/11/2019 11:20:12"));
ArrayList<Integer> prices = new ArrayList<Integer>(Arrays.asList(20, 20, 20, 20, 20, -1, 0, 20, 20));
ArrayList<Integer> bidIncrements = new ArrayList<Integer>(Arrays.asList(5, 5, 5, 5, 5, 5, 5, 0, -1));
ArrayList<String> testNames = new ArrayList<String>(Arrays.asList("item not existent", "end before start",
"both dates wrong format", "end wrong format", "start wrong format", "negative price",
"price eqaul to zero", "increment equal to zero", "increment negative"));
ArrayList<Class<? extends Exception>> exceptions = new ArrayList<Class<? extends Exception>>(
Arrays.asList(ItemNonExistentException.class, DateInvalidException.class, DateInvalidException.class,
DateInvalidException.class, DateInvalidException.class, PriceInvalidException.class,
PriceInvalidException.class, PriceInvalidException.class, PriceInvalidException.class));
Collection<DynamicTest> dynamicTests = new ArrayList<DynamicTest>();
Seller s = Seller.getInstance();
Item item = Item.getInstance();
ArrayList<Integer> items = new ArrayList<Integer>(Arrays.asList(0));
Item item = new Item();
item.setId(1);
for (int i = 0; i < items.size(); i++) {
final int j = i;
Executable exec = () -> assertThrows(exceptions.get(j), () -> s.createAuction(Item.getItem(items.get(j)),
startTimes.get(j), endTimes.get(j), prices.get(j), bidIncrements.get(j)));
DynamicTest dTest = DynamicTest.dynamicTest(testNames.get(i), exec);
Executable exec = () -> assertThrows(ItemNonExistentException.class, () -> s
.createAuction(Item.getItem(items.get(j)), "11/12/2019 11:50:11", "12/12/2019 12:50:11", 10d, 5d));
DynamicTest dTest = DynamicTest.dynamicTest("Item not existent", exec);
dynamicTests.add(dTest);
}
ArrayList<String> startTimes = new ArrayList<String>(
Arrays.asList("11/11/2019 11:20:12", "111112019", "10/11/2019", "111112019"));
ArrayList<String> endTimes = new ArrayList<String>(
Arrays.asList("10/11/2019 11:20:12", "10112019", "111112019", "11/11/2019"));
ArrayList<String> testTimeNames = new ArrayList<String>(Arrays.asList("End time before Start time",
"Both dates in a wrong format", "End time in a wrong format", "Start time in a wrong format"));
for (int i = 0; i < startTimes.size(); i++) {
final int j = i;
Executable exec = () -> assertThrows(DateInvalidException.class,
() -> s.createAuction(Item.getItem(1), startTimes.get(j), endTimes.get(j), 10d, 5d));
DynamicTest dTest = DynamicTest.dynamicTest(testTimeNames.get(j), exec);
dynamicTests.add(dTest);
}
ArrayList<Integer> prices = new ArrayList<Integer>(Arrays.asList(-1, 0));
ArrayList<String> testPriceNames = new ArrayList<String>(
Arrays.asList("Negative price", "Price equal to zero"));
for (int i = 0; i < prices.size(); i++) {
final int j = i;
Executable exec = () -> assertThrows(PriceInvalidException.class, () -> s.createAuction(Item.getItem(1),
"11/12/2019 21:50:11", "12/12/2019 21:50:11", prices.get(j), 5d));
DynamicTest dTest = DynamicTest.dynamicTest(testPriceNames.get(j), exec);
dynamicTests.add(dTest);
}
ArrayList<Integer> bidIncrements = new ArrayList<Integer>(Arrays.asList(-1, 0));
ArrayList<String> testIncrementNames = new ArrayList<String>(
Arrays.asList("Negative increment", "Increment equal to zero"));
for (int i = 0; i < bidIncrements.size(); i++) {
final int j = i;
Executable exec = () -> assertThrows(PriceInvalidException.class, () -> s.createAuction(Item.getItem(1),
"11/12/2019 21:50:11", "12/12/2019 21:50:11", 10, bidIncrements.get(j)));
DynamicTest dTest = DynamicTest.dynamicTest(testIncrementNames.get(j), exec);
dynamicTests.add(dTest);
......@@ -110,23 +147,6 @@ public class SellerUnitTest {
@TestFactory
public Collection<DynamicTest> createItemAcceptanceTest() {
ArrayList<String> titles = new ArrayList<String>(
Arrays.asList("", "9999", "t-#\\", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "title",
"title", "title", "title", "title", "title"));
ArrayList<String> descriptions = new ArrayList<String>(Arrays.asList("description", "description",
"description", "description", "", "9999", "too long", "description", "description", "description"));
ArrayList<String> categories = new ArrayList<String>(Arrays.asList("Sport and Outdoor", "Sport and Outdoor",
"Sport and Outdoor", "Sport and Outdoor", "Sport and Outdoor", "Sport and Outdoor", "Sport and Outdoor",
"Sport and Outdoor", "Sport and Outdoor", ""));
ArrayList<String> testNames = new ArrayList<String>(Arrays.asList("title empty", "numeric title",
"symbols in the title", "title too long", "description empty", "numeric description",
"description too long", "no pictures inserted", "too many pictures", "category not existent"));
ArrayList<Class<? extends Exception>> exceptions = new ArrayList<Class<? extends Exception>>(
Arrays.asList(TitleInvalidException.class, TitleInvalidException.class, TitleInvalidException.class,
TitleInvalidException.class, DescriptionInvalidException.class,
DescriptionInvalidException.class, DescriptionInvalidException.class,
ImageInvalidException.class, ImageInvalidException.class, CategoryInvalidException.class));
Collection<DynamicTest> dynamicTests = new ArrayList<DynamicTest>();
Seller s = Seller.getInstance();
......@@ -138,28 +158,74 @@ public class SellerUnitTest {
ip1.setId(1);
ip2.setId(2);
ArrayList<String> titles = new ArrayList<String>(Arrays.asList("", "9999", "t-#\\", "too long"));
ArrayList<String> testTitleNames = new ArrayList<String>(
Arrays.asList("Title empty", "Numeric title", "Symbols in the title", "Title too long"));
for (int i = 0; i < titles.size(); i++) {
String prepareTitle = titles.get(i);
if (i == 3) {
for (int k = 0; k < 60; k++) {
prepareTitle += "a";
}
}
final String title = prepareTitle;
final int j = i;
Executable exec = () -> assertThrows(TitleInvalidException.class,
() -> s.createItem(title, "Beautiful bike", new int[] { 1, 2 }, "Sport and Outdoor"));
DynamicTest dTest = DynamicTest.dynamicTest(testTitleNames.get(j), exec);
dynamicTests.add(dTest);
}
ArrayList<String> descriptions = new ArrayList<String>(Arrays.asList("", "9999", "too long"));
ArrayList<String> testDescriptionNames = new ArrayList<String>(
Arrays.asList("Description empty", "Numeric description", "Description too long"));
for (int i = 0; i < descriptions.size(); i++) {
String prepareDescription = descriptions.get(i);
int[] prepareImages = { 1, 2 };
if (i == 6) {
if (i == 2) {
for (int k = 0; k < 600; k++) {
prepareDescription += "a";
}
}
if (i == 7) {
final String description = prepareDescription;
final int j = i;
prepareImages = new int[0];
Executable exec = () -> assertThrows(DescriptionInvalidException.class,
() -> s.createItem("Red bike", description, new int[] { 1, 2 }, "Sport and Outdoor"));
DynamicTest dTest = DynamicTest.dynamicTest(testDescriptionNames.get(j), exec);
}
dynamicTests.add(dTest);
}
ArrayList<String> testItemPictureNames = new ArrayList<String>(
Arrays.asList("No picture inserted", "Too many pictures"));
for (int i = 0; i < 2; i++) {
int[] prepareImages = { 1, 2 };
if (i == 8) {
if (i == 0) {
prepareImages = new int[0];
} else if (i == 1) {
prepareImages = new int[11];
......@@ -174,13 +240,25 @@ public class SellerUnitTest {
}
final String description = prepareDescription;
final int j = i;
final int[] images = prepareImages;
Executable exec = () -> assertThrows(exceptions.get(j),
() -> s.createItem(titles.get(j), description, images, categories.get(j)));
DynamicTest dTest = DynamicTest.dynamicTest(testNames.get(i), exec);
Executable exec = () -> assertThrows(ImageInvalidException.class,
() -> s.createItem("Red bike", "Beautiful bike", images, "Sport and Outdoor"));
DynamicTest dTest = DynamicTest.dynamicTest(testItemPictureNames.get(i), exec);
dynamicTests.add(dTest);
}
ArrayList<String> categories = new ArrayList<String>(Arrays.asList(""));
for (int i = 0; i < categories.size(); i++) {
final int j = i;
Executable exec = () -> assertThrows(CategoryInvalidException.class,
() -> s.createItem("Red bike", "Beautiful Bike", new int[] { 1, 2 }, categories.get(j)));
DynamicTest dTest = DynamicTest.dynamicTest("Category not existent", exec);
dynamicTests.add(dTest);
......
......@@ -53,6 +53,8 @@ public class UserUnitTest {
assertTrue(User.login(username, email, password));
assertTrue(User.login("", email, password));
assertThrows(LoginInvalidException.class, () -> User.login(username, email, "wrong password"));
assertThrows(LoginInvalidException.class, () -> User.login("wrongUsername", email, "wrong password"));
assertThrows(LoginInvalidException.class, () -> User.login("", "wrongemail@gmail.com", "wrong password"));
assertThrows(LoginInvalidException.class, () -> User.login("", email, "wrong password"));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment