Skip to content

Commit

Permalink
Merge pull request #8 from janeirodigital/adjust-exceptions
Browse files Browse the repository at this point in the history
Narrow thrown exceptions
  • Loading branch information
justinwb authored Mar 29, 2022
2 parents ca39e6d + 5f6ded1 commit ab35a2f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/main/java/com/janeirodigital/sai/httputils/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,17 @@ public static Response getRdfResource(OkHttpClient httpClient, URL url, Headers
* @return Jena Model
* @throws SaiHttpException
*/
public static Model getRdfModelFromResponse(Response response) throws SaiHttpException, SaiRdfException {
public static Model getRdfModelFromResponse(Response response) throws SaiHttpException {
Objects.requireNonNull(response, "Must provide a response to get RDF model from");
checkRdfResponse(response);
String body;
HttpUrl requestUrl = response.request().url();
try { body = response.peekBody(Long.MAX_VALUE).string(); } catch (IOException ex) {
throw new SaiHttpException("Failed to access response body", ex);
try {
body = response.peekBody(Long.MAX_VALUE).string();
return getModelFromString(requestUrlToUri(requestUrl), body, getContentType(response).getValue());
} catch (IOException | SaiRdfException ex) {
throw new SaiHttpException("Failed to convert response body to rdf graph", ex);
}
return getModelFromString(requestUrlToUri(requestUrl), body, getContentType(response).getValue());
}

/**
Expand All @@ -240,7 +242,7 @@ public static Model getRdfModelFromResponse(Response response) throws SaiHttpExc
* @return OkHttp Response
* @throws SaiHttpException
*/
public static Response putRdfResource(OkHttpClient httpClient, URL url, Resource resource, ContentType contentType) throws SaiHttpException, SaiRdfException {
public static Response putRdfResource(OkHttpClient httpClient, URL url, Resource resource, ContentType contentType) throws SaiHttpException {
return putRdfResource(httpClient, url, resource, contentType, null, null);
}

Expand All @@ -255,7 +257,7 @@ public static Response putRdfResource(OkHttpClient httpClient, URL url, Resource
* @return OkHttp Response
* @throws SaiHttpException
*/
public static Response putRdfResource(OkHttpClient httpClient, URL url, Resource resource, ContentType contentType, String jsonLdContext) throws SaiHttpException, SaiRdfException {
public static Response putRdfResource(OkHttpClient httpClient, URL url, Resource resource, ContentType contentType, String jsonLdContext) throws SaiHttpException {
return putRdfResource(httpClient, url, resource, contentType, jsonLdContext, null);
}

Expand All @@ -270,7 +272,7 @@ public static Response putRdfResource(OkHttpClient httpClient, URL url, Resource
* @return OkHttp Response
* @throws SaiHttpException
*/
public static Response putRdfResource(OkHttpClient httpClient, URL url, Resource resource, ContentType contentType, Headers headers) throws SaiHttpException, SaiRdfException {
public static Response putRdfResource(OkHttpClient httpClient, URL url, Resource resource, ContentType contentType, Headers headers) throws SaiHttpException {
return putRdfResource(httpClient, url, resource, contentType, null, headers);
}

Expand All @@ -286,16 +288,20 @@ public static Response putRdfResource(OkHttpClient httpClient, URL url, Resource
* @return OkHttp Response
* @throws SaiHttpException
*/
public static Response putRdfResource(OkHttpClient httpClient, URL url, Resource resource, ContentType contentType, String jsonLdContext, Headers headers) throws SaiHttpException, SaiRdfException {
public static Response putRdfResource(OkHttpClient httpClient, URL url, Resource resource, ContentType contentType, String jsonLdContext, Headers headers) throws SaiHttpException {
Objects.requireNonNull(contentType, "Must provide a content-type for the PUT request on an RDF document");
String body = "";
Lang lang = getLangForContentType(contentType.getValue());
if (resource != null) {
if (lang.equals(Lang.JSONLD11)) {
body = getJsonLdStringFromModel(resource.getModel(), jsonLdContext);
} else {
body = getStringFromRdfModel(resource.getModel(), lang);
try {
if (resource != null) {
if (lang.equals(Lang.JSONLD11)) {
body = getJsonLdStringFromModel(resource.getModel(), jsonLdContext);
} else {
body = getStringFromRdfModel(resource.getModel(), lang);
}
}
} catch (SaiRdfException ex) {
throw new SaiHttpException("Unable to get string from rdf model", ex);
}
return checkResponse(putResource(httpClient, url, headers, body, contentType));
}
Expand All @@ -309,12 +315,12 @@ public static Response putRdfResource(OkHttpClient httpClient, URL url, Resource
* @return OkHttp Response
* @throws SaiHttpException
*/
public static Response putRdfContainer(OkHttpClient httpClient, URL url, Resource resource, ContentType contentType, String jsonLdContext) throws SaiHttpException, SaiRdfException {
public static Response putRdfContainer(OkHttpClient httpClient, URL url, Resource resource, ContentType contentType, String jsonLdContext) throws SaiHttpException {
Headers headers = addLinkRelationHeader(LinkRelation.TYPE, LDP_BASIC_CONTAINER);
return putRdfResource(httpClient, url, resource, contentType, jsonLdContext, headers);
}

public static Response putRdfContainer(OkHttpClient httpClient, URL url, Resource resource, ContentType contentType) throws SaiHttpException, SaiRdfException {
public static Response putRdfContainer(OkHttpClient httpClient, URL url, Resource resource, ContentType contentType) throws SaiHttpException {
return putRdfContainer(httpClient, url, resource, contentType, null);
}

Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/janeirodigital/sai/httputils/HttpUtilsTests.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.janeirodigital.sai.httputils;

import com.janeirodigital.mockwebserver.RequestMatchingFixtureDispatcher;
import com.janeirodigital.sai.rdfutils.RdfUtils;
import com.janeirodigital.sai.rdfutils.SaiRdfException;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
Expand All @@ -12,6 +13,8 @@
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.junit.jupiter.api.*;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import java.io.IOException;
import java.net.MalformedURLException;
Expand All @@ -31,6 +34,8 @@
import static com.janeirodigital.sai.httputils.HttpUtils.*;
import static com.janeirodigital.sai.rdfutils.RdfUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;

public class HttpUtilsTests {

Expand Down Expand Up @@ -264,6 +269,19 @@ void updateJsonLdHttpResource() throws SaiHttpException, SaiRdfException {
assertTrue(response.isSuccessful());
}

@Test
@DisplayName("Fail to update a JSON-LD RDF resource")
void failToUpdateJsonLdHttpResource() throws SaiRdfException {
URL url = toUrl(server, "/put-jsonld-resource");
Model readableModel = getModelFromString(urlToUri(url), getJsonLdString(url), LD_JSON);
Resource resource = getResourceFromModel(readableModel, url);
try (MockedStatic<RdfUtils> mockUtils = Mockito.mockStatic(RdfUtils.class, Mockito.CALLS_REAL_METHODS)) {
mockUtils.when(() -> RdfUtils.getJsonLdStringFromModel(any(Model.class), anyString())).thenThrow(SaiRdfException.class);
assertThrows(SaiHttpException.class, () -> putRdfResource(httpClient, url, resource, ContentType.LD_JSON, ""));
}

}

@Test
@DisplayName("Create an RDF container")
void createRdfContainerHttpResource() throws SaiHttpException, SaiRdfException {
Expand Down

0 comments on commit ab35a2f

Please sign in to comment.