Revize a885eacd
Přidáno uživatelem Jiri Trefil před asi 2 roky(ů)
pom.xml | ||
---|---|---|
120 | 120 |
<scope>test</scope> |
121 | 121 |
</dependency> |
122 | 122 |
|
123 |
|
|
123 |
<!-- json library with minimal overhead --> |
|
124 |
<dependency> |
|
125 |
<groupId>com.googlecode.json-simple</groupId> |
|
126 |
<artifactId>json-simple</artifactId> |
|
127 |
<version>1.1.1</version> |
|
128 |
</dependency> |
|
124 | 129 |
<!-- UI TESTING --> |
125 | 130 |
<dependency> |
126 | 131 |
<groupId>org.seleniumhq.selenium</groupId> |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/UserController.java | ||
---|---|---|
107 | 107 |
* @return String that represents JSON object |
108 | 108 |
*/ |
109 | 109 |
private String generateResponseObject(UserModelStatusCodes code, String jwtToken) { |
110 |
HashMap<String, String> json = new HashMap<>();
|
|
110 |
HashMap<String, Object> json = new HashMap<>();
|
|
111 | 111 |
json.put("message", code.getLabel()); |
112 | 112 |
if (jwtToken != null) { |
113 | 113 |
json.put("jwtToken", jwtToken); |
114 | 114 |
} |
115 |
return JSONBuilder.buildJson(json);
|
|
115 |
return JSONBuilder.buildJSON(json);
|
|
116 | 116 |
} |
117 | 117 |
|
118 | 118 |
|
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/OAuthServiceImpl.java | ||
---|---|---|
18 | 18 |
import java.io.IOException; |
19 | 19 |
import java.util.ArrayList; |
20 | 20 |
import java.util.HashMap; |
21 |
import java.util.Map; |
|
21 | 22 |
|
22 | 23 |
/** |
23 | 24 |
* Service which communicate with OAuth application |
... | ... | |
50 | 51 |
private UserService userService; |
51 | 52 |
|
52 | 53 |
|
53 |
private RequestBuilder requestBuilder = new RequestBuilder(); |
|
54 |
//private RequestBuilder requestBuilder = new RequestBuilder();
|
|
54 | 55 |
|
55 | 56 |
public ResponseEntity<String> authenticate(String token) { |
56 | 57 |
|
57 |
return requestBuilder.sendRequestResponse(AUTH_URL_AUTH, token);
|
|
58 |
return RequestBuilder.sendRequestResponse(AUTH_URL_AUTH, token);
|
|
58 | 59 |
} |
59 | 60 |
|
60 | 61 |
public ResponseEntity<String> loginUser(User user) { |
... | ... | |
64 | 65 |
return null; |
65 | 66 |
} |
66 | 67 |
//HttpURLConnection con = RequestBuilder.createConnection(AUTH_URL); |
67 |
HashMap<String, String> requestBody = new HashMap<>();
|
|
68 |
Map<String, Object> requestBody = new HashMap<>();
|
|
68 | 69 |
|
69 | 70 |
requestBody.put("name", userName); |
70 | 71 |
|
71 |
return requestBuilder.sendRequestResponse(AUTH_URL_LOGIN, requestBody);
|
|
72 |
return RequestBuilder.sendRequestResponse(AUTH_URL_LOGIN, requestBody);
|
|
72 | 73 |
} |
73 | 74 |
|
74 | 75 |
public ResponseEntity<String> logoutUser(User user) { |
... | ... | |
80 | 81 |
} |
81 | 82 |
|
82 | 83 |
//HttpURLConnection con = RequestBuilder.createConnection(AUTH_URL); |
83 |
HashMap<String, String> requestBody = new HashMap<>();
|
|
84 |
HashMap<String, Object> requestBody = new HashMap<>();
|
|
84 | 85 |
|
85 | 86 |
requestBody.put("name", userName); |
86 | 87 |
requestBody.put("token", token); |
87 | 88 |
|
88 |
return requestBuilder.sendRequestResponse(AUTH_URL_LOGOUT, requestBody);
|
|
89 |
return RequestBuilder.sendRequestResponse(AUTH_URL_LOGOUT, requestBody);
|
|
89 | 90 |
} |
90 | 91 |
|
91 | 92 |
@Override |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/JSONBuilder.java | ||
---|---|---|
1 | 1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils; |
2 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
3 |
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
2 |
import org.json.simple.JSONArray;
|
|
3 |
import org.json.simple.JSONObject;
|
|
4 | 4 |
|
5 |
import java.util.ArrayList; |
|
5 | 6 |
import java.util.HashMap; |
7 |
import java.util.Map; |
|
6 | 8 |
|
7 | 9 |
/** |
8 | 10 |
* Version 1.0 - only simple json can be built |
... | ... | |
10 | 12 |
* @author Vaclav Hrabik, Jiri Trefil |
11 | 13 |
*/ |
12 | 14 |
public class JSONBuilder { |
13 |
|
|
14 | 15 |
/** |
15 | 16 |
* Method transforms map into string representation of JSON object |
16 |
* @param map key value pair that will be generated as json line
|
|
17 |
* @param json key value pair that will be generated as json line
|
|
17 | 18 |
* @return String representation of JSON object |
18 |
*/ |
|
19 |
public static String buildJson(HashMap<String, String> map){ |
|
20 |
if(map == null) { |
|
21 |
return ""; |
|
22 |
} |
|
23 |
ObjectMapper mapper = new ObjectMapper(); |
|
24 |
ObjectNode jsonObject = mapper.createObjectNode(); |
|
25 |
for (String key : map.keySet()) { |
|
26 |
jsonObject.put(key,map.get(key)); |
|
19 |
* */ |
|
20 |
|
|
21 |
public static String buildJSON(Map<String,Object> json){ |
|
22 |
|
|
23 |
JSONObject jsonObject = new JSONObject(); |
|
24 |
for(String key : json.keySet()){ |
|
25 |
jsonObject.put(key,parseJSONValue(json.get(key))); |
|
27 | 26 |
} |
28 |
return jsonObject.toString(); |
|
27 |
String jsonString = jsonObject.toJSONString(); |
|
28 |
|
|
29 |
return jsonString; |
|
29 | 30 |
} |
30 | 31 |
|
32 |
private static Object parseJSONValue(Object value){ |
|
33 |
|
|
34 |
if(value instanceof HashMap<?,?>){ |
|
35 |
Map<String,Object> map = null; |
|
36 |
try{ |
|
37 |
map = (HashMap<String,Object>) value; |
|
38 |
} |
|
39 |
catch (ClassCastException e){ |
|
40 |
throw new RuntimeException("Provided object of HashMap is not <String,Object> typed!"); |
|
41 |
} |
|
42 |
JSONObject jsonObject = new JSONObject(); |
|
43 |
for(String key : map.keySet()) |
|
44 |
jsonObject.put(key,parseJSONValue(map.get(key))); |
|
45 |
return jsonObject; |
|
31 | 46 |
|
32 | 47 |
|
48 |
} |
|
49 |
if (value instanceof ArrayList){ |
|
50 |
JSONArray jsonArray = new JSONArray(); |
|
51 |
ArrayList<Object> list; |
|
52 |
list = (ArrayList<Object>) value; |
|
53 |
for(int i = 0,n=list.size(); i < n; i++) |
|
54 |
jsonArray.add(parseJSONValue(list.get(i))); |
|
55 |
return jsonArray; |
|
56 |
} |
|
57 |
|
|
58 |
|
|
59 |
//some simple data type without any indentation, we can just return it |
|
60 |
return value; |
|
61 |
} |
|
33 | 62 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/ParamsBuilder.java | ||
---|---|---|
12 | 12 |
* @param parameters Key: value pair parameters |
13 | 13 |
* @return String JSON representation |
14 | 14 |
*/ |
15 |
public static String createPostParams(HashMap<String,String> parameters) {
|
|
16 |
return JSONBuilder.buildJson(parameters);
|
|
15 |
public static String createPostParams(HashMap<String,Object> parameters) {
|
|
16 |
return JSONBuilder.buildJSON(parameters);
|
|
17 | 17 |
} |
18 | 18 |
|
19 | 19 |
/** |
src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/UserControllerTest.java | ||
---|---|---|
39 | 39 |
@Test |
40 | 40 |
public void registerUserNew() throws Exception { |
41 | 41 |
Mockito.when(userService.registerUser(any())).thenReturn(UserModelStatusCodes.USER_CREATED); |
42 |
HashMap<String,String> map = new HashMap<>();
|
|
42 |
HashMap<String,Object> map = new HashMap<>();
|
|
43 | 43 |
map.put("name","pepa"); |
44 | 44 |
map.put("password","ahojSvete"); |
45 | 45 |
map.put("email","yxz@ahoj.cz"); |
46 |
String json = JSONBuilder.buildJson(map);
|
|
46 |
String json = JSONBuilder.buildJSON(map);
|
|
47 | 47 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
48 | 48 |
.post("/v2/user/register") |
49 | 49 |
.accept(MediaType.APPLICATION_JSON).content(json) |
... | ... | |
58 | 58 |
@Test |
59 | 59 |
public void registerUserInvalidArguments() throws Exception { |
60 | 60 |
Mockito.when(userService.registerUser(any())).thenReturn(UserModelStatusCodes.INVALID_USER_ARGUMENTS); |
61 |
HashMap<String,String> map = new HashMap<>();
|
|
61 |
HashMap<String,Object> map = new HashMap<>();
|
|
62 | 62 |
map.put("name","pepa"); |
63 | 63 |
map.put("password","ahojSvete"); |
64 | 64 |
map.put("email","yxz"); |
65 |
String json = JSONBuilder.buildJson(map);
|
|
65 |
String json = JSONBuilder.buildJSON(map);
|
|
66 | 66 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
67 | 67 |
.post("/v2/user/register") |
68 | 68 |
.accept(MediaType.APPLICATION_JSON).content(json) |
... | ... | |
80 | 80 |
@Test |
81 | 81 |
public void registerUserExists() throws Exception { |
82 | 82 |
Mockito.when(userService.registerUser(any())).thenReturn(UserModelStatusCodes.USER_EXISTS); |
83 |
HashMap<String,String> map = new HashMap<>();
|
|
83 |
HashMap<String,Object> map = new HashMap<>();
|
|
84 | 84 |
map.put("name","pepa"); |
85 | 85 |
map.put("password","ahojSvete"); |
86 | 86 |
map.put("email","yxz@ahoj.cz"); |
87 |
String json = JSONBuilder.buildJson(map);
|
|
87 |
String json = JSONBuilder.buildJSON(map);
|
|
88 | 88 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
89 | 89 |
.post("/v2/user/register") |
90 | 90 |
.accept(MediaType.APPLICATION_JSON).content(json) |
... | ... | |
100 | 100 |
@Test |
101 | 101 |
public void loginValidUser() throws Exception { |
102 | 102 |
Mockito.when(userService.verifyUser(any())).thenReturn(UserModelStatusCodes.USER_LOGGED_IN); |
103 |
HashMap<String,String> map = new HashMap<>();
|
|
103 |
HashMap<String,Object> map = new HashMap<>();
|
|
104 | 104 |
map.put("name","pepa"); |
105 | 105 |
map.put("password","ahojSvete"); |
106 | 106 |
map.put("email","yxz@ahoj.cz"); |
107 |
String json = JSONBuilder.buildJson(map);
|
|
107 |
String json = JSONBuilder.buildJSON(map);
|
|
108 | 108 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
109 | 109 |
.post("/v2/user/login") |
110 | 110 |
.accept(MediaType.APPLICATION_JSON).content(json) |
... | ... | |
120 | 120 |
@Test |
121 | 121 |
public void loginInValidUser() throws Exception { |
122 | 122 |
Mockito.when(userService.verifyUser(any())).thenReturn(UserModelStatusCodes.USER_LOGIN_FAILED); |
123 |
HashMap<String,String> map = new HashMap<>();
|
|
123 |
HashMap<String,Object> map = new HashMap<>();
|
|
124 | 124 |
map.put("name","pepa"); |
125 | 125 |
map.put("password","ahojSvete"); |
126 | 126 |
map.put("email","yxz@ahoj.cz"); |
127 |
String json = JSONBuilder.buildJson(map);
|
|
127 |
String json = JSONBuilder.buildJSON(map);
|
|
128 | 128 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
129 | 129 |
.post("/v2/user/login") |
130 | 130 |
.accept(MediaType.APPLICATION_JSON).content(json) |
src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/OAuthServiceImplTest.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.UserModelStatusCodes; |
4 | 4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder; |
|
5 | 6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.RequestBuilder; |
6 | 7 |
import org.junit.Test; |
7 | 8 |
import org.junit.runner.RunWith; |
... | ... | |
28 | 29 |
@Autowired |
29 | 30 |
private OAuthService oAuthService; |
30 | 31 |
|
31 |
@MockBean |
|
32 |
private RequestBuilder requestBuilder; |
|
33 | 32 |
|
34 | 33 |
/** |
35 | 34 |
* Mocked User |
36 | 35 |
*/ |
37 | 36 |
private final User mockUser = new User("foo", "foo@foo.cz", "foo"); |
38 |
|
|
37 |
private final String sampleToken = "eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiIyMGU4NmJiMC1lMDU4LTQwYTMtYjQyZC02ZTBjOWIyMmQ5MWQiL" + |
|
38 |
"CJzdWIiOiJmb28iLCJpYXQiOjE2ODE0MTY2ODAsImV4cCI6MTY4MTQxNjk4MH0.YJTwPEI4njQqYRuLGilf_oVl0gGD5BWFmdolk1O" + |
|
39 |
"vYWIgTcoIAFwh6bwqrW7XM6Hlj-ItYj9EmihYIqN5gfJVtg"; |
|
40 |
// valid user logged in - http response with code 200 and token in body is expected |
|
39 | 41 |
@Test |
40 | 42 |
public void loginValidUser(){ |
41 |
ResponseEntity<String> response = ResponseEntity.ok().body("eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiIyMGU4NmJiMC1lMDU4LTQwYTMtYjQyZC02ZTBjOWIyMmQ5MWQiL" + |
|
42 |
"CJzdWIiOiJmb28iLCJpYXQiOjE2ODE0MTY2ODAsImV4cCI6MTY4MTQxNjk4MH0.YJTwPEI4njQqYRuLGilf_oVl0gGD5BWFmdolk1O" + |
|
43 |
"vYWIgTcoIAFwh6bwqrW7XM6Hlj-ItYj9EmihYIqN5gfJVtg"); |
|
44 |
HashMap<String, String> hashMap = new HashMap<>(); |
|
45 |
hashMap.put("name", mockUser.getName()); |
|
46 |
// when(requestBuilder.sendRequestResponse(anyString(), anyCollection())).thenReturn(response); |
|
47 |
// try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
|
48 |
// requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), mockHashMap)).thenReturn(response); |
|
49 |
// } |
|
43 |
ResponseEntity<String> response = ResponseEntity.ok().body(sampleToken); |
|
44 |
try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
|
45 |
requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyMap())).thenReturn(response); |
|
46 |
ResponseEntity<String> response1 = oAuthService.loginUser(mockUser); |
|
50 | 47 |
|
51 |
ResponseEntity<String> response1 = oAuthService.loginUser(mockUser); |
|
48 |
assertEquals(response, response1); |
|
49 |
} |
|
50 |
} |
|
52 | 51 |
|
53 |
assertEquals(response, response1); |
|
52 |
// return 200 response if valid user is trying to authenticate |
|
53 |
@Test |
|
54 |
public void authenticateValidUser(){ |
|
55 |
ResponseEntity<String> expectedResponse = ResponseEntity.ok().body(mockUser.getName()); |
|
56 |
try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
|
57 |
requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyString())).thenReturn(expectedResponse); |
|
58 |
ResponseEntity<String> response = oAuthService.authenticate(sampleToken); |
|
59 |
assertEquals(expectedResponse, response); |
|
60 |
} |
|
54 | 61 |
|
55 | 62 |
} |
56 | 63 |
|
64 |
// return 401 if unauthorized user is trying to authenticate |
|
65 |
@Test |
|
66 |
public void authenticateInvalidUser(){ |
|
67 |
ResponseEntity<String> expectedResponse = ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(mockUser.getName()); |
|
68 |
try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
|
69 |
requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyString())).thenReturn(expectedResponse); |
|
70 |
ResponseEntity<String> response = oAuthService.authenticate(sampleToken); |
|
71 |
assertEquals(expectedResponse, response); |
|
72 |
} |
|
57 | 73 |
|
58 |
/* public ResponseEntity<String> loginUser(User user) { |
|
59 |
final String userName = user.getName(); |
|
60 |
|
|
61 |
if(userName == null) { |
|
62 |
return null; |
|
74 |
} |
|
75 |
@Test |
|
76 |
public void logoutUser(){ |
|
77 |
Map<String,Object> json = new HashMap<>(); |
|
78 |
json.put("message","ok"); |
|
79 |
//have to set token to the user - in this scenario we assume user is logged in |
|
80 |
mockUser.setToken(sampleToken); |
|
81 |
String jsonString = JSONBuilder.buildJSON(json); |
|
82 |
ResponseEntity<String> expectedResponse = ResponseEntity.status(HttpStatus.OK).body(jsonString); |
|
83 |
try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
|
84 |
requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyMap())).thenReturn(expectedResponse); |
|
85 |
ResponseEntity<String> response = oAuthService.logoutUser(mockUser); |
|
86 |
assertEquals(expectedResponse, response); |
|
63 | 87 |
} |
64 |
//HttpURLConnection con = RequestBuilder.createConnection(AUTH_URL); |
|
65 |
HashMap<String, String> requestBody = new HashMap<>(); |
|
66 | 88 |
|
67 |
requestBody.put("name", userName); |
|
68 | 89 |
|
69 |
return RequestBuilder.sendRequestResponse(AUTH_URL_LOGIN, requestBody); |
|
70 | 90 |
} |
71 |
*/ |
|
91 |
|
|
72 | 92 |
|
73 | 93 |
|
74 | 94 |
|
Také k dispozici: Unified diff
#10366 Dopsání testů pro autentikační službu. Mírný refactoring kódu pro vytváření JSON stringů.