Archived
0

implement EDDN parser

This commit is contained in:
iMoHax
2016-09-16 15:14:33 +03:00
parent 7913c742b3
commit 53c7f09b54
20 changed files with 1310 additions and 37 deletions

View File

@@ -0,0 +1,29 @@
package ru.trader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class TestUtils {
private final static Logger LOG = LoggerFactory.getLogger(TestUtils.class);
public static String read(InputStream is){
StringBuilder builder = new StringBuilder();
String line;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))){
while ((line = reader.readLine()) != null){
if (builder.length() > 0){
builder.append("\n");
}
builder.append(line);
}
} catch (IOException e) {
LOG.error("Error on read file", e);
}
return builder.toString();
}
}

View File

@@ -0,0 +1,258 @@
package ru.trader.emdn;
import com.fasterxml.jackson.core.JsonParseException;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.trader.TestUtils;
import ru.trader.emdn.entities.*;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Collection;
public class ParserTest extends Assert {
private final static Logger LOG = LoggerFactory.getLogger(ParserTest.class);
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void testParseV1() throws Exception {
EMDNParser parser = new EMDNParser();
try (InputStream is = getClass().getResourceAsStream("/emdn/v1.json")) {
String json = TestUtils.read(is);
Message message = parser.parse(json);
assertNotNull(message);
assertEquals("http://schemas.elite-markets.net/eddn/commodity/1", message.getSchemaRef());
Header header = message.getHeader();
assertNotNull(header);
assertEquals("abcdef0123456789", header.getUploaderId());
assertEquals("My Awesome Market Uploader", header.getSoftwareName());
assertEquals("v3.14", header.getSoftwareVersion());
assertEquals(LocalDateTime.of(2014, 11, 17, 13, 35), header.getGatewayTimestamp());
Body body = message.getBody();
assertNotNull(body);
assertEquals(LocalDateTime.of(2014, 11, 17, 12, 34, 56), body.getTimestamp());
StarSystem system = body.getSystem();
assertNotNull(system);
assertEquals("Eranin", system.getName());
assertNull(system.getId());
assertNull(system.getAddress());
Station station = body.getStation();
assertNotNull(station);
assertEquals("Azeban Orbital", station.getName());
assertNull(station.getId());
Collection<Item> items = body.getCommodities();
assertNotNull(items);
assertEquals(1, items.size());
Item item = items.iterator().next();
assertNotNull(item);
assertEquals("Gold", item.getName());
assertNull(item.getId());
assertEquals(1024, item.getBuyPrice());
assertEquals(7, item.getSupply());
assertEquals(LEVEL_TYPE.LOW, item.getSupplyLevel());
assertEquals(1138, item.getSellPrice());
assertEquals(42, item.getDemand());
assertEquals(LEVEL_TYPE.MEDIUM, item.getDemandLevel());
}
}
@Test
public void testParseV2() throws Exception {
EMDNParser parser = new EMDNParser();
try (InputStream is = getClass().getResourceAsStream("/emdn/v2.json")) {
String json = TestUtils.read(is);
Message message = parser.parse(json);
assertNotNull(message);
assertEquals("http://schemas.elite-markets.net/eddn/commodity/2", message.getSchemaRef());
Header header = message.getHeader();
assertNotNull(header);
assertEquals("abcdef0123456789", header.getUploaderId());
assertEquals("My Awesome Market Uploader", header.getSoftwareName());
assertEquals("v3.14", header.getSoftwareVersion());
assertEquals(LocalDateTime.of(2014, 11, 17, 13, 35), header.getGatewayTimestamp());
Body body = message.getBody();
assertNotNull(body);
assertEquals(LocalDateTime.of(2014, 11, 17, 12, 34, 56), body.getTimestamp());
StarSystem system = body.getSystem();
assertNotNull(system);
assertEquals("Eranin", system.getName());
assertNull(system.getId());
assertNull(system.getAddress());
Station station = body.getStation();
assertNotNull(station);
assertEquals("Azeban Orbital", station.getName());
assertNull(station.getId());
Collection<Item> items = body.getCommodities();
assertNotNull(items);
assertEquals(2, items.size());
int found = 0;
for (Item item : items) {
assertNotNull(item);
if ("Gold".equals(item.getName())){
found++;
assertNull(item.getId());
assertEquals(1024, item.getBuyPrice());
assertEquals(7, item.getSupply());
assertEquals(LEVEL_TYPE.LOW, item.getSupplyLevel());
assertEquals(1138, item.getSellPrice());
assertEquals(42, item.getDemand());
assertEquals(LEVEL_TYPE.MEDIUM, item.getDemandLevel());
} else
if ("Explosives".equals(item.getName())){
found++;
assertNull(item.getId());
assertEquals(999, item.getBuyPrice());
assertEquals(1500, item.getSupply());
assertEquals(LEVEL_TYPE.LOW, item.getSupplyLevel());
assertEquals(0, item.getSellPrice());
assertEquals(0, item.getDemand());
assertNull(item.getDemandLevel());
}
}
assertEquals("Expected items not found", 2, found);
}
}
@Test
public void testParseV1Min() throws Exception {
EMDNParser parser = new EMDNParser();
try (InputStream is = getClass().getResourceAsStream("/emdn/v1m.json")) {
String json = TestUtils.read(is);
Message message = parser.parse(json);
assertNotNull(message);
assertEquals("http://schemas.elite-markets.net/eddn/commodity/1", message.getSchemaRef());
Header header = message.getHeader();
assertNotNull(header);
assertEquals("abcdef0123456789", header.getUploaderId());
assertEquals("My Awesome Market Uploader", header.getSoftwareName());
assertEquals("v3.14", header.getSoftwareVersion());
assertNull(header.getGatewayTimestamp());
Body body = message.getBody();
assertNotNull(body);
assertEquals(LocalDateTime.of(2014, 11, 17, 12, 34, 56), body.getTimestamp());
StarSystem system = body.getSystem();
assertNotNull(system);
assertEquals("Eranin", system.getName());
assertNull(system.getId());
assertNull(system.getAddress());
Station station = body.getStation();
assertNotNull(station);
assertEquals("Azeban Orbital", station.getName());
assertNull(station.getId());
Collection<Item> items = body.getCommodities();
assertNotNull(items);
assertEquals(1, items.size());
Item item = items.iterator().next();
assertNotNull(item);
assertEquals("Gold", item.getName());
assertNull(item.getId());
assertEquals(1024, item.getBuyPrice());
assertEquals(7, item.getSupply());
assertNull(item.getSupplyLevel());
assertEquals(1138, item.getSellPrice());
assertEquals(42, item.getDemand());
assertNull(item.getDemandLevel());
}
}
@Test
public void testParseV2Min() throws Exception {
EMDNParser parser = new EMDNParser();
try (InputStream is = getClass().getResourceAsStream("/emdn/v2m.json")) {
String json = TestUtils.read(is);
Message message = parser.parse(json);
assertNotNull(message);
assertEquals("http://schemas.elite-markets.net/eddn/commodity/2", message.getSchemaRef());
Header header = message.getHeader();
assertNotNull(header);
assertEquals("abcdef0123456789", header.getUploaderId());
assertEquals("My Awesome Market Uploader", header.getSoftwareName());
assertEquals("v3.14", header.getSoftwareVersion());
assertNull(header.getGatewayTimestamp());
Body body = message.getBody();
assertNotNull(body);
assertEquals(LocalDateTime.of(2014, 11, 17, 12, 34, 56), body.getTimestamp());
StarSystem system = body.getSystem();
assertNotNull(system);
assertEquals("Eranin", system.getName());
assertNull(system.getId());
assertNull(system.getAddress());
Station station = body.getStation();
assertNotNull(station);
assertEquals("Azeban Orbital", station.getName());
assertNull(station.getId());
Collection<Item> items = body.getCommodities();
assertNotNull(items);
assertEquals(1, items.size());
for (Item item : items) {
assertNotNull(item);
assertEquals("Gold", item.getName());
assertNull(item.getId());
assertEquals(1024, item.getBuyPrice());
assertEquals(7, item.getSupply());
assertNull(item.getSupplyLevel());
assertEquals(1138, item.getSellPrice());
assertEquals(42, item.getDemand());
assertNull(item.getDemandLevel());
}
}
}
@Test
public void testParseV1Error() throws Exception {
EMDNParser parser = new EMDNParser();
try (InputStream is = getClass().getResourceAsStream("/emdn/v1e.json")) {
String json = TestUtils.read(is);
Message message = parser.parse(json);
assertNull(message);
}
}
@Test
public void testParseV2Error() throws Exception {
EMDNParser parser = new EMDNParser();
try (InputStream is = getClass().getResourceAsStream("/emdn/v2e.json")) {
String json = TestUtils.read(is);
Message message = parser.parse(json);
assertNull(message);
}
}
@Test
public void testParseError() throws Exception {
EMDNParser parser = new EMDNParser();
String json = "Eranin sdfe adsf";
exception.expect(JsonParseException.class);
parser.parse(json);
}
@Test
public void testParseError2() throws Exception {
EMDNParser parser = new EMDNParser();
String json = "{\n" +
" \"$schemaRef\": \"http://schemas.elite-markets.net/eddn/commodity/2\",\n" +
" \"header\": {\n" +
" \"uploaderID\": \"abcdef0123456789\",\n" +
" \"softwareName\": \"My Aweso";
exception.expect(JsonParseException.class);
parser.parse(json);
}
}

