Search in sources :

Example 56 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project com-liferay-apio-architect by liferay.

the class PlainJSONPageMessageMapperTest method testPlainJSONPageMessageMapper.

@Test
public void testPlainJSONPageMessageMapper() {
    HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);
    JsonObject jsonObject = MockPageWriter.write(httpHeaders, _pageMessageMapper);
    Builder builder = new Builder();
    Conditions conditions = builder.where("collection", isALinkTo("localhost/p/name/id/root")).where("elements", is(aJsonArrayThat(_containsTheElements))).where("numberOfItems", is(aJsonInt(equalTo(3)))).where("pages", _isAJsonObjectWithThePages).where("self", isALinkTo("localhost/p/name/id/root?page=2&per_page=3")).where("totalNumberOfItems", is(aJsonInt(equalTo(9)))).build();
    assertThat(jsonObject, is(aJsonObjectWith(conditions)));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) Builder(com.liferay.apio.architect.test.util.json.Conditions.Builder) JsonObject(com.google.gson.JsonObject) Conditions(com.liferay.apio.architect.test.util.json.Conditions) Test(org.junit.Test)

Example 57 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project com-liferay-apio-architect by liferay.

the class JSONLDPageMessageMapperTest method testJSONLDPageMessageMapper.

@Test
public void testJSONLDPageMessageMapper() {
    HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);
    JsonObject jsonObject = MockPageWriter.write(httpHeaders, _pageMessageMapper);
    Builder builder = new Builder();
    Conditions conditions = builder.where("@context", is(aJsonArrayThat(contains(_theContext)))).where("@id", isALinkTo("localhost/p/name/id/root")).where("@type", containsTheTypes("Collection")).where("member", is(aJsonArrayThat(contains(_theMembers)))).where("numberOfItems", is(aJsonInt(equalTo(3)))).where("operation", is(aJsonArrayThat(_containsTheOperations))).where("totalItems", is(aJsonInt(equalTo(9)))).where("view", _isAJsonObjectWithTheView).build();
    assertThat(jsonObject, is(aJsonObjectWith(conditions)));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) Builder(com.liferay.apio.architect.test.util.json.Conditions.Builder) JsonObject(com.google.gson.JsonObject) Conditions(com.liferay.apio.architect.test.util.json.Conditions) Test(org.junit.Test)

Example 58 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders 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)

Example 59 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project com-liferay-apio-architect by liferay.

the class ErrorWriterTest method testWriteErrorCreatesCorrectJsonObject.

@Test
public void testWriteErrorCreatesCorrectJsonObject() {
    ErrorMessageMapper errorMessageMapper = new TestErrorMessageMapper();
    APIError apiError = new APIError(new IllegalArgumentException(), "A title", "A description", "A type", 404);
    HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);
    Mockito.when(httpHeaders.getHeaderString("start")).thenReturn("true");
    Mockito.when(httpHeaders.getHeaderString("end")).thenReturn("true");
    String error = ErrorWriter.writeError(errorMessageMapper, apiError, httpHeaders);
    Conditions.Builder builder = new Conditions.Builder();
    Conditions conditions = builder.where("description", is(aJsonString(equalTo("A description")))).where("title", is(aJsonString(equalTo("A title")))).where("type", is(aJsonString(equalTo("A type")))).where("status", is(aJsonInt(equalTo(404)))).where("start", is(aJsonBoolean(true))).where("end", is(aJsonBoolean(true))).withStrictModeDeactivated().build();
    assertThat(error, is(aJsonObjectStringWith(conditions)));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) JSONObjectBuilder(com.liferay.apio.architect.message.json.JSONObjectBuilder) ErrorMessageMapper(com.liferay.apio.architect.message.json.ErrorMessageMapper) JsonMatchers.aJsonString(com.liferay.apio.architect.test.util.json.JsonMatchers.aJsonString) APIError(com.liferay.apio.architect.error.APIError) Conditions(com.liferay.apio.architect.test.util.json.Conditions) Test(org.junit.Test)

Example 60 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project microservice_framework by CJSCommonPlatform.

the class RestAdapterGenerator_POSTMethodBodyTest method shouldPassHttpHeadersToRestProcessor.

@SuppressWarnings("unchecked")
@Test
public void shouldPassHttpHeadersToRestProcessor() throws Exception {
    generator.run(restRamlWithCommandApiDefaults().with(resource("/path").with(httpActionWithDefaultMapping(POST).withHttpActionOfDefaultRequestType())).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> resourceClass = compiler.compiledClassOf(BASE_PACKAGE, "resource", "DefaultCommandApiPathResource");
    final Object resourceObject = getInstanceOf(resourceClass);
    final HttpHeaders headers = new ThreadLocalHttpHeaders();
    setField(resourceObject, "headers", headers);
    final Method method = firstMethodOf(resourceClass).get();
    method.invoke(resourceObject, NOT_USED_JSONOBJECT);
    verify(restProcessor).process(anyString(), any(Function.class), anyString(), any(Optional.class), eq(headers), any(Collection.class));
}
Also used : ThreadLocalHttpHeaders(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpHeaders) HttpHeaders(javax.ws.rs.core.HttpHeaders) ThreadLocalHttpHeaders(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpHeaders) Function(java.util.function.Function) Optional(java.util.Optional) Collection(java.util.Collection) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) JsonObject(javax.json.JsonObject) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

HttpHeaders (javax.ws.rs.core.HttpHeaders)87 Test (org.junit.Test)57 Message (org.apache.cxf.message.Message)31 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 Optional (java.util.Optional)11 CatalogFramework (ddf.catalog.CatalogFramework)10 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)10 JsonObject (com.google.gson.JsonObject)9 Locale (java.util.Locale)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 Response (javax.ws.rs.core.Response)8 UriInfo (javax.ws.rs.core.UriInfo)8 IOException (java.io.IOException)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 MediaType (javax.ws.rs.core.MediaType)7 RequestInfo (com.liferay.apio.architect.request.RequestInfo)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Conditions (com.liferay.apio.architect.test.util.json.Conditions)5 OutputStream (java.io.OutputStream)5 Annotation (java.lang.annotation.Annotation)5