* added sample code for eval article

* BAEL-2803 Sample Code

* updated tests, repo method names

* moved sources for BAEL-2803 here

* removed dependencies introduced by BAEL-2803

* re-added sources

* moved code to new module

* removed eval article code
This commit is contained in:
Paul Latzelsperger
2019-04-02 21:13:24 +02:00
committed by maibin
parent 8a9cfe80e9
commit c9211557bd
3 changed files with 76 additions and 7 deletions
@@ -1,90 +0,0 @@
package com.baeldung.exists;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.ignoreCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.boot.Application;
import com.baeldung.boot.domain.Car;
import com.baeldung.boot.repository.CarRepository;
import java.util.Arrays;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
public class CarRepositoryIntegrationTest {
@Autowired
private CarRepository repository;
private int searchId;
@Before
public void setup() {
List<Car> cars = repository.saveAll(Arrays.asList(new Car(200, "BMW"), new Car(300, "Audi")));
searchId = cars.get(0).getId();
}
@After
public void teardown() {
repository.deleteAll();
}
@Test
public void whenIdIsCorrect_thenExistsShouldReturnTrue() {
assertThat(repository.existsById(searchId)).isTrue();
}
@Test
public void givenExample_whenExists_thenIsTrue() {
ExampleMatcher modelMatcher = ExampleMatcher.matching()
.withIgnorePaths("id") // must explicitly ignore -> PK
.withMatcher("model", ignoreCase());
Car probe = new Car();
probe.setModel("bmw");
Example<Car> example = Example.of(probe, modelMatcher);
assertThat(repository.exists(example)).isTrue();
}
@Test
public void givenPower_whenExists_thenIsFalse() {
assertThat(repository.existsCarByPower(200)).isTrue();
assertThat(repository.existsCarByPower(800)).isFalse();
}
@Test
public void existsByDerivedQuery_byModel() {
assertThat(repository.existsCarByModel("Audi")).isTrue();
assertThat(repository.existsCarByModel("audi")).isFalse();
assertThat(repository.existsCarByModel("AUDI")).isFalse();
assertThat(repository.existsCarByModel("")).isFalse();
}
@Test
public void givenModelName_whenExistsExact_thenIsTrue() {
assertThat(repository.existsCarExactCustomQuery("BMW")).isTrue();
assertThat(repository.existsCarExactCustomQuery("Bmw")).isFalse();
assertThat(repository.existsCarExactCustomQuery("bmw")).isFalse();
assertThat(repository.existsCarExactCustomQuery("")).isFalse();
}
@Test
public void givenModelName_whenExistsLike_thenIsTrue() {
assertThat(repository.existsCarLikeCustomQuery("BMW")).isTrue();
assertThat(repository.existsCarLikeCustomQuery("Bmw")).isTrue();
assertThat(repository.existsCarLikeCustomQuery("bmw")).isTrue();
assertThat(repository.existsCarLikeCustomQuery("")).isFalse();
}
}