Skip to content

Commit

Permalink
Merge pull request #2 from gustavoleitao/add-filter-by-uuid
Browse files Browse the repository at this point in the history
feat: add filter by UUID
  • Loading branch information
gustavoleitao authored Oct 11, 2022
2 parents db527f9 + 42649c2 commit aad8d8d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
23 changes: 13 additions & 10 deletions src/main/java/io/github/gustavoleitao/queryapi/ConverterUtil.java
Original file line number Diff line number Diff line change
@@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,6 +17,8 @@
@MappedSuperclass
public class BaseClass implements Serializable {

@Column(columnDefinition = "uuid")
protected UUID uuidParam;
protected String noIdea;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +64,7 @@ public void noFilter() {
}

@Test
public void likeFilter(){
public void likeFilter() {
var paramters = ParamterBuild.instance()
.like("content", "Simple%")
.build();
Expand All @@ -73,7 +73,7 @@ public void likeFilter(){
}

@Test
public void eqStringFilter(){
public void eqStringFilter() {
var paramters = ParamterBuild.instance()
.eq("content", "Simple text")
.build();
Expand All @@ -83,7 +83,7 @@ public void eqStringFilter(){
}

@Test
public void eqIntFilter(){
public void eqIntFilter() {
var paramters = ParamterBuild.instance()
.eq("iValue", "10")
.build();
Expand All @@ -93,7 +93,7 @@ public void eqIntFilter(){
}

@Test
public void gtIntFilter(){
public void gtIntFilter() {
var paramters = ParamterBuild.instance()
.gt("iValue", "10")
.build();
Expand All @@ -103,7 +103,7 @@ public void gtIntFilter(){
}

@Test
public void ltIntFilter(){
public void ltIntFilter() {
var paramters = ParamterBuild.instance()
.lt("iValue", "10")
.build();
Expand All @@ -113,7 +113,7 @@ public void ltIntFilter(){
}

@Test
public void geIntFilter(){
public void geIntFilter() {
var paramters = ParamterBuild.instance()
.ge("iValue", "11")
.build();
Expand All @@ -123,7 +123,7 @@ public void geIntFilter(){
}

@Test
public void leIntFilter(){
public void leIntFilter() {
var paramters = ParamterBuild.instance()
.le("iValue", "9")
.build();
Expand All @@ -133,7 +133,7 @@ public void leIntFilter(){
}

@Test
public void gtDoubleFilter(){
public void gtDoubleFilter() {
var paramters = ParamterBuild.instance()
.gt("dValue", "0.66")
.build();
Expand All @@ -143,7 +143,7 @@ public void gtDoubleFilter(){
}

@Test
public void eqEnumFilter(){
public void eqEnumFilter() {
var paramters = ParamterBuild.instance()
.eq("state", "OFF")
.build();
Expand All @@ -154,7 +154,7 @@ public void eqEnumFilter(){
}

@Test
public void eqBooleanFilter(){
public void eqBooleanFilter() {
var paramters = ParamterBuild.instance()
.eq("bValue", "true")
.build();
Expand All @@ -165,7 +165,7 @@ public void eqBooleanFilter(){
}

@Test
public void eqNonPrimitiveBooleanFilter(){
public void eqNonPrimitiveBooleanFilter() {
var paramters = ParamterBuild.instance()
.eq("bNonPrimitiveValue", "true")
.build();
Expand All @@ -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();
Expand All @@ -197,7 +197,7 @@ public void eqOne2OneFilter(){
}

@Test
public void likeOne2OneFilter(){
public void likeOne2OneFilter() {
var paramters = ParamterBuild.instance()
.like("other.text", "rand%")
.build();
Expand All @@ -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<Greeting> 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<Greeting> 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<Greeting> result = repository.findAll(new QuerySpecification<>(paramters, fixedFilter));
Assert.assertEquals(0, result.size());
}

@BeforeAll
Expand Down

0 comments on commit aad8d8d

Please sign in to comment.