Revize d60d25d0
Přidáno uživatelem Jiri Trefil před asi 2 roky(ů)
pom.xml | ||
---|---|---|
93 | 93 |
</dependency> |
94 | 94 |
|
95 | 95 |
|
96 |
|
|
97 |
|
|
96 | 98 |
<!-- Junit libraries for unit tests--> |
97 | 99 |
<dependency> |
98 | 100 |
<groupId>org.junit.jupiter</groupId> |
... | ... | |
122 | 124 |
<version>5.2.0</version> |
123 | 125 |
<scope>test</scope> |
124 | 126 |
</dependency> |
127 |
<!-- Spring TESTING --> |
|
128 |
<dependency> |
|
129 |
<groupId>org.springframework.security</groupId> |
|
130 |
<artifactId>spring-security-test</artifactId> |
|
131 |
<scope>test</scope> |
|
132 |
</dependency> |
|
125 | 133 |
|
126 | 134 |
</dependencies> |
127 | 135 |
|
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/UserController.java | ||
---|---|---|
1 | 1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.controller; |
2 | 2 |
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.UserModelStatusCodes; |
|
3 | 4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
4 | 5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.UserService; |
5 | 6 |
import org.springframework.beans.factory.annotation.Autowired; |
... | ... | |
26 | 27 |
* @return message |
27 | 28 |
*/ |
28 | 29 |
@PostMapping(value = "/register") |
29 |
public String registerUser(@RequestBody User user) {
|
|
30 |
int statusCode = userService.registerUser(user);
|
|
31 |
return "user register";
|
|
30 |
public ResponseEntity<String> registerUser(@RequestBody User user) {
|
|
31 |
UserModelStatusCodes statusCode = userService.registerUser(user);
|
|
32 |
return getResponseEntity(statusCode);
|
|
32 | 33 |
} |
33 | 34 |
|
34 | 35 |
/** |
... | ... | |
37 | 38 |
* @return message |
38 | 39 |
*/ |
39 | 40 |
@PostMapping(value = "/login") |
40 |
public String loginUser(@RequestBody User user) {
|
|
41 |
int statusCode = userService.loginUser(user);
|
|
42 |
return "user logged";
|
|
41 |
public ResponseEntity<String> loginUser(@RequestBody User user) {
|
|
42 |
UserModelStatusCodes statusCode = userService.loginUser(user);
|
|
43 |
return getResponseEntity(statusCode);
|
|
43 | 44 |
} |
44 | 45 |
|
45 | 46 |
/** |
... | ... | |
48 | 49 |
* @return message after logout |
49 | 50 |
*/ |
50 | 51 |
@PostMapping(value = "/logout") |
51 |
public String logoutUser(@RequestBody User user) {
|
|
52 |
int statusCode = userService.logoutUser(user);
|
|
53 |
return "user logout";
|
|
52 |
public ResponseEntity<String> logoutUser(@RequestBody User user) {
|
|
53 |
UserModelStatusCodes statusCode = userService.logoutUser(user);
|
|
54 |
return getResponseEntity(statusCode);
|
|
54 | 55 |
} |
55 | 56 |
|
57 |
/** |
|
58 |
* Method to create response |
|
59 |
* @param statusCode UserModelStatusCodes code |
|
60 |
* @return ResponseEntity with code and msg |
|
61 |
*/ |
|
62 |
private ResponseEntity<String> getResponseEntity(UserModelStatusCodes statusCode) { |
|
63 |
String message = this.generateResponseObject(statusCode); |
|
64 |
int code = statusCode.statusCode; |
|
65 |
return new ResponseEntity<>(message, HttpStatus.valueOf(code)); |
|
66 |
} |
|
67 |
|
|
68 |
/** |
|
69 |
* Method to create JSON object |
|
70 |
* @param code UserModelStatusCodes code |
|
71 |
* @return String that represents JSON object |
|
72 |
*/ |
|
73 |
private String generateResponseObject(UserModelStatusCodes code){ |
|
74 |
HashMap<String, String> json = new HashMap<>(); |
|
75 |
json.put("msg", code.label); |
|
76 |
return JSONBuilder.buildJson(json); |
|
77 |
|
|
78 |
} |
|
79 |
|
|
80 |
|
|
81 |
|
|
56 | 82 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/dials/UserModelStatusCodes.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials; |
|
2 |
|
|
3 |
/** |
|
4 |
* Enum for status codes |
|
5 |
*/ |
|
6 |
public enum UserModelStatusCodes { |
|
7 |
INVALID_USER_ARGUMENTS("Invalid user arguments for registration",400), |
|
8 |
USER_EXISTS("User exists",400), |
|
9 |
HASH_FAILED("Error occurred while creating user",500), |
|
10 |
USER_CREATED("User successfully created",201), |
|
11 |
USER_LOGGED_IN("User logged in successfully",200), |
|
12 |
USER_LOGIN_FAILED("User login failed",401), |
|
13 |
USER_CREATION_FAILED("Error occurred while creating user",503), |
|
14 |
USER_LOGGED_OUT("User logged out.",200); |
|
15 |
|
|
16 |
public final String label; |
|
17 |
public final int statusCode; |
|
18 |
|
|
19 |
private UserModelStatusCodes(String s, int i) { |
|
20 |
this.label = s; |
|
21 |
this.statusCode = i; |
|
22 |
} |
|
23 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/User.java | ||
---|---|---|
34 | 34 |
@Column(length = MAX_COLUMN_LENGTH) |
35 | 35 |
private String email; |
36 | 36 |
|
37 |
/** |
|
38 |
* Password of the User |
|
39 |
*/ |
|
40 |
@Column(length = MAX_COLUMN_LENGTH) |
|
41 |
private String password; |
|
42 |
|
|
37 | 43 |
public User() { |
38 | 44 |
} |
39 | 45 |
|
... | ... | |
42 | 48 |
this.email = email; |
43 | 49 |
} |
44 | 50 |
|
51 |
public User(String name, String email, String password){ |
|
52 |
this.name = name; |
|
53 |
this.email = email; |
|
54 |
this.password = password; |
|
55 |
} |
|
56 |
|
|
57 |
|
|
45 | 58 |
/** |
46 | 59 |
* Getter of id |
47 | 60 |
* @return id of the user |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserService.java | ||
---|---|---|
1 | 1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service; |
2 | 2 |
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.UserModelStatusCodes; |
|
3 | 4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
4 | 5 |
|
5 | 6 |
/** |
... | ... | |
13 | 14 |
* @param user serialized JSON object representing user |
14 | 15 |
* @return Integer - status code of the operation, ie 1 - successful, 0 - name taken, ... |
15 | 16 |
*/ |
16 |
public int registerUser(User user);
|
|
17 |
public UserModelStatusCodes registerUser(User user);
|
|
17 | 18 |
/** |
18 | 19 |
* Method attempts to log in a user and returns status code indicating login result |
19 | 20 |
* @param user serialized JSON object representing user |
20 | 21 |
* @return Integer - status code of the operation, ie 1 - successful, 0 - failed, .... |
21 | 22 |
*/ |
22 |
public int loginUser(User user);
|
|
23 |
public UserModelStatusCodes loginUser(User user);
|
|
23 | 24 |
/** |
24 | 25 |
* Method attempts to log out a user |
25 | 26 |
* @param user serialized JSON object representing user |
26 | 27 |
* @return Integer - status code of the operation, ie 1 - successful, 0 - failed, .... |
27 | 28 |
*/ |
28 |
public int logoutUser(User user);
|
|
29 |
public UserModelStatusCodes logoutUser(User user);
|
|
29 | 30 |
|
30 | 31 |
|
31 | 32 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserServiceImpl.java | ||
---|---|---|
1 | 1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service; |
2 | 2 |
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.UserModelStatusCodes; |
|
3 | 4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
4 | 5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.UserRepository; |
5 | 6 |
import org.springframework.beans.factory.annotation.Autowired; |
6 | 7 |
import org.springframework.stereotype.Service; |
7 | 8 |
|
9 |
import java.nio.charset.StandardCharsets; |
|
10 |
import java.security.MessageDigest; |
|
11 |
import java.security.NoSuchAlgorithmException; |
|
12 |
import java.util.regex.Pattern; |
|
13 |
|
|
8 | 14 |
/** |
9 | 15 |
* Service provides functionality for user login/register/logout |
10 | 16 |
* @author Vaclav Hrabik, Jiri Trefil |
... | ... | |
25 | 31 |
* |
26 | 32 |
*/ |
27 | 33 |
@Override |
28 |
public int registerUser(User user) {
|
|
34 |
public UserModelStatusCodes registerUser(User user) {
|
|
29 | 35 |
final String email = user.getEmail(); |
30 | 36 |
final String name = user.getName(); |
31 |
//if client request violates constraints - kill the request. User will not be registered. |
|
32 |
if (email.length() > User.getEmailConstraint() || name.length() > User.getNameConstraint()) { |
|
33 |
return 0; |
|
34 |
} |
|
35 |
//if the username is taken, kill the request aswell |
|
37 |
final String password = user.getPassword(); |
|
38 |
|
|
39 |
if (!verifyUserParameters(name, email, password)) |
|
40 |
return UserModelStatusCodes.INVALID_USER_ARGUMENTS; |
|
41 |
|
|
42 |
//if the username is taken, kill the request as well |
|
36 | 43 |
if (userRepository.findByName(user.getName()) != null) { |
37 |
return -1; |
|
44 |
return UserModelStatusCodes.USER_EXISTS; |
|
45 |
} |
|
46 |
|
|
47 |
String passwordHash = hashPassword(password); |
|
48 |
//server side fault |
|
49 |
if (passwordHash == null) { |
|
50 |
return UserModelStatusCodes.HASH_FAILED; |
|
38 | 51 |
} |
39 | 52 |
//save the user |
40 | 53 |
User u = userRepository.save(user); |
41 | 54 |
|
42 | 55 |
//TODO request to OAuth for token - send user info to the oauth app for token |
43 | 56 |
//return okay status code, the user was created |
44 |
return 1; |
|
57 |
return UserModelStatusCodes.USER_CREATED; |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* @param email String - user email provided by client |
|
62 |
* @param name String - username provided by client |
|
63 |
* @return true if @param User is valid |
|
64 |
*/ |
|
65 |
private boolean verifyUserParameters(String name, String email, String password) { |
|
66 |
//if client send somehow not whole register request. |
|
67 |
if (email == null || name == null || password == null) { |
|
68 |
return false; |
|
69 |
} |
|
70 |
//if client request violates constraints - kill the request. User will not be registered. |
|
71 |
if (email.length() > User.getEmailConstraint() || name.length() > User.getNameConstraint()) { |
|
72 |
return false; |
|
73 |
} |
|
74 |
//regex for email validation |
|
75 |
String emailRegex = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$"; |
|
76 |
final Pattern emailPattern = Pattern.compile(emailRegex); |
|
77 |
//return true if email is valid (pattern wise) |
|
78 |
return emailPattern.matcher(email).find(); |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* Method to hash password |
|
83 |
* @param password password from client |
|
84 |
* @return hashed password for database |
|
85 |
*/ |
|
86 |
private String hashPassword(String password) { |
|
87 |
//standard java security encryption module |
|
88 |
MessageDigest digest = null; |
|
89 |
try{ |
|
90 |
//try to instance the class - throws an error if algorithm |
|
91 |
digest = MessageDigest.getInstance("SHA3-256"); |
|
92 |
} |
|
93 |
catch(NoSuchAlgorithmException exception){ |
|
94 |
exception.printStackTrace(); |
|
95 |
return null; |
|
96 |
} |
|
97 |
byte [] tmp = digest.digest(password.getBytes(StandardCharsets.UTF_8)); |
|
98 |
return new String(tmp,StandardCharsets.UTF_8); |
|
45 | 99 |
} |
46 | 100 |
|
47 | 101 |
/** |
... | ... | |
52 | 106 |
* 1 - if user is logged successfully |
53 | 107 |
*/ |
54 | 108 |
@Override |
55 |
public int loginUser(User user) {
|
|
109 |
public UserModelStatusCodes loginUser(User user) {
|
|
56 | 110 |
User u = userRepository.findByName(user.getName()); |
57 | 111 |
//TODO request to OAuth for authentication |
58 |
return u == null ? 0 : 1;
|
|
112 |
return u == null ? UserModelStatusCodes.USER_LOGIN_FAILED : UserModelStatusCodes.USER_LOGGED_IN;
|
|
59 | 113 |
} |
60 | 114 |
|
61 | 115 |
/** |
62 | 116 |
* Methods invalidates JWT token in OAuth application |
63 | 117 |
* @param user serialized JSON object representing user |
64 |
* @return |
|
118 |
* @return UserModelStatusCodes user logged out status flag
|
|
65 | 119 |
*/ |
66 | 120 |
@Override |
67 |
public int logoutUser(User user) {
|
|
121 |
public UserModelStatusCodes logoutUser(User user) {
|
|
68 | 122 |
//TODO with OAuth app |
69 |
return 0;
|
|
123 |
return UserModelStatusCodes.USER_LOGGED_OUT;
|
|
70 | 124 |
} |
71 | 125 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/JSONBuilder.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils; |
|
2 |
import com.fasterxml.jackson.databind.ObjectMapper; |
|
3 |
import com.fasterxml.jackson.databind.node.ObjectNode; |
|
4 |
|
|
5 |
import java.util.HashMap; |
|
6 |
|
|
7 |
/** |
|
8 |
* Version 1.0 - only simple json can be built |
|
9 |
* @version 1.0 |
|
10 |
* @author Vaclav Hrabik, Jiri Trefil |
|
11 |
*/ |
|
12 |
public class JSONBuilder { |
|
13 |
|
|
14 |
/** |
|
15 |
* Method transforms map into string representation of JSON object |
|
16 |
* @param map key value pair that will be generated as json line |
|
17 |
* @return String representation of JSON object |
|
18 |
*/ |
|
19 |
public static String buildJson(HashMap<String, String> map){ |
|
20 |
ObjectMapper mapper = new ObjectMapper(); |
|
21 |
ObjectNode jsonObject = mapper.createObjectNode(); |
|
22 |
for (String key : map.keySet()) { |
|
23 |
jsonObject.put(key,map.get(key)); |
|
24 |
} |
|
25 |
return jsonObject.toString(); |
|
26 |
} |
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
} |
src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/UserControllerTest.java | ||
---|---|---|
1 | 1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.controller; |
2 | 2 |
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.UserModelStatusCodes; |
|
3 | 4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.UserService; |
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.UserServiceImpl; |
|
5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder; |
|
6 |
import org.junit.jupiter.api.Test; |
|
5 | 7 |
import org.junit.jupiter.api.extension.ExtendWith; |
8 |
import org.mockito.Mockito; |
|
6 | 9 |
import org.springframework.beans.factory.annotation.Autowired; |
7 | 10 |
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
8 | 11 |
import org.springframework.boot.test.mock.mockito.MockBean; |
9 | 12 |
import org.springframework.security.test.context.support.WithMockUser; |
10 | 13 |
import org.springframework.test.context.junit.jupiter.SpringExtension; |
11 | 14 |
import org.springframework.test.web.servlet.MockMvc; |
15 |
import org.springframework.test.web.servlet.MvcResult; |
|
16 |
import org.springframework.test.web.servlet.RequestBuilder; |
|
17 |
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
|
18 |
|
|
19 |
import java.util.HashMap; |
|
20 |
|
|
21 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
22 |
import static org.mockito.ArgumentMatchers.any; |
|
12 | 23 |
|
13 | 24 |
@ExtendWith(SpringExtension.class) |
14 | 25 |
@WebMvcTest(value = UserController.class) |
... | ... | |
22 | 33 |
private UserService userService; |
23 | 34 |
|
24 | 35 |
|
36 |
@Test |
|
37 |
public void registerUserNew() throws Exception { |
|
38 |
Mockito.when(userService.registerUser(any())).thenReturn(UserModelStatusCodes.USER_CREATED); |
|
39 |
HashMap<String,String> map = new HashMap<>(); |
|
40 |
map.put("name","pepa"); |
|
41 |
map.put("password","ahojSvete"); |
|
42 |
map.put("email","yxz@ahoj.cz"); |
|
43 |
String json = JSONBuilder.buildJson(map); |
|
44 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
45 |
.post("/v2/user/register") |
|
46 |
.accept(MediaType.APPLICATION_JSON).content(json) |
|
47 |
.contentType(MediaType.APPLICATION_JSON); |
|
48 |
|
|
49 |
MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
50 |
|
|
51 |
MockHttpServletResponse response = result.getResponse(); |
|
52 |
|
|
53 |
assertEquals(HttpStatus.CREATED.value(), response.getStatus()); |
|
54 |
} |
|
55 |
@Test |
|
56 |
public void registerUserInvalidArguments() throws Exception { |
|
57 |
Mockito.when(userService.registerUser(any())).thenReturn(UserModelStatusCodes.INVALID_USER_ARGUMENTS); |
|
58 |
HashMap<String,String> map = new HashMap<>(); |
|
59 |
map.put("name","pepa"); |
|
60 |
map.put("password","ahojSvete"); |
|
61 |
map.put("email","yxz"); |
|
62 |
String json = JSONBuilder.buildJson(map); |
|
63 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
64 |
.post("/v2/user/register") |
|
65 |
.accept(MediaType.APPLICATION_JSON).content(json) |
|
66 |
.contentType(MediaType.APPLICATION_JSON); |
|
67 |
|
|
68 |
MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
69 |
|
|
70 |
MockHttpServletResponse response = result.getResponse(); |
|
71 |
|
|
72 |
assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatus()); |
|
73 |
} |
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
@Test |
|
78 |
public void registerUserExists() throws Exception { |
|
79 |
Mockito.when(userService.registerUser(any())).thenReturn(UserModelStatusCodes.USER_EXISTS); |
|
80 |
HashMap<String,String> map = new HashMap<>(); |
|
81 |
map.put("name","pepa"); |
|
82 |
map.put("password","ahojSvete"); |
|
83 |
map.put("email","yxz@ahoj.cz"); |
|
84 |
String json = JSONBuilder.buildJson(map); |
|
85 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
86 |
.post("/v2/user/register") |
|
87 |
.accept(MediaType.APPLICATION_JSON).content(json) |
|
88 |
.contentType(MediaType.APPLICATION_JSON); |
|
89 |
|
|
90 |
MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
91 |
|
|
92 |
MockHttpServletResponse response = result.getResponse(); |
|
93 |
|
|
94 |
assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatus()); |
|
95 |
} |
|
96 |
|
|
97 |
@Test |
|
98 |
public void loginValidUser() throws Exception { |
|
99 |
Mockito.when(userService.loginUser(any())).thenReturn(UserModelStatusCodes.USER_LOGGED_IN); |
|
100 |
HashMap<String,String> map = new HashMap<>(); |
|
101 |
map.put("name","pepa"); |
|
102 |
map.put("password","ahojSvete"); |
|
103 |
map.put("email","yxz@ahoj.cz"); |
|
104 |
String json = JSONBuilder.buildJson(map); |
|
105 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
106 |
.post("/v2/user/login") |
|
107 |
.accept(MediaType.APPLICATION_JSON).content(json) |
|
108 |
.contentType(MediaType.APPLICATION_JSON); |
|
109 |
|
|
110 |
MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
111 |
|
|
112 |
MockHttpServletResponse response = result.getResponse(); |
|
113 |
|
|
114 |
assertEquals(HttpStatus.OK.value(), response.getStatus()); |
|
115 |
} |
|
116 |
|
|
117 |
@Test |
|
118 |
public void loginInValidUser() throws Exception { |
|
119 |
Mockito.when(userService.loginUser(any())).thenReturn(UserModelStatusCodes.USER_LOGIN_FAILED); |
|
120 |
HashMap<String,String> map = new HashMap<>(); |
|
121 |
map.put("name","pepa"); |
|
122 |
map.put("password","ahojSvete"); |
|
123 |
map.put("email","yxz@ahoj.cz"); |
|
124 |
String json = JSONBuilder.buildJson(map); |
|
125 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
126 |
.post("/v2/user/login") |
|
127 |
.accept(MediaType.APPLICATION_JSON).content(json) |
|
128 |
.contentType(MediaType.APPLICATION_JSON); |
|
129 |
|
|
130 |
MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
131 |
|
|
132 |
MockHttpServletResponse response = result.getResponse(); |
|
133 |
|
|
134 |
assertEquals(HttpStatus.UNAUTHORIZED.value(), response.getStatus()); |
|
135 |
} |
|
136 |
|
|
137 |
@Test |
|
138 |
public void logoutUser() throws Exception { |
|
139 |
Mockito.when(userService.logoutUser(any())).thenReturn(UserModelStatusCodes.USER_LOGGED_OUT); |
|
140 |
HashMap<String,String> map = new HashMap<>(); |
|
141 |
map.put("name","pepa"); |
|
142 |
map.put("password","ahojSvete"); |
|
143 |
map.put("email","yxz"); |
|
144 |
String json = JSONBuilder.buildJson(map); |
|
145 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
146 |
.post("/v2/user/logout") |
|
147 |
.accept(MediaType.APPLICATION_JSON).content(json) |
|
148 |
.contentType(MediaType.APPLICATION_JSON); |
|
149 |
|
|
150 |
MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
151 |
|
|
152 |
MockHttpServletResponse response = result.getResponse(); |
|
153 |
|
|
154 |
assertEquals(HttpStatus.OK.value(), response.getStatus()); |
|
155 |
} |
|
156 |
|
|
157 |
|
|
158 |
|
|
25 | 159 |
} |
src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserServiceImpTest.java | ||
---|---|---|
15 | 15 |
import org.springframework.test.context.junit.jupiter.SpringExtension; |
16 | 16 |
|
17 | 17 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
18 |
import static org.mockito.ArgumentMatchers.any; |
|
19 |
import static org.mockito.Mockito.mock; |
|
18 | 20 |
|
19 | 21 |
/** |
20 | 22 |
* Tests for userServiceImplementation |
... | ... | |
39 | 41 |
@Autowired |
40 | 42 |
private UserService userService; |
41 | 43 |
|
44 |
|
|
45 |
|
|
42 | 46 |
/** |
43 | 47 |
* Mocked userRepository |
44 | 48 |
*/ |
... | ... | |
153 | 157 |
@Test |
154 | 158 |
public void whenUserIsNotRegistered_thenReturnMSG() { |
155 | 159 |
|
156 |
Mockito.when(userRepository.findByName(mockUser.getName())).thenReturn(mockUser); |
|
160 |
//Mockito.when(userRepository.findByName(mockUser.getName())).thenReturn(mockUser);
|
|
157 | 161 |
|
158 | 162 |
String name = "notest"; |
159 | 163 |
int foundCode = userService.loginUser(new User(name, "")); |
160 | 164 |
|
161 | 165 |
assertEquals(foundCode, 0); |
162 | 166 |
} |
167 |
|
|
163 | 168 |
} |
Také k dispozici: Unified diff
#10244 završení implementace service a controllerů. Sepsány jednotkové testy.