From 0cb0f9aeda4a478dbc366931dc5ffe8d3b5814e7 Mon Sep 17 00:00:00 2001 From: Lars Vierbergen Date: Fri, 22 Nov 2024 14:40:40 +0100 Subject: [PATCH] Add automation:registrations link on API root --- .../automations/AutomationLinkRelations.java | 1 + .../ContentGridAutomationsConfiguration.java | 19 +++++++++++++++++++ .../rest/AutomationsRestControllerTest.java | 19 ++++++++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/contentgrid-automations-rest/src/main/java/com/contentgrid/spring/automations/AutomationLinkRelations.java b/contentgrid-automations-rest/src/main/java/com/contentgrid/spring/automations/AutomationLinkRelations.java index 880d34a7..8cbe2d9b 100644 --- a/contentgrid-automations-rest/src/main/java/com/contentgrid/spring/automations/AutomationLinkRelations.java +++ b/contentgrid-automations-rest/src/main/java/com/contentgrid/spring/automations/AutomationLinkRelations.java @@ -14,5 +14,6 @@ public class AutomationLinkRelations { public static final LinkRelation ANNOTATION = HalLinkRelation.curied(CURIE, "annotation"); public static final String ANNOTATION_STRING = CURIE+":annotation"; public static final LinkRelation TARGET_ENTITY = HalLinkRelation.curied(CURIE, "target-entity"); + public static final LinkRelation REGISTRATIONS = HalLinkRelation.curied(CURIE, "registrations"); } diff --git a/contentgrid-automations-rest/src/main/java/com/contentgrid/spring/automations/ContentGridAutomationsConfiguration.java b/contentgrid-automations-rest/src/main/java/com/contentgrid/spring/automations/ContentGridAutomationsConfiguration.java index bab935bd..e59c776b 100644 --- a/contentgrid-automations-rest/src/main/java/com/contentgrid/spring/automations/ContentGridAutomationsConfiguration.java +++ b/contentgrid-automations-rest/src/main/java/com/contentgrid/spring/automations/ContentGridAutomationsConfiguration.java @@ -1,5 +1,8 @@ package com.contentgrid.spring.automations; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; + import com.contentgrid.spring.automations.rest.AutomationAnnotationRepresentationModelAssembler; import com.contentgrid.spring.automations.rest.AutomationRepresentationModelAssembler; import com.contentgrid.spring.automations.rest.AutomationsRestController; @@ -9,6 +12,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.io.ResourceLoader; +import org.springframework.data.rest.webmvc.RepositoryLinksResource; +import org.springframework.hateoas.server.RepresentationModelProcessor; @Import({ AutomationRepresentationModelAssembler.class, @@ -37,4 +42,18 @@ CurieProviderCustomizer automationCurieProvider() { return CurieProviderCustomizer.register(AutomationLinkRelations.CURIE, AutomationLinkRelations.TEMPLATE); } + @Bean + RepresentationModelProcessor automationRepositoryLinksRepresentationModelProcessor() { + // This must be a class instead of a lambda so the generic parameter can be determined by spring-hateoas + return new RepresentationModelProcessor() { + @Override + public RepositoryLinksResource process(RepositoryLinksResource model) { + return model.add( + linkTo(methodOn(AutomationsRestController.class).getAutomations()) + .withRel(AutomationLinkRelations.REGISTRATIONS) + ); + } + }; + } + } diff --git a/contentgrid-automations-rest/src/test/java/com/contentgrid/spring/automations/rest/AutomationsRestControllerTest.java b/contentgrid-automations-rest/src/test/java/com/contentgrid/spring/automations/rest/AutomationsRestControllerTest.java index 645e2dfb..beef767d 100644 --- a/contentgrid-automations-rest/src/test/java/com/contentgrid/spring/automations/rest/AutomationsRestControllerTest.java +++ b/contentgrid-automations-rest/src/test/java/com/contentgrid/spring/automations/rest/AutomationsRestControllerTest.java @@ -117,6 +117,23 @@ void setup() { .build()); } + @Test + void getRoot_containsLink() throws Exception { + mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON) + .header("X-ABAC-Context", headerEncode(DEFAULT_POLICY)) + ) + .andExpect(status().isOk()) + .andExpect(content().json(""" + { + _links: { + "automation:registrations": { + href: "http://localhost/.contentgrid/automations" + } + } + } + """)); + } + @Test void getAutomations_http200() throws Exception { mockMvc.perform(get("/.contentgrid/automations") @@ -249,4 +266,4 @@ void getAutomation_noAccess_http404() throws Exception { .andExpect(status().isNotFound()); } -} \ No newline at end of file +}