Skip to content

Commit

Permalink
Merge pull request #2 from soujava/829-dynamic-query-restrictions
Browse files Browse the repository at this point in the history
Create more test scenarios to the new interfaces
  • Loading branch information
njr-11 authored Nov 26, 2024
2 parents a8707ca + a87a71f commit 8d01623
Show file tree
Hide file tree
Showing 3 changed files with 413 additions and 0 deletions.
130 changes: 130 additions & 0 deletions api/src/test/java/jakarta/data/metamodel/AttributeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package jakarta.data.metamodel;

import jakarta.data.BasicRestriction;
import jakarta.data.Operator;
import jakarta.data.Restriction;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;

import java.util.Set;

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

class AttributeTest {

private final Attribute<String> testAttribute = () -> "testAttribute";
@Test
void shouldCreateEqualToRestriction() {
Restriction<String> restriction = testAttribute.equalTo("testValue");

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(restriction).isInstanceOf(BasicRestriction.class);
BasicRestriction<String> basic = (BasicRestriction<String>) restriction;
soft.assertThat(basic.field()).isEqualTo("testAttribute");
soft.assertThat(basic.value()).isEqualTo("testValue");
soft.assertThat(basic.comparison()).isEqualTo(Operator.EQUAL);
soft.assertThat(basic.isNegated()).isFalse();
});
}

@Test
void shouldCreateNotEqualToRestriction() {
Restriction<String> restriction = testAttribute.notEqualTo("testValue");

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(restriction).isInstanceOf(BasicRestriction.class);
BasicRestriction<String> basic = (BasicRestriction<String>) restriction;
soft.assertThat(basic.field()).isEqualTo("testAttribute");
soft.assertThat(basic.value()).isEqualTo("testValue");
soft.assertThat(basic.comparison()).isEqualTo(Operator.EQUAL);
soft.assertThat(basic.isNegated()).isTrue();
});
}

@Test
void shouldCreateInRestriction() {
Restriction<String> restriction = testAttribute.in("value1", "value2");

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(restriction).isInstanceOf(BasicRestriction.class);
BasicRestriction<String> basic = (BasicRestriction<String>) restriction;
soft.assertThat(basic.field()).isEqualTo("testAttribute");
soft.assertThat(basic.value()).isEqualTo(Set.of("value1", "value2"));
soft.assertThat(basic.comparison()).isEqualTo(Operator.IN);
soft.assertThat(basic.isNegated()).isFalse();
});
}

@Test
void shouldThrowExceptionForEmptyInRestriction() {
assertThatThrownBy(() -> testAttribute.in())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("values are required");
}

@Test
void shouldCreateNotInRestriction() {
Restriction<String> restriction = testAttribute.notIn("value1", "value2");

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(restriction).isInstanceOf(BasicRestriction.class);
BasicRestriction<String> basic = (BasicRestriction<String>) restriction;
soft.assertThat(basic.field()).isEqualTo("testAttribute");
soft.assertThat(basic.value()).isEqualTo(Set.of("value1", "value2"));
soft.assertThat(basic.comparison()).isEqualTo(Operator.IN);
soft.assertThat(basic.isNegated()).isTrue();
});
}

@Test
void shouldThrowExceptionForEmptyNotInRestriction() {
assertThatThrownBy(() -> testAttribute.notIn())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("values are required");
}

@Test
void shouldCreateIsNullRestriction() {
Restriction<String> restriction = testAttribute.isNull();

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(restriction).isInstanceOf(BasicRestriction.class);
BasicRestriction<String> basic = (BasicRestriction<String>) restriction;
soft.assertThat(basic.field()).isEqualTo("testAttribute");
soft.assertThat(basic.value()).isNull();
soft.assertThat(basic.comparison()).isEqualTo(Operator.EQUAL);
soft.assertThat(basic.isNegated()).isFalse();
});
}

@Test
void shouldCreateNotNullRestriction() {
Restriction<String> restriction = testAttribute.notNull();

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(restriction).isInstanceOf(BasicRestriction.class);
BasicRestriction<String> basic = (BasicRestriction<String>) restriction;
soft.assertThat(basic.field()).isEqualTo("testAttribute");
soft.assertThat(basic.value()).isNull();
soft.assertThat(basic.comparison()).isEqualTo(Operator.EQUAL);
soft.assertThat(basic.isNegated()).isTrue();
});
}
}
132 changes: 132 additions & 0 deletions api/src/test/java/jakarta/data/metamodel/SortableAttributeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package jakarta.data.metamodel;

