Created
May 1, 2019 20:45
-
-
Save stevesoltys/efd11ceff5a5b53ade713a1754c16071 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.stevesoltys.calendar.controller; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import com.fasterxml.jackson.databind.SerializationFeature; | |
| import lombok.extern.log4j.Log4j2; | |
| import net.javacrumbs.jsonunit.core.Configuration; | |
| 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.core.io.ClassPathResource; | |
| import org.springframework.core.io.Resource; | |
| import org.springframework.mock.web.MockHttpServletResponse; | |
| import org.springframework.security.test.context.support.WithMockUser; | |
| import org.springframework.test.context.ActiveProfiles; | |
| import org.springframework.test.web.servlet.MockMvc; | |
| import java.io.IOException; | |
| import java.nio.file.Files; | |
| import java.util.stream.Collectors; | |
| import static net.javacrumbs.jsonunit.JsonAssert.when; | |
| import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; | |
| import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER; | |
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | |
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | |
| /** | |
| * @author Steve Soltys | |
| */ | |
| @ActiveProfiles("test") | |
| @SpringBootTest | |
| @AutoConfigureMockMvc | |
| @WithMockUser(roles = "USER") | |
| @Log4j2 | |
| public class RestControllerTest { | |
| @Autowired | |
| MockMvc mvc; | |
| public void assertGetRequestEquals(String url, String responseName) throws Exception { | |
| assertGetRequestEquals(url, responseName, when(IGNORING_ARRAY_ORDER)); | |
| } | |
| public void assertGetRequestEquals(String url, String responseName, Configuration configuration) throws Exception { | |
| String expected = getResponseJson(responseName); | |
| MockHttpServletResponse response = mvc.perform(get(url)) | |
| .andExpect(status().isOk()) | |
| .andReturn().getResponse(); | |
| ObjectMapper objectMapper = new ObjectMapper(); | |
| objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); | |
| log.info("Response: " + response.getContentAsString()); | |
| assertThatJson(objectMapper.readTree(response.getContentAsString())) | |
| .withConfiguration(tmp -> configuration) | |
| .isEqualTo(objectMapper.readTree(expected)); | |
| } | |
| String getResponseJson(String responseName) throws IOException { | |
| Resource resource = new ClassPathResource("response/" + responseName + ".json"); | |
| return Files.lines(resource.getFile().toPath()).collect(Collectors.joining("\n")); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment