refactor(tagging): deduplicate tag-dispatch logic across tag controllers (#3594) (#3619)

* refactor(tagging): deduplicate tag-dispatch logic across tag controllers (#3594)

* fix(docs): support superclass REST method extraction in regen_action_docs

- Read inherited JAX-RS methods from superclass when a REST controller extends a base class
- Add test_extract_rest_inherits_methods_from_superclass test
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-09-14 12:52:37 -04:00
1 parent e82836348b
commit 7981e23dcd
6 files changed
+231 -110

No files matched your search

@@ -0,0 +1,73 @@
package io.github.hectorvent.floci.core.common;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
/**
* Base REST controller for tag endpoints mounted at {@code /{arn: .+}}.
*
* <p>Shared between {@link SharedTagsController} (mounted at {@code /tags}) and
* {@link V1TagsController} (mounted at {@code /v1/tags}).
*/
@Produces(MediaType.APPLICATION_JSON)
public abstract class AbstractTagsController {
private final TagDispatcher dispatcher;
protected AbstractTagsController(TagDispatcher dispatcher) {
this.dispatcher = dispatcher;
}
protected TagDispatcher dispatcher() {
return dispatcher;
}
@GET
@Path("/{arn: .+}")
public Response listTags(@Context HttpHeaders headers, @PathParam("arn") String arn) {
return dispatcher.listTagsForArn(headers, arn);
}
@POST
@Path("/{arn: .+}")
@Consumes(MediaType.APPLICATION_JSON)
public Response tagResourcePost(@Context HttpHeaders headers,
@PathParam("arn") String arn,
String body) {
TagHandler handler = dispatcher.resolveHandler(arn);
return dispatcher.tagResourcePost(headers, arn, body,
Response.status(handler.tagResourceSuccessStatus()).build());
}
@PUT
@Path("/{arn: .+}")
@Consumes(MediaType.APPLICATION_JSON)
public Response tagResourcePut(@Context HttpHeaders headers,
@PathParam("arn") String arn,
String body) {
TagHandler handler = dispatcher.resolveHandler(arn);
return dispatcher.tagResourcePut(headers, arn, body,
Response.status(handler.tagResourceSuccessStatus()).build());
}
@DELETE
@Path("/{arn: .+}")
public Response untagResource(@Context HttpHeaders headers,
@Context UriInfo uriInfo,
@PathParam("arn") String arn) {
TagHandler handler = dispatcher.resolveHandler(arn);
return dispatcher.untagResourceForArn(headers, uriInfo, arn,
Response.status(handler.untagResourceSuccessStatus()).build(), handler);
}
}
@@ -7,9 +7,7 @@ import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Context;
@@ -34,75 +32,35 @@ import jakarta.ws.rs.core.UriInfo;
*/
@Path("/tags")
@Produces(MediaType.APPLICATION_JSON)
public class SharedTagsController {
private final TagDispatcher dispatcher;
public class SharedTagsController extends AbstractTagsController {
@Inject
public SharedTagsController(Instance<TagHandler> handlers,
RegionResolver regionResolver,
ObjectMapper objectMapper) {
this.dispatcher = new TagDispatcher(handlers, regionResolver, objectMapper);
super(new TagDispatcher(handlers, regionResolver, objectMapper));
}
@GET
public Response listTagsByQuery(@Context HttpHeaders headers,
@QueryParam("resourceArn") String arn) {
return dispatcher.listTagsForArn(headers, arn);
}
@GET
@Path("/{arn: .+}")
public Response listTags(@Context HttpHeaders headers, @PathParam("arn") String arn) {
return dispatcher.listTagsForArn(headers, arn);
return dispatcher().listTagsForArn(headers, arn);
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response tagResourceByBody(@Context HttpHeaders headers, String body) {
String arn = dispatcher.readResourceArn(body);
TagHandler handler = dispatcher.resolveHandler(arn);
return dispatcher.doTagResource(headers, handler, arn, body,
Response.ok(dispatcher.emptyObject()).build());
}
@POST
@Path("/{arn: .+}")
@Consumes(MediaType.APPLICATION_JSON)
public Response tagResourcePost(@Context HttpHeaders headers,
@PathParam("arn") String arn,
String body) {
TagHandler handler = dispatcher.resolveHandler(arn);
return dispatcher.tagResourcePost(headers, arn, body,
Response.status(handler.tagResourceSuccessStatus()).build());
}
@PUT
@Path("/{arn: .+}")
@Consumes(MediaType.APPLICATION_JSON)
public Response tagResourcePut(@Context HttpHeaders headers,
@PathParam("arn") String arn,
String body) {
TagHandler handler = dispatcher.resolveHandler(arn);
return dispatcher.tagResourcePut(headers, arn, body,
Response.status(handler.tagResourceSuccessStatus()).build());
String arn = dispatcher().readResourceArn(body);
TagHandler handler = dispatcher().resolveHandler(arn);
return dispatcher().doTagResource(headers, handler, arn, body,
Response.ok(dispatcher().emptyObject()).build());
}
@DELETE
public Response untagResourceByQuery(@Context HttpHeaders headers,
@Context UriInfo uriInfo,
@QueryParam("resourceArn") String arn) {
return dispatcher.untagResourceForArn(headers, uriInfo, arn,
Response.ok(dispatcher.emptyObject()).build());
}
@DELETE
@Path("/{arn: .+}")
public Response untagResource(@Context HttpHeaders headers,
@Context UriInfo uriInfo,
@PathParam("arn") String arn) {
TagHandler handler = dispatcher.resolveHandler(arn);
return dispatcher.untagResourceForArn(headers, uriInfo, arn,
Response.status(handler.untagResourceSuccessStatus()).build(), handler);
return dispatcher().untagResourceForArn(headers, uriInfo, arn,
Response.ok(dispatcher().emptyObject()).build());
}
}
@@ -3,19 +3,9 @@ package io.github.hectorvent.floci.core.common;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
/**
* Dispatcher for the AWS services that put their tag endpoints on {@code /v1/tags/{resourceArn}}
@@ -28,52 +18,12 @@ import jakarta.ws.rs.core.UriInfo;
*/
@Path("/v1/tags")
@Produces(MediaType.APPLICATION_JSON)
public class V1TagsController {
private final TagDispatcher dispatcher;
public class V1TagsController extends AbstractTagsController {
@Inject
public V1TagsController(@V1Tags Instance<TagHandler> handlers,
RegionResolver regionResolver,
ObjectMapper objectMapper) {
this.dispatcher = new TagDispatcher(handlers, regionResolver, objectMapper);
}
@GET
@Path("/{arn: .+}")
public Response listTags(@Context HttpHeaders headers, @PathParam("arn") String arn) {
return dispatcher.listTagsForArn(headers, arn);
}
@POST
@Path("/{arn: .+}")
@Consumes(MediaType.APPLICATION_JSON)
public Response tagResourcePost(@Context HttpHeaders headers,
@PathParam("arn") String arn,
String body) {
TagHandler handler = dispatcher.resolveHandler(arn);
return dispatcher.tagResourcePost(headers, arn, body,
Response.status(handler.tagResourceSuccessStatus()).build());
}
@PUT
@Path("/{arn: .+}")
@Consumes(MediaType.APPLICATION_JSON)
public Response tagResourcePut(@Context HttpHeaders headers,
@PathParam("arn") String arn,
String body) {
TagHandler handler = dispatcher.resolveHandler(arn);
return dispatcher.tagResourcePut(headers, arn, body,
Response.status(handler.tagResourceSuccessStatus()).build());
}
@DELETE
@Path("/{arn: .+}")
public Response untagResource(@Context HttpHeaders headers,
@Context UriInfo uriInfo,
@PathParam("arn") String arn) {
TagHandler handler = dispatcher.resolveHandler(arn);
return dispatcher.untagResourceForArn(headers, uriInfo, arn,
Response.status(handler.untagResourceSuccessStatus()).build(), handler);
super(new TagDispatcher(handlers, regionResolver, objectMapper));
}
}
@@ -0,0 +1,97 @@
package io.github.hectorvent.floci.core.common;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class AbstractTagsControllerTest {
private TagDispatcher dispatcher;
private TagHandler handler;
private AbstractTagsController controller;
private static class TestTagsController extends AbstractTagsController {
TestTagsController(TagDispatcher dispatcher) {
super(dispatcher);
}
}
@BeforeEach
void setUp() {
dispatcher = mock(TagDispatcher.class);
handler = mock(TagHandler.class);
controller = new TestTagsController(dispatcher);
}
@Test
void listTagsDelegatesToDispatcher() {
HttpHeaders headers = mock(HttpHeaders.class);
String arn = "arn:aws:apigateway:us-east-1::/restapis/123";
Response expected = Response.ok().build();
when(dispatcher.listTagsForArn(headers, arn)).thenReturn(expected);
Response actual = controller.listTags(headers, arn);
assertEquals(expected, actual);
verify(dispatcher).listTagsForArn(headers, arn);
}
@Test
void tagResourcePostDispatchesWithHandlerSuccessStatus() {
HttpHeaders headers = mock(HttpHeaders.class);
String arn = "arn:aws:kafka:us-east-1:000000000000:cluster/test/id";
String body = "{\"tags\":{\"env\":\"test\"}}";
when(dispatcher.resolveHandler(arn)).thenReturn(handler);
when(handler.tagResourceSuccessStatus()).thenReturn(204);
Response expected = Response.noContent().build();
when(dispatcher.tagResourcePost(eq(headers), eq(arn), eq(body), any(Response.class))).thenReturn(expected);
Response actual = controller.tagResourcePost(headers, arn, body);
assertEquals(expected, actual);
verify(dispatcher).tagResourcePost(eq(headers), eq(arn), eq(body), any(Response.class));
}
@Test
void tagResourcePutDispatchesWithHandlerSuccessStatus() {
HttpHeaders headers = mock(HttpHeaders.class);
String arn = "arn:aws:apigateway:us-east-1::/restapis/123";
String body = "{\"tags\":{\"env\":\"test\"}}";
when(dispatcher.resolveHandler(arn)).thenReturn(handler);
when(handler.tagResourceSuccessStatus()).thenReturn(200);
Response expected = Response.ok().build();
when(dispatcher.tagResourcePut(eq(headers), eq(arn), eq(body), any(Response.class))).thenReturn(expected);
Response actual = controller.tagResourcePut(headers, arn, body);
assertEquals(expected, actual);
verify(dispatcher).tagResourcePut(eq(headers), eq(arn), eq(body), any(Response.class));
}
@Test
void untagResourceDispatchesWithHandlerSuccessStatus() {
HttpHeaders headers = mock(HttpHeaders.class);
UriInfo uriInfo = mock(UriInfo.class);
String arn = "arn:aws:apigateway:us-east-1::/restapis/123";
when(dispatcher.resolveHandler(arn)).thenReturn(handler);
when(handler.untagResourceSuccessStatus()).thenReturn(204);
Response expected = Response.noContent().build();
when(dispatcher.untagResourceForArn(eq(headers), eq(uriInfo), eq(arn), any(Response.class), eq(handler)))
.thenReturn(expected);
Response actual = controller.untagResource(headers, uriInfo, arn);
assertEquals(expected, actual);
verify(dispatcher).untagResourceForArn(eq(headers), eq(uriInfo), eq(arn), any(Response.class), eq(handler));
}
}