Search in sources :

Example 6 with Form

use of com.liferay.apio.architect.form.Form in project com-liferay-apio-architect by liferay.

the class FormConditionsTest method testFormWithInvalidDescriptionUpdatesDescription.

@Test
public void testFormWithInvalidDescriptionUpdatesDescription() {
    Form.Builder<Map<String, Object>> builder = Form.Builder.empty();
    Form<Map<String, Object>> form = builder.title(__ -> "title").description(__ -> null).constructor(HashMap::new).build();
    Builder<Map<String, Object>> formConditionsBuilder = new Builder<>();
    FormConditions<Map<String, Object>> formConditions = formConditionsBuilder.whereString("string", isReturnedIn(map -> null)).build();
    Description description = new StringDescription();
    formConditions.describeMismatch(form, description);
    String expected = "was a Form...\n\t...whose description was (null or an empty " + "string)\n\t...that should have returned something that is " + "\"String\" instead of null\n";
    assertThat(description.toString(), is(expected));
}
Also used : Description(org.hamcrest.Description) StringDescription(org.hamcrest.StringDescription) Map(java.util.Map) FormMatchers.isReturnedIn(com.liferay.apio.architect.test.util.form.FormMatchers.isReturnedIn) Is.is(org.hamcrest.core.Is.is) Form(com.liferay.apio.architect.form.Form) Builder(com.liferay.apio.architect.test.util.form.FormConditions.Builder) HashMap(java.util.HashMap) Test(org.junit.Test) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Description(org.hamcrest.Description) StringDescription(org.hamcrest.StringDescription) Form(com.liferay.apio.architect.form.Form) StringDescription(org.hamcrest.StringDescription) Builder(com.liferay.apio.architect.test.util.form.FormConditions.Builder) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 7 with Form

use of com.liferay.apio.architect.form.Form in project com-liferay-apio-architect by liferay.

the class FormConditions method matchesSafely.

@Override
protected boolean matchesSafely(Form<T> form, Description description) {
    T t = form.get(_body);
    String title = form.getTitle(Locale::getDefault);
    boolean result = true;
    if (emptyOrNullString().matches(title)) {
        description.appendText("was a Form...\n").appendText("\t...whose title was ").appendDescriptionOf(emptyOrNullString()).appendText("\n");
        result = false;
    }
    String formDescription = form.getDescription(Locale::getDefault);
    if (emptyOrNullString().matches(formDescription)) {
        if (result) {
            description.appendText("was a Form...\n");
        }
        description.appendText("\t...whose description was ").appendDescriptionOf(emptyOrNullString()).appendText("\n");
        result = false;
    }
    Stream<Matcher<T>> stream = _matchers.stream();
    List<Matcher<T>> matchers = stream.filter(matcher -> !matcher.matches(t)).collect(Collectors.toList());
    if (!matchers.isEmpty()) {
        if (result) {
            description.appendText("was a Form...\n");
        }
        matchers.forEach(matcher -> {
            description.appendText("\t...that ");
            matcher.describeMismatch(t, description);
            description.appendText("\n");
        });
        result = false;
    }
    return result;
}
Also used : Locale(java.util.Locale) Description(org.hamcrest.Description) Arrays(java.util.Arrays) Try(com.liferay.apio.architect.functional.Try) Date(java.util.Date) Body(com.liferay.apio.architect.form.Body) Form(com.liferay.apio.architect.form.Form) HashMap(java.util.HashMap) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TypeSafeDiagnosingMatcher(org.hamcrest.TypeSafeDiagnosingMatcher) List(java.util.List) Stream(java.util.stream.Stream) DateTransformer.asDate(com.liferay.apio.architect.date.DateTransformer.asDate) Locale(java.util.Locale) Map(java.util.Map) Matcher(org.hamcrest.Matcher) IsEmptyString.emptyOrNullString(org.hamcrest.text.IsEmptyString.emptyOrNullString) Optional(java.util.Optional) TypeSafeDiagnosingMatcher(org.hamcrest.TypeSafeDiagnosingMatcher) Matcher(org.hamcrest.Matcher) IsEmptyString.emptyOrNullString(org.hamcrest.text.IsEmptyString.emptyOrNullString)

Example 8 with Form

use of com.liferay.apio.architect.form.Form in project com-liferay-apio-architect by liferay.

the class SingleModelWriter method writeEmbeddedModelFields.

/**
 * Writes a related {@link SingleModel} with the {@link
 * SingleModelMessageMapper}. This method uses a {@link FieldsWriter} to
 * write the different fields of its {@link Representor}. If no {@code
 * Representor} or {@link com.liferay.apio.architect.uri.Path} exists for
 * the model, this method doesn't do anything.
 *
 * @param singleModel the {@code SingleModel} to write
 * @param embeddedPathElements the related model's embedded path elements
 */
