All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cosium.hal_mock_mvc.Templates Maven / Gradle / Ivy

The newest version!
package com.cosium.hal_mock_mvc;

import static java.util.Objects.requireNonNull;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.hateoas.MediaTypes;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;

/**
 * @author Réda Housni Alaoui
 */
public class Templates {

  private final RequestExecutor requestExecutor;
  private final ObjectMapper objectMapper;
  private final HalFormsBody body;

  Templates(RequestExecutor requestExecutor, ResultActions resultActions) throws Exception {
    this.requestExecutor = requireNonNull(requestExecutor);

    resultActions.andExpect(content().contentType(MediaTypes.HAL_FORMS_JSON));

    MvcResult mvcResult = resultActions.andReturn();
    MockHttpServletResponse response = mvcResult.getResponse();
    int responseStatus = response.getStatus();
    String jsonBody = response.getContentAsString();
    if (responseStatus < 200 || responseStatus >= 400) {
      throw new IllegalStateException(
          "GET on "
              + mvcResult.getRequest().getRequestURI()
              + " failed with code "
              + responseStatus
              + " and body '"
              + jsonBody
              + "'");
    }
    objectMapper =
        new ObjectMapper()
            .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
            .registerModule(new JacksonModule());
    body = objectMapper.readValue(jsonBody, HalFormsBody.class);
  }

  public Optional