Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Yevhen Zavhorodnii committed Mar 13, 2024
1 parent b52ba31 commit c2fd1ce
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Map;

import com.structurizr.Workspace;
Expand Down Expand Up @@ -41,6 +43,13 @@ void exportEmptyWorkspaceReturnValidYaml() {
});
}

// TODO: move to a helper class
// those methods are required to check the yaml output is the same and decision to make custom check was made because standard assertEquals does not work with yaml
// issues with standard assertEquals:
// yaml may have different order of elements
// the same value may be represented in different ways (e.g. true/false vs yes/no)
// order of elements in lists may be different
// some fields are not important for the comparison
void assertYamlEquals(String expectedYaml, String actualYaml, String message, String[] ignoreFields) {
Yaml yaml = new Yaml();
Map<String, Object> expected = yaml.load(expectedYaml);
Expand All @@ -49,7 +58,31 @@ void assertYamlEquals(String expectedYaml, String actualYaml, String message, St
Map<String, Object> actual = yaml.load(actualYaml);
excludeFields(actual, ignoreFields);

assertEquals(expected, actual, message);
assertMapEquals(expected, actual, message);
}

@SuppressWarnings("unchecked")
void assertMapEquals(Map<String, Object> expected, Map<String, Object> actual, String message) {
assertEquals(expected.size(), actual.size(), message + ": different size");

expected.forEach((key, value) -> {
if (value instanceof Map) {
assertMapEquals((Map<String, Object>) value, (Map<String, Object>) actual.get(key), message);
} else if (value instanceof ArrayList) {
ArrayList<Object> expectedList = (ArrayList<Object>) value;
ArrayList<Object> actualList = (ArrayList<Object>) actual.get(key);
assertEquals(expectedList.size(), actualList.size(), message + ": different list size");
for (int i = 0; i < expectedList.size(); i++) {
if (expectedList.get(i) instanceof Map) {
assertMapEquals((Map<String, Object>) expectedList.get(i), (Map<String, Object>) actualList.get(i), message);
} else {
assertTrue(actualList.contains(expectedList.get(i)), message);
}
}
} else {
assertEquals(value, actual.get(key), message);
}
});
}

@SuppressWarnings("unchecked")
Expand Down
2 changes: 1 addition & 1 deletion lib/src/test/resources/amazon_threagile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ trust_boundaries:
tags: null
type: execution-environment
technical_assets_inside:
- ta-2
- ta-3
- ta-18
- ta-2
- ta-1
trust_boundaries_nested: null

0 comments on commit c2fd1ce

Please sign in to comment.