use of com.liferay.apio.architect.single.model.SingleModel 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.single.model.SingleModel in project com-liferay-apio-architect by liferay.
the class ItemRoutesTest method _testItemRoutes.
private void _testItemRoutes(ItemRoutes<String, Long> itemRoutes) throws Exception {
Optional<ItemRoutes<String, Long>> optional = Optional.of(itemRoutes);
Map map = optional.flatMap(ItemRoutes::getFormOptional).map(form -> {
assertThat(form.id, is("u/name"));
return (Map) form.get(_body);
}).get();
Optional<String> valueOptional = _body.getValueOptional("key");
assertThat(map.get("key"), is(valueOptional.get()));
SingleModel<String> singleModel = optional.flatMap(ItemRoutes::getItemFunctionOptional).get().apply(null).apply(42L).getUnchecked();
assertThat(singleModel.getResourceName(), is("name"));
assertThat(singleModel.getModel(), is("Apio"));
optional.flatMap(ItemRoutes::getDeleteConsumerOptional).get().apply(null).accept(42L);
SingleModel<String> updatedSingleModel = optional.flatMap(ItemRoutes::getUpdateItemFunctionOptional).get().apply(null).apply(42L).apply(_body).getUnchecked();
assertThat(updatedSingleModel.getResourceName(), is("name"));
assertThat(updatedSingleModel.getModel(), is("Updated"));
List<Operation> operations = updatedSingleModel.getOperations();
assertThat(operations, hasSize(2));
Operation firstOperation = operations.get(0);
assertThat(firstOperation.getFormOptional(), is(emptyOptional()));
assertThat(firstOperation.method, is(DELETE));
assertThat(firstOperation.name, is("name/delete"));
Operation secondOperation = operations.get(1);
assertThat(secondOperation.getFormOptional(), is(optionalWithValue()));
assertThat(secondOperation.method, is(PUT));
assertThat(secondOperation.name, is("name/update"));
}
use of com.liferay.apio.architect.single.model.SingleModel in project com-liferay-apio-architect by liferay.
the class FieldsWriterTest method testWriteEmbeddedRelatedModelsWithEmbeddedPredicate.
@SuppressWarnings("unchecked")
@Test
public void testWriteEmbeddedRelatedModelsWithEmbeddedPredicate() {
List<String> linkedRelatedModelURLs = new ArrayList<>();
List<String> embeddedRelatedModelURLs = new ArrayList<>();
List<FunctionalList<String>> firstEmbeddedPathElementsList = new ArrayList<>();
List<FunctionalList<String>> secondEmbeddedPathElementsList = new ArrayList<>();
List<FunctionalList<String>> thirdEmbeddedPathElementsList = new ArrayList<>();
List<SingleModel> singleModels = new ArrayList<>();
Function<SingleModel<?>, Optional<Path>> pathFunction = Mockito.mock(Function.class);
Mockito.when(_requestInfo.getEmbedded()).thenReturn("first.embedded2"::equals);
Mockito.when(pathFunction.apply(Mockito.any())).thenReturn(Optional.of(new Path("name1", "id1")), Optional.of(new Path("name2", "id2")), Optional.of(new Path("name3", "id3")), Optional.of(new Path("name4", "id4")));
_fieldsWriter.writeRelatedModels(pathFunction, (singleModel, embeddedPathElements) -> {
singleModels.add(singleModel);
firstEmbeddedPathElementsList.add(embeddedPathElements);
}, (url, embeddedPathElements) -> {
linkedRelatedModelURLs.add(url);
secondEmbeddedPathElementsList.add(embeddedPathElements);
}, (url, embeddedPathElements) -> {
embeddedRelatedModelURLs.add(url);
thirdEmbeddedPathElementsList.add(embeddedPathElements);
});
assertThat(singleModels, hasSize(equalTo(1)));
SingleModel singleModel = singleModels.get(0);
assertThat(singleModel.getModel(), is(instanceOf(FirstEmbeddedModel.class)));
assertThat(linkedRelatedModelURLs, hasSize(equalTo(3)));
assertThat(linkedRelatedModelURLs, contains("www.liferay.com/p/name1/id1", "www.liferay.com/p/name3/id3", "www.liferay.com/p/name4/id4"));
assertThat(embeddedRelatedModelURLs, hasSize(equalTo(1)));
assertThat(embeddedRelatedModelURLs, contains("www.liferay.com/p/name2/id2"));
assertThat(firstEmbeddedPathElementsList, hasSize(equalTo(1)));
assertThat(firstEmbeddedPathElementsList, contains(aFunctionalListThat(contains("first", "embedded2"))));
assertThat(secondEmbeddedPathElementsList, hasSize(equalTo(3)));
assertThat(secondEmbeddedPathElementsList, contains(aFunctionalListThat(contains("first", "embedded1")), aFunctionalListThat(contains("first", "linked1")), aFunctionalListThat(contains("first", "linked2"))));
assertThat(thirdEmbeddedPathElementsList, hasSize(equalTo(1)));
assertThat(thirdEmbeddedPathElementsList, contains(aFunctionalListThat(contains("first", "embedded2"))));
}
use of com.liferay.apio.architect.single.model.SingleModel in project com-liferay-apio-architect by liferay.
the class FieldsWriterTest method testGetSingleModel.
@Test
public void testGetSingleModel() {
SingleModel<Integer> parentSingleModel = new SingleModel<>(3, "", Collections.emptyList());
RelatedModel<Integer, String> relatedModel = new RelatedModel<>("key", FirstEmbeddedId.class, String::valueOf);
Optional<SingleModel<FirstEmbeddedModel>> optional = FieldsWriter.getSingleModel(relatedModel, parentSingleModel, MockWriterUtil::getSingleModel);
assertThat(optional, is(optionalWithValue()));
optional.ifPresent(singleModel -> {
assertThat(singleModel.getResourceName(), is("first"));
FirstEmbeddedModel firstEmbeddedModel = singleModel.getModel();
assertThat(firstEmbeddedModel.getId(), is("3"));
});
}
Aggregations