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

Draft
wants to merge 11 commits into
base: develop
Choose a base branch
from
Draft
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 @@ -6,13 +6,16 @@
import static org.junit.jupiter.params.provider.Arguments.arguments;
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.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import bio.terra.app.configuration.ApplicationConfiguration;
import bio.terra.common.TestUtils;
import bio.terra.common.category.Unit;
import bio.terra.common.iam.AuthenticatedUserRequestFactory;
import bio.terra.controller.SnapshotsApi;
import bio.terra.model.ErrorModel;
import bio.terra.model.SnapshotRequestAssetModel;
import bio.terra.model.SnapshotRequestContentsModel;
Expand Down Expand Up @@ -46,7 +49,6 @@
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.boot.test.mock.mockito.SpyBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ActiveProfiles;
Expand All @@ -63,19 +65,21 @@
SnapshotsApiController.class,
SnapshotRequestValidator.class
})
@MockBean({
JobService.class,
SnapshotService.class,
IamService.class,
FileService.class,
AuthenticatedUserRequestFactory.class,
SnapshotBuilderService.class
})
@Tag(Unit.TAG)
@WebMvcTest
class SnapshotValidationTest {

@Autowired private MockMvc mvc;
@MockBean private JobService jobService;
@MockBean private SnapshotService snapshotService;
@MockBean private IamService iamService;
@MockBean private IngestRequestValidator ingestRequestValidator;
@MockBean private FileService fileService;
@MockBean private AuthenticatedUserRequestFactory authenticatedUserRequestFactory;
@MockBean private SnapshotBuilderService snapshotBuilderService;
@SpyBean private ApplicationConfiguration applicationConfiguration;
@MockBean private ApplicationConfiguration applicationConfiguration;

private SnapshotRequestModel snapshotByAssetRequest;

Expand All @@ -93,9 +97,10 @@ void setup() {

private ErrorModel expectBadSnapshotCreateRequest(SnapshotRequestModel snapshotRequest)
throws Exception {
var uri = linkTo(methodOn(SnapshotsApi.class).createSnapshot(snapshotRequest)).toUri();
MvcResult result =
mvc.perform(
post("/api/repository/v1/snapshots")
post(uri)
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtils.mapToJson(snapshotRequest)))
.andExpect(status().is4xxClientError())
Expand Down Expand Up @@ -318,10 +323,10 @@ private void checkValidationErrorModel(ErrorModel errorModel, String[] messageCo
*
* We check to see if the code is wrapped in quotes to prevent matching on substrings.
*/
List<Matcher<? super String>> expectedMatches =
var expectedMatches =
Arrays.stream(messageCodes)
.map(code -> containsString("'" + code + "'"))
.collect(Collectors.toList());
.toList();
assertThat("Detail codes are right", details, containsInAnyOrder(expectedMatches));
}
}
Loading