View File

@@ -0,0 +1,117 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://schemas.elite-markets.net/eddn/commodity/1#",
"type": "object",
"additionalProperties": false,
"properties": {
"$schemaRef": {
"id": "#$schemaRef",
"type": "string",
"additionalProperties": false
},
"header": {
"id": "#header",
"type": "object",
"additionalProperties": true,
"properties": {
"uploaderID": {
"id": "#uploaderID",
"type": "string",
"additionalProperties": false
},
"softwareName": {
"id": "#softwareName",
"type": "string",
"additionalProperties": false
},
"softwareVersion": {
"id": "#softwareVersion",
"type": "string",
"additionalProperties": false
},
"gatewayTimestamp": {
"id": "#gatewayTimestamp",
"type": "string",
"format": "date-time",
"description": "Timestamp upon receipt at the gateway. If present, this property will be overwritten by the gateway; submitters are not intended to populate this property.",
"additionalProperties": false
}
}
},
"message": {
"id": "#message",
"type": "object",
"additionalProperties": true,
"properties": {
"systemName": {
"id": "#systemName",
"type": "string",
"additionalProperties": false
},
"stationName": {
"id": "#stationName",
"type": "string",
"additionalProperties": false
},
"itemName": {
"id": "#itemName",
"type": "string",
"additionalProperties": false
},
"buyPrice": {
"id": "#buyPrice",
"type": "integer",
"description": "Price to buy from the market",
"additionalProperties": false
},
"stationStock": {
"id": "#stationStock",
"type": "integer",
"additionalProperties": false
},
"supplyLevel": {
"$ref": "#/definitions/levelType"
},
"sellPrice": {
"id": "#sellPrice",
"type": "integer",
"description": "Price to sell to the market",
"additionalProperties": false
},
"demand": {
"id": "#demand",
"type": "integer",
"additionalProperties": false
},
"demandLevel": {
"$ref": "#/definitions/levelType"
},
"timestamp": {
"id": "#timestamp",
"type": "string",
"format": "date-time",
"additionalProperties": false
}
},
"required": [
"systemName",
"stationName",
"itemName",
"stationStock",
"sellPrice",
"demand",
"timestamp"
]
}
},
"required": [
"$schemaRef",
"header",
"message"
],
"definitions" : {
"levelType" : {
"enum": ["Low", "Med", "High"]
}
}
}

