Skip to content

Commit

Permalink
[risk=low][RW-12566] Add a Get Featured Workspaces (from DB) endpoint (
Browse files Browse the repository at this point in the history
…#8650)

* rm unused WorkspaceServiceFakeImpl
  • Loading branch information
jmthibault79 authored Jul 15, 2024
1 parent 6cfc19b commit debc763
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.pmiops.workbench.api;

import jakarta.inject.Provider;
import org.pmiops.workbench.config.WorkbenchConfig;
import org.pmiops.workbench.exceptions.NotImplementedException;
import org.pmiops.workbench.model.WorkspaceResponseListResponse;
import org.pmiops.workbench.workspaces.WorkspaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeaturedWorkspaceController implements FeaturedWorkspaceApiDelegate {

private final Provider<WorkbenchConfig> workbenchConfigProvider;
private final WorkspaceService workspaceService;

@Autowired
FeaturedWorkspaceController(
Provider<WorkbenchConfig> workbenchConfigProvider, WorkspaceService workspaceService) {
this.workbenchConfigProvider = workbenchConfigProvider;
this.workspaceService = workspaceService;
}

/**
* Get all Featured workspaces, using the Featured Workspace DB table as the source.
*
* @return List of all Featured workspaces
*/
@Override
public ResponseEntity<WorkspaceResponseListResponse> getFeaturedWorkspaces() {
if (workbenchConfigProvider.get().featureFlags.enablePublishedWorkspacesViaDb) {
return ResponseEntity.ok(
new WorkspaceResponseListResponse().items(workspaceService.getFeaturedWorkspaces()));
} else {
throw new NotImplementedException(
"Not implemented in this environment: combine the results of getFeaturedWorkspacesConfig() and getPublishedWorkspaces() to generate the list of featured workspaces.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,34 @@
import jakarta.inject.Provider;
import org.pmiops.workbench.config.FeaturedWorkspacesConfig;
import org.pmiops.workbench.model.FeaturedWorkspacesConfigResponse;
import org.pmiops.workbench.workspaces.WorkspaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

@Deprecated(since = "July 2024", forRemoval = true)
@RestController
public class FeaturedWorkspacesController implements FeaturedWorkspacesConfigApiDelegate {
private final Provider<FeaturedWorkspacesConfig> configProvider;
private final Provider<FeaturedWorkspacesConfig> featuredWorkspacesConfigProvider;

@Autowired
FeaturedWorkspacesController(Provider<FeaturedWorkspacesConfig> configProvider) {
this.configProvider = configProvider;
FeaturedWorkspacesController(
Provider<FeaturedWorkspacesConfig> featuredWorkspacesConfigProvider) {
this.featuredWorkspacesConfigProvider = featuredWorkspacesConfigProvider;
}

/**
* @deprecated Get all Featured workspaces, using the Featured Workspaces Config JSON as the
* source. Use this in conjunction with {@link WorkspaceService#getPublishedWorkspaces()} to
* construct the list of (pre-July-2024) Featured workspaces.
* <p>Use {@link FeaturedWorkspaceController#getFeaturedWorkspaces()} to retrieve the
* Featured/Published workspaces from July 2024 onward.
* @return List of all Featured workspaces
*/
@Deprecated(since = "July 2024", forRemoval = true)
@Override
public ResponseEntity<FeaturedWorkspacesConfigResponse> getFeaturedWorkspacesConfig() {
FeaturedWorkspacesConfig fwConfig = configProvider.get();
FeaturedWorkspacesConfig fwConfig = featuredWorkspacesConfigProvider.get();
return ResponseEntity.ok(
new FeaturedWorkspacesConfigResponse().featuredWorkspacesList(fwConfig.featuredWorkspaces));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ WorkspaceResponse toApiWorkspaceResponse(

default List<WorkspaceResponse> toApiWorkspaceResponseList(
WorkspaceDao workspaceDao,
List<RawlsWorkspaceListResponse> workspaces,
List<RawlsWorkspaceListResponse> fcWorkspaces,
FeaturedWorkspaceService featuredWorkspaceService) {
// fields must include at least "workspace.workspaceId", otherwise
// the map creation will fail
Map<String, RawlsWorkspaceListResponse> fcWorkspacesByUuid =
workspaces.stream()
fcWorkspaces.stream()
.collect(
Collectors.toMap(
fcWorkspace -> fcWorkspace.getWorkspace().getWorkspaceId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.pmiops.workbench.api.FeaturedWorkspacesController;
import org.pmiops.workbench.db.model.DbUser;
import org.pmiops.workbench.db.model.DbUserRecentWorkspace;
import org.pmiops.workbench.db.model.DbWorkspace;
Expand Down Expand Up @@ -34,8 +35,24 @@ public interface WorkspaceService {

List<WorkspaceResponse> getWorkspaces();

/**
* @deprecated Get all Published workspaces, as we defined this term before July 2024. Use this in
* conjunction with {@link FeaturedWorkspacesController#getFeaturedWorkspacesConfig()} to
* construct the list of (pre-July-2024) Featured workspaces.
* <p>Use {@link WorkspaceService#getFeaturedWorkspaces()} to retrieve the Featured/Published
* workspaces from July 2024 onward.
* @return List of all Published workspaces
*/
@Deprecated(since = "July 2024", forRemoval = true)
List<WorkspaceResponse> getPublishedWorkspaces();

/**
* Get all Featured workspaces from the DB.
*
* @return List of all Featured workspaces
*/
List<WorkspaceResponse> getFeaturedWorkspaces();

/**
* Return the email associated with the group that we use to indicate that a workspace is
* published. (implementation detail: it's the RT auth domain group email)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ public List<WorkspaceResponse> getPublishedWorkspaces() {
.toList();
}

@Override
public List<WorkspaceResponse> getFeaturedWorkspaces() {
return workspaceMapper
.toApiWorkspaceResponseList(
workspaceDao, fireCloudService.getWorkspaces(), featuredWorkspaceService)
.stream()
.filter(workspaceResponse -> workspaceResponse.getWorkspace().getFeaturedCategory() != null)
.toList();
}

@Override
public String getPublishedWorkspacesGroupEmail() {
// All users with CT access also have RT access, so we know that any user with access to
Expand Down
21 changes: 21 additions & 0 deletions api/src/main/resources/workbench-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,27 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/WorkspaceResponseListResponse'
/v1/workspaces/featured:
get:
tags:
- Featured Workspace
description: Returns a list of featured workspaces
operationId: getFeaturedWorkspaces
responses:
200:
description: A list of featured workspace entity objects including their featured
workspace categories.
content:
application/json:
schema:
$ref: '#/components/schemas/WorkspaceResponseListResponse'
501:
description: This endpoint is not available in this environment because
enablePublishedWorkspacesViaDb is not set.
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
/v1/workspaces/{workspaceNamespace}/{workspaceId}/isNotebookTransferComplete:
get:
tags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,4 +676,38 @@ public void testGetWorkspace_featuredCategory() {
assertThat(response.getWorkspace().getFeaturedCategory())
.isEqualTo(FeaturedWorkspaceCategory.TUTORIAL_WORKSPACES);
}

@Test
public void testGetFeaturedWorkspaces_all() {
// start with none
var before = workspaceService.getFeaturedWorkspaces();
assertThat(before).isEmpty();

// set all workspaces as featured
when(mockFeaturedWorkspaceService.isFeaturedWorkspace(any())).thenReturn(true);
when(mockFeaturedWorkspaceService.getFeaturedCategory(any()))
.thenReturn(FeaturedWorkspaceCategory.TUTORIAL_WORKSPACES);

var result = workspaceService.getFeaturedWorkspaces();
assertThat(result).hasSize(dbWorkspaces.size());
}

@Test
public void testGetFeaturedWorkspaces_one() {
// arbitrary choice from dbWorkspaces
var testWorkspace = dbWorkspaces.get(2);

when(mockFeaturedWorkspaceService.isFeaturedWorkspace(testWorkspace)).thenReturn(true);
when(mockFeaturedWorkspaceService.getFeaturedCategory(testWorkspace))
.thenReturn(FeaturedWorkspaceCategory.DEMO_PROJECTS);

var result = workspaceService.getFeaturedWorkspaces();
assertThat(result).hasSize(1);
var resultWorkspace = result.get(0).getWorkspace();
assertThat(resultWorkspace.getName()).isEqualTo(testWorkspace.getName());
assertThat(resultWorkspace.getNamespace()).isEqualTo(testWorkspace.getWorkspaceNamespace());
assertThat(resultWorkspace.getId()).isEqualTo(testWorkspace.getFirecloudName());
assertThat(resultWorkspace.getFeaturedCategory())
.isEqualTo(FeaturedWorkspaceCategory.DEMO_PROJECTS);
}
}

0 comments on commit debc763

Please sign in to comment.