6 Commits

Author SHA1 Message Date
bbdcd30900 Checksums. 2023-05-01 19:03:43 +04:00
Katerina881
67a097458c LabWork3 is done with extra task. 2023-03-21 11:29:45 +04:00
8946fad520 Labwork3 is done. 2023-03-21 02:18:51 +04:00
9655a84025 Labwork3 is done. 2023-03-21 02:18:28 +04:00
Katerina881
444e67736e Front output of array was changed to basic for arrays. 2023-03-07 11:57:44 +04:00
3136b75b1b Labwork2 is done. 2023-03-07 00:41:40 +04:00
33 changed files with 926 additions and 51 deletions

View File

@@ -1,2 +1,2 @@
#Thu Feb 10 13:21:42 GMT+04:00 2022 #Tue Mar 21 10:03:06 GMT+04:00 2023
gradle.version=7.3.3 gradle.version=7.3.3

Binary file not shown.

3
.idea/.gitignore generated vendored
View File

@@ -1,3 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml

1
.idea/.name generated Normal file
View File

@@ -0,0 +1 @@
sbapp

2
.idea/compiler.xml generated
View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="CompilerConfiguration"> <component name="CompilerConfiguration">
<bytecodeTargetLevel target="17" /> <bytecodeTargetLevel target="11" />
</component> </component>
</project> </project>

2
.idea/misc.xml generated
View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="17" project-jdk-type="JavaSDK" /> <component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="17" project-jdk-type="JavaSDK" />
</project> </project>

2
.idea/vcs.xml generated
View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="" vcs="Git" />
</component> </component>
</project> </project>

View File

@@ -14,7 +14,10 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2:2.1.214'
implementation 'junit:junit:4.13.2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
} }
tasks.named('test') { tasks.named('test') {

View File

@@ -5,9 +5,9 @@
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
</head> </head>
<body> <body>
<div class = "flex-container" style = "flex-direction: column; display: flex;"> <div class = "flex-container" style = "flex-direction: column; display: flex; margin-top: 10px; width: 800px; height: 200px">
<div class = "flex-container" style = "flex-direction: row;display: flex; margin-bottom: 10px; gap: 10px;align-items: center; width: 500px;"> <div class = "flex-container" style = "flex-direction: row;display: flex; margin-bottom: 10px; gap: 10px;align-items: center;">
<input type="text" class="form-control form-control-sm" id="word" placeholder="Enter word" style="border: 3px solid black;"> <input type="text" class="form-control form-control-sm" id="value" placeholder="Enter" style="border: 3px solid black;">
<select class = "form-select-sm" id="transformation" aria-label="form-select-sm example"> <select class = "form-select-sm" id="transformation" aria-label="form-select-sm example">
<option value="toupper" selected> toUpper</option> <option value="toupper" selected> toUpper</option>
<option value="removews"> removeWhitespaces</option> <option value="removews"> removeWhitespaces</option>
@@ -16,7 +16,11 @@
</select> </select>
<input id = "result" class="form-control form-control-sm" readonly style="border: 3px dotted black;"> <input id = "result" class="form-control form-control-sm" readonly style="border: 3px dotted black;">
</div> </div>
<div class = "flex-container" style = "flex-direction: row;display: flex; width: 500px; justify-content: center;"> <div class = "flex-container" style = "flex-direction: row;display: flex; justify-content: center; gap: 10px">
<select class = "form-select-sm" id="converter" aria-label="form-select-sm example">
<option value="String" selected> String</option>
<option value="AString"> AString</option>
</select>
<button type="button" class = "btn btn-dark align-self-center" onclick="transform()"> <button type="button" class = "btn btn-dark align-self-center" onclick="transform()">
Transform Transform
</button> </button>
@@ -27,9 +31,13 @@
<script> <script>
async function transform() async function transform()
{ {
const word = document.getElementById("word").value
const _value = document.getElementById("value").value
const func = document.getElementById("transformation").value const func = document.getElementById("transformation").value
document.getElementById("result").value = await (await fetch(`http://localhost:8080/${func}?word=${word}`)).text() const convType = document.getElementById("converter").value
document.getElementById("result").value = await (await fetch(`http://localhost:8080/${func}?value=${_value}&type=${convType}`)).text()
} }
</script> </script>
</body> </body>

View File

@@ -1,34 +0,0 @@
package ru.ulstu.is.sbapp.Controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StringController {
@GetMapping("/toupper")
public String toupper(@RequestParam(value = "word",defaultValue = " ") String word)
{
return word.toUpperCase();
}
@GetMapping("/removews")
public String removewhitespaces(@RequestParam(value = "word",defaultValue = " ") String word)
{
return word.replaceAll("\s+","");
}
@GetMapping("/removedigits")
public String removedigits(@RequestParam(value = "word",defaultValue = " ") String word)
{
return word.replaceAll("\\d","");
}
@GetMapping("/tolower")
public String tolower(@RequestParam(value = "word", defaultValue = " ") String word)
{
return word.toLowerCase();
}
}

View File

@@ -0,0 +1,102 @@
package ru.ulstu.is.sbapp.Models;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
public class Customer
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String first_name;
@Column(nullable = false)
private String last_name;
@Column()
private String middle_name;
@OneToMany(fetch = FetchType.EAGER)
private List<Movie> movies;
public Customer()
{
}
public Customer(String fullName)
{
String[] partsName = fullName.split(" ");
this.first_name = partsName[0];
this.last_name = partsName[1];
if(partsName.length == 3)
{
this.middle_name = partsName[2];
}
this.movies = new ArrayList<>();
}
public Long getId()
{
return id;
}
public String getFirst_name()
{
return first_name;
}
public void setFirst_name(String fullName)
{
String[] partsName = fullName.split(" ");
this.first_name = partsName[0];
}
public String getLast_name()
{
return last_name;
}
public void setLast_name(String fullName)
{
String[] partsName = fullName.split(" ");
this.last_name = partsName[1];
}
public String getMiddle_name()
{
return middle_name.isBlank() ? null : middle_name;
}
public void setMiddle_name(String fullName)
{
String[] partsName = fullName.split(" ");
if(partsName.length == 3)
{
this.middle_name = partsName[2];
}
}
public List<Movie> getMovies()
{
return movies;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
return Objects.equals(id, customer.id);
}
@Override
public String toString()
{
return "Customer: " + "ID: " + id + " | FirstName: " + first_name + " | LastName: " + last_name + " | MiddleName: " + middle_name;
}
}

View File

@@ -0,0 +1,62 @@
package ru.ulstu.is.sbapp.Models;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
public class Genre
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String name;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "genre")
private List<Movie> movies;
public Genre()
{
}
public Genre(String name)
{
this.name = name;
this.movies = new ArrayList<>();
}
public Long getId()
{
return id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public List<Movie> getMovies()
{
return movies;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Genre genre = (Genre) o;
return Objects.equals(id, genre.id);
}
@Override
public String toString()
{
return "Genre: " + " ID: " + id + " | Name: " + name;
}
}

View File

@@ -0,0 +1,95 @@
package ru.ulstu.is.sbapp.Models;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
public class Movie
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String title;
@Column(nullable = false)
private int length;
@Column(nullable = false)
private double score;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "genre_fk")
private Genre genre;
public Movie()
{
}
public Movie(String title, int length, double score, Genre genre)
{
this.title = title;
this.length = length;
this.score = score;
this.genre = genre;
}
public Long getId()
{
return id;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getLength()
{
return Integer.toString(length);
}
public void setLength(int length)
{
this.length = length;
}
public String getScore()
{
return Double.toString(score);
}
public void setScore(Double score)
{
this.score = score;
}
public Genre getGenre()
{
return genre;
}
public void setGenre(Genre genre)
{
this.genre = genre;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Movie movie = (Movie) o;
return Objects.equals(id, movie.id);
}
@Override
public String toString()
{
return "Movie: " + " ID: " + id + " | Title: " + title + " | Length: " + length + " | Score: " + score;
}
}

View File

@@ -4,10 +4,8 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication @SpringBootApplication
@RestController
public class SbappApplication { public class SbappApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@@ -0,0 +1,75 @@
package ru.ulstu.is.sbapp.Services;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import ru.ulstu.is.sbapp.Models.Customer;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import java.util.List;
@Service
public class CustomerService
{
@PersistenceContext
private EntityManager em;
@Transactional
public Customer addCustomer(String fullName)
{
if(!StringUtils.hasText(fullName))
{
throw new IllegalArgumentException("Customer's name or surname is missing");
}
final Customer customer = new Customer(fullName);
em.persist(customer);
return customer;
}
@Transactional(readOnly = true)
public Customer findCustomer(Long id)
{
final Customer customer = em.find(Customer.class,id);
if(customer == null)
{
throw new EntityNotFoundException(String.format("Customer with id [%s] is not found", id));
}
return customer;
}
@Transactional(readOnly = true)
public List<Customer> findAllCustomers()
{
return em.createQuery("select c from Customer c",Customer.class).getResultList();
}
@Transactional
public Customer updateCustomer(Long id, String fullName)
{
if(!StringUtils.hasText(fullName))
{
throw new IllegalArgumentException("Customer's name or surname is missing");
}
final Customer specificCustomer = findCustomer(id);
specificCustomer.setFirst_name(fullName);
specificCustomer.setLast_name(fullName);
specificCustomer.setMiddle_name(fullName);
return em.merge(specificCustomer);
}
@Transactional
public Customer deleteCustomer(Long id)
{
final Customer specificCustomer = findCustomer(id);
em.remove(specificCustomer);
return specificCustomer;
}
@Transactional
public void deleteAllCustomers()
{
em.createQuery("delete from Customer").executeUpdate();
}
}

View File

@@ -0,0 +1,73 @@
package ru.ulstu.is.sbapp.Services;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import ru.ulstu.is.sbapp.Models.Genre;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import java.util.List;
@Service
public class GenreService
{
@PersistenceContext
private EntityManager em;
@Transactional
public Genre addGenre(String name)
{
if(!StringUtils.hasText(name))
{
throw new IllegalArgumentException("Genre's name is missing");
}
final Genre genre = new Genre(name);
em.persist(genre);
return genre;
}
@Transactional(readOnly = true)
public Genre findGenre(Long id)
{
final Genre specificGenre = em.find(Genre.class,id);
if(specificGenre == null)
{
throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", id));
}
return specificGenre;
}
@Transactional(readOnly = true)
public List<Genre> findAllGenres()
{
return em.createQuery("select g from Genre g",Genre.class).getResultList();
}
@Transactional
public Genre updateGenre(Long id, String name)
{
if(!StringUtils.hasText(name))
{
throw new IllegalArgumentException("Genre's name is missing");
}
final Genre specificGenre = findGenre(id);
specificGenre.setName(name);
return em.merge(specificGenre);
}
@Transactional
public Genre deleteGenre(Long id)
{
final Genre specificGenre = findGenre(id);
em.remove(specificGenre);
return specificGenre;
}
@Transactional
public void deleteAllGenres()
{
em.createQuery("delete from Genre").executeUpdate();
}
}

View File

@@ -0,0 +1,89 @@
package ru.ulstu.is.sbapp.Services;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import ru.ulstu.is.sbapp.Models.Movie;
import ru.ulstu.is.sbapp.Models.Customer;
import ru.ulstu.is.sbapp.Models.Genre;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import java.util.List;
@Service
public class MovieService
{
@PersistenceContext
private EntityManager em;
@Transactional
public Movie addMovie(String title, int length, double score, Genre genre, Customer customer)
{
if(!StringUtils.hasText(title) || length == 0 || score == 0 || genre == null || customer == null)
{
throw new IllegalArgumentException("Some of the movie's properties are incorrect.");
}
final Movie movie = new Movie(title,length,score,genre);
movie.getGenre().getMovies().add(movie);
customer.getMovies().add(movie);
em.persist(movie);
return movie;
}
@Transactional(readOnly = true)
public Movie findMovie(Long id)
{
final Movie specificMovie = em.find(Movie.class, id);
if(specificMovie == null)
{
throw new EntityNotFoundException(String.format("Movie with id [%s] is not found", id));
}
return specificMovie;
}
@Transactional(readOnly = true)
public List<Movie> findAllMovies()
{
return em.createQuery("select m from Movie m",Movie.class).getResultList();
}
@Transactional
public Movie updateMovie(Long id, String title, int length, double score, Genre genre)
{
if(!StringUtils.hasText(title) || length == 0 || score == 0 || genre == null)
{
throw new IllegalArgumentException("Some of the movie's properties are incorrect.");
}
final Movie specificMovie = findMovie(id);
specificMovie.setLength(length);
specificMovie.setGenre(genre);
specificMovie.setTitle(title);
specificMovie.setScore(score);
return em.merge(specificMovie);
}
@Transactional
public Movie deleteMovie(Long id, Customer customer)
{
final Movie specificMovie = findMovie(id);
specificMovie.getGenre().getMovies().remove(specificMovie);
customer.getMovies().remove(specificMovie);
em.remove(specificMovie);
return specificMovie;
}
@Transactional
public List<Movie> findAllSpecificMovies(Genre specGenre)
{
return em.createQuery("SELECT m FROM Movie m WHERE m.genre = :genre",Movie.class).setParameter("genre",specGenre).getResultList();
}
@Transactional
public void deleteAllMovies()
{
em.createQuery("delete from Movie").executeUpdate();
}
}

View File

@@ -1 +1,11 @@
spring.main.banner-mode=off
#server.port=8080
spring.datasource.url=jdbc:h2:file:./data
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=update
spring.h2.console.enabled=true
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

View File

@@ -0,0 +1,93 @@
package ru.ulstu.is.sbapp;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.ulstu.is.sbapp.Models.Customer;
import ru.ulstu.is.sbapp.Services.CustomerService;
import javax.persistence.EntityNotFoundException;
import java.util.List;
@SpringBootTest
public class JpaCustomerTests
{
private static final Logger log = LoggerFactory.getLogger(JpaCustomerTests.class);
@Autowired
private CustomerService customerService;
@Test
void testCustomerCreate()
{
customerService.deleteAllCustomers();
final Customer customer = customerService.addCustomer("Nikita Lisov");
log.info(customer.toString());
Assertions.assertNotNull(customer.getId());
}
@Test
void testCustomerRead()
{
customerService.deleteAllCustomers();
final Customer customer = customerService.addCustomer("Nikita Lisov");
log.info(customer.toString());
final Customer findCustomer = customerService.findCustomer(customer.getId());
log.info(findCustomer.toString());
Assertions.assertEquals(customer, findCustomer);
}
@Test
void testCustomerReadNotFound()
{
customerService.deleteAllCustomers();
Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(-1L));
}
@Test
void testCustomerReadAll()
{
customerService.deleteAllCustomers();
customerService.addCustomer("Nikita Lisov");
customerService.addCustomer("Evelina Aust Sergeevna");
final List<Customer> customers = customerService.findAllCustomers();
log.info(customers.toString());
Assertions.assertEquals(customers.size(), 2);
}
@Test
void testCustomerReadAllEmpty()
{
customerService.deleteAllCustomers();
final List<Customer> customers = customerService.findAllCustomers();
log.info(customers.toString());
Assertions.assertEquals(customers.size(), 0);
}
@Test
void testCustomerChanges()
{
customerService.deleteAllCustomers();
final Customer customer = customerService.addCustomer("Nikita Lisov");
log.info(customer.toString());
final Customer changedCustomer = customerService.updateCustomer(customer.getId(),"Evelina Potter");
log.info(changedCustomer.toString());
Assertions.assertEquals("Evelina",changedCustomer.getFirst_name());
}
@Test
void testDeleteSpecificCustomer()
{
customerService.deleteAllCustomers();
final Customer customer1 = customerService.addCustomer("Nikita Lisov");
final Customer customer2 = customerService.addCustomer("Evelina Potter");
customerService.deleteCustomer(customer1.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(customer1.getId()));
}
}

View File

@@ -0,0 +1,93 @@
package ru.ulstu.is.sbapp;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.ulstu.is.sbapp.Models.Customer;
import ru.ulstu.is.sbapp.Models.Genre;
import ru.ulstu.is.sbapp.Services.GenreService;
import javax.persistence.EntityNotFoundException;
import java.util.List;
@SpringBootTest
public class JpaGenreTests
{
private static final Logger log = LoggerFactory.getLogger(JpaCustomerTests.class);
@Autowired
private GenreService genreService;
@Test
void testCustomerCreate()
{
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Fantasy");
log.info(genre.toString());
Assertions.assertNotNull(genre.getId());
}
@Test
void testCustomerRead()
{
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Nikita Lisov");
log.info(genre.toString());
final Genre findCustomer = genreService.findGenre(genre.getId());
log.info(findCustomer.toString());
Assertions.assertEquals(genre, findCustomer);
}
@Test
void testCustomerReadNotFound()
{
genreService.deleteAllGenres();
Assertions.assertThrows(EntityNotFoundException.class, () -> genreService.findGenre(-1L));
}
@Test
void testCustomerReadAll()
{
genreService.deleteAllGenres();
genreService.addGenre("Nikita Lisov");
genreService.addGenre("Evelina Aust Sergeevna");
final List<Genre> customers = genreService.findAllGenres();
log.info(customers.toString());
Assertions.assertEquals(customers.size(), 2);
}
@Test
void testCustomerReadAllEmpty()
{
genreService.deleteAllGenres();
final List<Genre> customers = genreService.findAllGenres();
log.info(customers.toString());
Assertions.assertEquals(customers.size(), 0);
}
@Test
void testGenreChanges()
{
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Fantasy");
log.info(genre.toString());
final Genre changedGenre = genreService.updateGenre(genre.getId(),"Sci-Fi");
log.info(changedGenre.toString());
Assertions.assertEquals("Sci-Fi",changedGenre.getName());
}
@Test
void testDeleteSpecificGenre()
{
genreService.deleteAllGenres();
final Genre genre1 = genreService.addGenre("Nikita Lisov");
final Genre genre2 = genreService.addGenre("Evelina Potter");
genreService.deleteGenre(genre1.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> genreService.findGenre(genre1.getId()));
}
}

