Search in sources :

Example 1 with RootModel

use of com.liferay.apio.architect.test.util.model.RootModel in project com-liferay-apio-architect by liferay.

the class MockSingleModelWriter method write.

/**
 * Writes a {@link RootModel}, with the hierarchy of embedded models and
 * multiple fields.
 *
 * @param httpHeaders the request's {@code HttpHeaders}
 * @param singleModelMessageMapper the {@link SingleModelMessageMapper} to
 *        use for writing the JSON object
 */
public static JsonObject write(HttpHeaders httpHeaders, SingleModelMessageMapper<RootModel> singleModelMessageMapper) {
    RequestInfo requestInfo = getRequestInfo(httpHeaders);
    Operation deleteOperation = new Operation(DELETE, "delete-operation");
    Operation putOperation = new Operation(createForm("u", "r"), PUT, "update-operation");
    SingleModel<RootModel> singleModel = new SingleModel<>(() -> "first", "root", asList(deleteOperation, putOperation));
    SingleModelWriter<RootModel> singleModelWriter = SingleModelWriter.create(builder -> builder.singleModel(singleModel).modelMessageMapper(singleModelMessageMapper).pathFunction(MockWriterUtil::identifierToPath).resourceNameFunction(__ -> Optional.of("models")).representorFunction(MockWriterUtil::getRepresentorOptional).requestInfo(requestInfo).singleModelFunction(MockWriterUtil::getSingleModel).build());
    Optional<String> optional = singleModelWriter.write();
    if (!optional.isPresent()) {
        throw new AssertionError("Writer failed to write");
    }
    return new Gson().fromJson(optional.get(), JsonObject.class);
}
Also used : SingleModel(com.liferay.apio.architect.single.model.SingleModel) JsonObject(com.google.gson.JsonObject) MockFormCreator.createForm(com.liferay.apio.architect.test.util.form.MockFormCreator.createForm) RequestInfo(com.liferay.apio.architect.request.RequestInfo) SingleModelMessageMapper(com.liferay.apio.architect.message.json.SingleModelMessageMapper) Operation(com.liferay.apio.architect.operation.Operation) HttpHeaders(javax.ws.rs.core.HttpHeaders) SingleModelWriter(com.liferay.apio.architect.writer.SingleModelWriter) Arrays.asList(java.util.Arrays.asList) Gson(com.google.gson.Gson) RootModel(com.liferay.apio.architect.test.util.model.RootModel) DELETE(com.liferay.apio.architect.operation.Method.DELETE) Optional(java.util.Optional) MockWriterUtil.getRequestInfo(com.liferay.apio.architect.test.util.writer.MockWriterUtil.getRequestInfo) SingleModel(com.liferay.apio.architect.single.model.SingleModel) PUT(com.liferay.apio.architect.operation.Method.PUT) RootModel(com.liferay.apio.architect.test.util.model.RootModel) Gson(com.google.gson.Gson) 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)

Example 2 with RootModel

use of com.liferay.apio.architect.test.util.model.RootModel in project com-liferay-apio-architect by liferay.

the class MockRepresentorCreator method createRootModelRepresentor.

/**
 * Creates a mock {@code Representor} for {@code RootModel}.
 *
 * @param  activateNulls whether to add {@code null} empty values
 * @return the mock {@code Representor} for {@code RootModel}
 */
