Compare commits
6 Commits
TommyVersi
...
master
Author | SHA1 | Date |
---|---|---|
|
8c79d986d5 | 2 weeks ago |
|
cc543d1af3 | 2 weeks ago |
![]() |
7f96ab14a4 | 3 weeks ago |
|
ac30d228ca | 3 weeks ago |
|
2fb0914cca | 3 weeks ago |
|
24e80f96f9 | 3 weeks ago |
@ -1,6 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="Encoding">
|
<component name="Encoding">
|
||||||
<file url="file://$PROJECT_DIR$/WF-WEBAPI/src/main/java" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -1,8 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="ProjectModuleManager">
|
|
||||||
<modules>
|
|
||||||
<module fileurl="file://$PROJECT_DIR$/.idea/WF-WEB-API.iml" filepath="$PROJECT_DIR$/.idea/WF-WEB-API.iml" />
|
|
||||||
</modules>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
@ -1,33 +0,0 @@
|
|||||||
package com.example.wfwebapi.config;
|
|
||||||
|
|
||||||
import com.example.wfwebapi.security.JwtAuthenticationFilter;
|
|
||||||
import com.example.wfwebapi.security.JwtTokenProvider;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.security.config.Customizer;
|
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
||||||
import org.springframework.security.web.SecurityFilterChain;
|
|
||||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class SecurityConfig {
|
|
||||||
|
|
||||||
private final JwtTokenProvider tokenProvider;
|
|
||||||
|
|
||||||
public SecurityConfig(JwtTokenProvider tokenProvider) {
|
|
||||||
this.tokenProvider = tokenProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
|
||||||
http.csrf(csrf -> csrf.disable())
|
|
||||||
.authorizeHttpRequests(authz -> authz
|
|
||||||
.requestMatchers("/api/v1/auth/**").permitAll() // par exemple, pour la connexion
|
|
||||||
.anyRequest().authenticated()
|
|
||||||
)
|
|
||||||
.addFilterBefore(new JwtAuthenticationFilter(tokenProvider), UsernamePasswordAuthenticationFilter.class)
|
|
||||||
.httpBasic(Customizer.withDefaults());
|
|
||||||
|
|
||||||
return http.build();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
spring.application.name=WF-WEBAPI
|
|
||||||
|
|
||||||
# Utilisation de H2 avec le script d'initialisation
|
|
||||||
spring.datasource.url=jdbc:h2:mem:testdb
|
|
||||||
spring.datasource.driverClassName=org.h2.Driver
|
|
||||||
spring.datasource.username=sa
|
|
||||||
spring.datasource.password=
|
|
||||||
spring.sql.init.mode=always
|
|
||||||
spring.sql.init.schema-locations=classpath:init.sql
|
|
||||||
spring.jpa.hibernate.ddl-auto=none
|
|
||||||
spring.h2.console.enabled=true
|
|
||||||
server.port=8080
|
|
@ -1,338 +0,0 @@
|
|||||||
-- Suppression des tables
|
|
||||||
DROP TABLE IF EXISTS Commentary;
|
|
||||||
DROP TABLE IF EXISTS Favorite;
|
|
||||||
DROP TABLE IF EXISTS DailyQuote;
|
|
||||||
DROP TABLE IF EXISTS Quote;
|
|
||||||
DROP TABLE IF EXISTS Caracter;
|
|
||||||
DROP TABLE IF EXISTS Source;
|
|
||||||
DROP TABLE IF EXISTS Record_quiz;
|
|
||||||
DROP TABLE IF EXISTS Quiz_Question;
|
|
||||||
DROP TABLE IF EXISTS Quiz;
|
|
||||||
DROP TABLE IF EXISTS Question;
|
|
||||||
DROP TABLE IF EXISTS Admin;
|
|
||||||
DROP TABLE IF EXISTS Users;
|
|
||||||
DROP TABLE IF EXISTS Image;
|
|
||||||
|
|
||||||
-- Création des tables
|
|
||||||
-------------------------------------------------------------------------
|
|
||||||
|
|
||||||
CREATE TABLE Image(
|
|
||||||
id_img NUMERIC PRIMARY KEY,
|
|
||||||
imgPath varchar(300) NOT NULL UNIQUE
|
|
||||||
);
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------
|
|
||||||
|
|
||||||
CREATE TABLE Users(
|
|
||||||
id_user SERIAL PRIMARY KEY,
|
|
||||||
username varchar(50) NOT NULL,
|
|
||||||
email varchar(50) NOT NULL,
|
|
||||||
password varchar(100) NOT NULL,
|
|
||||||
img NUMERIC NOT NULL,
|
|
||||||
creation date NOT NULL,
|
|
||||||
CONSTRAINT unique_col UNIQUE (email),
|
|
||||||
CONSTRAINT fk_img FOREIGN KEY(img) REFERENCES Image(id_img)
|
|
||||||
);
|
|
||||||
|
|
||||||
Create OR REPLACE Function IfUserIsAdmin() RETURNS trigger AS $$
|
|
||||||
DECLARE
|
|
||||||
|
|
||||||
BEGIN
|
|
||||||
Delete From Admin
|
|
||||||
where users = OLD.id_user;
|
|
||||||
|
|
||||||
RETURN OLD;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql ;
|
|
||||||
|
|
||||||
|
|
||||||
Create Trigger IfUserIsAdmin BEFORE DELETE on Users
|
|
||||||
FOR EACH ROW
|
|
||||||
EXECUTE FUNCTION IfUserIsAdmin();
|
|
||||||
|
|
||||||
|
|
||||||
Create OR REPLACE Function DeleteUserFavorite() RETURNS trigger AS $$
|
|
||||||
DECLARE
|
|
||||||
|
|
||||||
BEGIN
|
|
||||||
Delete From Favorite
|
|
||||||
where users = OLD.id_user;
|
|
||||||
|
|
||||||
RETURN OLD;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql ;
|
|
||||||
|
|
||||||
|
|
||||||
Create Trigger DeleteUserFavorite BEFORE DELETE on Users
|
|
||||||
FOR EACH ROW
|
|
||||||
EXECUTE FUNCTION DeleteUserFavorite();
|
|
||||||
|
|
||||||
Create OR REPLACE Function DeleteUserCommentary() RETURNS trigger AS $$
|
|
||||||
DECLARE
|
|
||||||
|
|
||||||
BEGIN
|
|
||||||
Delete From Commentary
|
|
||||||
where users = OLD.id_user;
|
|
||||||
|
|
||||||
RETURN OLD;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql ;
|
|
||||||
|
|
||||||
|
|
||||||
Create Trigger DeleteUserCommentary BEFORE DELETE on Users
|
|
||||||
FOR EACH ROW
|
|
||||||
EXECUTE FUNCTION DeleteUserCommentary();
|
|
||||||
-------------------------------------------------------------------------
|
|
||||||
|
|
||||||
CREATE TABLE Admin(
|
|
||||||
users SERIAL PRIMARY KEY,
|
|
||||||
CONSTRAINT fk_user FOREIGN KEY(users) REFERENCES Users(id_user)
|
|
||||||
);
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------
|
|
||||||
|
|
||||||
CREATE TABLE Question(
|
|
||||||
id_question SERIAL PRIMARY KEY,
|
|
||||||
texte text NOT NULL UNIQUE,
|
|
||||||
answerA varchar(30) NOT NULL,
|
|
||||||
answerB varchar(30) NOT NULL,
|
|
||||||
answerC varchar(30) NOT NULL,
|
|
||||||
answerD varchar(30) NOT NULL,
|
|
||||||
cAnswer varchar(30) NOT NULL,
|
|
||||||
CONSTRAINT check_cAnswer CHECK (cAnswer = answerA OR cAnswer = answerB OR cAnswer = answerC OR cAnswer = answerD)
|
|
||||||
);
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------
|
|
||||||
|
|
||||||
CREATE TABLE Quiz(
|
|
||||||
id_quiz SERIAL PRIMARY KEY,
|
|
||||||
title varchar(40) NOT NULL,
|
|
||||||
img NUMERIC NOT NULL,
|
|
||||||
nb_quest numeric Default 0,
|
|
||||||
CONSTRAINT fk_img FOREIGN KEY(img) REFERENCES Image(id_img)
|
|
||||||
);
|
|
||||||
|
|
||||||
Create OR REPLACE Function DeleteQuiz() RETURNS trigger AS $$
|
|
||||||
DECLARE
|
|
||||||
|
|
||||||
BEGIN
|
|
||||||
Delete From Quiz_Question
|
|
||||||
where quiz=OLD.id_quiz;
|
|
||||||
|
|
||||||
Delete From Record_quiz
|
|
||||||
where quiz=OLD.id_quiz;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql ;
|
|
||||||
|
|
||||||
Create Trigger DeleteQuiz BEFORE DELETE on Quiz
|
|
||||||
FOR EACH ROW
|
|
||||||
EXECUTE FUNCTION DeleteQuiz();
|
|
||||||
-------------------------------------------------------------------------
|
|
||||||
|
|
||||||
CREATE TABLE Quiz_Question(
|
|
||||||
quiz SERIAL NOT NULL,
|
|
||||||
question SERIAL NOT NULL,
|
|
||||||
PRIMARY KEY (quiz, question),
|
|
||||||
CONSTRAINT fk_quiz FOREIGN KEY(quiz) REFERENCES Quiz(id_quiz),
|
|
||||||
CONSTRAINT fk_question FOREIGN KEY(question) REFERENCES Question(id_question)
|
|
||||||
);
|
|
||||||
|
|
||||||
Create OR REPLACE Function NombreQuestionQuiz() RETURNS trigger AS $$
|
|
||||||
DECLARE
|
|
||||||
nb numeric;
|
|
||||||
BEGIN
|
|
||||||
|
|
||||||
IF TG_OP='DELETE' Then
|
|
||||||
SELECT count(quiz) INTO nb
|
|
||||||
FROM Quiz_Question
|
|
||||||
WHERE quiz = OLD.quiz;
|
|
||||||
Else
|
|
||||||
SELECT count(quiz) INTO nb
|
|
||||||
FROM Quiz_Question
|
|
||||||
WHERE quiz = NEW.quiz;
|
|
||||||
End IF;
|
|
||||||
|
|
||||||
|
|
||||||
Update Quiz
|
|
||||||
set nb_quest=nb
|
|
||||||
where id_quiz=NEW.quiz;
|
|
||||||
|
|
||||||
Return OLD;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql ;
|
|
||||||
|
|
||||||
Create Trigger NombreQuestionQuiz AFTER INSERT or UPDATE or DELETE on Quiz_Question
|
|
||||||
FOR EACH ROW
|
|
||||||
EXECUTE FUNCTION NombreQuestionQuiz();
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------
|
|
||||||
|
|
||||||
CREATE TABLE Record_quiz(
|
|
||||||
users SERIAL NOT NULL,
|
|
||||||
quiz SERIAL NOT NULL,
|
|
||||||
nbPoint numeric DEFAULT 0,
|
|
||||||
timeQ numeric DEFAULT 0,
|
|
||||||
PRIMARY KEY (users, quiz),
|
|
||||||
CONSTRAINT fk_user FOREIGN KEY(users) REFERENCES Users(id_user),
|
|
||||||
CONSTRAINT fk_quiz FOREIGN KEY(quiz) REFERENCES Quiz(id_quiz),
|
|
||||||
CONSTRAINT err_nbPoint CHECK(nbPoint >= 0),
|
|
||||||
CONSTRAINT err_timeQ CHECK(timeQ >= 0)
|
|
||||||
);
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------
|
|
||||||
|
|
||||||
CREATE TABLE Source(
|
|
||||||
id_source SERIAL PRIMARY KEY,
|
|
||||||
title varchar(100) NOT NULL,
|
|
||||||
dateS numeric(4) NOT NULL
|
|
||||||
);
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------
|
|
||||||
|
|
||||||
CREATE TABLE Caracter(
|
|
||||||
id_caracter SERIAL PRIMARY KEY,
|
|
||||||
caracter varchar(100) NOT NULL,
|
|
||||||
id_img NUMERIC NOT NULL
|
|
||||||
);
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------
|
|
||||||
|
|
||||||
CREATE TABLE Quote(
|
|
||||||
id_quote SERIAL PRIMARY KEY,
|
|
||||||
content text NOT NULL,
|
|
||||||
likes numeric DEFAULT '0',
|
|
||||||
langue char(2) NOT NULL,
|
|
||||||
isValide boolean NOT NULL DEFAULT 'false',
|
|
||||||
reason varchar(100) NOT NULL,
|
|
||||||
id_caracter SERIAL NOT NULL,
|
|
||||||
id_source SERIAL NOT NULL,
|
|
||||||
id_user_verif SERIAL NOT NULL,
|
|
||||||
CONSTRAINT fk_caracter FOREIGN KEY(id_caracter) REFERENCES Caracter(id_caracter),
|
|
||||||
CONSTRAINT fk_source FOREIGN KEY(id_source) REFERENCES Source(id_source),
|
|
||||||
CONSTRAINT fk_userverif FOREIGN KEY(id_user_verif) REFERENCES Users(id_user),
|
|
||||||
CONSTRAINT err_nbLike CHECK (likes >= 0),
|
|
||||||
CONSTRAINT err_language CHECK (langue = 'fr' OR langue = 'en')
|
|
||||||
);
|
|
||||||
|
|
||||||
Create OR REPLACE Function DeleteQuoteBEFORE() RETURNS trigger AS $$
|
|
||||||
DECLARE
|
|
||||||
|
|
||||||
BEGIN
|
|
||||||
Delete From Favorite
|
|
||||||
where quote=OLD.id_quote;
|
|
||||||
|
|
||||||
|
|
||||||
Delete From Commentary
|
|
||||||
where quote=OLD.id_quote;
|
|
||||||
|
|
||||||
|
|
||||||
If OLD.id_quote in (Select citation_id From DailyQuote) Then
|
|
||||||
Update DailyQuote
|
|
||||||
set citation_id = (Select id_quote
|
|
||||||
From Quote
|
|
||||||
Where id_quote!=OLD.id_quote
|
|
||||||
ORDER BY RAND()
|
|
||||||
LIMIT 1)
|
|
||||||
Where citation_id=OLD.id_quote;
|
|
||||||
END IF;
|
|
||||||
|
|
||||||
Return OLD;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql ;
|
|
||||||
|
|
||||||
Create Trigger DeleteQuoteBEFORE BEFORE DELETE on Quote
|
|
||||||
FOR EACH ROW
|
|
||||||
EXECUTE FUNCTION DeleteQuoteBEFORE();
|
|
||||||
|
|
||||||
|
|
||||||
Create OR REPLACE Function DeleteQuoteAFTER() RETURNS trigger AS $$
|
|
||||||
DECLARE
|
|
||||||
nb numeric;
|
|
||||||
BEGIN
|
|
||||||
Select count(id_caracter) into nb
|
|
||||||
from Quote
|
|
||||||
where id_caracter=OLD.id_caracter;
|
|
||||||
|
|
||||||
IF nb <= 1 Then
|
|
||||||
Delete from Caracter
|
|
||||||
where id_caracter=OLD.id_caracter;
|
|
||||||
END IF;
|
|
||||||
|
|
||||||
|
|
||||||
Select count(id_source) into nb
|
|
||||||
from Quote
|
|
||||||
where id_source=OLD.id_source;
|
|
||||||
|
|
||||||
IF nb <= 1 Then
|
|
||||||
Delete from Source
|
|
||||||
where id_source=OLD.id_source;
|
|
||||||
END IF;
|
|
||||||
|
|
||||||
Return OLD;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql ;
|
|
||||||
|
|
||||||
Create Trigger DeleteQuoteAFTER AFTER DELETE on Quote
|
|
||||||
FOR EACH ROW
|
|
||||||
EXECUTE FUNCTION DeleteQuoteAFTER();
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------
|
|
||||||
|
|
||||||
CREATE TABLE DailyQuote(
|
|
||||||
citation_id INT PRIMARY KEY,
|
|
||||||
FOREIGN KEY (citation_id) REFERENCES Quote(id_quote) ON DELETE CASCADE
|
|
||||||
);
|
|
||||||
|
|
||||||
Create OR REPLACE Function UniqueDailyQuote() RETURNS trigger AS $$
|
|
||||||
DECLARE
|
|
||||||
nb numeric;
|
|
||||||
BEGIN
|
|
||||||
Select count(*) into nb
|
|
||||||
from DailyQuote;
|
|
||||||
|
|
||||||
IF nb = 0 Then
|
|
||||||
INSERT INTO DailyQuote (citation_id)
|
|
||||||
VALUES( (Select id_quote
|
|
||||||
From Quote
|
|
||||||
Where id_quote!=OLD.id_quote
|
|
||||||
ORDER BY RAND()
|
|
||||||
LIMIT 1 ) );
|
|
||||||
|
|
||||||
ELSIF nb>1 then
|
|
||||||
|
|
||||||
DELETE From DailyQuote
|
|
||||||
where citation_id!=NEW.citation_id;
|
|
||||||
END IF;
|
|
||||||
RETURN OLD;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql ;
|
|
||||||
|
|
||||||
Create Trigger UniqueDailyQuote AFTER INSERT or DELETE on DailyQuote
|
|
||||||
FOR EACH ROW
|
|
||||||
EXECUTE FUNCTION UniqueDailyQuote();
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
CREATE TABLE Favorite(
|
|
||||||
users SERIAL NOT NULL,
|
|
||||||
quote SERIAL NOT NULL,
|
|
||||||
PRIMARY KEY (users, quote),
|
|
||||||
CONSTRAINT fk_quote FOREIGN KEY(quote) REFERENCES Quote(id_quote),
|
|
||||||
CONSTRAINT fk_user FOREIGN KEY(users) REFERENCES Users(id_user)
|
|
||||||
);
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------
|
|
||||||
|
|
||||||
CREATE TABLE Commentary(
|
|
||||||
id_comment SERIAL PRIMARY KEY,
|
|
||||||
quote SERIAL NOT NULL,
|
|
||||||
users SERIAL NOT NULL,
|
|
||||||
dateC date NOT NULL,
|
|
||||||
comment text NOT NULL,
|
|
||||||
CONSTRAINT fk_quote FOREIGN KEY(quote) REFERENCES Quote(id_quote),
|
|
||||||
CONSTRAINT fk_user FOREIGN KEY(users) REFERENCES Users(id_user)
|
|
||||||
);
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------
|
|
||||||
|
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.example.wfwebapi.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class SecurityConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||||
|
http
|
||||||
|
// Désactive CSRF
|
||||||
|
.csrf(csrf -> csrf.disable())
|
||||||
|
// Désactive la protection des frames (utile pour la console H2)
|
||||||
|
.headers(headers -> headers.frameOptions().disable())
|
||||||
|
// Autorise toutes les requêtes sans authentification
|
||||||
|
.authorizeHttpRequests(authorize -> authorize.anyRequest().permitAll());
|
||||||
|
return http.build();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.example.wfwebapi.exception;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
public ResponseEntity<String> handleAllExceptions(Exception ex) {
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred : " + ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(NullPointerException.class)
|
||||||
|
public ResponseEntity<String> handleNullPointerException(NullPointerException ex) {
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Null pointer exception caught : " + ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(ResourceNotFoundException.class)
|
||||||
|
public ResponseEntity<String> handleResourceNotFoundException(ResourceNotFoundException ex) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Resource not found : " + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.example.wfwebapi.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import com.example.wfwebapi.model.Quiz;
|
||||||
|
|
||||||
|
public interface QuizRepository extends JpaRepository<Quiz, Long> {
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
spring.application.name=WF-WEB-API
|
@ -0,0 +1,144 @@
|
|||||||
|
-- Suppression des tables (si elles existent)
|
||||||
|
DROP TABLE IF EXISTS commentary;
|
||||||
|
DROP TABLE IF EXISTS favorite;
|
||||||
|
DROP TABLE IF EXISTS daily_quote;
|
||||||
|
DROP TABLE IF EXISTS quote;
|
||||||
|
DROP TABLE IF EXISTS caracter;
|
||||||
|
DROP TABLE IF EXISTS source;
|
||||||
|
DROP TABLE IF EXISTS record_quiz;
|
||||||
|
DROP TABLE IF EXISTS quiz_question;
|
||||||
|
DROP TABLE IF EXISTS quiz;
|
||||||
|
DROP TABLE IF EXISTS question;
|
||||||
|
DROP TABLE IF EXISTS admin;
|
||||||
|
DROP TABLE IF EXISTS users;
|
||||||
|
DROP TABLE IF EXISTS image;
|
||||||
|
|
||||||
|
-- Création des tables
|
||||||
|
|
||||||
|
-- Table image (l'id est auto-incrémenté)
|
||||||
|
CREATE TABLE image (
|
||||||
|
id_img INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||||
|
img_path VARCHAR(300) NOT NULL UNIQUE
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Table users (l'id est auto-incrémenté)
|
||||||
|
CREATE TABLE users (
|
||||||
|
id_user INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||||
|
username VARCHAR(50) NOT NULL,
|
||||||
|
email VARCHAR(50) NOT NULL,
|
||||||
|
password VARCHAR(100) NOT NULL,
|
||||||
|
img INTEGER NOT NULL,
|
||||||
|
creation DATE NOT NULL,
|
||||||
|
CONSTRAINT unique_col UNIQUE (email),
|
||||||
|
CONSTRAINT fk_users_img FOREIGN KEY (img) REFERENCES image(id_img)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Table admin
|
||||||
|
-- La colonne 'users' doit contenir l'id d'un utilisateur existant (pas auto-incrémenté ici)
|
||||||
|
CREATE TABLE admin (
|
||||||
|
users INTEGER PRIMARY KEY,
|
||||||
|
CONSTRAINT fk_admin_user FOREIGN KEY (users) REFERENCES users(id_user)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Table question (l'id est auto-incrémenté)
|
||||||
|
CREATE TABLE question (
|
||||||
|
id_question INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||||
|
texte TEXT NOT NULL UNIQUE,
|
||||||
|
answer_a VARCHAR(30) NOT NULL,
|
||||||
|
answer_b VARCHAR(30) NOT NULL,
|
||||||
|
answer_c VARCHAR(30) NOT NULL,
|
||||||
|
answer_d VARCHAR(30) NOT NULL,
|
||||||
|
c_answer VARCHAR(30) NOT NULL,
|
||||||
|
CONSTRAINT check_c_answer CHECK (c_answer = answer_a OR c_answer = answer_b OR c_answer = answer_c OR c_answer = answer_d)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Table quiz (l'id est auto-incrémenté)
|
||||||
|
CREATE TABLE quiz (
|
||||||
|
id_quiz INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||||
|
title VARCHAR(40) NOT NULL,
|
||||||
|
img INTEGER NOT NULL,
|
||||||
|
nb_quest NUMERIC DEFAULT 0,
|
||||||
|
CONSTRAINT fk_quiz_img FOREIGN KEY (img) REFERENCES image(id_img)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Table quiz_question
|
||||||
|
-- Les colonnes ici sont des clés étrangères et doivent être de type INTEGER (les valeurs proviennent des tables quiz et question)
|
||||||
|
CREATE TABLE quiz_question (
|
||||||
|
quiz INTEGER NOT NULL,
|
||||||
|
question INTEGER NOT NULL,
|
||||||
|
PRIMARY KEY (quiz, question),
|
||||||
|
CONSTRAINT fk_quiz_question_quiz FOREIGN KEY (quiz) REFERENCES quiz(id_quiz),
|
||||||
|
CONSTRAINT fk_quiz_question_question FOREIGN KEY (question) REFERENCES question(id_question)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Table record_quiz
|
||||||
|
CREATE TABLE record_quiz (
|
||||||
|
users INTEGER NOT NULL,
|
||||||
|
quiz INTEGER NOT NULL,
|
||||||
|
nb_point NUMERIC DEFAULT 0,
|
||||||
|
time_q NUMERIC DEFAULT 0,
|
||||||
|
PRIMARY KEY (users, quiz),
|
||||||
|
CONSTRAINT fk_recordquiz_user FOREIGN KEY (users) REFERENCES users(id_user),
|
||||||
|
CONSTRAINT fk_recordquiz_quiz FOREIGN KEY (quiz) REFERENCES quiz(id_quiz),
|
||||||
|
CONSTRAINT err_nb_point CHECK (nb_point >= 0),
|
||||||
|
CONSTRAINT err_time_q CHECK (time_q >= 0)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Table source (l'id est auto-incrémenté)
|
||||||
|
CREATE TABLE source (
|
||||||
|
id_source INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||||
|
title VARCHAR(100) NOT NULL,
|
||||||
|
date_s NUMERIC(4) NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Table caracter (l'id est auto-incrémenté)
|
||||||
|
CREATE TABLE caracter (
|
||||||
|
id_caracter INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||||
|
caracter VARCHAR(100) NOT NULL,
|
||||||
|
id_img INTEGER NOT NULL,
|
||||||
|
CONSTRAINT fk_caracter_image FOREIGN KEY (id_img) REFERENCES image(id_img)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Table quote (l'id est auto-incrémenté)
|
||||||
|
CREATE TABLE quote (
|
||||||
|
id_quote INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||||
|
content TEXT NOT NULL,
|
||||||
|
likes NUMERIC DEFAULT 0,
|
||||||
|
langue CHAR(2) NOT NULL,
|
||||||
|
is_valide BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
|
reason VARCHAR(100) NOT NULL,
|
||||||
|
id_caracter INTEGER NOT NULL,
|
||||||
|
id_source INTEGER NOT NULL,
|
||||||
|
id_user_verif INTEGER NOT NULL,
|
||||||
|
CONSTRAINT fk_quote_caracter FOREIGN KEY (id_caracter) REFERENCES caracter(id_caracter),
|
||||||
|
CONSTRAINT fk_quote_source FOREIGN KEY (id_source) REFERENCES source(id_source),
|
||||||
|
CONSTRAINT fk_quote_userverif FOREIGN KEY (id_user_verif) REFERENCES users(id_user),
|
||||||
|
CONSTRAINT err_nb_like CHECK (likes >= 0),
|
||||||
|
CONSTRAINT err_language CHECK (langue = 'fr' OR langue = 'en')
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Table daily_quote
|
||||||
|
CREATE TABLE daily_quote (
|
||||||
|
citation_id INTEGER PRIMARY KEY,
|
||||||
|
FOREIGN KEY (citation_id) REFERENCES quote(id_quote) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Table favorite
|
||||||
|
CREATE TABLE favorite (
|
||||||
|
users INTEGER NOT NULL,
|
||||||
|
quote INTEGER NOT NULL,
|
||||||
|
PRIMARY KEY (users, quote),
|
||||||
|
CONSTRAINT fk_favorite_quote FOREIGN KEY (quote) REFERENCES quote(id_quote),
|
||||||
|
CONSTRAINT fk_favorite_user FOREIGN KEY (users) REFERENCES users(id_user)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Table commentary (l'id est auto-incrémenté)
|
||||||
|
CREATE TABLE commentary (
|
||||||
|
id_comment INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||||
|
quote INTEGER NOT NULL,
|
||||||
|
users INTEGER NOT NULL,
|
||||||
|
date_c DATE NOT NULL,
|
||||||
|
comment TEXT NOT NULL,
|
||||||
|
CONSTRAINT fk_commentary_quote FOREIGN KEY (quote) REFERENCES quote(id_quote),
|
||||||
|
CONSTRAINT fk_commentary_user FOREIGN KEY (users) REFERENCES users(id_user)
|
||||||
|
);
|
@ -0,0 +1,54 @@
|
|||||||
|
package com.example.wfwebapi.assembler;
|
||||||
|
|
||||||
|
import com.example.wfwebapi.controller.QuoteController;
|
||||||
|
import com.example.wfwebapi.model.Quote;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.hateoas.EntityModel;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
|
||||||
|
|
||||||
|
class QuoteModelAssemblerTest {
|
||||||
|
|
||||||
|
private QuoteModelAssembler assembler;
|
||||||
|
private Quote quote;
|
||||||
|
private EntityModel<Quote> model;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
assembler = new QuoteModelAssembler();
|
||||||
|
quote = new Quote();
|
||||||
|
quote.setId(1L);
|
||||||
|
model = assembler.toModel(quote);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testModelContent() {
|
||||||
|
assertEquals(quote, model.getContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testHasSelfLink() {
|
||||||
|
assertTrue(model.getLinks().hasLink("self"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testHasQuotesLink() {
|
||||||
|
assertTrue(model.getLinks().hasLink("quotes"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSelfLinkHref() {
|
||||||
|
String expectedHref = linkTo(methodOn(QuoteController.class).getQuoteById(1L)).withSelfRel().getHref();
|
||||||
|
String actualHref = model.getRequiredLink("self").getHref();
|
||||||
|
assertEquals(expectedHref, actualHref);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testQuotesLinkHref() {
|
||||||
|
String expectedHref = linkTo(methodOn(QuoteController.class).getAllQuotes(0, 10)).withRel("quotes").getHref();
|
||||||
|
String actualHref = model.getRequiredLink("quotes").getHref();
|
||||||
|
assertEquals(expectedHref, actualHref);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
package com.example.wfwebapi.controller;
|
||||||
|
|
||||||
|
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||||
|
import com.example.wfwebapi.model.Admin;
|
||||||
|
import com.example.wfwebapi.repository.AdminRepository;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
class AdminControllerTest {
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AdminRepository adminRepository;
|
||||||
|
|
||||||
|
private String baseUrl() {
|
||||||
|
return "http://localhost:" + port + "/api/v1/admin/";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetAdminFound() {
|
||||||
|
Admin admin = new Admin();
|
||||||
|
admin.setUserId(999L);
|
||||||
|
adminRepository.save(admin);
|
||||||
|
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
ResponseEntity<Admin> response = restTemplate.getForEntity(baseUrl() + "999", Admin.class);
|
||||||
|
|
||||||
|
assertEquals(200, response.getStatusCodeValue());
|
||||||
|
assertEquals(999L, response.getBody().getUserId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetAdminNotFound() {
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
|
||||||
|
Exception exception = assertThrows(Exception.class, () ->
|
||||||
|
restTemplate.getForEntity(baseUrl() + "123456", Admin.class)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertTrue(exception.getMessage().contains("404"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,111 @@
|
|||||||
|
package com.example.wfwebapi.controller;
|
||||||
|
|
||||||
|
import com.example.wfwebapi.model.Image;
|
||||||
|
import com.example.wfwebapi.model.User;
|
||||||
|
import com.example.wfwebapi.repository.ImageRepository;
|
||||||
|
import com.example.wfwebapi.repository.UserRepository;
|
||||||
|
import com.example.wfwebapi.security.JwtTokenProvider;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
|
import org.springframework.http.*;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
class AuthControllerTest {
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AuthController authController;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ImageRepository imageRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JwtTokenProvider tokenProvider;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TestRestTemplate restTemplate;
|
||||||
|
|
||||||
|
private String getUrl(String path) {
|
||||||
|
return "http://localhost:" + port + "/api/v1/auth" + path;
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
userRepository.deleteAll();
|
||||||
|
imageRepository.deleteAll();
|
||||||
|
|
||||||
|
Image image = new Image();
|
||||||
|
image.setImgPath("fake/path.png");
|
||||||
|
image = imageRepository.save(image);
|
||||||
|
|
||||||
|
User user = new User();
|
||||||
|
user.setUsername("john");
|
||||||
|
user.setPassword("password123");
|
||||||
|
user.setEmail("john@example.com");
|
||||||
|
user.setCreation(java.time.LocalDate.now());
|
||||||
|
user.setImage(image);
|
||||||
|
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void contextLoads() {
|
||||||
|
assertNotNull(authController);
|
||||||
|
assertNotNull(userRepository);
|
||||||
|
assertNotNull(tokenProvider);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testLoginSuccess() {
|
||||||
|
AuthController.AuthRequest request = new AuthController.AuthRequest();
|
||||||
|
request.setUsername("john");
|
||||||
|
request.setPassword("password123");
|
||||||
|
|
||||||
|
ResponseEntity<AuthController.AuthResponse> response = restTemplate.postForEntity(
|
||||||
|
getUrl("/login"), request, AuthController.AuthResponse.class
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
assertNotNull(response.getBody());
|
||||||
|
assertNotNull(response.getBody().getToken());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testLoginInvalidPassword() {
|
||||||
|
AuthController.AuthRequest request = new AuthController.AuthRequest();
|
||||||
|
request.setUsername("john");
|
||||||
|
request.setPassword("wrongpass");
|
||||||
|
|
||||||
|
ResponseEntity<String> response = restTemplate.postForEntity(
|
||||||
|
getUrl("/login"), request, String.class
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
|
||||||
|
assertEquals("Mot de passe invalide", response.getBody());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testLoginUserNotFound() {
|
||||||
|
AuthController.AuthRequest request = new AuthController.AuthRequest();
|
||||||
|
request.setUsername("unknown");
|
||||||
|
request.setPassword("any");
|
||||||
|
|
||||||
|
ResponseEntity<String> response = restTemplate.postForEntity(
|
||||||
|
getUrl("/login"), request, String.class
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
|
||||||
|
assertTrue(response.getBody().contains("Utilisateur non trouvé"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,118 @@
|
|||||||
|
package com.example.wfwebapi.controller;
|
||||||
|
|
||||||
|
import com.example.wfwebapi.model.Caracter;
|
||||||
|
import com.example.wfwebapi.model.Image;
|
||||||
|
import com.example.wfwebapi.repository.CaracterRepository;
|
||||||
|
import com.example.wfwebapi.repository.ImageRepository;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
|
import org.springframework.http.*;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
class CaracterControllerTest {
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TestRestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CaracterRepository caracterRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ImageRepository imageRepository;
|
||||||
|
|
||||||
|
private Image image;
|
||||||
|
|
||||||
|
private String getUrl(String path) {
|
||||||
|
return "http://localhost:" + port + "/api/v1/caracter" + path;
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
caracterRepository.deleteAll();
|
||||||
|
imageRepository.deleteAll();
|
||||||
|
|
||||||
|
image = new Image();
|
||||||
|
image.setImgPath("path/to/image.png");
|
||||||
|
imageRepository.save(image);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCreateAndGetCaracterById() {
|
||||||
|
Caracter caracter = new Caracter();
|
||||||
|
caracter.setCaracter("Déterminé");
|
||||||
|
caracter.setImage(image);
|
||||||
|
|
||||||
|
ResponseEntity<Caracter> postResponse = restTemplate.postForEntity(getUrl(""), caracter, Caracter.class);
|
||||||
|
assertEquals(HttpStatus.OK, postResponse.getStatusCode());
|
||||||
|
Long id = Objects.requireNonNull(postResponse.getBody()).getId();
|
||||||
|
|
||||||
|
ResponseEntity<Caracter> getResponse = restTemplate.getForEntity(getUrl("/" + id), Caracter.class);
|
||||||
|
assertEquals(HttpStatus.OK, getResponse.getStatusCode());
|
||||||
|
assertEquals("Déterminé", getResponse.getBody().getCaracter());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetAllCaracters() {
|
||||||
|
Caracter c = new Caracter();
|
||||||
|
c.setCaracter("Créatif");
|
||||||
|
c.setImage(image);
|
||||||
|
caracterRepository.save(c);
|
||||||
|
|
||||||
|
ResponseEntity<Caracter[]> response = restTemplate.getForEntity(getUrl("/"), Caracter[].class);
|
||||||
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
assertTrue(response.getBody().length >= 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateCaracter() {
|
||||||
|
Caracter c = new Caracter();
|
||||||
|
c.setCaracter("Patient");
|
||||||
|
c.setImage(image);
|
||||||
|
c = caracterRepository.save(c);
|
||||||
|
|
||||||
|
c.setCaracter("Impulsif");
|
||||||
|
|
||||||
|
HttpEntity<Caracter> request = new HttpEntity<>(c);
|
||||||
|
ResponseEntity<Caracter> response = restTemplate.exchange(getUrl(""), HttpMethod.PUT, request, Caracter.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
assertEquals("Impulsif", response.getBody().getCaracter());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteCaracter() {
|
||||||
|
Caracter c = new Caracter();
|
||||||
|
c.setCaracter("Sérieux");
|
||||||
|
c.setImage(image);
|
||||||
|
c = caracterRepository.save(c);
|
||||||
|
|
||||||
|
ResponseEntity<Void> response = restTemplate.exchange(getUrl("?id=" + c.getId()), HttpMethod.DELETE, null, Void.class);
|
||||||
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
assertFalse(caracterRepository.existsById(c.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetCaracterNotFound() {
|
||||||
|
ResponseEntity<String> response = restTemplate.getForEntity(getUrl("/999999"), String.class);
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
assertTrue(response.getBody().contains("Caracter non trouvé"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteCaracterNotFound() {
|
||||||
|
ResponseEntity<String> response = restTemplate.exchange(getUrl("?id=999999"), HttpMethod.DELETE, null, String.class);
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
assertTrue(response.getBody().contains("Caracter non trouvé"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,138 @@
|
|||||||
|
package com.example.wfwebapi.controller;
|
||||||
|
|
||||||
|
import com.example.wfwebapi.model.*;
|
||||||
|
import com.example.wfwebapi.repository.*;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
|
import org.springframework.http.*;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
class CommentaryControllerTest {
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Autowired private TestRestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Autowired private CommentaryRepository commentaryRepository;
|
||||||
|
@Autowired private QuoteRepository quoteRepository;
|
||||||
|
@Autowired private UserRepository userRepository;
|
||||||
|
@Autowired private ImageRepository imageRepository;
|
||||||
|
@Autowired private CaracterRepository caracterRepository;
|
||||||
|
@Autowired private SourceRepository sourceRepository;
|
||||||
|
|
||||||
|
private Quote quote;
|
||||||
|
private User user;
|
||||||
|
|
||||||
|
private String getUrl(String path) {
|
||||||
|
return "http://localhost:" + port + "/api/v1/commentary" + path;
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
clearDatabase();
|
||||||
|
|
||||||
|
Image image = new Image();
|
||||||
|
image.setImgPath("test.png");
|
||||||
|
imageRepository.save(image);
|
||||||
|
|
||||||
|
user = new User();
|
||||||
|
user.setUsername("user");
|
||||||
|
user.setEmail("user@mail.com");
|
||||||
|
user.setPassword("pass");
|
||||||
|
user.setCreation(LocalDate.now());
|
||||||
|
user.setImage(image);
|
||||||
|
userRepository.save(user);
|
||||||
|
|
||||||
|
Caracter caracter = new Caracter();
|
||||||
|
caracter.setCaracter("Fort");
|
||||||
|
caracter.setImage(image);
|
||||||
|
caracterRepository.save(caracter);
|
||||||
|
|
||||||
|
Source source = new Source();
|
||||||
|
source.setTitle("source");
|
||||||
|
source.setDateS(2000);
|
||||||
|
sourceRepository.save(source);
|
||||||
|
|
||||||
|
quote = new Quote();
|
||||||
|
quote.setContent("Ceci est une citation.");
|
||||||
|
quote.setLangue("fr");
|
||||||
|
quote.setIsValide(true);
|
||||||
|
quote.setLikes(0);
|
||||||
|
quote.setReason("valid");
|
||||||
|
quote.setCaracter(caracter);
|
||||||
|
quote.setSource(source);
|
||||||
|
quote.setUserVerif(user);
|
||||||
|
quoteRepository.save(quote);
|
||||||
|
}
|
||||||
|
|
||||||
|
void clearDatabase() {
|
||||||
|
commentaryRepository.deleteAll();
|
||||||
|
quoteRepository.deleteAll();
|
||||||
|
caracterRepository.deleteAll();
|
||||||
|
sourceRepository.deleteAll();
|
||||||
|
userRepository.deleteAll();
|
||||||
|
imageRepository.deleteAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAddAndGetCommentary() {
|
||||||
|
Commentary commentary = new Commentary();
|
||||||
|
commentary.setUser(user);
|
||||||
|
commentary.setComment("Super citation !");
|
||||||
|
commentary.setDateC(LocalDate.now());
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
|
|
||||||
|
HttpEntity<Commentary> request = new HttpEntity<>(commentary, headers);
|
||||||
|
|
||||||
|
ResponseEntity<Commentary> postResponse = restTemplate.postForEntity(
|
||||||
|
getUrl("?quote=" + quote.getId()), request, Commentary.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, postResponse.getStatusCode());
|
||||||
|
assertNotNull(postResponse.getBody().getId());
|
||||||
|
|
||||||
|
ResponseEntity<Commentary[]> getResponse = restTemplate.getForEntity(
|
||||||
|
getUrl("/" + quote.getId()), Commentary[].class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, getResponse.getStatusCode());
|
||||||
|
assertEquals(1, getResponse.getBody().length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteCommentary() {
|
||||||
|
Commentary commentary = new Commentary();
|
||||||
|
commentary.setUser(user);
|
||||||
|
commentary.setQuote(quote);
|
||||||
|
commentary.setComment("À supprimer");
|
||||||
|
commentary.setDateC(LocalDate.now());
|
||||||
|
commentaryRepository.save(commentary);
|
||||||
|
|
||||||
|
ResponseEntity<Void> deleteResponse = restTemplate.exchange(
|
||||||
|
getUrl("?id=" + quote.getId()), HttpMethod.DELETE, null, Void.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, deleteResponse.getStatusCode());
|
||||||
|
|
||||||
|
List<Commentary> remaining = commentaryRepository.findByQuote_Id(quote.getId());
|
||||||
|
assertTrue(remaining.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteCommentaryNotFound() {
|
||||||
|
ResponseEntity<String> response = restTemplate.exchange(
|
||||||
|
getUrl("?id=99999"), HttpMethod.DELETE, null, String.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
assertTrue(response.getBody().contains("Aucun commentaire trouvé"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,103 @@
|
|||||||
|
package com.example.wfwebapi.controller;
|
||||||
|
|
||||||
|
import com.example.wfwebapi.model.*;
|
||||||
|
import com.example.wfwebapi.repository.*;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
|
import org.springframework.http.*;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
class DailyQuoteControllerTest {
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Autowired private TestRestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Autowired private DailyQuoteRepository dailyQuoteRepository;
|
||||||
|
@Autowired private QuoteRepository quoteRepository;
|
||||||
|
@Autowired private UserRepository userRepository;
|
||||||
|
@Autowired private ImageRepository imageRepository;
|
||||||
|
@Autowired private CaracterRepository caracterRepository;
|
||||||
|
@Autowired private SourceRepository sourceRepository;
|
||||||
|
|
||||||
|
private Quote quote;
|
||||||
|
|
||||||
|
private String getUrl() {
|
||||||
|
return "http://localhost:" + port + "/api/v1/dailyquote";
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
dailyQuoteRepository.deleteAll();
|
||||||
|
quoteRepository.deleteAll();
|
||||||
|
caracterRepository.deleteAll();
|
||||||
|
sourceRepository.deleteAll();
|
||||||
|
userRepository.deleteAll();
|
||||||
|
imageRepository.deleteAll();
|
||||||
|
|
||||||
|
Image image = new Image();
|
||||||
|
image.setImgPath("daily.png");
|
||||||
|
imageRepository.save(image);
|
||||||
|
|
||||||
|
User user = new User();
|
||||||
|
user.setUsername("dailyUser");
|
||||||
|
user.setPassword("pwd");
|
||||||
|
user.setEmail("daily@mail.com");
|
||||||
|
user.setCreation(LocalDate.now());
|
||||||
|
user.setImage(image);
|
||||||
|
userRepository.save(user);
|
||||||
|
|
||||||
|
Caracter caracter = new Caracter();
|
||||||
|
caracter.setCaracter("Calme");
|
||||||
|
caracter.setImage(image);
|
||||||
|
caracterRepository.save(caracter);
|
||||||
|
|
||||||
|
Source source = new Source();
|
||||||
|
source.setTitle("Livre A");
|
||||||
|
source.setDateS(2001);
|
||||||
|
sourceRepository.save(source);
|
||||||
|
|
||||||
|
quote = new Quote();
|
||||||
|
quote.setContent("Chaque jour est un cadeau.");
|
||||||
|
quote.setLangue("fr");
|
||||||
|
quote.setIsValide(true);
|
||||||
|
quote.setLikes(10);
|
||||||
|
quote.setReason("approuvée");
|
||||||
|
quote.setUserVerif(user);
|
||||||
|
quote.setCaracter(caracter);
|
||||||
|
quote.setSource(source);
|
||||||
|
quoteRepository.save(quote);
|
||||||
|
|
||||||
|
DailyQuote dailyQuote = new DailyQuote();
|
||||||
|
dailyQuote.setCitationId(quote.getId()); // CORRECT : ne pas faire setQuote()
|
||||||
|
dailyQuoteRepository.save(dailyQuote);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetDailyQuote() {
|
||||||
|
ResponseEntity<DailyQuote> response = restTemplate.getForEntity(getUrl(), DailyQuote.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
assertNotNull(response.getBody());
|
||||||
|
assertEquals(quote.getId(), response.getBody().getCitationId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetDailyQuoteNotFound() {
|
||||||
|
dailyQuoteRepository.deleteAll();
|
||||||
|
|
||||||
|
ResponseEntity<String> response = restTemplate.getForEntity(getUrl(), String.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
assertTrue(response.getBody().contains("Aucune DailyQuote définie"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,133 @@
|
|||||||
|
package com.example.wfwebapi.controller;
|
||||||
|
|
||||||
|
import com.example.wfwebapi.model.*;
|
||||||
|
import com.example.wfwebapi.repository.*;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
|
import org.springframework.http.*;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
class FavoriteControllerTest {
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Autowired private TestRestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Autowired private FavoriteRepository favoriteRepository;
|
||||||
|
@Autowired private QuoteRepository quoteRepository;
|
||||||
|
@Autowired private UserRepository userRepository;
|
||||||
|
@Autowired private ImageRepository imageRepository;
|
||||||
|
@Autowired private CaracterRepository caracterRepository;
|
||||||
|
@Autowired private SourceRepository sourceRepository;
|
||||||
|
|
||||||
|
private User user;
|
||||||
|
private Quote quote;
|
||||||
|
|
||||||
|
private String getUrl(String path) {
|
||||||
|
return "http://localhost:" + port + "/api/v1/favorite" + path;
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
favoriteRepository.deleteAll();
|
||||||
|
quoteRepository.deleteAll();
|
||||||
|
caracterRepository.deleteAll();
|
||||||
|
sourceRepository.deleteAll();
|
||||||
|
userRepository.deleteAll();
|
||||||
|
imageRepository.deleteAll();
|
||||||
|
|
||||||
|
Image image = new Image();
|
||||||
|
image.setImgPath("fav.png");
|
||||||
|
imageRepository.save(image);
|
||||||
|
|
||||||
|
user = new User();
|
||||||
|
user.setUsername("testuser");
|
||||||
|
user.setPassword("secret");
|
||||||
|
user.setEmail("test@ex.com");
|
||||||
|
user.setCreation(LocalDate.now());
|
||||||
|
user.setImage(image);
|
||||||
|
userRepository.save(user);
|
||||||
|
|
||||||
|
Caracter caracter = new Caracter();
|
||||||
|
caracter.setCaracter("Sérieux");
|
||||||
|
caracter.setImage(image);
|
||||||
|
caracterRepository.save(caracter);
|
||||||
|
|
||||||
|
Source source = new Source();
|
||||||
|
source.setTitle("Source X");
|
||||||
|
source.setDateS(1999);
|
||||||
|
sourceRepository.save(source);
|
||||||
|
|
||||||
|
quote = new Quote();
|
||||||
|
quote.setContent("La connaissance est le pouvoir.");
|
||||||
|
quote.setLangue("fr");
|
||||||
|
quote.setIsValide(true);
|
||||||
|
quote.setLikes(100);
|
||||||
|
quote.setReason("Validée");
|
||||||
|
quote.setCaracter(caracter);
|
||||||
|
quote.setSource(source);
|
||||||
|
quote.setUserVerif(user);
|
||||||
|
quoteRepository.save(quote);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAddAndGetFavorite() {
|
||||||
|
ResponseEntity<Void> postResponse = restTemplate.postForEntity(
|
||||||
|
getUrl("?user=" + user.getId() + ""e=" + quote.getId()),
|
||||||
|
null,
|
||||||
|
Void.class
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, postResponse.getStatusCode());
|
||||||
|
|
||||||
|
ResponseEntity<Favorite[]> getResponse = restTemplate.getForEntity(
|
||||||
|
getUrl("/" + user.getId()),
|
||||||
|
Favorite[].class
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, getResponse.getStatusCode());
|
||||||
|
assertEquals(1, getResponse.getBody().length);
|
||||||
|
assertEquals(quote.getId(), getResponse.getBody()[0].getQuote().getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteFavorite() {
|
||||||
|
Favorite favorite = new Favorite();
|
||||||
|
favorite.setId(new FavoriteId(user.getId(), quote.getId()));
|
||||||
|
favorite.setUser(user);
|
||||||
|
favorite.setQuote(quote);
|
||||||
|
favoriteRepository.save(favorite);
|
||||||
|
|
||||||
|
ResponseEntity<Void> response = restTemplate.exchange(
|
||||||
|
getUrl("?user=" + user.getId() + ""e=" + quote.getId()),
|
||||||
|
HttpMethod.DELETE,
|
||||||
|
null,
|
||||||
|
Void.class
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
assertFalse(favoriteRepository.existsById(new FavoriteId(user.getId(), quote.getId())));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteFavoriteNotFound() {
|
||||||
|
ResponseEntity<String> response = restTemplate.exchange(
|
||||||
|
getUrl("?user=9999"e=9999"),
|
||||||
|
HttpMethod.DELETE,
|
||||||
|
null,
|
||||||
|
String.class
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
assertTrue(response.getBody().contains("Favorite non trouvée"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,102 @@
|
|||||||
|
package com.example.wfwebapi.controller;
|
||||||
|
|
||||||
|
import com.example.wfwebapi.model.Image;
|
||||||
|
import com.example.wfwebapi.repository.ImageRepository;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
|
import org.springframework.http.*;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
class ImageControllerTest {
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Autowired private TestRestTemplate restTemplate;
|
||||||
|
@Autowired private ImageRepository imageRepository;
|
||||||
|
|
||||||
|
private String getUrl(String path) {
|
||||||
|
return "http://localhost:" + port + "/api/v1/image" + path;
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
imageRepository.deleteAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCreateAndGetImage() {
|
||||||
|
Image image = new Image();
|
||||||
|
image.setImgPath("assets/img/test.png");
|
||||||
|
|
||||||
|
ResponseEntity<Image> postResponse = restTemplate.postForEntity(
|
||||||
|
getUrl(""), image, Image.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, postResponse.getStatusCode());
|
||||||
|
Image created = postResponse.getBody();
|
||||||
|
assertNotNull(created);
|
||||||
|
assertNotNull(created.getId());
|
||||||
|
|
||||||
|
ResponseEntity<Image> getResponse = restTemplate.getForEntity(
|
||||||
|
getUrl("/" + created.getId()), Image.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, getResponse.getStatusCode());
|
||||||
|
assertEquals("assets/img/test.png", getResponse.getBody().getImgPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateImage() {
|
||||||
|
Image image = new Image();
|
||||||
|
image.setImgPath("original.png");
|
||||||
|
image = imageRepository.save(image);
|
||||||
|
|
||||||
|
image.setImgPath("updated.png");
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
|
HttpEntity<Image> entity = new HttpEntity<>(image, headers);
|
||||||
|
|
||||||
|
ResponseEntity<Image> putResponse = restTemplate.exchange(
|
||||||
|
getUrl(""), HttpMethod.PUT, entity, Image.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, putResponse.getStatusCode());
|
||||||
|
assertEquals("updated.png", putResponse.getBody().getImgPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteImage() {
|
||||||
|
Image image = new Image();
|
||||||
|
image.setImgPath("todelete.png");
|
||||||
|
image = imageRepository.save(image);
|
||||||
|
|
||||||
|
ResponseEntity<Void> deleteResponse = restTemplate.exchange(
|
||||||
|
getUrl("?id=" + image.getId()), HttpMethod.DELETE, null, Void.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, deleteResponse.getStatusCode());
|
||||||
|
assertFalse(imageRepository.existsById(image.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetImageNotFound() {
|
||||||
|
ResponseEntity<String> response = restTemplate.getForEntity(
|
||||||
|
getUrl("/9999"), String.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
assertTrue(response.getBody().contains("Image non trouvée"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteImageNotFound() {
|
||||||
|
ResponseEntity<String> response = restTemplate.exchange(
|
||||||
|
getUrl("?id=9999"), HttpMethod.DELETE, null, String.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
assertTrue(response.getBody().contains("Image non trouvée"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,112 @@
|
|||||||
|
package com.example.wfwebapi.controller;
|
||||||
|
|
||||||
|
import com.example.wfwebapi.model.Question;
|
||||||
|
import com.example.wfwebapi.repository.QuestionRepository;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
|
import org.springframework.http.*;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
class QuestionControllerTest {
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Autowired private TestRestTemplate restTemplate;
|
||||||
|
@Autowired private QuestionRepository questionRepository;
|
||||||
|
|
||||||
|
private String getUrl(String path) {
|
||||||
|
return "http://localhost:" + port + "/api/v1/question" + path;
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
questionRepository.deleteAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCreateAndGetQuestion() {
|
||||||
|
Question question = new Question();
|
||||||
|
question.setTexte("Quel est le langage de ce test ?");
|
||||||
|
question.setAnswerA("Python");
|
||||||
|
question.setAnswerB("Java");
|
||||||
|
question.setAnswerC("C++");
|
||||||
|
question.setAnswerD("JavaScript");
|
||||||
|
question.setCAnswer("Java");
|
||||||
|
|
||||||
|
ResponseEntity<Question> postResponse = restTemplate.postForEntity(getUrl(""), question, Question.class);
|
||||||
|
assertEquals(HttpStatus.OK, postResponse.getStatusCode());
|
||||||
|
assertNotNull(postResponse.getBody());
|
||||||
|
Long id = postResponse.getBody().getId();
|
||||||
|
assertNotNull(id);
|
||||||
|
|
||||||
|
ResponseEntity<Question> getResponse = restTemplate.getForEntity(getUrl("/" + id), Question.class);
|
||||||
|
assertEquals(HttpStatus.OK, getResponse.getStatusCode());
|
||||||
|
assertEquals("Java", getResponse.getBody().getCAnswer());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateQuestion() {
|
||||||
|
Question q = new Question();
|
||||||
|
q.setTexte("Old Q");
|
||||||
|
q.setAnswerA("A");
|
||||||
|
q.setAnswerB("B");
|
||||||
|
q.setAnswerC("C");
|
||||||
|
q.setAnswerD("D");
|
||||||
|
q.setCAnswer("B");
|
||||||
|
q = questionRepository.save(q);
|
||||||
|
|
||||||
|
q.setTexte("Updated Q");
|
||||||
|
q.setCAnswer("A");
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
|
HttpEntity<Question> entity = new HttpEntity<>(q, headers);
|
||||||
|
|
||||||
|
ResponseEntity<Question> response = restTemplate.exchange(
|
||||||
|
getUrl(""), HttpMethod.PUT, entity, Question.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
assertEquals("Updated Q", response.getBody().getTexte());
|
||||||
|
assertEquals("A", response.getBody().getCAnswer());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteQuestion() {
|
||||||
|
Question q = new Question();
|
||||||
|
q.setTexte("À supprimer");
|
||||||
|
q.setAnswerA("1");
|
||||||
|
q.setAnswerB("2");
|
||||||
|
q.setAnswerC("3");
|
||||||
|
q.setAnswerD("4");
|
||||||
|
q.setCAnswer("2");
|
||||||
|
q = questionRepository.save(q);
|
||||||
|
|
||||||
|
ResponseEntity<Void> delete = restTemplate.exchange(
|
||||||
|
getUrl("?id=" + q.getId()), HttpMethod.DELETE, null, Void.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, delete.getStatusCode());
|
||||||
|
assertFalse(questionRepository.existsById(q.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetQuestionNotFound() {
|
||||||
|
ResponseEntity<String> response = restTemplate.getForEntity(getUrl("/9999"), String.class);
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
assertTrue(response.getBody().contains("Question non trouvée"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteQuestionNotFound() {
|
||||||
|
ResponseEntity<String> response = restTemplate.exchange(
|
||||||
|
getUrl("?id=9999"), HttpMethod.DELETE, null, String.class);
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
assertTrue(response.getBody().contains("Question non trouvée"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,130 @@
|
|||||||
|
package com.example.wfwebapi.controller;
|
||||||
|
|
||||||
|
import com.example.wfwebapi.model.Image;
|
||||||
|
import com.example.wfwebapi.model.Quiz;
|
||||||
|
import com.example.wfwebapi.repository.ImageRepository;
|
||||||
|
import com.example.wfwebapi.repository.QuizRepository;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
|
import org.springframework.http.*;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
class QuizControllerTest {
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Autowired private TestRestTemplate restTemplate;
|
||||||
|
@Autowired private QuizRepository quizRepository;
|
||||||
|
@Autowired private ImageRepository imageRepository;
|
||||||
|
|
||||||
|
private String getUrl(String path) {
|
||||||
|
return "http://localhost:" + port + "/api/v1/quiz" + path;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Image image;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
quizRepository.deleteAll();
|
||||||
|
imageRepository.deleteAll();
|
||||||
|
|
||||||
|
image = new Image();
|
||||||
|
image.setImgPath("quiz/image.png");
|
||||||
|
imageRepository.save(image);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCreateAndGetQuiz() {
|
||||||
|
Quiz quiz = new Quiz();
|
||||||
|
quiz.setTitle("Histoire");
|
||||||
|
quiz.setImage(image);
|
||||||
|
quiz.setNbQuest(5);
|
||||||
|
|
||||||
|
ResponseEntity<Quiz> postResponse = restTemplate.postForEntity(getUrl(""), quiz, Quiz.class);
|
||||||
|
assertEquals(HttpStatus.OK, postResponse.getStatusCode());
|
||||||
|
Quiz created = postResponse.getBody();
|
||||||
|
assertNotNull(created);
|
||||||
|
assertNotNull(created.getId());
|
||||||
|
|
||||||
|
ResponseEntity<Quiz> getResponse = restTemplate.getForEntity(getUrl("/" + created.getId()), Quiz.class);
|
||||||
|
assertEquals(HttpStatus.OK, getResponse.getStatusCode());
|
||||||
|
assertEquals("Histoire", getResponse.getBody().getTitle());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateQuiz() {
|
||||||
|
Quiz quiz = new Quiz();
|
||||||
|
quiz.setTitle("Sciences");
|
||||||
|
quiz.setImage(image);
|
||||||
|
quiz.setNbQuest(10);
|
||||||
|
quiz = quizRepository.save(quiz);
|
||||||
|
|
||||||
|
quiz.setTitle("Sciences - Maj");
|
||||||
|
quiz.setNbQuest(12);
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
|
HttpEntity<Quiz> entity = new HttpEntity<>(quiz, headers);
|
||||||
|
|
||||||
|
ResponseEntity<Quiz> putResponse = restTemplate.exchange(getUrl(""), HttpMethod.PUT, entity, Quiz.class);
|
||||||
|
assertEquals(HttpStatus.OK, putResponse.getStatusCode());
|
||||||
|
assertEquals("Sciences - Maj", putResponse.getBody().getTitle());
|
||||||
|
assertEquals(12, putResponse.getBody().getNbQuest());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteQuiz() {
|
||||||
|
Quiz quiz = new Quiz();
|
||||||
|
quiz.setTitle("À supprimer");
|
||||||
|
quiz.setImage(image);
|
||||||
|
quiz.setNbQuest(3);
|
||||||
|
quiz = quizRepository.save(quiz);
|
||||||
|
|
||||||
|
ResponseEntity<Void> deleteResponse = restTemplate.exchange(
|
||||||
|
getUrl("?id=" + quiz.getId()), HttpMethod.DELETE, null, Void.class);
|
||||||
|
assertEquals(HttpStatus.OK, deleteResponse.getStatusCode());
|
||||||
|
assertFalse(quizRepository.existsById(quiz.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetQuizNotFound() {
|
||||||
|
ResponseEntity<String> response = restTemplate.getForEntity(getUrl("/9999"), String.class);
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
assertTrue(response.getBody().contains("Quiz non trouvé"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteQuizNotFound() {
|
||||||
|
ResponseEntity<String> response = restTemplate.exchange(
|
||||||
|
getUrl("?id=9999"), HttpMethod.DELETE, null, String.class);
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
assertTrue(response.getBody().contains("Quiz non trouvé"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetAllQuizzes() {
|
||||||
|
Quiz q1 = new Quiz();
|
||||||
|
q1.setTitle("Test 1");
|
||||||
|
q1.setImage(image);
|
||||||
|
q1.setNbQuest(5);
|
||||||
|
|
||||||
|
Quiz q2 = new Quiz();
|
||||||
|
q2.setTitle("Test 2");
|
||||||
|
q2.setImage(image);
|
||||||
|
q2.setNbQuest(10);
|
||||||
|
|
||||||
|
quizRepository.save(q1);
|
||||||
|
quizRepository.save(q2);
|
||||||
|
|
||||||
|
ResponseEntity<Quiz[]> response = restTemplate.getForEntity(getUrl(""), Quiz[].class);
|
||||||
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
assertEquals(2, response.getBody().length);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,134 @@
|
|||||||
|
package com.example.wfwebapi.controller;
|
||||||
|
|
||||||
|
import com.example.wfwebapi.model.*;
|
||||||
|
import com.example.wfwebapi.repository.*;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
|
import org.springframework.hateoas.EntityModel;
|
||||||
|
import org.springframework.http.*;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
class QuoteControllerTest {
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Autowired private TestRestTemplate restTemplate;
|
||||||
|
@Autowired private QuoteRepository quoteRepository;
|
||||||
|
@Autowired private UserRepository userRepository;
|
||||||
|
@Autowired private SourceRepository sourceRepository;
|
||||||
|
@Autowired private CaracterRepository caracterRepository;
|
||||||
|
@Autowired private ImageRepository imageRepository;
|
||||||
|
|
||||||
|
private User user;
|
||||||
|
private Source source;
|
||||||
|
private Caracter caracter;
|
||||||
|
|
||||||
|
private String getUrl(String path) {
|
||||||
|
return "http://localhost:" + port + "/api/v1/quote" + path;
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
quoteRepository.deleteAll();
|
||||||
|
userRepository.deleteAll();
|
||||||
|
sourceRepository.deleteAll();
|
||||||
|
caracterRepository.deleteAll();
|
||||||
|
imageRepository.deleteAll();
|
||||||
|
|
||||||
|
Image img = new Image();
|
||||||
|
img.setImgPath("img.png");
|
||||||
|
img = imageRepository.save(img); // correction ici
|
||||||
|
|
||||||
|
caracter = new Caracter();
|
||||||
|
caracter.setCaracter("Test Character");
|
||||||
|
caracter.setImage(img);
|
||||||
|
caracter = caracterRepository.save(caracter);
|
||||||
|
|
||||||
|
source = new Source();
|
||||||
|
source.setTitle("Test Source");
|
||||||
|
source.setDateS(2020);
|
||||||
|
source = sourceRepository.save(source);
|
||||||
|
|
||||||
|
user = new User();
|
||||||
|
user.setUsername("verif");
|
||||||
|
user.setEmail("v@v.fr");
|
||||||
|
user.setPassword("pass");
|
||||||
|
user.setImage(img);
|
||||||
|
user.setCreation(java.time.LocalDate.now());
|
||||||
|
user = userRepository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteQuote() {
|
||||||
|
Quote quote = new Quote();
|
||||||
|
quote.setContent("À supprimer");
|
||||||
|
quote.setLangue("fr");
|
||||||
|
quote.setLikes(0);
|
||||||
|
quote.setIsValide(false);
|
||||||
|
quote.setReason("test");
|
||||||
|
quote.setCaracter(caracter);
|
||||||
|
quote.setSource(source);
|
||||||
|
quote.setUserVerif(user);
|
||||||
|
quote = quoteRepository.save(quote);
|
||||||
|
|
||||||
|
ResponseEntity<Void> response = restTemplate.exchange(
|
||||||
|
getUrl("/delete?idQuote=" + quote.getId()), HttpMethod.DELETE, null, Void.class);
|
||||||
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
assertFalse(quoteRepository.existsById(quote.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetQuoteNotFound() {
|
||||||
|
ResponseEntity<String> response = restTemplate.getForEntity(
|
||||||
|
getUrl("/9999"), String.class);
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
assertTrue(response.getBody().contains("Citation non trouvée"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetAllQuotes() {
|
||||||
|
Quote quote1 = new Quote();
|
||||||
|
quote1.setContent("Q1");
|
||||||
|
quote1.setLangue("fr");
|
||||||
|
quote1.setLikes(5);
|
||||||
|
quote1.setIsValide(true);
|
||||||
|
quote1.setReason("ok");
|
||||||
|
quote1.setCaracter(caracter);
|
||||||
|
quote1.setSource(source);
|
||||||
|
quote1.setUserVerif(user);
|
||||||
|
|
||||||
|
Quote quote2 = new Quote();
|
||||||
|
quote2.setContent("Q2");
|
||||||
|
quote2.setLangue("en");
|
||||||
|
quote2.setLikes(10);
|
||||||
|
quote2.setIsValide(true);
|
||||||
|
quote2.setReason("ok");
|
||||||
|
quote2.setCaracter(caracter);
|
||||||
|
quote2.setSource(source);
|
||||||
|
quote2.setUserVerif(user);
|
||||||
|
|
||||||
|
quoteRepository.save(quote1);
|
||||||
|
quoteRepository.save(quote2);
|
||||||
|
|
||||||
|
ResponseEntity<String> response = restTemplate.getForEntity(
|
||||||
|
getUrl("/all?index=0&count=10"), String.class);
|
||||||
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetDailyQuoteNotFound() {
|
||||||
|
ResponseEntity<String> response = restTemplate.getForEntity(
|
||||||
|
getUrl("/dailyquote?year=2023&month=12&day=1&lang=zz"), String.class);
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
assertTrue(response.getBody().contains("Aucune citation trouvée"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,118 @@
|
|||||||
|
package com.example.wfwebapi.controller;
|
||||||
|
|
||||||
|
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||||
|
import com.example.wfwebapi.model.RecordQuiz;
|
||||||
|
import com.example.wfwebapi.model.RecordQuizId;
|
||||||
|
import com.example.wfwebapi.repository.RecordQuizRepository;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
public class RecordQuizControllerTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private RecordQuizRepository recordQuizRepository;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private RecordQuizController recordQuizController;
|
||||||
|
|
||||||
|
private RecordQuizId recordQuizId;
|
||||||
|
private RecordQuiz recordQuiz;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
recordQuizId = new RecordQuizId(1L, 1L);
|
||||||
|
recordQuiz = new RecordQuiz();
|
||||||
|
recordQuiz.setId(recordQuizId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRecord_success() {
|
||||||
|
when(recordQuizRepository.findById(recordQuizId)).thenReturn(Optional.of(recordQuiz));
|
||||||
|
|
||||||
|
RecordQuiz result = recordQuizController.getRecord(1L, 1L);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(recordQuizId, result.getId());
|
||||||
|
verify(recordQuizRepository, times(1)).findById(recordQuizId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRecord_notFound() {
|
||||||
|
when(recordQuizRepository.findById(recordQuizId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> {
|
||||||
|
recordQuizController.getRecord(1L, 1L);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals("RecordQuiz non trouvé", exception.getMessage());
|
||||||
|
verify(recordQuizRepository, times(1)).findById(recordQuizId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateRecord_success() {
|
||||||
|
when(recordQuizRepository.save(recordQuiz)).thenReturn(recordQuiz);
|
||||||
|
|
||||||
|
RecordQuiz result = recordQuizController.createRecord(recordQuiz);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(recordQuizId, result.getId());
|
||||||
|
verify(recordQuizRepository, times(1)).save(recordQuiz);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateRecord_success() {
|
||||||
|
when(recordQuizRepository.existsById(recordQuizId)).thenReturn(true);
|
||||||
|
when(recordQuizRepository.save(recordQuiz)).thenReturn(recordQuiz);
|
||||||
|
|
||||||
|
RecordQuiz result = recordQuizController.updateRecord(recordQuiz);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(recordQuizId, result.getId());
|
||||||
|
verify(recordQuizRepository, times(1)).existsById(recordQuizId);
|
||||||
|
verify(recordQuizRepository, times(1)).save(recordQuiz);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateRecord_notFound() {
|
||||||
|
when(recordQuizRepository.existsById(recordQuizId)).thenReturn(false);
|
||||||
|
|
||||||
|
ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> {
|
||||||
|
recordQuizController.updateRecord(recordQuiz);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals("RecordQuiz non trouvé", exception.getMessage());
|
||||||
|
verify(recordQuizRepository, times(1)).existsById(recordQuizId);
|
||||||
|
verify(recordQuizRepository, times(0)).save(recordQuiz);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteRecord_success() {
|
||||||
|
when(recordQuizRepository.existsById(recordQuizId)).thenReturn(true);
|
||||||
|
|
||||||
|
recordQuizController.deleteRecord(1L, 1L);
|
||||||
|
|
||||||
|
verify(recordQuizRepository, times(1)).deleteById(recordQuizId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteRecord_notFound() {
|
||||||
|
when(recordQuizRepository.existsById(recordQuizId)).thenReturn(false);
|
||||||
|
|
||||||
|
ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> {
|
||||||
|
recordQuizController.deleteRecord(1L, 1L);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals("RecordQuiz non trouvé", exception.getMessage());
|
||||||
|
verify(recordQuizRepository, times(1)).existsById(recordQuizId);
|
||||||
|
verify(recordQuizRepository, times(0)).deleteById(recordQuizId);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class AdminTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
Admin admin = new Admin();
|
||||||
|
User user = new User();
|
||||||
|
|
||||||
|
admin.setUserId(123L);
|
||||||
|
admin.setUser(user);
|
||||||
|
|
||||||
|
assertEquals(123L, admin.getUserId());
|
||||||
|
assertEquals(user, admin.getUser());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class CaracterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
Caracter caracter = new Caracter();
|
||||||
|
Image image = new Image();
|
||||||
|
|
||||||
|
caracter.setId(1L);
|
||||||
|
caracter.setCaracter("Guerrier");
|
||||||
|
caracter.setImage(image);
|
||||||
|
|
||||||
|
assertEquals(1L, caracter.getId());
|
||||||
|
assertEquals("Guerrier", caracter.getCaracter());
|
||||||
|
assertEquals(image, caracter.getImage());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class CommentaryTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
Commentary commentary = new Commentary();
|
||||||
|
Quote quote = new Quote();
|
||||||
|
User user = new User();
|
||||||
|
LocalDate date = LocalDate.of(2024, 4, 5);
|
||||||
|
String text = "Ceci est un commentaire";
|
||||||
|
|
||||||
|
commentary.setId(1L);
|
||||||
|
commentary.setQuote(quote);
|
||||||
|
commentary.setUser(user);
|
||||||
|
commentary.setDateC(date);
|
||||||
|
commentary.setComment(text);
|
||||||
|
|
||||||
|
assertEquals(1L, commentary.getId());
|
||||||
|
assertEquals(quote, commentary.getQuote());
|
||||||
|
assertEquals(user, commentary.getUser());
|
||||||
|
assertEquals(date, commentary.getDateC());
|
||||||
|
assertEquals(text, commentary.getComment());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class DailyQuoteTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
DailyQuote dailyQuote = new DailyQuote();
|
||||||
|
Quote quote = new Quote();
|
||||||
|
|
||||||
|
dailyQuote.setCitationId(1L);
|
||||||
|
dailyQuote.setQuote(quote);
|
||||||
|
|
||||||
|
assertEquals(1L, dailyQuote.getCitationId());
|
||||||
|
assertEquals(quote, dailyQuote.getQuote());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class FavoriteIdTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
FavoriteId id = new FavoriteId();
|
||||||
|
id.setUser(1L);
|
||||||
|
id.setQuote(2L);
|
||||||
|
|
||||||
|
assertEquals(1L, id.getUser());
|
||||||
|
assertEquals(2L, id.getQuote());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConstructor() {
|
||||||
|
FavoriteId id = new FavoriteId(3L, 4L);
|
||||||
|
|
||||||
|
assertEquals(3L, id.getUser());
|
||||||
|
assertEquals(4L, id.getQuote());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEqualsAndHashCode() {
|
||||||
|
FavoriteId id1 = new FavoriteId(1L, 2L);
|
||||||
|
FavoriteId id2 = new FavoriteId(1L, 2L);
|
||||||
|
FavoriteId id3 = new FavoriteId(2L, 1L);
|
||||||
|
|
||||||
|
assertEquals(id1, id2);
|
||||||
|
assertEquals(id1.hashCode(), id2.hashCode());
|
||||||
|
assertNotEquals(id1, id3);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class FavoriteTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
Favorite favorite = new Favorite();
|
||||||
|
FavoriteId favoriteId = new FavoriteId();
|
||||||
|
User user = new User();
|
||||||
|
Quote quote = new Quote();
|
||||||
|
|
||||||
|
favorite.setId(favoriteId);
|
||||||
|
favorite.setUser(user);
|
||||||
|
favorite.setQuote(quote);
|
||||||
|
|
||||||
|
assertEquals(favoriteId, favorite.getId());
|
||||||
|
assertEquals(user, favorite.getUser());
|
||||||
|
assertEquals(quote, favorite.getQuote());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class ImageTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
Image image = new Image();
|
||||||
|
image.setId(1L);
|
||||||
|
image.setImgPath("/images/avatar.png");
|
||||||
|
|
||||||
|
assertEquals(1L, image.getId());
|
||||||
|
assertEquals("/images/avatar.png", image.getImgPath());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class QuestionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
Question question = new Question();
|
||||||
|
|
||||||
|
question.setId(1L);
|
||||||
|
question.setTexte("Quelle est la capitale de la France ?");
|
||||||
|
question.setAnswerA("Paris");
|
||||||
|
question.setAnswerB("Lyon");
|
||||||
|
question.setAnswerC("Marseille");
|
||||||
|
question.setAnswerD("Nice");
|
||||||
|
question.setCAnswer("Paris");
|
||||||
|
|
||||||
|
assertEquals(1L, question.getId());
|
||||||
|
assertEquals("Quelle est la capitale de la France ?", question.getTexte());
|
||||||
|
assertEquals("Paris", question.getAnswerA());
|
||||||
|
assertEquals("Lyon", question.getAnswerB());
|
||||||
|
assertEquals("Marseille", question.getAnswerC());
|
||||||
|
assertEquals("Nice", question.getAnswerD());
|
||||||
|
assertEquals("Paris", question.getCAnswer());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class QuizQuestionIdTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
QuizQuestionId id = new QuizQuestionId();
|
||||||
|
id.setQuiz(1L);
|
||||||
|
id.setQuestion(2L);
|
||||||
|
|
||||||
|
assertEquals(1L, id.getQuiz());
|
||||||
|
assertEquals(2L, id.getQuestion());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConstructor() {
|
||||||
|
QuizQuestionId id = new QuizQuestionId(3L, 4L);
|
||||||
|
|
||||||
|
assertEquals(3L, id.getQuiz());
|
||||||
|
assertEquals(4L, id.getQuestion());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEqualsAndHashCode() {
|
||||||
|
QuizQuestionId id1 = new QuizQuestionId(1L, 2L);
|
||||||
|
QuizQuestionId id2 = new QuizQuestionId(1L, 2L);
|
||||||
|
QuizQuestionId id3 = new QuizQuestionId(2L, 1L);
|
||||||
|
|
||||||
|
assertEquals(id1, id2);
|
||||||
|
assertEquals(id1.hashCode(), id2.hashCode());
|
||||||
|
assertNotEquals(id1, id3);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class QuizQuestionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
QuizQuestion quizQuestion = new QuizQuestion();
|
||||||
|
QuizQuestionId id = new QuizQuestionId();
|
||||||
|
Quiz quiz = new Quiz();
|
||||||
|
Question question = new Question();
|
||||||
|
|
||||||
|
quizQuestion.setId(id);
|
||||||
|
quizQuestion.setQuiz(quiz);
|
||||||
|
quizQuestion.setQuestion(question);
|
||||||
|
|
||||||
|
assertEquals(id, quizQuestion.getId());
|
||||||
|
assertEquals(quiz, quizQuestion.getQuiz());
|
||||||
|
assertEquals(question, quizQuestion.getQuestion());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class QuizTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
Quiz quiz = new Quiz();
|
||||||
|
Image image = new Image();
|
||||||
|
|
||||||
|
quiz.setId(1L);
|
||||||
|
quiz.setTitle("Histoire de France");
|
||||||
|
quiz.setImage(image);
|
||||||
|
quiz.setNbQuest(10);
|
||||||
|
|
||||||
|
assertEquals(1L, quiz.getId());
|
||||||
|
assertEquals("Histoire de France", quiz.getTitle());
|
||||||
|
assertEquals(image, quiz.getImage());
|
||||||
|
assertEquals(10, quiz.getNbQuest());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class QuoteTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
Quote quote = new Quote();
|
||||||
|
Caracter caracter = new Caracter();
|
||||||
|
Source source = new Source();
|
||||||
|
User user = new User();
|
||||||
|
|
||||||
|
quote.setId(1L);
|
||||||
|
quote.setContent("Ceci est une citation.");
|
||||||
|
quote.setLikes(42);
|
||||||
|
quote.setLangue("fr");
|
||||||
|
quote.setIsValide(true);
|
||||||
|
quote.setReason("Vérifiée manuellement");
|
||||||
|
quote.setCaracter(caracter);
|
||||||
|
quote.setSource(source);
|
||||||
|
quote.setUserVerif(user);
|
||||||
|
|
||||||
|
assertEquals(1L, quote.getId());
|
||||||
|
assertEquals("Ceci est une citation.", quote.getContent());
|
||||||
|
assertEquals(42, quote.getLikes());
|
||||||
|
assertEquals("fr", quote.getLangue());
|
||||||
|
assertTrue(quote.getIsValide());
|
||||||
|
assertEquals("Vérifiée manuellement", quote.getReason());
|
||||||
|
assertEquals(caracter, quote.getCaracter());
|
||||||
|
assertEquals(source, quote.getSource());
|
||||||
|
assertEquals(user, quote.getUserVerif());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class RecordQuizIdTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
RecordQuizId id = new RecordQuizId();
|
||||||
|
id.setUser(1L);
|
||||||
|
id.setQuiz(2L);
|
||||||
|
|
||||||
|
assertEquals(1L, id.getUser());
|
||||||
|
assertEquals(2L, id.getQuiz());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConstructor() {
|
||||||
|
RecordQuizId id = new RecordQuizId(3L, 4L);
|
||||||
|
|
||||||
|
assertEquals(3L, id.getUser());
|
||||||
|
assertEquals(4L, id.getQuiz());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEqualsAndHashCode() {
|
||||||
|
RecordQuizId id1 = new RecordQuizId(1L, 2L);
|
||||||
|
RecordQuizId id2 = new RecordQuizId(1L, 2L);
|
||||||
|
RecordQuizId id3 = new RecordQuizId(2L, 1L);
|
||||||
|
|
||||||
|
assertEquals(id1, id2);
|
||||||
|
assertEquals(id1.hashCode(), id2.hashCode());
|
||||||
|
assertNotEquals(id1, id3);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class RecordQuizTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
RecordQuiz record = new RecordQuiz();
|
||||||
|
RecordQuizId id = new RecordQuizId();
|
||||||
|
User user = new User();
|
||||||
|
Quiz quiz = new Quiz();
|
||||||
|
|
||||||
|
record.setId(id);
|
||||||
|
record.setUser(user);
|
||||||
|
record.setQuiz(quiz);
|
||||||
|
record.setNbPoint(8);
|
||||||
|
record.setTimeQ(120);
|
||||||
|
|
||||||
|
assertEquals(id, record.getId());
|
||||||
|
assertEquals(user, record.getUser());
|
||||||
|
assertEquals(quiz, record.getQuiz());
|
||||||
|
assertEquals(8, record.getNbPoint());
|
||||||
|
assertEquals(120, record.getTimeQ());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class SourceTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
Source source = new Source();
|
||||||
|
|
||||||
|
source.setId(1L);
|
||||||
|
source.setTitle("Discours historique");
|
||||||
|
source.setDateS(1945);
|
||||||
|
|
||||||
|
assertEquals(1L, source.getId());
|
||||||
|
assertEquals("Discours historique", source.getTitle());
|
||||||
|
assertEquals(1945, source.getDateS());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.example.wfwebapi.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class UserTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGettersAndSetters() {
|
||||||
|
User user = new User();
|
||||||
|
Image image = new Image();
|
||||||
|
LocalDate creationDate = LocalDate.of(2024, 1, 1);
|
||||||
|
|
||||||
|
user.setId(1L);
|
||||||
|
user.setUsername("testuser");
|
||||||
|
user.setEmail("test@example.com");
|
||||||
|
user.setPassword("securePassword123");
|
||||||
|
user.setImage(image);
|
||||||
|
user.setCreation(creationDate);
|
||||||
|
|
||||||
|
assertEquals(1L, user.getId());
|
||||||
|
assertEquals("testuser", user.getUsername());
|
||||||
|
assertEquals("test@example.com", user.getEmail());
|
||||||
|
assertEquals("securePassword123", user.getPassword());
|
||||||
|
assertEquals(image, user.getImage());
|
||||||
|
assertEquals(creationDate, user.getCreation());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue