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)));
}
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)));
}
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);
}
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)));
}
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));
}
Aggregations