public <S> void writeEmbeddedModelFields(SingleModel<S> singleModel, FunctionalList<String> embeddedPathElements, RepresentorFunction representorFunction) {
    Optional<FieldsWriter<S, ?>> optional = getFieldsWriter(singleModel, embeddedPathElements, _requestInfo, _pathFunction, representorFunction, _representorFunction, _singleModelFunction, _singleModel);
    if (!optional.isPresent()) {
        return;
    }
    FieldsWriter<S, ?> fieldsWriter = optional.get();
    fieldsWriter.writeBooleanFields((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceBooleanField(_jsonObjectBuilder, embeddedPathElements, field, value));
    fieldsWriter.writeBooleanListFields((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceBooleanListField(_jsonObjectBuilder, embeddedPathElements, field, value));
    fieldsWriter.writeLocalizedStringFields((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceStringField(_jsonObjectBuilder, embeddedPathElements, field, value));
    fieldsWriter.writeNumberFields((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceNumberField(_jsonObjectBuilder, embeddedPathElements, field, value));
    fieldsWriter.writeNumberListFields((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceNumberListField(_jsonObjectBuilder, embeddedPathElements, field, value));
    fieldsWriter.writeStringFields((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceStringField(_jsonObjectBuilder, embeddedPathElements, field, value));
    fieldsWriter.writeStringListFields((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceStringListField(_jsonObjectBuilder, embeddedPathElements, field, value));
    fieldsWriter.writeLinks((fieldName, link) -> _singleModelMessageMapper.mapEmbeddedResourceLink(_jsonObjectBuilder, embeddedPathElements, fieldName, link));
    fieldsWriter.writeTypes(types -> _singleModelMessageMapper.mapEmbeddedResourceTypes(_jsonObjectBuilder, embeddedPathElements, types));
    fieldsWriter.writeBinaries((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceLink(_jsonObjectBuilder, embeddedPathElements, field, value));
    List<Operation> operations = singleModel.getOperations();
    operations.forEach(operation -> {
        JSONObjectBuilder operationJSONObjectBuilder = new JSONObjectBuilder();
        _singleModelMessageMapper.onStartEmbeddedOperation(_jsonObjectBuilder, operationJSONObjectBuilder, embeddedPathElements, operation);
        Optional<Form> formOptional = operation.getFormOptional();
        formOptional.ifPresent(form -> {
            String url = createFormURL(_requestInfo.getServerURL(), form);
            _singleModelMessageMapper.mapEmbeddedOperationFormURL(_jsonObjectBuilder, operationJSONObjectBuilder, embeddedPathElements, url);
        });
        _singleModelMessageMapper.mapEmbeddedOperationMethod(_jsonObjectBuilder, operationJSONObjectBuilder, embeddedPathElements, operation.method);
        _singleModelMessageMapper.onFinishEmbeddedOperation(_jsonObjectBuilder, operationJSONObjectBuilder, embeddedPathElements, operation);
    });
    fieldsWriter.writeRelatedModels(embeddedSingleModel -> getPathOptional(embeddedSingleModel, _pathFunction, _representorFunction), this::writeEmbeddedModelFields, (resourceURL, resourceEmbeddedPathElements) -> _singleModelMessageMapper.mapLinkedResourceURL(_jsonObjectBuilder, resourceEmbeddedPathElements, resourceURL), (resourceURL, resourceEmbeddedPathElements) -> _singleModelMessageMapper.mapEmbeddedResourceURL(_jsonObjectBuilder, resourceEmbeddedPathElements, resourceURL));
    fieldsWriter.writeRelatedCollections(_resourceNameFunction, (url, resourceEmbeddedPathElements) -> _singleModelMessageMapper.mapLinkedResourceURL(_jsonObjectBuilder, resourceEmbeddedPathElements, url));
    _writeNestedResources(representorFunction, singleModel, embeddedPathElements);
}
Also used : JSONObjectBuilder(com.liferay.apio.architect.message.json.JSONObjectBuilder) WriterUtil.getFieldsWriter(com.liferay.apio.architect.writer.util.WriterUtil.getFieldsWriter) Form(com.liferay.apio.architect.form.Form) Operation(com.liferay.apio.architect.operation.Operation)

Example 9 with Form

use of com.liferay.apio.architect.form.Form in project com-liferay-apio-architect by liferay.

the class SingleModelWriter method write.

/**
 * Writes the handled {@link SingleModel} to a string. This method uses a
 * {@link FieldsWriter} to write the different fields of its {@link
 * Representor}. If no {@code Representor} or {@code Path} exists for the
 * model, this method returns {@code Optional#empty()}.
 *
 * @return the string representation of the {@code SingleModel}, if the
 *         model's {@code Representor} and {@code Path} exist; returns
 *         {@code Optional#empty()} otherwise
 */
@SuppressWarnings("Duplicates")
public Optional<String> write() {
    Optional<FieldsWriter<T, ?>> optional = getFieldsWriter(_singleModel, null, _requestInfo, _pathFunction, _representorFunction, _singleModelFunction);
    if (!optional.isPresent()) {
        return Optional.empty();
    }
    FieldsWriter<T, ?> fieldsWriter = optional.get();
    _singleModelMessageMapper.onStart(_jsonObjectBuilder, _singleModel, _requestInfo.getHttpHeaders());
    fieldsWriter.writeBooleanFields((field, value) -> _singleModelMessageMapper.mapBooleanField(_jsonObjectBuilder, field, value));
    fieldsWriter.writeBooleanListFields((field, value) -> _singleModelMessageMapper.mapBooleanListField(_jsonObjectBuilder, field, value));
    fieldsWriter.writeLocalizedStringFields((field, value) -> _singleModelMessageMapper.mapStringField(_jsonObjectBuilder, field, value));
    fieldsWriter.writeNumberFields((field, value) -> _singleModelMessageMapper.mapNumberField(_jsonObjectBuilder, field, value));
    fieldsWriter.writeNumberListFields((field, value) -> _singleModelMessageMapper.mapNumberListField(_jsonObjectBuilder, field, value));
    fieldsWriter.writeStringFields((field, value) -> _singleModelMessageMapper.mapStringField(_jsonObjectBuilder, field, value));
    fieldsWriter.writeStringListFields((field, value) -> _singleModelMessageMapper.mapStringListField(_jsonObjectBuilder, field, value));
    fieldsWriter.writeLinks((fieldName, link) -> _singleModelMessageMapper.mapLink(_jsonObjectBuilder, fieldName, link));
    fieldsWriter.writeTypes(types -> _singleModelMessageMapper.mapTypes(_jsonObjectBuilder, types));
    fieldsWriter.writeBinaries((field, value) -> _singleModelMessageMapper.mapLink(_jsonObjectBuilder, field, value));
    fieldsWriter.writeSingleURL(url -> _singleModelMessageMapper.mapSelfURL(_jsonObjectBuilder, url));
    List<Operation> operations = _singleModel.getOperations();
    operations.forEach(operation -> {
        JSONObjectBuilder operationJSONObjectBuilder = new JSONObjectBuilder();
        _singleModelMessageMapper.onStartOperation(_jsonObjectBuilder, operationJSONObjectBuilder, operation);
        Optional<Form> formOptional = operation.getFormOptional();
        formOptional.map(form -> createFormURL(_requestInfo.getServerURL(), form)).ifPresent(url -> _singleModelMessageMapper.mapOperationFormURL(_jsonObjectBuilder, operationJSONObjectBuilder, url));
        _singleModelMessageMapper.mapOperationMethod(_jsonObjectBuilder, operationJSONObjectBuilder, operation.method);
        _singleModelMessageMapper.onFinishOperation(_jsonObjectBuilder, operationJSONObjectBuilder, operation);
    });
    fieldsWriter.writeRelatedModels(singleModel -> getPathOptional(singleModel, _pathFunction, _representorFunction), this::writeEmbeddedModelFields, (resourceURL, embeddedPathElements) -> _singleModelMessageMapper.mapLinkedResourceURL(_jsonObjectBuilder, embeddedPathElements, resourceURL), (resourceURL, embeddedPathElements) -> _singleModelMessageMapper.mapEmbeddedResourceURL(_jsonObjectBuilder, embeddedPathElements, resourceURL));
    fieldsWriter.writeRelatedCollections(_resourceNameFunction, (url, embeddedPathElements) -> _singleModelMessageMapper.mapLinkedResourceURL(_jsonObjectBuilder, embeddedPathElements, url));
    _writeNestedResources(_representorFunction, _singleModel, null);
    _singleModelMessageMapper.onFinish(_jsonObjectBuilder, _singleModel, _requestInfo.getHttpHeaders());
    JsonObject jsonObject = _jsonObjectBuilder.build();
    return Optional.of(jsonObject.toString());
}
Also used : JsonObject(com.google.gson.JsonObject) ResourceNameFunction(com.liferay.apio.architect.writer.alias.ResourceNameFunction) RepresentorFunction(com.liferay.apio.architect.writer.alias.RepresentorFunction) RequestInfo(com.liferay.apio.architect.request.RequestInfo) Form(com.liferay.apio.architect.form.Form) SingleModelMessageMapper(com.liferay.apio.architect.message.json.SingleModelMessageMapper) WriterUtil.getFieldsWriter(com.liferay.apio.architect.writer.util.WriterUtil.getFieldsWriter) SingleModelFunction(com.liferay.apio.architect.writer.alias.SingleModelFunction) Function(java.util.function.Function) Operation(com.liferay.apio.architect.operation.Operation) Representor(com.liferay.apio.architect.representor.Representor) JSONObjectBuilder(com.liferay.apio.architect.message.json.JSONObjectBuilder) List(java.util.List) Unsafe.unsafeCast(com.liferay.apio.architect.unsafe.Unsafe.unsafeCast) WriterUtil.getPathOptional(com.liferay.apio.architect.writer.util.WriterUtil.getPathOptional) FunctionalList(com.liferay.apio.architect.list.FunctionalList) Map(java.util.Map) Optional(java.util.Optional) SingleModel(com.liferay.apio.architect.single.model.SingleModel) PathFunction(com.liferay.apio.architect.writer.alias.PathFunction) Collections(java.util.Collections) URLCreator.createFormURL(com.liferay.apio.architect.writer.url.URLCreator.createFormURL) JSONObjectBuilder(com.liferay.apio.architect.message.json.JSONObjectBuilder) WriterUtil.getFieldsWriter(com.liferay.apio.architect.writer.util.WriterUtil.getFieldsWriter) Form(com.liferay.apio.architect.form.Form) JsonObject(com.google.gson.JsonObject) Operation(com.liferay.apio.architect.operation.Operation)

Example 10 with Form

use of com.liferay.apio.architect.form.Form 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();
}
Also used : SingleModel(com.liferay.apio.architect.single.model.SingleModel) JsonObject(com.google.gson.JsonObject) ResourceNameFunction(com.liferay.apio.architect.writer.alias.ResourceNameFunction) RequestInfo(com.liferay.apio.architect.request.RequestInfo) URLCreator.createCollectionPageURL(com.liferay.apio.architect.writer.url.URLCreator.createCollectionPageURL) WriterUtil.getFieldsWriter(com.liferay.apio.architect.writer.util.WriterUtil.getFieldsWriter) SingleModelFunction(com.liferay.apio.architect.writer.alias.SingleModelFunction) Function(java.util.function.Function) URLCreator.createCollectionURL(com.liferay.apio.architect.writer.url.URLCreator.createCollectionURL) Operation(com.liferay.apio.architect.operation.Operation) Representor(com.liferay.apio.architect.representor.Representor) WriterUtil.getPathOptional(com.liferay.apio.architect.writer.util.WriterUtil.getPathOptional) FunctionalList(com.liferay.apio.architect.list.FunctionalList) Page(com.liferay.apio.architect.pagination.Page) Map(java.util.Map) PageType(com.liferay.apio.architect.pagination.PageType) SingleModel(com.liferay.apio.architect.single.model.SingleModel) PathFunction(com.liferay.apio.architect.writer.alias.PathFunction) RepresentorFunction(com.liferay.apio.architect.writer.alias.RepresentorFunction) Collection(java.util.Collection) Form(com.liferay.apio.architect.form.Form) Path(com.liferay.apio.architect.uri.Path) JSONObjectBuilder(com.liferay.apio.architect.message.json.JSONObjectBuilder) PageMessageMapper(com.liferay.apio.architect.message.json.PageMessageMapper) List(java.util.List) URLCreator.createNestedCollectionURL(com.liferay.apio.architect.writer.url.URLCreator.createNestedCollectionURL) Unsafe.unsafeCast(com.liferay.apio.architect.unsafe.Unsafe.unsafeCast) Optional(java.util.Optional) Collections(java.util.Collections) URLCreator.createFormURL(com.liferay.apio.architect.writer.url.URLCreator.createFormURL) JSONObjectBuilder(com.liferay.apio.architect.message.json.JSONObjectBuilder) Form(com.liferay.apio.architect.form.Form) JsonObject(com.google.gson.JsonObject) Operation(com.liferay.apio.architect.operation.Operation)

Aggregations

Form (com.liferay.apio.architect.form.Form)13 Map (java.util.Map)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)6 Description (org.hamcrest.Description)6 Builder (com.liferay.apio.architect.test.util.form.FormConditions.Builder)5 FormMatchers.isReturnedIn (com.liferay.apio.architect.test.util.form.FormMatchers.isReturnedIn)5 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)5 StringDescription (org.hamcrest.StringDescription)5 Is.is (org.hamcrest.core.Is.is)5 Optional (java.util.Optional)4 JSONObjectBuilder (com.liferay.apio.architect.message.json.JSONObjectBuilder)3 Operation (com.liferay.apio.architect.operation.Operation)3 RequestInfo (com.liferay.apio.architect.request.RequestInfo)3 WriterUtil.getFieldsWriter (com.liferay.apio.architect.writer.util.WriterUtil.getFieldsWriter)3 Collections (java.util.Collections)3 List (java.util.List)3 Function (java.util.function.Function)3 JsonObject (com.google.gson.JsonObject)2 Try (com.liferay.apio.architect.functional.Try)2