Search in sources :

Example 1 with WILDCARD_TYPE

use of javax.ws.rs.core.MediaType.WILDCARD_TYPE in project trellis by trellis-ldp.

the class TrellisHttpResourceTest method testNoBaseURL.

@Test
void testNoBaseURL() throws Exception {
    final TrellisHttpResource matcher = new TrellisHttpResource();
    matcher.baseUrl = Optional.empty();
    matcher.services = mockBundler;
    matcher.request = mockRequest;
    matcher.uriInfo = mockUriInfo;
    matcher.headers = mockHttpHeaders;
    when(mockUriInfo.getPathParameters()).thenReturn(new MultivaluedHashMap<>(singletonMap("path", "resource")));
    when(mockUriInfo.getPath()).thenReturn("resource");
    when(mockUriInfo.getBaseUri()).thenReturn(new URI("http://my.example.com/"));
    when(mockUriInfo.getQueryParameters()).thenReturn(new MultivaluedHashMap<>());
    when(mockHttpHeaders.getRequestHeaders()).thenReturn(new MultivaluedHashMap<>());
    when(mockTrellisRequest.getAcceptableMediaTypes()).thenReturn(singletonList(WILDCARD_TYPE));
    try (final Response res = matcher.getResourceHeaders().toCompletableFuture().join()) {
        assertTrue(getLinks(res).stream().anyMatch(l -> l.getRel().equals("self") && l.getUri().toString().startsWith("http://my.example.com/")), "Missing rel=self header with correct prefix!");
    }
}
Also used : Response(javax.ws.rs.core.Response) MockitoAnnotations.openMocks(org.mockito.MockitoAnnotations.openMocks) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) ServiceBundler(org.trellisldp.common.ServiceBundler) Mock(org.mockito.Mock) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Application(javax.ws.rs.core.Application) Collections.singletonList(java.util.Collections.singletonList) WILDCARD_TYPE(javax.ws.rs.core.MediaType.WILDCARD_TYPE) Metadata(org.trellisldp.api.Metadata) ResourceService(org.trellisldp.api.ResourceService) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Collections.singletonMap(java.util.Collections.singletonMap) URI(java.net.URI) Dataset(org.apache.commons.rdf.api.Dataset) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) DELETED_RESOURCE(org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE) TrellisRequest(org.trellisldp.common.TrellisRequest) CompletableFuture.runAsync(java.util.concurrent.CompletableFuture.runAsync) Stream.of(java.util.stream.Stream.of) CONFIG_HTTP_BASE_URL(org.trellisldp.common.HttpConstants.CONFIG_HTTP_BASE_URL) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Stream(java.util.stream.Stream) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) Executable(org.junit.jupiter.api.function.Executable) Assertions(org.junit.jupiter.api.Assertions) Optional(java.util.Optional) Request(javax.ws.rs.core.Request) UriInfo(javax.ws.rs.core.UriInfo) MISSING_RESOURCE(org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 2 with WILDCARD_TYPE

use of javax.ws.rs.core.MediaType.WILDCARD_TYPE in project trellis by trellis-ldp.

the class GetHandlerTest method testGetBinary.

@Test
void testGetBinary() {
    when(mockResource.getBinaryMetadata()).thenReturn(of(testBinary));
    when(mockResource.getInteractionModel()).thenReturn(LDP.NonRDFSource);
    when(mockTrellisRequest.getAcceptableMediaTypes()).thenReturn(singletonList(WILDCARD_TYPE));
    final GetConfiguration config = new GetConfiguration(false, true, true, null, baseUrl);
    final GetHandler handler = new GetHandler(mockTrellisRequest, mockBundler, extensions, config);
    try (final Response res = handler.getRepresentation(handler.standardHeaders(handler.initialize(mockResource))).toCompletableFuture().join().build()) {
        assertEquals(OK, res.getStatusInfo(), ERR_RESPONSE_CODE);
        assertEquals(-1, res.getLength(), "Incorrect response length!");
        assertEquals(from(time), res.getLastModified(), "Incorrect content-type header!");
        assertTrue(res.getMediaType().isCompatible(TEXT_PLAIN_TYPE), "Incorrect content-type header!");
        assertTrue(res.getLinks().stream().anyMatch(link -> link.getRel().equals("describedby") && link.getUri().toString().endsWith(EXT_DESCRIPTION)), "Missing rel=describedby header!");
        assertTrue(res.getLinks().stream().anyMatch(link -> link.getRel().equals("canonical") && !link.getUri().toString().endsWith(EXT_DESCRIPTION)), "Missing rel=canonical header!");
        assertAll(CHECK_LINK_TYPES, checkLdpType(res, LDP.NonRDFSource));
    }
}
Also used : Response(javax.ws.rs.core.Response) DELETE(javax.ws.rs.HttpMethod.DELETE) SortedSet(java.util.SortedSet) GET(javax.ws.rs.HttpMethod.GET) Optional.of(java.util.Optional.of) ACCEPT_POST(org.trellisldp.common.HttpConstants.ACCEPT_POST) Prefer(org.trellisldp.common.Prefer) ACCEPT_DATETIME(org.trellisldp.common.HttpConstants.ACCEPT_DATETIME) Collections.singletonList(java.util.Collections.singletonList) SKOS(org.trellisldp.vocabulary.SKOS) WILDCARD_TYPE(javax.ws.rs.core.MediaType.WILDCARD_TYPE) MediaType(javax.ws.rs.core.MediaType) Arrays.asList(java.util.Arrays.asList) JSONLD.compacted(org.trellisldp.vocabulary.JSONLD.compacted) PREFER(org.trellisldp.common.HttpConstants.PREFER) RANGE(org.trellisldp.common.HttpConstants.RANGE) OK(javax.ws.rs.core.Response.Status.OK) PATCH(javax.ws.rs.HttpMethod.PATCH) Instant(java.time.Instant) ACCEPT_RANGES(org.trellisldp.common.HttpConstants.ACCEPT_RANGES) Test(org.junit.jupiter.api.Test) VARY(javax.ws.rs.core.HttpHeaders.VARY) List(java.util.List) Stream(java.util.stream.Stream) APPLICATION_LD_JSON(org.trellisldp.common.RdfMediaType.APPLICATION_LD_JSON) Response(javax.ws.rs.core.Response) POST(javax.ws.rs.HttpMethod.POST) UTC(java.time.ZoneOffset.UTC) PUT(javax.ws.rs.HttpMethod.PUT) Arrays.stream(java.util.Arrays.stream) Link(javax.ws.rs.core.Link) LINK(javax.ws.rs.core.HttpHeaders.LINK) Instant.now(java.time.Instant.now) LD_PATCH(org.trellisldp.api.Syntax.LD_PATCH) APPLICATION_N_TRIPLES(org.trellisldp.common.RdfMediaType.APPLICATION_N_TRIPLES) TEXT_HTML_TYPE(javax.ws.rs.core.MediaType.TEXT_HTML_TYPE) ACL(org.trellisldp.vocabulary.ACL) TreeSet(java.util.TreeSet) PREFERENCE_APPLIED(org.trellisldp.common.HttpConstants.PREFERENCE_APPLIED) MEMENTO_DATETIME(org.trellisldp.common.HttpConstants.MEMENTO_DATETIME) APPLICATION_SPARQL_UPDATE(org.trellisldp.common.RdfMediaType.APPLICATION_SPARQL_UPDATE) RDFA(org.apache.commons.rdf.api.RDFSyntax.RDFA) NOT_ACCEPTABLE(javax.ws.rs.core.Response.Status.NOT_ACCEPTABLE) TURTLE(org.apache.commons.rdf.api.RDFSyntax.TURTLE) Date.from(java.util.Date.from) OPTIONS(javax.ws.rs.HttpMethod.OPTIONS) ACCEPT_PATCH(org.trellisldp.common.HttpConstants.ACCEPT_PATCH) LDP(org.trellisldp.vocabulary.LDP) JSONLD(org.apache.commons.rdf.api.RDFSyntax.JSONLD) SimpleEntry(java.util.AbstractMap.SimpleEntry) Trellis(org.trellisldp.vocabulary.Trellis) BinaryMetadata(org.trellisldp.api.BinaryMetadata) APPLICATION_LD_JSON_TYPE(org.trellisldp.common.RdfMediaType.APPLICATION_LD_JSON_TYPE) ZonedDateTime.ofInstant(java.time.ZonedDateTime.ofInstant) NotAcceptableException(javax.ws.rs.NotAcceptableException) TEXT_TURTLE_TYPE(org.trellisldp.common.RdfMediaType.TEXT_TURTLE_TYPE) DESCRIPTION(org.trellisldp.common.HttpConstants.DESCRIPTION) Predicate.isEqual(java.util.function.Predicate.isEqual) NO_CONTENT(javax.ws.rs.core.Response.Status.NO_CONTENT) Mockito.when(org.mockito.Mockito.when) EntityTag(javax.ws.rs.core.EntityTag) TEXT_PLAIN_TYPE(javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE) Collectors.toList(java.util.stream.Collectors.toList) HEAD(javax.ws.rs.HttpMethod.HEAD) OA(org.trellisldp.vocabulary.OA) DigestUtils.sha256Hex(org.apache.commons.codec.digest.DigestUtils.sha256Hex) Executable(org.junit.jupiter.api.function.Executable) Assertions(org.junit.jupiter.api.Assertions) APPLICATION_JSON_TYPE(javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE) NTRIPLES(org.apache.commons.rdf.api.RDFSyntax.NTRIPLES) RFC_1123_DATE_TIME(java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME) Test(org.junit.jupiter.api.Test)

Aggregations

Collections.singletonList (java.util.Collections.singletonList)2 Stream (java.util.stream.Stream)2 WILDCARD_TYPE (javax.ws.rs.core.MediaType.WILDCARD_TYPE)2 Response (javax.ws.rs.core.Response)2 Assertions (org.junit.jupiter.api.Assertions)2 Test (org.junit.jupiter.api.Test)2 Executable (org.junit.jupiter.api.function.Executable)2 URI (java.net.URI)1 Instant (java.time.Instant)1 Instant.now (java.time.Instant.now)1 UTC (java.time.ZoneOffset.UTC)1 ZonedDateTime.ofInstant (java.time.ZonedDateTime.ofInstant)1 RFC_1123_DATE_TIME (java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME)1 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 Arrays.asList (java.util.Arrays.asList)1 Arrays.stream (java.util.Arrays.stream)1 Collections.singletonMap (java.util.Collections.singletonMap)1 Date.from (java.util.Date.from)1 List (java.util.List)1 Optional (java.util.Optional)1