View File

@@ -0,0 +1,204 @@
package ru.ulstu.is.sbapp;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.ulstu.is.sbapp.Services.MovieService;
import ru.ulstu.is.sbapp.Models.Movie;
import ru.ulstu.is.sbapp.Models.Customer;
import ru.ulstu.is.sbapp.Services.CustomerService;
import ru.ulstu.is.sbapp.Models.Genre;
import ru.ulstu.is.sbapp.Services.GenreService;
import javax.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
public class JpaMovieTests
{
private static final Logger log = LoggerFactory.getLogger(JpaCustomerTests.class);
@Autowired
private MovieService movieService;
@Autowired
private GenreService genreService;
@Autowired
private CustomerService customerService;
@Test
void testMovieCreate()
{
movieService.deleteAllMovies();
customerService.deleteAllCustomers();
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Fantasy");
log.info(genre.toString());
final Customer customer = customerService.addCustomer("Nikita Lisov");
log.info(customer.toString());
final Movie movie = movieService.addMovie("Arcane",36,4.5, genre, customer);
log.info(movie.toString());
Assertions.assertNotNull(movie.getId());
Assertions.assertTrue(customer.getMovies().contains(movie));
}
@Test
void testMovieRead()
{
movieService.deleteAllMovies();
customerService.deleteAllCustomers();
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Fantasy");
log.info(genre.toString());
final Customer customer = customerService.addCustomer("Nikita Lisov");
log.info(customer.toString());
final Movie movie = movieService.addMovie("Arcane",36,4.5, genre, customer);
log.info(movie.toString());
final Movie findMovie = movieService.findMovie(movie.getId());
log.info(findMovie.toString());
Assertions.assertEquals(movie,findMovie);
}
@Test
void testCheckMoviesCustomer()
{
movieService.deleteAllMovies();
customerService.deleteAllCustomers();
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Fantasy");
log.info(genre.toString());
final Customer customer = customerService.addCustomer("Nikita Lisov");
log.info(customer.toString());
final Movie movie = movieService.addMovie("Arcane",36,4.5, genre, customer);
log.info(movie.toString());
log.info(customer.getMovies().toString());
final List<Movie> movies = movieService.findAllMovies();
log.info(movies.toString());
Assertions.assertEquals(movies.size(),customer.getMovies().size());
}
@Test
void testCheckMoviesGenre()
{
movieService.deleteAllMovies();
customerService.deleteAllCustomers();
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Fantasy");
log.info(genre.toString());
final Customer customer = customerService.addCustomer("Nikita Lisov");
log.info(customer.toString());
final Movie movie = movieService.addMovie("Arcane",36,4.5, genre, customer);
log.info(movie.toString());
final List<Movie> movies = movieService.findAllMovies();
log.info(movies.toString());
Assertions.assertEquals(movies.size(),genre.getMovies().size());
}
@Test
void testCheckMultipleMovies()
{
movieService.deleteAllMovies();
customerService.deleteAllCustomers();
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Fantasy");
log.info(genre.toString());
final Customer customer1 = customerService.addCustomer("Nikita Lisov");
log.info(customer1.toString());
final Customer customer2 = customerService.addCustomer("Evelina Potter");
log.info(customer2.toString());
final Movie movie1 = movieService.addMovie("Arcane",36,4.5, genre, customer1);
log.info(movie1.toString());
final Movie movie2 = movieService.addMovie("Harry Potter",128,10.0, genre, customer2);
log.info(movie2.toString());
final List<Movie> movies = movieService.findAllMovies();
log.info(movies.toString());
Assertions.assertEquals(movies.size(),customer1.getMovies().size() + customer2.getMovies().size());
}
@Test
void testMovieReadNotFound()
{
movieService.deleteAllMovies();
customerService.deleteAllCustomers();
genreService.deleteAllGenres();
Assertions.assertThrows(EntityNotFoundException.class, () -> movieService.findMovie(-1L));
}
@Test
void testMovieReadAllEmpty()
{
movieService.deleteAllMovies();
customerService.deleteAllCustomers();
genreService.deleteAllGenres();
final List<Movie> movies = movieService.findAllMovies();
log.info(movies.toString());
Assertions.assertEquals(movies.size(),0);
}
@Test
void testGetAllGenreMovies()
{
movieService.deleteAllMovies();
customerService.deleteAllCustomers();
genreService.deleteAllGenres();
final Genre genre1 = genreService.addGenre("Fantasy");
log.info(genre1.toString());
final Genre genre2 = genreService.addGenre("Sci-Fi");
log.info(genre2.toString());
final Customer customer = customerService.addCustomer("Nikita Lisov");
log.info(customer.toString());
final Movie movie1 = movieService.addMovie("Arcane",36,4.5, genre1, customer);
log.info(movie1.toString());
final Movie movie2 = movieService.addMovie("Harry Potter",128,10.0, genre1, customer);
log.info(movie2.toString());
final Movie movie3 = movieService.addMovie("Interstellar",2,9.5, genre2, customer);
log.info(movie3.toString());
List<Movie> movies = new ArrayList<>(){};
movies.add(movie1);
movies.add(movie2);
List<Movie> expectedMovies = movieService.findAllSpecificMovies(genre1);
log.info(expectedMovies.toString());
Assertions.assertArrayEquals(movies.toArray(),expectedMovies.toArray());
}
}

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