org.springframework.data.domain.ExampleMatcher Java Examples

The following examples show how to use org.springframework.data.domain.ExampleMatcher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ArangoRepositoryTest.java    From spring-data with Apache License 2.0 7 votes vote down vote up
@Test
public void findAllSortByExampleTest() {
	final List<Customer> toBeRetrieved = new LinkedList<>();
	toBeRetrieved.add(new Customer("A", "Z", 0));
	toBeRetrieved.add(new Customer("B", "C", 0));
	toBeRetrieved.add(new Customer("B", "D", 0));
	repository.saveAll(toBeRetrieved);
	repository.save(new Customer("A", "A", 1));
	final Example<Customer> example = Example.of(new Customer("", "", 0),
		ExampleMatcher.matchingAny().withIgnoreNullValues().withIgnorePaths(new String[] { "location", "alive" }));
	final List<Sort.Order> orders = new LinkedList<>();
	orders.add(new Sort.Order(Sort.Direction.ASC, "name"));
	orders.add(new Sort.Order(Sort.Direction.ASC, "surname"));
	final Sort sort = Sort.by(orders);
	final Iterable<Customer> retrieved = repository.findAll(example, sort);
	assertTrue(equals(toBeRetrieved, retrieved, cmp, eq, true));
}
 
Example #2
Source File: ArangoRepositoryTest.java    From spring-data with Apache License 2.0 6 votes vote down vote up
@Test
public void endingWithByExampleNestedTest() {
	final List<Customer> toBeRetrieved = new LinkedList<>();
	final Customer check = new Customer("Abba", "Bbaaaa", 100);
	final Customer nested = new Customer("$B*\\wa?[a.b]baaa", "", 67);
	final Customer nested2 = new Customer("qwerty", "", 10);
	nested2.setAddress(new Address("123456"));
	nested.setNestedCustomer(nested2);
	check.setNestedCustomer(nested);
	toBeRetrieved.add(check);
	toBeRetrieved.add(new Customer("B", "", 43));
	toBeRetrieved.add(new Customer("C", "", 76));
	repository.saveAll(toBeRetrieved);
	final Customer exampleCustomer = new Customer("Abba", "Bbaaaa", 100);
	final Customer nested3 = new Customer("B*\\wa?[a.b]baAa", null, 66);
	nested3.setNestedCustomer(nested2);
	exampleCustomer.setNestedCustomer(nested3);
	final Example<Customer> example = Example.of(exampleCustomer,
		ExampleMatcher.matching().withMatcher("nestedCustomer.name", match -> match.endsWith())
				.withIgnoreCase("nestedCustomer.name").withIgnoreNullValues()
				.withTransformer("nestedCustomer.age", o -> Optional.of(Integer.valueOf(o.get().toString()) + 1)));
	final Customer retrieved = repository.findOne(example).get();
	assertEquals(check, retrieved);
}
 
Example #3
Source File: DatastoreTemplate.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
private <T> void validateExample(Example<T> example) {
	Assert.notNull(example, "A non-null example is expected");

	ExampleMatcher matcher = example.getMatcher();
	if (!matcher.isAllMatching()) {
		throw new DatastoreDataException("Unsupported MatchMode. Only MatchMode.ALL is supported");
	}
	if (matcher.isIgnoreCaseEnabled()) {
		throw new DatastoreDataException("Ignore case matching is not supported");
	}
	if (!(matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.EXACT
			|| matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.DEFAULT)) {
		throw new DatastoreDataException("Unsupported StringMatcher. Only EXACT and DEFAULT are supported");
	}

	Optional<String> path =
			example.getMatcher().getIgnoredPaths().stream().filter((s) -> s.contains(".")).findFirst();
	if (path.isPresent()) {
		throw new DatastoreDataException("Ignored paths deeper than 1 are not supported");
	}
	if (matcher.getPropertySpecifiers().hasValues()) {
		throw new DatastoreDataException("Property matchers are not supported");
	}
}
 
