This commit is contained in:
Татьяна Артамонова 2023-05-28 21:58:56 +04:00
parent c81df378fe
commit f40170e244
6 changed files with 231 additions and 163 deletions

View File

@ -28,16 +28,16 @@ public class Controller {
this.songService = songService; this.songService = songService;
} }
@GetMapping("/find") // @GetMapping("/find")
public Map<String, List<Object>> GetResult (@RequestParam(value = "name") String value) { // public Map<String, List<Object>> GetResult (@RequestParam(value = "name") String value) {
Map<String, List<Object>> resultMap = new HashMap<>(); // Map<String, List<Object>> resultMap = new HashMap<>();
//
List<Object> resultList = new ArrayList<>(); // List<Object> resultList = new ArrayList<>();
resultList.add(songService.findAllSongsByName(value)); // resultList.add(songService.findAllSongsByName(value));
resultList.add(albumService.findAllAlbumsByName(value)); // resultList.add(albumService.findAllAlbumsByName(value));
resultList.add(artistService.findAllArtistsByName(value)); // resultList.add(artistService.findAllArtistsByName(value));
//
resultMap.put(value, resultList); // resultMap.put(value, resultList);
return resultMap; // return resultMap;
} // }
} }

View File

@ -41,12 +41,18 @@ public class AlbumService {
return currentAlbum; return currentAlbum;
} }
@Transactional @Transactional
public Album addArtist(Album currentAlbum, List<Artist> artists){ public Album addArtist(Long AlbumId, Long ArtistId){
for (Artist artist : artists) { final Album album = em.find(Album.class, AlbumId);
currentAlbum.addArtist(artist); if (album == null) {
throw new EntityNotFoundException(String.format("Album with id [%s] is not found", AlbumId));
} }
em.merge(currentAlbum);
return currentAlbum; final Artist artist = em.find(Artist.class, ArtistId);
if (artist == null) {
throw new EntityNotFoundException(String.format("Artist with id [%s] is not found", ArtistId));
}
album.addArtist(artist);
return em.merge(album);
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Album findAlbum(Long id) { public Album findAlbum(Long id) {
@ -122,6 +128,7 @@ public class AlbumService {
em.createQuery("update Song s SET s.album = null ").executeUpdate(); em.createQuery("update Song s SET s.album = null ").executeUpdate();
em.createQuery("delete from Album").executeUpdate(); em.createQuery("delete from Album").executeUpdate();
} }
@Transactional @Transactional
public List<Album> findFilteredAlbum(Long[] arr) { public List<Album> findFilteredAlbum(Long[] arr) {
if (arr.length == 0) { if (arr.length == 0) {
@ -133,12 +140,10 @@ public class AlbumService {
} }
return albumList; return albumList;
} }
@Transactional // @Transactional
public List<Album> findAllAlbumsByName(String name){ // public List<Album> findAllAlbumsByName(String name){
List<Album> resultList; //
resultList = em.createQuery("select A from Album A where A.albumName = :name", Album.class) // return em.createQuery("select A from Album A where A.albumName = :name", Album.class)
.setParameter("name", name).getResultList(); // .setParameter("name", name).getResultList();
// }
return resultList;
}
} }

View File

@ -90,13 +90,11 @@ public class ArtistService {
.setParameter("artistAlbum", currentArtist).getResultList(); .setParameter("artistAlbum", currentArtist).getResultList();
return songList; return songList;
} }
@Transactional // @Transactional
public List<Artist> findAllArtistsByName(String name){ // public List<Artist> findAllArtistsByName(String name){
List<Artist> resultList; // return em.createQuery("select A from Artist A where A.artistName = :name", Artist.class)
resultList = em.createQuery("select A from Artist A where A.artistName = :name", Artist.class) // .setParameter("name", name).getResultList();
.setParameter("name", name).getResultList(); // }
return resultList;
}
@Transactional @Transactional
public void deleteAllArtists() { public void deleteAllArtists() {
em.createQuery("delete from Artist").executeUpdate(); em.createQuery("delete from Artist").executeUpdate();

View File

@ -3,35 +3,36 @@ package ru.ulstu.is.sbapp.database.service;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.is.sbapp.database.model.Album;
import ru.ulstu.is.sbapp.database.model.Artist;
import ru.ulstu.is.sbapp.database.model.Song;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service @Service
public class FindByNameService { public class FindByNameService {
@PersistenceContext @PersistenceContext
private EntityManager em; private EntityManager em;
@Autowired
private AlbumService albumService;
@Autowired
private SongService songService;
@Autowired
private ArtistService artistService;
@Transactional @Transactional
public Map<String, List<Object>> GetResult(String name) { public Map<String, List<Object>> GetResult(String name) {
Map<String, List<Object>> resultMap = new HashMap<>(); Map<String, List<Object>> resultMap = new HashMap<>();
Album album = new Album();
Artist artist = new Artist();
Song song = new Song();
List<Object> resultList = new ArrayList<>(); List<Album> albumList = em.createQuery("select A from Album A where A.albumName = :name", Album.class)
resultList.add(songService.findAllSongsByName(name)); .setParameter("name", name).getResultList();
resultList.add(albumService.findAllAlbumsByName(name)); List<Artist> artistList = em.createQuery("select A from Artist A where A.artistName = :name", Artist.class)
resultList.add(artistService.findAllArtistsByName(name)); .setParameter("name", name).getResultList();
List<Song> songList = em.createQuery("select S from Song S where S.songName = :name", Song.class)
.setParameter("name", name).getResultList();
resultMap.put(name, resultList); resultMap.put(album.getAlbumName(), Collections.singletonList(albumList));
resultMap.put(artist.getArtistName(), Collections.singletonList(artistList));
resultMap.put(song.getSongName(), Collections.singletonList(songList));
return resultMap; return resultMap;
} }
} }

View File

@ -89,12 +89,10 @@ public class SongService {
em.createQuery("delete from Song").executeUpdate(); em.createQuery("delete from Song").executeUpdate();
} }
@Transactional // @Transactional
public List<Song> findAllSongsByName(String name){ // public List<Song> findAllSongsByName(String name){
List<Song> resultList; // return em.createQuery("select S from Song S where S.songName = :name", Song.class)
resultList = em.createQuery("select S from Song S where S.songName = :name", Song.class) // .setParameter("name", name).getResultList();
.setParameter("name", name).getResultList(); // }
return resultList;
}
} }

