feat(like-expressions): add Song + SongRepository with integration tests (#6633)
This commit is contained in:
parent
3465c347d5
commit
7b51ca1dce
|
@ -0,0 +1,75 @@
|
|||
package com.baeldung.entity;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
public class Song {
|
||||
|
||||
@Id private long id;
|
||||
private String name;
|
||||
@Column(name = "length_in_seconds")
|
||||
private int lengthInSeconds;
|
||||
private String compositor;
|
||||
private String singer;
|
||||
private LocalDateTime released;
|
||||
private String genre;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getLengthInSeconds() {
|
||||
return lengthInSeconds;
|
||||
}
|
||||
|
||||
public void setLengthInSeconds(int lengthInSeconds) {
|
||||
this.lengthInSeconds = lengthInSeconds;
|
||||
}
|
||||
|
||||
public String getCompositor() {
|
||||
return compositor;
|
||||
}
|
||||
|
||||
public void setCompositor(String compositor) {
|
||||
this.compositor = compositor;
|
||||
}
|
||||
|
||||
public String getSinger() {
|
||||
return singer;
|
||||
}
|
||||
|
||||
public void setSinger(String singer) {
|
||||
this.singer = singer;
|
||||
}
|
||||
|
||||
public LocalDateTime getReleased() {
|
||||
return released;
|
||||
}
|
||||
|
||||
public void setReleased(LocalDateTime released) {
|
||||
this.released = released;
|
||||
}
|
||||
|
||||
public String getGenre() {
|
||||
return genre;
|
||||
}
|
||||
|
||||
public void setGenre(String genre) {
|
||||
this.genre = genre;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.repository;
|
||||
|
||||
import com.baeldung.entity.Song;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface SongRepository extends JpaRepository<Song, Long> {
|
||||
|
||||
List<Song> findByNameLike(String name);
|
||||
|
||||
List<Song> findByNameNotLike(String name);
|
||||
|
||||
List<Song> findByNameStartingWith(String startingWith);
|
||||
|
||||
List<Song> findByNameEndingWith(String endingWith);
|
||||
|
||||
List<Song> findBySingerContaining(String singer);
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.repository;
|
||||
|
||||
import com.baeldung.entity.Song;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@Sql(scripts = { "/test-song-data.sql" })
|
||||
public class SongRepositoryIntegrationTest {
|
||||
|
||||
@Autowired private SongRepository songRepository;
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
public void givenSong_WhenFindLikeByName_ThenShouldReturnOne() {
|
||||
List<Song> songs = songRepository.findByNameLike("Despacito");
|
||||
assertEquals(1, songs.size());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
public void givenSong_WhenFindByNameNotLike_thenShouldReturn3Songs() {
|
||||
List<Song> songs = songRepository.findByNameNotLike("Despacito");
|
||||
assertEquals(5, songs.size());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
public void givenSong_WhenFindByNameStartingWith_thenShouldReturn2Songs() {
|
||||
List<Song> songs = songRepository.findByNameStartingWith("Co");
|
||||
assertEquals(2, songs.size());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
public void givenSong_WhenFindByNameEndingWith_thenShouldReturn2Songs() {
|
||||
List<Song> songs = songRepository.findByNameEndingWith("Life");
|
||||
assertEquals(2, songs.size());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
public void givenSong_WhenFindBySingerContaining_thenShouldReturn2Songs() {
|
||||
List<Song> songs = songRepository.findBySingerContaining("Luis");
|
||||
assertEquals(2, songs.size());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
INSERT INTO song(id,name,length_in_seconds,compositor,singer,released,genre)
|
||||
VALUES
|
||||
(1,'Despacito',209,'Luis Fonsi','Luis Fonsi, Daddy Yankee','2017-01-12','Reggaeton'),
|
||||
(2,'Con calma',188,'Daddy Yankee','Daddy Yankee','2019-01-24','Reggaeton'),
|
||||
(3,'It''s My Life',205,'Bon Jovi','Jon Bon Jovi','2000-05-23','Pop'),
|
||||
(4,'Live is Life',242,'Opus','Opus','1985-01-01','Reggae'),
|
||||
(5,'Countdown to Extinction',249,'Megadeth','Megadeth','1992-07-14','Heavy Metal'),
|
||||
(6, 'Si nos dejan',139,'Luis Miguel','Luis Miguel','1995-10-17','Bolero');
|
Loading…
Reference in New Issue