|
| 1 | +package ee.bitweb.core.api.model.exception; |
| 2 | + |
| 3 | +import ee.bitweb.core.exception.persistence.Criteria; |
| 4 | +import org.junit.jupiter.api.DisplayName; |
| 5 | +import org.junit.jupiter.api.Tag; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.Arguments; |
| 9 | +import org.junit.jupiter.params.provider.MethodSource; |
| 10 | + |
| 11 | +import java.util.stream.Stream; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.*; |
| 14 | + |
| 15 | +@Tag("unit") |
| 16 | +class CriteriaResponseTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + @DisplayName("Should create from Criteria object") |
| 20 | + void shouldCreateFromCriteriaObject() { |
| 21 | + Criteria criteria = new Criteria("id", "123"); |
| 22 | + |
| 23 | + CriteriaResponse response = new CriteriaResponse(criteria); |
| 24 | + |
| 25 | + assertAll( |
| 26 | + () -> assertEquals("id", response.getField()), |
| 27 | + () -> assertEquals("123", response.getValue()) |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + @DisplayName("Should create with direct parameters") |
| 33 | + void shouldCreateWithDirectParameters() { |
| 34 | + CriteriaResponse response = new CriteriaResponse("email", "test@example.com"); |
| 35 | + |
| 36 | + assertAll( |
| 37 | + () -> assertEquals("email", response.getField()), |
| 38 | + () -> assertEquals("test@example.com", response.getValue()) |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + static Stream<Arguments> comparisonCases() { |
| 43 | + return Stream.of( |
| 44 | + Arguments.of("alpha", "1", "beta", "1", -1, "field alpha < beta"), |
| 45 | + Arguments.of("beta", "1", "alpha", "1", 1, "field beta > alpha"), |
| 46 | + Arguments.of("field", "1", "field", "1", 0, "equal fields and values"), |
| 47 | + Arguments.of("field", "aaa", "field", "bbb", -1, "same field, value aaa < bbb"), |
| 48 | + Arguments.of("field", "bbb", "field", "aaa", 1, "same field, value bbb > aaa") |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + @ParameterizedTest(name = "compareTo: {5}") |
| 53 | + @MethodSource("comparisonCases") |
| 54 | + void shouldCompareCorrectly(String field1, String value1, String field2, String value2, |
| 55 | + int expectedSign, String description) { |
| 56 | + CriteriaResponse first = new CriteriaResponse(field1, value1); |
| 57 | + CriteriaResponse second = new CriteriaResponse(field2, value2); |
| 58 | + |
| 59 | + int result = first.compareTo(second); |
| 60 | + |
| 61 | + assertEquals(expectedSign, Integer.signum(result)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @DisplayName("Should compare case-insensitively") |
| 66 | + void shouldCompareCaseInsensitively() { |
| 67 | + CriteriaResponse lower = new CriteriaResponse("email", "value"); |
| 68 | + CriteriaResponse upper = new CriteriaResponse("EMAIL", "VALUE"); |
| 69 | + |
| 70 | + assertEquals(0, lower.compareTo(upper)); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + @DisplayName("Should handle null field - nulls come first") |
| 75 | + void shouldHandleNullFieldNullsFirst() { |
| 76 | + CriteriaResponse withNull = new CriteriaResponse(null, "value"); |
| 77 | + CriteriaResponse withValue = new CriteriaResponse("field", "value"); |
| 78 | + |
| 79 | + assertTrue(withNull.compareTo(withValue) < 0); |
| 80 | + assertTrue(withValue.compareTo(withNull) > 0); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + @DisplayName("Should handle null value - nulls come first") |
| 85 | + void shouldHandleNullValueNullsFirst() { |
| 86 | + CriteriaResponse withNull = new CriteriaResponse("field", null); |
| 87 | + CriteriaResponse withValue = new CriteriaResponse("field", "value"); |
| 88 | + |
| 89 | + assertTrue(withNull.compareTo(withValue) < 0); |
| 90 | + assertTrue(withValue.compareTo(withNull) > 0); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + @DisplayName("Should handle both null fields") |
| 95 | + void shouldHandleBothNullFields() { |
| 96 | + CriteriaResponse first = new CriteriaResponse(null, "a"); |
| 97 | + CriteriaResponse second = new CriteriaResponse(null, "b"); |
| 98 | + |
| 99 | + assertTrue(first.compareTo(second) < 0); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + @DisplayName("Should handle comparison with null object") |
| 104 | + void shouldHandleComparisonWithNullObject() { |
| 105 | + CriteriaResponse response = new CriteriaResponse("field", "value"); |
| 106 | + |
| 107 | + assertTrue(response.compareTo(null) > 0); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + @DisplayName("Should be equal when field and value match") |
| 112 | + void shouldBeEqualWhenFieldAndValueMatch() { |
| 113 | + CriteriaResponse first = new CriteriaResponse("field", "value"); |
| 114 | + CriteriaResponse second = new CriteriaResponse("field", "value"); |
| 115 | + |
| 116 | + assertEquals(first, second); |
| 117 | + assertEquals(first.hashCode(), second.hashCode()); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + @DisplayName("Should not be equal when field differs") |
| 122 | + void shouldNotBeEqualWhenFieldDiffers() { |
| 123 | + CriteriaResponse first = new CriteriaResponse("field1", "value"); |
| 124 | + CriteriaResponse second = new CriteriaResponse("field2", "value"); |
| 125 | + |
| 126 | + assertNotEquals(first, second); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + @DisplayName("Should not be equal when value differs") |
| 131 | + void shouldNotBeEqualWhenValueDiffers() { |
| 132 | + CriteriaResponse first = new CriteriaResponse("field", "value1"); |
| 133 | + CriteriaResponse second = new CriteriaResponse("field", "value2"); |
| 134 | + |
| 135 | + assertNotEquals(first, second); |
| 136 | + } |
| 137 | +} |
0 commit comments