forked from Realhedin/topjava02
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserTestData.java
More file actions
91 lines (73 loc) · 3.3 KB
/
UserTestData.java
File metadata and controls
91 lines (73 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
package ru.javawebinar.topjava;
import ru.javawebinar.topjava.matcher.ModelMatcher;
import ru.javawebinar.topjava.model.BaseEntity;
import ru.javawebinar.topjava.model.Role;
import ru.javawebinar.topjava.model.User;
import ru.javawebinar.topjava.util.PasswordUtil;
import java.util.EnumSet;
import java.util.Objects;
import java.util.Set;
/**
* User: gkislin
* Date: 26.08.2014
*/
public class UserTestData {
private static final LoggerWrapper LOG = LoggerWrapper.get(UserTestData.class);
public static final TestUser USER = new TestUser(BaseEntity.START_SEQ, "User", "user@yandex.ru", "password", true, Role.ROLE_USER);
public static final User ADMIN = new TestUser(BaseEntity.START_SEQ + 1, "Admin", "admin@gmail.com", "admin", true, Role.ROLE_ADMIN);
public static class TestUser extends User {
public TestUser(User u) {
this(u.getId(), u.getName(), u.getEmail(), u.getPassword(), u.isEnabled(), u.getRoles());
}
public TestUser(String name, String email, String password, Role role, Role... roles) {
this(null, name, email, password, true, EnumSet.of(role, roles));
}
public TestUser(Integer id, String name, String email, String password, boolean enabled, Role role, Role... roles) {
this(id, name, email, password, enabled, EnumSet.of(role, roles));
}
public TestUser(Integer id, String name, String email, String password, boolean enabled, Set<Role> roles) {
super(id, name, email, password, enabled, roles);
}
public User copyAsUser() {
return new User(this);
}
public User asUser() {
return new User(this);
}
@Override
public String toString() {
return "User (" +
"id=" + id +
", email=" + email +
", name=" + name +
", enabled=" + enabled +
", password=" + password +
", authorities=" + roles +
')';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TestUser that = (TestUser) o;
return //comparePassword(this.password, that.password) &&
Objects.equals(this.id, that.id)
&& Objects.equals(this.name, that.name)
&& Objects.equals(this.email, that.email)
&& Objects.equals(this.enabled, that.enabled)
&& Objects.equals(this.roles, that.roles)
&& Objects.equals(this.caloriesPerDay, that.caloriesPerDay);
}
}
private static boolean comparePassword(String rawPassword, String password) {
if (PasswordUtil.isEncoded(rawPassword)) {
LOG.warn("Expected password couldn't be compared with actual");
} else if (!PasswordUtil.isMatch(rawPassword, password)) {
LOG.error("Password " + password + " doesn't match encoded " + password);
return false;
}
return true;
}
public static final ModelMatcher<User, TestUser> MATCHER = new ModelMatcher<>(
u -> ((u instanceof TestUser) ? (TestUser) u : new TestUser(u)), User.class);
}