Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BENCH-826] Split GitRepo, DataRepo, Folder, FlexibleResource APIs from MockMvcUtils #1400

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package bio.terra.workspace.app.controller;

import static bio.terra.workspace.common.utils.MockMvcUtils.assertApiFlexibleResourceEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;

Expand All @@ -10,11 +9,18 @@
import bio.terra.workspace.common.GcpCloudUtils;
import bio.terra.workspace.common.StairwayTestUtils;
import bio.terra.workspace.common.fixtures.PolicyFixtures;
import bio.terra.workspace.common.mocks.MockFlexibleResourceApi;
import bio.terra.workspace.common.utils.MockMvcUtils;
import bio.terra.workspace.common.utils.TestUtils;
import bio.terra.workspace.connected.UserAccessUtils;
import bio.terra.workspace.generated.model.ApiAccessScope;
import bio.terra.workspace.generated.model.ApiCloningInstructionsEnum;
import bio.terra.workspace.generated.model.ApiFlexibleResource;
import bio.terra.workspace.generated.model.ApiFlexibleResourceAttributes;
import bio.terra.workspace.generated.model.ApiManagedBy;
import bio.terra.workspace.generated.model.ApiPrivateResourceState;
import bio.terra.workspace.generated.model.ApiPrivateResourceUser;
import bio.terra.workspace.generated.model.ApiStewardshipType;
import bio.terra.workspace.generated.model.ApiWorkspaceDescription;
import bio.terra.workspace.generated.model.ApiWsmPolicyInputs;
import bio.terra.workspace.service.crl.CrlService;
Expand All @@ -26,7 +32,10 @@
import bio.terra.workspace.service.resource.controlled.cloud.any.flexibleresource.ControlledFlexibleResource;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.UUID;
import org.apache.http.HttpStatus;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
Expand All @@ -47,6 +56,7 @@ public class ControlledFlexibleResourceApiControllerConnectedTest extends BaseCo

@Autowired MockMvc mockMvc;
@Autowired MockMvcUtils mockMvcUtils;
@Autowired MockFlexibleResourceApi mockFlexibleResourceApi;
@Autowired ObjectMapper objectMapper;
@Autowired UserAccessUtils userAccessUtils;
@Autowired JobService jobService;
Expand All @@ -60,7 +70,7 @@ public class ControlledFlexibleResourceApiControllerConnectedTest extends BaseCo
private UUID workspaceId2;
private ApiFlexibleResource sourceFlexResource;

