Skip to content

Commit

Permalink
Allow resuming traversal after a 204 no content
Browse files Browse the repository at this point in the history
  • Loading branch information
reda-alaoui committed Feb 6, 2025
1 parent e1b4295 commit 3cae26c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/src/main/java/com/cosium/hal_mock_mvc/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ public HalMockMvc createAndShift() throws Exception {
return requestExecutor.assertCreatedAndShift(submit());
}

/** Submit the form by expecting 204 No Content then resume the traversal. */
public HalMockMvc submitAndExpectNoContent() throws Exception {
return requestExecutor.assert204NoContentAndResume(submit());
}

/** Submits the form */
public ResultActions submit() throws Exception {
String contentType = template.representation().contentType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public HalMockMvc assertCreatedAndShift(ResultActions resultActions) throws Exce
return shiftTo(location);
}

public HalMockMvc assert204NoContentAndResume(ResultActions resultActions) throws Exception {
resultActions.andExpect(status().isNoContent());
String requestURI = resultActions.andReturn().getRequest().getRequestURI();
return shiftTo(requestURI);
}

private HalMockMvc shiftTo(String location) {
return HalMockMvc.builder(mockMvc)
.baseUri(location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public interface SubmittableTemplate {
* at the returned Location header.
*/
HalMockMvc createAndShift() throws Exception;

/** Submit the template by expecting 204 No Content then resume the traversal. */
HalMockMvc submitAndExpect204NoContent() throws Exception;
}
14 changes: 14 additions & 0 deletions core/src/main/java/com/cosium/hal_mock_mvc/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public HalMockMvc createAndShift() throws Exception {
return createAndShift(null);
}

@Override
public HalMockMvc submitAndExpect204NoContent() throws Exception {
return submitAndExpect204NoContent(null);
}

/**
* Submits the template by expecting a 201 Created response then begins a new traversal starting
* at the returned Location header.
Expand All @@ -55,6 +60,15 @@ public HalMockMvc createAndShift(String content) throws Exception {
return requestExecutor.assertCreatedAndShift(submit(content));
}

/**
* Submit the template by expecting 204 No Content then resume the traversal.
*
* @param content The content to submit
*/
public HalMockMvc submitAndExpect204NoContent(String content) throws Exception {
return requestExecutor.assert204NoContentAndResume(submit(content));
}

@Override
public ResultActions submit() throws Exception {
return submit(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public ResultActions submit() throws Exception {
public HalMockMvc createAndShift() throws Exception {
return requestExecutor.assertCreatedAndShift(submit());
}

@Override
public HalMockMvc submitAndExpect204NoContent() throws Exception {
return requestExecutor.assert204NoContentAndResume(submit());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cosium.hal_mock_mvc;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.tuple;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.afford;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
Expand Down Expand Up @@ -204,6 +205,48 @@ void test8() throws Exception {
assertThat(myController.personByName).containsKey("john");
}

@Test
@DisplayName("PUT template then GET updated resource")
void test9() throws Exception {

HalMockMvc.builder(mockMvc)
.baseUri(linkTo(methodOn(MyController.class).list()).toUri())
.build()
.follow()
.templates()
.byKey("create")
.createAndShift(JSON.std.composeString().startObject().put("name", "john").end().finish())
.follow()
.templates()
.byKey("changeCity")
.submitAndExpect204NoContent(
JSON.std.composeString().startObject().put("city", "Casablanca").end().finish())
.follow()
.get()
.andExpect(status().isOk())
.andExpect(jsonPath("$.value").value("Casablanca"));
}

@Test
@DisplayName("submitAndExpect204NoContent fails if the response status is not 204")
void test10() throws Exception {

Template template =
HalMockMvc.builder(mockMvc)
.baseUri(linkTo(methodOn(MyController.class).list()).toUri())
.build()
.follow()
.templates()
.byKey("create");

String createCommand =
JSON.std.composeString().startObject().put("name", "john").end().finish();

assertThatThrownBy(() -> template.submitAndExpect204NoContent(createCommand))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Status expected:<204> but was:<201>");
}

@Controller
@RequestMapping("/HalMockMvcFormsTest")
public static class MyController {
Expand Down Expand Up @@ -269,6 +312,15 @@ public ResponseEntity<?> changeCity(
return ResponseEntity.noContent().build();
}

@GetMapping("/{name}/city")
public ResponseEntity<?> getCity(@PathVariable("name") String name) {
PersonResource resource = personByName.get(name);
if (resource == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(Map.of("value", resource.city));
}

@DeleteMapping("/{name}")
public ResponseEntity<?> deleteByName(@PathVariable("name") String name) {
personByName.remove(name);
Expand Down

0 comments on commit 3cae26c

Please sign in to comment.