Compare commits

...

4 Commits

13 changed files with 301 additions and 253 deletions

View File

@ -17,6 +17,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2:2.1.210' implementation 'com.h2database:h2:2.1.210'
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5' implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
implementation 'org.hibernate.validator:hibernate-validator'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
} }

View File

@ -1,110 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>LabWork02</title>
<script src="/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.min.css">
</head>
<body class="bg-light">
<div class="container">
<div class="py-5 text-center"><h2>Лабораторная работа 2</h2></div>
<div class="row">
<div>
<form>
<div class="row">
<div class="col-md-6 mb-3">
<label for="input1">Значение 1 (string или double)</label>
<input class="form-control" id="input1" placeholder="Введите значение 1">
</div>
<div class="col-md-6 mb-3">
<label for="input1">Значение 2 (string или double)</label>
<input class="form-control" id="input2" placeholder="Введите значение 2">
</div>
</div>
<div class="row">
<div class="form-group">
<label for="operator">Выберите функцию</label>
<select class="form-control" id="operator">
<option value>Выбрать...</option>
<option value="Func1">(string, double)Сумма</option>
<option value="Func2">(double)Минимум, (string)Сумма в верхний регистр</option>
<option value="Func3">(double)Mаксимум, (string)Разделить</option>
<option value="Func4">(double)Число 1 в степени числа 2, (string)Сумма в нижний регистр</option>
</select>
</div>
</div>
<div class="row">
<div class="form-group">
<label for="operator">Выберите тип данных</label>
<select class="form-control" id="Type">
<option value>Выбрать...</option>
<option value="str">String</option>
<option value="double">Double</option>
</select>
</div>
</div>
<div class="row">
<div class="form-group">
<label for="result">Результат</label>
<input type="text" class="form-control result" id="result" readonly>
<hr class="mb-4">
<button type="button" class="btn btn-primary btn-block" onclick="calculate()">Посчитать</button>
</div>
</div>
</form>
</div>
</div>
</div>
<!--<div class="container mt-4">-->
<!-- <div class="row justify-content-center">-->
<!-- <div class="col-lg-6">-->
<!-- <h3 class="text-center font-weight-light my-2">Лабораторная работа 2</h3>-->
<!-- <form>-->
<!-- <div class="form-row">-->
<!-- <div class="col">-->
<!-- <div class="form-group">-->
<!-- <label for="input1">Значение 1 (string или double)</label>-->
<!-- <input class="form-control" id="input1" placeholder="Введите значение 1">-->
<!-- </div>-->
<!-- </div>-->
<!-- <div class="col">-->
<!-- <div class="form-group">-->
<!-- <label for="input2">Значение 2 (string или double)</label>-->
<!-- <input class="form-control" id="input2" placeholder="Введите значение 2">-->
<!-- </div>-->
<!-- </div>-->
<!-- <div class="col">-->
<!-- <div class="form-group">-->
<!-- <label for="operator">Выбрать функцию...</label>-->
<!-- <select class="form-control" id="operator">-->
<!-- <option value="Func1">Сумма</option>-->
<!-- <option value="Func2">Минимум, Сумма в верхний регистр</option>-->
<!-- <option value="Func3">Mаксимум, Разделить</option>-->
<!-- <option value="Func4">Число 1 в степени числа 2, Сумма в нижний регистр</option>-->
<!-- </select>-->
<!-- </div>-->
<!-- </div>-->
<!-- <div class="col">-->
<!-- <div class="form-group">-->
<!-- <label for="operator">Выбрать тип данных...</label>-->
<!-- <select class="form-control" id="Type">-->
<!-- <option value="str">String</option>-->
<!-- <option value="double">Double</option>-->
<!-- </select>-->
<!-- </div>-->
<!-- </div>-->
<!-- </div>-->
<!-- <div class="form-group">-->
<!-- <label for="result">Результат</label>-->
<!-- <input type="text" class="form-control result" id="result" readonly>-->
<!-- </div>-->
<!-- <button type="button" class="btn btn-primary btn-block" onclick="calculate()">Посчитать</button>-->
<!-- </form>-->
<!-- </div>-->
<!-- </div>-->
<!--</div>-->
<script src = "script.js"></script>
</body>
</html>

View File

@ -1,15 +0,0 @@
{
"name": "IP",
"version": "1.0.0",
"main": "index.html",
"scripts": {
"start": "http-server -p 3001 ./",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"bootstrap": "5.2.1"
},
"devDependencies": {
"http-server": "^14.1.1"
}
}

View File

@ -1,13 +0,0 @@
'use strict'
async function calculate(){
let num1 = document.getElementById("input1").value
let num2 = document.getElementById("input2").value
let operator = document.getElementById("operator").value
let result = document.getElementById("result")
let type = document.getElementById("Type").value
let response = await fetch(`http://localhost:8080/${operator}?Type=${type}&value1=${num1}&value2=${num2}`)
let res = await response.text()
result.value = res
}

View File

@ -9,18 +9,21 @@ import java.util.Objects;
@Table(name = "album") @Table(name = "album")
public class Album { public class Album {
@Id @Id
@GeneratedValue @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
@Column(name = "name") @Column(name = "name")
private String albumName; private String albumName;
@OneToMany(cascade = {CascadeType.MERGE})
@JoinColumn(name = "songs", nullable = true) @OneToMany(mappedBy = "album", cascade = CascadeType.ALL, orphanRemoval = true,fetch = FetchType.LAZY)
private List<Song> songs; private List<Song> songs = new ArrayList<>();
@ManyToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.EAGER)
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
@JoinTable(name = "albums_artists", @JoinTable(name = "albums_artists",
joinColumns = @JoinColumn(name = "album_fk"), joinColumns = @JoinColumn(name = "album_id"),
inverseJoinColumns = @JoinColumn(name = "artist_fk")) inverseJoinColumns = @JoinColumn(name = "artist_id"))
private List<Artist> artists; private List<Artist> artists = new ArrayList<>();
public Album(){ public Album(){
} }
public Album(String albumName){ public Album(String albumName){

View File

@ -14,8 +14,8 @@ public class Artist {
private String artistName; private String artistName;
@Column(name = "genre") @Column(name = "genre")
private String genre; private String genre;
@ManyToMany(mappedBy = "artists", cascade = CascadeType.MERGE, fetch = FetchType.EAGER) @ManyToMany(mappedBy = "artists", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
private List<Album> album; private List<Album> albums;
public Artist() { public Artist() {
} }
@ -44,25 +44,25 @@ public class Artist {
this.genre = genre; this.genre = genre;
} }
public List<Album> getAlbum() { public List<Album> getAlbums() {
return album; return albums;
} }
public void setAlbum(List<Album> album) { public void setAlbums(List<Album> albums) {
this.album = album; this.albums = albums;
} }
public void addAlbum(Album album) { public void addAlbum(Album album) {
if (this.album == null) { if (this.albums == null) {
this.album = new ArrayList<>(); this.albums = new ArrayList<>();
} }
if (!this.album.contains(album)) if (!this.albums.contains(album))
this.album.add(album); this.albums.add(album);
} }
public void removeAlbum(Album album) { public void removeAlbum(Album album) {
if (this.album.contains(album)) if (this.albums.contains(album))
this.album.remove(album); this.albums.remove(album);
} }
@Override @Override

View File

@ -8,14 +8,17 @@ import java.util.Objects;
@Table(name = "song") @Table(name = "song")
public class Song { public class Song {
@Id @Id
@GeneratedValue @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
@Column(name = "name") @Column(name = "name")
private String songName; private String songName;
@Column(name = "duration") @Column(name = "duration")
private Double duration; private Double duration;
@ManyToOne(cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
@JoinColumn(name = "album", nullable = true) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "album_id")
private Album album; private Album album;
public Song(){ public Song(){
} }

View File

@ -1,5 +1,6 @@
package ru.ulstu.is.sbapp.database.service; package ru.ulstu.is.sbapp.database.service;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -8,7 +9,9 @@ import ru.ulstu.is.sbapp.database.model.Song;
import ru.ulstu.is.sbapp.database.model.Album; import ru.ulstu.is.sbapp.database.model.Album;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
@Service @Service
@ -38,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) {
@ -119,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) {

View File

@ -1,5 +1,6 @@
package ru.ulstu.is.sbapp.database.service; package ru.ulstu.is.sbapp.database.service;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -9,13 +10,17 @@ import ru.ulstu.is.sbapp.database.model.Album;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
@Service @Service
public class ArtistService { public class ArtistService {
@PersistenceContext @PersistenceContext
private EntityManager em; private EntityManager em;
private SongService ss;
@Transactional @Transactional
public Artist addArtist(String name, String genre) { public Artist addArtist(String name, String genre) {
if (!StringUtils.hasText(name) || !StringUtils.hasText(genre)) { if (!StringUtils.hasText(name) || !StringUtils.hasText(genre)) {
@ -66,9 +71,9 @@ public class ArtistService {
@Transactional @Transactional
public Artist deleteArtist(Long id) { public Artist deleteArtist(Long id) {
final Artist currentArtist = findArtist(id); final Artist currentArtist = findArtist(id);
int size = currentArtist.getAlbum().size(); int size = currentArtist.getAlbums().size();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
Album temp = currentArtist.getAlbum().get(i); Album temp = currentArtist.getAlbums().get(i);
temp.removeArtist(currentArtist); temp.removeArtist(currentArtist);
currentArtist.removeAlbum(temp); currentArtist.removeAlbum(temp);
em.remove(temp); em.remove(temp);
@ -76,9 +81,9 @@ public class ArtistService {
em.remove(currentArtist); em.remove(currentArtist);
return currentArtist; return currentArtist;
} }
@Transactional @Transactional
public List<Song> findAllSongsProducedArtist(Artist currentArtist){ public List<Song> findAllSongsProducedArtist(Artist currentArtist){
if(currentArtist.getAlbum().size() == 0){ if(currentArtist.getAlbums().size() == 0){
throw new IllegalArgumentException("Artist doesn`t produced"); throw new IllegalArgumentException("Artist doesn`t produced");
} }
List songList = em.createQuery("SELECT DISTINCT a.songs FROM Album a where :artistAlbum MEMBER OF a.artists") List songList = em.createQuery("SELECT DISTINCT a.songs FROM Album a where :artistAlbum MEMBER OF a.artists")

View File

@ -0,0 +1,57 @@
package ru.ulstu.is.sbapp.database.service;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
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.*;
@Service
public class FindByNameService {
@PersistenceContext
private EntityManager em;
@Transactional
public Map<String, List<Object>> GetResult(String name) {
Map<String, List<Object>> resultMap = new HashMap<>();
// запрос для поиска песен по названию
List<Song> songs = em.createQuery("SELECT s FROM Song s WHERE s.songName = :name", Song.class)
.setParameter("name", name).getResultList();
// запрос для поиска альбомов по названию
List<Album> albums = em.createQuery("SELECT a FROM Album a WHERE a.albumName = :name", Album.class)
.setParameter("name", name).getResultList();
// запрос для поиска артистов по имени
List<Artist> artists = em.createQuery("SELECT a FROM Artist a WHERE a.artistName = :name", Artist.class)
.setParameter("name", name).getResultList();
// добавляем результаты поиска песен в resultMap
List<Object> songsResult = new ArrayList<>();
for (Song song : songs) {
songsResult.add(song);
}
resultMap.put("songs", songsResult);
// добавляем результаты поиска альбомов в resultMap
List<Object> albumsResult = new ArrayList<>();
for (Album album : albums) {
albumsResult.add(album);
}
resultMap.put("albums", albumsResult);
// добавляем результаты поиска артистов в resultMap
List<Object> artistsResult = new ArrayList<>();
for (Artist artist : artists) {
artistsResult.add(artist);
}
resultMap.put("artists", artistsResult);
return resultMap;
}
}

View File

@ -1,14 +1,18 @@
package ru.ulstu.is.sbapp.database.service; package ru.ulstu.is.sbapp.database.service;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import ru.ulstu.is.sbapp.database.model.Artist;
import ru.ulstu.is.sbapp.database.model.Song; import ru.ulstu.is.sbapp.database.model.Song;
import ru.ulstu.is.sbapp.database.model.Album; import ru.ulstu.is.sbapp.database.model.Album;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
@Service @Service
public class SongService { public class SongService {

View File

@ -1,118 +1,215 @@
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;
import ru.ulstu.is.sbapp.database.model.Song; import ru.ulstu.is.sbapp.database.model.Song;
import ru.ulstu.is.sbapp.database.model.Album; import ru.ulstu.is.sbapp.database.model.Album;
import ru.ulstu.is.sbapp.database.service.ArtistService; import ru.ulstu.is.sbapp.database.service.ArtistService;
import ru.ulstu.is.sbapp.database.service.FindByNameService;
import ru.ulstu.is.sbapp.database.service.SongService; 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.List; 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 @SpringBootTest
public class SbappApplicationTests { public class SbappApplicationTests {
@Autowired @Autowired
private AlbumService albumService; private AlbumService albumService;
@Autowired @Autowired
private ArtistService artistService;
@Autowired
private SongService songService; private SongService songService;
@Autowired @Autowired
private ArtistService artistService; private FindByNameService findService;
@Test
void testSongCreate(){
songService.deleteAllSongs();
final Song song = songService.addSong("Song",2.36);
Assertions.assertNotNull(song.getId());
}
@Test @Test
void test1(){ void testArtistCreate(){
artistService.deleteAllArtists();
final Artist artist = artistService.addArtist("Artist", "genre");
Assertions.assertNotNull(artist.getId());
}
@Test
void testAlbumCreate() throws IOException {
albumService.deleteAllAlbums(); albumService.deleteAllAlbums();
artistService.deleteAllArtists(); artistService.deleteAllArtists();
songService.deleteAllSongs(); songService.deleteAllSongs();
final Artist artist1 = artistService.addArtist("Artist", "genre");
final Artist artist2 = artistService.addArtist("Artist", "genre");
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());
}
final Song song = songService.addSong("song", 2.50); @Test
Assertions.assertNotNull(song.getId()); void ReadAlbum() throws IOException {
albumService.deleteAllAlbums();
Song song2 = songService.addSong("song2", 3.10); artistService.deleteAllArtists();
songService.deleteAllSongs();
List<Song> songs = new ArrayList<>(); final Artist artist1 = artistService.addArtist("Artist", "genre");
songs.add(song); final Song song = songService.addSong("Song",3.28);
songs.add(song2); final Album album2= albumService.addAlbum("Album");
album2.addArtist(artist1);
Artist artist = artistService.addArtist("artist", "genre"); final Album findAlbum = albumService.findAlbum(album2.getId());
Assertions.assertNotNull(artist.getId()); Assertions.assertEquals(album2, findAlbum);
}
Artist artist2 = artistService.addArtist("artist2", "genre"); @Test
void ReadAlbumTrue() throws IOException {
List<Artist> artists = new ArrayList<>(); albumService.deleteAllAlbums();
artists.add(artist); artistService.deleteAllArtists();
artists.add(artist2); songService.deleteAllSongs();
final Artist artist1 = artistService.addArtist("Artist", "genre");
Album album = albumService.addAlbum("album"); final Artist artist2 = artistService.addArtist("Artist2", "genre");
Album album2 = albumService.addAlbum("album2"); final Song song = songService.addSong("Song",3.19);
Assertions.assertNotNull(album.getId()); Album album2= albumService.addAlbum("Album");
album2 =albumService.addArtist(album2.getId(),artist1.getId());
albumService.addArtist(album, artists); album2 =albumService.addArtist(album2.getId(),artist2.getId());
albumService.addSong(album, songs); final Album findAlbum = albumService.findAlbum(album2.getId());
Assertions.assertEquals(album2, findAlbum);
artist2 = artistService.updateArtist(artist2.getId(), "artist3", "genre2"); }
Assertions.assertEquals("artist3", artistService.findArtist(artist2.getId()).getArtistName()); @Test
void ReadSong(){
song2 = songService.updateSong(song2.getId(), "song3", 3.40, null); songService.deleteAllSongs();
Assertions.assertEquals("song3", songService.findSong(song2.getId()).getSongName()); final Song song = songService.addSong("Song",3.29);
final Album album = albumService.addAlbum("Album");
album = albumService.updateAlbum(album.getId(), "album3", songs, artists); song.setAlbum(album);
Assertions.assertEquals("album3", albumService.findAlbum(album.getId()).getAlbumName());
Assertions.assertEquals(true, artistService.findAllSongsProducedArtist(artist).contains(song));
Assertions.assertEquals(true, songService.findSongsInAlbum(song).contains(song2));
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()); @Test
Assertions.assertEquals(album, findAlbum); void testAlbumCheck() throws IOException {
//TestReadAll
final List<Song> songss = songService.findAllSongs();
Assertions.assertEquals(songss.size(), 2);
final List<Artist> artistss = artistService.findAllArtists();
Assertions.assertEquals(artistss.size(), 2);
final List<Album> 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<Song> list = songService.findAllSongs();
List<Artist> list2 = artistService.findAllArtists();
albumService.deleteAllAlbums(); albumService.deleteAllAlbums();
songService.deleteAllSongs();
artistService.deleteAllArtists(); 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 testAlbumPreviewCheck() 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",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)); Assertions.assertThrows(EntityNotFoundException.class, () -> songService.findSong(-1L));
}
@Test
void testArtistReadNotFound() {
artistService.deleteAllArtists();
Assertions.assertThrows(EntityNotFoundException.class, () -> artistService.findArtist(-1L)); Assertions.assertThrows(EntityNotFoundException.class, () -> artistService.findArtist(-1L));
}
@Test
void testAlbumReadNotFound() {
albumService.deleteAllAlbums();
Assertions.assertThrows(EntityNotFoundException.class, () -> albumService.findAlbum(-1L)); 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
void testDop() {
String nameToTest = "a";
//TestReadAllEmpty // создаем данные для тестирования
final List<Song> newComponents = songService.findAllSongs(); albumService.deleteAllAlbums();
Assertions.assertEquals(newComponents.size(), 0); artistService.deleteAllArtists();
songService.deleteAllSongs();
final Artist artist1 = artistService.addArtist(nameToTest, "genre");
final Album album1= albumService.addAlbum(nameToTest);
final Song song1 = songService.addSong(nameToTest, 3.29);
album1.addArtist(artist1);
artist1.addAlbum(album1);
album1.addSong(song1);
final List<Album> newOrders = albumService.findAllAlbums(); Map<String, List<Object>> resultMap = findService.GetResult(nameToTest);
Assertions.assertEquals(newOrders.size(), 0);
final List<Artist> newArtists = artistService.findAllArtists(); List<Object> artistsList = resultMap.get("artists");
Assertions.assertEquals(newArtists.size(), 0); assertEquals(1, artistsList.size());
Object artistResult = artistsList.get(0);
assertEquals(nameToTest, ((Artist) artistResult).getArtistName());
List<Object> albumsList = resultMap.get("albums");
assertEquals(1, albumsList.size());
Object albumResult = albumsList.get(0);
assertEquals(nameToTest, ((Album) albumResult).getAlbumName());
List<Object> songsList = resultMap.get("songs");
assertEquals(1, songsList.size());
Object songResult = songsList.get(0);
assertEquals(nameToTest, ((Song) songResult).getSongName());
} }
} }

View File

@ -0,0 +1,6 @@
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=create-drop