use of com.liferay.apio.architect.uri.Path in project com-liferay-apio-architect by liferay.
the class PageWriter method write.
/**
* Writes the handled {@link Page} to a string. This method uses a {@link
* FieldsWriter} to write the different fields of its items' {@link
* Representor}. If no {@code Representor} or {@code Path} exist for the
* model, this method returns {@code Optional#empty()}.
*
* @return the representation of the {@code Page}, if the {@code
* Representor} and {@code Path} exist for the model; returns {@code
* Optional#empty()} otherwise
*/
@SuppressWarnings("Duplicates")
public String write() {
_pageMessageMapper.onStart(_jsonObjectBuilder, _page, _requestInfo.getHttpHeaders());
_pageMessageMapper.mapItemTotalCount(_jsonObjectBuilder, _page.getTotalCount());
Collection<T> items = _page.getItems();
_pageMessageMapper.mapPageCount(_jsonObjectBuilder, items.size());
_writePageURLs();
String url = _getCollectionURL();
_pageMessageMapper.mapCollectionURL(_jsonObjectBuilder, url);
String resourceName = _page.getResourceName();
items.forEach(model -> _writeItem(new SingleModel<>(model, resourceName, Collections.emptyList())));
List<Operation> operations = _page.getOperations();
operations.forEach(operation -> {
JSONObjectBuilder operationJSONObjectBuilder = new JSONObjectBuilder();
_pageMessageMapper.onStartOperation(_jsonObjectBuilder, operationJSONObjectBuilder, operation);
Optional<Form> formOptional = operation.getFormOptional();
formOptional.map(form -> createFormURL(_requestInfo.getServerURL(), form)).ifPresent(formURL -> _pageMessageMapper.mapOperationFormURL(_jsonObjectBuilder, operationJSONObjectBuilder, formURL));
_pageMessageMapper.mapOperationMethod(_jsonObjectBuilder, operationJSONObjectBuilder, operation.method);
_pageMessageMapper.onFinishOperation(_jsonObjectBuilder, operationJSONObjectBuilder, operation);
});
_pageMessageMapper.onFinish(_jsonObjectBuilder, _page, _requestInfo.getHttpHeaders());
JsonObject jsonObject = _jsonObjectBuilder.build();
return jsonObject.toString();
}
use of com.liferay.apio.architect.uri.Path in project com-liferay-apio-architect by liferay.
the class NestedCollectionRoutesTest method _testNestedCollectionRoutes.
private void _testNestedCollectionRoutes(NestedCollectionRoutes<String, Long> nestedCollectionRoutes) {
Optional<NestedCollectionRoutes<String, Long>> optional = Optional.of(nestedCollectionRoutes);
Map map = optional.flatMap(NestedCollectionRoutes::getFormOptional).map(form -> {
assertThat(form.id, is("c/name/nested"));
return (Map) form.get(_body);
}).get();
Optional<String> valueOptional = _body.getValueOptional("key");
assertThat(map.get("key"), is(valueOptional.get()));
SingleModel<String> singleModel = optional.flatMap(NestedCollectionRoutes::getNestedCreateItemFunctionOptional).get().apply(null).apply(42L).apply(_body).getUnchecked();
assertThat(singleModel.getResourceName(), is("nested"));
assertThat(singleModel.getModel(), is("Apio"));
Path path = new Path("name", "42");
Page<String> page = optional.flatMap(NestedCollectionRoutes::getNestedGetPageFunctionOptional).get().apply(null).apply(path).apply(42L).getUnchecked();
assertThat(page.getItems(), hasSize(1));
assertThat(page.getItems(), hasItem("Apio"));
assertThat(page.getPathOptional(), optionalWithValue(equalTo(path)));
assertThat(page.getTotalCount(), is(1));
List<Operation> operations = page.getOperations();
assertThat(operations, hasSize(1));
Operation secondOperation = operations.get(0);
assertThat(secondOperation.getFormOptional(), is(optionalWithValue()));
assertThat(secondOperation.method, is(POST));
assertThat(secondOperation.name, is("name/nested/create"));
}
use of com.liferay.apio.architect.uri.Path in project com-liferay-apio-architect by liferay.
the class PageEndpointImpl method deleteCollectionItem.
@Override
public Response deleteCollectionItem(String id) throws Exception {
Try<ItemRoutes<T, S>> itemRoutesTry = Try.fromOptional(_itemRoutesSupplier::get, notFound(_name));
ThrowableConsumer<S> throwableConsumer = itemRoutesTry.mapOptional(ItemRoutes::getDeleteConsumerOptional, notAllowed(DELETE, _name, id)).map(function -> function.apply(_httpServletRequest)).getUnchecked();
Path path = new Path(_name, id);
throwableConsumer.accept(_identifierFunction.apply(path));
return noContent().build();
}
use of com.liferay.apio.architect.uri.Path in project com-liferay-apio-architect by liferay.
the class PathLongIdentifierMapperTest method testMapThrowsExceptionIfNoLongPathId.
@Test(expected = BadRequestException.class)
public void testMapThrowsExceptionIfNoLongPathId() {
PathLongIdentifierMapper pathLongIdentifierMapper = new PathLongIdentifierMapper();
pathLongIdentifierMapper.map(new Path("name", "wrong"));
}
use of com.liferay.apio.architect.uri.Path in project com-liferay-apio-architect by liferay.
the class PathLongIdentifierMapperTest method testMapReturnsPath.
@Test
public void testMapReturnsPath() {
Path path = _pathIdentifierMapper.map("name", 42L);
assertThat(path.getName(), is("name"));
assertThat(path.getId(), is("42"));
}
Aggregations