Example #4
Source File: PassengerRepositoryIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenPassengers_whenFindByIgnoringMatcher_thenExpectedReturned() {
    Passenger jill = Passenger.from("Jill", "Smith", 50);
    Passenger eve = Passenger.from("Eve", "Jackson", 95);
    Passenger fred = Passenger.from("Fred", "Bloggs", 22);
    Passenger siya = Passenger.from("Siya", "Kolisi", 85);
    Passenger ricki = Passenger.from("Ricki", "Bobbie", 36);

    ExampleMatcher ignoringExampleMatcher = ExampleMatcher.matchingAny().withMatcher("lastName",
        ExampleMatcher.GenericPropertyMatchers.startsWith().ignoreCase()).withIgnorePaths("firstName", "seatNumber");

    Example<Passenger> example = Example.of(Passenger.from(null, "b", null),
        ignoringExampleMatcher);

    List<Passenger> passengers = repository.findAll(example);

    assertThat(passengers, contains(fred, ricki));
    assertThat(passengers, not(contains(jill)));
    assertThat(passengers, not(contains(eve)));
    assertThat(passengers, not(contains(siya)));
}
 
Example #5
Source File: ArangoRepositoryTest.java    From spring-data with Apache License 2.0 6 votes vote down vote up
@Test
public void exampleWithRefPropertyTest() {

	ShoppingCart shoppingCart = new ShoppingCart();
	shoppingCartRepository.save(shoppingCart);

	Customer customer = new Customer("Dhiren", "Upadhyay", 28);
	customer.setShoppingCart(shoppingCart);
	repository.save(customer);

	Customer customer1 = new Customer();
	customer1.setShoppingCart(shoppingCart);
	ExampleMatcher exampleMatcher = ExampleMatcher.matching().withIgnorePaths("age", "location", "alive");
	Example<Customer> example = Example.of(customer1, exampleMatcher);

	final Customer retrieved = repository.findOne(example).orElse(null);
	assertEquals(customer, retrieved);
}
 
Example #6
Source File: ArangoRepositoryTest.java    From spring-data with Apache License 2.0 6 votes vote down vote up
@Test
public void endingWithByExampleNestedIncludeNullTest() {
	final List<Customer> toBeRetrieved = new LinkedList<>();
	final Customer check = new Customer("Abba", "Bbaaaa", 100);
	final Customer nested = new Customer("$B*\\wa?[a.b]baaa", "", 67);
	final Customer nested2 = new Customer("qwerty", "", 10);
	nested2.setAddress(new Address("123456"));
	nested.setNestedCustomer(nested2);
	check.setNestedCustomer(nested);
	toBeRetrieved.add(check);
	toBeRetrieved.add(new Customer("B", "", 43));
	toBeRetrieved.add(new Customer("C", "", 76));
	repository.saveAll(toBeRetrieved);
	final Customer exampleCustomer = new Customer("Abba", "Bbaaaa", 100);
	final Customer nested3 = new Customer("B*\\wa?[a.b]baAa", "", 66);
	nested3.setNestedCustomer(nested2);
	exampleCustomer.setNestedCustomer(nested3);
	final Example<Customer> example = Example.of(exampleCustomer,
		ExampleMatcher.matching().withMatcher("nestedCustomer.name", match -> match.endsWith())
				.withIgnorePaths(new String[] { "arangoId", "id", "key", "rev" })
				.withIgnoreCase("nestedCustomer.name").withIncludeNullValues()
				.withTransformer("nestedCustomer.age", o -> Optional.of(Integer.valueOf(o.get().toString()) + 1)));
	final Customer retrieved = repository.findOne(example).get();
	assertEquals(check, retrieved);
}
 
Example #7
Source File: AddressService.java    From resilient-transport-service with Apache License 2.0 6 votes vote down vote up
public Address validateAddress(AddressDTO addressDTO) {

        Address addressToSearch = new Address(addressDTO.getCountry(), addressDTO.getCity(), addressDTO.getPostcode(),
            addressDTO.getStreet(), addressDTO.getStreetNumber());

        //@formatter:off
        ExampleMatcher matcher =
            ExampleMatcher.matching()
                    .withMatcher("country", startsWith().ignoreCase())
                    .withMatcher("postcode", startsWith().ignoreCase())
                    .withMatcher("street", contains().ignoreCase())
                    .withMatcher("streetNumber", contains().ignoreCase())
                    .withMatcher("city", contains().ignoreCase());

        //@formatter:on
        Example<Address> searchExample = Example.of(addressToSearch, matcher);

        return addressRepository.findOne(searchExample);

    }
 
