test(jolt): 添加 Jolt框架相关测试用例- 新增多个测试类,覆盖 Jolt 框架的各种操作:
- TestJoltCardinality: 测试 cardinality 操作 - TestJoltDefault:测试 default 操作 - TestJoltModify: 测试 modify操作 - TestJoltRemove: 测试 remove 操作 - TestJoltShift: 测试 shift 操作 - TestJoltSort: 测试 sort 操作 - 在 pom.xml 中添加 Jolt 相关依赖
This commit is contained in:
parent
9ace3f6737
commit
f01f5cd00a
|
@ -24,6 +24,16 @@
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bazaarvoice.jolt</groupId>
|
||||||
|
<artifactId>jolt-core</artifactId>
|
||||||
|
<version>${latest.jolt.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bazaarvoice.jolt</groupId>
|
||||||
|
<artifactId>json-utils</artifactId>
|
||||||
|
<version>${latest.jolt.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<profiles>
|
<profiles>
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.hzya.frame.jolt;
|
||||||
|
|
||||||
|
import com.bazaarvoice.jolt.Chainr;
|
||||||
|
import com.bazaarvoice.jolt.JsonUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* "cardinality"
|
||||||
|
*
|
||||||
|
* @Author:liuyang
|
||||||
|
* @Package:com.hzya.frame
|
||||||
|
* @Project:fw-nifi
|
||||||
|
* @name:TestJolt
|
||||||
|
* @Date:2025/6/18 11:19
|
||||||
|
* @Filename:TestJolt
|
||||||
|
*/
|
||||||
|
public class TestJoltCardinality {
|
||||||
|
|
||||||
|
private final String jsonFileRootRoute = "/Users/liuyang/workspaces/hzya/fw-nifi/jolt-demo";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ONE:如果输入值是一个列表,则获取该列表中的第一个元素,并将其设置为该元素的数据,不支持其他类型
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo() {
|
||||||
|
//输入json
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/cardinality/input.json");
|
||||||
|
//spec
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/cardinality/spec.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 'MANY':如果输入不是列表,则创建一个列表并将第一个元素设置为输入值
|
||||||
|
* 如果输入是"null",使它成为一个空列表。如果输入是一个列表,不支持
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo2() {
|
||||||
|
//输入json
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/cardinality/input2.json");
|
||||||
|
//spec
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/cardinality/spec2.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
package com.hzya.frame.jolt;
|
||||||
|
|
||||||
|
//import org.flywaydb.core.internal.util.JsonUtils;
|
||||||
|
//import org.flywaydb.core.internal.util.JsonUtils;
|
||||||
|
//import org.flywaydb.core.internal.util.JsonUtils;
|
||||||
|
|
||||||
|
import com.bazaarvoice.jolt.Chainr;
|
||||||
|
import com.bazaarvoice.jolt.JsonUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
//import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* jolt框架的default部分测试,
|
||||||
|
* default是一种为json对象设置默认值的核心操作,当目标json中某个字段不存在,或字段值为null时,
|
||||||
|
* 用你指定的默认值填充该字段,如果字段已存在且值非 null,则不会覆盖原有值
|
||||||
|
*
|
||||||
|
* @Author:liuyang
|
||||||
|
* @Package:com.hzya.frame
|
||||||
|
* @Project:fw-nifi
|
||||||
|
* @name:TestJolt
|
||||||
|
* @Date:2025/6/18 11:19
|
||||||
|
* @Filename:TestJolt
|
||||||
|
*/
|
||||||
|
public class TestJoltDefault {
|
||||||
|
|
||||||
|
private final String jsonFileRootRoute = "/Users/liuyang/workspaces/hzya/fw-nifi/jolt-demo";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 增加一些指定属性
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo() {
|
||||||
|
//输入json
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/default/input.json");
|
||||||
|
//spec
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/default/spec.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 增加一些指定属性
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo2() {
|
||||||
|
//输入json
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/default/input3.json");
|
||||||
|
//spec
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/default/spec3.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.hzya.frame.jolt;
|
||||||
|
|
||||||
|
import com.bazaarvoice.jolt.Chainr;
|
||||||
|
import com.bazaarvoice.jolt.JsonUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* jolt框架的modify部分测试
|
||||||
|
*
|
||||||
|
* @Author:liuyang
|
||||||
|
* @Package:com.hzya.frame
|
||||||
|
* @Project:fw-nifi
|
||||||
|
* @name:TestJolt
|
||||||
|
* @Date:2025/6/18 11:19
|
||||||
|
* @Filename:TestJolt
|
||||||
|
*/
|
||||||
|
public class TestJoltModify {
|
||||||
|
|
||||||
|
private final String jsonFileRootRoute = "/Users/liuyang/workspaces/hzya/fw-nifi/jolt-demo";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDemo() {
|
||||||
|
//输入json
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/modify/input.json");
|
||||||
|
//spec
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/modify/spec.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.hzya.frame.jolt;
|
||||||
|
|
||||||
|
import com.bazaarvoice.jolt.Chainr;
|
||||||
|
import com.bazaarvoice.jolt.JsonUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* "remove" 是一种用于从输入 JSON 中删除指定字段或元素的核心转换操作
|
||||||
|
*
|
||||||
|
* @Author:liuyang
|
||||||
|
* @Package:com.hzya.frame
|
||||||
|
* @Project:fw-nifi
|
||||||
|
* @name:TestJolt
|
||||||
|
* @Date:2025/6/18 11:19
|
||||||
|
* @Filename:TestJolt
|
||||||
|
*/
|
||||||
|
public class TestJoltRemove {
|
||||||
|
|
||||||
|
private final String jsonFileRootRoute = "/Users/liuyang/workspaces/hzya/fw-nifi/jolt-demo";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDemo() {
|
||||||
|
//输入json
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/remove/input.json");
|
||||||
|
//spec
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/remove/spec.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDemo2() {
|
||||||
|
//输入json
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/remove/input3.json");
|
||||||
|
//spec
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/remove/spec3.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,218 @@
|
||||||
|
package com.hzya.frame.jolt;
|
||||||
|
|
||||||
|
//import org.flywaydb.core.internal.util.JsonUtils;
|
||||||
|
//import org.flywaydb.core.internal.util.JsonUtils;
|
||||||
|
//import org.flywaydb.core.internal.util.JsonUtils;
|
||||||
|
|
||||||
|
import com.bazaarvoice.jolt.Chainr;
|
||||||
|
import com.bazaarvoice.jolt.JsonUtils;
|
||||||
|
//import org.flywaydb.core.internal.util.JsonUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
//import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* jolt框架的shift部分测试,Shiftr的基本操作是复制输入JSON的value
|
||||||
|
*
|
||||||
|
* @Author:liuyang
|
||||||
|
* @Package:com.hzya.frame
|
||||||
|
* @Project:fw-nifi
|
||||||
|
* @name:TestJolt
|
||||||
|
* @Date:2025/6/18 11:19
|
||||||
|
* @Filename:TestJolt
|
||||||
|
*/
|
||||||
|
public class TestJoltShift {
|
||||||
|
|
||||||
|
private final String jsonFileRootRoute = "/Users/liuyang/workspaces/hzya/fw-nifi/jolt-demo";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDemo() {
|
||||||
|
//输入json
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/input.json");
|
||||||
|
//spec
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/spec.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字段映射符
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo2() {
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/input2.json");
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/spec2.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* *通配符
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo3() {
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/input3.json");
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/spec3.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDemo4() {
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/input4.json");
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/spec4.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* &通配符
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo5() {
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/input5.json");
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/spec5.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* $通配符
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo6() {
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/input6.json");
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/spec6.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* #通配符,在右边
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo7() {
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/input7.json");
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/spec7.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* #通配符,在左边
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo8() {
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/input8.json");
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/spec8.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* | 通配符,在左边
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo9() {
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/input9.json");
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/spec9.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @通配符,在左边
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo10() {
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/input10.json");
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/spec10.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @通配符,在右边
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo11() {
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/input11.json");
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/spec11.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理输入中的json数组
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo12() {
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/input12.json");
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/spec12.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理输出的json数组
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo13() {
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/input13.json");
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/spec13.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同一个输入源,可以输出到多个目标输出处
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo14() {
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/input14.json");
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/spec14.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 隐式数组
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo15() {
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/input15.json");
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/shift/spec15.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.hzya.frame.jolt;
|
||||||
|
|
||||||
|
import com.bazaarvoice.jolt.Chainr;
|
||||||
|
import com.bazaarvoice.jolt.JsonUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* "Sort":排序场景
|
||||||
|
*
|
||||||
|
* @Author:liuyang
|
||||||
|
* @Package:com.hzya.frame
|
||||||
|
* @Project:fw-nifi
|
||||||
|
* @name:TestJolt
|
||||||
|
* @Date:2025/6/18 11:19
|
||||||
|
* @Filename:TestJolt
|
||||||
|
*/
|
||||||
|
public class TestJoltSort {
|
||||||
|
|
||||||
|
private final String jsonFileRootRoute = "/Users/liuyang/workspaces/hzya/fw-nifi/jolt-demo";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认按字母升序排列(A → Z),排序后键顺序为:a → b → d → r
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDemo() {
|
||||||
|
//输入json
|
||||||
|
Object input = JsonUtils.filepathToObject(jsonFileRootRoute + "/sort/input.json");
|
||||||
|
//spec
|
||||||
|
Object spec = JsonUtils.filepathToObject(jsonFileRootRoute + "/sort/spec.json");
|
||||||
|
|
||||||
|
Chainr chainr = Chainr.fromSpec(spec);
|
||||||
|
Object transform = chainr.transform(input);
|
||||||
|
System.out.println(JsonUtils.toJsonString(transform));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue