1 package net.avcompris.examples.users3.core.tests;
2
3 import static com.google.common.base.Preconditions.checkNotNull;
4 import static com.google.common.base.Preconditions.checkState;
5 import static net.avcompris.commons3.core.tests.CoreTestUtils.random40;
6 import static net.avcompris.commons3.databeans.DataBeans.instantiate;
7 import static net.avcompris.examples.shared3.core.tests.MyCoreTestUtils.defaultUser;
8 import static net.avcompris.examples.users3.query.UserFiltering.Field.ROLE;
9 import static net.avcompris.examples.users3.query.UserFilteringsUtils.eq;
10
11 import javax.annotation.Nullable;
12
13 import org.apache.commons.lang3.ArrayUtils;
14
15 import net.avcompris.commons3.api.User;
16 import net.avcompris.commons3.api.exception.ServiceException;
17 import net.avcompris.commons3.api.exception.UsernameNotFoundException;
18 import net.avcompris.examples.shared3.Role;
19 import net.avcompris.examples.users3.api.UserCreate;
20 import net.avcompris.examples.users3.api.UserInfo;
21 import net.avcompris.examples.users3.api.UsersInfo;
22 import net.avcompris.examples.users3.api.UsersQuery;
23 import net.avcompris.examples.users3.core.api.UsersService;
24
25 public abstract class UsersCoreTestUtils {
26
27 private static String CORRELATION_ID;
28 private static UsersService USERS_SERVICE;
29
30 public static String setCorrelationId(final String correlationId) {
31
32 CORRELATION_ID = checkNotNull(correlationId, "correlationId");
33
34 return correlationId;
35 }
36
37 public static String getCorrelationId() {
38
39 checkState(CORRELATION_ID != null, "CORRELATION_ID should have been set");
40
41 return CORRELATION_ID;
42 }
43
44 public static void setUsersService(final UsersService usersService) {
45
46 USERS_SERVICE = checkNotNull(usersService, "usersService");
47 }
48
49 public static UsersService getUsersService() {
50
51 checkState(USERS_SERVICE != null, "USERS_SERVICE should have been set");
52
53 return USERS_SERVICE;
54 }
55
56 public static User ensureAnyUser(final Role role, final String... usernamesToExclude) throws ServiceException {
57
58 checkNotNull(role, "role");
59
60 final UsersInfo users = getUsers(instantiate(UsersQuery.class)
61 .setFiltering(eq(ROLE, role)));
62
63 for (final UserInfo userInfo : users.getResults()) {
64
65 if (!ArrayUtils.contains(usernamesToExclude, userInfo.getUsername())) {
66
67 return toUser(userInfo);
68 }
69 }
70
71 final String username = random40("U-");
72
73 return toUser(createUser(username, instantiate(UserCreate.class)
74 .setEnabled(true)
75 .setRole(role)));
76 }
77
78 public static UserInfo createUser(final String username, final UserCreate create) throws ServiceException {
79
80 checkNotNull(username, "username");
81 checkNotNull(create, "create");
82
83 return getUsersService().createUser(getCorrelationId(), defaultUser(), username, create);
84 }
85
86 public static UsersInfo getUsers() throws ServiceException {
87
88 return getUsers(null);
89 }
90
91 public static UsersInfo getUsers(@Nullable final UsersQuery query) throws ServiceException {
92
93 return getUsersService().getUsers(getCorrelationId(), defaultUser(), query);
94 }
95
96 public static int getUserCount() throws Exception {
97
98 return getUsersService().getUsers(getCorrelationId(), defaultUser(), null)
99 .getTotal();
100 }
101
102 public static UserInfo getUser(String username) throws ServiceException {
103
104 return getUsersService().getUser(getCorrelationId(), defaultUser(), username);
105 }
106
107 public static String ensureUsername(final String username) throws ServiceException {
108
109 try {
110
111 getUsersService().getUser(getCorrelationId(), defaultUser(), username);
112
113 return username;
114
115 } catch (final UsernameNotFoundException e) {
116
117
118 }
119
120 getUsersService().createUser(getCorrelationId(), defaultUser(), username, instantiate(UserCreate.class)
121 .setRole(Role.REGULAR)
122 .setEnabled(true));
123
124 return username;
125 }
126
127 public static User toUser(final UserInfo userInfo) {
128
129 return new User() {
130
131 @Override
132 public String getUsername() {
133 return userInfo.getUsername();
134 }
135
136 @Override
137 public Role getRole() {
138 return userInfo.getRole();
139 }
140 };
141 }
142 }