|
1 | 1 | package org.tdl.vireo.model; |
2 | 2 |
|
| 3 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
3 | 6 | import java.util.stream.Stream; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Test; |
4 | 9 | import org.junit.jupiter.params.provider.Arguments; |
5 | 10 | import org.mockito.InjectMocks; |
| 11 | +import org.tdl.vireo.model.response.Views; |
| 12 | + |
| 13 | +import com.fasterxml.jackson.databind.MapperFeature; |
| 14 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 15 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 16 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
6 | 17 |
|
7 | 18 | public class CustomActionDefinitionTest extends AbstractModelCustomMethodTest<CustomActionDefinition> { |
8 | 19 |
|
@@ -43,4 +54,62 @@ private static Stream<Arguments> getParameterMethodStream() { |
43 | 54 | ); |
44 | 55 | } |
45 | 56 |
|
| 57 | + /** |
| 58 | + * Regression test for GitHub issue #2104. |
| 59 | + * |
| 60 | + * Verifies that {@code CustomActionDefinition.position} is included in the |
| 61 | + * JSON output when serialized under {@link Views.SubmissionList} (and its |
| 62 | + * sub-views such as {@link Views.SubmissionIndividualActionLogs}). |
| 63 | + * |
| 64 | + * The application sets {@code jackson.mapper.default-view-inclusion: false}, |
| 65 | + * so any field without a matching {@code @JsonView} annotation is silently |
| 66 | + * excluded when a view is active. The fix overrides {@code getPosition()} in |
| 67 | + * {@code CustomActionDefinition} with {@code @JsonView(Views.SubmissionList.class)}. |
| 68 | + * Removing that annotation will cause this test to fail. |
| 69 | + */ |
| 70 | + @Test |
| 71 | + public void testPositionSerializedInSubmissionListView() throws Exception { |
| 72 | + // Configure ObjectMapper to match application.yml: default-view-inclusion: false |
| 73 | + ObjectMapper mapper = JsonMapper.builder() |
| 74 | + .disable(MapperFeature.DEFAULT_VIEW_INCLUSION) |
| 75 | + .build(); |
| 76 | + |
| 77 | + CustomActionDefinition definition = new CustomActionDefinition("Test action", true); |
| 78 | + definition.setPosition(3L); |
| 79 | + |
| 80 | + // Serialize under Views.SubmissionList — the view used by /submission/query |
| 81 | + String json = mapper.writerWithView(Views.SubmissionList.class).writeValueAsString(definition); |
| 82 | + ObjectNode node = (ObjectNode) mapper.readTree(json); |
| 83 | + |
| 84 | + assertNotNull(node.get("position"), |
| 85 | + "position must be present in SubmissionList view JSON; " + |
| 86 | + "ensure getPosition() in CustomActionDefinition is annotated with @JsonView(Views.SubmissionList.class)"); |
| 87 | + assertTrue(node.get("position").isNumber(), |
| 88 | + "position must be a numeric value in SubmissionList view JSON"); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Verifies that {@code position} is also included when serialized under the |
| 93 | + * {@link Views.SubmissionIndividualActionLogs} view used by the admin |
| 94 | + * {@code GET /submission/get-one/{id}} endpoint, since that view extends |
| 95 | + * {@link Views.SubmissionList}. |
| 96 | + */ |
| 97 | + @Test |
| 98 | + public void testPositionSerializedInSubmissionIndividualActionLogsView() throws Exception { |
| 99 | + ObjectMapper mapper = JsonMapper.builder() |
| 100 | + .disable(MapperFeature.DEFAULT_VIEW_INCLUSION) |
| 101 | + .build(); |
| 102 | + |
| 103 | + CustomActionDefinition definition = new CustomActionDefinition("Test action", true); |
| 104 | + definition.setPosition(5L); |
| 105 | + |
| 106 | + String json = mapper.writerWithView(Views.SubmissionIndividualActionLogs.class).writeValueAsString(definition); |
| 107 | + ObjectNode node = (ObjectNode) mapper.readTree(json); |
| 108 | + |
| 109 | + assertNotNull(node.get("position"), |
| 110 | + "position must be present in SubmissionIndividualActionLogs view JSON"); |
| 111 | + assertTrue(node.get("position").isNumber(), |
| 112 | + "position must be a numeric value in SubmissionIndividualActionLogs view JSON"); |
| 113 | + } |
| 114 | + |
46 | 115 | } |
0 commit comments