Example #8
Source File: UserRepositoryTest.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
@Test
public void testExample() {

    UserEntity admin = new UserEntity();
    admin.setUserName("example");
    admin.setLoginName("example");
    admin.setPassword(BCryptPassWordUtils.encode("example"));
    admin.setEmail("[email protected]");

    ExampleMatcher matcher = ExampleMatcher.matching()
            .withMatcher("userName", endsWith())
            .withMatcher("loginName", startsWith().ignoreCase());

    Example<UserEntity> example = Example.of(admin, matcher);

    MyFastJsonUtils.prettyPrint(userRepository.findAll(example));
    System.out.println(userRepository.count(example));

}
 
Example #9
Source File: UseCase1DTOService.java    From celerio-angular-quickstart with Apache License 2.0 6 votes vote down vote up
@Transactional(readOnly = true)
public PageResponse<UseCase1DTO> findAll(PageRequestByExample<UseCase1DTO> req) {
    Example<UseCase1> example = null;
    UseCase1 useCase1 = toEntity(req.example);

    if (useCase1 != null) {
        ExampleMatcher matcher = ExampleMatcher.matching() //
                .withMatcher(UseCase1_.dummy.getName(), match -> match.ignoreCase().startsWith());

        example = Example.of(useCase1, matcher);
    }

    Page<UseCase1> page;
    if (example != null) {
        page = useCase1Repository.findAll(example, req.toPageable());
    } else {
        page = useCase1Repository.findAll(req.toPageable());
    }

    List<UseCase1DTO> content = page.getContent().stream().map(this::toDTO).collect(Collectors.toList());
    return new PageResponse<>(page.getTotalPages(), page.getTotalElements(), content);
}
 
Example #10
Source File: RoleDTOService.java    From celerio-angular-quickstart with Apache License 2.0 6 votes vote down vote up
@Transactional(readOnly = true)
public PageResponse<RoleDTO> findAll(PageRequestByExample<RoleDTO> req) {
    Example<Role> example = null;
    Role role = toEntity(req.example);

    if (role != null) {
        ExampleMatcher matcher = ExampleMatcher.matching() //
                .withMatcher(Role_.roleName.getName(), match -> match.ignoreCase().startsWith());

        example = Example.of(role, matcher);
    }

    Page<Role> page;
    if (example != null) {
        page = roleRepository.findAll(example, req.toPageable());
    } else {
        page = roleRepository.findAll(req.toPageable());
    }

    List<RoleDTO> content = page.getContent().stream().map(this::toDTO).collect(Collectors.toList());
    return new PageResponse<>(page.getTotalPages(), page.getTotalElements(), content);
}
 
Example #11
Source File: UseCase3DTOService.java    From celerio-angular-quickstart with Apache License 2.0 6 votes vote down vote up
@Transactional(readOnly = true)
public PageResponse<UseCase3DTO> findAll(PageRequestByExample<UseCase3DTO> req) {
    Example<UseCase3> example = null;
    UseCase3 useCase3 = toEntity(req.example);

    if (useCase3 != null) {
        ExampleMatcher matcher = ExampleMatcher.matching() //
                .withMatcher(UseCase3_.dummy.getName(), match -> match.ignoreCase().startsWith());

        example = Example.of(useCase3, matcher);
    }

    Page<UseCase3> page;
    if (example != null) {
        page = useCase3Repository.findAll(example, req.toPageable());
    } else {
        page = useCase3Repository.findAll(req.toPageable());
    }

    List<UseCase3DTO> content = page.getContent().stream().map(this::toDTO).collect(Collectors.toList());
    return new PageResponse<>(page.getTotalPages(), page.getTotalElements(), content);
}
 
