BAEl 2803 (#6574)
* BAEL-2803 Sample Code * updated tests, repo method names * moved sources for BAEL-2803 here * removed dependencies introduced by BAEL-2803 * re-added sources * renamed test methods according to BDD
This commit is contained in:
		
							parent
							
								
									ccc39861fe
								
							
						
					
					
						commit
						07b62fd912
					
				| @ -0,0 +1,48 @@ | |||||||
|  | package com.baeldung.exists; | ||||||
|  | 
 | ||||||
|  | import javax.persistence.Entity; | ||||||
|  | import javax.persistence.GeneratedValue; | ||||||
|  | import javax.persistence.Id; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * @author paullatzelsperger | ||||||
|  |  * @since 2019-03-20 | ||||||
|  |  */ | ||||||
|  | @Entity | ||||||
|  | public class Car { | ||||||
|  | 
 | ||||||
|  |     @Id | ||||||
|  |     @GeneratedValue | ||||||
|  |     private int id; | ||||||
|  |     private Integer power; | ||||||
|  |     private String model; | ||||||
|  | 
 | ||||||
|  |     Car() { | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public Car(int power, String model) { | ||||||
|  |         this.power = power; | ||||||
|  |         this.model = model; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public Integer getPower() { | ||||||
|  |         return power; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setPower(Integer power) { | ||||||
|  |         this.power = power; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public String getModel() { | ||||||
|  |         return model; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setModel(String model) { | ||||||
|  |         this.model = model; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public int getId() { | ||||||
|  |         return id; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,24 @@ | |||||||
|  | package com.baeldung.exists; | ||||||
|  | 
 | ||||||
|  | import org.springframework.data.jpa.repository.JpaRepository; | ||||||
|  | import org.springframework.data.jpa.repository.Query; | ||||||
|  | import org.springframework.data.repository.query.Param; | ||||||
|  | import org.springframework.stereotype.Repository; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * @author paullatzelsperger | ||||||
|  |  * @since 2019-03-20 | ||||||
|  |  */ | ||||||
|  | @Repository | ||||||
|  | public interface CarRepository extends JpaRepository<Car, Integer> { | ||||||
|  | 
 | ||||||
|  |     boolean existsCarByPower(int power); | ||||||
|  | 
 | ||||||
|  |     boolean existsCarByModel(String model); | ||||||
|  | 
 | ||||||
|  |     @Query("select case when count(c)> 0 then true else false end from Car c where c.model = :model") | ||||||
|  |     boolean existsCarExactCustomQuery(@Param("model") String model); | ||||||
|  | 
 | ||||||
|  |     @Query("select case when count(c)> 0 then true else false end from Car c where lower(c.model) like lower(:model)") | ||||||
|  |     boolean existsCarLikeCustomQuery(@Param("model") String model); | ||||||
|  | } | ||||||
| @ -0,0 +1,86 @@ | |||||||
|  | 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 java.util.Arrays; | ||||||
|  | import java.util.List; | ||||||
|  | 
 | ||||||
|  | @RunWith(SpringRunner.class) | ||||||
|  | @SpringBootTest | ||||||
|  | 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(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user