Search in sources :

Example 1 with Form

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

the class FormEndpointTest method testValidNestedCreatorFormMethodReturnsSuccess.

@Test
public void testValidNestedCreatorFormMethodReturnsSuccess() {
    FormEndpoint formEndpoint = new FormEndpoint(__ -> Optional.empty(), __ -> Optional.empty(), (name, nestedName) -> Optional.of(_nestedCollectionRoutes()));
    Try<Form> nestedCreatorFormTry = formEndpoint.nestedCreatorForm("", "");
    assertThat(nestedCreatorFormTry, is(aSuccessTry()));
    Form form = nestedCreatorFormTry.getUnchecked();
    assertThat(form.id, is("c/name/nestedName"));
}
Also used : Form(com.liferay.apio.architect.form.Form) Test(org.junit.Test)

Example 2 with Form

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

the class FormEndpointTest method testValidUpdaterFormMethodReturnsSuccess.

@Test
public void testValidUpdaterFormMethodReturnsSuccess() {
    FormEndpoint formEndpoint = new FormEndpoint(__ -> Optional.empty(), __ -> Optional.of(_itemRoutes()), (name, nestedName) -> Optional.empty());
    Try<Form> updaterFormTry = formEndpoint.updaterForm("");
    assertThat(updaterFormTry, is(aSuccessTry()));
    Form form = updaterFormTry.getUnchecked();
    assertThat(form.id, is("u/name"));
}
Also used : Form(com.liferay.apio.architect.form.Form) Test(org.junit.Test)

Example 3 with Form

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

the class FormMessageBodyWriter method writeTo.

@Override
public void writeTo(Success<Form> success, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream outputStream) throws IOException, WebApplicationException {
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
    PrintWriter printWriter = new PrintWriter(outputStreamWriter, true);
    Form form = success.getValue();
    Optional<FormMessageMapper> optional = _formMessageMapperManager.getFormMessageMapperOptional(_request);
    FormMessageMapper formMessageMapper = optional.orElseThrow(NotSupportedException::new);
    RequestInfo requestInfo = RequestInfo.create(builder -> builder.httpHeaders(_httpHeaders).httpServletRequest(_httpServletRequest).serverURL(_providerManager.provideMandatory(_httpServletRequest, ServerURL.class)).embedded(_providerManager.provideOptional(_httpServletRequest, Embedded.class).orElse(__ -> false)).fields(_providerManager.provideOptional(_httpServletRequest, Fields.class).orElse(__ -> string -> true)).language(_providerManager.provideOptional(_httpServletRequest, Language.class).orElse(Locale::getDefault)).build());
    FormWriter formWriter = FormWriter.create(builder -> builder.form(form).formMessageMapper(formMessageMapper).requestInfo(requestInfo).build());
    httpHeaders.put(CONTENT_TYPE, Collections.singletonList(formMessageMapper.getMediaType()));
    printWriter.println(formWriter.write());
    printWriter.close();
}
Also used : CONTENT_TYPE(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE) Provider(javax.ws.rs.ext.Provider) RequestInfo(com.liferay.apio.architect.request.RequestInfo) FormMessageMapperManager(com.liferay.apio.architect.wiring.osgi.manager.message.json.FormMessageMapperManager) Embedded(com.liferay.apio.architect.response.control.Embedded) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaType(javax.ws.rs.core.MediaType) Component(org.osgi.service.component.annotations.Component) Locale(java.util.Locale) Fields(com.liferay.apio.architect.response.control.Fields) Success(com.liferay.apio.architect.functional.Try.Success) GenericUtil(com.liferay.apio.architect.wiring.osgi.util.GenericUtil) OutputStreamWriter(java.io.OutputStreamWriter) ProviderManager(com.liferay.apio.architect.wiring.osgi.manager.ProviderManager) NotSupportedException(javax.ws.rs.NotSupportedException) OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) Try(com.liferay.apio.architect.functional.Try) Context(javax.ws.rs.core.Context) FormWriter(com.liferay.apio.architect.writer.FormWriter) Form(com.liferay.apio.architect.form.Form) ServerURL(com.liferay.apio.architect.url.ServerURL) IOException(java.io.IOException) FormMessageMapper(com.liferay.apio.architect.message.json.FormMessageMapper) StandardCharsets(java.nio.charset.StandardCharsets) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Language(com.liferay.apio.architect.language.Language) HttpHeaders(javax.ws.rs.core.HttpHeaders) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) Request(javax.ws.rs.core.Request) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) Fields(com.liferay.apio.architect.response.control.Fields) Form(com.liferay.apio.architect.form.Form) FormMessageMapper(com.liferay.apio.architect.message.json.FormMessageMapper) OutputStreamWriter(java.io.OutputStreamWriter) NotSupportedException(javax.ws.rs.NotSupportedException) RequestInfo(com.liferay.apio.architect.request.RequestInfo) FormWriter(com.liferay.apio.architect.writer.FormWriter) PrintWriter(java.io.PrintWriter)

Example 4 with Form

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

the class FormConditionsTest method testInvokingDescribesToUpdatesDescription.

@Test
public void testInvokingDescribesToUpdatesDescription() {
    Builder<Map<String, Object>> builder = new Builder<>();
    FormConditions formConditions = builder.whereBoolean("boolean", isReturnedIn(map -> map.get("boolean"))).whereString("string", isReturnedIn(map -> map.get("string"))).build();
    Description description = new StringDescription();
    formConditions.describeTo(description);
    StringBuilder stringBuilder = new StringBuilder();
    String expected = stringBuilder.append("a Form...\n\t...that should have a non empty title\n\t...that ").append("should have a non empty description\n\t...that should return ").append("something that is <true>\n\t...that should return something that ").append("is \"String\"\n").toString();
    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) 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 5 with Form

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

the class FormConditionsTest method testFormWithInvalidTitleUpdatesDescription.

@Test
public void testFormWithInvalidTitleUpdatesDescription() {
    Form.Builder<Map<String, Object>> builder = Form.Builder.empty();
    Form<Map<String, Object>> form = builder.title(__ -> "").description(__ -> "description").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 title 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)

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