|
|
|
@ -97,28 +97,28 @@ public abstract class AbstractControllerTest { |
|
|
|
|
|
|
|
protected static final String SYS_ADMIN_EMAIL = "sysadmin@thingsboard.org"; |
|
|
|
private static final String SYS_ADMIN_PASSWORD = "sysadmin"; |
|
|
|
|
|
|
|
|
|
|
|
protected static final String TENANT_ADMIN_EMAIL = "testtenant@thingsboard.org"; |
|
|
|
private static final String TENANT_ADMIN_PASSWORD = "tenant"; |
|
|
|
|
|
|
|
protected static final String CUSTOMER_USER_EMAIL = "testcustomer@thingsboard.org"; |
|
|
|
private static final String CUSTOMER_USER_PASSWORD = "customer"; |
|
|
|
|
|
|
|
|
|
|
|
protected MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), |
|
|
|
MediaType.APPLICATION_JSON.getSubtype(), |
|
|
|
Charset.forName("utf8")); |
|
|
|
|
|
|
|
protected MockMvc mockMvc; |
|
|
|
|
|
|
|
|
|
|
|
protected String token; |
|
|
|
protected String refreshToken; |
|
|
|
protected String username; |
|
|
|
|
|
|
|
private TenantId tenantId; |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("rawtypes") |
|
|
|
private HttpMessageConverter mappingJackson2HttpMessageConverter; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
private WebApplicationContext webApplicationContext; |
|
|
|
|
|
|
|
@ -132,7 +132,7 @@ public abstract class AbstractControllerTest { |
|
|
|
log.info("Finished test: {}", description.getMethodName()); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
void setConverters(HttpMessageConverter<?>[] converters) { |
|
|
|
|
|
|
|
@ -144,7 +144,7 @@ public abstract class AbstractControllerTest { |
|
|
|
Assert.assertNotNull("the JSON message converter must not be null", |
|
|
|
this.mappingJackson2HttpMessageConverter); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
public void setup() throws Exception { |
|
|
|
log.info("Executing setup"); |
|
|
|
@ -188,7 +188,7 @@ public abstract class AbstractControllerTest { |
|
|
|
public void teardown() throws Exception { |
|
|
|
log.info("Executing teardown"); |
|
|
|
loginSysAdmin(); |
|
|
|
doDelete("/api/tenant/"+tenantId.getId().toString()) |
|
|
|
doDelete("/api/tenant/" + tenantId.getId().toString()) |
|
|
|
.andExpect(status().isOk()); |
|
|
|
log.info("Executed teardown"); |
|
|
|
} |
|
|
|
@ -196,7 +196,7 @@ public abstract class AbstractControllerTest { |
|
|
|
protected void loginSysAdmin() throws Exception { |
|
|
|
login(SYS_ADMIN_EMAIL, SYS_ADMIN_PASSWORD); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected void loginTenantAdmin() throws Exception { |
|
|
|
login(TENANT_ADMIN_EMAIL, TENANT_ADMIN_PASSWORD); |
|
|
|
} |
|
|
|
@ -204,13 +204,13 @@ public abstract class AbstractControllerTest { |
|
|
|
protected void loginCustomerUser() throws Exception { |
|
|
|
login(CUSTOMER_USER_EMAIL, CUSTOMER_USER_PASSWORD); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected User createUserAndLogin(User user, String password) throws Exception { |
|
|
|
User savedUser = doPost("/api/user", user, User.class); |
|
|
|
logout(); |
|
|
|
doGet("/api/noauth/activate?activateToken={activateToken}", TestMailService.currentActivateToken) |
|
|
|
.andExpect(status().isSeeOther()) |
|
|
|
.andExpect(header().string(HttpHeaders.LOCATION, "/login/createPassword?activateToken=" + TestMailService.currentActivateToken)); |
|
|
|
.andExpect(status().isSeeOther()) |
|
|
|
.andExpect(header().string(HttpHeaders.LOCATION, "/login/createPassword?activateToken=" + TestMailService.currentActivateToken)); |
|
|
|
JsonNode tokenInfo = readResponse(doPost("/api/noauth/activate", "activateToken", TestMailService.currentActivateToken, "password", password).andExpect(status().isOk()), JsonNode.class); |
|
|
|
validateAndSetJwtToken(tokenInfo, user.getEmail()); |
|
|
|
return savedUser; |
|
|
|
@ -247,14 +247,14 @@ public abstract class AbstractControllerTest { |
|
|
|
Assert.assertNotNull(token); |
|
|
|
Assert.assertFalse(token.isEmpty()); |
|
|
|
int i = token.lastIndexOf('.'); |
|
|
|
Assert.assertTrue(i>0); |
|
|
|
String withoutSignature = token.substring(0, i+1); |
|
|
|
Jwt<Header,Claims> jwsClaims = Jwts.parser().parseClaimsJwt(withoutSignature); |
|
|
|
Assert.assertTrue(i > 0); |
|
|
|
String withoutSignature = token.substring(0, i + 1); |
|
|
|
Jwt<Header, Claims> jwsClaims = Jwts.parser().parseClaimsJwt(withoutSignature); |
|
|
|
Claims claims = jwsClaims.getBody(); |
|
|
|
String subject = claims.getSubject(); |
|
|
|
Assert.assertEquals(username, subject); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected void logout() throws Exception { |
|
|
|
this.token = null; |
|
|
|
this.refreshToken = null; |
|
|
|
@ -266,24 +266,24 @@ public abstract class AbstractControllerTest { |
|
|
|
request.header(ThingsboardSecurityConfiguration.JWT_TOKEN_HEADER_PARAM, "Bearer " + this.token); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected ResultActions doGet(String urlTemplate, Object... urlVariables) throws Exception { |
|
|
|
MockHttpServletRequestBuilder getRequest = get(urlTemplate, urlVariables); |
|
|
|
setJwtToken(getRequest); |
|
|
|
return mockMvc.perform(getRequest); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected <T> T doGet(String urlTemplate, Class<T> responseClass, Object... urlVariables) throws Exception { |
|
|
|
return readResponse(doGet(urlTemplate, urlVariables).andExpect(status().isOk()), responseClass); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected <T> T doGetTyped(String urlTemplate, TypeReference<T> responseType, Object... urlVariables) throws Exception { |
|
|
|
return readResponse(doGet(urlTemplate, urlVariables).andExpect(status().isOk()), responseType); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected <T> T doGetTypedWithPageLink(String urlTemplate, TypeReference<T> responseType, |
|
|
|
TextPageLink pageLink, |
|
|
|
Object... urlVariables) throws Exception { |
|
|
|
TextPageLink pageLink, |
|
|
|
Object... urlVariables) throws Exception { |
|
|
|
List<Object> pageLinkVariables = new ArrayList<>(); |
|
|
|
urlTemplate += "limit={limit}"; |
|
|
|
pageLinkVariables.add(pageLink.getLimit()); |
|
|
|
@ -299,18 +299,18 @@ public abstract class AbstractControllerTest { |
|
|
|
urlTemplate += "&textOffset={textOffset}"; |
|
|
|
pageLinkVariables.add(pageLink.getTextOffset()); |
|
|
|
} |
|
|
|
|
|
|
|
Object[] vars = new Object[urlVariables.length + pageLinkVariables.size()]; |
|
|
|
|
|
|
|
Object[] vars = new Object[urlVariables.length + pageLinkVariables.size()]; |
|
|
|
System.arraycopy(urlVariables, 0, vars, 0, urlVariables.length); |
|
|
|
System.arraycopy(pageLinkVariables.toArray(), 0, vars, urlVariables.length, pageLinkVariables.size()); |
|
|
|
|
|
|
|
|
|
|
|
return readResponse(doGet(urlTemplate, vars).andExpect(status().isOk()), responseType); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected <T> T doPost(String urlTemplate, Class<T> responseClass, String... params) throws Exception { |
|
|
|
return readResponse(doPost(urlTemplate, params).andExpect(status().isOk()), responseClass); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected <T> T doPost(String urlTemplate, T content, Class<T> responseClass, String... params) throws Exception { |
|
|
|
return readResponse(doPost(urlTemplate, content, params).andExpect(status().isOk()), responseClass); |
|
|
|
} |
|
|
|
@ -318,15 +318,15 @@ public abstract class AbstractControllerTest { |
|
|
|
protected <T> T doDelete(String urlTemplate, Class<T> responseClass, String... params) throws Exception { |
|
|
|
return readResponse(doDelete(urlTemplate, params).andExpect(status().isOk()), responseClass); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected ResultActions doPost(String urlTemplate, String... params) throws Exception { |
|
|
|
MockHttpServletRequestBuilder postRequest = post(urlTemplate); |
|
|
|
setJwtToken(postRequest); |
|
|
|
populateParams(postRequest, params); |
|
|
|
return mockMvc.perform(postRequest); |
|
|
|
} |
|
|
|
|
|
|
|
protected <T> ResultActions doPost(String urlTemplate, T content, String... params) throws Exception { |
|
|
|
|
|
|
|
protected <T> ResultActions doPost(String urlTemplate, T content, String... params) throws Exception { |
|
|
|
MockHttpServletRequestBuilder postRequest = post(urlTemplate); |
|
|
|
setJwtToken(postRequest); |
|
|
|
String json = json(content); |
|
|
|
@ -334,25 +334,25 @@ public abstract class AbstractControllerTest { |
|
|
|
populateParams(postRequest, params); |
|
|
|
return mockMvc.perform(postRequest); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected ResultActions doDelete(String urlTemplate, String... params) throws Exception { |
|
|
|
MockHttpServletRequestBuilder deleteRequest = delete(urlTemplate); |
|
|
|
setJwtToken(deleteRequest); |
|
|
|
populateParams(deleteRequest, params); |
|
|
|
return mockMvc.perform(deleteRequest); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected void populateParams(MockHttpServletRequestBuilder request, String... params) { |
|
|
|
if (params != null && params.length > 0) { |
|
|
|
Assert.assertEquals(params.length % 2, 0); |
|
|
|
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<String, String>(); |
|
|
|
for (int i=0;i<params.length;i+=2) { |
|
|
|
paramsMap.add(params[i], params[i+1]); |
|
|
|
for (int i = 0; i < params.length; i += 2) { |
|
|
|
paramsMap.add(params[i], params[i + 1]); |
|
|
|
} |
|
|
|
request.params(paramsMap); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
protected String json(Object o) throws IOException { |
|
|
|
MockHttpOutputMessage mockHttpOutputMessage = new MockHttpOutputMessage(); |
|
|
|
@ -360,14 +360,14 @@ public abstract class AbstractControllerTest { |
|
|
|
o, MediaType.APPLICATION_JSON, mockHttpOutputMessage); |
|
|
|
return mockHttpOutputMessage.getBodyAsString(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
protected <T> T readResponse(ResultActions result, Class<T> responseClass) throws Exception { |
|
|
|
byte[] content = result.andReturn().getResponse().getContentAsByteArray(); |
|
|
|
MockHttpInputMessage mockHttpInputMessage = new MockHttpInputMessage(content); |
|
|
|
return (T) this.mappingJackson2HttpMessageConverter.read(responseClass, mockHttpInputMessage); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected <T> T readResponse(ResultActions result, TypeReference<T> type) throws Exception { |
|
|
|
byte[] content = result.andReturn().getResponse().getContentAsByteArray(); |
|
|
|
ObjectMapper mapper = new ObjectMapper(); |
|
|
|
|