Lowercased fields and use GSON's alternative name to make it work.

This commit is contained in:
Mark Nellemann 2019-08-24 10:52:53 +02:00
parent cb9a603b0e
commit d64e80ecb0
11 changed files with 3184 additions and 3400 deletions

View file

@ -17,7 +17,6 @@ apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray' apply plugin: 'com.jfrog.bintray'
//apply from: "${project.projectDir}/gradle/clover.gradle" //apply from: "${project.projectDir}/gradle/clover.gradle"
version = "${version}-SNAPSHOT"
sourceCompatibility = 1.8 sourceCompatibility = 1.8
targetCompatibility = 1.8 targetCompatibility = 1.8

View file

@ -1,6 +1,6 @@
id = libpaqle id = libpaqle
group = biz.nellemann.libs group = biz.nellemann.libs
version = 1.0.0 version = 1.0.1
licenses = ['APACHE-2.0'] // or something else licenses = ['APACHE-2.0'] // or something else
groovyVersion = 2.5.8 groovyVersion = 2.5.8
slf4jVersion = 1.7.28 slf4jVersion = 1.7.28

View file

@ -25,6 +25,7 @@ import java.util.List;
import biz.nellemann.libpaqle.pojo.PaqleResponse; import biz.nellemann.libpaqle.pojo.PaqleResponse;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -78,25 +79,28 @@ public class Paqle {
} }
protected String getJson(List<String> vatNumbers) throws Exception { protected String getJson(String vatNumber) throws Exception {
String cvrIds = String.join(",", vatNumbers); //String cvrIds = String.join(",", vatNumbers);
String jsonText = get(baseUrl + "/entities?customer=" + company + "&news&cvrIds=" + cvrIds); String jsonText = get(baseUrl + "/entities?customer=" + company + "&news&vatNumbers=" + "DK"+vatNumber);
log.debug("getEntities() response: " + jsonText); log.debug("getJson() response: " + jsonText);
return jsonText; return jsonText;
} }
protected PaqleResponse deserializeJson(String json) throws JsonSyntaxException { protected PaqleResponse deserializeJson(String json) throws JsonSyntaxException {
Gson gson = new Gson(); //Gson gson = new Gson();
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssz")
.create();
PaqleResponse response = gson.fromJson(json, PaqleResponse.class); PaqleResponse response = gson.fromJson(json, PaqleResponse.class);
return response; return response;
} }
protected PaqleResponse getResponse(List<String> vatNumbers) { protected PaqleResponse getResponseByVatNumber(String vatNumber) {
PaqleResponse response; PaqleResponse response;
try { try {
String json = getJson(vatNumbers); String json = getJson(vatNumber);
response = deserializeJson(json); response = deserializeJson(json);
} catch ( Exception e) { } catch ( Exception e) {
log.error("Error", e); log.error("Error", e);

View file

@ -19,15 +19,24 @@
package biz.nellemann.libpaqle.pojo; package biz.nellemann.libpaqle.pojo;
import com.google.gson.annotations.SerializedName;
public class Entity { public class Entity {
@SerializedName(value = "published", alternate = {"Published"})
Boolean published; Boolean published;
@SerializedName(value = "hidden", alternate = {"Hidden"})
Boolean hidden; Boolean hidden;
Integer OriginalId;
Integer CurrentId; @SerializedName(value = "type", alternate = {"Type"})
String type; String type;
@SerializedName(value = "countryCode", alternate = {"CountryCode"})
String countryCode; String countryCode;
String PaqleUrl;
@SerializedName(value = "paqleUrl", alternate = {"PaqleUrl"})
String paqleUrl;
/* /*
"Entities": [ "Entities": [

View file

@ -1,16 +1,35 @@
package biz.nellemann.libpaqle.pojo; package biz.nellemann.libpaqle.pojo;
import com.google.gson.annotations.SerializedName;
import java.util.Date;
import java.util.List; import java.util.List;
public class News { public class News {
Integer EntityOriginalId; @SerializedName(value = "entityOriginalId", alternate = {"EntityOriginalId"})
String ClusterHash; Long entityOriginalId;
String Published;
String LanguageCode; @SerializedName(value = "clusterHash", alternate = {"ClusterHash"})
String clusterHash;
@SerializedName(value = "published", alternate = {"Published"})
Date published;
@SerializedName(value = "languageCode", alternate = {"LanguageCode"})
String languageCode;
@SerializedName(value = "sourceName", alternate = {"SourceName"})
String sourceName; String sourceName;
String Url;
List<NewsHeadline> Headline; @SerializedName(value = "url", alternate = {"Url", "URL"})
String url;
@SerializedName(value = "headline", alternate = {"Headline", "HeadLine"})
List<NewsHeadline> headline;
@SerializedName(value = "extract", alternate = {"Extract"})
List<NewsHeadline> extract;
/* /*
News": [ News": [

View file

@ -1,8 +1,13 @@
package biz.nellemann.libpaqle.pojo; package biz.nellemann.libpaqle.pojo;
import com.google.gson.annotations.SerializedName;
public class NewsExtract { public class NewsExtract {
String Text; @SerializedName(value = "text", alternate = {"Text"})
Boolean Highlight; String text;
@SerializedName(value = "highlight", alternate = {"Highlight", "HighLight"})
Boolean highlight;
} }

View file

@ -1,7 +1,10 @@
package biz.nellemann.libpaqle.pojo; package biz.nellemann.libpaqle.pojo;
import com.google.gson.annotations.SerializedName;
public class NewsHeadline { public class NewsHeadline {
String Text; @SerializedName(value = "text", alternate = {"Text"})
String text;
} }

View file

@ -1,13 +1,18 @@
package biz.nellemann.libpaqle.pojo; package biz.nellemann.libpaqle.pojo;
import com.google.gson.annotations.SerializedName;
import java.util.List; import java.util.List;
// https://entityapi.paqle.net/documentation/entities // https://entityapi.paqle.net/documentation/entities
public class PaqleResponse { public class PaqleResponse {
List<Entity> Entities; @SerializedName(value = "entities", alternate = {"Entities"})
List<News> News; List<Entity> entities;
@SerializedName(value = "news", alternate = {"News"})
List<News> news;
} }

View file

@ -23,18 +23,19 @@ class PaqleSpec extends Specification {
void "test successful parsing of test JSON"() { void "test successful parsing of test JSON"() {
setup: setup:
def testFile = new File(getClass().getResource('/test.json').toURI()) def testFile = new File(getClass().getResource('/15027800.json').toURI())
def testJson = testFile.getText('UTF-8') def testJson = testFile.getText('UTF-8')
mockServer.enqueue(new MockResponse().setBody(testJson)); mockServer.enqueue(new MockResponse().setBody(testJson));
HttpUrl baseUrl = mockServer.url("/"); HttpUrl baseUrl = mockServer.url("/");
paqle.baseUrl = baseUrl.toString() paqle.baseUrl = baseUrl.toString()
when: when:
PaqleResponse response = paqle.getResponse(["2955224", "4004110055"]) PaqleResponse response = paqle.getResponseByVatNumber("15027800")
then: then:
response.Entities.size() == 2 response.entities.size() == 1
response.News.size() == 100 response.news.size() == 100
response.news[0].headline.text.contains('Opkøb skubber til Frejas vækst')
} }
// TODO: Test error-cases, 401, 404, timeouts, empty responses, etc. // TODO: Test error-cases, 401, 404, timeouts, empty responses, etc.

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff