Skip to content

Commit

Permalink
add json array methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lusoalex committed Dec 21, 2017
1 parent 013a72f commit 28e5159
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 12 deletions.
44 changes: 37 additions & 7 deletions src/main/java/com/github/lusoalex/Chainr.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.github.lusoalex;

import java.util.List;
import java.util.Map;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

/**
* Created by Alexandre on 30/11/2016.
* Created by lusoalex @github.com on 30/11/2016.
*/
public class Chainr {

Expand All @@ -15,18 +17,46 @@ public Chainr(com.bazaarvoice.jolt.Chainr chainr) {
this.proxyChainr = chainr;
}

public JsonObject transform(JsonObject input) {
Map<String, Object> transformed = (Map<String, Object>) proxyChainr.transform(input.getMap());
public Object transform(JsonObject input) {
return proxyChainr.transform(input.getMap());
}

public Object transform(int to, JsonObject input) {
return proxyChainr.transform(to, input.getMap());
}

public Object transform(int from, int to, JsonObject input) {
return proxyChainr.transform(from, to, input.getMap());
}

public JsonObject transformToJsonObject(JsonObject input) {
Map<String, Object> transformed = (Map<String, Object>) transform(input);
return new JsonObject(transformed);
}

public JsonObject transform(int to, JsonObject input) {
Map<String, Object> transformed = (Map<String, Object>) proxyChainr.transform(to, input.getMap());
public JsonObject transformToJsonObject(int to, JsonObject input) {
Map<String, Object> transformed = (Map<String, Object>) transform(to, input);
return new JsonObject(transformed);
}

public JsonObject transform(int from, int to, JsonObject input) {
Map<String, Object> transformed = (Map<String, Object>) proxyChainr.transform(from, to, input.getMap());
public JsonObject transformToJsonObject(int from, int to, JsonObject input) {
Map<String, Object> transformed = (Map<String, Object>) transform(from, to, input);
return new JsonObject(transformed);
}

public JsonArray transformToJsonArray(JsonObject input) {
List<Object> transformed = (List<Object>) transform(input);
return new JsonArray(transformed);
}

public JsonArray transformToJsonArray(int to, JsonObject input) {
List<Object> transformed = (List<Object> ) transform(to, input);
return new JsonArray(transformed);
}

public JsonArray transformToJsonArray(int from, int to, JsonObject input) {
List<Object> transformed = (List<Object> ) transform(from, to, input);
return new JsonArray(transformed);
}

}
62 changes: 62 additions & 0 deletions src/test/java/com/github/lusoalex/ChainrTestJsonArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.github.lusoalex;

import com.github.lusoalex.chainr.ChainrBuilder;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;

/**
* These tests only aim to validate our entry point to jolt.
* JOLT functions are/should be tested into the core project : https://github.com/bazaarvoice/jolt
*/
public class ChainrTestJsonArray {

final static private String RESSOURCE_PATH = "src/test/resources/json/defaultr/json/array/";

@Test
public void testTransformWithFullSpecs() throws IOException {
JsonObject jsonTest = Vertx.vertx().fileSystem().readFileBlocking(RESSOURCE_PATH+"simpleTestCaseOne.json").toJsonObject();

JsonObject input = jsonTest.getJsonObject("input");
JsonArray specs = jsonTest.getJsonArray("specs");
JsonArray expected = jsonTest.getJsonArray("expected");

Chainr chainr = new ChainrBuilder(specs).build();
JsonArray result = chainr.transformToJsonArray(input);

Assert.assertTrue("Jolt mapping result not as expected.", expected.equals(result));
}

@Test
public void splitUsingOnlyFirstSpecs() throws IOException {
JsonObject jsonTest = Vertx.vertx().fileSystem().readFileBlocking(RESSOURCE_PATH+"splitTestCaseOne.json").toJsonObject();

JsonObject input = jsonTest.getJsonObject("input");
JsonArray specs = jsonTest.getJsonArray("specs");
JsonArray expected = jsonTest.getJsonArray("expected");

Chainr chainr = new ChainrBuilder(specs).build();
JsonArray result = chainr.transformToJsonArray(1,input);

Assert.assertTrue("Jolt mapping result not as expected.", expected.equals(result));
}

@Test
public void splitUsingOnlySecondSpecs() throws IOException {
JsonObject jsonTest = Vertx.vertx().fileSystem().readFileBlocking(RESSOURCE_PATH+"splitTestCaseTwo.json").toJsonObject();

JsonObject input = jsonTest.getJsonObject("input");
JsonArray specs = jsonTest.getJsonArray("specs");
JsonArray expected = jsonTest.getJsonArray("expected");

Chainr chainr = new ChainrBuilder(specs).build();
JsonArray result = chainr.transformToJsonArray(0,2,input);

Assert.assertTrue("Jolt mapping result not as expected.", expected.equals(result));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* These tests only aim to validate our entry point to jolt.
* JOLT functions are/should be tested into the core project : https://github.com/bazaarvoice/jolt
*/
public class ChainrTest {
public class ChainrTestJsonObject {

final static private String RESSOURCE_PATH = "src/test/resources/json/defaultr/";
final static private String RESSOURCE_PATH = "src/test/resources/json/defaultr/json/object/";

@Test
public void shiftThenRemove() throws IOException {
Expand All @@ -36,7 +36,7 @@ private void testTransformWithFullSpecs(String fileName) throws IOException {
JsonObject expected = jsonTest.getJsonObject("expected");

Chainr chainr = new ChainrBuilder(specs).build();
JsonObject result = chainr.transform(input);
JsonObject result = chainr.transformToJsonObject(input);

Assert.assertTrue("Jolt mapping result not as expected.", expected.equals(result));
}
Expand All @@ -50,7 +50,7 @@ public void splitUsingOnlyFirstSpecs() throws IOException {
JsonObject expected = jsonTest.getJsonObject("expected");

Chainr chainr = new ChainrBuilder(specs).build();
JsonObject result = chainr.transform(1,input);
JsonObject result = chainr.transformToJsonObject(1,input);

Assert.assertTrue("Jolt mapping result not as expected.", expected.equals(result));
}
Expand All @@ -64,7 +64,7 @@ public void splitUsingOnlySecondSpecs() throws IOException {
JsonObject expected = jsonTest.getJsonObject("expected");

Chainr chainr = new ChainrBuilder(specs).build();
JsonObject result = chainr.transform(1,2,input);
JsonObject result = chainr.transformToJsonObject(1,2,input);

Assert.assertTrue("Jolt mapping result not as expected.", expected.equals(result));
}
Expand Down
75 changes: 75 additions & 0 deletions src/test/resources/json/defaultr/json/array/simpleTestCaseOne.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"input": {
"content": [
{
"name": "Elysee",
"country": "FRANCE",
"address": "Avenue des Champs-Élysées",
"city": "PARIS",
"stores_id": 1,
"zip_code": "75000",
"gps_x": "48.869729",
"gps_y": "2.307784",
"phone_number": "+33 1 00 00 00 00",
"backend_url": "https://north-europe.company.com/"
},
{
"name": "Belem",
"country": "PORTUGAL",
"address": "Avenida Brasilia",
"city": "LISBON",
"stores_id": 2,
"zip_code": "75000",
"gps_x": "38.693060",
"gps_y": "-9.218120",
"phone_number": "+351 000 000 000",
"backend_url": "https://south-europe.company.com/"
},
{
"name": "Yu garden",
"country": "CHINA",
"address": "huanpu qu",
"city": "Shanghai",
"stores_id": 3,
"zip_code": "XXXX",
"gps_x": "31.227208",
"gps_y": "121.491836",
"phone_number": "+86 000 000 000",
"backend_url": "https://south-asia.company.com/"
}
]
},
"specs": [
{
"operation": "shift",
"spec": {
"content": {
"*": {
"stores_id": "[&1].key",
"backend_url": "[&1].value",
"country": "[&1].country"
}
}
}
},
{
"operation": "remove",
"spec": {
"*": {
"country": ""
}
}
}
]
,
"expected": [ {
"key" : 1,
"value" : "https://north-europe.company.com/"
}, {
"key" : 2,
"value" : "https://south-europe.company.com/"
}, {
"key" : 3,
"value" : "https://south-asia.company.com/"
} ]
}
78 changes: 78 additions & 0 deletions src/test/resources/json/defaultr/json/array/splitTestCaseOne.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"input": {
"content": [
{
"name": "Elysee",
"country": "FRANCE",
"address": "Avenue des Champs-Élysées",
"city": "PARIS",
"stores_id": 1,
"zip_code": "75000",
"gps_x": "48.869729",
"gps_y": "2.307784",
"phone_number": "+33 1 00 00 00 00",
"backend_url": "https://north-europe.company.com/"
},
{
"name": "Belem",
"country": "PORTUGAL",
"address": "Avenida Brasilia",
"city": "LISBON",
"stores_id": 2,
"zip_code": "75000",
"gps_x": "38.693060",
"gps_y": "-9.218120",
"phone_number": "+351 000 000 000",
"backend_url": "https://south-europe.company.com/"
},
{
"name": "Yu garden",
"country": "CHINA",
"address": "huanpu qu",
"city": "Shanghai",
"stores_id": 3,
"zip_code": "XXXX",
"gps_x": "31.227208",
"gps_y": "121.491836",
"phone_number": "+86 000 000 000",
"backend_url": "https://south-asia.company.com/"
}
]
},
"specs": [
{
"operation": "shift",
"spec": {
"content": {
"*": {
"stores_id": "[&1].key",
"backend_url": "[&1].value",
"country": "[&1].country"
}
}
}
},
{
"operation": "remove",
"spec": {
"*": {
"country": ""
}
}
}
]
,
"expected": [ {
"country" : "FRANCE",
"key" : 1,
"value" : "https://north-europe.company.com/"
}, {
"country" : "PORTUGAL",
"key" : 2,
"value" : "https://south-europe.company.com/"
}, {
"country" : "CHINA",
"key" : 3,
"value" : "https://south-asia.company.com/"
} ]
}
Loading

0 comments on commit 28e5159

Please sign in to comment.