Skip to content

Commit

Permalink
Exclude deprecated model from coverage calculation, add hashCode tests
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Oct 22, 2024
1 parent 9e8ab7f commit fe05f8c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static int hash(Map<Object, Object> stack, BaseModel<?> model) {
stack.put(model, model);

for (Map.Entry<String, Object> e : model.properties.entrySet()) {
result = 31 * result + (e == null ? 0 : hash(stack, e));
result = 31 * result + hash(stack, e);
}

stack.remove(model);
Expand All @@ -58,12 +58,12 @@ private static int hash(Map<Object, Object> stack, Object value) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) value;
for (Map.Entry<String, Object> e : map.entrySet()) {
result = 31 * result + (e == null ? 0 : hash(stack, e));
result = 31 * result + hash(stack, e);
}
} else if (value instanceof List) {
List<?> list = (List<?>) value;
for (Object e : list) {
result = 31 * result + (e == null ? 0 : hash(stack, e));
result = 31 * result + hash(stack, e);
}
} else {
result = Objects.hash(value);
Expand Down
32 changes: 32 additions & 0 deletions model/src/test/java/io/smallrye/openapi/model/BaseModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.microprofile.openapi.models.Constructible;
Expand Down Expand Up @@ -62,4 +64,34 @@ void removeFromNonMap() {

assertEquals("Hello", test.getProperty("test", Object.class));
}

@Test
void testHashCodeEquality() {
TestMapModel test1 = new TestMapModel();
test1.setMapProperty("p1", Map.of("k1", List.of("value1")));
test1.setListProperty("p2", List.of(new TestMapModel()));
test1.setProperty("p3", test1);

TestMapModel test2 = new TestMapModel();
test2.setMapProperty("p1", Map.of("k1", List.of("value1")));
test2.setListProperty("p2", List.of(new TestMapModel()));
test2.setProperty("p3", test2);

assertEquals(test1.hashCode(), test2.hashCode());
}

@Test
void testHashCodeInequality() {
TestMapModel test1 = new TestMapModel();
test1.setMapProperty("p1", Map.of("k1", List.of("value1")));
test1.setListProperty("p2", List.of(new TestMapModel()));
test1.setProperty("p3", test1);

TestMapModel test2 = new TestMapModel();
test2.setMapProperty("p1", Map.of("k1", List.of("value2")));
test2.setListProperty("p2", List.of(new TestMapModel()));
test2.setProperty("p3", test2);

assertNotEquals(test1.hashCode(), test2.hashCode());
}
}
7 changes: 5 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@
<version.resteasy>6.2.3.Final</version.resteasy>
<version.weld.core>5.1.2.Final</version.weld.core>

<!-- Ignore the test data classes in test coverage calculation -->
<sonar.coverage.exclusions>testsuite/data/**/*.java</sonar.coverage.exclusions>
<!-- Ignore the test data classes and deprecated models in test coverage calculation -->
<sonar.coverage.exclusions>
testsuite/data/**/*.java,
core/src/main/java/io/smallrye/openapi/api/models/**/*.java
</sonar.coverage.exclusions>
<!--
Ignore rule java:S119 the checks type parameter names. The generic types in the
io.smallrye.openapi.runtime.io package use two-character type parameter names for the
Expand Down

0 comments on commit fe05f8c

Please sign in to comment.