use of io.pravega.controller.server.rest.generated.model.ScopesList in project pravega by pravega.
the class StreamMetaDataTests method testListScopes.
/**
* Test for listScopes REST API.
*
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void testListScopes() throws ExecutionException, InterruptedException {
final String resourceURI = getURI() + "v1/scopes";
// Test to list scopes.
List<String> scopesList = Arrays.asList("scope1", "scope2");
when(mockControllerService.listScopes()).thenReturn(CompletableFuture.completedFuture(scopesList));
Response response = addAuthHeaders(client.target(resourceURI).request()).buildGet().invoke();
assertEquals("List Scopes response code", 200, response.getStatus());
assertTrue(response.bufferEntity());
final ScopesList scopesList1 = response.readEntity(ScopesList.class);
assertEquals("List count", scopesList1.getScopes().size(), 2);
assertEquals("List element", scopesList1.getScopes().get(0).getScopeName(), "scope1");
assertEquals("List element", scopesList1.getScopes().get(1).getScopeName(), "scope2");
response.close();
// Test for list scopes failure.
final CompletableFuture<List<String>> completableFuture = new CompletableFuture<>();
completableFuture.completeExceptionally(new Exception());
when(mockControllerService.listScopes()).thenReturn(completableFuture);
response = addAuthHeaders(client.target(resourceURI).request()).buildGet().invoke();
assertEquals("List Scopes response code", 500, response.getStatus());
response.close();
}
use of io.pravega.controller.server.rest.generated.model.ScopesList in project pravega by pravega.
the class StreamMetadataResourceImpl method listScopes.
/**
* Implementation of listScopes REST API.
*
* @param securityContext The security for API access.
* @param asyncResponse AsyncResponse provides means for asynchronous server side response processing.
*/
@Override
public void listScopes(final SecurityContext securityContext, final AsyncResponse asyncResponse) {
long traceId = LoggerHelpers.traceEnter(log, "listScopes");
long requestId = requestIdGenerator.nextLong();
final Principal principal;
final List<String> authHeader = getAuthorizationHeader();
try {
principal = restAuthHelper.authenticate(authHeader);
restAuthHelper.authorize(authHeader, authorizationResource.ofScopes(), principal, READ);
} catch (AuthException e) {
log.warn(requestId, "Get scopes failed due to authentication failure.", e);
asyncResponse.resume(Response.status(Status.fromStatusCode(e.getResponseCode())).build());
LoggerHelpers.traceLeave(log, "listScopes", traceId);
return;
}
controllerService.listScopes(requestId).thenApply(scopesList -> {
ScopesList scopes = new ScopesList();
scopesList.forEach(scope -> {
try {
if (restAuthHelper.isAuthorized(authHeader, authorizationResource.ofScope(scope), principal, READ)) {
scopes.addScopesItem(new ScopeProperty().scopeName(scope));
}
} catch (AuthException e) {
log.warn(requestId, e.getMessage(), e);
// Ignore. This exception occurs under abnormal circumstances and not to determine
// whether the user is authorized. In case it does occur, we assume that the user
// is unauthorized.
}
});
return Response.status(Status.OK).entity(scopes).build();
}).exceptionally(exception -> {
log.warn(requestId, "listScopes failed with exception: ", exception);
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}).thenApply(response -> {
asyncResponse.resume(response);
LoggerHelpers.traceLeave(log, "listScopes", traceId);
return response;
});
}
use of io.pravega.controller.server.rest.generated.model.ScopesList in project pravega by pravega.
the class StreamMetaDataAuthFocusedTests method listScopes.
private ScopesList listScopes(List<String> scopeNames, String username, String password) {
final String resourceURI = getURI() + "v1/scopes";
when(mockControllerService.listScopes(anyLong())).thenReturn(CompletableFuture.completedFuture(scopeNames));
Invocation requestInvocation = this.invocationBuilder(resourceURI, username, password).buildGet();
Response response = requestInvocation.invoke();
ScopesList scopes = response.readEntity(ScopesList.class);
response.close();
return scopes;
}
use of io.pravega.controller.server.rest.generated.model.ScopesList in project pravega by pravega.
the class StreamMetaDataAuthFocusedTests method testListScopesReturnsAllScopesForUserWithPermissionOnRootAndChildren.
// endregion
// region Scope listing tests
@Test
public void testListScopesReturnsAllScopesForUserWithPermissionOnRootAndChildren() {
// Arrange
final String resourceURI = getURI() + "v1/scopes";
when(mockControllerService.listScopes(anyLong())).thenReturn(CompletableFuture.completedFuture(Arrays.asList("scopea", "scopeb", "scopec")));
Invocation requestInvocation = this.invocationBuilder(resourceURI, USER_SCOPE_LISTER, DEFAULT_PASSWORD).buildGet();
// Act
Response response = requestInvocation.invoke();
ScopesList scopes = response.readEntity(ScopesList.class);
// Assert
assertEquals(3, scopes.getScopes().size());
response.close();
}
use of io.pravega.controller.server.rest.generated.model.ScopesList in project pravega by pravega.
the class StreamMetaDataTests method testlistScopes.
/**
* Test for listScopes REST API.
*
* @throws ExecutionException
* @throws InterruptedException
*/
@Test(timeout = 30000)
public void testlistScopes() throws ExecutionException, InterruptedException {
final String resourceURI = getURI() + "v1/scopes";
// Test to list scopes.
List<String> scopesList = Arrays.asList("scope1", "scope2", "scope3");
when(mockControllerService.listScopes(anyLong())).thenReturn(CompletableFuture.completedFuture(scopesList));
Response response = addAuthHeaders(client.target(resourceURI).request()).buildGet().invoke();
assertEquals("List Scopes response code", 200, response.getStatus());
assertTrue(response.bufferEntity());
verifyScopes(response.readEntity(ScopesList.class));
response.close();
// Test for list scopes failure.
final CompletableFuture<List<String>> completableFuture = new CompletableFuture<>();
completableFuture.completeExceptionally(new Exception());
when(mockControllerService.listScopes(anyLong())).thenReturn(completableFuture);
response = addAuthHeaders(client.target(resourceURI).request()).buildGet().invoke();
assertEquals("List Scopes response code", 500, response.getStatus());
response.close();
}
Aggregations