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

Use spring HATEOAS to generate URIs for tests #1706

Closed
wants to merge 11 commits into from
Closed
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ dependencies {

implementation 'com.squareup.okhttp3:okhttp'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.hateoas:spring-hateoas'
implementation 'io.micrometer:micrometer-registry-prometheus'

implementation 'com.fasterxml.jackson.core:jackson-core:2.15.3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ public NamedParameterJdbcTemplate synapseJdbcTemplate(
return new NamedParameterJdbcTemplate(ds);
}

// Use Primary to fix an issue with unqualified ObjectMapper injections in spring-hateoas.
@Primary
@Bean("objectMapper")
public ObjectMapper objectMapper() {
return new ObjectMapper()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package bio.terra.common.fixtures;
package bio.terra.app.configuration;

import bio.terra.app.configuration.ApplicationConfiguration;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
Expand All @@ -20,13 +22,15 @@
import bio.terra.model.DRSPassportRequestModel;
import bio.terra.service.filedata.DrsService;
import bio.terra.service.filedata.exception.DrsObjectNotFoundException;
import java.net.URI;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
Expand All @@ -35,12 +39,9 @@
@ContextConfiguration(classes = {DataRepositoryServiceApiController.class})
@Tag(Unit.TAG)
@WebMvcTest
@MockBean(ApplicationConfiguration.class)
class DataRepositoryServiceApiControllerTest {

private static final String GET_DRS_OBJECT_ENDPOINT = "/ga4gh/drs/v1/objects/{object_id}";
private static final String GET_DRS_OBJECT_ACCESS_ENDPOINT =
"/ga4gh/drs/v1/objects/{object_id}/access/{access_id}";

// Test fixtures
private static final String DRS_ID = "foo";
private static final String DRS_ACCESS_ID = "bar";
Expand All @@ -54,7 +55,6 @@ class DataRepositoryServiceApiControllerTest {

@Autowired private MockMvc mvc;

@MockBean private ApplicationConfiguration applicationConfiguration;
@MockBean private DrsService drsService;
@MockBean private AuthenticatedUserRequestFactory authenticatedUserRequestFactory;

Expand All @@ -66,45 +66,68 @@ void setUp() {
when(authenticatedUserRequestFactory.from(any())).thenReturn(TEST_USER);
}

private static <T> URI createUri(ResponseEntity<T> object) {
return linkTo(object).toUri();
}

private static DataRepositoryServiceApiController getApi() {
return methodOn(DataRepositoryServiceApiController.class);
}

private static URI createGetUri() {
return createUri(getApi().getObject(DRS_ID, false));
}

private static URI createAccessUri() {
return createUri(getApi().getAccessURL(DRS_ID, DRS_ACCESS_ID, null));
}

@Test
void testUnknownDrsIdWithGetFlow() throws Exception {
when(drsService.lookupObjectByDrsId(TEST_USER, DRS_ID, false))
.thenThrow(DrsObjectNotFoundException.class);
mvc.perform(get(GET_DRS_OBJECT_ENDPOINT, DRS_ID)).andExpect(status().isNotFound());
mvc.perform(get(createGetUri())).andExpect(status().isNotFound());

when(drsService.getAccessUrlForObjectId(TEST_USER, DRS_ID, DRS_ACCESS_ID, null))
.thenThrow(DrsObjectNotFoundException.class);
mvc.perform(get(GET_DRS_OBJECT_ACCESS_ENDPOINT, DRS_ID, DRS_ACCESS_ID))
.andExpect(status().isNotFound());
mvc.perform(get(createAccessUri())).andExpect(status().isNotFound());
}

@Test
void testKnownDrsIdWithGetFlow() throws Exception {
when(drsService.lookupObjectByDrsId(TEST_USER, DRS_ID, false)).thenReturn(DRS_OBJECT);
mvc.perform(get(GET_DRS_OBJECT_ENDPOINT, DRS_ID))
mvc.perform(get(createGetUri()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(DRS_ID));

when(drsService.getAccessUrlForObjectId(TEST_USER, DRS_ID, DRS_ACCESS_ID, null))
.thenReturn(DRS_ACCESS_URL_OBJECT);
mvc.perform(get(GET_DRS_OBJECT_ACCESS_ENDPOINT, DRS_ID, DRS_ACCESS_ID))
mvc.perform(get(createAccessUri()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.url").value(DRS_ACCESS_URL));
}

private static URI createPostObjectUri() {
return createUri(getApi().postObject(DRS_ID, PASSPORT));
}

private static URI createPostAccessUri() {
return createUri(getApi().postAccessURL(DRS_ID, DRS_ACCESS_ID, PASSPORT, null));
}

@Test
void testUnknownDrsIdWithPostFlow() throws Exception {
when(drsService.lookupObjectByDrsIdPassport(DRS_ID, PASSPORT))
.thenThrow(DrsObjectNotFoundException.class);
mvc.perform(
post(GET_DRS_OBJECT_ENDPOINT, DRS_ID)
post(createPostObjectUri())
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtils.mapToJson(PASSPORT)));

when(drsService.postAccessUrlForObjectId(DRS_ID, DRS_ACCESS_ID, PASSPORT, null))
.thenThrow(DrsObjectNotFoundException.class);
mvc.perform(
post(GET_DRS_OBJECT_ACCESS_ENDPOINT, DRS_ID, DRS_ACCESS_ID)
post(createPostAccessUri())
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtils.mapToJson(PASSPORT)))
.andExpect(status().isNotFound());
Expand All @@ -114,7 +137,7 @@ void testUnknownDrsIdWithPostFlow() throws Exception {
void testKnownDrsIdWithPostFlow() throws Exception {
when(drsService.lookupObjectByDrsIdPassport(DRS_ID, PASSPORT)).thenReturn(DRS_OBJECT);
mvc.perform(
post(GET_DRS_OBJECT_ENDPOINT, DRS_ID)
post(createPostObjectUri())
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtils.mapToJson(PASSPORT)))
.andExpect(status().isOk())
Expand All @@ -123,7 +146,7 @@ void testKnownDrsIdWithPostFlow() throws Exception {
when(drsService.postAccessUrlForObjectId(DRS_ID, DRS_ACCESS_ID, PASSPORT, null))
.thenReturn(DRS_ACCESS_URL_OBJECT);
mvc.perform(
post(GET_DRS_OBJECT_ACCESS_ENDPOINT, DRS_ID, DRS_ACCESS_ID)
post(createPostAccessUri())
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtils.mapToJson(PASSPORT)))
.andExpect(status().isOk())
Expand All @@ -134,13 +157,13 @@ void testKnownDrsIdWithPostFlow() throws Exception {
void testUnknownDrsIdWithOptionsFlow() throws Exception {
when(drsService.lookupAuthorizationsByDrsId(DRS_ID))
.thenThrow(DrsObjectNotFoundException.class);
mvc.perform(options(GET_DRS_OBJECT_ENDPOINT, DRS_ID)).andExpect(status().isNotFound());
mvc.perform(options(createGetUri())).andExpect(status().isNotFound());
}

@Test
void testKnownDrsIdWithOptionsFlow() throws Exception {
when(drsService.lookupAuthorizationsByDrsId(DRS_ID)).thenReturn(PASSPORT_AUTHORIZATIONS);
mvc.perform(options(GET_DRS_OBJECT_ENDPOINT, DRS_ID))
mvc.perform(options(createGetUri()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.passport_auth_issuers[0]").value(PASSPORT_ISSUER));
}
Expand Down
Loading
Loading