Skip to content
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

Merged
merged 9 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions tx-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,31 @@ SPDX-License-Identifier: Apache-2.0
<build>
<finalName>traceability-app-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>

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.

<id>copy-testdata</id>
<phase>process-test-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/classes/testdata/jsonfiles</outputDirectory>
<resources>
<resource>
<directory>testdata</directory>
<includes>
<include>CX_Testdata_MessagingTest_v*</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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"),
Expand All @@ -68,29 +72,73 @@
);
private final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();

@Value("")
@Test
public void test() throws IOException {
public void test() throws IOException, URISyntaxException {

Choose a reason for hiding this comment

The 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");
}

Choose a reason for hiding this comment

The 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);

Choose a reason for hiding this comment

The 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());
Fixed Show fixed Hide fixed
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]);
Fixed Show fixed Hide fixed
int part2 = Integer.parseInt(parts2[i]);
Fixed Show fixed Hide fixed

if (part1 != part2) {
return Integer.compare(part1, part2);
}
}

return 0;
}
public List<String> isValid(MultipartFile file) {
if (file == null || file.isEmpty()) {
return List.of();
Expand Down
Loading