Skip to content

Commit

Permalink
Merge pull request #3 from soujava/829-dynamic-query-restrictions
Browse files Browse the repository at this point in the history
829 dynamic query restrictions
  • Loading branch information
njr-11 authored Nov 27, 2024
2 parents 8d01623 + 014fd07 commit 1e70a1a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
8 changes: 7 additions & 1 deletion api/src/main/java/jakarta/data/BasicRestrictionRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@
// The proper way for users to obtain instances is via
// the static metamodel or Restrict.* methods

import java.util.Objects;

record BasicRestrictionRecord<T>(
String field,
boolean isNegated,
Operator comparison,
Object value) implements BasicRestriction<T> {

BasicRestrictionRecord {
Objects.requireNonNull(field, "Field must not be null");
}

BasicRestrictionRecord(String field, Operator comparison, Object value) {
this(field, false, comparison, value);
}
}
}
8 changes: 7 additions & 1 deletion api/src/main/java/jakarta/data/TextRestrictionRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
// The proper way for users to obtain instances is via
// the static metamodel or Restrict.* methods

import java.util.Objects;

record TextRestrictionRecord<T>(
String field,
boolean isNegated,
Expand All @@ -29,6 +31,10 @@ record TextRestrictionRecord<T>(
boolean isEscaped,
String value) implements TextRestriction<T> {

TextRestrictionRecord {
Objects.requireNonNull(field, "Field must not be null");
}

TextRestrictionRecord(String field, boolean negated, Operator comparison, boolean escaped, String value) {
this(field, negated, comparison, true, escaped, value);
}
Expand All @@ -49,4 +55,4 @@ record TextRestrictionRecord<T>(
public Restriction<T> ignoreCase() {
return new TextRestrictionRecord<>(field, isNegated, comparison, false, isEscaped, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;


class BasicRestrictionRecordTest {

Expand Down Expand Up @@ -72,4 +74,11 @@ void shouldSupportNegatedRestrictionUsingDefaultConstructor() {
soft.assertThat(negatedRestriction.value()).isEqualTo("Unknown");
});
}

@Test
void shouldThrowExceptionWhenFieldIsNull() {
assertThatThrownBy(() -> new BasicRestrictionRecord<>(null, Operator.EQUAL, "testValue"))
.isInstanceOf(NullPointerException.class)
.hasMessage("Field must not be null");
}
}
9 changes: 9 additions & 0 deletions api/src/test/java/jakarta/data/TextRestrictionRecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;


class TextRestrictionRecordTest {

Expand Down Expand Up @@ -119,4 +121,11 @@ void shouldSupportNegationForTextRestriction() {
soft.assertThat(restriction.isEscaped()).isFalse();
});
}

@Test
void shouldThrowExceptionWhenFieldIsNullInTextRestriction() {
assertThatThrownBy(() -> new TextRestrictionRecord<>(null, Operator.LIKE, "testValue"))
.isInstanceOf(NullPointerException.class)
.hasMessage("Field must not be null");
}
}

0 comments on commit 1e70a1a

Please sign in to comment.