|
|
@ -16,13 +16,21 @@ |
|
|
package org.thingsboard.server.msa; |
|
|
package org.thingsboard.server.msa; |
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
|
|
import io.restassured.RestAssured; |
|
|
|
|
|
import io.restassured.builder.ResponseSpecBuilder; |
|
|
import io.restassured.common.mapper.TypeRef; |
|
|
import io.restassured.common.mapper.TypeRef; |
|
|
|
|
|
import io.restassured.config.HeaderConfig; |
|
|
|
|
|
import io.restassured.config.HttpClientConfig; |
|
|
|
|
|
import io.restassured.config.RestAssuredConfig; |
|
|
|
|
|
import io.restassured.filter.log.RequestLoggingFilter; |
|
|
|
|
|
import io.restassured.filter.log.ResponseLoggingFilter; |
|
|
import io.restassured.http.ContentType; |
|
|
import io.restassured.http.ContentType; |
|
|
import io.restassured.path.json.JsonPath; |
|
|
import io.restassured.path.json.JsonPath; |
|
|
import io.restassured.response.ValidatableResponse; |
|
|
import io.restassured.response.ValidatableResponse; |
|
|
import io.restassured.specification.RequestSpecification; |
|
|
import io.restassured.specification.RequestSpecification; |
|
|
|
|
|
import io.restassured.specification.ResponseSpecification; |
|
|
|
|
|
import org.hamcrest.Matchers; |
|
|
import org.springframework.http.HttpStatus; |
|
|
import org.springframework.http.HttpStatus; |
|
|
import org.thingsboard.rest.client.RestClient; |
|
|
|
|
|
import org.thingsboard.server.common.data.Device; |
|
|
import org.thingsboard.server.common.data.Device; |
|
|
import org.thingsboard.server.common.data.id.DeviceId; |
|
|
import org.thingsboard.server.common.data.id.DeviceId; |
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
@ -40,24 +48,33 @@ import java.util.List; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
import static io.restassured.RestAssured.given; |
|
|
import static io.restassured.RestAssured.given; |
|
|
|
|
|
import static org.apache.http.params.CoreConnectionPNames.CONNECTION_TIMEOUT; |
|
|
|
|
|
import static org.apache.http.params.CoreConnectionPNames.SO_TIMEOUT; |
|
|
import static org.hamcrest.Matchers.is; |
|
|
import static org.hamcrest.Matchers.is; |
|
|
import static org.hamcrest.core.AnyOf.anyOf; |
|
|
import static org.hamcrest.core.AnyOf.anyOf; |
|
|
import static org.thingsboard.server.common.data.StringUtils.isEmpty; |
|
|
import static org.thingsboard.server.common.data.StringUtils.isEmpty; |
|
|
import static org.thingsboard.server.msa.AbstractContainerTest.getRequestFactoryForSelfSignedCert; |
|
|
|
|
|
|
|
|
|
|
|
public class TestRestClient { |
|
|
public class TestRestClient { |
|
|
private static final String JWT_TOKEN_HEADER_PARAM = "X-Authorization"; |
|
|
private static final String JWT_TOKEN_HEADER_PARAM = "X-Authorization"; |
|
|
private final String baseURL; |
|
|
private final String baseURL; |
|
|
private String token; |
|
|
private String token; |
|
|
private String refreshToken; |
|
|
private String refreshToken; |
|
|
private RequestSpecification spec; |
|
|
private RequestSpecification requestSpec; |
|
|
|
|
|
private ResponseSpecification responseSpec; |
|
|
protected static final String ACTIVATE_TOKEN_REGEX = "/api/noauth/activate?activateToken="; |
|
|
protected static final String ACTIVATE_TOKEN_REGEX = "/api/noauth/activate?activateToken="; |
|
|
|
|
|
|
|
|
public TestRestClient(String url) { |
|
|
public TestRestClient(String url) { |
|
|
|
|
|
RestAssured.filters(new RequestLoggingFilter(), new ResponseLoggingFilter()); |
|
|
|
|
|
|
|
|
baseURL = url; |
|
|
baseURL = url; |
|
|
spec = given().baseUri(baseURL).contentType(ContentType.JSON); |
|
|
requestSpec = given().baseUri(baseURL) |
|
|
|
|
|
.contentType(ContentType.JSON) |
|
|
|
|
|
.config(RestAssuredConfig.config() |
|
|
|
|
|
.headerConfig(HeaderConfig.headerConfig() |
|
|
|
|
|
.overwriteHeadersWithName(JWT_TOKEN_HEADER_PARAM))); |
|
|
|
|
|
|
|
|
if (url.matches("^(https)://.*$")) { |
|
|
if (url.matches("^(https)://.*$")) { |
|
|
spec.relaxedHTTPSValidation(); |
|
|
requestSpec.relaxedHTTPSValidation(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -70,12 +87,11 @@ public class TestRestClient { |
|
|
.getBody().jsonPath(); |
|
|
.getBody().jsonPath(); |
|
|
token = jsonPath.get("token"); |
|
|
token = jsonPath.get("token"); |
|
|
refreshToken = jsonPath.get("refreshToken"); |
|
|
refreshToken = jsonPath.get("refreshToken"); |
|
|
spec.header(JWT_TOKEN_HEADER_PARAM, "Bearer " + token) |
|
|
requestSpec.header(JWT_TOKEN_HEADER_PARAM, "Bearer " + token); |
|
|
.contentType(ContentType.JSON); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public Device postDevice(String accessToken, Device device) { |
|
|
public Device postDevice(String accessToken, Device device) { |
|
|
return given().spec(spec).body(device) |
|
|
return given().spec(requestSpec).body(device) |
|
|
.pathParams("accessToken", accessToken) |
|
|
.pathParams("accessToken", accessToken) |
|
|
.post("/api/device?accessToken={accessToken}") |
|
|
.post("/api/device?accessToken={accessToken}") |
|
|
.then() |
|
|
.then() |
|
|
@ -85,7 +101,7 @@ public class TestRestClient { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public ValidatableResponse getDeviceById(DeviceId deviceId, int statusCode) { |
|
|
public ValidatableResponse getDeviceById(DeviceId deviceId, int statusCode) { |
|
|
return given().spec(spec) |
|
|
return given().spec(requestSpec) |
|
|
.pathParams("deviceId", deviceId.getId()) |
|
|
.pathParams("deviceId", deviceId.getId()) |
|
|
.get("/api/device/{deviceId}") |
|
|
.get("/api/device/{deviceId}") |
|
|
.then() |
|
|
.then() |
|
|
@ -97,7 +113,7 @@ public class TestRestClient { |
|
|
.as(Device.class); |
|
|
.as(Device.class); |
|
|
} |
|
|
} |
|
|
public DeviceCredentials getDeviceCredentialsByDeviceId(DeviceId deviceId) { |
|
|
public DeviceCredentials getDeviceCredentialsByDeviceId(DeviceId deviceId) { |
|
|
return given().spec(spec).get("/api/device/{deviceId}/credentials", deviceId.getId()) |
|
|
return given().spec(requestSpec).get("/api/device/{deviceId}/credentials", deviceId.getId()) |
|
|
.then() |
|
|
.then() |
|
|
.assertThat() |
|
|
.assertThat() |
|
|
.statusCode(HttpStatus.OK.value()) |
|
|
.statusCode(HttpStatus.OK.value()) |
|
|
@ -106,41 +122,41 @@ public class TestRestClient { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public ValidatableResponse postTelemetry(String credentialsId, JsonNode telemetry) { |
|
|
public ValidatableResponse postTelemetry(String credentialsId, JsonNode telemetry) { |
|
|
return given().spec(spec).body(telemetry) |
|
|
return given().spec(requestSpec).body(telemetry) |
|
|
.post("/api/v1/{credentialsId}/telemetry", credentialsId) |
|
|
.post("/api/v1/{credentialsId}/telemetry", credentialsId) |
|
|
.then() |
|
|
.then() |
|
|
.statusCode(HttpStatus.OK.value()); |
|
|
.statusCode(HttpStatus.OK.value()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public ValidatableResponse deleteDevice(DeviceId deviceId) { |
|
|
public ValidatableResponse deleteDevice(DeviceId deviceId) { |
|
|
return given().spec(spec) |
|
|
return given().spec(requestSpec) |
|
|
.delete("/api/device/{deviceId}", deviceId.getId()) |
|
|
.delete("/api/device/{deviceId}", deviceId.getId()) |
|
|
.then() |
|
|
.then() |
|
|
.statusCode(HttpStatus.OK.value()); |
|
|
.statusCode(HttpStatus.OK.value()); |
|
|
} |
|
|
} |
|
|
public ValidatableResponse deleteDeviceIfExists(DeviceId deviceId) { |
|
|
public ValidatableResponse deleteDeviceIfExists(DeviceId deviceId) { |
|
|
return given().spec(spec) |
|
|
return given().spec(requestSpec) |
|
|
.delete("/api/device/{deviceId}", deviceId.getId()) |
|
|
.delete("/api/device/{deviceId}", deviceId.getId()) |
|
|
.then() |
|
|
.then() |
|
|
.statusCode(anyOf(is(HttpStatus.OK.value()),is(HttpStatus.NOT_FOUND.value()))); |
|
|
.statusCode(anyOf(is(HttpStatus.OK.value()),is(HttpStatus.NOT_FOUND.value()))); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public ValidatableResponse postTelemetryAttribute(String entityType, DeviceId deviceId, String scope, JsonNode attribute) { |
|
|
public ValidatableResponse postTelemetryAttribute(String entityType, DeviceId deviceId, String scope, JsonNode attribute) { |
|
|
return given().spec(spec).body(attribute) |
|
|
return given().spec(requestSpec).body(attribute) |
|
|
.post("/api/plugins/telemetry/{entityType}/{entityId}/attributes/{scope}", entityType, deviceId.getId(), scope) |
|
|
.post("/api/plugins/telemetry/{entityType}/{entityId}/attributes/{scope}", entityType, deviceId.getId(), scope) |
|
|
.then() |
|
|
.then() |
|
|
.statusCode(HttpStatus.OK.value()); |
|
|
.statusCode(HttpStatus.OK.value()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public ValidatableResponse postAttribute(String accessToken, JsonNode attribute) { |
|
|
public ValidatableResponse postAttribute(String accessToken, JsonNode attribute) { |
|
|
return given().spec(spec).body(attribute) |
|
|
return given().spec(requestSpec).body(attribute) |
|
|
.post("/api/v1/{accessToken}/attributes/", accessToken) |
|
|
.post("/api/v1/{accessToken}/attributes/", accessToken) |
|
|
.then() |
|
|
.then() |
|
|
.statusCode(HttpStatus.OK.value()); |
|
|
.statusCode(HttpStatus.OK.value()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public JsonNode getAttributes(String accessToken, String clientKeys, String sharedKeys) { |
|
|
public JsonNode getAttributes(String accessToken, String clientKeys, String sharedKeys) { |
|
|
return given().spec(spec) |
|
|
return given().spec(requestSpec) |
|
|
.queryParam("clientKeys", clientKeys) |
|
|
.queryParam("clientKeys", clientKeys) |
|
|
.queryParam("sharedKeys", sharedKeys) |
|
|
.queryParam("sharedKeys", sharedKeys) |
|
|
.get("/api/v1/{accessToken}/attributes", accessToken) |
|
|
.get("/api/v1/{accessToken}/attributes", accessToken) |
|
|
@ -153,7 +169,7 @@ public class TestRestClient { |
|
|
public PageData<RuleChain> getRuleChains(PageLink pageLink) { |
|
|
public PageData<RuleChain> getRuleChains(PageLink pageLink) { |
|
|
Map<String, String> params = new HashMap<>(); |
|
|
Map<String, String> params = new HashMap<>(); |
|
|
addPageLinkToParam(params, pageLink); |
|
|
addPageLinkToParam(params, pageLink); |
|
|
return given().spec(spec).queryParams(params) |
|
|
return given().spec(requestSpec).queryParams(params) |
|
|
.get("/api/ruleChains") |
|
|
.get("/api/ruleChains") |
|
|
.then() |
|
|
.then() |
|
|
.statusCode(HttpStatus.OK.value()) |
|
|
.statusCode(HttpStatus.OK.value()) |
|
|
@ -162,7 +178,7 @@ public class TestRestClient { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public RuleChain postRootRuleChain(RuleChain ruleChain) { |
|
|
public RuleChain postRootRuleChain(RuleChain ruleChain) { |
|
|
return given().spec(spec) |
|
|
return given().spec(requestSpec) |
|
|
.body(ruleChain) |
|
|
.body(ruleChain) |
|
|
.post("/api/ruleChain") |
|
|
.post("/api/ruleChain") |
|
|
.then() |
|
|
.then() |
|
|
@ -172,7 +188,7 @@ public class TestRestClient { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public RuleChainMetaData postRuleChainMetadata(RuleChainMetaData ruleChainMetaData) { |
|
|
public RuleChainMetaData postRuleChainMetadata(RuleChainMetaData ruleChainMetaData) { |
|
|
return given().spec(spec) |
|
|
return given().spec(requestSpec) |
|
|
.body(ruleChainMetaData) |
|
|
.body(ruleChainMetaData) |
|
|
.post("/api/ruleChain/metadata") |
|
|
.post("/api/ruleChain/metadata") |
|
|
.then() |
|
|
.then() |
|
|
@ -182,14 +198,14 @@ public class TestRestClient { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void setRootRuleChain(RuleChainId ruleChainId) { |
|
|
public void setRootRuleChain(RuleChainId ruleChainId) { |
|
|
given().spec(spec) |
|
|
given().spec(requestSpec) |
|
|
.post("/api/ruleChain/{ruleChainId}/root", ruleChainId.getId()) |
|
|
.post("/api/ruleChain/{ruleChainId}/root", ruleChainId.getId()) |
|
|
.then() |
|
|
.then() |
|
|
.statusCode(HttpStatus.OK.value()); |
|
|
.statusCode(HttpStatus.OK.value()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void deleteRuleChain(RuleChainId ruleChainId) { |
|
|
public void deleteRuleChain(RuleChainId ruleChainId) { |
|
|
given().spec(spec) |
|
|
given().spec(requestSpec) |
|
|
.delete("/api/ruleChain/{ruleChainId}", ruleChainId.getId()) |
|
|
.delete("/api/ruleChain/{ruleChainId}", ruleChainId.getId()) |
|
|
.then() |
|
|
.then() |
|
|
.statusCode(HttpStatus.OK.value()); |
|
|
.statusCode(HttpStatus.OK.value()); |
|
|
@ -224,7 +240,7 @@ public class TestRestClient { |
|
|
params.put("fromType", fromId.getEntityType().name()); |
|
|
params.put("fromType", fromId.getEntityType().name()); |
|
|
params.put("relationTypeGroup", relationTypeGroup.name()); |
|
|
params.put("relationTypeGroup", relationTypeGroup.name()); |
|
|
|
|
|
|
|
|
return given().spec(spec) |
|
|
return given().spec(requestSpec) |
|
|
.pathParams(params) |
|
|
.pathParams(params) |
|
|
.get("/api/relations?fromId={fromId}&fromType={fromType}&relationTypeGroup={relationTypeGroup}") |
|
|
.get("/api/relations?fromId={fromId}&fromType={fromType}&relationTypeGroup={relationTypeGroup}") |
|
|
.then() |
|
|
.then() |
|
|
@ -234,7 +250,7 @@ public class TestRestClient { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public JsonNode postServerSideRpc(DeviceId deviceId, JsonNode serverRpcPayload) { |
|
|
public JsonNode postServerSideRpc(DeviceId deviceId, JsonNode serverRpcPayload) { |
|
|
return given().spec(spec) |
|
|
return given().spec(requestSpec) |
|
|
.body(serverRpcPayload) |
|
|
.body(serverRpcPayload) |
|
|
.post("/api/rpc/twoway/{deviceId}", deviceId.getId()) |
|
|
.post("/api/rpc/twoway/{deviceId}", deviceId.getId()) |
|
|
.then() |
|
|
.then() |
|
|
|