Skip to content

Commit

Permalink
CODENVY-1506 Rework workspace manager methods to optionally return ru…
Browse files Browse the repository at this point in the history
…ntime info (eclipse-che#3714)
  • Loading branch information
Michail Kuznetsov authored Jan 16, 2017
1 parent c865230 commit 44c296f
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,24 @@ public WorkspaceImpl getWorkspace(String name, String namespace) throws NotFound
return normalizeState(workspaceDao.get(name, namespace));
}

/**
* Gets list of workspaces which user can read. Runtimes are included
*
* @deprecated use #getWorkspaces(String user, boolean includeRuntimes) instead
*
* @param user
* the id of the user
* @return the list of workspaces or empty list if user can't read any workspace
* @throws NullPointerException
* when {@code user} is null
* @throws ServerException
* when any server error occurs while getting workspaces with {@link WorkspaceDao#getWorkspaces(String)}
*/
@Deprecated
public List<WorkspaceImpl> getWorkspaces(String user) throws ServerException {
return getWorkspaces(user, true);
}

/**
* Gets list of workspaces which user can read
*
Expand All @@ -226,21 +244,42 @@ public WorkspaceImpl getWorkspace(String name, String namespace) throws NotFound
*
* @param user
* the id of the user
* @param includeRuntimes
* if <code>true</code>, will fetch runtime info for workspaces.
* If <code>false</code>, will not fetch runtime info.
* @return the list of workspaces or empty list if user can't read any workspace
* @throws NullPointerException
* when {@code user} is null
* @throws ServerException
* when any server error occurs while getting workspaces with {@link WorkspaceDao#getWorkspaces(String)}
*/
public List<WorkspaceImpl> getWorkspaces(String user) throws ServerException {
public List<WorkspaceImpl> getWorkspaces(String user, boolean includeRuntimes) throws ServerException {
requireNonNull(user, "Required non-null user id");
final List<WorkspaceImpl> workspaces = workspaceDao.getWorkspaces(user);
for (WorkspaceImpl workspace : workspaces) {
workspace.setStatus(runtimes.getStatus(workspace.getId()));
normalizeState(workspace, includeRuntimes);
}
return workspaces;
}

/**
* Gets list of workspaces which has given namespace. Runtimes are included
*
* @deprecated use #getByNamespace(String user, boolean includeRuntimes) instead
*
* @param namespace
* the namespace to find workspaces
* @return the list of workspaces or empty list if no matches
* @throws NullPointerException
* when {@code namespace} is null
* @throws ServerException
* when any server error occurs while getting workspaces with {@link WorkspaceDao#getByNamespace(String)}
*/
@Deprecated
public List<WorkspaceImpl> getByNamespace(String namespace) throws ServerException {
return getByNamespace(namespace, true);
}

/**
* Gets list of workspaces which has given namespace
*
Expand All @@ -249,17 +288,20 @@ public List<WorkspaceImpl> getWorkspaces(String user) throws ServerException {
*
* @param namespace
* the namespace to find workspaces
* @param includeRuntimes
* if <code>true</code>, will fetch runtime info for workspaces.
* If <code>false</code>, will not fetch runtime info.
* @return the list of workspaces or empty list if no matches
* @throws NullPointerException
* when {@code namespace} is null
* @throws ServerException
* when any server error occurs while getting workspaces with {@link WorkspaceDao#getByNamespace(String)}
*/
public List<WorkspaceImpl> getByNamespace(String namespace) throws ServerException {
public List<WorkspaceImpl> getByNamespace(String namespace, boolean includeRuntimes) throws ServerException {
requireNonNull(namespace, "Required non-null namespace");
final List<WorkspaceImpl> workspaces = workspaceDao.getByNamespace(namespace);
for (WorkspaceImpl workspace : workspaces) {
normalizeState(workspace);
normalizeState(workspace, includeRuntimes);
}
return workspaces;
}
Expand Down Expand Up @@ -737,10 +779,19 @@ private String sessionUserNameOr(String nameIfNoUser) {
}

private WorkspaceImpl normalizeState(WorkspaceImpl workspace) throws ServerException {
try {
return normalizeState(workspace, runtimes.get(workspace.getId()));
} catch (NotFoundException e) {
return normalizeState(workspace, null);
return normalizeState(workspace, true);
}

private WorkspaceImpl normalizeState(WorkspaceImpl workspace, boolean includeRuntimes) throws ServerException {
if (includeRuntimes) {
try {
return normalizeState(workspace, runtimes.get(workspace.getId()));
} catch (NotFoundException e) {
return normalizeState(workspace, null);
}
} else {
workspace.setStatus(runtimes.getStatus(workspace.getId()));
return workspace;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public List<WorkspaceDto> getWorkspaces(@ApiParam("The number of the items to sk
@QueryParam("status")
String status) throws ServerException, BadRequestException {
//TODO add maxItems & skipCount to manager
return workspaceManager.getWorkspaces(EnvironmentContext.getCurrent().getSubject().getUserId())
return workspaceManager.getWorkspaces(EnvironmentContext.getCurrent().getSubject().getUserId(), false)
.stream()
.filter(ws -> status == null || status.equalsIgnoreCase(ws.getStatus().toString()))
.map(workspace -> linksInjector.injectLinks(asDto(workspace), getServiceContext()))
Expand All @@ -234,7 +234,7 @@ public List<WorkspaceDto> getByNamespace(@ApiParam("Workspace status")
@ApiParam("The namespace")
@PathParam("namespace")
String namespace) throws ServerException, BadRequestException {
return workspaceManager.getByNamespace(namespace)
return workspaceManager.getByNamespace(namespace, false)
.stream()
.filter(ws -> status == null || status.equalsIgnoreCase(ws.getStatus().toString()))
.map(workspace -> linksInjector.injectLinks(asDto(workspace), getServiceContext()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public void unsubscribe() {

@Override
public void onCascadeEvent(BeforeAccountRemovedEvent event) throws Exception {
for (WorkspaceImpl workspace : workspaceManager.getByNamespace(event.getAccount().getName())) {
for (WorkspaceImpl workspace : workspaceManager.getByNamespace(event.getAccount().getName(), false)) {
workspaceManager.removeWorkspace(workspace.getId());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

/**
Expand Down Expand Up @@ -205,7 +206,7 @@ public void shouldBeAbleToGetWorkspaceByKeyWithoutOwner() throws Exception {
}

@Test
public void shouldBeAbleToGetWorkspacesAvailableForUser() throws Exception {
public void shouldBeAbleToGetWorkspacesAvailableForUserWithRuntimes() throws Exception {
// given
final WorkspaceConfig config = createConfig();

Expand All @@ -214,38 +215,88 @@ public void shouldBeAbleToGetWorkspacesAvailableForUser() throws Exception {

when(workspaceDao.getWorkspaces(NAMESPACE)).thenReturn(asList(workspace1, workspace2));
final RuntimeDescriptor descriptor = createDescriptor(workspace2, RUNNING);
when(runtimes.get(workspace2.getId())).thenReturn(descriptor);
when(runtimes.get(workspace1.getId())).thenThrow(new NotFoundException("no runtime"));

// when
final List<WorkspaceImpl> result = workspaceManager.getWorkspaces(NAMESPACE, true);

// then
assertEquals(result.size(), 2);

final WorkspaceImpl res1 = result.get(0);
assertEquals(res1.getStatus(), STOPPED, "Workspace status wasn't changed from STARTING to STOPPED");
assertNull(res1.getRuntime(), "Workspace has unexpected runtime");
assertFalse(res1.isTemporary(), "Workspace must be permanent");

final WorkspaceImpl res2 = result.get(1);
assertEquals(res2.getStatus(), RUNNING, "Workspace status wasn't changed to the runtime instance status");
assertEquals(res2.getRuntime(), descriptor.getRuntime(), "Workspace doesn't have expected runtime");
assertFalse(res2.isTemporary(), "Workspace must be permanent");
}

@Test
public void shouldBeAbleToGetWorkspacesAvailableForUserWithoutRuntimes() throws Exception {
// given
final WorkspaceConfig config = createConfig();

final WorkspaceImpl workspace1 = createAndMockWorkspace(config, NAMESPACE);
final WorkspaceImpl workspace2 = createAndMockWorkspace(config, NAMESPACE_2);

when(workspaceDao.getWorkspaces(NAMESPACE)).thenReturn(asList(workspace1, workspace2));
when(runtimes.getStatus(workspace2.getId())).thenReturn(RUNNING);
when(runtimes.getStatus(workspace1.getId())).thenReturn(STOPPED);

// when
final List<WorkspaceImpl> result = workspaceManager.getWorkspaces(NAMESPACE);
final List<WorkspaceImpl> result = workspaceManager.getWorkspaces(NAMESPACE, false);

// then
assertEquals(result.size(), 2);

final WorkspaceImpl res1 = result.get(0);
assertEquals(res1.getStatus(), STOPPED, "Workspace status wasn't changed from STARTING to STOPPED");
assertNull(res1.getRuntime(), "Workspace has unexpected runtime");
assertFalse(res1.isTemporary(), "Workspace must be permanent");

final WorkspaceImpl res2 = result.get(1);
assertEquals(res2.getStatus(), RUNNING, "Workspace status wasn't changed to the runtime instance status");
assertNull(res1.getRuntime(), "Workspace has unexpected runtime");
assertFalse(res2.isTemporary(), "Workspace must be permanent");
}

@Test
public void shouldBeAbleToGetWorkspacesByNamespace() throws Exception {
public void shouldBeAbleToGetWorkspacesByNamespaceWithoutRuntimes() throws Exception {
// given
final WorkspaceImpl workspace = createAndMockWorkspace();
createAndMockDescriptor(workspace, RUNNING);

// when
final List<WorkspaceImpl> result = workspaceManager.getByNamespace(workspace.getNamespace());
final List<WorkspaceImpl> result = workspaceManager.getByNamespace(workspace.getNamespace(), false);

// then
assertEquals(result.size(), 1);

final WorkspaceImpl res1 = result.get(0);
assertEquals(res1.getStatus(), RUNNING, "Workspace status wasn't changed to the runtime instance status");
assertNull(res1.getRuntime(), "workspace has unexpected runtime");
assertFalse(res1.isTemporary(), "Workspace must be permanent");
}

@Test
public void shouldBeAbleToGetWorkspacesByNamespaceWithRuntimes() throws Exception {
// given
final WorkspaceImpl workspace = createAndMockWorkspace();
final RuntimeDescriptor descriptor = createAndMockDescriptor(workspace, RUNNING);

// when
final List<WorkspaceImpl> result = workspaceManager.getByNamespace(workspace.getNamespace(), true);

// then
assertEquals(result.size(), 1);

final WorkspaceImpl res1 = result.get(0);
assertEquals(res1.getStatus(), RUNNING, "Workspace status wasn't changed to the runtime instance status");
assertEquals(res1.getRuntime(), descriptor.getRuntime(), "Workspace doesn't have expected runtime");
assertFalse(res1.isTemporary(), "Workspace must be permanent");
}

Expand Down Expand Up @@ -856,6 +907,7 @@ private RuntimeDescriptor createAndMockDescriptor(WorkspaceImpl workspace, Works
throws ServerException, NotFoundException, ConflictException {
RuntimeDescriptor descriptor = createDescriptor(workspace, status);
when(runtimes.get(workspace.getId())).thenReturn(descriptor);
when(runtimes.getStatus(workspace.getId())).thenReturn(status);
return descriptor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public void shouldGetWorkspaceById() throws Exception {
public void shouldGetWorkspaces() throws Exception {
final WorkspaceImpl workspace1 = createWorkspace(createConfigDto());
final WorkspaceImpl workspace2 = createWorkspace(createConfigDto(), STARTING);
when(wsManager.getWorkspaces(USER_ID)).thenReturn(asList(workspace1, workspace2));
when(wsManager.getWorkspaces(USER_ID, false)).thenReturn(asList(workspace1, workspace2));

final Response response = given().auth()
.basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD)
Expand All @@ -329,7 +329,7 @@ public void shouldGetWorkspaces() throws Exception {
public void shouldGetWorkspacesByNamespace() throws Exception {
final WorkspaceImpl workspace1 = createWorkspace(createConfigDto());
final WorkspaceImpl workspace2 = createWorkspace(createConfigDto(), STARTING);
when(wsManager.getByNamespace(NAMESPACE)).thenReturn(asList(workspace1, workspace2));
when(wsManager.getByNamespace(NAMESPACE, false)).thenReturn(asList(workspace1, workspace2));

final Response response = given().auth()
.basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD)
Expand All @@ -347,7 +347,7 @@ public void shouldGetWorkspacesByNamespace() throws Exception {
public void shouldGetWorkspacesByStatus() throws Exception {
final WorkspaceImpl workspace1 = createWorkspace(createConfigDto());
final WorkspaceImpl workspace2 = createWorkspace(createConfigDto(), STARTING);
when(wsManager.getWorkspaces(USER_ID)).thenReturn(asList(workspace1, workspace2));
when(wsManager.getWorkspaces(USER_ID, false)).thenReturn(asList(workspace1, workspace2));

final Response response = given().auth()
.basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD)
Expand Down

0 comments on commit 44c296f

Please sign in to comment.