From 42649c21e5774835d9407bf2a51dd0ad51b39c23 Mon Sep 17 00:00:00 2001 From: Felipe Lima Date: Tue, 11 Oct 2022 11:08:12 -0300 Subject: [PATCH] feat: add filter by UUID --- .../gustavoleitao/queryapi/ConverterUtil.java | 23 ++++---- .../queryapi/test/BaseClass.java | 4 ++ .../gustavoleitao/queryapi/test/Greeting.java | 5 +- .../queryapi/test/GrettingRepositoryTest.java | 53 +++++++++++-------- 4 files changed, 52 insertions(+), 33 deletions(-) diff --git a/src/main/java/io/github/gustavoleitao/queryapi/ConverterUtil.java b/src/main/java/io/github/gustavoleitao/queryapi/ConverterUtil.java index b7e6b5c..4e01646 100644 --- a/src/main/java/io/github/gustavoleitao/queryapi/ConverterUtil.java +++ b/src/main/java/io/github/gustavoleitao/queryapi/ConverterUtil.java @@ -1,33 +1,36 @@ package io.github.gustavoleitao.queryapi; import java.util.Date; +import java.util.UUID; /** * Classe utilitária de conversão de dados */ public class ConverterUtil { - public static Comparable toComparable(Class clazz, String key, Object data){ + public static Comparable toComparable(Class clazz, String key, Object data) { if (clazz.isEnum()) { return createEnum(clazz, data); - }else if (Boolean.class.isAssignableFrom(clazz) || boolean.class.isAssignableFrom(clazz)){ + } else if (Boolean.class.isAssignableFrom(clazz) || boolean.class.isAssignableFrom(clazz)) { return Boolean.valueOf(data.toString()); - }else if (Integer.class.isAssignableFrom(clazz) || int.class.isAssignableFrom(clazz)){ + } else if (Integer.class.isAssignableFrom(clazz) || int.class.isAssignableFrom(clazz)) { return Integer.valueOf(data.toString()); - }else if (Double.class.isAssignableFrom(clazz) || double.class.isAssignableFrom(clazz)){ + } else if (Double.class.isAssignableFrom(clazz) || double.class.isAssignableFrom(clazz)) { return Double.valueOf(data.toString()); - }else if (Float.class.isAssignableFrom(clazz) || float.class.isAssignableFrom(clazz)){ + } else if (Float.class.isAssignableFrom(clazz) || float.class.isAssignableFrom(clazz)) { return Float.valueOf(data.toString()); - }else if (Short.class.isAssignableFrom(clazz) || short.class.isAssignableFrom(clazz)){ + } else if (Short.class.isAssignableFrom(clazz) || short.class.isAssignableFrom(clazz)) { return Short.valueOf(data.toString()); - }else if (Long.class.isAssignableFrom(clazz) || long.class.isAssignableFrom(clazz)){ + } else if (Long.class.isAssignableFrom(clazz) || long.class.isAssignableFrom(clazz)) { return Long.valueOf(data.toString()); - }else if (Date.class.isAssignableFrom(clazz)){ + } else if (Date.class.isAssignableFrom(clazz)) { return DateUtil.parseDate(data.toString()); - }else if (String.class.isAssignableFrom(clazz)) { + } else if (String.class.isAssignableFrom(clazz)) { return data.toString(); - }else{ + } else if (UUID.class.isAssignableFrom(clazz)) { + return UUID.fromString(data.toString()); + } else { return data.toString(); } diff --git a/src/test/java/io/github/gustavoleitao/queryapi/test/BaseClass.java b/src/test/java/io/github/gustavoleitao/queryapi/test/BaseClass.java index d710c57..dd379cf 100644 --- a/src/test/java/io/github/gustavoleitao/queryapi/test/BaseClass.java +++ b/src/test/java/io/github/gustavoleitao/queryapi/test/BaseClass.java @@ -5,8 +5,10 @@ import lombok.Data; import lombok.NoArgsConstructor; +import javax.persistence.Column; import javax.persistence.MappedSuperclass; import java.io.Serializable; +import java.util.UUID; @Builder @Data @@ -15,6 +17,8 @@ @MappedSuperclass public class BaseClass implements Serializable { + @Column(columnDefinition = "uuid") + protected UUID uuidParam; protected String noIdea; } diff --git a/src/test/java/io/github/gustavoleitao/queryapi/test/Greeting.java b/src/test/java/io/github/gustavoleitao/queryapi/test/Greeting.java index 324a6b9..05d88c5 100644 --- a/src/test/java/io/github/gustavoleitao/queryapi/test/Greeting.java +++ b/src/test/java/io/github/gustavoleitao/queryapi/test/Greeting.java @@ -6,18 +6,19 @@ import javax.persistence.*; import java.util.Date; +import java.util.UUID; @Entity @Data public class Greeting extends BaseClass { public Greeting() { - super("noIdea"); + super(UUID.fromString("00000000-0000-0000-0000-000000000002"),"noIdea"); } @Builder (builderMethodName = "greetingBuilder") public Greeting(String noIdea, Long id, String content, int iValue, double dValue, State state, boolean bValue, Boolean bNonPrimitiveValue, SomeOther other, Date dateValue) { - super(noIdea); + super(UUID.fromString("00000000-0000-0000-0000-000000000002"),"noIdea"); this.id = id; this.content = content; this.iValue = iValue; diff --git a/src/test/java/io/github/gustavoleitao/queryapi/test/GrettingRepositoryTest.java b/src/test/java/io/github/gustavoleitao/queryapi/test/GrettingRepositoryTest.java index a7c6ff3..0636751 100644 --- a/src/test/java/io/github/gustavoleitao/queryapi/test/GrettingRepositoryTest.java +++ b/src/test/java/io/github/gustavoleitao/queryapi/test/GrettingRepositoryTest.java @@ -8,20 +8,20 @@ import org.junit.jupiter.api.TestInstance; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.text.ParseException; import java.util.Arrays; import java.util.List; +import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @SpringBootTest @TestInstance(TestInstance.Lifecycle.PER_CLASS) -public class GrettingRepositoryTest { +class GrettingRepositoryTest { @Autowired private GreetingRepository repository; @@ -64,7 +64,7 @@ public void noFilter() { } @Test - public void likeFilter(){ + public void likeFilter() { var paramters = ParamterBuild.instance() .like("content", "Simple%") .build(); @@ -73,7 +73,7 @@ public void likeFilter(){ } @Test - public void eqStringFilter(){ + public void eqStringFilter() { var paramters = ParamterBuild.instance() .eq("content", "Simple text") .build(); @@ -83,7 +83,7 @@ public void eqStringFilter(){ } @Test - public void eqIntFilter(){ + public void eqIntFilter() { var paramters = ParamterBuild.instance() .eq("iValue", "10") .build(); @@ -93,7 +93,7 @@ public void eqIntFilter(){ } @Test - public void gtIntFilter(){ + public void gtIntFilter() { var paramters = ParamterBuild.instance() .gt("iValue", "10") .build(); @@ -103,7 +103,7 @@ public void gtIntFilter(){ } @Test - public void ltIntFilter(){ + public void ltIntFilter() { var paramters = ParamterBuild.instance() .lt("iValue", "10") .build(); @@ -113,7 +113,7 @@ public void ltIntFilter(){ } @Test - public void geIntFilter(){ + public void geIntFilter() { var paramters = ParamterBuild.instance() .ge("iValue", "11") .build(); @@ -123,7 +123,7 @@ public void geIntFilter(){ } @Test - public void leIntFilter(){ + public void leIntFilter() { var paramters = ParamterBuild.instance() .le("iValue", "9") .build(); @@ -133,7 +133,7 @@ public void leIntFilter(){ } @Test - public void gtDoubleFilter(){ + public void gtDoubleFilter() { var paramters = ParamterBuild.instance() .gt("dValue", "0.66") .build(); @@ -143,7 +143,7 @@ public void gtDoubleFilter(){ } @Test - public void eqEnumFilter(){ + public void eqEnumFilter() { var paramters = ParamterBuild.instance() .eq("state", "OFF") .build(); @@ -154,7 +154,7 @@ public void eqEnumFilter(){ } @Test - public void eqBooleanFilter(){ + public void eqBooleanFilter() { var paramters = ParamterBuild.instance() .eq("bValue", "true") .build(); @@ -165,7 +165,7 @@ public void eqBooleanFilter(){ } @Test - public void eqNonPrimitiveBooleanFilter(){ + public void eqNonPrimitiveBooleanFilter() { var paramters = ParamterBuild.instance() .eq("bNonPrimitiveValue", "true") .build(); @@ -186,7 +186,7 @@ public void gtDateFilter() throws ParseException { } @Test - public void eqOne2OneFilter(){ + public void eqOne2OneFilter() { var paramters = ParamterBuild.instance() .eq("other.text", "random") .build(); @@ -197,7 +197,7 @@ public void eqOne2OneFilter(){ } @Test - public void likeOne2OneFilter(){ + public void likeOne2OneFilter() { var paramters = ParamterBuild.instance() .like("other.text", "rand%") .build(); @@ -208,14 +208,25 @@ public void likeOne2OneFilter(){ } @Test - public void filterInBaseClass(){ + public void filterInBaseClass() { var paramters = ParamterBuild.instance() - .eq("noIdea", "lii") + .eq("state", "ON") .build(); - List result = repository.findAll(new QuerySpecification<>(paramters)); - Assert.assertEquals(1, result.size()); - Assert.assertTrue(result.stream().findAny().orElseThrow(() -> new RuntimeException("Registro não encontrado")) - .getNoIdea().equals("lii")); + String fixedFilter = "{\"uuidParam\": \"" + "00000000-0000-0000-0000-000000000002" + "\"}"; + List result = repository.findAll(new QuerySpecification<>(paramters, fixedFilter)); + Assert.assertEquals(2, result.size()); + Assert.assertEquals(result.stream().findAny().orElseThrow(() -> new RuntimeException("Registro não encontrado")) + .getUuidParam(), UUID.fromString("00000000-0000-0000-0000-000000000002")); + } + + @Test + public void filterInBaseClassUuidParamNotFound() { + var paramters = ParamterBuild.instance() + .eq("state", "ON") + .build(); + String fixedFilter = "{\"uuidParam\": \"" + "00000000-0000-0000-0000-000000000003" + "\"}"; + List result = repository.findAll(new QuerySpecification<>(paramters, fixedFilter)); + Assert.assertEquals(0, result.size()); } @BeforeAll