forked from JavaOPs/topjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_11_test_UserService.patch
More file actions
335 lines (317 loc) · 11.3 KB
/
3_11_test_UserService.patch
File metadata and controls
335 lines (317 loc) · 11.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
Index: src/test/java/ru/javawebinar/topjava/service/UserServiceTest.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/test/java/ru/javawebinar/topjava/service/UserServiceTest.java (date 1531054719665)
+++ src/test/java/ru/javawebinar/topjava/service/UserServiceTest.java (date 1531054719665)
@@ -0,0 +1,87 @@
+package ru.javawebinar.topjava.service;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DataAccessException;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.jdbc.Sql;
+import org.springframework.test.context.jdbc.SqlConfig;
+import org.springframework.test.context.junit4.SpringRunner;
+import ru.javawebinar.topjava.model.Role;
+import ru.javawebinar.topjava.model.User;
+import ru.javawebinar.topjava.util.exception.NotFoundException;
+
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+
+import static ru.javawebinar.topjava.UserTestData.*;
+
+@ContextConfiguration({
+ "classpath:spring/spring-app.xml",
+ "classpath:spring/spring-db.xml"
+})
+@RunWith(SpringRunner.class)
+@Sql(scripts = "classpath:db/populateDB.sql", config = @SqlConfig(encoding = "UTF-8"))
+public class UserServiceTest {
+
+ @Autowired
+ private UserService service;
+
+ @Test
+ public void create() throws Exception {
+ User newUser = new User(null, "New", "new@gmail.com", "newPass", 1555, false, new Date(), Collections.singleton(Role.ROLE_USER));
+ User created = service.create(newUser);
+ newUser.setId(created.getId());
+ assertMatch(service.getAll(), ADMIN, newUser, USER);
+ }
+
+ @Test(expected = DataAccessException.class)
+ public void duplicateMailCreate() throws Exception {
+ service.create(new User(null, "Duplicate", "user@yandex.ru", "newPass", Role.ROLE_USER));
+ }
+
+ @Test
+ public void delete() throws Exception {
+ service.delete(USER_ID);
+ assertMatch(service.getAll(), ADMIN);
+ }
+
+ @Test(expected = NotFoundException.class)
+ public void notFoundDelete() throws Exception {
+ service.delete(1);
+ }
+
+ @Test
+ public void get() throws Exception {
+ User user = service.get(USER_ID);
+ assertMatch(user, USER);
+ }
+
+ @Test(expected = NotFoundException.class)
+ public void getNotFound() throws Exception {
+ service.get(1);
+ }
+
+ @Test
+ public void getByEmail() throws Exception {
+ User user = service.getByEmail("user@yandex.ru");
+ assertMatch(user, USER);
+ }
+
+ @Test
+ public void update() throws Exception {
+ User updated = new User(USER);
+ updated.setName("UpdatedName");
+ updated.setCaloriesPerDay(330);
+ service.update(updated);
+ assertMatch(service.get(USER_ID), updated);
+ }
+
+ @Test
+ public void getAll() throws Exception {
+ List<User> all = service.getAll();
+ assertMatch(all, ADMIN, USER);
+ }
+}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/repository/jdbc/JdbcMealRepositoryImpl.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/repository/jdbc/JdbcMealRepositoryImpl.java (date 1531054719648)
+++ src/main/java/ru/javawebinar/topjava/repository/jdbc/JdbcMealRepositoryImpl.java (date 1531054719648)
@@ -0,0 +1,37 @@
+package ru.javawebinar.topjava.repository.jdbc;
+
+import org.springframework.stereotype.Repository;
+import ru.javawebinar.topjava.model.Meal;
+import ru.javawebinar.topjava.repository.MealRepository;
+
+import java.time.LocalDateTime;
+import java.util.List;
+
+@Repository
+public class JdbcMealRepositoryImpl implements MealRepository {
+
+ @Override
+ public Meal save(Meal meal, int userId) {
+ return null;
+ }
+
+ @Override
+ public boolean delete(int id, int userId) {
+ return false;
+ }
+
+ @Override
+ public Meal get(int id, int userId) {
+ return null;
+ }
+
+ @Override
+ public List<Meal> getAll(int userId) {
+ return null;
+ }
+
+ @Override
+ public List<Meal> getBetween(LocalDateTime startDate, LocalDateTime endDate, int userId) {
+ return null;
+ }
+}
Index: src/main/java/ru/javawebinar/topjava/model/AbstractBaseEntity.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/model/AbstractBaseEntity.java (date 1531054144000)
+++ src/main/java/ru/javawebinar/topjava/model/AbstractBaseEntity.java (date 1531054719601)
@@ -1,8 +1,13 @@
package ru.javawebinar.topjava.model;
public abstract class AbstractBaseEntity {
+ public static final int START_SEQ = 100000;
+
protected Integer id;
+ public AbstractBaseEntity() {
+ }
+
protected AbstractBaseEntity(Integer id) {
this.id = id;
}
@@ -23,4 +28,22 @@
public String toString() {
return String.format("Entity %s (%s)", getClass().getName(), id);
}
+
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ AbstractBaseEntity that = (AbstractBaseEntity) o;
+ return id != null && id.equals(that.id);
+ }
+
+ @Override
+ public int hashCode() {
+ return id == null ? 0 : id;
+ }
}
\ No newline at end of file
Index: src/main/java/ru/javawebinar/topjava/model/User.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/model/User.java (date 1531054144000)
+++ src/main/java/ru/javawebinar/topjava/model/User.java (date 1531054719640)
@@ -1,8 +1,8 @@
package ru.javawebinar.topjava.model;
-import java.util.Date;
-import java.util.EnumSet;
-import java.util.Set;
+import org.springframework.util.CollectionUtils;
+
+import java.util.*;
import static ru.javawebinar.topjava.util.MealsUtil.DEFAULT_CALORIES_PER_DAY;
@@ -20,17 +20,25 @@
private int caloriesPerDay = DEFAULT_CALORIES_PER_DAY;
+ public User() {
+ }
+
+ public User(User u) {
+ this(u.getId(), u.getName(), u.getEmail(), u.getPassword(), u.getCaloriesPerDay(), u.isEnabled(), u.getRegistered(), u.getRoles());
+ }
+
public User(Integer id, String name, String email, String password, Role role, Role... roles) {
- this(id, name, email, password, DEFAULT_CALORIES_PER_DAY, true, EnumSet.of(role, roles));
+ this(id, name, email, password, DEFAULT_CALORIES_PER_DAY, true, new Date(), EnumSet.of(role, roles));
}
- public User(Integer id, String name, String email, String password, int caloriesPerDay, boolean enabled, Set<Role> roles) {
+ public User(Integer id, String name, String email, String password, int caloriesPerDay, boolean enabled, Date registered, Collection<Role> roles) {
super(id, name);
this.email = email;
this.password = password;
this.caloriesPerDay = caloriesPerDay;
this.enabled = enabled;
- this.roles = roles;
+ this.registered = registered;
+ setRoles(roles);
}
public String getEmail() {
@@ -77,6 +85,10 @@
return password;
}
+ public void setRoles(Collection<Role> roles) {
+ this.roles = CollectionUtils.isEmpty(roles) ? Collections.emptySet() : EnumSet.copyOf(roles);
+ }
+
@Override
public String toString() {
return "User (" +
Index: pom.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- pom.xml (date 1531054144000)
+++ pom.xml (date 1531054768609)
@@ -114,6 +114,12 @@
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <version>3.10.0</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<profiles>
Index: src/main/resources/spring/spring-app.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/resources/spring/spring-app.xml (date 1531054144000)
+++ src/main/resources/spring/spring-app.xml (date 1531054719656)
@@ -15,7 +15,7 @@
<!-- Not necessary, already included with component-scan -->
<!--<context:annotation-config/>-->
- <context:component-scan base-package="ru.javawebinar.**.repository"/>
+ <context:component-scan base-package="ru.javawebinar.**.repository.jdbc"/>
<context:component-scan base-package="ru.javawebinar.**.service"/>
Index: src/test/java/ru/javawebinar/topjava/UserTestData.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/test/java/ru/javawebinar/topjava/UserTestData.java (date 1531054144000)
+++ src/test/java/ru/javawebinar/topjava/UserTestData.java (date 1531054719672)
@@ -3,10 +3,27 @@
import ru.javawebinar.topjava.model.Role;
import ru.javawebinar.topjava.model.User;
+import java.util.Arrays;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static ru.javawebinar.topjava.model.AbstractBaseEntity.START_SEQ;
+
public class UserTestData {
- public static final int USER_ID = 1;
- public static final int ADMIN_ID = 2;
+ public static final int USER_ID = START_SEQ;
+ public static final int ADMIN_ID = START_SEQ + 1;
public static final User USER = new User(USER_ID, "User", "user@yandex.ru", "password", Role.ROLE_USER);
public static final User ADMIN = new User(ADMIN_ID, "Admin", "admin@gmail.com", "admin", Role.ROLE_ADMIN);
+
+ public static void assertMatch(User actual, User expected) {
+ assertThat(actual).isEqualToIgnoringGivenFields(expected, "registered", "roles");
+ }
+
+ public static void assertMatch(Iterable<User> actual, User... expected) {
+ assertMatch(actual, Arrays.asList(expected));
+ }
+
+ public static void assertMatch(Iterable<User> actual, Iterable<User> expected) {
+ assertThat(actual).usingElementComparatorIgnoringFields("registered", "roles").isEqualTo(expected);
+ }
}
Index: src/main/java/ru/javawebinar/topjava/model/AbstractNamedEntity.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/model/AbstractNamedEntity.java (date 1531054144000)
+++ src/main/java/ru/javawebinar/topjava/model/AbstractNamedEntity.java (date 1531054719622)
@@ -4,6 +4,9 @@
protected String name;
+ public AbstractNamedEntity() {
+ }
+
protected AbstractNamedEntity(Integer id, String name) {
super(id);
this.name = name;