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 all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ _**For better traceability add the corresponding GitHub issue number in each cha

- XXX Removed EdcNotifiactionMockServiceImpl class and replaced with mocks

### Changed

- XXX updated JsonSchemaTest now the test pulls the latest version of the json file

## [11.0.2 - 29.05.2024]
### Added
- #1010 Made submodel path configurable
Expand Down
18 changes: 18 additions & 0 deletions tx-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,24 @@ SPDX-License-Identifier: Apache-2.0
</resources>
</configuration>
</execution>
<execution>
<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>
<configuration>
<resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,34 @@
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.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;

import static java.util.Objects.isNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

@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,74 @@ public class JsonSchemaTest {
);
private final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();

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

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

List<String> strings = isValid(multipartFile);
for (String string : strings) {
log.info(string);
}
public void testLatestJsonSchema() {
try {
String highestVersionFile = findHighestVersionFile();
log.info("File Version: " + highestVersionFile);
InputStream file = JsonSchemaTest.class.getResourceAsStream("/testdata/jsonfiles/" + highestVersionFile);

MockMultipartFile multipartFile = new MockMultipartFile(
"file", // Parameter name in the multipart request
"import-request.json", // Original file name
"application/json", // Content type
file
);

List<String> errors = isValid(multipartFile);
for (String string : errors) {
log.info(string);
}

assertEquals(1, errors.size());
} catch (Exception e) {
log.error("Exception encountered", e);
fail("Exception encountered: " + e.getMessage());
}

assertEquals(0, strings.size());
}

private String findHighestVersionFile() throws URISyntaxException, IOException {
Path dir = Path.of(JsonSchemaTest.class.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("\\.");

try {
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);
}
}
} catch (NumberFormatException e) {
log.info("Error:" + e);
}

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