private final String sourceResourceName =
private static final String sourceResourceName =
TestUtils.appendRandomNumber("source-flexible-resource-name");
private static final String sourceTypeNamespace = "terra";
private static final String sourceType = "fake-flexible-type";
Expand All @@ -80,7 +90,7 @@ public void setup() throws Exception {
.getId();
// Source flex resource used in clone tests.
sourceFlexResource =
mockMvcUtils
mockFlexibleResourceApi
.createFlexibleResource(
userAccessUtils.defaultUserAuthRequest(),
workspaceId,
Expand Down Expand Up @@ -137,7 +147,7 @@ void clone_policiesMerged() throws Exception {

// Clone resource
String destResourceName = TestUtils.appendRandomNumber("dest-resource-name");
mockMvcUtils.cloneFlexResource(
mockFlexibleResourceApi.cloneFlexibleResourceAndWait(
userAccessUtils.defaultUserAuthRequest(),
/*sourceWorkspaceId=*/ workspaceId,
sourceFlexResource.getMetadata().getResourceId(),
Expand Down Expand Up @@ -165,7 +175,7 @@ void clone_copyResource() throws Exception {
String destDescription = "new description";

ApiFlexibleResource clonedFlexResource =
mockMvcUtils.cloneFlexResource(
mockFlexibleResourceApi.cloneFlexibleResourceAndWait(
userAccessUtils.defaultUserAuthRequest(),
/*sourceWorkspaceId=*/ workspaceId,
sourceFlexResource.getMetadata().getResourceId(),
Expand All @@ -175,7 +185,7 @@ void clone_copyResource() throws Exception {
destDescription);

// Assert resource returned in clone flight response.
mockMvcUtils.assertClonedControlledFlexibleResource(
assertClonedControlledFlexibleResource(
sourceFlexResource,
clonedFlexResource,
/*expectedDestWorkspaceId=*/ workspaceId2,
Expand All @@ -185,28 +195,30 @@ void clone_copyResource() throws Exception {
userAccessUtils.getDefaultUserEmail());

// Assert resource returned by get
final ApiFlexibleResource gotResource =
mockMvcUtils.getFlexibleResource(
ApiFlexibleResource gotResource =
mockFlexibleResourceApi.getFlexibleResource(
userAccessUtils.defaultUserAuthRequest(),
workspaceId2,
clonedFlexResource.getMetadata().getResourceId());

assertApiFlexibleResourceEquals(clonedFlexResource, gotResource);
MockFlexibleResourceApi.assertApiFlexibleResourceEquals(clonedFlexResource, gotResource);
}

@Test
void clone_copyResource_undo() throws Exception {
String destResourceName = TestUtils.appendRandomNumber("dest-resource-name");
String destDescription = "new description";

mockMvcUtils.cloneFlex_undo(
mockFlexibleResourceApi.cloneFlexibleResourceAndExpect(
userAccessUtils.defaultUserAuthRequest(),
/*sourceWorkspaceId=*/ workspaceId,
sourceFlexResource.getMetadata().getResourceId(),
/*destWorkspaceId=*/ workspaceId2,
ApiCloningInstructionsEnum.RESOURCE,
destResourceName,
destDescription);
destDescription,
List.of(HttpStatus.SC_INTERNAL_SERVER_ERROR),
/*shouldUndo=*/ true);

// Assert clone doesn't exist. There's no resource ID, so search on resource name.
mockMvcUtils.assertNoResourceWithName(
Expand All @@ -216,19 +228,21 @@ void clone_copyResource_undo() throws Exception {
@Test
public void clone_requesterNoReadAccessOnSourceWorkspace_throws403() throws Exception {
String destResourceName = TestUtils.appendRandomNumber("dest-resource-name");
mockMvcUtils.cloneFlex_forbidden(
mockFlexibleResourceApi.cloneFlexibleResourceAndExpect(
userAccessUtils.secondUserAuthRequest(),
/*sourceWorkspaceId=*/ workspaceId,
/*sourceResourceId=*/ sourceFlexResource.getMetadata().getResourceId(),
/*destWorkspaceId=*/ workspaceId2,
ApiCloningInstructionsEnum.RESOURCE,
/*destResourceName=*/ destResourceName,
/*description=*/ null);
/*description=*/ null,
List.of(HttpStatus.SC_FORBIDDEN),
/*shouldUndo=*/ false);
}

@Test
public void clone_requesterNoWriteAccessOnDestWorkspace_throws403() throws Exception {
final AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
mockMvcUtils.grantRole(
userRequest, workspaceId, WsmIamRole.READER, userAccessUtils.getSecondUserEmail());
mockMvcUtils.grantRole(
Expand All @@ -237,14 +251,16 @@ public void clone_requesterNoWriteAccessOnDestWorkspace_throws403() throws Excep
// Always remove roles before test terminates.
try {
String destResourceName = TestUtils.appendRandomNumber("dest-resource-name");
mockMvcUtils.cloneFlex_forbidden(
mockFlexibleResourceApi.cloneFlexibleResourceAndExpect(
userAccessUtils.secondUserAuthRequest(),
/*sourceWorkspaceId=*/ workspaceId,
/*sourceResourceId=*/ sourceFlexResource.getMetadata().getResourceId(),
/*destWorkspaceId=*/ workspaceId2,
ApiCloningInstructionsEnum.RESOURCE,
/*destResourceName=*/ destResourceName,
/*description=*/ null);
/*description=*/ null,
List.of(HttpStatus.SC_FORBIDDEN),
/*shouldUndo=*/ false);
} finally {
mockMvcUtils.removeRole(
userRequest, workspaceId, WsmIamRole.READER, userAccessUtils.getSecondUserEmail());
Expand All @@ -271,24 +287,23 @@ public void clone_SecondUserHasWriteAccessOnDestWorkspace_succeeds() throws Exce
String destResourceName = TestUtils.appendRandomNumber("dest-resource-name");
String destDescription = "new description";
ApiFlexibleResource clonedFlexResource =
mockMvcUtils.cloneFlexResource(
mockFlexibleResourceApi.cloneFlexibleResourceAndWait(
userAccessUtils.secondUserAuthRequest(),
/*sourceWorkspaceId=*/ workspaceId,
sourceFlexResource.getMetadata().getResourceId(),
/*destWorkspaceId=*/ workspaceId2,
ApiCloningInstructionsEnum.RESOURCE,
destResourceName,
destDescription);
mockMvcUtils.assertClonedControlledFlexibleResource(
assertClonedControlledFlexibleResource(
sourceFlexResource,
clonedFlexResource,
/*expectedDestWorkspaceId=*/ workspaceId2,
destResourceName,
/*expectedResourceDescription=*/ destDescription,
userAccessUtils.getSecondUserEmail(),
userAccessUtils.getSecondUserEmail());

mockMvcUtils.deleteFlexibleResource(
mockFlexibleResourceApi.deleteFlexibleResource(
userAccessUtils.defaultUserAuthRequest(),
workspaceId2,
clonedFlexResource.getMetadata().getResourceId());
Expand All @@ -305,4 +320,37 @@ public void clone_SecondUserHasWriteAccessOnDestWorkspace_succeeds() throws Exce
userAccessUtils.getSecondUserEmail());
}
}

public static void assertClonedControlledFlexibleResource(
@NotNull ApiFlexibleResource originalFlexibleResource,
ApiFlexibleResource actualFlexibleResource,
UUID expectedDestWorkspaceId,
String expectedResourceName,
String expectedResourceDescription,
String expectedCreatedBy,
String expectedLastUpdatedBy) {
// Attributes are immutable upon cloning.
ApiFlexibleResourceAttributes originalAttributes = originalFlexibleResource.getAttributes();

MockFlexibleResourceApi.assertFlexibleResource(
actualFlexibleResource,
ApiStewardshipType.CONTROLLED,
ApiCloningInstructionsEnum.DEFINITION,
expectedDestWorkspaceId,
expectedResourceName,
expectedResourceDescription,
expectedCreatedBy,
expectedLastUpdatedBy,
originalAttributes.getTypeNamespace(),
originalAttributes.getType(),
originalAttributes.getData());

MockMvcUtils.assertControlledResourceMetadata(
actualFlexibleResource.getMetadata().getControlledResourceMetadata(),
ApiAccessScope.SHARED_ACCESS,
ApiManagedBy.USER,
new ApiPrivateResourceUser(),
ApiPrivateResourceState.NOT_APPLICABLE,
null);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package bio.terra.workspace.app.controller;

import static bio.terra.workspace.common.fixtures.ControlledResourceFixtures.RESOURCE_DESCRIPTION;
import static bio.terra.workspace.common.utils.MockMvcUtils.CONTROLLED_FLEXIBLE_RESOURCES_V1_PATH_FORMAT;
import static bio.terra.workspace.common.mocks.MockFlexibleResourceApi.CREATE_CONTROLLED_FLEXIBLE_RESOURCES_PATH_FORMAT;
import static bio.terra.workspace.common.utils.MockMvcUtils.USER_REQUEST;
import static bio.terra.workspace.common.utils.MockMvcUtils.assertApiFlexibleResourceEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import bio.terra.workspace.common.BaseUnitTest;
import bio.terra.workspace.common.mocks.MockFlexibleResourceApi;
import bio.terra.workspace.common.utils.MockMvcUtils;
import bio.terra.workspace.common.utils.TestUtils;
import bio.terra.workspace.generated.model.ApiCloningInstructionsEnum;
import bio.terra.workspace.generated.model.ApiCreateControlledFlexibleResourceRequestBody;
import bio.terra.workspace.generated.model.ApiCreatedControlledFlexibleResource;
import bio.terra.workspace.generated.model.ApiFlexibleResource;
import bio.terra.workspace.generated.model.ApiStewardshipType;
Expand All @@ -39,6 +40,7 @@ public class ControlledFlexibleResourceApiControllerTest extends BaseUnitTest {

@Autowired MockMvc mockMvc;
@Autowired MockMvcUtils mockMvcUtils;
@Autowired MockFlexibleResourceApi mockFlexibleResourceApi;
@Autowired ObjectMapper objectMapper;
private static final String defaultDecodedData = "{\"name\":\"original JSON\"}";
private static final String defaultNewDecodedData = "{\"description\":\"this is new JSON\"}";
Expand Down Expand Up @@ -106,7 +108,7 @@ public void create() throws Exception {
ApiFlexibleResource createdFlexResource =
createDefaultFlexResourceWithData(workspaceId, encodedJsonString).getFlexibleResource();

mockMvcUtils.assertFlexibleResource(
MockFlexibleResourceApi.assertFlexibleResource(
createdFlexResource,
ApiStewardshipType.CONTROLLED,
ApiCloningInstructionsEnum.DEFINITION,
Expand All @@ -120,9 +122,9 @@ public void create() throws Exception {
expectedInputJsonString);

ApiFlexibleResource gotResource =
mockMvcUtils.getFlexibleResource(
mockFlexibleResourceApi.getFlexibleResource(
USER_REQUEST, workspaceId, createdFlexResource.getMetadata().getResourceId());
assertApiFlexibleResourceEquals(createdFlexResource, gotResource);
MockFlexibleResourceApi.assertApiFlexibleResourceEquals(createdFlexResource, gotResource);
}

@Test
Expand All @@ -132,14 +134,14 @@ public void create_rejectsLargeData() throws Exception {
byte[] veryLargeData = new byte[6000];
Arrays.fill(veryLargeData, (byte) 'a');

var request =
mockMvcUtils.createFlexibleResourceRequestBody(
ApiCreateControlledFlexibleResourceRequestBody request =
mockFlexibleResourceApi.createFlexibleResourceRequestBody(
defaultName, defaultTypeNamespace, defaultType, veryLargeData);

mockMvcUtils.postExpect(
USER_REQUEST,
objectMapper.writeValueAsString(request),
CONTROLLED_FLEXIBLE_RESOURCES_V1_PATH_FORMAT.formatted(workspaceId),
CREATE_CONTROLLED_FLEXIBLE_RESOURCES_PATH_FORMAT.formatted(workspaceId),
HttpStatus.SC_BAD_REQUEST);
}

Expand All @@ -161,7 +163,7 @@ public void update() throws Exception {
ApiCloningInstructionsEnum.DEFINITION, flexResource.getMetadata().getCloningInstructions());

ApiFlexibleResource updatedFlex =
mockMvcUtils.updateFlexibleResource(
mockFlexibleResourceApi.updateFlexibleResource(
workspaceId,
resourceId,
newName,
Expand All @@ -188,7 +190,7 @@ public void update_onlyNameAndDescription() throws Exception {
var newDescription = "This is an updated description";

ApiFlexibleResource updatedFlex =
mockMvcUtils.updateFlexibleResource(
mockFlexibleResourceApi.updateFlexibleResource(
workspaceId, resourceId, newName, newDescription, null, null);

assertEquals(newName, updatedFlex.getMetadata().getName());
Expand All @@ -211,7 +213,7 @@ public void update_onlyData() throws Exception {
byte[] encodedNewData = defaultNewDecodedData.getBytes(StandardCharsets.UTF_8);

ApiFlexibleResource updatedFlex =
mockMvcUtils.updateFlexibleResource(
mockFlexibleResourceApi.updateFlexibleResource(
workspaceId, resourceId, null, null, encodedNewData, null);

assertEquals(defaultName, updatedFlex.getMetadata().getName());
Expand All @@ -230,7 +232,7 @@ public void update_rejectsLargeData() throws Exception {
byte[] veryLargeData = new byte[6000];
Arrays.fill(veryLargeData, (byte) 'a');

mockMvcUtils.updateFlexibleResourceExpect(
mockFlexibleResourceApi.updateFlexibleResourceExpect(
workspaceId, resourceId, null, null, veryLargeData, null, HttpStatus.SC_BAD_REQUEST);
}

Expand All @@ -242,21 +244,22 @@ private ApiCreatedControlledFlexibleResource createDefaultFlexResource(UUID work

private ApiCreatedControlledFlexibleResource createDefaultFlexResourceWithData(
UUID workspaceId, byte[] data) throws Exception {
return mockMvcUtils.createFlexibleResource(
return mockFlexibleResourceApi.createFlexibleResource(
USER_REQUEST, workspaceId, defaultName, defaultTypeNamespace, defaultType, data);
}

@Test
public void delete() throws Exception {
UUID workspaceId = mockMvcUtils.createWorkspaceWithoutCloudContext(USER_REQUEST).getId();

var resourceId = createDefaultFlexResourceWithData(workspaceId, null).getResourceId();
UUID resourceId = createDefaultFlexResourceWithData(workspaceId, null).getResourceId();

mockMvcUtils.getFlexibleResourceExpect(workspaceId, resourceId, HttpStatus.SC_OK);
mockFlexibleResourceApi.getFlexibleResourceExpect(workspaceId, resourceId, HttpStatus.SC_OK);

mockMvcUtils.deleteFlexibleResource(USER_REQUEST, workspaceId, resourceId);
mockFlexibleResourceApi.deleteFlexibleResource(USER_REQUEST, workspaceId, resourceId);

mockMvcUtils.getFlexibleResourceExpect(workspaceId, resourceId, HttpStatus.SC_NOT_FOUND);
mockFlexibleResourceApi.getFlexibleResourceExpect(
workspaceId, resourceId, HttpStatus.SC_NOT_FOUND);
}

@Test
Expand All @@ -266,15 +269,14 @@ void clone_copyNothing() throws Exception {

String destResourceName = TestUtils.appendRandomNumber("dest-resource-name");
ApiFlexibleResource clonedFlexResource =
mockMvcUtils.cloneFlexResource(
mockFlexibleResourceApi.cloneFlexibleResourceAndWait(
USER_REQUEST,
/*sourceWorkspaceId=*/ workspaceId,
resourceId,
/*destWorkspaceId=*/ workspaceId,
ApiCloningInstructionsEnum.NOTHING,
destResourceName,
null);

assertNull(clonedFlexResource);

// Assert clone doesn't exist. There's no resource ID, so search on resource name.
Expand Down
Loading