forked from JavaOPs/topjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestUtil.java
More file actions
45 lines (37 loc) · 1.58 KB
/
Copy pathTestUtil.java
File metadata and controls
45 lines (37 loc) · 1.58 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
package ru.javawebinar.topjava;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.RequestPostProcessor;
import ru.javawebinar.topjava.matcher.ModelMatcher;
import ru.javawebinar.topjava.model.User;
import java.io.UnsupportedEncodingException;
/**
* GKislin
* 05.01.2015.
*/
public class TestUtil {
public static ResultActions print(ResultActions action) throws UnsupportedEncodingException {
System.out.println(getContent(action));
return action;
}
public static String getContent(ResultActions action) throws UnsupportedEncodingException {
return action.andReturn().getResponse().getContentAsString();
}
public static RequestPostProcessor userHttpBasic(User user) {
return SecurityMockMvcRequestPostProcessors.httpBasic(user.getEmail(), user.getPassword());
}
public static void authorize(User user) {
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(user.getEmail(), user.getPassword()));
}
/**
* Compare entities using toString
*/
public static class ToStringModelMatcher<T> extends ModelMatcher<T, String> {
public ToStringModelMatcher(Class<T> entityClass) {
super(Object::toString, entityClass);
}
}
}