Skip to content

Commit cfa48cd

Browse files
committed
ACC-2100 Return 501 Not implemented when following unidirectional *-to-many relation
1 parent 40855c9 commit cfa48cd

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

contentgrid-appserver-rest/src/main/java/com/contentgrid/appserver/rest/property/handler/XToManyRelationRequestHandler.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,17 @@ protected ResponseEntity<Object> getProperty(
7272
var targetPathSegment = property.getTargetEndPoint().getEntity().getPathSegment();
7373
datamodelApi.findById(application, property.getSourceEndPoint().getEntity(), instanceId)
7474
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Entity with id %s not found".formatted(instanceId)));
75-
var redirectUrl = linkTo(methodOn(EntityRestController.class).listEntity(application, targetPathSegment, 0,
76-
Map.of(property.getTargetEndPoint().getName().getValue(), instanceId.toString()))).toUri(); // TODO: use RelationSearchFilter
77-
78-
return ResponseEntity.status(HttpStatus.FOUND).location(redirectUrl).build();
75+
// TODO: use FilterName of relation
76+
var filterName = property.getTargetEndPoint().getName();
77+
if (filterName != null) {
78+
var redirectUrl = linkTo(methodOn(EntityRestController.class)
79+
.listEntity(application, targetPathSegment, 0, Map.of(filterName.getValue(), instanceId.toString())))
80+
.toUri();
81+
82+
return ResponseEntity.status(HttpStatus.FOUND).location(redirectUrl).build();
83+
} else {
84+
throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Following an unidirectional *-to-many relation not implemented.");
85+
}
7986
}
8087

8188
@Override

contentgrid-appserver-rest/src/test/java/com/contentgrid/appserver/rest/property/handler/RelationRequestHandlerTest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.UUID;
3030
import java.util.stream.Stream;
3131
import org.junit.jupiter.api.AfterEach;
32+
import org.junit.jupiter.api.Disabled;
3233
import org.junit.jupiter.api.Nested;
3334
import org.junit.jupiter.api.Test;
3435
import org.junit.jupiter.params.ParameterizedTest;
@@ -134,7 +135,7 @@ void followManyToOneRelation() throws Exception {
134135
@Test
135136
void followOneToManyRelation() throws Exception {
136137
Mockito.doReturn(Optional.of(EntityData.builder()
137-
.name(EntityName.of("person"))
138+
.name(TestApplication.PERSON.getName())
138139
.id(PERSON_ID)
139140
.build()))
140141
.when(datamodelApi)
@@ -152,7 +153,7 @@ void followOneToManyRelation() throws Exception {
152153
@Test
153154
void followManyToManyRelation() throws Exception {
154155
Mockito.doReturn(Optional.of(EntityData.builder()
155-
.name(EntityName.of("product"))
156+
.name(TestApplication.PRODUCT.getName())
156157
.id(PRODUCT_ID)
157158
.build()))
158159
.when(datamodelApi)
@@ -167,6 +168,25 @@ void followManyToManyRelation() throws Exception {
167168
.findById(TestApplication.APPLICATION, TestApplication.PRODUCT, PRODUCT_ID);
168169
}
169170

171+
@Test
172+
@Disabled("Following unidirectional *-to-many relations not implemented")
173+
void followUnidirectionalToManyRelation() throws Exception {
174+
Mockito.doReturn(Optional.of(EntityData.builder()
175+
.name(TestApplication.PERSON.getName())
176+
.id(PERSON_ID)
177+
.build()))
178+
.when(datamodelApi)
179+
.findById(TestApplication.APPLICATION, TestApplication.PERSON, PERSON_ID);
180+
181+
mockMvc.perform(get("/persons/{sourceId}/friends", PERSON_ID))
182+
.andExpect(status().isFound())
183+
.andExpect(header().string(HttpHeaders.LOCATION,
184+
"http://localhost/persons?page=0&_internal_person__friends=%s".formatted(PERSON_ID))); // TODO: change url
185+
186+
Mockito.verify(datamodelApi)
187+
.findById(TestApplication.APPLICATION, TestApplication.PERSON, PERSON_ID);
188+
}
189+
170190
@Test
171191
void followOneToManyRelationItem() throws Exception {
172192
var targetId = EntityId.of(UUID.randomUUID());

0 commit comments

Comments
 (0)