Skip to content

Commit bee7e8a

Browse files
authored
Merge pull request #6 from filip26/feat/fr
Add Tests
2 parents 2a5e04f + cf54aa3 commit bee7e8a

13 files changed

+151
-5
lines changed

.github/workflows/codacy-coverage.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This workflow will build a Java project with Maven
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
3+
4+
name: Codacy Coverage
5+
6+
on:
7+
push:
8+
branches: [ main ]
9+
10+
jobs:
11+
build:
12+
permissions:
13+
contents: read
14+
pull-requests: write
15+
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- uses: actions/checkout@v4
20+
- name: Set up JDK 8
21+
uses: actions/setup-java@v4
22+
with:
23+
java-version: 8
24+
distribution: 'temurin'
25+
- name: Build with Maven
26+
run: mvn -B package jacoco:report
27+
- name: Run codacy-coverage-reporter
28+
uses: codacy/codacy-coverage-reporter-action@v1.3.0
29+
with:
30+
project-token: ${{secrets.CODACY_PROJECT_TOKEN}}
31+
coverage-reports: ./target/site/jacoco/jacoco.xml

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
An implementation of the [RFC 8785 JSON Canonicalization Scheme (JCS)](https://www.rfc-editor.org/rfc/rfc8785) specification in Java, utilizing [Jakarta JSON Processing](https://github.com/eclipse-ee4j/jsonp) as input. The canonical version is written to a provided `Writer`.
44

5+
Formerly part of [titanium-json-ld](https://github.com/filip26/titanium-json-ld).
6+
57
## Resources
68

79
- [RFC 8785 JSON Canonicalization Scheme (JCS)](https://www.rfc-editor.org/rfc/rfc8785)

pom.xml

+9-3
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@
2222
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
2323
<argLine>-Dfile.encoding=UTF-8</argLine>
2424

25-
<jakarta.json.version>2.1.3</jakarta.json.version>
25+
<jakarta.json.version>2.0.1</jakarta.json.version>
2626

2727

2828
<!-- test resources -->
2929
<junit.version>5.12.0</junit.version>
3030
</properties>
3131

32-
<name>Copper Multcodec</name>
32+
<name>Titanium JCS</name>
3333

3434
<description>
35-
Multicodec &amp; Multihash API, Registry
35+
An implementation of the RFC 8785 JSON Canonicalization Scheme (JCS) specification in Java, utilizing Jakarta JSON Processing.
3636
</description>
3737

3838
<licenses>
@@ -80,6 +80,12 @@
8080
<version>${junit.version}</version>
8181
<scope>test</scope>
8282
</dependency>
83+
<dependency>
84+
<groupId>org.glassfish</groupId>
85+
<artifactId>jakarta.json</artifactId>
86+
<version>${jakarta.json.version}</version>
87+
<scope>test</scope>
88+
</dependency>
8389
</dependencies>
8490

8591
<build>

src/main/java/com/apicatalog/jcs/JsonCanonicalizer.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public static final void canonize(final JsonValue value, final Writer writer) th
7474

7575
if (valueType == null || ValueType.NULL == valueType) {
7676
writer.write("null");
77+
return;
7778
}
7879

7980
switch (valueType) {
@@ -127,7 +128,7 @@ public static final void canonizeArray(final JsonArray value, final Writer write
127128

128129
writer.write("[");
129130

130-
for (JsonValue item : value.asJsonArray()) {
131+
for (final JsonValue item : value.asJsonArray()) {
131132

132133
if (next) {
133134
writer.write(",");
@@ -139,7 +140,6 @@ public static final void canonizeArray(final JsonArray value, final Writer write
139140
}
140141

141142
writer.write("]");
142-
143143
}
144144

145145
private static final void canonizeObject(final JsonObject value, final Writer writer) throws IOException {
@@ -151,6 +151,7 @@ private static final void canonizeObject(final JsonObject value, final Writer wr
151151

152152
if (properties != null && !properties.isEmpty()) {
153153
final ArrayList<String> sortedProperties = new ArrayList<>(properties);
154+
154155
Collections.sort(sortedProperties);
155156

156157
for (final String propertyName : sortedProperties) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.apicatalog.jcs;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.BufferedInputStream;
6+
import java.io.BufferedReader;
7+
import java.io.IOException;
8+
import java.io.InputStreamReader;
9+
import java.nio.charset.StandardCharsets;
10+
import java.util.stream.Collectors;
11+
12+
import org.junit.jupiter.params.ParameterizedTest;
13+
import org.junit.jupiter.params.provider.ValueSource;
14+
15+
import jakarta.json.Json;
16+
import jakarta.json.JsonReader;
17+
import jakarta.json.JsonValue;
18+
19+
class JsonCanonicalizerTest {
20+
21+
@ParameterizedTest
22+
@ValueSource(strings = { "primitive-data-types", "uni-sort", "array", "object" })
23+
void testCanonize(String name) throws IOException {
24+
assertEquals(getResource(name + ".out.json"), JsonCanonicalizer.canonize(getJson(name + ".in.json")));
25+
}
26+
27+
static String getResource(String name) throws IOException {
28+
try (BufferedInputStream is = new BufferedInputStream(JsonCanonicalizerTest.class.getResourceAsStream(name))) {
29+
return new BufferedReader(
30+
new InputStreamReader(is, StandardCharsets.UTF_8))
31+
.lines()
32+
.collect(Collectors.joining("\n"));
33+
}
34+
}
35+
36+
static JsonValue getJson(String name) {
37+
try (JsonReader reader = Json.createReader(
38+
JsonCanonicalizerTest.class.getResourceAsStream(name))) {
39+
return reader.read();
40+
}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
56
3+
,
4+
{
5+
"d": true,
6+
"10": null,
7+
"1": [ ],
8+
"": false
9+
}
10+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[56,{"":false,"1":[],"10":null,"d":true}]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"1": {
3+
"f": {
4+
"f": "hi",
5+
"F": 5
6+
},
7+
"\n": 56.0
8+
},
9+
"10": {
10+
11+
},
12+
"": "empty",
13+
"a": {
14+
15+
},
16+
"111": [
17+
{
18+
"e": "yes",
19+
"E": "no"
20+
}
21+
],
22+
"A": {
23+
"b": "123"
24+
},
25+
"České": "ěšĎů"
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"":"empty","1":{"\n":56,"f":{"F":5,"f":"hi"}},"10":{},"111":[{"E":"no","e":"yes"}],"A":{"b":"123"},"a":{},"České":"ěšĎů"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"numbers": [
3+
333333333.33333329,
4+
1E30,
5+
4.50,
6+
2e-3,
7+
0.000000000000000000000000001
8+
],
9+
"string": "\u20ac$\u000F\u000aA'\u0042\u0022\u005c\\\"\/",
10+
"literals": [
11+
null,
12+
true,
13+
false
14+
]
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"literals":[null,true,false],"numbers":[333333333.3333333,1e+30,4.5,0.002,1e-27],"string":"€$\u000f\nA'B\"\\\\\"/"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"\u20ac": "Euro Sign",
3+
"\r": "Carriage Return",
4+
"\ufb33": "Hebrew Letter Dalet With Dagesh",
5+
"1": "One",
6+
"\ud83d\ude00": "Emoji: Grinning Face",
7+
"\u0080": "Control",
8+
"\u00f6": "Latin Small Letter O With Diaeresis"
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"\r":"Carriage Return","1":"One","€":"Control","ö":"Latin Small Letter O With Diaeresis","€":"Euro Sign","😀":"Emoji: Grinning Face","דּ":"Hebrew Letter Dalet With Dagesh"}

0 commit comments

Comments
 (0)