Skip to content

Commit

Permalink
JNG-5883 Add testCases for new Query Customizer functions (#320)
Browse files Browse the repository at this point in the history
* Add testCases for new Query Customizer functions

* add test for or filters
  • Loading branch information
gaborflorian authored Aug 13, 2024
1 parent acf307e commit d36f801
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
*/

import com.google.inject.Inject;
import hu.blackbelt.judo.dao.api.DAO;
import hu.blackbelt.judo.dispatcher.api.FileType;
import hu.blackbelt.judo.meta.asm.runtime.AsmUtils;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.primitives.primitives.myentitywithoptionalfields.*;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.primitives.primitives.myenum.MyEnum;
import hu.blackbelt.judo.psm.generator.sdk.core.test.guice.PrimitivesDaoModules;
import hu.blackbelt.judo.requirement.report.annotation.Requirement;
import hu.blackbelt.judo.runtime.core.jsl.fixture.JudoRuntimeExtension;
import hu.blackbelt.judo.runtime.core.jsl.fixture.JudoRuntimeFixture;
import hu.blackbelt.judo.sdk.query.StringFilter;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -36,8 +39,14 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.*;

@Slf4j
public class QueryCustomizerFunctionsTest {
Expand Down Expand Up @@ -421,11 +430,219 @@ public void testMask() {

assertEquals(entity1.identifier().getIdentifier(), maskedResult.identifier().getIdentifier());
assertEquals(entity1.getIntegerAttr(), maskedResult.getIntegerAttr());
assertEquals(null, maskedResult.getScaledAttr());
assertNull(maskedResult.getScaledAttr());
assertEquals(entity1.getStringAttr(), maskedResult.getStringAttr());
assertEquals(null, maskedResult.getRegexAttr());
assertNull(maskedResult.getRegexAttr());
}

@Test
public void testQueryCustomizerClearMask() {
MyEntityWithOptionalFieldsQueryCustomizer queryCustomizer = myEntityWithOptionalFieldsDao
.query()
.maskedBy(MyEntityWithOptionalFieldsMask.myEntityWithOptionalFieldsMask()
.withStringAttr()
.withIntegerAttr())
.filterByStringAttr(StringFilter.equalTo("test"));

MyEntityWithOptionalFields maskedResult = queryCustomizer
.selectOne()
.orElseThrow();

assertEquals(entity1.identifier().getIdentifier(), maskedResult.identifier().getIdentifier());
assertEquals(entity1.getIntegerAttr(), maskedResult.getIntegerAttr());
assertNull(maskedResult.getScaledAttr());
assertEquals(entity1.getStringAttr(), maskedResult.getStringAttr());
assertNull(maskedResult.getRegexAttr());

MyEntityWithOptionalFields nonMaskedResult = queryCustomizer
.clearMask()
.selectOne()
.orElseThrow();

assertEquals(entity1.identifier().getIdentifier(), nonMaskedResult.identifier().getIdentifier());
assertEquals(entity1.getIntegerAttr(), nonMaskedResult.getIntegerAttr());
assertEquals(entity1.getScaledAttr(), nonMaskedResult.getScaledAttr());
assertEquals(entity1.getStringAttr(), nonMaskedResult.getStringAttr());
assertEquals(entity1.getRegexAttr(), nonMaskedResult.getRegexAttr());
}

@Test
public void testQueryCustomizerClearFilter() {
MyEntityWithOptionalFieldsQueryCustomizer queryCustomizer = myEntityWithOptionalFieldsDao
.query()
.filterByStringAttr(StringFilter.equalTo("test"));

List<MyEntityWithOptionalFields> filteredResult = queryCustomizer
.selectList();

assertEquals(1, filteredResult.size());
assertEquals(entity1.identifier().getIdentifier(), filteredResult.get(0).identifier().getIdentifier());
assertEquals(entity1.getIntegerAttr(), filteredResult.get(0).getIntegerAttr());
assertEquals(entity1.getStringAttr(), filteredResult.get(0).getStringAttr());


List<MyEntityWithOptionalFields> nonFilteredResult = queryCustomizer
.clearFilter()
.selectList();

assertEquals(2, nonFilteredResult.size());

}

@Test
public void testQueryCustomizerClearOrderBy() {
MyEntityWithOptionalFieldsQueryCustomizer queryCustomizer = myEntityWithOptionalFieldsDao
.query()
.orderBy(MyEntityWithOptionalFieldsAttribute.STRING_ATTR);

MyEntityWithOptionalFields orderByResult = queryCustomizer
.selectOne()
.orElseThrow();

assertEquals(entity2.identifier().getIdentifier(), orderByResult.identifier().getIdentifier());

orderByResult = queryCustomizer
.orderBy(MyEntityWithOptionalFieldsAttribute.DERIVED_SCALED_ATTR)
.selectOne()
.orElseThrow();

assertEquals(entity2.identifier().getIdentifier(), orderByResult.identifier().getIdentifier());

orderByResult = queryCustomizer
.clearOrderBy()
.orderByDescending(MyEntityWithOptionalFieldsAttribute.STRING_ATTR)
.orderByDescending(MyEntityWithOptionalFieldsAttribute.DERIVED_SCALED_ATTR)
.selectOne()
.orElseThrow();

assertEquals(entity1.identifier().getIdentifier(), orderByResult.identifier().getIdentifier());

}

@Test
public void testQueryCustomizerCopy() {
MyEntityWithOptionalFieldsQueryCustomizer queryCustomizer = myEntityWithOptionalFieldsDao
.query()
.orderBy(MyEntityWithOptionalFieldsAttribute.STRING_ATTR)
.filterByStringAttr(StringFilter.equalTo("test"))
.maskedBy(MyEntityWithOptionalFieldsMask.myEntityWithOptionalFieldsMask()
.withStringAttr())
;

MyEntityWithOptionalFields orderByResult = queryCustomizer
.selectOne()
.orElseThrow();

assertEquals(entity1.identifier().getIdentifier(), orderByResult.identifier().getIdentifier());
assertEquals(entity1.getStringAttr(), orderByResult.getStringAttr());
assertNull(orderByResult.getScaledAttr());

orderByResult = queryCustomizer
.copy()
.selectOne()
.orElseThrow();

assertEquals(entity1.identifier().getIdentifier(), orderByResult.identifier().getIdentifier());
assertEquals(entity1.getStringAttr(), orderByResult.getStringAttr());
assertNull(orderByResult.getScaledAttr());

queryCustomizer.copy().clearOrderBy().clearMask().clearFilter();

orderByResult = queryCustomizer
.selectOne()
.orElseThrow();

assertEquals(entity1.identifier().getIdentifier(), orderByResult.identifier().getIdentifier());
assertEquals(entity1.getStringAttr(), orderByResult.getStringAttr());
assertNull(orderByResult.getScaledAttr());

}

@Test
public void testQueryCustomizerAppend(JudoRuntimeFixture runtimeFixture) {
AsmUtils asmUtils = new AsmUtils(runtimeFixture.modelHolder.getAsmModel().getResourceSet());

DAO.QueryCustomizer<java.util.UUID> queryCustomizer = DAO.QueryCustomizer.<java.util.UUID>builder()
.orderBy(DAO.OrderBy.builder().descending(false).attribute(asmUtils.resolveAttribute("Primitives.Primitives.MyEntityWithOptionalFields#stringAttr").orElseThrow()).build())
.filter("this.stringAttr!like('tes%') and this.stringAttr!like('%t')")
.mask(Map.of("stringAttr", true))
.build();

MyEntityWithOptionalFields result = myEntityWithOptionalFieldsDao
.query()
.append(queryCustomizer)
.filterBy("this.stringAttr!like('%test%')")
.selectOne()
.orElseThrow();

assertEquals(entity1.identifier().getIdentifier(), result.identifier().getIdentifier());
assertEquals(entity1.getStringAttr(), result.getStringAttr());
assertNull(result.getScaledAttr());

queryCustomizer = DAO.QueryCustomizer.<java.util.UUID>builder()
.orderBy(DAO.OrderBy.builder().descending(false).attribute(asmUtils.resolveAttribute("Primitives.Primitives.MyEntityWithOptionalFields#stringAttr").orElseThrow()).build())
.filter("this.stringAttr!like('tes%') and this.stringAttr!like('%t')")
.build();

result = myEntityWithOptionalFieldsDao
.query()
.append(queryCustomizer)
.selectOne()
.orElseThrow();

assertEquals(entity1.identifier().getIdentifier(), result.identifier().getIdentifier());
assertEquals(entity1.getStringAttr(), result.getStringAttr());
assertNotNull(result.getScaledAttr());

queryCustomizer = DAO.QueryCustomizer.<java.util.UUID>builder()
.orderBy(DAO.OrderBy.builder().descending(false).attribute(asmUtils.resolveAttribute("Primitives.Primitives.MyEntityWithOptionalFields#stringAttr").orElseThrow()).build())
.build();

result = myEntityWithOptionalFieldsDao
.query()
.append(queryCustomizer)
.selectOne()
.orElseThrow();

assertEquals(entity2.identifier().getIdentifier(), result.identifier().getIdentifier());
assertEquals(entity2.getStringAttr(), result.getStringAttr());
assertNotNull(result.getScaledAttr());

}

@Test
public void testQueryCustomizerAppendWithOrFilter(JudoRuntimeFixture runtimeFixture) {
AsmUtils asmUtils = new AsmUtils(runtimeFixture.modelHolder.getAsmModel().getResourceSet());

DAO.QueryCustomizer<java.util.UUID> queryCustomizer = DAO.QueryCustomizer.<java.util.UUID>builder()
.orderBy(DAO.OrderBy.builder().descending(false).attribute(asmUtils.resolveAttribute("Primitives.Primitives.MyEntityWithOptionalFields#stringAttr").orElseThrow()).build())
.filter("this.stringAttr == 'test' or this.stringAttr == 'Another'")
.mask(Map.of("stringAttr", true))
.build();

List<MyEntityWithOptionalFields> result = myEntityWithOptionalFieldsDao
.query()
.append(queryCustomizer)
.selectList();

assertEquals(2, result.size());
assertThat(result.stream().map(e -> e.identifier().getIdentifier()).toList(), containsInAnyOrder(entity1.identifier().getIdentifier(), entity2.identifier().getIdentifier()));
assertNull(result.get(0).getScaledAttr());
assertNull(result.get(1).getScaledAttr());

result = myEntityWithOptionalFieldsDao
.query()
.append(queryCustomizer)
.filterBy("this.stringAttr!like('%test%')")
.selectList();

assertEquals(1, result.size());
assertEquals(entity1.identifier().getIdentifier(), result.get(0).identifier().getIdentifier());
assertNull(result.get(0).getScaledAttr());


}

private void assertOrderBy(MyEntityWithOptionalFieldsAttribute attribute, MyEntityWithOptionalFields firstEntity) {
MyEntityWithOptionalFields orderBy = myEntityWithOptionalFieldsDao
.query()
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

<judo-tatami-jsl-version>1.1.4.20240808_113814_898e9db7_develop</judo-tatami-jsl-version>
<judo-runtime-core-version>1.0.6.20240807_160904_472d7c55_develop</judo-runtime-core-version>
<judo-psm-generator-sdk-core-version>1.0.0.20240809_162212_865aea2f_develop</judo-psm-generator-sdk-core-version>
<judo-psm-generator-sdk-core-version>1.0.0.20240813_134215_aedd4425_feature_JNG_5883_QueryCustomizer_should_have_append_clearFilter_clearOrder_function</judo-psm-generator-sdk-core-version>

<judo-meta-psm-version>1.3.0.20240713_040609_6f9ce2de_develop</judo-meta-psm-version>

Expand Down

0 comments on commit d36f801

Please sign in to comment.