private static Coupon couponCreator(Company company , Coupon coupon , String message , String title, int amount,
long id , String image , double price , CouponType type , Date start_date , Date end_date){
coupon.setCompany(company);
coupon.setMessage(message);
coupon.setAmount(amount);
coupon.setId(id);
coupon.setType(type);
coupon.setTitle(title);
coupon.setImage(image);
coupon.setPrice(price);
coupon.setEndDate(end_date);
coupon.setStartDate(start_date);
return coupon;
}
public Coupon createCompanyCoupon(Company company , Coupon coupon , String message , String title, int amount,
long id , String image , double price , CouponType type , Date start_date , Date end_date) {
coupon = couponCreator(company, coupon, message, title, amount, id, image, price, type, start_date, end_date);
return coupon;
}
@Before
public void createCoupons() {
System.out.println("Setting coupons data! ");
Coupon coupon1 = companyServiceTesting.createCompanyCoupon(setOfCompanies.get("company1"), callForCoupon(), "Travelling low prices", "FamilyTravelling",
23, 765475475, "C://ProgramFiles", 35.8, CouponType.TRAVELING, CouponSystemDate.dateFormat(2017, 12, 12)
, CouponSystemDate.dateFormat(2019, 12, 12));
Coupon coupon2 = companyServiceTesting.createCompanyCoupon(setOfCompanies.get("company2"), callForCoupon(), "Camping low prices", "FamilyCamping",
13, 9999887, "C://ProgramFiles", 55.8, CouponType.CAMPING, CouponSystemDate.dateFormat(2016, 10, 10)
, CouponSystemDate.dateFormat(2018, 9, 1));
Coupon coupon3 = companyServiceTesting.createCompanyCoupon(setOfCompanies.get("company1"), callForCoupon(), "Electricity low prices", "Electricity",
12, 5424352, "C://ProgramFiles", 25.8, CouponType.ELECTRICITY, CouponSystemDate.dateFormat(2017, 7, 3)
, CouponSystemDate.dateFormat(2018, 4, 5));
Coupon coupon4 = companyServiceTesting.createCompanyCoupon(setOfCompanies.get("company2"), callForCoupon(), "Food low prices", "FamilyDinner",
53, 989873, "C://ProgramFiles", 15.8, CouponType.FOOD, CouponSystemDate.dateFormat(2017, 4, 11)
, CouponSystemDate.dateFormat(2018, 4, 11));
Coupon coupon5 = companyServiceTesting.createCompanyCoupon(setOfCompanies.get("company1"), callForCoupon(), "Health insurance low prices", "FamilyInsurance",
83, 33344455, "C://ProgramFiles", 88.8, CouponType.HEALTH, CouponSystemDate.dateFormat(2017, 8, 9)
, CouponSystemDate.dateFormat(2018, 8, 9));
Coupon coupon6 = companyServiceTesting.createCompanyCoupon(setOfCompanies.get("company2"), callForCoupon(), "Sport village low prices", "SportVacation",
73, 6665432, "C://ProgramFiles", 95.8, CouponType.SPORTS, CouponSystemDate.dateFormat(2017, 7, 11)
, CouponSystemDate.dateFormat(2019, 7, 11));
System.out.println("Coupon1: "+coupon1);
System.out.println("Coupon2: "+coupon2);
setInCouponCollection("coupon1", coupon1);
setInCouponCollection("coupon2", coupon2);
setInCouponCollection("coupon3", coupon3);
setInCouponCollection("coupon4", coupon4);
setInCouponCollection("coupon5", coupon5);
setInCouponCollection("coupon6", coupon6);
}
Coupon1: Coupon [id=6665432, title=SportVacation, startDate=2017-07-11, endDate=2019-07-11, amount=73, message=Sport village low prices, price=95.8, image=C://ProgramFiles, type=SPORTS]
Coupon2: Coupon [id=6665432, title=SportVacation, startDate=2017-07-11, endDate=2019-07-11, amount=73, message=Sport village low prices, price=95.8, image=C://ProgramFiles, type=SPORTS]
Checking the setted data!
Key: coupon1 Value: Coupon [id=6665432, title=SportVacation, startDate=2017-07-11, endDate=2019-07-11, amount=73, message=Sport village low prices, price=95.8, image=C://ProgramFiles, type=SPORTS]
Key: coupon3 Value: Coupon [id=6665432, title=SportVacation, startDate=2017-07-11, endDate=2019-07-11, amount=73, message=Sport village low prices, price=95.8, image=C://ProgramFiles, type=SPORTS]
Key: coupon5 Value: Coupon [id=6665432, title=SportVacation, startDate=2017-07-11, endDate=2019-07-11, amount=73, message=Sport village low prices, price=95.8, image=C://ProgramFiles, type=SPORTS]
@AfterClass
public static void checkTheSetedObjects() {
System.out.println("Checking the setted data! ");
Iterator<Map.Entry<String, Coupon>> iterator3 = setOfCoupons.entrySet().iterator();
while(iterator3.hasNext()){
String key = iterator3.next().getKey();
Coupon value = iterator3.next().getValue();
System.out.println("Key: "+key+" Value: "+value);
}
}
Setting companies data!
Setting employee data!
Setting coupons data!
Setting customer data!
Setting users data!
Checking the setted data!
Iterated value: Coupon [id=6665432, title=SportVacation, startDate=2017-07-11, endDate=2019-07-11, amount=73, message=Sport village low prices, price=95.8, image=C://ProgramFiles, type=SPORTS]
Iterated key:coupon6
Iterated value: Coupon [id=6665432, title=SportVacation, startDate=2017-07-11, endDate=2019-07-11, amount=73, message=Sport village low prices, price=95.8, image=C://ProgramFiles, type=SPORTS]
Iterated key:company1
Iterated value: Coupon [id=6665432, title=SportVacation, startDate=2017-07-11, endDate=2019-07-11, amount=73, message=Sport village low prices, price=95.8, image=C://ProgramFiles, type=SPORTS]
Iterated key:company2
Iterated value: Customer{id=4325432, custFirstName='Yoni', custLastName='Yonivich', password='Yoni@09087', email='Yoni@gmail.com'}
Iterated key:coupon4
Iterated value: Customer{id=4325432, custFirstName='Yoni', custLastName='Yonivich', password='Yoni@09087', email='Yoni@gmail.com'}
Iterated key:coupon5
Iterated value: Customer{id=4325432, custFirstName='Yoni', custLastName='Yonivich', password='Yoni@09087', email='Yoni@gmail.com'}
Iterated key:customer5
Iterated value: Customer{id=4325432, custFirstName='Yoni', custLastName='Yonivich', password='Yoni@09087', email='Yoni@gmail.com'}
Iterated key:customer3
Iterated value: User{nickName='customer6', email='Yoni@gmail.com', password='Yoni@09087', role='customer'}
Iterated key:user1
Iterated value: User{nickName='customer6', email='Yoni@gmail.com', password='Yoni@09087', role='customer'}
Iterated key:user5
Iterated value: User{nickName='customer6', email='Yoni@gmail.com', password='Yoni@09087', role='customer'}
Iterated key:user3
Iterated value: Employee{id=9879874, firstName='Avi', lastName='AviAla', email='Avi@gmail.com', phoneNumber='0547334567', country='Israel'}
Iterated key:employee6
Iterated value: Employee{id=9879874, firstName='Avi', lastName='AviAla', email='Avi@gmail.com', phoneNumber='0547334567', country='Israel'}
Iterated key:employee3
Iterated value: Employee{id=9879874, firstName='Avi', lastName='AviAla', email='Avi@gmail.com', phoneNumber='0547334567', country='Israel'}
Iterated key:employee5
Setting login data!
Test successfully accomplished ? true
public class CouponSystemUsersDataSetter {
private static CouponSystemBeanFactory couponBeanFactory = CouponSystemBeanFactory.getCouponSystemBeanFactoryInstance();
private static CompanyServiceTesting companyServiceTesting = CompanyServiceTesting.companyServiceTestingInstance();
private static AdminServiceTesting adminServiceTesting = AdminServiceTesting.getAdminServiceTestingInstance();
public static Map<String,Object> getInstances() {
return instances;
}
private static Map<String,Object> instances = new HashMap<>();
private RegistrationData callForRegistration(){
return couponBeanFactory.getBeanInstance("registration" , RegistrationData.class);
}
private static Company callForCompany(){
return couponBeanFactory.getBeanInstance("company" , Company.class);
}
private static Coupon callForCoupon(){
return couponBeanFactory.getBeanInstance("coupon" , Coupon.class);
}
private Customer callForCustomer(){
return couponBeanFactory.getBeanInstance("customer",Customer.class);
}
private Employee callForEmployee(){
return couponBeanFactory.getBeanInstance("employee",Employee.class);
}
private User callForUser(){
return couponBeanFactory.getBeanInstance("user",User.class);
}
@Before
public void createCoupons(){
System.out.println("Setting coupons data! ");
Map<String,Object> instances = getInstances();
instances.put("coupon1",companyServiceTesting.createCompanyCoupon((Company)instances.get("company1"),callForCoupon(),"Travelling low prices","FamilyTravelling",
23,765475475,"C://ProgramFiles",35.8, CouponType.TRAVELING, CouponSystemDate.dateFormat(2017,12,12)
,CouponSystemDate.dateFormat(2019,12,12)));
instances.put("coupon2",companyServiceTesting.createCompanyCoupon((Company)instances.get("company2"),callForCoupon(),"Camping low prices","FamilyCamping",
13,9999887,"C://ProgramFiles",55.8, CouponType.CAMPING, CouponSystemDate.dateFormat(2016,10,10)
,CouponSystemDate.dateFormat(2018,9,1)));
instances.put("coupon3",companyServiceTesting.createCompanyCoupon((Company)instances.get("company1"),callForCoupon(),"Electricity low prices","Electricity",
12,5424352,"C://ProgramFiles",25.8, CouponType.ELECTRICITY, CouponSystemDate.dateFormat(2017,7,3)
,CouponSystemDate.dateFormat(2018,4,5)));
instances.put("coupon4",companyServiceTesting.createCompanyCoupon((Company)instances.get("company2"),callForCoupon(),"Food low prices","FamilyDinner",
53,989873,"C://ProgramFiles",15.8, CouponType.FOOD, CouponSystemDate.dateFormat(2017,4,11)
,CouponSystemDate.dateFormat(2018,4,11)));
instances.put("coupon5",companyServiceTesting.createCompanyCoupon((Company)instances.get("company1"),callForCoupon(),"Health insurance low prices","FamilyInsurance",
83,33344455,"C://ProgramFiles",88.8, CouponType.HEALTH, CouponSystemDate.dateFormat(2017,8,9)
,CouponSystemDate.dateFormat(2018,8,9)));
instances.put("coupon6",companyServiceTesting.createCompanyCoupon((Company)instances.get("company2"),callForCoupon(),"Sport village low prices","SportVacation",
73,6665432,"C://ProgramFiles",95.8, CouponType.SPORTS, CouponSystemDate.dateFormat(2017,7,11)
,CouponSystemDate.dateFormat(2019,7,11)));
}
@BeforeClass
public static void createCompanies(){
System.out.println("Setting companies data! ");
Map<String,Object> instances = getInstances();
instances.put("company1",adminServiceTesting.createCompany("ORG","Free@88",34553453,"ORG@.com",callForCompany()));
instances.put("company2",adminServiceTesting.createCompany("PPP","Trading@000",4536579,"PPP@.com",callForCompany()));
}
@Test
public void createCustomers(){
System.out.println("Setting customer data! ");
Map<String,Object> instances = getInstances();
instances.put("customer1",companyServiceTesting.createCustomer(787654,"Natali@gmail.com","Nata000@99","Nata","Vladimi",callForCustomer()));
instances.put("customer2",companyServiceTesting.createCustomer(980982,"Maks@gmail.com","MMM@mmm999","Maks","Burkov",callForCustomer()));
instances.put("customer3",companyServiceTesting.createCustomer(888776655,"Sasha@gamil.com","SSS0001988@","Sasha","Sashkoviz",callForCustomer()));
instances.put("customer4",companyServiceTesting.createCustomer(4443332,"Lumila@gmail.com","lumi00088@","Lumila","Lumovich",callForCustomer()));
instances.put("customer5",companyServiceTesting.createCustomer(222888444,"Yani@gmail.com","yani@_777","Yani","Yovi",callForCustomer()));
instances.put("customer6",companyServiceTesting.createCustomer(4325432,"Yoni@gmail.com","Yoni@09087","Yoni","Yonivich",callForCustomer()));
}
@Before
public void createEmployees(){
System.out.println("Setting employee data! ");
Map<String,Object> instances = getInstances();
instances.put("employee1",companyServiceTesting.createCompanyEmployee(callForEmployee(),(Company)instances.get("company1"),222222,"Rahel@gmail.com","Israel","Rahel","Rehelovich","059876534"));
instances.put("employee2",companyServiceTesting.createCompanyEmployee(callForEmployee(),(Company)instances.get("company2"),111111,"Yosi@gmail.com","Israel","Yosi","Izhak","055312567"));
instances.put("employee3",companyServiceTesting.createCompanyEmployee(callForEmployee(),(Company)instances.get("company1"),45353,"Pavel@gmail.com","Israel","Pavel","Pavlov","050689321"));
instances.put("employee4",companyServiceTesting.createCompanyEmployee(callForEmployee(),(Company)instances.get("company2"),3498766,"Luba@gmail.com","Israel","Luba","Lubovkina","0547890122"));
instances.put("employee5",companyServiceTesting.createCompanyEmployee(callForEmployee(),(Company)instances.get("company1"),8888453,"Miha@gmail.com","Israel","Miha","Mihuelkin","050934122"));
instances.put("employee6",companyServiceTesting.createCompanyEmployee(callForEmployee(),(Company)instances.get("company2"),9879874,"Avi@gmail.com","Israel","Avi","AviAla","0547334567"));
}
@After
public void createUsers(){
System.out.println("Setting users data! ");
Map<String,Object> instances = getInstances();
Customer c1 = (Customer) instances.get("customer1"); Customer c2 = (Customer) instances.get("customer2"); Customer c3 = (Customer) instances.get("customer3");
Customer c4 = (Customer) instances.get("customer4"); Customer c5 = (Customer) instances.get("customer5"); Customer c6 = (Customer) instances.get("customer6");
instances.put("user1",RegisterServiceTesting.createUser("Customer1",c1.getPassword(),c1.getEmail(),"customer",callForUser()));
instances.put("user2",RegisterServiceTesting.createUser("Customer2",c2.getPassword(),c2.getEmail(),"customer",callForUser()));
instances.put("user3",RegisterServiceTesting.createUser("Customer3",c3.getPassword(),c3.getEmail(),"customer",callForUser()));
instances.put("user4",RegisterServiceTesting.createUser("customer4",c4.getPassword(),c4.getEmail(),"customer",callForUser()));
instances.put("user5",RegisterServiceTesting.createUser("customer5",c5.getPassword(),c5.getEmail(),"customer",callForUser()));
instances.put("user6",RegisterServiceTesting.createUser("customer6",c6.getPassword(),c6.getEmail(),"customer",callForUser()));
}
@AfterClass
public static void setLoginData(){
System.out.println("Setting login data! ");
Map<String,Object> instances = getInstances();
instances.put("login1",instances.get("user1"));
instances.put("login2",instances.get("user2"));
instances.put("login3",instances.get("user3"));
}
@AfterClass
public static void checkTheSetedObjects(){
System.out.println("Checking the setted data! ");
Map<String,Object> instances = getInstances();
Iterator<Map.Entry<String,Object>> iterator = instances.entrySet().iterator();
while (iterator.hasNext()){
Object value = iterator.next().getValue();
System.out.println("Iterated value: "+value);
}
}
@Entity
@Table(name = "coupons")
public class Coupon implements Serializable{
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
@Transient
private Company company;
@Id
@Column(name = "COUPON_ID")
@Size(min = 5, max = 20 , message = "Coupon id must be 5-20! ")
private long id;
@Column(name = "COUPON_TITLE" , unique = true)
@Size(min = 10, max = 20 , message = "Coupon title must be 10-20! ")
private String title;
@Temporal(TemporalType.DATE)
@Column(name = "RELEASE_DATE")
@Pattern(regexp = "^(\\d{4})-(\\d{2})-(\\d{2})$",
message = "Wrong date format! Correct example: YYYY-MM-DD")
private Date startDate;
@Temporal(TemporalType.DATE)
@Column(name = "EXPIRATION_DATE")
@Pattern(regexp = "^(\\d{4})-(\\d{2})-(\\d{2})$",
message = "Wrong date format! Correct example: YYYY-MM-DD" )
private Date endDate;
@Column(name = "COUPON_AMOUNT")
private int amount;
@Column(name = "COUPON_MESSAGE")
@Size(min = 10, max = 30 , message = "Coupon message must be 10-30! ")
private String message;
@Column(name = "COUPON_PRICE")
private double price;
@Column(name = "COUPON_IMAGE")
@Pattern(regexp = "([^\\s]+(\\.(?i)(jpg|png|gif|bmp))$)",
message = "Wrong image url! Correct examples: " +
"1. “a.jpg”, “a.gif”,”a.png”, “a.bmp”,\n" +
"2. “..jpg”, “..gif”,”..png”, “..bmp”,\n" +
"3. “a.JPG”, “a.GIF”,”a.PNG”, “a.BMP”,\n" +
"4. “a.JpG”, “a.GiF”,”a.PnG”, “a.BmP”,\n" +
"5. “jpg.jpg”, “gif.gif”,”png.png”, “bmp.bmp”")
private String image;
@Column(name = "COUPON_TYPE")
@Enumerated(EnumType.STRING)
@NotBlank(message = "Coupon type can't be empty! ")
private CouponType type;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Coupon coupon = (Coupon) o;
if (id != coupon.id) return false;
if (amount != coupon.amount) return false;
if (Double.compare(coupon.price, price) != 0) return false;
if (!title.equals(coupon.title)) return false;
if (!startDate.equals(coupon.startDate)) return false;
if (!endDate.equals(coupon.endDate)) return false;
if (!message.equals(coupon.message)) return false;
if (!image.equals(coupon.image)) return false;
return type == coupon.type;
}
@Override
public int hashCode() {
int result;
long temp;
result = (int) (id ^ (id >>> 32));
result = 31 * result + title.hashCode();
result = 31 * result + startDate.hashCode();
result = 31 * result + endDate.hashCode();
result = 31 * result + amount;
result = 31 * result + message.hashCode();
temp = Double.doubleToLongBits(price);
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + image.hashCode();
result = 31 * result + type.hashCode();
return result;
}
public Coupon() {}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public CouponType getType() {
return type;
}
public void setType(CouponType type) {
this.type = type;
}
@Override
public String toString() {
return "Coupon [id=" + id + ", title=" + title + ", startDate=" + startDate + ", endDate=" + endDate
+ ", amount=" + amount + ", message=" + message + ", price=" + price + ", image=" + image + ", type="
+ type + "]";
}
}