public void AddProductAuctionAlreadyFinished()
        {
            AuctionService auctionService = new AuctionService();
            ProductService productService = new ProductService();
            CurrencyService currencyService = new CurrencyService();
            UserService userService = new UserService();
            ProductAuctionService productAuctionService = new ProductAuctionService();
            RoleService roleService = new RoleService();

            User user = userService.GetUserById(3);
            Product product = productService.GetProductById(1);
            Double price = 101;
            Currency currency = currencyService.getCurrencyById(1);
            Boolean result = false;

            try
            {
                result = productAuctionService.AddProductAuction(user, product, price, currency);
            }
            catch (AuctionException exc)
            {
                Assert.AreEqual("Cannot add a new auction for the selected product because it is expired", exc.Message);
            }
            Assert.IsFalse(result);
        }
        public static void AddUser()
        {
            /*User user = new User();
            user.FirstName = "Gigi";
            user.LastName = "Vasile";
            user.Email = "*****@*****.**";*/

            IUserService userService = new UserService();
            IRoleService roleService = new RoleService();
            //userService.AddUser(user);
            //userService.UpdateLastName("*****@*****.**", "bau");
            //Role role = roleService.GetRoleByName("XYZ");
            //userService.AddRole(role);
            userService.UpdateEmail("*****@*****.**", "*****@*****.**");
        }
        public void TestAddProductAuctionDifferentCurrency()
        {
            AuctionService auctionService = new AuctionService();
            ProductService productService = new ProductService();
            CurrencyService currencyService = new CurrencyService();
            UserService userService = new UserService();
            ProductAuctionService productAuctionService = new ProductAuctionService();
            RoleService roleService = new RoleService();

            User user = userService.GetUserById(2);
            Product product = productService.GetProductById(1);
            Double price = 100;
            Currency currency = currencyService.getCurrencyById(2);

            try
            {
                productAuctionService.AddProductAuction(user, product, price, currency);
            }
            catch (ValidationException exc)
            {
                Assert.AreEqual("The auction must have the same currency as the product: " + product.Auction.Currency.Name, exc.Message);
            }
        }
        private void CheckUser(User user)
        {
            IRoleService roleService = new RoleService();
            IConfiguration configuration = ConfigurationService.GetInstance();
            IUserService userService = new UserService();

            ICollection<Role> roles = roleService.GetRolesFromAnUser(user);
            ICollection<Rating> ratings = userService.GetAllRatingsOfAnUser(user);

            bool ok = false;
            foreach (Role role in roles)
                if (role.Name.Equals(Constants.OWNER))
                    ok = true;

            if (!ok)
                throw new AuctionException("User " + user.Email + " does not have the rights to add an auction");

            int nrOfActiveAuctions = this.GetNumberOfActiveAuctionsStartedByUser(user);

            int sum = 0;
            int nr = 0;
            DateTime maxDate = new DateTime(1000, 1, 1);
            foreach (Rating rating in ratings)
            {
                if ((DateTime.Now - rating.Date).TotalDays < configuration.GetValue(Constants.NR_OF_DAYS_USED_TO_DETERMINE_RATING))
                {
                    sum += rating.Grade;
                    nr++;
                    if (rating.Date > maxDate)
                        maxDate = rating.Date;
                }
            }

            double ratingCalc, maxNrOfAuctions;
            if(nr > 0)
            {
                ratingCalc = sum / nr;
                maxNrOfAuctions = ratingCalc / 10 * configuration.GetValue(Constants.MAX_NR_OF_STARTED_AUCTION);
            }
            else
            {
                ratingCalc = 10;
                maxNrOfAuctions = configuration.GetValue(Constants.MAX_NR_OF_STARTED_AUCTION);
            }
            int intMaxNrOfAuctions = (int)maxNrOfAuctions;

            if(ratingCalc < configuration.GetValue(Constants.RATING_THRESH_HOLD_FOR_AUCTION))
            {
                if((DateTime.Now - maxDate).Days >= configuration.GetValue(Constants.NR_OF_DAY_BEFORE_RATING_RESET))
                {
                    for(int i=0;i<ratings.Count;i++)
                    {
                        DataMapperFactoryMethod.GetCurrentFactory().UserFactory.RemoveRating(ratings.ElementAt(i));
                    }
                }
                else
                    throw new AuctionException("Auction can not be added because the rating of the user is small than " + configuration.GetValue(Constants.RATING_THRESH_HOLD_FOR_AUCTION) + "resetare");
            }

            if (nrOfActiveAuctions > intMaxNrOfAuctions)
                throw new AuctionException("Auction can not be added because max number of auctions per user is "+configuration.GetValue(Constants.MAX_NR_OF_STARTED_AUCTION));
        }
        public void TestAddProductAuctionInvalidDate()
        {
            AuctionService auctionService = new AuctionService();
            ProductService productService = new ProductService();
            CurrencyService currencyService = new CurrencyService();
            UserService userService = new UserService();
            ProductAuctionService productAuctionService = new ProductAuctionService();
            RoleService roleService = new RoleService();

            User user = userService.GetUserById(3);
            Product product = productService.GetProductById(1);
            Double price = 101;
            Currency currency = currencyService.getCurrencyById(1);

            product.Auction.EndDate = DateTime.Today;

            Boolean result = false;
            try
            {
                result = productAuctionService.AddProductAuction(user, product, price, currency);
            }
            catch (ValidationException exc)
            {
                Assert.AreEqual("Cannot add a new auction for the selected product because it is expired", exc.Message);
            }
            Assert.AreEqual(result, false);
        }
        public void TestFinishAuctionNullProduct()
        {
            UserService userService = new UserService();
            ProductAuctionService productAuctionService = new ProductAuctionService();

            User user = userService.GetUserById(1);
            Boolean result = false;

            try
            {
                productAuctionService.closeAuction(user, null);
            }
            catch (EntityDoesNotExistException exc)
            {
                Assert.AreEqual("Product is null", exc.Message);
            }

            Assert.IsFalse(result);
        }
        public void TestFinishAuctionNotTheSameUser()
        {
            UserService userService = new UserService();
            ProductAuctionService productAuctionService = new ProductAuctionService();
            ProductService productService = new ProductService();

            User user = userService.GetUserById(2);
            Product product = productService.GetProductById(1);
            Boolean result = false;

            try
            {
                productAuctionService.closeAuction(user, product);
            }
            catch (AuctionException exc)
            {
                Assert.AreEqual("You are not allowed to close the auction - you are not the owner!", exc.Message);
            }

            Assert.IsFalse(result);
        }
        public void TestFinishAuctionAlreadyFinished()
        {
            UserService userService = new UserService();
            ProductAuctionService productAuctionService = new ProductAuctionService();
            ProductService productService = new ProductService();

            User user = userService.GetUserById(1);
            Product product = productService.GetProductById(1);
            Boolean result = false;

            try
            {
                result = productAuctionService.closeAuction(user, product);
            }
            catch (AuctionException exc)
            {
                Assert.AreEqual("Auction already closed", exc.Message);
            }
            Assert.IsFalse(result);
        }
        public void TestAddProductNullCurrency()
        {
            AuctionService auctionService = new AuctionService();
            ProductService productService = new ProductService();
            CurrencyService currencyService = new CurrencyService();
            UserService userService = new UserService();
            ProductAuctionService productAuctionService = new ProductAuctionService();
            RoleService roleService = new RoleService();

            Product product = productService.GetProductById(1);
            Double price = 200;
            Currency currency = currencyService.getCurrencyById(1);

            Role role = roleService.GetRoleByName("owner");
            userService.AddRoleToUser("*****@*****.**", role);

            User user = userService.GetUserById(2);
            try
            {
                productAuctionService.AddProductAuction(user, product, price, null);
            }
            catch (EntityDoesNotExistException exc)
            {
                Assert.AreEqual("Currency is null", exc.Message);
            }
        }
        public void TestAddProductAuctionUserWithNoRole()
        {
            AuctionService auctionService = new AuctionService();
            ProductService productService = new ProductService();
            CurrencyService currencyService = new CurrencyService();
            UserService userService = new UserService();
            ProductAuctionService productAuctionService = new ProductAuctionService();
            RoleService roleService = new RoleService();

            Product product = productService.GetProductById(1);
            Double price = 20000;
            Currency currency = currencyService.getCurrencyById(1);

            User user = userService.GetUserById(3);
            try
            {
                productAuctionService.AddProductAuction(user, product, price, currency);
            }
            catch (ValidationException exc)
            {
                Assert.AreEqual("The user is not actioneer", exc.Message);
            }
        }
        public void TestAddProductAuctionSameUser()
        {
            AuctionService auctionService = new AuctionService();
            ProductService productService = new ProductService();
            CurrencyService currencyService = new CurrencyService();
            UserService userService = new UserService();
            ProductAuctionService productAuctionService = new ProductAuctionService();
            RoleService roleService = new RoleService();

            Auction auction = auctionService.GetAuctionById(1);
            Role role = roleService.GetRoleByName("actioneer");
            userService.AddRoleToUser("[email protected]", role);
            User user = userService.GetUserById(1);
            Product product = productService.GetProductById(1);
            Double price = 100;
            Currency currency = currencyService.getCurrencyById(1);

            try
            {
                productAuctionService.AddProductAuction(user, product, price, currency);
            }
            catch (ValidationException exc)
            {
                Assert.AreEqual("The ownwer and the actioneer cannot be the same user", exc.Message);
            }
        }
        public void TestAddProductAuctionPriceTheSameAsTheLastAuction()
        {
            AuctionService auctionService = new AuctionService();
            ProductService productService = new ProductService();
            CurrencyService currencyService = new CurrencyService();
            UserService userService = new UserService();
            ProductAuctionService productAuctionService = new ProductAuctionService();
            RoleService roleService = new RoleService();

            User user = userService.GetUserById(2);
            Product product = productService.GetProductById(1);
            Double price = 101;
            Currency currency = currencyService.getCurrencyById(1);

            try
            {
                productAuctionService.AddProductAuction(user, product, price, currency);
            }
            catch (ValidationException exc)
            {
                Assert.AreEqual("The price is too low", exc.Message);
            }
        }
        public void TestAddProductAuctionPriceHigherThanStartPice()
        {
            AuctionService auctionService = new AuctionService();
            ProductService productService = new ProductService();
            CurrencyService currencyService = new CurrencyService();
            UserService userService = new UserService();
            ProductAuctionService productAuctionService = new ProductAuctionService();
            RoleService roleService = new RoleService();

            User user = userService.GetUserById(2);
            Product product = productService.GetProductById(1);
            Double price = 1000;
            Currency currency = currencyService.getCurrencyById(1);

            Assert.IsNotNull(currency.IdCurrency);
            Assert.IsNotNull(product.Auction.Currency.IdCurrency);

            Assert.AreEqual(currency.IdCurrency, 1);
            Assert.AreEqual(product.Auction.Currency.IdCurrency, 1);

            Boolean result = false;
            try
            {
                result = productAuctionService.AddProductAuction(user, product, price, currency);
            }
            catch (ValidationException exc)
            {
                Assert.AreEqual("", exc.Message);
            }
            Assert.AreEqual(result, true);
        }
        public void TestAddProductAuctionOwner()
        {
            AuctionService auctionService = new AuctionService();
            ProductService productService = new ProductService();
            CurrencyService currencyService = new CurrencyService();
            UserService userService = new UserService();
            ProductAuctionService productAuctionService = new ProductAuctionService();
            RoleService roleService = new RoleService();

            Product product = productService.GetProductById(1);
            Double price = 2000;
            Currency currency = currencyService.getCurrencyById(1);

            Role role = roleService.GetRoleByName("owner");
            userService.AddRoleToUser("*****@*****.**", role);

            User user = userService.GetUserById(3);
            try
            {
                productAuctionService.AddProductAuction(user, product, price, currency);
            }
            catch (ValidationException exc)
            {
                Assert.AreEqual("The user is not actioneer", exc.Message);
            }
        }
        public void TestAddProductAuctionNullUser()
        {
            CurrencyService currencyService = new CurrencyService();
            UserService userService = new UserService();
            ProductAuctionService productAuctionService = new ProductAuctionService();
            RoleService roleService = new RoleService();

            User user = userService.GetUserById(3);
            Double price = 200;
            Currency currency = currencyService.getCurrencyById(2);

            try
            {
                productAuctionService.AddProductAuction(null, new Product(), price, currency);
            }
            catch (EntityDoesNotExistException exc)
            {
                Assert.AreEqual("User is null", exc.Message);
            }
        }
        public void TestAddProductAuctionNullAuction()
        {
            CurrencyService currencyService = new CurrencyService();
            UserService userService = new UserService();
            ProductAuctionService productAuctionService = new ProductAuctionService();
            RoleService roleService = new RoleService();

            User user = userService.GetUserById(3);
            Double price = 101;
            Currency currency = currencyService.getCurrencyById(1);

            Boolean result = false;
            try
            {
                result = productAuctionService.AddProductAuction(user, new Product(), price, currency);
            }
            catch (ValidationException exc)
            {
                Assert.AreEqual("Cannot add a new auction for the selected product", exc.Message);
            }
            Assert.AreEqual(result, false);
        }