Example #12
Source File: BookDTOService.java    From celerio-angular-quickstart with Apache License 2.0 6 votes vote down vote up
@Transactional(readOnly = true)
public PageResponse<BookDTO> findAll(PageRequestByExample<BookDTO> req) {
    Example<Book> example = null;
    Book book = toEntity(req.example);

    if (book != null) {
        ExampleMatcher matcher = ExampleMatcher.matching() //
                .withMatcher(Book_.title.getName(), match -> match.ignoreCase().startsWith())
                .withMatcher(Book_.summary.getName(), match -> match.ignoreCase().startsWith());

        example = Example.of(book, matcher);
    }

    Page<Book> page;
    if (example != null) {
        page = bookRepository.findAll(example, req.toPageable());
    } else {
        page = bookRepository.findAll(req.toPageable());
    }

    List<BookDTO> content = page.getContent().stream().map(this::toDTO).collect(Collectors.toList());
    return new PageResponse<>(page.getTotalPages(), page.getTotalElements(), content);
}
 
Example #13
Source File: ArangoRepositoryTest.java    From spring-data with Apache License 2.0 6 votes vote down vote up
@Test
public void countByExampleTest() {
	final List<Customer> toBeRetrieved = new LinkedList<>();
	toBeRetrieved.add(new Customer("A", "Z", 0));
	toBeRetrieved.add(new Customer("B", "X", 0));
	toBeRetrieved.add(new Customer("B", "Y", 0));
	toBeRetrieved.add(new Customer("C", "V", 0));
	toBeRetrieved.add(new Customer("D", "T", 0));
	toBeRetrieved.add(new Customer("D", "U", 0));
	toBeRetrieved.add(new Customer("E", "S", 0));
	toBeRetrieved.add(new Customer("F", "P", 0));
	toBeRetrieved.add(new Customer("F", "Q", 0));
	toBeRetrieved.add(new Customer("F", "R", 0));
	repository.saveAll(toBeRetrieved);
	final Example<Customer> example = Example.of(new Customer("", "", 0),
		ExampleMatcher.matchingAny().withIgnoreNullValues().withIgnorePaths(new String[] { "location", "alive" }));
	final long size = repository.count(example);
	assertTrue(size == toBeRetrieved.size());
}
 
Example #14
Source File: ProjectDTOService.java    From celerio-angular-quickstart with Apache License 2.0 6 votes vote down vote up
@Transactional(readOnly = true)
public PageResponse<ProjectDTO> findAll(PageRequestByExample<ProjectDTO> req) {
    Example<Project> example = null;
    Project project = toEntity(req.example);

    if (project != null) {
        ExampleMatcher matcher = ExampleMatcher.matching() //
                .withMatcher(Project_.name.getName(), match -> match.ignoreCase().startsWith())
                .withMatcher(Project_.url.getName(), match -> match.ignoreCase().startsWith());

        example = Example.of(project, matcher);
    }

    Page<Project> page;
    if (example != null) {
        page = projectRepository.findAll(example, req.toPageable());
    } else {
        page = projectRepository.findAll(req.toPageable());
    }

    List<ProjectDTO> content = page.getContent().stream().map(this::toDTO).collect(Collectors.toList());
    return new PageResponse<>(page.getTotalPages(), page.getTotalElements(), content);
}
 
Example #15
Source File: AuthorDTOService.java    From celerio-angular-quickstart with Apache License 2.0 6 votes vote down vote up
@Transactional(readOnly = true)
public PageResponse<AuthorDTO> findAll(PageRequestByExample<AuthorDTO> req) {
    Example<Author> example = null;
    Author author = toEntity(req.example);

    if (author != null) {
        ExampleMatcher matcher = ExampleMatcher.matching() //
                .withMatcher(Author_.lastName.getName(), match -> match.ignoreCase().startsWith())
                .withMatcher(Author_.firstName.getName(), match -> match.ignoreCase().startsWith())
                .withMatcher(Author_.email.getName(), match -> match.ignoreCase().startsWith());

        example = Example.of(author, matcher);
    }

    Page<Author> page;
    if (example != null) {
        page = authorRepository.findAll(example, req.toPageable());
    } else {
        page = authorRepository.findAll(req.toPageable());
    }

    List<AuthorDTO> content = page.getContent().stream().map(this::toDTO).collect(Collectors.toList());
    return new PageResponse<>(page.getTotalPages(), page.getTotalElements(), content);
}
 
