From f40170e244f377854d198f7c000f945947a05971 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0=20=D0=90=D1=80?= =?UTF-8?q?=D1=82=D0=B0=D0=BC=D0=BE=D0=BD=D0=BE=D0=B2=D0=B0?= Date: Sun, 28 May 2023 21:58:56 +0400 Subject: [PATCH] lab --- .../database/controllers/Controller.java | 24 +- .../sbapp/database/service/AlbumService.java | 31 +- .../sbapp/database/service/ArtistService.java | 12 +- .../database/service/FindByNameService.java | 33 +- .../sbapp/database/service/SongService.java | 12 +- .../ulstu/is/sbapp/SbappApplicationTests.java | 282 +++++++++++------- 6 files changed, 231 insertions(+), 163 deletions(-) diff --git a/src/main/java/ru/ulstu/is/sbapp/database/controllers/Controller.java b/src/main/java/ru/ulstu/is/sbapp/database/controllers/Controller.java index 29fca26..7296a27 100644 --- a/src/main/java/ru/ulstu/is/sbapp/database/controllers/Controller.java +++ b/src/main/java/ru/ulstu/is/sbapp/database/controllers/Controller.java @@ -28,16 +28,16 @@ public class Controller { this.songService = songService; } - @GetMapping("/find") - public Map> GetResult (@RequestParam(value = "name") String value) { - Map> resultMap = new HashMap<>(); - - List resultList = new ArrayList<>(); - resultList.add(songService.findAllSongsByName(value)); - resultList.add(albumService.findAllAlbumsByName(value)); - resultList.add(artistService.findAllArtistsByName(value)); - - resultMap.put(value, resultList); - return resultMap; - } +// @GetMapping("/find") +// public Map> GetResult (@RequestParam(value = "name") String value) { +// Map> resultMap = new HashMap<>(); +// +// List resultList = new ArrayList<>(); +// resultList.add(songService.findAllSongsByName(value)); +// resultList.add(albumService.findAllAlbumsByName(value)); +// resultList.add(artistService.findAllArtistsByName(value)); +// +// resultMap.put(value, resultList); +// return resultMap; +// } } diff --git a/src/main/java/ru/ulstu/is/sbapp/database/service/AlbumService.java b/src/main/java/ru/ulstu/is/sbapp/database/service/AlbumService.java index 6af3bab..cb99890 100644 --- a/src/main/java/ru/ulstu/is/sbapp/database/service/AlbumService.java +++ b/src/main/java/ru/ulstu/is/sbapp/database/service/AlbumService.java @@ -41,12 +41,18 @@ public class AlbumService { return currentAlbum; } @Transactional - public Album addArtist(Album currentAlbum, List artists){ - for (Artist artist : artists) { - currentAlbum.addArtist(artist); + public Album addArtist(Long AlbumId, Long ArtistId){ + final Album album = em.find(Album.class, AlbumId); + 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) public Album findAlbum(Long id) { @@ -122,6 +128,7 @@ public class AlbumService { em.createQuery("update Song s SET s.album = null ").executeUpdate(); em.createQuery("delete from Album").executeUpdate(); } + @Transactional public List findFilteredAlbum(Long[] arr) { if (arr.length == 0) { @@ -133,12 +140,10 @@ public class AlbumService { } return albumList; } - @Transactional - public List findAllAlbumsByName(String name){ - List resultList; - resultList = em.createQuery("select A from Album A where A.albumName = :name", Album.class) - .setParameter("name", name).getResultList(); - - return resultList; - } +// @Transactional +// public List findAllAlbumsByName(String name){ +// +// return em.createQuery("select A from Album A where A.albumName = :name", Album.class) +// .setParameter("name", name).getResultList(); +// } } diff --git a/src/main/java/ru/ulstu/is/sbapp/database/service/ArtistService.java b/src/main/java/ru/ulstu/is/sbapp/database/service/ArtistService.java index 20e828c..dd11634 100644 --- a/src/main/java/ru/ulstu/is/sbapp/database/service/ArtistService.java +++ b/src/main/java/ru/ulstu/is/sbapp/database/service/ArtistService.java @@ -90,13 +90,11 @@ public class ArtistService { .setParameter("artistAlbum", currentArtist).getResultList(); return songList; } - @Transactional - public List findAllArtistsByName(String name){ - List resultList; - resultList = em.createQuery("select A from Artist A where A.artistName = :name", Artist.class) - .setParameter("name", name).getResultList(); - return resultList; - } +// @Transactional +// public List findAllArtistsByName(String name){ +// return em.createQuery("select A from Artist A where A.artistName = :name", Artist.class) +// .setParameter("name", name).getResultList(); +// } @Transactional public void deleteAllArtists() { em.createQuery("delete from Artist").executeUpdate(); diff --git a/src/main/java/ru/ulstu/is/sbapp/database/service/FindByNameService.java b/src/main/java/ru/ulstu/is/sbapp/database/service/FindByNameService.java index c769740..1ebdd4c 100644 --- a/src/main/java/ru/ulstu/is/sbapp/database/service/FindByNameService.java +++ b/src/main/java/ru/ulstu/is/sbapp/database/service/FindByNameService.java @@ -3,35 +3,36 @@ package ru.ulstu.is.sbapp.database.service; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.transaction.Transactional; -import org.springframework.beans.factory.annotation.Autowired; 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.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; @Service public class FindByNameService { @PersistenceContext private EntityManager em; - @Autowired - private AlbumService albumService; - @Autowired - private SongService songService; - @Autowired - private ArtistService artistService; + @Transactional public Map> GetResult(String name) { Map> resultMap = new HashMap<>(); + Album album = new Album(); + Artist artist = new Artist(); + Song song = new Song(); - List resultList = new ArrayList<>(); - resultList.add(songService.findAllSongsByName(name)); - resultList.add(albumService.findAllAlbumsByName(name)); - resultList.add(artistService.findAllArtistsByName(name)); + List albumList = em.createQuery("select A from Album A where A.albumName = :name", Album.class) + .setParameter("name", name).getResultList(); + List artistList = em.createQuery("select A from Artist A where A.artistName = :name", Artist.class) + .setParameter("name", name).getResultList(); + List 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; } } diff --git a/src/main/java/ru/ulstu/is/sbapp/database/service/SongService.java b/src/main/java/ru/ulstu/is/sbapp/database/service/SongService.java index f07c6a9..3f61ceb 100644 --- a/src/main/java/ru/ulstu/is/sbapp/database/service/SongService.java +++ b/src/main/java/ru/ulstu/is/sbapp/database/service/SongService.java @@ -89,12 +89,10 @@ public class SongService { em.createQuery("delete from Song").executeUpdate(); } - @Transactional - public List findAllSongsByName(String name){ - List resultList; - resultList = em.createQuery("select S from Song S where S.songName = :name", Song.class) - .setParameter("name", name).getResultList(); - return resultList; - } +// @Transactional +// public List findAllSongsByName(String name){ +// return em.createQuery("select S from Song S where S.songName = :name", Song.class) +// .setParameter("name", name).getResultList(); +// } } diff --git a/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java b/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java index b819678..f90bef9 100644 --- a/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java +++ b/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java @@ -1,7 +1,9 @@ package ru.ulstu.is.sbapp; +import jakarta.transaction.Transactional; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.platform.commons.logging.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; 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 jakarta.persistence.*; + +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; 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 public class SbappApplicationTests { @Autowired private AlbumService albumService; @Autowired - private SongService songService; - @Autowired private ArtistService artistService; @Autowired + private SongService songService; + @Autowired private FindByNameService findService; - @Test - void test1() { - albumService.deleteAllAlbums(); - artistService.deleteAllArtists(); + void testSongCreate(){ songService.deleteAllSongs(); - - final Song song = songService.addSong("song", 2.50); + final Song song = songService.addSong("Song",2.36); Assertions.assertNotNull(song.getId()); } @Test - void test2() { - final Song song = songService.addSong("song", 2.50); - - Song song2 = songService.addSong("song2", 3.10); - - List songs = new ArrayList<>(); - songs.add(song); - songs.add(song2); - - Artist artist = artistService.addArtist("artist", "genre"); + void testArtistCreate(){ + artistService.deleteAllArtists(); + final Artist artist = artistService.addArtist("Artist", "genre"); Assertions.assertNotNull(artist.getId()); } + @Test - void test3() { - Artist artist = artistService.addArtist("artist", "genre"); + void testAlbumCreate() throws IOException { + 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()); + } - - Artist artist2 = artistService.addArtist("artist2", "genre"); - - List artists = new ArrayList<>(); - artists.add(artist); - artists.add(artist2); - - Album album = albumService.addAlbum("album"); - Album album2 = albumService.addAlbum("album2"); - Assertions.assertNotNull(album.getId()); + @Test + void ReadAlbum() throws IOException { + albumService.deleteAllAlbums(); + artistService.deleteAllArtists(); + songService.deleteAllSongs(); + final Artist artist1 = artistService.addArtist("Artist", "genre"); + final Song song = songService.addSong("Song",3.28); + final Album album2= albumService.addAlbum("Album"); + album2.addArtist(artist1); + final Album findAlbum = albumService.findAlbum(album2.getId()); + Assertions.assertEquals(album2, findAlbum); } @Test - void test4() { - Album album = albumService.addAlbum("album"); - List artists = new ArrayList<>(); - List songs = new ArrayList<>(); - Artist artist2 = artistService.addArtist("artist2", "genre"); - Song song2 = songService.addSong("song2", 3.10); - - final Song song = songService.addSong("song", 2.50); - Artist artist = artistService.addArtist("artist", "genre"); - - - albumService.addArtist(album, artists); - 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()); - + void ReadAlbumTrue() throws IOException { + albumService.deleteAllAlbums(); + artistService.deleteAllArtists(); + songService.deleteAllSongs(); + final Artist artist1 = artistService.addArtist("Artist", "genre"); + final Artist artist2 = artistService.addArtist("Artist2", "genre"); + final Song song = songService.addSong("Song",3.19); + Album album2= albumService.addAlbum("Album"); + album2 =albumService.addArtist(album2.getId(),artist1.getId()); + album2 =albumService.addArtist(album2.getId(),artist2.getId()); + final Album findAlbum = albumService.findAlbum(album2.getId()); + Assertions.assertEquals(album2, findAlbum); } @Test - void test5(){ - Album album = albumService.addAlbum("album"); - List artists = new ArrayList<>(); - List songs = new ArrayList<>(); - Artist artist2 = artistService.addArtist("artist2", "genre"); - Song song2 = songService.addSong("song2", 3.10); - - final Song song = songService.addSong("song", 2.50); - Artist artist = artistService.addArtist("artist", "genre"); - - - + void ReadSong(){ + songService.deleteAllSongs(); + final Song song = songService.addSong("Song",3.29); + final Album album = albumService.addAlbum("Album"); + song.setAlbum(album); final Song findSong = songService.findSong(song.getId()); Assertions.assertEquals(song, findSong); - + } + @Test + void ReadArtist(){ + artistService.deleteAllArtists(); + final Artist artist = artistService.addArtist("Artist", "genre"); final Artist findArtist = artistService.findArtist(artist.getId()); Assertions.assertEquals(artist, findArtist); - - final Album findAlbum = albumService.findAlbum(album.getId()); - Assertions.assertEquals(album, findAlbum); } - -// -// //TestReadAll -// final List songss = songService.findAllSongs(); -// Assertions.assertEquals(songss.size(), 2); -// -// final List artistss = artistService.findAllArtists(); -// Assertions.assertEquals(artistss.size(), 2); -// -// final List albumss = albumService.findAllAlbums(); -// Assertions.assertEquals(albumss.size(), 2); -// -// //TestReadNotFound -// -// //album2 = albumService.updateAlbum(album2.getId(), "стул",songs, artistService.findAllArtists()); -// //albumService.deleteAlbum(album.getId()); -// //albumService.deleteAlbum(album2.getId()); -// List list = songService.findAllSongs(); -// List list2 = artistService.findAllArtists(); + @Test + void testAlbumCheck() throws IOException { + albumService.deleteAllAlbums(); + artistService.deleteAllArtists(); + songService.deleteAllSongs(); + final Artist artist1 = artistService.addArtist("Artist", "genre"); + final Artist artist2 = artistService.addArtist("Artist1", "genre1"); + final Song song = songService.addSong("Song",3.28); + final Album album2= albumService.addAlbum("Album"); + album2.addArtist(artist1); + album2.addArtist(artist2); + Assertions.assertEquals(album2.getArtists().size(),2); + } @Test - void test6() { + void testAlbumPreviewCheck() throws IOException { albumService.deleteAllAlbums(); - songService.deleteAllSongs(); artistService.deleteAllArtists(); - - - Assertions.assertThrows(EntityNotFoundException.class, () -> songService.findSong(-1L)); - Assertions.assertThrows(EntityNotFoundException.class, () -> artistService.findArtist(-1L)); - Assertions.assertThrows(EntityNotFoundException.class, () -> albumService.findAlbum(-1L)); - - //TestReadAllEmpty - final List newComponents = songService.findAllSongs(); - Assertions.assertEquals(newComponents.size(), 0); - - final List newOrders = albumService.findAllAlbums(); - Assertions.assertEquals(newOrders.size(), 0); - - final List newArtists = artistService.findAllArtists(); - Assertions.assertEquals(newArtists.size(), 0); + songService.deleteAllSongs(); + final Artist artist1 = artistService.addArtist("Artist", "genre"); + final Artist artist2 = artistService.addArtist("Artist1", "genre1"); + final Song song = songService.addSong("Song",2.10); + final Album album2= albumService.addAlbum("Album"); + album2.addArtist(artist1); + album2.addArtist(artist2); + Album v = albumService.findAlbum(album2.getId()); + } + @Test + void testSongReadNotFound() { + songService.deleteAllSongs(); + Assertions.assertThrows(EntityNotFoundException.class, () -> songService.findSong(-1L)); + } + @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 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 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 albums = albumService.findAllAlbums(); + Assertions.assertEquals(albums.size(),2); } - @Test void testDop() { - var temp = findService.GetResult("a"); - Assertions.assertEquals(findService.GetResult("a"), temp); +// var temp = findService.GetResult("a"); +// 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> result = findService.GetResult(name); + + // проверяем результаты + assertEquals(result.size(), 3); + assertTrue(result.containsKey(album.getAlbumName())); + assertTrue(result.containsKey(artist.getArtistName())); + assertTrue(result.containsKey(song.getSongName())); + + List albums = result.get("Test Album"); + List artists = result.get("Test Artist"); + List 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"); }