From 5f6ded12ab4b655828d3e98b89879a7e8219074d Mon Sep 17 00:00:00 2001 From: Justin Bingham Date: Tue, 29 Mar 2022 08:36:10 -0400 Subject: [PATCH] narrow thrown exceptions --- .../sai/httputils/HttpUtils.java | 36 +++++++++++-------- .../sai/httputils/HttpUtilsTests.java | 18 ++++++++++ 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/janeirodigital/sai/httputils/HttpUtils.java b/src/main/java/com/janeirodigital/sai/httputils/HttpUtils.java index b3592f4..e8cec5f 100644 --- a/src/main/java/com/janeirodigital/sai/httputils/HttpUtils.java +++ b/src/main/java/com/janeirodigital/sai/httputils/HttpUtils.java @@ -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()); } /** @@ -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); } @@ -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); } @@ -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); } @@ -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)); } @@ -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); } diff --git a/src/test/java/com/janeirodigital/sai/httputils/HttpUtilsTests.java b/src/test/java/com/janeirodigital/sai/httputils/HttpUtilsTests.java index a7013cc..f8760ed 100644 --- a/src/test/java/com/janeirodigital/sai/httputils/HttpUtilsTests.java +++ b/src/test/java/com/janeirodigital/sai/httputils/HttpUtilsTests.java @@ -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; @@ -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; @@ -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 { @@ -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 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 {