Skip to content

Commit

Permalink
Use {to,from}Json in MVC tests. (#490)
Browse files Browse the repository at this point in the history
This shorthand intends to eliminate some of the clumsy and repetitive
interaction with `ObjectMapper` and `MvcResult`.
  • Loading branch information
jladieu authored Feb 5, 2024
1 parent 23715b1 commit 8319ab6
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,23 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.databiosphere.workspacedataservice.generated.CapabilitiesServerModel;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

@DirtiesContext
@SpringBootTest
@AutoConfigureMockMvc
class CapabilitiesControllerTest {
@Autowired MockMvc mockMvc;
@Autowired ObjectMapper objectMapper;
class CapabilitiesControllerTest extends MockMvcTestBase {

@Value("classpath:capabilities.json")
Resource capabilitiesResource;

@Test
void resourceFileIsValid() {
assertDoesNotThrow(
() ->
objectMapper.readValue(
capabilitiesResource.getInputStream(), CapabilitiesServerModel.class),
() -> fromJson(capabilitiesResource.getInputStream(), CapabilitiesServerModel.class),
"Have you modified capabilities.json? Is it still valid JSON?");
}

Expand All @@ -41,12 +30,9 @@ void restResponseIsValid() throws Exception {
MvcResult mvcResult =
mockMvc.perform(get("/capabilities/v1")).andExpect(status().isOk()).andReturn();

String rawResponse = mvcResult.getResponse().getContentAsString();

// is the response parsable into the capabilities model?
CapabilitiesServerModel actual =
assertDoesNotThrow(
() -> objectMapper.readValue(rawResponse, CapabilitiesServerModel.class));
assertDoesNotThrow(() -> fromJson(mvcResult, CapabilitiesServerModel.class));
// is the response non-empty?
assertThat(actual).isNotEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
import java.util.UUID;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
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.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

/**
Expand All @@ -22,12 +18,7 @@
* <p>See also CorsLocalMockMvcTest for testing CORS behavior in the "local" Spring profile
*/
@DirtiesContext
@SpringBootTest
@AutoConfigureMockMvc
class CorsLiveMockMvcTest {

@Autowired MockMvc mockMvc;

class CorsLiveMockMvcTest extends MockMvcTestBase {
private static final String versionId = "v0.2";

@ParameterizedTest(name = "CORS response headers for non-local profile to {0} should be correct")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@
import java.util.UUID;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
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.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

/**
Expand All @@ -25,12 +21,7 @@
*/
@ActiveProfiles(profiles = {"local-cors"})
@DirtiesContext
@SpringBootTest
@AutoConfigureMockMvc
class CorsLocalMockMvcTest {

@Autowired MockMvc mockMvc;

class CorsLocalMockMvcTest extends MockMvcTestBase {
private static final String versionId = "v0.2";

@ParameterizedTest(name = "CORS response headers for the local profile {0} should be correct")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,22 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.util.UUID;
import org.databiosphere.workspacedataservice.dao.InstanceDao;
import org.databiosphere.workspacedataservice.generated.GenericJobServerModel;
import org.databiosphere.workspacedataservice.generated.ImportRequestServerModel;
import org.junit.jupiter.api.Test;
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.http.MediaType;
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.MvcResult;

@ActiveProfiles(profiles = {"mock-instance-dao", "mock-sam"})
@DirtiesContext
@SpringBootTest
@AutoConfigureMockMvc
class ImportControllerMockMvcTest {
class ImportControllerMockMvcTest extends MockMvcTestBase {

@Autowired private MockMvc mockMvc;
@Autowired private ObjectMapper mapper;
@Autowired private InstanceDao instanceDao;

@Test
Expand All @@ -37,22 +29,18 @@ void smokeTestCreateImport() throws Exception {
ImportRequestServerModel importRequest =
new ImportRequestServerModel(
ImportRequestServerModel.TypeEnum.PFB, new URI("https://terra.bio"));
String postBody = mapper.writeValueAsString(importRequest);

// calling the API should result in 201 Created
MvcResult mvcResult =
mockMvc
.perform(
post("/{instanceUuid}/import/v1", instanceId)
.content(postBody)
.content(toJson(importRequest))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isAccepted())
.andReturn();

// and the API response should be a valid GenericJobServerModel
assertDoesNotThrow(
() ->
mapper.readValue(
mvcResult.getResponse().getContentAsString(), GenericJobServerModel.class));
assertDoesNotThrow(() -> fromJson(mvcResult, GenericJobServerModel.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,20 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.UUID;
import org.databiosphere.workspacedataservice.dao.JobDao;
import org.databiosphere.workspacedataservice.generated.GenericJobServerModel;
import org.junit.jupiter.api.Test;
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.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

@ActiveProfiles(profiles = {"mock-sam"})
@DirtiesContext
@SpringBootTest
@AutoConfigureMockMvc
class JobControllerMockMvcTest {
@Autowired private MockMvc mockMvc;
@Autowired private ObjectMapper mapper;
class JobControllerMockMvcTest extends MockMvcTestBase {
@MockBean private JobDao jobDao;

@Test
Expand All @@ -52,8 +43,7 @@ void smokeTestGetJob() throws Exception {
.andReturn();

// and the API response should be a valid GenericJobServerModel
GenericJobServerModel actual =
mapper.readValue(mvcResult.getResponse().getContentAsString(), GenericJobServerModel.class);
GenericJobServerModel actual = fromJson(mvcResult, GenericJobServerModel.class);

// which is equal to the expected job
assertEquals(expected, actual);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.databiosphere.workspacedataservice.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
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.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

@AutoConfigureMockMvc
@SpringBootTest
class MockMvcTestBase {
@Autowired private ObjectMapper mapper;
@Autowired protected MockMvc mockMvc;

protected String toJson(Object value) throws JsonProcessingException {
return mapper.writeValueAsString(value);
}

protected <T> T fromJson(MvcResult result, Class<T> valueType)
throws UnsupportedEncodingException, JsonProcessingException {
return mapper.readValue(result.getResponse().getContentAsString(), valueType);
}

protected <T> T fromJson(InputStream inputStream, Class<T> valueType) throws IOException {
return mapper.readValue(inputStream, valueType);
}
}
Loading

0 comments on commit 8319ab6

Please sign in to comment.