-
Notifications
You must be signed in to change notification settings - Fork 6
Home
Arex-compare-sdk is a Java library that can be used to compare two json strings.
-
Out-of-order comparsion Supports out-of-order comparison of arrays with limited configuration.
-
Exclusion/Inclusion Inclusion means that you can specify nodes in the interface you want to compare. Exclusion means that you can specify nodes in the interface you want to ignore.
-
The support for Map
When configuring, use the symbol "*" to replace the variable key
-
Node decompression
Dynamically load decompression classes into SDK with SPI
- COMPILE
git clone https://github.com/arextest/arex-compare-sdk.git
cd arex-compare-sdk
mvn install
- MAVEN
<dependency>
<groupId>io.arex</groupId>
<artifactId>compare-sdk</artifactId>
<version>0.0.1</version>
</dependency>
<repository>
<id>github</id>
<url>https://raw.github.com/arextest/arex-jar-respository/main</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
-
EXAMPLE01
public void example01() { CompareSDK sdk = new CompareSDK(); String str1 = "{\"addressId\":\"123\",\"name\":null," + "\"family\":[{\"id\":1,\"member\":{\"mother\":\"B\",\"father\":\"A\",\"brother\":\"F\",\"sister\":\"D\"},\"stuNum\":{\"total\":\"1\"},\"salaryList\":[\"1\",\"2\"]}," + "{\"id\":2,\"member\":{\"mother\":\"A\",\"father\":\"F\",\"brother\":\"C\",\"sister\":\"E\"},\"stuNum\":{\"total\":\"2\"},\"salaryList\":[\"1\",\"2\"]}]}"; String str2 = "{\"addressId\":\"123\",\"name\":null," + "\"family\":[{\"id\":2,\"member\":{\"mother\":\"B\",\"father\":\"A\",\"brother\":\"F\",\"sister\":\"D\"},\"stuNum\":{\"total\":\"1\"},\"salaryList\":[\"1\",\"2\"]}," + "{\"id\":2,\"member\":{\"mother\":\"A\",\"father\":\"F\",\"brother\":\"C\",\"sister\":\"E\"},\"stuNum\":{\"total\":\"2\"},\"salaryList\":[\"1\",\"2\"]}]}"; CompareResult result = sdk.compare(str1, str2); }
-
EXAMPLE02
public void example02() { CompareSDK sdk = new CompareSDK(); sdk.getGlobalOptions() .putNameToLower(true) .putNullEqualsEmpty(true) .putPluginJarUrl("./lib/arex-compare-sdk-plugin-0.1.0-jar-with-dependencies.jar");; String str1 = "{\"addressId\":\"123\",\"name\":null," + "\"family\":[{\"id\":1,\"member\":{\"mother\":\"B\",\"father\":\"A\",\"brother\":\"F\",\"sister\":\"D\"},\"stuNum\":{\"total\":\"1\"},\"salaryList\":[\"1\",\"2\"]}," + "{\"id\":2,\"member\":{\"mother\":\"A\",\"father\":\"F\",\"brother\":\"C\",\"sister\":\"E\"},\"stuNum\":{\"total\":\"2\"},\"salaryList\":[\"1\",\"2\"]}]," + "\"subObj\":\"H4sIAAAAAAAAAKtWys3PK8lQslIyVdJRqkxNLAIyjQyMDIG8lMRKIMfQXKkWAMavr8AmAAAA\"," + "\"introduceList\":[{\"aid\":{\"id\":1},\"familyHobbies\":[{\"hobbyId\":\"1\"}]},{\"aid\":{\"id\":2},\"familyHobbies\":[{\"hobbyId\":\"2\"}],\"addition\":\"ad\"}],\"nullList\":null,\"view\":18}"; String str2 = "{\"addressId\":\"123\",\"name\":null," + "\"family\":[{\"id\":3,\"member\":{\"mother\":\"A\",\"father\":\"F\",\"brother\":\"C\",\"sister\":\"E\"},\"stuNum\":{\"total\":\"2\"},\"salaryList\":[\"1\",\"2\"]}," + "{\"id\":1,\"member\":{\"mother\":\"B\",\"father\":\"A\",\"brother\":\"C\",\"sister\":\"E\"},\"stuNum\":{\"total\":\"1\"},\"salaryList\":[\"1\",\"2\"]}]," + "\"subObj\":\"H4sIAAAAAAAAAKtWys3PK8lQslIyVdJRqkxNLAIyjQyMDIG8lMRKIMfQXKkWAMavr8AmAAAA\"," + "\"introduceList\":[{\"aid\":{\"id\":3},\"familyHobbies\":[{\"hobbyId\":\"1\"}]},{\"aid\":{\"id\":1},\"familyHobbies\":[{\"hobbyId\":\"2\"}],\"addition\":\"ad\"}],\"nullList\":[],\"view\":17}"; CompareOptions compareOptions = CompareOptions.options() .putListSortConfig(new HashMap<String, String>() {{ put("family", "member\\mother,member\\father"); put("introduceList", "aid\\id"); }}) .putDecompressConfig(new HashMap<String, List<String>>() {{ put("Gzip", Arrays.asList("subObj")); }}) .putExclusions(Arrays.asList("family\\member\\mother", "view")) .putInclusions(Arrays.asList("introduceList\\familyHobbies", "nullList","view")); CompareResult result = sdk.compare(str1, str2, compareOptions); }
-
GLOBALOPTIONS
-
nameToLower
If true, it will change the message and configuration to lowercase, for the inconsistency between the actual message and the contract case
-
nullEqualsEmpty
This option is true, The null, CollectionUtils.isEmpty and Strings.empty are equal. For example:the baseMsg:{"age":""} is consistent with testMsg:"{"age":null}"
-
pluginJarUrl
The url address of the plug-in jar which is specified by the interface. It supports:
- http/https :https://raw.githubusercontent.com/arextest/arex-compare-sdk/main/src/main/resources/lib/arex-compare-sdk-plugin-0.1.0-jar-with-dependencies.jar
- relative/absolute path: ./lib/arex-compare-sdk-plugin-0.1.0-jar-with-dependencies.jar
-
-
CompareOptions
-
inclusions
The path of node that you want to compare, such as "introduceList\familyHobbies"
-
exclusions
The path of node that you want to ignore, such as"view". If the node value is inconsistent but ignore,the field LogTag.ig of the entity LogEntity will be true.
-
listSortConfig
The ordering rules for list nodes. key:the list node path, value:The primary key formed by the child nodes under the list.
-
decompressConfig
The config to decompress
- key:The bean name of the decompression method which is implement the DecompressService interface, you can use an alias
- value:the node path need to decompress such as "subObj"
-
Arex-compare-sdk loads the plug-in jar through SPI
-
interface
package com.arextest.diff.service; public interface DecompressService{ // if not empty, decompressConfig.key need to use alias String getAliasName(); String decompress(String str) throws Throwable; }
-
plugin Jar packaging
The plugin jar needs to be packaged together with the dependent jar, otherwise NoClassDefineFound will appear. Example of packaging plugins in POM
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>make-assembly</id> <!-- this is used for inheritance merges --> <phase>package</phase> <!-- bind to the packaging phase --> <goals> <goal>single</goal> </goals> </execution> </executions> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
Copyright (C) 2022 ArexTest
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see https://www.gnu.org/licenses/.