forked from package-url/packageurl-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPurlSpecRefTest.java
More file actions
210 lines (177 loc) · 7.51 KB
/
PurlSpecRefTest.java
File metadata and controls
210 lines (177 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* SPDX-License-Identifier: MIT
* Copyright (c) AboutCode, and contributors. All Rights Reserved.
*/
package com.github.packageurl;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
public class PurlSpecRefTest {
public static class TestSuite {
@JsonProperty("$schema")
public String schema;
public List<TestCase> tests;
}
public static class TestCase {
public String description;
public String test_group;
public String test_type;
public PurlOrComponent input;
public PurlOrComponent expected_output;
public boolean expected_failure;
public String expected_failure_reason;
public TestCase() {
}
}
@JsonDeserialize(using = PurlOrComponentDeserializer.class)
public static class PurlOrComponent {
public String purl;
public PurlComponents components;
}
public static class PurlComponents {
public String type;
public String namespace;
public String name;
public String version;
public Map<String, String> qualifiers;
public String subpath;
}
public static class PurlOrComponentDeserializer extends JsonDeserializer<PurlOrComponent> {
@Override
public PurlOrComponent deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
ObjectCodec codec = p.getCodec();
JsonNode node = codec.readTree(p);
PurlOrComponent value = new PurlOrComponent();
if (node.isTextual()) {
value.purl = node.asText();
} else if (node.isObject()) {
value.components = codec.treeToValue(node, PurlComponents.class);
}
return value;
}
}
static Stream<TestCase> collectTestCases() throws Exception {
ObjectMapper mapper = new ObjectMapper();
URL dirURL = PurlSpecRefTest.class.getClassLoader().getResource("purl-spec/tests/types/");
if (dirURL == null) {
throw new RuntimeException("Resource directory 'purl-spec/tests/types/' not found.");
}
Path testDataPath = Paths.get(dirURL.toURI());
List<Path> jsonFiles = Files.list(testDataPath)
.filter(p -> p.toString().endsWith(".json"))
.collect(Collectors.toList());
Stream.Builder<TestCase> builder = Stream.builder();
for (Path jsonFile : jsonFiles) {
try (InputStream is = Files.newInputStream(jsonFile)) {
TestSuite suite = mapper.readValue(is, TestSuite.class);
suite.tests.forEach(builder::add);
}
}
return builder.build();
}
void runRoundtripTest(TestCase testCase) throws Exception {
String result;
try {
result = new PackageURL(testCase.input.purl).canonicalize();
} catch (Exception e) {
assertTrue(testCase.expected_failure, "Unexpected failure: " + e.getMessage());
return;
}
assertFalse(testCase.expected_failure, "Expected failure but parsing succeeded");
assertEquals(testCase.expected_output.purl, result);
}
void runBuildTest(TestCase testCase) throws Exception {
PurlComponents input = testCase.input.components;
String result;
try {
result = new PackageURL(input.type, input.namespace, input.name, input.version, input.qualifiers,
input.subpath).canonicalize();
} catch (Exception e) {
assertTrue(testCase.expected_failure, "Unexpected failure: " + e.getMessage());
return;
}
assertFalse(testCase.expected_failure, "Expected failure but build succeeded");
assertEquals(testCase.expected_output.purl, result);
}
void runParseTest(TestCase testCase) throws Exception {
PackageURL result;
try {
result = new PackageURL(testCase.input.purl);
} catch (Exception e) {
assertTrue(testCase.expected_failure, "Unexpected failure: " + e.getMessage());
return;
}
assertFalse(testCase.expected_failure, "Expected failure but parsing succeeded");
PurlComponents expected = testCase.expected_output.components;
result.canonicalize();
assertEquals(expected.type, result.getType(), "Type mismatch");
assertEquals(expected.namespace, result.getNamespace(), "Namespace mismatch");
assertEquals(expected.name, result.getName(), "Name mismatch");
assertEquals(expected.version, result.getVersion(), "Version mismatch");
assertEquals(expected.subpath, result.getSubpath(), "Subpath mismatch");
assertEquals(
expected.qualifiers != null ? expected.qualifiers : Collections.emptyMap(),
result.getQualifiers(),
"Qualifiers mismatch");
}
@ParameterizedTest(name = "{0}")
@MethodSource("collectTestCases")
void runTest(TestCase testCase) throws Exception {
switch (testCase.test_type) {
case "roundtrip":
runRoundtripTest(testCase);
break;
case "build":
runBuildTest(testCase);
break;
case "parse":
runParseTest(testCase);
break;
default:
throw new IllegalArgumentException("Unknown test_type: " + testCase.test_type);
}
}
}