Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.springframework.context.MessageSourceResolvable;
Expand Down Expand Up @@ -56,16 +57,19 @@ public Map<String, HalFormsTemplate> findTemplates(RepresentationModel<?> resour
Map<String, HalFormsTemplate> templates = new HashMap<>();
Link selfLink = resource.getLink(IanaLinkRelations.SELF).orElse(null);

resource.getLinks().stream() //
Supplier<Stream<HalFormsAffordanceModel>> streamOfAffordances = () -> resource.getLinks().stream() //
.flatMap(it -> it.getAffordances().stream()) //
.map(it -> it.getAffordanceModel(MediaTypes.HAL_FORMS_JSON)) //
.peek(it -> {
Assert.notNull(it, "No HAL Forms affordance model found but expected!");
}) //
.map(HalFormsAffordanceModel.class::cast) //
.filter(it -> !it.hasHttpMethod(HttpMethod.GET)) //
.map(HalFormsAffordanceModel.class::cast)
.filter(it -> !it.hasHttpMethod(HttpMethod.GET));

long numberOfAffordances = streamOfAffordances.get().count();

streamOfAffordances.get()
.forEach(it -> {

HalFormsTemplate template = HalFormsTemplate.forMethod(it.getHttpMethod()) //
.withProperties(factory.createProperties(it))
.withContentType(it.getInput().getPrimaryMediaType());
Expand All @@ -75,11 +79,11 @@ public Map<String, HalFormsTemplate> findTemplates(RepresentationModel<?> resour
if (selfLink == null || !target.equals(selfLink.getHref())) {
template = template.withTarget(target);
}

template = applyTo(template, TemplateTitle.of(it, templates.isEmpty()));
templates.put(templates.isEmpty() ? "default" : it.getName(), template);
templates.put(templates.isEmpty() ?
(numberOfAffordances == 1 ? "default" : it.getName()) : it.getName(), template);
});

return templates;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ void singleEmployee() throws Exception {
.andExpect(jsonPath("$._links['employees'].href", is("http://localhost/employees")))

.andExpect(jsonPath("$._templates.*", hasSize(2)))
.andExpect(jsonPath("$._templates['default'].method", is("put")))
.andExpect(jsonPath("$._templates['default'].properties[0].name", is("name")))
.andExpect(jsonPath("$._templates['default'].properties[0].required").value(true))
.andExpect(jsonPath("$._templates['default'].properties[1].name", is("role")))
.andExpect(jsonPath("$._templates['default'].properties[1].required").doesNotExist())
.andExpect(jsonPath("$._templates['updateEmployee'].method", is("put")))
.andExpect(jsonPath("$._templates['updateEmployee'].properties[0].name", is("name")))
.andExpect(jsonPath("$._templates['updateEmployee'].properties[0].required").value(true))
.andExpect(jsonPath("$._templates['updateEmployee'].properties[1].name", is("role")))
.andExpect(jsonPath("$._templates['updateEmployee'].properties[1].required").doesNotExist())

.andExpect(jsonPath("$._templates['partiallyUpdateEmployee'].method", is("patch")))
.andExpect(jsonPath("$._templates['partiallyUpdateEmployee'].properties[0].name", is("name")))
Expand Down