View File

@@ -0,0 +1,119 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://schemas.elite-markets.net/eddn/commodity/2#",
"type": "object",
"additionalProperties": false,
"properties": {
"$schemaRef": {
"type": "string",
"additionalProperties": false
},
"header": {
"type": "object",
"additionalProperties": true,
"properties": {
"uploaderID": {
"type": "string",
"additionalProperties": false
},
"softwareName": {
"type": "string",
"additionalProperties": false
},
"softwareVersion": {
"type": "string",
"additionalProperties": false
},
"gatewayTimestamp": {
"type": "string",
"format": "date-time",
"description": "Timestamp upon receipt at the gateway. If present, this property will be overwritten by the gateway; submitters are not intended to populate this property.",
"additionalProperties": false
}
}
},
"message": {
"type": "object",
"additionalProperties": false,
"properties": {
"systemName": {
"type": "string",
"additionalProperties": false
},
"stationName": {
"type": "string",
"additionalProperties": false
},
"timestamp": {
"type": "string",
"format": "date-time",
"additionalProperties": false
},
"commodities": {
"type": "array",
"additionalProperties": false,
"items": {
"oneOf": [
{
"type" : "object",
"properties": {
"name": {
"type": "string",
"additionalProperties": false
},
"buyPrice": {
"type": "integer",
"description": "Price to buy from the market",
"additionalProperties": false
},
"supply": {
"type": "integer",
"additionalProperties": false
},
"supplyLevel": {
"$ref": "#/definitions/levelType"
},
"sellPrice": {
"type": "integer",
"description": "Price to sell to the market",
"additionalProperties": false
},
"demand": {
"type": "integer",
"additionalProperties": false
},
"demandLevel": {
"$ref": "#/definitions/levelType"
}
},
"required": [
"name",
"buyPrice",
"supply",
"sellPrice",
"demand"
]
}
]
}
}
},
"required": [
"systemName",
"stationName",
"commodities",
"timestamp"
]
}
},
"required": [
"$schemaRef",
"header",
"message"
],
"definitions" : {
"levelType" : {
"enum": ["Low", "Med", "High"]
}
}
}

View File

@@ -0,0 +1,114 @@
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"id" : "http://schemas.elite-markets.net/eddn/commodity/3#",
"type" : "object",
"additionalProperties" : false,
"required": [ "$schemaRef", "header", "message" ],
"properties": {
"$schemaRef": {
"type" : "string"
},
"header": {
"type" : "object",
"additionalProperties" : true,
"required" : [ "uploaderID", "softwareName", "softwareVersion" ],
"properties" : {
"uploaderID": {
"type" : "string"
},
"softwareName": {
"type" : "string"
},
"softwareVersion": {
"type" : "string"
},
"gatewayTimestamp": {
"type" : "string",
"format" : "date-time",
"description" : "Timestamp upon receipt at the gateway. If present, this property will be overwritten by the gateway; submitters are not intended to populate this property."
}
}
},
"message": {
"type" : "object",
"additionalProperties" : false,
"required" : [ "system", "station", "timestamp", "commodities" ],
"properties" : {
"system" : {
"$ref" : "#/definitions/systemWithId"
},
"station" : {
"$ref" : "#/definitions/nameWithId"
},
"timestamp": {
"type" : "string",
"format" : "date-time"
},
"commodities": {
"type" : "array",
"minItems" : 1,
"items" : {
"oneOf" : [
{
"type" : "object",
"additionalProperties" : false,
"required" : [ "name", "buyPrice", "supply", "sellPrice", "demand" ],
"properties" : {
"name": {
"type" :"string",
"minLength" :1
},
"id": {
"type" : "integer",
"description" : "If present should be treated by clients as taking precedence over \"name\""
},
"buyPrice": {
"type" :"integer",
"description" :"Price to buy from the market"
},
"supply": {
"type" : "integer"
},
"supplyLevel": {
"$ref" : "#/definitions/levelType"
},
"sellPrice": {
"type" : "integer",
"description" : "Price to sell to the market"
},
"demand":{
"type":"integer"
},
"demandLevel":{
"$ref":"#/definitions/levelType"
}
}
}
]
}
}
}
}
},
"definitions": {
"systemWithId" : {
"type" :"object",
"additionalProperties" : false,
"required" : [ "name" ],
"properties" : {
"name" : { "type": "string", "minLength" : 1 },
"id" : { "type": "integer", "description" : "If present should be treated by clients as taking precedence over \"name\"" },
"address" : { "type": "integer" }
}
},
"nameWithId" : {
"type" :"object",
"additionalProperties" : false,
"required" : [ "name" ],
"properties" : {
"name" : { "type": "string", "minLength" : 1 },
"id" : { "type": "integer", "description" : "If present should be treated by clients as taking precedence over \"name\"" }
}
}
}
}

