Skip to content

Commit

Permalink
AJ-1981: final unit tests to control plane (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidangb authored Sep 7, 2024
1 parent 3367d0c commit 4d8df93
Show file tree
Hide file tree
Showing 21 changed files with 208 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
import org.databiosphere.workspacedata.model.SearchRequest;
import org.databiosphere.workspacedata.model.StatusResponse;
import org.databiosphere.workspacedata.model.TsvUploadResponse;
import org.databiosphere.workspacedataservice.common.DataPlaneTestBase;
import org.databiosphere.workspacedataservice.config.TwdsProperties;
import org.databiosphere.workspacedataservice.common.ControlPlaneTestBase;
import org.databiosphere.workspacedataservice.dao.WorkspaceRepository;
import org.databiosphere.workspacedataservice.service.CollectionService;
import org.databiosphere.workspacedataservice.shared.model.WorkspaceId;
import org.databiosphere.workspacedataservice.workspace.WorkspaceDataTableType;
import org.databiosphere.workspacedataservice.workspace.WorkspaceRecord;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -49,31 +52,38 @@
@ActiveProfiles(profiles = "mock-sam")
@DirtiesContext
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class GeneratedClientTests extends DataPlaneTestBase {
class GeneratedClientTests extends ControlPlaneTestBase {

private ApiClient apiClient;
@LocalServerPort int port;

@Autowired TwdsProperties twdsProperties;
private UUID collectionId;
private final String version = "v0.2";

@Autowired CollectionService collectionService;
@Autowired NamedParameterJdbcTemplate namedTemplate;
@Autowired WorkspaceRepository workspaceRepository;

@BeforeEach
void init() throws ApiException {
WorkspaceId workspaceId = WorkspaceId.of(UUID.randomUUID());

// create the workspace record
workspaceRepository.save(
new WorkspaceRecord(workspaceId, WorkspaceDataTableType.WDS, /* newFlag= */ true));

apiClient = new ApiClient();
apiClient.setBasePath("http://localhost:" + port);
CollectionRequest collectionRequest = new CollectionRequest();
collectionRequest.setName(RandomStringUtils.randomAlphabetic(16));
collectionRequest.setDescription("description");
collectionId = createNewCollection(collectionRequest, twdsProperties.workspaceId().id());
collectionId = createNewCollection(collectionRequest, workspaceId.id());
}

@AfterEach
void afterEach() {
TestUtils.cleanAllCollections(collectionService, namedTemplate);
TestUtils.cleanAllWorkspaces(namedTemplate);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
@ActiveProfiles({"control-plane"})
@TestPropertySource(
properties = {
// enable all controllers
"controlPlanePreview=on",
// turn off pubsub autoconfiguration for tests
"spring.cloud.gcp.pubsub.enabled=false",
// aggressive retry settings so unit tests don't run too long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.time.OffsetDateTime;
import java.util.UUID;
import java.util.stream.Stream;
import org.databiosphere.workspacedataservice.common.DataPlaneTestBase;
import org.databiosphere.workspacedataservice.config.TwdsProperties;
import org.databiosphere.workspacedataservice.dao.RecordDao;
import org.databiosphere.workspacedataservice.generated.GenericJobServerModel;
Expand All @@ -37,18 +38,22 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;

@DirtiesContext
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles({"mock-backup-dao", "mock-restore-dao", "mock-clone-dao"})
class AllControllersPermissionsTest extends MockMvcTestBase {
class AllControllersPermissionsTest extends DataPlaneTestBase {
@Autowired private MockMvc mockMvc;
@Autowired PermissionService permissionService;
@MockBean SamAuthorizationDaoFactory samAuthorizationDaoFactory;
@MockBean CollectionService collectionService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.databiosphere.workspacedataservice.common.DataPlaneTestBase;
import org.databiosphere.workspacedataservice.config.TwdsProperties;
import org.databiosphere.workspacedataservice.generated.CollectionServerModel;
import org.databiosphere.workspacedataservice.sam.MockSamAuthorizationDao;
Expand All @@ -32,6 +33,8 @@
import org.junit.jupiter.api.TestInstance;
import org.mockito.stubbing.OngoingStubbing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.RowMapper;
Expand All @@ -40,6 +43,7 @@
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

/**
Expand All @@ -51,10 +55,13 @@
@TestPropertySource(
properties = {"twds.instance.workspace-id=45f90f59-f83d-453f-961a-480ec740df9f"})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CollectionControllerMockMvcSingleTenantTest extends MockMvcTestBase {
@AutoConfigureMockMvc
@SpringBootTest
class CollectionControllerMockMvcSingleTenantTest extends DataPlaneTestBase {

@Autowired private ObjectMapper objectMapper;
@Autowired private MockMvc mockMvc;
@Autowired private NamedParameterJdbcTemplate namedTemplate;
@Autowired private ObjectMapper objectMapper;
@Autowired private TwdsProperties twdsProperties;

@MockBean SamAuthorizationDaoFactory samAuthorizationDaoFactory;
Expand Down Expand Up @@ -104,7 +111,7 @@ void createCollectionInSingleTenantWorkspace() throws Exception {
mockMvc
.perform(
post("/collections/v1/{workspaceId}", workspaceId)
.content(toJson(collectionServerModel))
.content(objectMapper.writeValueAsString(collectionServerModel))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andReturn();
Expand Down Expand Up @@ -143,7 +150,7 @@ void createCollectionOutsideSingleTenantWorkspace() throws Exception {
mockMvc
.perform(
post("/collections/v1/{workspaceId}", workspaceId)
.content(toJson(collectionServerModel))
.content(objectMapper.writeValueAsString(collectionServerModel))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andReturn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,11 @@
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.web.bind.MethodArgumentNotValidException;

@ActiveProfiles(profiles = {"mock-sam"})
@DirtiesContext
// this class explicitly tests multi-tenancy, so it ensures that
// `enforce-collections-match-workspace-id` is turned off
@TestPropertySource(properties = {"twds.tenancy.enforce-collections-match-workspace-id=false"})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CollectionControllerMockMvcTest extends MockMvcTestBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.databiosphere.workspacedataservice.TestUtils;
import org.databiosphere.workspacedataservice.common.DataPlaneTestBase;
import org.databiosphere.workspacedataservice.config.TwdsProperties;
import org.databiosphere.workspacedataservice.common.ControlPlaneTestBase;
import org.databiosphere.workspacedataservice.dao.WorkspaceRepository;
import org.databiosphere.workspacedataservice.generated.CollectionRequestServerModel;
import org.databiosphere.workspacedataservice.service.CollectionService;
import org.databiosphere.workspacedataservice.shared.model.RecordAttributes;
import org.databiosphere.workspacedataservice.shared.model.RecordRequest;
import org.databiosphere.workspacedataservice.shared.model.RecordResponse;
import org.databiosphere.workspacedataservice.shared.model.RecordType;
import org.databiosphere.workspacedataservice.shared.model.WorkspaceId;
import org.databiosphere.workspacedataservice.workspace.WorkspaceDataTableType;
import org.databiosphere.workspacedataservice.workspace.WorkspaceRecord;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -32,25 +36,35 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;

@ActiveProfiles(profiles = "mock-sam")
@DirtiesContext
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ConcurrentDataTypeChangesTest extends DataPlaneTestBase {
class ConcurrentDataTypeChangesTest extends ControlPlaneTestBase {

@Autowired private CollectionService collectionService;
@Autowired private NamedParameterJdbcTemplate namedTemplate;
@Autowired private TestRestTemplate restTemplate;
@Autowired private WorkspaceRepository workspaceRepository;

private HttpHeaders headers;
private UUID instanceId;

private static final String recordId = "concurrent-changes";
private static final RecordType recordType = RecordType.valueOf("concurrency");
private static final String versionId = "v0.2";

@Autowired private TwdsProperties twdsProperties;

@BeforeEach
void setUp() throws JsonProcessingException {
WorkspaceId workspaceId = WorkspaceId.of(UUID.randomUUID());

// create the workspace record
workspaceRepository.save(
new WorkspaceRecord(workspaceId, WorkspaceDataTableType.WDS, /* newFlag= */ true));

String name = "test-name";
String description = "test-description";

Expand All @@ -68,7 +82,7 @@ void setUp() throws JsonProcessingException {
new HttpEntity<>(
objectMapper.writeValueAsString(collectionRequestServerModel), headers),
String.class,
twdsProperties.workspaceId().id());
workspaceId);
assertEquals(HttpStatus.CREATED, response.getStatusCode());

instanceId =
Expand All @@ -77,15 +91,8 @@ void setUp() throws JsonProcessingException {

@AfterEach
void tearDown() {
ResponseEntity<String> response =
restTemplate.exchange(
"/collections/v1/{workspaceId}/{instanceid}",
HttpMethod.DELETE,
new HttpEntity<>("", headers),
String.class,
twdsProperties.workspaceId().id(),
instanceId);
assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
TestUtils.cleanAllCollections(collectionService, namedTemplate);
TestUtils.cleanAllWorkspaces(namedTemplate);
}

@Test
Expand Down Expand Up @@ -154,7 +161,7 @@ void concurrentColumnCreation() {
attrs.put("sys_name", recordId);
RecordAttributes expected = new RecordAttributes(attrs);

RecordAttributes actual = finalResponse.getBody().recordAttributes();
RecordAttributes actual = Objects.requireNonNull(finalResponse.getBody()).recordAttributes();

assertEquals(
expected.attributeSet().size(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang3.RandomStringUtils;
import org.databiosphere.workspacedataservice.common.DataPlaneTestBase;
import org.databiosphere.workspacedataservice.common.ControlPlaneTestBase;
import org.databiosphere.workspacedataservice.service.MDCServletRequestListener;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;
Expand All @@ -26,7 +26,7 @@
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = {"spring.main.allow-bean-definition-overriding=true"})
class FullStackMDCRequestResponseTest extends DataPlaneTestBase {
class FullStackMDCRequestResponseTest extends ControlPlaneTestBase {

// hmmmmm https://github.com/gradle/gradle/issues/5975

Expand Down
Loading

0 comments on commit 4d8df93

Please sign in to comment.