Skip to content

Commit

Permalink
Revert "Migrate MVC assertions to Spring Framework 6.2 M2."
Browse files Browse the repository at this point in the history
This reverts commit 8806015.

$ Conflicts:
$	server/pom.xml
  • Loading branch information
odrotbohm committed Jun 19, 2024
1 parent a85064d commit 0b1f2e7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
import org.springframework.hateoas.client.LinkDiscoverer;
import org.springframework.hateoas.client.LinkDiscoverers;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.assertj.MockMvcTester;
import org.springframework.test.web.servlet.assertj.MvcTestResult;
import org.springframework.test.web.servlet.assertj.AssertableMockMvc;
import org.springframework.test.web.servlet.assertj.AssertableMvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.Assert;
Expand All @@ -46,12 +45,12 @@ public abstract class AbstractWebIntegrationTest {
@Autowired WebApplicationContext context;
@Autowired LinkDiscoverers links;

protected MockMvcTester mvc;
protected AssertableMockMvc mvc;

@BeforeEach
void setUp() {

this.mvc = MockMvcTester.create(MockMvcBuilders.webAppContextSetup(context).//
this.mvc = AssertableMockMvc.create(MockMvcBuilders.webAppContextSetup(context).//
defaultRequest(MockMvcRequestBuilders.get("/").locale(Locale.US)).//
build());
}
Expand All @@ -63,11 +62,11 @@ void setUp() {
* @param rel must not be {@literal null}.
* @return will never be {@literal null}.
*/
protected Condition<MvcTestResult> linkWithRel(LinkRelation rel) {
protected Condition<AssertableMvcResult> linkWithRel(LinkRelation rel) {

Assert.notNull(rel, "LinkRelation must not be null!");

return new Condition<>(it -> hasLink(it.getMvcResult(), rel), "Expected to find link with relation %s!", rel);
return new Condition<>(it -> hasLink(it, rel), "Expected to find link with relation %s!", rel);
}

@SuppressWarnings("null")
Expand All @@ -77,7 +76,7 @@ protected LinkDiscoverer getDiscovererFor(MockHttpServletResponse response) {

@SneakyThrows
@SuppressWarnings("null")
private boolean hasLink(MvcResult result, LinkRelation rel) {
private boolean hasLink(AssertableMvcResult result, LinkRelation rel) {

var response = result.getResponse();
var content = response.getContentAsString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class OrderResourceIntegrationTest extends AbstractWebIntegrationTest {
@Test
void exposesOrdersResourceViaRootResource() throws Exception {

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

assertThat(result).hasStatus(HttpStatus.OK);
assertThat(result).contentType().isCompatibleWith(MediaTypes.HAL_JSON);
assertThat(result).bodyJson().hasPath("$._links.restbucks:orders.href").isNotNull();
result.hasStatus(HttpStatus.OK);
result.contentType().isCompatibleWith(MediaTypes.HAL_JSON);
result.body().jsonPath().extractingPath("$._links.restbucks:orders.href").isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private MockHttpServletResponse accessRootResource() throws Exception {
.hasStatus(HttpStatus.OK)
.has(linkWithRel(ORDERS_REL));

return response.getMvcResult().getResponse();
return response.getResponse();
}

/**
Expand All @@ -154,7 +154,7 @@ private MockHttpServletResponse createNewOrder(MockHttpServletResponse source) t
var drinksTemplate = parse.read("$._templates.default.properties[0].options.link.href", String.class);
var drinksOptionsUri = Link.of(drinksTemplate).expand().getHref();
var drinksOptionsResponse = mvc.perform(get(drinksOptionsUri))
.getMvcResult().getResponse().getContentAsString();
.getResponse().getContentAsString();
var drinkUri = JsonPath.parse(drinksOptionsResponse).read("$._embedded.drinks[0].value", String.class);

// Select location
Expand All @@ -172,8 +172,7 @@ private MockHttpServletResponse createNewOrder(MockHttpServletResponse source) t
.hasStatus(HttpStatus.CREATED)
.headers().containsHeader(HttpHeaders.LOCATION);

return mvc.perform(get(response.getMvcResult().getResponse().getHeader(HttpHeaders.LOCATION)))
.getMvcResult().getResponse();
return mvc.perform(get(response.getResponse().getHeader(HttpHeaders.LOCATION))).getResponse();
}

/**
Expand All @@ -191,11 +190,11 @@ private MockHttpServletResponse discoverOrdersResource(MockHttpServletResponse s
LOG.info("Root resource returned: " + content);
LOG.info(String.format("Found orders link pointing to %s… Following…", ordersLink));

var result = mvc.perform(get(ordersLink.expand().getHref()));
var perform = mvc.perform(get(ordersLink.expand().getHref()));

assertThat(result).hasStatus(HttpStatus.OK);
assertThat(perform).hasStatus(HttpStatus.OK);

var response = result.getMvcResult().getResponse();
var response = perform.getResponse();

LOG.info("Found orders: " + response.getContentAsString());
return response;
Expand Down Expand Up @@ -229,15 +228,15 @@ private MockHttpServletResponse accessFirstOrder(MockHttpServletResponse source)
LOG.info(String.format("Picking first order using JSONPath expression %s…", FIRST_ORDER_EXPRESSION));
LOG.info(String.format("Discovered self link pointing to %s… Following", orderLink));

var result = mvc.perform(get(orderLink.getHref()));
var perform = mvc.perform(get(orderLink.getHref()));

assertThat(result)
assertThat(perform)
.has(linkWithRel(IanaLinkRelations.SELF))
.has(linkWithRel(CANCEL_REL))
.has(linkWithRel(UPDATE_REL))
.has(linkWithRel(PAYMENT_REL));

return result.getMvcResult().getResponse();
return perform.getResponse();
}

/**
Expand Down Expand Up @@ -280,7 +279,7 @@ private MockHttpServletResponse triggerPayment(MockHttpServletResponse response)
assertThat(mvc.perform(delete(selfLink.getHref())))
.hasStatus(HttpStatus.METHOD_NOT_ALLOWED);

return action.getMvcResult().getResponse();
return action.getResponse();
}

/**
Expand Down Expand Up @@ -314,7 +313,7 @@ private MockHttpServletResponse pollUntilOrderHasReceiptLink(MockHttpServletResp
LOG.info("Poll state of order until receipt is ready…");

var action = mvc.perform(get(orderLink.expand().getHref()).headers(headers));
pollResponse = action.getMvcResult().getResponse();
pollResponse = action.getResponse();

var status = pollResponse.getStatus();
etag = pollResponse.getHeader("ETag");
Expand Down Expand Up @@ -359,7 +358,7 @@ private MockHttpServletResponse takeReceipt(MockHttpServletResponse response) th

assertThat(result).hasStatus(HttpStatus.OK);

var receiptResponse = result.getMvcResult().getResponse();
var receiptResponse = result.getResponse();

LOG.info("Accessing receipt, got:" + receiptResponse.getContentAsString());
LOG.info("Taking receipt…");
Expand All @@ -368,7 +367,7 @@ private MockHttpServletResponse takeReceipt(MockHttpServletResponse response) th

assertThat(result).hasStatus(HttpStatus.OK);

return result.getMvcResult().getResponse();
return result.getResponse();
}

/**
Expand All @@ -389,9 +388,9 @@ private void verifyOrderTaken(MockHttpServletResponse response) throws Exception
.doesNotHave(linkWithRel(UPDATE_REL))
.doesNotHave(linkWithRel(CANCEL_REL))
.doesNotHave(linkWithRel(PAYMENT_REL))
.bodyJson().hasPathSatisfying("$.status", it -> assertThat(it).isEqualTo("Delivered"));
.body().jsonPath().extractingPath("$.status").isEqualTo("Delivered");

LOG.info("Final order state: " + result.getMvcResult().getResponse().getContentAsString());
LOG.info("Final order state: " + result.getResponse().getContentAsString());
}

/**
Expand Down

0 comments on commit 0b1f2e7

Please sign in to comment.