import jakarta.data.BasicRestriction;
import jakarta.data.CompositeRestriction;
import jakarta.data.Operator;
import jakarta.data.Restriction;
import jakarta.data.Sort;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;


class SortableAttributeTest {

//it ignores the implementation of the SortableAttribute interface and uses an anonymous class to test the methods
private final SortableAttribute<String> testAttribute = new SortableAttribute<String>() {
@Override
public Sort<String> asc() {
throw new UnsupportedOperationException("It is not the focus of this test");
}

@Override
public Sort<String> desc() {
throw new UnsupportedOperationException("It is not the focus of this test");
}

@Override
public String name() {
return "testAttribute";
}
};

@Test
void shouldCreateGreaterThanRestriction() {
Restriction<String> restriction = testAttribute.greaterThan(10);

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(restriction).isInstanceOf(BasicRestriction.class);
BasicRestriction<String> basic = (BasicRestriction<String>) restriction;
soft.assertThat(basic.field()).isEqualTo("testAttribute");
soft.assertThat(basic.value()).isEqualTo(10);
soft.assertThat(basic.comparison()).isEqualTo(Operator.GREATER_THAN);
soft.assertThat(basic.isNegated()).isFalse();
});
}

@Test
void shouldCreateGreaterThanEqualRestriction() {
Restriction<String> restriction = testAttribute.greaterThanEqual(10);

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(restriction).isInstanceOf(BasicRestriction.class);
BasicRestriction<String> basic = (BasicRestriction<String>) restriction;
soft.assertThat(basic.field()).isEqualTo("testAttribute");
soft.assertThat(basic.value()).isEqualTo(10);
soft.assertThat(basic.comparison()).isEqualTo(Operator.GREATER_THAN_EQUAL);
soft.assertThat(basic.isNegated()).isFalse();
});
}

@Test
void shouldCreateLessThanRestriction() {
Restriction<String> restriction = testAttribute.lessThan(10);

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(restriction).isInstanceOf(BasicRestriction.class);
BasicRestriction<String> basic = (BasicRestriction<String>) restriction;
soft.assertThat(basic.field()).isEqualTo("testAttribute");
soft.assertThat(basic.value()).isEqualTo(10);
soft.assertThat(basic.comparison()).isEqualTo(Operator.LESS_THAN);
soft.assertThat(basic.isNegated()).isFalse();
});
}

@Test
void shouldCreateLessThanOrEqualRestriction() {
Restriction<String> restriction = testAttribute.lessThanEqual(10);

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(restriction).isInstanceOf(BasicRestriction.class);
BasicRestriction<String> basic = (BasicRestriction<String>) restriction;
soft.assertThat(basic.field()).isEqualTo("testAttribute");
soft.assertThat(basic.value()).isEqualTo(10);
soft.assertThat(basic.comparison()).isEqualTo(Operator.LESS_THAN_EQUAL);
soft.assertThat(basic.isNegated()).isFalse();
});
}

@Test
void shouldCreateBetweenRestriction() {
Restriction<String> restriction = testAttribute.between(5, 15);

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(restriction).isInstanceOf(CompositeRestriction.class);
CompositeRestriction<String> composite = (CompositeRestriction<String>) restriction;
soft.assertThat(composite.type()).isEqualTo(CompositeRestriction.Type.ALL);
soft.assertThat(composite.restrictions()).hasSize(2);

Restriction<String> lowerBound = composite.restrictions().get(0);
soft.assertThat(lowerBound).isInstanceOf(BasicRestriction.class);
BasicRestriction<String> lower = (BasicRestriction<String>) lowerBound;
soft.assertThat(lower.field()).isEqualTo("testAttribute");
soft.assertThat(lower.value()).isEqualTo(5);
soft.assertThat(lower.comparison()).isEqualTo(Operator.GREATER_THAN_EQUAL);
soft.assertThat(lower.isNegated()).isFalse();

Restriction<String> upperBound = composite.restrictions().get(1);
soft.assertThat(upperBound).isInstanceOf(BasicRestriction.class);
BasicRestriction<String> upper = (BasicRestriction<String>) upperBound;
soft.assertThat(upper.field()).isEqualTo("testAttribute");
soft.assertThat(upper.value()).isEqualTo(15);
soft.assertThat(upper.comparison()).isEqualTo(Operator.LESS_THAN_EQUAL);
soft.assertThat(upper.isNegated()).isFalse();
});
}
}
Loading

0 comments on commit 8d01623

Please sign in to comment.