Example #16
Source File: UserDTOService.java    From celerio-angular-quickstart with Apache License 2.0 6 votes vote down vote up
@Transactional(readOnly = true)
public PageResponse<UserDTO> findAll(PageRequestByExample<UserDTO> req) {
    Example<User> example = null;
    User user = toEntity(req.example);

    if (user != null) {
        ExampleMatcher matcher = ExampleMatcher.matching() //
                .withMatcher(User_.login.getName(), match -> match.ignoreCase().startsWith())
                .withMatcher(User_.email.getName(), match -> match.ignoreCase().startsWith())
                .withMatcher(User_.firstName.getName(), match -> match.ignoreCase().startsWith())
                .withMatcher(User_.lastName.getName(), match -> match.ignoreCase().startsWith());

        example = Example.of(user, matcher);
    }

    Page<User> page;
    if (example != null) {
        page = userRepository.findAll(example, req.toPageable());
    } else {
        page = userRepository.findAll(req.toPageable());
    }

    List<UserDTO> content = page.getContent().stream().map(this::toDTO).collect(Collectors.toList());
    return new PageResponse<>(page.getTotalPages(), page.getTotalElements(), content);
}
 
Example #17
Source File: PassportDTOService.java    From celerio-angular-quickstart with Apache License 2.0 6 votes vote down vote up
@Transactional(readOnly = true)
public PageResponse<PassportDTO> findAll(PageRequestByExample<PassportDTO> req) {
    Example<Passport> example = null;
    Passport passport = toEntity(req.example);

    if (passport != null) {
        ExampleMatcher matcher = ExampleMatcher.matching() //
                .withMatcher(Passport_.passportNumber.getName(), match -> match.ignoreCase().startsWith());

        example = Example.of(passport, matcher);
    }

    Page<Passport> page;
    if (example != null) {
        page = passportRepository.findAll(example, req.toPageable());
    } else {
        page = passportRepository.findAll(req.toPageable());
    }

    List<PassportDTO> content = page.getContent().stream().map(this::toDTO).collect(Collectors.toList());
    return new PageResponse<>(page.getTotalPages(), page.getTotalElements(), content);
}
 
Example #18
Source File: ArangoRepositoryTest.java    From spring-data with Apache License 2.0 5 votes vote down vote up
@Test
public void startingWithByExampleTest() {
	final List<Customer> toBeRetrieved = new LinkedList<>();
	final Customer check = new Customer("Abba", "Bbaaaa", 100);
	toBeRetrieved.add(check);
	toBeRetrieved.add(new Customer("Baabba", "", 67));
	toBeRetrieved.add(new Customer("B", "", 43));
	toBeRetrieved.add(new Customer("C", "", 76));
	repository.saveAll(toBeRetrieved);
	final Example<Customer> example = Example.of(new Customer(null, "bb", 100), ExampleMatcher.matching()
			.withMatcher("surname", match -> match.startsWith()).withIgnoreCase("surname").withIgnoreNullValues());
	final Customer retrieved = repository.findOne(example).get();
	assertEquals(check, retrieved);
}
 
Example #19
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void queryByExampleExactMatchTest() {
	this.expectedEx.expect(DatastoreDataException.class);
	this.expectedEx.expectMessage("Unsupported StringMatcher. Only EXACT and DEFAULT are supported");

	this.datastoreTemplate.queryByExample(
			Example.of(new SimpleTestEntity(),
					ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.REGEX)),
			null);
}
 
Example #20
Source File: ArangoRepositoryTest.java    From spring-data with Apache License 2.0 5 votes vote down vote up
@Test
public void containingExampleTest() {
	final Customer entity = new Customer("name", "surname", 10);
	repository.save(entity);

	final Customer probe = new Customer();
	probe.setName("am");
	final Example<Customer> example = Example.of(probe,
		ExampleMatcher.matching().withStringMatcher(StringMatcher.CONTAINING).withIgnorePaths("arangoId", "id",
			"key", "rev", "surname", "age"));
	final Optional<Customer> retrieved = repository.findOne(example);
	assertThat(retrieved.isPresent(), is(true));
	assertThat(retrieved.get().getName(), is("name"));
}
 