View File

@@ -0,0 +1,21 @@
{
"$schemaRef": "http://schemas.elite-markets.net/eddn/commodity/1",
"header": {
"uploaderID": "abcdef0123456789",
"softwareName": "My Awesome Market Uploader",
"softwareVersion": "v3.14",
"gatewayTimestamp": "2014-11-17T13:35:00+00:00"
},
"message": {
"systemName": "Eranin",
"stationName": "Azeban Orbital",
"itemName": "Gold",
"buyPrice": 1024,
"stationStock": 7,
"supplyLevel": "Low",
"sellPrice": 1138,
"demand": 42,
"demandLevel": "Med",
"timestamp": "2014-11-17T12:34:56+00:00"
}
}

View File

@@ -0,0 +1,13 @@
{
"$schemaRef": "http://schemas.elite-markets.net/eddn/commodity/1",
"header": {
},
"message": {
"itemName": "Gold",
"buyPrice": 1024,
"stationStock": 7,
"sellPrice": 1138,
"demand": 42,
"timestamp": "2014-11-17T12:34:56+00:00"
}
}

View File

@@ -0,0 +1,18 @@
{
"$schemaRef": "http://schemas.elite-markets.net/eddn/commodity/1",
"header": {
"uploaderID": "abcdef0123456789",
"softwareName": "My Awesome Market Uploader",
"softwareVersion": "v3.14"
},
"message": {
"systemName": "Eranin",
"stationName": "Azeban Orbital",
"itemName": "Gold",
"buyPrice": 1024,
"stationStock": 7,
"sellPrice": 1138,
"demand": 42,
"timestamp": "2014-11-17T12:34:56+00:00"
}
}

View File

@@ -0,0 +1,33 @@
{
"$schemaRef": "http://schemas.elite-markets.net/eddn/commodity/2",
"header": {
"uploaderID": "abcdef0123456789",
"softwareName": "My Awesome Market Uploader",
"softwareVersion": "v3.14",
"gatewayTimestamp": "2014-11-17T13:35:00+00:00"
},
"message": {
"systemName": "Eranin",
"stationName": "Azeban Orbital",
"timestamp": "2014-11-17T12:34:56+00:00",
"commodities": [
{
"name": "Gold",
"buyPrice": 1024,
"supply": 7,
"supplyLevel": "Low",
"sellPrice": 1138,
"demand": 42,
"demandLevel": "Med"
},
{
"name": "Explosives",
"buyPrice": 999,
"supply": 1500,
"supplyLevel": "Low",
"sellPrice": 0,
"demand": 0
}
]
}
}

View File

@@ -0,0 +1,13 @@
{
"$schemaRef": "http://schemas.elite-markets.net/eddn/commodity/2",
"header": {
"uploaderID": "abcdef0123456789",
"softwareName": "My Awesome Market Uploader",
"softwareVersion": "v3.14"
},
"message": {
"systemName": "Eranin",
"stationName": "Azeban Orbital",
"timestamp": "2014-11-17T12:34:56+00:00"
}
}

View File

@@ -0,0 +1,22 @@
{
"$schemaRef": "http://schemas.elite-markets.net/eddn/commodity/2",
"header": {
"uploaderID": "abcdef0123456789",
"softwareName": "My Awesome Market Uploader",
"softwareVersion": "v3.14"
},
"message": {
"systemName": "Eranin",
"stationName": "Azeban Orbital",
"timestamp": "2014-11-17T12:34:56+00:00",
"commodities": [
{
"name": "Gold",
"buyPrice": 1024,
"supply": 7,
"sellPrice": 1138,
"demand": 42
}
]
}
}