Skip to content
Snippets Groups Projects
AuctionManagerUnitTest.java 6.45 KiB
Newer Older
package unitTest;

cristiano's avatar
cristiano committed
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.text.ParseException;
cristiano's avatar
cristiano committed
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;
import java.util.function.Function;
import java.util.stream.Stream;

cristiano's avatar
cristiano committed
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DynamicTest;
cristiano's avatar
cristiano committed
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
cristiano's avatar
cristiano committed
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.function.ThrowingConsumer;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
cristiano's avatar
cristiano committed
import exceptions.BidBelowZeroException;
import exceptions.ImageInvalidException;
import project.Auction;
cristiano's avatar
cristiano committed
import project.AuctionManager;
import project.Bid;
import project.Category;
import project.Item;
import project.ItemPicture;
import project.User;

public class AuctionManagerUnitTest {

cristiano's avatar
cristiano committed
	@BeforeEach
	public void reset() {

		Item.items = new ArrayList<Item>();
		User.users = new ArrayList<User>();
		Auction.auctions = new ArrayList<Auction>();
		Category.categories = new ArrayList<Category>();

	}

	@TestFactory
	public Collection<DynamicTest> checkTimeTest() {

		AuctionManager am = AuctionManager.getInstance();
		User user = User.getInstance();
		Auction a = Auction.getInstance();
		Date endDate = AuctionManager.parseDate("03/12/2019 10:00:00");
cristiano's avatar
cristiano committed
		a.setEnd(endDate);
		am.setAuction(a);
cristiano's avatar
cristiano committed
		ArrayList<Bid> bidList = new ArrayList<Bid>();
cristiano's avatar
cristiano committed
		for (int x = 0; x < 10; x++) {
			Date d = AuctionManager.parseDate("03/12/2059 " + x + ":00:00");
cristiano's avatar
cristiano committed
			Bid b = new Bid(d, 10, user, a);
			bidList.add(b);
		}

		a.setBids(bidList);
cristiano's avatar
cristiano committed

		ArrayList<Date> rightList = new ArrayList<Date>();

		for (int x = 0; x < 10; x++) {

			String dateToParse = "03/12/2019 0" + x + ":00:00";

			Date d = AuctionManager.parseDate(dateToParse);
cristiano's avatar
cristiano committed
			rightList.add(d);
cristiano's avatar
cristiano committed
		}

		ArrayList<Date> wrongList = new ArrayList<Date>();
		for (int x = 10; x < 20; x++) {

			String dateToParse = "03/12/2019 " + x + ":00:00";

			Date d = AuctionManager.parseDate(dateToParse);
cristiano's avatar
cristiano committed
			wrongList.add(d);
cristiano's avatar
cristiano committed
		}

		Collection<DynamicTest> dynamicTests = new ArrayList<DynamicTest>();

		for (int e = 0; e < rightList.size(); e++) {
cristiano's avatar
cristiano committed
			Date toRemove = rightList.get(e);
			Executable exec = () -> assertTrue(am.checkTime(toRemove));
			DynamicTest dTest = DynamicTest.dynamicTest("Before end date", exec);

			dynamicTests.add(dTest);
		}

		for (int e = 0; e < wrongList.size(); e++) {
			Date toRemove = wrongList.get(e);
			Executable exec = () -> assertFalse(am.checkTime(toRemove));
			DynamicTest dTest = DynamicTest.dynamicTest("After end date", exec);

			dynamicTests.add(dTest);
		}

		return dynamicTests;
	}

	@Test
	public void checkTimeTwoBidsTest() {

		Date commonDate = AuctionManager.parseDate("03/12/2019 10:00:00");
cristiano's avatar
cristiano committed
		User user = User.getInstance();
		AuctionManager auctionManager = AuctionManager.getInstance();
		Auction auction = Auction.getInstance();

		auctionManager.setAuction(auction);
		Bid a = new Bid(commonDate, 10, user, auction);

		ArrayList<Bid> toSet = auction.getBids();
		toSet.add(a);

		auction.setBids(toSet);
cristiano's avatar
cristiano committed

		Bid b = new Bid(commonDate, 10, user, auction);

		assertFalse(auctionManager.checkTime(b.getDate()));
	}

	@ParameterizedTest
	@ValueSource(ints = { -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 })
	public void placeBidTestOpenAuction(int toCheck) throws BidBelowZeroException, ParseException {
cristiano's avatar
cristiano committed

		AuctionManager auctionManager = AuctionManager.getInstance();
		Auction auction = Auction.getInstance();
		User user = User.getInstance();

		Date rightEndDate = AuctionManager.parseDate("03/12/2021 10:00:00");
cristiano's avatar
cristiano committed

		// Sets the auction related to the AuctionManager with a valid date
		auction.setEnd(rightEndDate);
		auctionManager.setAuction(auction);

		if (toCheck < 1) {

			assertThrows(BidBelowZeroException.class, () -> auctionManager.placeBid(user, toCheck));

		} else {

			assertTrue(auctionManager.placeBid(user, toCheck));
cristiano's avatar
cristiano committed

		}

cristiano's avatar
cristiano committed

	@TestFactory
	public Collection<DynamicTest> placeBidTestEndedAuction() {
cristiano's avatar
cristiano committed

		AuctionManager auctionManager = AuctionManager.getInstance();
		Auction auction = Auction.getInstance();
		User user = User.getInstance();
cristiano's avatar
cristiano committed

		// Sets the auction related to the AuctionManager with a valid date
		auctionManager.setAuction(auction);

		ArrayList<Integer> values = new ArrayList<Integer>();

		for (int e = -4; e < 6; e++) {

			values.add(e);
cristiano's avatar
cristiano committed

		}

		Collection<DynamicTest> dynamicTests = new ArrayList<DynamicTest>();

cristiano's avatar
cristiano committed
		// Change the date of the auction managed by the manager with a wrong date and
		// re-run the test
		Date wrongEndDate = AuctionManager.parseDate("03/12/2008 10:00:00");
cristiano's avatar
cristiano committed
		auction.setEnd(wrongEndDate);

		for (int e = 0; e < 10; e++) {
cristiano's avatar
cristiano committed

			DynamicTest dTest;
			int toCheck = values.get(e);
cristiano's avatar
cristiano committed

			if (toCheck < 1) {
				Executable exec = () -> assertThrows(BidBelowZeroException.class,
						() -> auctionManager.placeBid(user, toCheck));
				dTest = DynamicTest.dynamicTest("Value below zero with ended Auction", exec);
			} else {
				Executable exec = () -> assertFalse(auctionManager.placeBid(user, toCheck));
				dTest = DynamicTest.dynamicTest("Value over zero with ended Auction", exec);
			}

			dynamicTests.add(dTest);
		}

		return dynamicTests;
	}
cristiano's avatar
cristiano committed
	
	@TestFactory
cristiano's avatar
cristiano committed
	public Collection<DynamicTest> externalAddBidTestFalse() {
cristiano's avatar
cristiano committed
		
		Auction auction = Auction.getInstance();
		User user = User.getInstance();
		AuctionManager manager = AuctionManager.getInstance();
		ArrayList<Integer> values = new ArrayList<Integer>();
		
		manager.setAuction(auction);
		
cristiano's avatar
cristiano committed
		for(int i = 1; i < 6; i++) {
			values.add(i);	
cristiano's avatar
cristiano committed
		}
		
		Collection<DynamicTest> dynamicTests = new ArrayList<DynamicTest>();

cristiano's avatar
cristiano committed
		for (int e = 0; e < 5; e++) {
cristiano's avatar
cristiano committed

			DynamicTest dTest;
cristiano's avatar
cristiano committed
			auction.setPrice(10);
cristiano's avatar
cristiano committed
			int toCheck = values.get(e);

cristiano's avatar
cristiano committed
			Executable exec = () -> assertFalse(manager.placeBid(user, toCheck));
			dTest = DynamicTest.dynamicTest("Lower bid than current auction price", exec);
cristiano's avatar
cristiano committed

			dynamicTests.add(dTest);
		}

		return dynamicTests;
		
	}
cristiano's avatar
cristiano committed
	

	@ParameterizedTest
	@ValueSource(ints = { 2, 3, 4, 5, 6 })
	public void externalAddBidTrue(int toCheck) throws BidBelowZeroException, ParseException {

		Auction auction = Auction.getInstance();
		User user = User.getInstance();
		AuctionManager manager = AuctionManager.getInstance();
		
		auction.setPrice(1);
		manager.setAuction(auction);

		assertTrue(manager.placeBid(user, toCheck));

	}
cristiano's avatar
cristiano committed