Search in sources :

Example 11 with Path

use of com.liferay.apio.architect.uri.Path in project com-liferay-apio-architect by liferay.

the class PageTest method testHasNextReturnsFalseWhenIsLast.

@Test
public void testHasNextReturnsFalseWhenIsLast() {
    Pagination pagination = new Pagination(1, 10);
    _path = new Path("name", "id");
    Page<String> page = new Page<>("", _pageItems, pagination, _path, emptyList());
    assertThat(page.hasNext(), is(false));
}
Also used : Path(com.liferay.apio.architect.uri.Path) Test(org.junit.Test)

Example 12 with Path

use of com.liferay.apio.architect.uri.Path in project com-liferay-apio-architect by liferay.

the class PageTest method setUp.

@Before
public void setUp() {
    _pageItems = new PageItems<>(Collections.singleton("apio"), 10);
    Pagination pagination = new Pagination(1, 4);
    _path = new Path("name", "id");
    _operations = Collections.singletonList(new Operation(POST, "operation"));
    _page = new Page<>("name", _pageItems, pagination, _path, _operations);
}
Also used : Path(com.liferay.apio.architect.uri.Path) Operation(com.liferay.apio.architect.operation.Operation) Before(org.junit.Before)

Example 13 with Path

use of com.liferay.apio.architect.uri.Path 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"))));
}
Also used : SingleModel(com.liferay.apio.architect.single.model.SingleModel) Path(com.liferay.apio.architect.uri.Path) FunctionalList(com.liferay.apio.architect.list.FunctionalList) Optional(java.util.Optional) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 14 with Path

use of com.liferay.apio.architect.uri.Path in project com-liferay-apio-architect by liferay.

the class MockPageWriter method write.

/**
 * Writes a Collection of {@link RootModel}, with the hierarchy of embedded
 * models and multiple fields.
 *
 * @param httpHeaders the request's {@code HttpHeaders}
 * @param pageMessageMapper the {@link PageMessageMapper} to use for writing
 *        the JSON object
 */
public static JsonObject write(HttpHeaders httpHeaders, PageMessageMapper<RootModel> pageMessageMapper) {
    RequestInfo requestInfo = getRequestInfo(httpHeaders);
    Collection<RootModel> items = Arrays.asList(() -> "1", () -> "2", () -> "3");
    PageItems<RootModel> pageItems = new PageItems<>(items, 9);
    Pagination pagination = new Pagination(3, 2);
    Path path = new Path("name", "id");
    List<Operation> operations = Collections.singletonList(new Operation(createForm("c", "p"), POST, "create-operation"));
    Page<RootModel> page = new Page<>("root", pageItems, pagination, path, operations);
    PageWriter<RootModel> pageWriter = PageWriter.create(builder -> builder.page(page).pageMessageMapper(pageMessageMapper).pathFunction(MockWriterUtil::identifierToPath).resourceNameFunction(__ -> Optional.of("models")).representorFunction(MockWriterUtil::getRepresentorOptional).requestInfo(requestInfo).singleModelFunction(MockWriterUtil::getSingleModel).build());
    return new Gson().fromJson(pageWriter.write(), JsonObject.class);
}
Also used : Path(com.liferay.apio.architect.uri.Path) JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) MockFormCreator.createForm(com.liferay.apio.architect.test.util.form.MockFormCreator.createForm) RequestInfo(com.liferay.apio.architect.request.RequestInfo) Collection(java.util.Collection) PageItems(com.liferay.apio.architect.pagination.PageItems) Path(com.liferay.apio.architect.uri.Path) Operation(com.liferay.apio.architect.operation.Operation) PageMessageMapper(com.liferay.apio.architect.message.json.PageMessageMapper) List(java.util.List) PageWriter(com.liferay.apio.architect.writer.PageWriter) HttpHeaders(javax.ws.rs.core.HttpHeaders) Pagination(com.liferay.apio.architect.pagination.Pagination) Gson(com.google.gson.Gson) RootModel(com.liferay.apio.architect.test.util.model.RootModel) Page(com.liferay.apio.architect.pagination.Page) POST(com.liferay.apio.architect.operation.Method.POST) Optional(java.util.Optional) MockWriterUtil.getRequestInfo(com.liferay.apio.architect.test.util.writer.MockWriterUtil.getRequestInfo) Collections(java.util.Collections) RootModel(com.liferay.apio.architect.test.util.model.RootModel) Gson(com.google.gson.Gson) Page(com.liferay.apio.architect.pagination.Page) Operation(com.liferay.apio.architect.operation.Operation) RequestInfo(com.liferay.apio.architect.request.RequestInfo) MockWriterUtil.getRequestInfo(com.liferay.apio.architect.test.util.writer.MockWriterUtil.getRequestInfo) Pagination(com.liferay.apio.architect.pagination.Pagination) PageItems(com.liferay.apio.architect.pagination.PageItems)

Aggregations

Path (com.liferay.apio.architect.uri.Path)14 Test (org.junit.Test)9 Optional (java.util.Optional)6 Operation (com.liferay.apio.architect.operation.Operation)5 SingleModel (com.liferay.apio.architect.single.model.SingleModel)5 Page (com.liferay.apio.architect.pagination.Page)4 Collections (java.util.Collections)4 List (java.util.List)4 POST (com.liferay.apio.architect.operation.Method.POST)3 Representor (com.liferay.apio.architect.representor.Representor)3 RequestInfo (com.liferay.apio.architect.request.RequestInfo)3 JsonObject (com.google.gson.JsonObject)2 Body (com.liferay.apio.architect.form.Body)2 Identifier (com.liferay.apio.architect.identifier.Identifier)2 FunctionalList (com.liferay.apio.architect.list.FunctionalList)2 PageMessageMapper (com.liferay.apio.architect.message.json.PageMessageMapper)2 DELETE (com.liferay.apio.architect.operation.Method.DELETE)2 PageItems (com.liferay.apio.architect.pagination.PageItems)2 Pagination (com.liferay.apio.architect.pagination.Pagination)2 Function (java.util.function.Function)2