public static Representor<RootModel, String> createRootModelRepresentor(boolean activateNulls) {
    Representor.Builder<RootModel, String> builder = new Representor.Builder<>(RootModelId.class);
    Representor.Builder<RootModel, String>.FirstStep firstStepBuilder = builder.types("Type 1", "Type 2").identifier(RootModel::getId).addBinary("binary1", __ -> null).addBinary("binary2", __ -> null).addBoolean("boolean1", __ -> true).addBoolean("boolean2", __ -> false).addBooleanList("booleanList1", __ -> asList(true, true, false, false)).addBooleanList("booleanList2", __ -> asList(true, false, true, false)).addDate("date1", __ -> new Date(1465981200000L)).addDate("date2", __ -> new Date(1491244560000L)).addLinkedModel("embedded1", FirstEmbeddedId.class, __ -> "first").addLinkedModel("embedded2", FirstEmbeddedId.class, __ -> "second").addLinkedModel("linked1", FirstEmbeddedId.class, __ -> "third").addLinkedModel("linked2", FirstEmbeddedId.class, __ -> "fourth").addLink("link1", "www.liferay.com").addLink("link2", "community.liferay.com").addLocalizedStringByLanguage("localizedString1", (model, language) -> "Translated 1").addLocalizedStringByLanguage("localizedString2", (model, language) -> "Translated 2").addNumber("number1", __ -> 2017).addNumber("number2", __ -> 42).addNumberList("numberList1", __ -> asList(1, 2, 3, 4, 5)).addNumberList("numberList2", __ -> asList(6, 7, 8, 9, 10)).addRelatedCollection("relatedCollection1", FirstEmbeddedId.class).addRelatedCollection("relatedCollection2", FirstEmbeddedId.class).addString("string1", __ -> "Live long and prosper").addString("string2", __ -> "Hypermedia").addStringList("stringList1", __ -> asList("a", "b", "c", "d", "e")).addStringList("stringList2", __ -> asList("f", "g", "h", "i", "j")).addNested("nested1", __ -> (FirstEmbeddedModel) () -> "id 1", nestedBuilder -> nestedBuilder.nestedTypes("Type 3").addNumber("number1", __ -> 2017).addString("string1", FirstEmbeddedModel::getId).addString("string2", __ -> "string2").build()).addNested("nested2", rootModel -> (SecondEmbeddedModel) rootModel::getId, nestedBuilder -> nestedBuilder.nestedTypes("Type 4").addBidirectionalModel("bidirectionalModel3", "bidirectionalKey", FirstEmbeddedId.class, (Function<SecondEmbeddedModel, String>) SecondEmbeddedModel::getId).addString("string1", SecondEmbeddedModel::getId).addNumber("number1", __ -> 42).addLinkedModel("linked3", ThirdEmbeddedId.class, __ -> "fifth").addNested("nested3", __ -> () -> "id 3", (Representor.Builder<ThirdEmbeddedModel, ?> thirdEmbeddedModelBuilder) -> thirdEmbeddedModelBuilder.nestedTypes("Type 5").addString("string1", ThirdEmbeddedModel::getId).build()).addNumber("number1", __ -> 42).addRelatedCollection("relatedCollection3", ThirdEmbeddedId.class).addString("string1", SecondEmbeddedModel::getId).build());
    if (activateNulls) {
        return firstStepBuilder.addBoolean("boolean3", __ -> null).addLink("link3", null).addLink("link4", "").addLocalizedStringByLanguage("localizedString3", (model, language) -> null).addLocalizedStringByLanguage("localizedString4", (model, language) -> "").addNumber("number3", __ -> null).addString("string3", __ -> null).addString("string4", __ -> "").build();
    }
    return firstStepBuilder.build();
}
Also used : Date(java.util.Date) RootModelId(com.liferay.apio.architect.test.util.identifier.RootModelId) Function(java.util.function.Function) Collections.singletonList(java.util.Collections.singletonList) Representor(com.liferay.apio.architect.representor.Representor) SecondEmbeddedId(com.liferay.apio.architect.test.util.identifier.SecondEmbeddedId) SecondEmbeddedModel(com.liferay.apio.architect.test.util.model.SecondEmbeddedModel) Arrays.asList(java.util.Arrays.asList) RootModel(com.liferay.apio.architect.test.util.model.RootModel) FirstEmbeddedId(com.liferay.apio.architect.test.util.identifier.FirstEmbeddedId) FirstEmbeddedModel(com.liferay.apio.architect.test.util.model.FirstEmbeddedModel) ThirdEmbeddedId(com.liferay.apio.architect.test.util.identifier.ThirdEmbeddedId) ThirdEmbeddedModel(com.liferay.apio.architect.test.util.model.ThirdEmbeddedModel) RootModel(com.liferay.apio.architect.test.util.model.RootModel) FirstEmbeddedModel(com.liferay.apio.architect.test.util.model.FirstEmbeddedModel) Representor(com.liferay.apio.architect.representor.Representor) Date(java.util.Date) Function(java.util.function.Function) SecondEmbeddedModel(com.liferay.apio.architect.test.util.model.SecondEmbeddedModel) ThirdEmbeddedId(com.liferay.apio.architect.test.util.identifier.ThirdEmbeddedId) ThirdEmbeddedModel(com.liferay.apio.architect.test.util.model.ThirdEmbeddedModel) FirstEmbeddedId(com.liferay.apio.architect.test.util.identifier.FirstEmbeddedId)

Example 3 with RootModel

use of com.liferay.apio.architect.test.util.model.RootModel 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

RootModel (com.liferay.apio.architect.test.util.model.RootModel)3 Gson (com.google.gson.Gson)2 JsonObject (com.google.gson.JsonObject)2 Operation (com.liferay.apio.architect.operation.Operation)2 RequestInfo (com.liferay.apio.architect.request.RequestInfo)2 MockFormCreator.createForm (com.liferay.apio.architect.test.util.form.MockFormCreator.createForm)2 MockWriterUtil.getRequestInfo (com.liferay.apio.architect.test.util.writer.MockWriterUtil.getRequestInfo)2 Arrays.asList (java.util.Arrays.asList)2 Optional (java.util.Optional)2 HttpHeaders (javax.ws.rs.core.HttpHeaders)2 PageMessageMapper (com.liferay.apio.architect.message.json.PageMessageMapper)1 SingleModelMessageMapper (com.liferay.apio.architect.message.json.SingleModelMessageMapper)1 DELETE (com.liferay.apio.architect.operation.Method.DELETE)1 POST (com.liferay.apio.architect.operation.Method.POST)1 PUT (com.liferay.apio.architect.operation.Method.PUT)1 Page (com.liferay.apio.architect.pagination.Page)1 PageItems (com.liferay.apio.architect.pagination.PageItems)1 Pagination (com.liferay.apio.architect.pagination.Pagination)1 Representor (com.liferay.apio.architect.representor.Representor)1 SingleModel (com.liferay.apio.architect.single.model.SingleModel)1