From cb9a603b0e108b15de5cdbf3971bbf0ab589a188 Mon Sep 17 00:00:00 2001 From: Mark Nellemann Date: Fri, 23 Aug 2019 15:44:31 +0200 Subject: [PATCH] Update README and getResponse(). --- README.md | 5 ++- .../java/biz/nellemann/libpaqle/Paqle.java | 34 ++++++++++--------- .../biz/nellemann/libpaqle/PaqleSpec.groovy | 22 ++++-------- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 40a2075..aa459bf 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ A Java library to query Paqle. Work in progress. ## Usage - TODO. - + Paqle paqle = new Paqle("MyCompany", "myUsername", "myPassword"); + PaqleResponse response = paqle.getResponse(["57020415"]); ### Gradle @@ -17,7 +17,6 @@ A Java library to query Paqle. Work in progress. dependencies { compile 'biz.nellemann.libs:libpaqle:1.+' - runtime "org.slf4j:slf4j-simple:1.7.28" } ## Development diff --git a/src/main/java/biz/nellemann/libpaqle/Paqle.java b/src/main/java/biz/nellemann/libpaqle/Paqle.java index d844760..3a187c2 100644 --- a/src/main/java/biz/nellemann/libpaqle/Paqle.java +++ b/src/main/java/biz/nellemann/libpaqle/Paqle.java @@ -78,12 +78,7 @@ public class Paqle { } - /** - * @param vatNumbers - * @return - * @throws IOException - */ - protected String getJson(List vatNumbers) throws IOException, Exception { + protected String getJson(List vatNumbers) throws Exception { String cvrIds = String.join(",", vatNumbers); String jsonText = get(baseUrl + "/entities?customer=" + company + "&news&cvrIds=" + cvrIds); log.debug("getEntities() response: " + jsonText); @@ -91,16 +86,23 @@ public class Paqle { } - /** - * Use GSON to deserialize JSON into POJO's. - * - * @param json - * @return - */ - protected PaqleResponse getResponse(String json) throws JsonSyntaxException { - Gson gson = new Gson(); - PaqleResponse response = gson.fromJson(json, PaqleResponse.class); - return response; + protected PaqleResponse deserializeJson(String json) throws JsonSyntaxException { + Gson gson = new Gson(); + PaqleResponse response = gson.fromJson(json, PaqleResponse.class); + return response; + } + + + protected PaqleResponse getResponse(List vatNumbers) { + PaqleResponse response; + try { + String json = getJson(vatNumbers); + response = deserializeJson(json); + } catch ( Exception e) { + log.error("Error", e); + return null; + } + return response; } diff --git a/src/test/groovy/biz/nellemann/libpaqle/PaqleSpec.groovy b/src/test/groovy/biz/nellemann/libpaqle/PaqleSpec.groovy index 109b8b8..d821be1 100644 --- a/src/test/groovy/biz/nellemann/libpaqle/PaqleSpec.groovy +++ b/src/test/groovy/biz/nellemann/libpaqle/PaqleSpec.groovy @@ -20,33 +20,23 @@ class PaqleSpec extends Specification { mockServer.shutdown() } - void "test succesfull parsing of test JSON"() { + void "test successful parsing of test JSON"() { setup: def testFile = new File(getClass().getResource('/test.json').toURI()) def testJson = testFile.getText('UTF-8') + mockServer.enqueue(new MockResponse().setBody(testJson)); + HttpUrl baseUrl = mockServer.url("/"); + paqle.baseUrl = baseUrl.toString() when: - PaqleResponse response = paqle.getResponse(testJson) + PaqleResponse response = paqle.getResponse(["2955224", "4004110055"]) then: response.Entities.size() == 2 response.News.size() == 100 } - void "test succesful HTTP GET 15027800"() { - - setup: - mockServer.enqueue(new MockResponse().setBody("{}")); - HttpUrl baseUrl = mockServer.url("/"); - paqle.baseUrl = baseUrl.toString() - - when: - def jsonString = paqle.getJson(["15027800"]) - - then: - jsonString != null - jsonString == "{}" - } + // TODO: Test error-cases, 401, 404, timeouts, empty responses, etc. }