Skip to content

Commit

Permalink
[#2112] Add integration tests for search devices operation
Browse files Browse the repository at this point in the history
Signed-off-by: Kartheeswaran Kalidass <kartheeswaran.kalidass@bosch.io>
  • Loading branch information
kaniyan committed Sep 3, 2020
1 parent 8c4128c commit 3748f18
Show file tree
Hide file tree
Showing 4 changed files with 333 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tests/pom.xml
Expand Up @@ -43,6 +43,8 @@ Test cases are run against Docker images of Hono server + (Apache Qpid Dispatch
<!-- should be set to false if testing against a registry that doesn't support GW mode -->
<deviceregistry.supportsGatewayMode>true</deviceregistry.supportsGatewayMode>
<deviceregistry.credentials.supportsClientContext>true</deviceregistry.credentials.supportsClientContext>
<!-- should be set to true if testing against a registry that supports search devices operation -->
<deviceregistry.supportsSearchDevices>false</deviceregistry.supportsSearchDevices>
<hono.amqp-network.host>hono-dispatch-router.hono</hono.amqp-network.host>
<hono.device-connection.host>hono-service-device-connection.hono</hono.device-connection.host>
<hono.registration.host>hono-service-device-registry.hono</hono.registration.host>
Expand Down Expand Up @@ -202,6 +204,7 @@ Test cases are run against Docker images of Hono server + (Apache Qpid Dispatch
<hono.mongodb.database.name>hono-it-tests</hono.mongodb.database.name>
<hono.deviceregistry.resources.folder>deviceregistry-mongodb</hono.deviceregistry.resources.folder>
<deviceregistry.credentials.supportsClientContext>false</deviceregistry.credentials.supportsClientContext>
<deviceregistry.supportsSearchDevices>true</deviceregistry.supportsSearchDevices>
</properties>
</profile>
<profile>
Expand Down Expand Up @@ -1093,6 +1096,7 @@ Test cases are run against Docker images of Hono server + (Apache Qpid Dispatch
<deviceregistry.http.port>${deviceregistry.http.port}</deviceregistry.http.port>
<deviceregistry.supportsGatewayMode>${deviceregistry.supportsGatewayMode}</deviceregistry.supportsGatewayMode>
<deviceregistry.credentials.supportsClientContext>${deviceregistry.credentials.supportsClientContext}</deviceregistry.credentials.supportsClientContext>
<deviceregistry.supportsSearchDevices>${deviceregistry.supportsSearchDevices}</deviceregistry.supportsSearchDevices>
<coap.host>${coap.ip}</coap.host>
<coap.port>${coap.port}</coap.port>
<coaps.port>${coaps.port}</coaps.port>
Expand Down
48 changes: 47 additions & 1 deletion tests/src/test/java/org/eclipse/hono/tests/CrudHttpClient.java
Expand Up @@ -484,6 +484,28 @@ public Future<HttpResponse<Buffer>> get(final String uri, final ResponsePredicat
return get(createRequestOptions().setURI(uri), successPredicates);
}

/**
* Retrieves a resource representation using a HTTP GET request.
*
* @param uri The resource to retrieve.
* @param queryParams The query parameters for the request.
* @param successPredicates Checks on the HTTP response that need to pass for the request
* to be considered successful.
* @return A future indicating the outcome of the request. The future will be completed with the
* HTTP response if all checks on the response have succeeded.
* Otherwise the future will be failed with the error produced by the first failing
* predicate.
* @throws NullPointerException if URI is {@code null}.
*/
public Future<HttpResponse<Buffer>> get(
final String uri,
final MultiMap queryParams,
final ResponsePredicate ... successPredicates) {

Objects.requireNonNull(uri);
return get(createRequestOptions().setURI(uri), queryParams, successPredicates);
}

/**
* Retrieves a resource representation using an HTTP GET request.
*
Expand All @@ -494,9 +516,31 @@ public Future<HttpResponse<Buffer>> get(final String uri, final ResponsePredicat
* HTTP response if all checks on the response have succeeded.
* Otherwise the future will be failed with the error produced by the first failing
* predicate.
*/
public Future<HttpResponse<Buffer>> get(
final RequestOptions requestOptions,
final ResponsePredicate... successPredicates) {

return get(requestOptions, null, successPredicates);
}

/**
* Retrieves a resource representation using a HTTP GET request.
*
* @param requestOptions The options to use for the request.
* @param queryParams The query parameters for the request.
* @param successPredicates Checks on the HTTP response that need to pass for the request
* to be considered successful.
* @return A future indicating the outcome of the request. The future will be completed with the
* HTTP response if all checks on the response have succeeded.
* Otherwise the future will be failed with the error produced by the first failing
* predicate.
* @throws NullPointerException if options is {@code null}.
*/
public Future<HttpResponse<Buffer>> get(final RequestOptions requestOptions, final ResponsePredicate ... successPredicates) {
public Future<HttpResponse<Buffer>> get(
final RequestOptions requestOptions,
final MultiMap queryParams,
final ResponsePredicate... successPredicates) {

Objects.requireNonNull(requestOptions);

Expand All @@ -505,6 +549,8 @@ public Future<HttpResponse<Buffer>> get(final RequestOptions requestOptions, fin
context.runOnContext(go -> {
final HttpRequest<Buffer> req = client.request(HttpMethod.GET, requestOptions);
addResponsePredicates(req, successPredicates);
Optional.ofNullable(queryParams)
.ifPresent(params -> req.queryParams().addAll(queryParams));
req.send(result);
});

Expand Down
Expand Up @@ -87,6 +87,12 @@ public final class DeviceRegistryHttpClient {
public static final String TEMPLATE_URI_CREDENTIALS_INSTANCE = String.format("/%s/%s/%%s/%%s/%%s",
RegistryManagementConstants.API_VERSION, RegistryManagementConstants.CREDENTIALS_HTTP_ENDPOINT);

/**
* The URI pattern for searching devices.
*/
public static final String TEMPLATE_URI_SEARCH_DEVICES_INSTANCE = String.format("/%s/%s/%%s",
RegistryManagementConstants.API_VERSION, RegistryManagementConstants.DEVICES_HTTP_ENDPOINT);

private static final Logger LOG = LoggerFactory.getLogger(DeviceRegistryHttpClient.class);

private static final String CONTENT_TYPE_APPLICATION_JSON = "application/json";
Expand Down Expand Up @@ -156,6 +162,10 @@ private static String registrationWithoutIdUri(final String tenant) {
.orElse(""));
}

private static String searchDevicesUri(final String tenant) {
return String.format(TEMPLATE_URI_SEARCH_DEVICES_INSTANCE, tenant);
}

// tenant management

/**
Expand Down Expand Up @@ -642,6 +652,46 @@ public Future<HttpResponse<Buffer>> deregisterDevice(

}

/**
* Finds devices belonging to the given tenant with optional filters, paging and sorting options.
*
* @param tenantId The tenant that the device belongs to.
* @param pageSize The maximum number of results to include in a response.
* @param pageOffset The offset into the result set from which to include objects in the response.
* @param filters The filters are predicates that objects in the result set must match.
* @param sortOptions A list of sort options.
* @param expectedStatusCode The status code indicating a successful outcome.
* @return A future indicating the outcome of the operation. The future will contain the response if the
* response contained the expected status code. Otherwise the future will fail.
* @throws NullPointerException if any of the parameters is {@code null}.
*/
public Future<HttpResponse<Buffer>> searchDevices(
final String tenantId,
final Optional<Integer> pageSize,
final Optional<Integer> pageOffset,
final List<String> filters,
final List<String> sortOptions,
final int expectedStatusCode) {

Objects.requireNonNull(tenantId);
Objects.requireNonNull(pageSize);
Objects.requireNonNull(pageOffset);
Objects.requireNonNull(filters);
Objects.requireNonNull(sortOptions);

final String requestUri = searchDevicesUri(tenantId);
final MultiMap queryParams = MultiMap.caseInsensitiveMultiMap();

pageSize.ifPresent(
pSize -> queryParams.add(RegistryManagementConstants.PARAM_PAGE_SIZE, String.valueOf(pSize)));
pageOffset.ifPresent(
pOffset -> queryParams.add(RegistryManagementConstants.PARAM_PAGE_OFFSET, String.valueOf(pOffset)));
filters.forEach(filterJson -> queryParams.add(RegistryManagementConstants.PARAM_FILTER_JSON, filterJson));
sortOptions.forEach(sortJson -> queryParams.add(RegistryManagementConstants.PARAM_SORT_JSON, sortJson));

return httpClient.get(requestUri, queryParams, ResponsePredicate.status(expectedStatusCode));
}

// credentials management

/**
Expand Down

0 comments on commit 3748f18

Please sign in to comment.