Example #21
Source File: TalkServiceImpl.java    From graphql-java-demo with MIT License 5 votes vote down vote up
public List<Talk> find(final Talk talk) {
    if (talk == null) {
        return talkRepository.findAll();
    }

    ExampleMatcher matcher = ExampleMatcher.matching()
            .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
            .withIgnoreCase();

    return StreamSupport.stream(talkRepository.findAll(Example.of(talk, matcher)).spliterator(), false)
            .collect(Collectors.toList());
}
 
Example #22
Source File: PersonServiceImpl.java    From graphql-java-demo with MIT License 5 votes vote down vote up
public List<Person> find(final Person person) {
    if (person == null) {
        return personRepository.findAll();
    }

    ExampleMatcher matcher = ExampleMatcher.matching()
            .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
            .withIgnoreCase();

    return StreamSupport.stream(personRepository.findAll(Example.of(person, matcher)).spliterator(), false)
            .collect(Collectors.toList());
}
 
Example #23
Source File: AccountMongoRepositoryManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenExample_whenFindAllWithExample_thenFindAllMacthings() {
    repository.save(new Account(null, "john", 12.3)).block();
    ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("owner", startsWith());
    Example<Account> example = Example.of(new Account(null, "jo", null), matcher);
    Flux<Account> accountFlux = repository.findAll(example);

    StepVerifier
            .create(accountFlux)
            .assertNext(account -> assertEquals("john", account.getOwner()))
            .expectComplete()
            .verify();
}
 
Example #24
Source File: DatastoreRepositoryExample.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private void retrieveAndPrintSingers() {
	System.out.println("The kind for singers has been cleared and "
			+ this.singerRepository.count() + " new singers have been inserted:");

	Iterable<Singer> allSingers = this.singerRepository.findAll();
	allSingers.forEach(System.out::println);

	System.out.println("You can also retrieve by keys for strong consistency: ");

	// Retrieving by keys or querying with a restriction to a single entity group
	// / family is strongly consistent.
	this.singerRepository
			.findAllById(Arrays.asList("singer1", "singer2", "singer3"))
			.forEach((x) -> System.out.println("retrieved singer: " + x));

	//Query by example: find all singers with the last name "Doe"
	Iterable<Singer> singers = this.singerRepository.findAll(
			Example.of(new Singer(null, null, "Doe", null), ExampleMatcher.matching().withIgnorePaths("singerId")));
	System.out.println("Query by example");
	singers.forEach(System.out::println);

	//Pageable parameter
	System.out.println("Using Pageable parameter");
	Pageable pageable = PageRequest.of(0, 1);
	Slice<Singer> slice;
	boolean hasNext = true;
	while (hasNext) {
		slice = this.singerRepository.findSingersByLastName("Doe", pageable);
		System.out.println(slice.getContent().get(0));
		hasNext = slice.hasNext();
		pageable = slice.getPageable().next();
	}
}
 
Example #25
Source File: UserRepository.java    From celerio-angular-quickstart with Apache License 2.0 5 votes vote down vote up
default List<User> complete(String query, int maxResults) {
    User probe = new User();
    probe.setLogin(query);

    ExampleMatcher matcher = ExampleMatcher.matching() //
            .withMatcher(User_.login.getName(), match -> match.ignoreCase().startsWith());

    Page<User> page = findAll(Example.of(probe, matcher), new PageRequest(0, maxResults));
    return page.getContent();
}
 