View File

@ -1,7 +1,9 @@
package ru.ulstu.is.sbapp; package ru.ulstu.is.sbapp;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.platform.commons.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import ru.ulstu.is.sbapp.database.model.Artist; import ru.ulstu.is.sbapp.database.model.Artist;
@ -13,153 +15,217 @@ import ru.ulstu.is.sbapp.database.service.SongService;
import ru.ulstu.is.sbapp.database.service.AlbumService; import ru.ulstu.is.sbapp.database.service.AlbumService;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest @SpringBootTest
public class SbappApplicationTests { public class SbappApplicationTests {
@Autowired @Autowired
private AlbumService albumService; private AlbumService albumService;
@Autowired @Autowired
private SongService songService;
@Autowired
private ArtistService artistService; private ArtistService artistService;
@Autowired @Autowired
private SongService songService;
@Autowired
private FindByNameService findService; private FindByNameService findService;
@Test @Test
void test1() { void testSongCreate(){
albumService.deleteAllAlbums();
artistService.deleteAllArtists();
songService.deleteAllSongs(); songService.deleteAllSongs();
final Song song = songService.addSong("Song",2.36);
final Song song = songService.addSong("song", 2.50);
Assertions.assertNotNull(song.getId()); Assertions.assertNotNull(song.getId());
} }
@Test @Test
void test2() { void testArtistCreate(){
final Song song = songService.addSong("song", 2.50); artistService.deleteAllArtists();
final Artist artist = artistService.addArtist("Artist", "genre");
Song song2 = songService.addSong("song2", 3.10);
List<Song> songs = new ArrayList<>();
songs.add(song);
songs.add(song2);
Artist artist = artistService.addArtist("artist", "genre");
Assertions.assertNotNull(artist.getId()); Assertions.assertNotNull(artist.getId());
} }
@Test @Test
void test3() { void testAlbumCreate() throws IOException {
Artist artist = artistService.addArtist("artist", "genre"); albumService.deleteAllAlbums();
artistService.deleteAllArtists();
songService.deleteAllSongs();
final Artist artist1 = artistService.addArtist("Artist", "genre");
final Artist artist2 = artistService.addArtist("Artist", "genre");
final Song song = songService.addSong("Song",3.27);
final Album album1= albumService.addAlbum("Album");
final Album album2= albumService.addAlbum("Album");
album1.addArtist(artist1);
album2.addArtist(artist1);
album2.addArtist(artist2);
Assertions.assertNotNull(album1.getId());
Assertions.assertNotNull(album2.getId());
}
@Test
Artist artist2 = artistService.addArtist("artist2", "genre"); void ReadAlbum() throws IOException {
albumService.deleteAllAlbums();
List<Artist> artists = new ArrayList<>(); artistService.deleteAllArtists();
artists.add(artist); songService.deleteAllSongs();
artists.add(artist2); final Artist artist1 = artistService.addArtist("Artist", "genre");
final Song song = songService.addSong("Song",3.28);
Album album = albumService.addAlbum("album"); final Album album2= albumService.addAlbum("Album");
Album album2 = albumService.addAlbum("album2"); album2.addArtist(artist1);
Assertions.assertNotNull(album.getId()); final Album findAlbum = albumService.findAlbum(album2.getId());
Assertions.assertEquals(album2, findAlbum);
} }
@Test @Test
void test4() { void ReadAlbumTrue() throws IOException {
Album album = albumService.addAlbum("album"); albumService.deleteAllAlbums();
List<Artist> artists = new ArrayList<>(); artistService.deleteAllArtists();
List<Song> songs = new ArrayList<>(); songService.deleteAllSongs();
Artist artist2 = artistService.addArtist("artist2", "genre"); final Artist artist1 = artistService.addArtist("Artist", "genre");
Song song2 = songService.addSong("song2", 3.10); final Artist artist2 = artistService.addArtist("Artist2", "genre");
final Song song = songService.addSong("Song",3.19);
final Song song = songService.addSong("song", 2.50); Album album2= albumService.addAlbum("Album");
Artist artist = artistService.addArtist("artist", "genre"); album2 =albumService.addArtist(album2.getId(),artist1.getId());
album2 =albumService.addArtist(album2.getId(),artist2.getId());
final Album findAlbum = albumService.findAlbum(album2.getId());
albumService.addArtist(album, artists); Assertions.assertEquals(album2, findAlbum);
albumService.addSong(album, songs);
artist2 = artistService.updateArtist(artist2.getId(), "artist3", "genre2");
Assertions.assertEquals("artist3", artistService.findArtist(artist2.getId()).getArtistName());
song2 = songService.updateSong(song2.getId(), "song3", 3.40, null);
Assertions.assertEquals("song3", songService.findSong(song2.getId()).getSongName());
album = albumService.updateAlbum(album.getId(), "album3", songs, artists);
Assertions.assertEquals("album3", albumService.findAlbum(album.getId()).getAlbumName());
} }
@Test @Test
void test5(){ void ReadSong(){
Album album = albumService.addAlbum("album"); songService.deleteAllSongs();
List<Artist> artists = new ArrayList<>(); final Song song = songService.addSong("Song",3.29);
List<Song> songs = new ArrayList<>(); final Album album = albumService.addAlbum("Album");
Artist artist2 = artistService.addArtist("artist2", "genre"); song.setAlbum(album);
Song song2 = songService.addSong("song2", 3.10);
final Song song = songService.addSong("song", 2.50);
Artist artist = artistService.addArtist("artist", "genre");
final Song findSong = songService.findSong(song.getId()); final Song findSong = songService.findSong(song.getId());
Assertions.assertEquals(song, findSong); Assertions.assertEquals(song, findSong);
}
@Test
void ReadArtist(){
artistService.deleteAllArtists();
final Artist artist = artistService.addArtist("Artist", "genre");
final Artist findArtist = artistService.findArtist(artist.getId()); final Artist findArtist = artistService.findArtist(artist.getId());
Assertions.assertEquals(artist, findArtist); Assertions.assertEquals(artist, findArtist);
final Album findAlbum = albumService.findAlbum(album.getId());
Assertions.assertEquals(album, findAlbum);
} }
@Test
// void testAlbumCheck() throws IOException {
// //TestReadAll albumService.deleteAllAlbums();
// final List<Song> songss = songService.findAllSongs(); artistService.deleteAllArtists();
// Assertions.assertEquals(songss.size(), 2); songService.deleteAllSongs();
// final Artist artist1 = artistService.addArtist("Artist", "genre");
// final List<Artist> artistss = artistService.findAllArtists(); final Artist artist2 = artistService.addArtist("Artist1", "genre1");
// Assertions.assertEquals(artistss.size(), 2); final Song song = songService.addSong("Song",3.28);
// final Album album2= albumService.addAlbum("Album");
// final List<Album> albumss = albumService.findAllAlbums(); album2.addArtist(artist1);
// Assertions.assertEquals(albumss.size(), 2); album2.addArtist(artist2);
// Assertions.assertEquals(album2.getArtists().size(),2);
// //TestReadNotFound }
//
// //album2 = albumService.updateAlbum(album2.getId(), "стул",songs, artistService.findAllArtists());
// //albumService.deleteAlbum(album.getId());
// //albumService.deleteAlbum(album2.getId());
// List<Song> list = songService.findAllSongs();
// List<Artist> list2 = artistService.findAllArtists();
@Test @Test
void test6() { void testAlbumPreviewCheck() throws IOException {
albumService.deleteAllAlbums(); albumService.deleteAllAlbums();
songService.deleteAllSongs();
artistService.deleteAllArtists(); artistService.deleteAllArtists();
songService.deleteAllSongs();
final Artist artist1 = artistService.addArtist("Artist", "genre");
Assertions.assertThrows(EntityNotFoundException.class, () -> songService.findSong(-1L)); final Artist artist2 = artistService.addArtist("Artist1", "genre1");
Assertions.assertThrows(EntityNotFoundException.class, () -> artistService.findArtist(-1L)); final Song song = songService.addSong("Song",2.10);
Assertions.assertThrows(EntityNotFoundException.class, () -> albumService.findAlbum(-1L)); final Album album2= albumService.addAlbum("Album");
album2.addArtist(artist1);
//TestReadAllEmpty album2.addArtist(artist2);
final List<Song> newComponents = songService.findAllSongs(); Album v = albumService.findAlbum(album2.getId());
Assertions.assertEquals(newComponents.size(), 0); }
@Test
final List<Album> newOrders = albumService.findAllAlbums(); void testSongReadNotFound() {
Assertions.assertEquals(newOrders.size(), 0); songService.deleteAllSongs();
Assertions.assertThrows(EntityNotFoundException.class, () -> songService.findSong(-1L));
final List<Artist> newArtists = artistService.findAllArtists(); }
Assertions.assertEquals(newArtists.size(), 0); @Test
void testArtistReadNotFound() {
artistService.deleteAllArtists();
Assertions.assertThrows(EntityNotFoundException.class, () -> artistService.findArtist(-1L));
}
@Test
void testAlbumReadNotFound() {
albumService.deleteAllAlbums();
Assertions.assertThrows(EntityNotFoundException.class, () -> albumService.findAlbum(-1L));
}
@Test
void testSongCount(){
songService.deleteAllSongs();
final Song song1 = songService.addSong("Song",3.15);
final Song song2 = songService.addSong("Song1",2.43);
final List<Song> songs = songService.findAllSongs();
Assertions.assertEquals(songs.size(),2);
}
@Test
void testArtistCount(){
artistService.deleteAllArtists();
final Artist cat1 = artistService.addArtist("Shorts", "genre");
final Artist ca2 = artistService.addArtist("Comedy", "genre");
final List<Artist> categories = artistService.findAllArtists();
assertEquals(categories.size(),2);
}
@Test
void testAlbumCount() throws IOException {
albumService.deleteAllAlbums();
artistService.deleteAllArtists();
songService.deleteAllSongs();
final Artist cat1 = artistService.addArtist("Artist", "genre");
final Song song2 = songService.addSong("Song",3.17);
final Album album1 = albumService.addAlbum("Album");
final Album album2 = albumService.addAlbum("Album1");
album1.addArtist(cat1);
album2.addArtist(cat1);
final List<Album> albums = albumService.findAllAlbums();
Assertions.assertEquals(albums.size(),2);
} }
@Test @Test
void testDop() { void testDop() {
var temp = findService.GetResult("a"); // var temp = findService.GetResult("a");
Assertions.assertEquals(findService.GetResult("a"), temp); // Assertions.assertEquals(findService.GetResult("a"), temp);
String name = "Test";
// создаем тестовые данные
final Artist artist = artistService.addArtist("Test Artist", "genre");
Assertions.assertNotNull(artist.getId());
final Song song = songService.addSong("Test Song", 2.50);
Assertions.assertNotNull(song.getId());
final Album album = albumService.addAlbum("Test Album");
//album = albumService.addArtist(album.getId(), artist.getId());
Assertions.assertNotNull(album.getId());
// вызываем тестируемый метод
Map<String, List<Object>> result = findService.GetResult(name);
// проверяем результаты
assertEquals(result.size(), 3);
assertTrue(result.containsKey(album.getAlbumName()));
assertTrue(result.containsKey(artist.getArtistName()));
assertTrue(result.containsKey(song.getSongName()));
List<Object> albums = result.get("Test Album");
List<Object> artists = result.get("Test Artist");
List<Object> songs = result.get("Test Song");
assertTrue(albums.size() == 1 && albums.get(0) instanceof Album);
assertTrue(artists.size() == 1 && artists.get(0) instanceof Artist);
assertTrue(songs.size() == 1 && songs.get(0) instanceof Song);
Album albumResult = (Album) albums.get(0);
Artist artistResult = (Artist) artists.get(0);
Song songResult = (Song) songs.get(0);
assertEquals(albumResult.getAlbumName(), "Test Album");
assertEquals(artistResult.getArtistName(), "Test Artist");
assertEquals(songResult.getSongName(), "Test Song");
} }