Skip to content

Commit

Permalink
Add automation:registrations link on API root
Browse files Browse the repository at this point in the history
  • Loading branch information
vierbergenlars committed Nov 25, 2024
1 parent 72784e9 commit 2654101
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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");

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -37,4 +42,18 @@ CurieProviderCustomizer automationCurieProvider() {
return CurieProviderCustomizer.register(AutomationLinkRelations.CURIE, AutomationLinkRelations.TEMPLATE);
}

@Bean
RepresentationModelProcessor<RepositoryLinksResource> automationRepositoryLinksRepresentationModelProcessor() {
// This must be a class instead of a lambda so the generic parameter can be determined by spring-hateoas
return new RepresentationModelProcessor<RepositoryLinksResource>() {
@Override
public RepositoryLinksResource process(RepositoryLinksResource model) {
return model.add(
linkTo(methodOn(AutomationsRestController.class).getAutomations())
.withRel(AutomationLinkRelations.REGISTRATIONS)
);
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;

Expand Down Expand Up @@ -116,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")
Expand Down Expand Up @@ -248,4 +266,4 @@ void getAutomation_noAccess_http404() throws Exception {
.andExpect(status().isNotFound());
}

}
}

0 comments on commit 2654101

Please sign in to comment.