Skip to content

Commit

Permalink
Revert "Migrate to Spring 6.2's AssertJ based MVC assertions."
Browse files Browse the repository at this point in the history
This reverts commit 6e547db.
  • Loading branch information
odrotbohm committed Jun 19, 2024
1 parent 0b1f2e7 commit 2990656
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2024 the original author or authors.
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,23 +15,26 @@
*/
package org.springsource.restbucks;

import lombok.SneakyThrows;
import static org.assertj.core.api.Assertions.*;

import lombok.RequiredArgsConstructor;

import java.util.Locale;
import java.util.Optional;

import org.assertj.core.api.Condition;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkRelation;
import org.springframework.hateoas.client.LinkDiscoverer;
import org.springframework.hateoas.client.LinkDiscoverers;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.assertj.AssertableMockMvc;
import org.springframework.test.web.servlet.assertj.AssertableMvcResult;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;

/**
Expand All @@ -45,43 +48,66 @@ public abstract class AbstractWebIntegrationTest {
@Autowired WebApplicationContext context;
@Autowired LinkDiscoverers links;

protected AssertableMockMvc mvc;
protected MockMvc mvc;

@BeforeEach
void setUp() {

this.mvc = AssertableMockMvc.create(MockMvcBuilders.webAppContextSetup(context).//
mvc = MockMvcBuilders.webAppContextSetup(context).//
defaultRequest(MockMvcRequestBuilders.get("/").locale(Locale.US)).//
build());
build();
}

/**
* Creates a AssertJ {@link Condition} that checks for the presence of a {@link Link} with the given
* {@link LinkRelation}.
* Creates a {@link ResultMatcher} that checks for the presence of a link with the given rel.
*
* @param rel must not be {@literal null}.
* @return will never be {@literal null}.
* @param rel
* @return
*/
protected Condition<AssertableMvcResult> linkWithRel(LinkRelation rel) {

Assert.notNull(rel, "LinkRelation must not be null!");
protected ResultMatcher linkWithRelIsPresent(LinkRelation rel) {
return new LinkWithRelMatcher(rel, true);
}

return new Condition<>(it -> hasLink(it, rel), "Expected to find link with relation %s!", rel);
/**
* Creates a {@link ResultMatcher} that checks for the non-presence of a link with the given rel.
*
* @param rel
* @return
*/
protected ResultMatcher linkWithRelIsNotPresent(LinkRelation rel) {
return new LinkWithRelMatcher(rel, false);
}

@SuppressWarnings("null")
protected LinkDiscoverer getDiscovererFor(MockHttpServletResponse response) {
return links.getRequiredLinkDiscovererFor(response.getContentType());
}

@SneakyThrows
@SuppressWarnings("null")
private boolean hasLink(AssertableMvcResult result, LinkRelation rel) {
@RequiredArgsConstructor
private class LinkWithRelMatcher implements ResultMatcher {

private final LinkRelation rel;
private final boolean present;

/*
* (non-Javadoc)
* @see org.springframework.test.web.servlet.ResultMatcher#match(org.springframework.test.web.servlet.MvcResult)
*/
@Override
public void match(MvcResult result) throws Exception {

MockHttpServletResponse response = result.getResponse();
String content = response.getContentAsString();
LinkDiscoverer discoverer = links.getRequiredLinkDiscovererFor(response.getContentType());

Optional<Link> link = discoverer.findLinkWithRel(rel, content);

assertThat(link).matches(it -> it.isPresent() == present, getMessage(link));
}

var response = result.getResponse();
var content = response.getContentAsString();
var discoverer = links.getRequiredLinkDiscovererFor(response.getContentType());
private String getMessage(Optional<Link> link) {

return discoverer.findLinkWithRel(rel, content).isPresent();
return String.format("Expected to %s link with relation %s, but found %s!",
present ? "find" : "not find", rel, present ? link.get() : "none");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2024 the original author or authors.
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,12 +15,12 @@
*/
package org.springsource.restbucks.order.web;

import static org.assertj.core.api.Assertions.*;
import static org.hamcrest.CoreMatchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import org.junit.jupiter.api.Test;
import org.springframework.hateoas.MediaTypes;
import org.springframework.http.HttpStatus;
import org.springsource.restbucks.AbstractWebIntegrationTest;

/**
Expand All @@ -33,10 +33,9 @@ class OrderResourceIntegrationTest extends AbstractWebIntegrationTest {
@Test
void exposesOrdersResourceViaRootResource() throws Exception {

var result = assertThat(mvc.perform(get("/"))); //

result.hasStatus(HttpStatus.OK);
result.contentType().isCompatibleWith(MediaTypes.HAL_JSON);
result.body().jsonPath().extractingPath("$._links.restbucks:orders.href").isNotNull();
mvc.perform(get("/")) //
.andExpect(status().isOk()) //
.andExpect(content().contentTypeCompatibleWith(MediaTypes.HAL_JSON)) //
.andExpect(jsonPath("$._links.restbucks:orders.href", notNullValue()));
}
}
Loading

0 comments on commit 2990656

Please sign in to comment.