Example #26
Source File: ArangoRepositoryTest.java    From spring-data with Apache License 2.0 5 votes vote down vote up
@Test
public void findAllPageableByExampleTest() {
	final List<Customer> toBeRetrieved = new LinkedList<>();
	toBeRetrieved.add(new Customer("A", "Z", 0));
	toBeRetrieved.add(new Customer("B", "X", 0));
	toBeRetrieved.add(new Customer("B", "Y", 0));
	toBeRetrieved.add(new Customer("C", "V", 0));
	toBeRetrieved.add(new Customer("D", "T", 0));
	toBeRetrieved.add(new Customer("D", "U", 0));
	toBeRetrieved.add(new Customer("E", "S", 0));
	toBeRetrieved.add(new Customer("F", "P", 0));
	toBeRetrieved.add(new Customer("F", "Q", 0));
	toBeRetrieved.add(new Customer("F", "R", 0));
	repository.saveAll(toBeRetrieved);
	repository.save(new Customer("A", "A", 1));
	final Example<Customer> example = Example.of(new Customer("", "", 0),
		ExampleMatcher.matchingAny().withIgnoreNullValues().withIgnorePaths(new String[] { "location", "alive" }));
	final List<Sort.Order> orders = new LinkedList<>();
	orders.add(new Sort.Order(Sort.Direction.ASC, "name"));
	orders.add(new Sort.Order(Sort.Direction.ASC, "surname"));
	final Sort sort = Sort.by(orders);
	final int pageNumber = 1;
	final int pageSize = 3;
	final Page<Customer> retrievedPage = repository.findAll(example, PageRequest.of(pageNumber, pageSize, sort));
	assertEquals(toBeRetrieved.size(), retrievedPage.getTotalElements());
	assertEquals(pageNumber, retrievedPage.getNumber());
	assertEquals(pageSize, retrievedPage.getSize());
	assertEquals((toBeRetrieved.size() + pageSize - 1) / pageSize, retrievedPage.getTotalPages());
	final List<Customer> expected = toBeRetrieved.subList(pageNumber * pageSize, (pageNumber + 1) * pageSize);
	assertTrue(equals(expected, retrievedPage, cmp, eq, true));
}
 
Example #27
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void queryByExampleCaseSensitiveTest() {
	this.expectedEx.expect(DatastoreDataException.class);
	this.expectedEx.expectMessage("Property matchers are not supported");

	this.datastoreTemplate.queryByExample(
			Example.of(new SimpleTestEntity(), ExampleMatcher.matching().withMatcher("id",
					ExampleMatcher.GenericPropertyMatcher::caseSensitive)),
			null);
}
 
Example #28
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void queryByExampleOptions() {
	EntityQuery.Builder builder = Query.newEntityQueryBuilder().setKind("test_kind");
	this.datastoreTemplate.queryByExample(
			Example.of(this.simpleTestEntity, ExampleMatcher.matching().withIgnorePaths("id")),
			new DatastoreQueryOptions.Builder().setLimit(10).setOffset(1).setSort(Sort.by("intField"))
					.build());

	StructuredQuery.CompositeFilter filter = StructuredQuery.CompositeFilter
			.and(PropertyFilter.eq("color", "simple_test_color"),
					PropertyFilter.eq("int_field", 1));
	verify(this.datastore, times(1)).run(builder.setFilter(filter)
			.addOrderBy(StructuredQuery.OrderBy.asc("int_field")).setLimit(10).setOffset(1).build());
}
 
Example #29
Source File: PassengerRepositoryIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenPassengers_whenFindByExampleCaseInsensitiveMatcher_thenExpectedReturned() {
    ExampleMatcher caseInsensitiveExampleMatcher = ExampleMatcher.matchingAll().withIgnoreCase();
    Example<Passenger> example = Example.of(Passenger.from("fred", "bloggs", null),
      caseInsensitiveExampleMatcher);

    Optional<Passenger> actual = repository.findOne(example);

    assertTrue(actual.isPresent());
    assertEquals(Passenger.from("Fred", "Bloggs", 22), actual.get());
}
 
Example #30
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void queryByExamplePropertyMatchersTest() {
	this.expectedEx.expect(DatastoreDataException.class);
	this.expectedEx.expectMessage("Property matchers are not supported");

	this.datastoreTemplate.queryByExample(
			Example.of(new SimpleTestEntity(), ExampleMatcher.matching().withMatcher("id",
					ExampleMatcher.GenericPropertyMatcher.of(ExampleMatcher.StringMatcher.REGEX))),
			null);
}