-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chore-XXX: Fixed jsonSchemaTest #1282
Changes from 2 commits
5ee7a1b
bb058ed
19f2d6f
7067243
1f13950
8cc2c80
c759a86
a0521bb
4d01e87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,20 +31,26 @@ | |
import org.eclipse.tractusx.traceability.assets.application.importpoc.validation.exception.JsonFileProcessingException; | ||
import org.eclipse.tractusx.traceability.assets.application.importpoc.validation.exception.NotSupportedSchemaException; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.mock.web.MockMultipartFile; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.nio.file.DirectoryStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.AbstractMap; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
|
@@ -53,8 +59,6 @@ | |
|
||
@Slf4j | ||
public class JsonSchemaTest { | ||
|
||
|
||
public static final Map<String, String> SUPPORTED_SCHEMA_VALIDATION = Map.ofEntries( | ||
Map.entry("urn:samm:io.catenax.batch:3.0.0#Batch", "/schema/semantichub/Batch_3.0.0-schema.json"), | ||
Map.entry("urn:samm:io.catenax.just_in_sequence_part:3.0.0#JustInSequencePart", "/schema/semantichub/JustInSequencePart_3.0.0-schema.json"), | ||
|
@@ -68,29 +72,73 @@ | |
); | ||
private final JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); | ||
|
||
@Value("") | ||
@Test | ||
public void test() throws IOException { | ||
public void test() throws IOException, URISyntaxException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add an appropriate method name |
||
String highestVersionFile = findHighestVersionFile(); | ||
if (highestVersionFile == null) { | ||
throw new IOException("No suitable file found"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is a test, there is no need to throw Exceptions. Maybe use assertNotNull. |
||
log.info("File Version:" + highestVersionFile); | ||
InputStream file = JsonSchemaTest.class.getResourceAsStream("/testdata/jsonfiles/" + highestVersionFile); | ||
if (file == null) { | ||
throw new IOException("Resource not found: /testdata/jsonfiles/" + highestVersionFile); | ||
} | ||
|
||
InputStream file = JsonSchemaTest.class.getResourceAsStream("/testdata/CX_Testdata_MessagingTest_v0.0.13.json"); | ||
// Convert the file to a MockMultipartFile | ||
MockMultipartFile multipartFile = new MockMultipartFile( | ||
"file", // Parameter name in the multipart request | ||
"import-request.json", // Original file name | ||
"application/json", // Content type | ||
"file", // Parameter name in the multipart request | ||
"import-request.json", // Original file name | ||
"application/json", // Content type | ||
file | ||
); | ||
|
||
|
||
List<String> strings = isValid(multipartFile); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use an appropriate variable name instead of strings |
||
for (String string : strings) { | ||
log.info(string); | ||
} | ||
|
||
|
||
assertEquals(0, strings.size()); | ||
assertEquals(1, strings.size()); | ||
|
||
} | ||
|
||
private String findHighestVersionFile() throws URISyntaxException, IOException { | ||
Path dir = Paths.get(getClass().getResource("/testdata/jsonfiles").toURI()); | ||
|
||
Pattern pattern = Pattern.compile("CX_Testdata_MessagingTest_v(\\d+\\.\\d+\\.\\d+)\\.json"); | ||
|
||
String highestVersionFile = null; | ||
String highestVersion = null; | ||
|
||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.json")) { | ||
for (Path entry : stream) { | ||
Matcher matcher = pattern.matcher(entry.getFileName().toString()); | ||
if (matcher.matches()) { | ||
String version = matcher.group(1); | ||
if (highestVersion == null || compareVersions(version, highestVersion) > 0) { | ||
highestVersion = version; | ||
highestVersionFile = entry.getFileName().toString(); | ||
} | ||
} | ||
} | ||
} | ||
return highestVersionFile; | ||
} | ||
|
||
private int compareVersions(String v1, String v2) { | ||
String[] parts1 = v1.split("\\."); | ||
String[] parts2 = v2.split("\\."); | ||
|
||
for (int i = 0; i < parts1.length; i++) { | ||
int part1 = Integer.parseInt(parts1[i]); | ||
|
||
int part2 = Integer.parseInt(parts2[i]); | ||
|
||
|
||
if (part1 != part2) { | ||
return Integer.compare(part1, part2); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
public List<String> isValid(MultipartFile file) { | ||
if (file == null || file.isEmpty()) { | ||
return List.of(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put execution into the already existing plugin.