Skip to content

Commit

Permalink
Add DocumentResolverTest (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
filip26 committed Apr 24, 2021
1 parent a12f8ac commit 21192d3
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.InputStream;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import com.apicatalog.jsonld.JsonLdError;
Expand All @@ -11,6 +13,8 @@

public class DocumentResolver {

private static final Logger LOGGER = Logger.getLogger(DocumentResolver.class.getName());

private MediaType fallbackContentType;

public DocumentResolver() {
Expand All @@ -26,7 +30,15 @@ public DocumentResolver() {
*/
public DocumentReader<InputStream> getReader(MediaType contentType) throws JsonLdError {
return findReader(contentType)
.or(() -> findReader(fallbackContentType))
.or(() -> {

if (fallbackContentType != null) {
LOGGER.log(Level.WARNING, "Content type [{0}] is not supported, trying again with [{1}].", new Object[] { contentType, fallbackContentType});
return findReader(fallbackContentType);
}

return Optional.empty();
})
.orElseThrow(() -> new JsonLdError(JsonLdErrorCode.LOADING_DOCUMENT_FAILED,
"Unsupported media type '" + contentType
+ "'. Supported content types are ["
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/apicatalog/jsonld/loader/FileLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@
public final class FileLoader implements DocumentLoader {

private final DocumentResolver resolver;

public FileLoader() {
this.resolver = new DocumentResolver();
this.resolver.setFallbackContentType(MediaType.JSON);
}

private static final Logger LOGGER = Logger.getLogger(FileLoader.class.getName());
Expand Down Expand Up @@ -99,5 +100,5 @@ private static final Optional<MediaType> detectedContentType(String name) {
}

return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.apicatalog.jsonld.document;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.InputStream;

import org.junit.jupiter.api.Test;

import com.apicatalog.jsonld.JsonLdError;
import com.apicatalog.jsonld.http.media.MediaType;

class DocumentResolverTest {

@Test
void test1() throws JsonLdError {
DocumentReader<InputStream> reader = (new DocumentResolver()).getReader(MediaType.N_QUADS);
assertNotNull(reader);
}

@Test
void test2() throws JsonLdError {
DocumentReader<InputStream> reader = (new DocumentResolver()).getReader(MediaType.JSON_LD);
assertNotNull(reader);
}

@Test
void test3() throws JsonLdError {
DocumentReader<InputStream> reader = (new DocumentResolver()).getReader(MediaType.JSON);
assertNotNull(reader);
}

@Test
void test4() throws JsonLdError {
DocumentReader<InputStream> reader = (new DocumentResolver()).getReader(MediaType.of("application", "test+json"));
assertNotNull(reader);
}

@Test
void test5() throws JsonLdError {
assertThrows(JsonLdError.class, () -> (new DocumentResolver()).getReader(MediaType.HTML));
}

@Test
void test6() throws JsonLdError {
DocumentResolver resolver = new DocumentResolver();
resolver.setFallbackContentType(MediaType.JSON);
DocumentReader<InputStream> reader = resolver.getReader(MediaType.of("text/plain"));
assertNotNull(reader);
}

@Test
void test7() throws JsonLdError {
DocumentResolver resolver = new DocumentResolver();
resolver.setFallbackContentType(MediaType.XHTML);
assertThrows(JsonLdError.class, () -> resolver.getReader(MediaType.of("text/plain")));
}

@Test
void test8() throws JsonLdError {
DocumentResolver resolver = new DocumentResolver();
resolver.setFallbackContentType(MediaType.ANY);
DocumentReader<InputStream> reader = resolver.getReader(MediaType.of("text/plain"));
assertNotNull(reader);
}

@Test
void test9() throws JsonLdError {
DocumentResolver resolver = new DocumentResolver();
resolver.setFallbackContentType(MediaType.XHTML);
DocumentReader<InputStream> reader = resolver.getReader(MediaType.of("text/plain+json"));
assertNotNull(reader);
}

}

0 comments on commit 21192d3

Please sign in to comment.