Skip to content

Commit

Permalink
chore: add test for affordances (Spring-HATEOAS, HAL-FORMS)
Browse files Browse the repository at this point in the history
  • Loading branch information
toedter committed Mar 12, 2022
1 parent e18109b commit a6837c2
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public static Object write(Object bean) {
try {
return halFormsTemplatePropertyWriter.value(bean, null, null);
} catch (Exception e) {
e.printStackTrace();
throw new IllegalArgumentException("Cannot write HAL-FORMS template.", e);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@
import org.junit.jupiter.api.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.hateoas.*;
import org.springframework.hateoas.mediatype.Affordances;
import org.springframework.http.HttpMethod;

import javax.persistence.Id;
import java.io.File;
import java.time.Instant;
import java.util.*;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@DisplayName("Jackson2JsonApi Integration Test")
Expand Down Expand Up @@ -73,8 +76,8 @@ class Movie {
private final String title = "Star Wars";
}

String jsonMovie = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(jsonMovie, "movieEntityModel.json");
String movieJson = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(movieJson, "movieEntityModel.json");
}

@Test
Expand All @@ -89,8 +92,8 @@ public String retrieveMyId() {
}
}

String jsonMovie = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(jsonMovie, "movieEntityModel.json");
String movieJson = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(movieJson, "movieEntityModel.json");
}

@Test
Expand All @@ -105,8 +108,8 @@ public String getMyId() {
}
}

String jsonMovie = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(jsonMovie, "movieEntityModel.json");
String movieJson = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(movieJson, "movieEntityModel.json");
}

@Test
Expand All @@ -121,8 +124,8 @@ public String getId() {
}
}

String jsonMovie = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(jsonMovie, "movieEntityModel.json");
String movieJson = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(movieJson, "movieEntityModel.json");
}

@Test
Expand All @@ -143,8 +146,8 @@ public String getId() {
}
}

String jsonMovie = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(jsonMovie, "movieEntityModel.json");
String movieJson = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(movieJson, "movieEntityModel.json");
}

@Test
Expand All @@ -156,8 +159,8 @@ class Movie {
private final String title = "Star Wars";
}

String jsonMovie = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(jsonMovie, "movieEntityModel.json");
String movieJson = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(movieJson, "movieEntityModel.json");
}

@Test
Expand All @@ -172,29 +175,29 @@ class Movie {
private final String title = "Star Wars";
}

String jsonMovie = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(jsonMovie, "movieEntityModel.json");
String movieJson = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(movieJson, "movieEntityModel.json");
}

@Test
void should_serialize_entity_model_with_annotated_jsonapi_id_and_type_and_meta_fields() throws Exception {
String jsonMovie = mapper.writeValueAsString(
String movieJson = mapper.writeValueAsString(
EntityModel.of(new MovieWithAnnotations("1", "my-movies", "metaValue", "Star Wars")));
compareWithFile(jsonMovie, "movieEntityModelWithThreeAnnotations.json");
compareWithFile(movieJson, "movieEntityModelWithThreeAnnotations.json");
}

@Test
void should_serialize_entity_model_with_annotated_jsonapi_id_and_type_methods() throws Exception {
String jsonMovie = mapper.writeValueAsString(
String movieJson = mapper.writeValueAsString(
EntityModel.of(new MovieWithGetters("1", "Star Wars", "my-movies", "metaValue")));
compareWithFile(jsonMovie, "movieEntityModelWithThreeAnnotations.json");
compareWithFile(movieJson, "movieEntityModelWithThreeAnnotations.json");
}

@Test
void should_serialize_entity_model_with_annotated_type_on_class() throws Exception {
String jsonMovie = mapper.writeValueAsString(
String movieJson = mapper.writeValueAsString(
EntityModel.of(new MovieDerivedWithTypeForClass("1", "Star Wars")));
compareWithFile(jsonMovie, "movieEntityModelWithAnnotations.json");
compareWithFile(movieJson, "movieEntityModelWithAnnotations.json");
}

@Test
Expand Down Expand Up @@ -981,8 +984,8 @@ class Movie {
private String type = "MyObjectType";
}

String jsonMovie = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(jsonMovie, "movieWithTypeAttribute.json");
String movieJson = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(movieJson, "movieWithTypeAttribute.json");
}

@Test
Expand Down Expand Up @@ -1014,8 +1017,8 @@ class Movie {
private String metaProperty = "metaValue";
}

String jsonMovie = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(jsonMovie, "movieEntityModelWithMeta.json");
String movieJson = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(movieJson, "movieEntityModelWithMeta.json");
}

@Test
Expand All @@ -1030,8 +1033,8 @@ public String getMetaProperty() {
}
}

String jsonMovie = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(jsonMovie, "movieEntityModelWithMeta.json");
String movieJson = mapper.writeValueAsString(EntityModel.of(new Movie()));
compareWithFile(movieJson, "movieEntityModelWithMeta.json");
}

@Test
Expand All @@ -1054,6 +1057,38 @@ void should_deserialize_jsonapimeta_method_annotation() throws Exception {
assertThat(entityModel.getContent().getMetaProperty()).isEqualTo("metaValue");
}

@Test
void should_serialize_affordance_with_proprietary_format() throws Exception {
Link link = Affordances.of(Link.of("/"))
.afford(HttpMethod.POST)
.withInputAndOutput(Movie.class) //
.withName("create-movie") //
.toLink();

RepresentationModel<?> movieModel = CollectionModel.of(Collections.singletonList(new Movie("1", "New Movie")), link);

mapper = createObjectMapper(new JsonApiConfiguration()
.withAffordancesRenderedAsLinkMeta(JsonApiConfiguration.AffordanceType.SPRING_HATEOAS));
String moviesJson = mapper.writeValueAsString(movieModel);
compareWithFile(moviesJson, "moviesCollectionModelWithAffordances.json");
}

@Test
void should_serialize_affordance_with_hal_forms_format() throws Exception {
Link link = Affordances.of(Link.of("/"))
.afford(HttpMethod.POST)
.withInputAndOutput(Movie.class) //
.withName("create-movie") //
.toLink();

RepresentationModel<?> movieModel = CollectionModel.of(Collections.singletonList(new Movie("1", "New Movie")), link);

mapper = createObjectMapper(new JsonApiConfiguration()
.withAffordancesRenderedAsLinkMeta(JsonApiConfiguration.AffordanceType.HAL_FORMS));
String moviesJson = mapper.writeValueAsString(movieModel);
compareWithFile(moviesJson, "moviesCollectionModelWithHalFormsAffordances.json");
}

private void compareWithFile(String json, String fileName) throws Exception {
File file = new ClassPathResource(fileName, getClass()).getFile();
ObjectMapper objectMapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"data": [
{
"id": "1",
"type": "movies",
"attributes": {
"title": "New Movie"
}
}
],
"links": {
"self": {
"href": "/",
"meta": {
"affordances": [
{
"name": "create-movie",
"link": {
"rel": "self",
"href": "/"
},
"httpMethod": "POST",
"inputProperties": [
{
"name": "id",
"type": "text"
},
{
"name": "title",
"type": "text"
}
]
}
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"data": [
{
"id": "1",
"type": "movies",
"attributes": {
"title": "New Movie"
}
}
],
"links": {
"self": {
"href": "/",
"meta": {
"_templates": {
"default": {
"method": "POST",
"properties": [
{
"name": "id",
"type": "text"
},
{
"name": "title",
"type": "text"
}
]
}
}
}
}
}
}

0 comments on commit a6837c2

Please sign in to comment.