Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
2096215195 | ||
|
8315a23855 |
@ -14,6 +14,9 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'com.h2database:h2:2.1.210'
|
||||
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
|
@ -2,40 +2,63 @@
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>Калькулятор</title>
|
||||
<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">
|
||||
<title>Calculator</title>
|
||||
<link rel="stylesheet" href="https://unpkg.com/chota@latest">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
|
||||
<div class="m-auto dflex flex-row align-items-center">
|
||||
<div class="col-sm-3 my-1">
|
||||
<input type="number" class="form-control" id="num1">
|
||||
</div>
|
||||
<div class="col-sm-3 my-1">
|
||||
<select class="form-control" id="operation">
|
||||
<option value="sum" selected>+</option>
|
||||
<option value="sub">-</option>
|
||||
<option value="mul">*</option>
|
||||
<option value="div">/</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-sm-3 my-1">
|
||||
<input type="number" class="form-control" id="num2">
|
||||
</div>
|
||||
<div class="col-sm-3 my-1">
|
||||
<button type="button" class="form-control btn btn-light" id="get-result" onclick="calculate()">=</button>
|
||||
</div>
|
||||
<div class="col-sm-3 my-1">
|
||||
<p class="h5" id="result"></p>
|
||||
</div>
|
||||
<div class="container" style="max-width:600px">
|
||||
<div class="card row is-full-screen is-center">
|
||||
<div class="col">
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<input type="text" id="value1">
|
||||
</div>
|
||||
<div class="col">
|
||||
<select id="operation">
|
||||
<option value="sum" selected>+</option>
|
||||
<option value="sub">-</option>
|
||||
<option value="mul">*</option>
|
||||
<option value="invert">~</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col">
|
||||
<input type="text" id="value2">
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<button type="button" id="get-int-result" onclick="calculate('intops')">Result for int</button>
|
||||
</div>
|
||||
<div class="col is-right">
|
||||
<button type="button" id="get-string-result" onclick="calculate('stringops')">Result for string</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card row is-center">
|
||||
<h5 class="is-center" id="result"></h5>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<script>
|
||||
async function calculate() {
|
||||
const num1 = document.getElementById("num1").value
|
||||
const num2 = document.getElementById("num2").value
|
||||
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/${document.getElementById("operation").value}?num1=${num1}&num2=${num2}`)).text()
|
||||
async function calculate(ops) {
|
||||
const value1 = document.getElementById("value1").value
|
||||
const value2 = document.getElementById("value2").value
|
||||
const op = document.getElementById("operation").value
|
||||
if (op == 'sum') {
|
||||
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/sum?value1=${value1}&value2=${value2}&ops=${ops}`)).text()
|
||||
} else if (op == 'sub') {
|
||||
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/sub?value1=${value1}&value2=${value2}&ops=${ops}`)).text()
|
||||
} else if (op == 'mul') {
|
||||
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/mul?value=${value1}&count=${value2}&ops=${ops}`)).text()
|
||||
} else {
|
||||
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/invert?value=${value1}&ops=${ops}`)).text()
|
||||
}
|
||||
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
@ -3,7 +3,7 @@ const http = require('http')
|
||||
|
||||
const requestListener = async function (req, res) {
|
||||
res.writeHead(200);
|
||||
fs.readFile('index.html', (err, data) => {
|
||||
fs.readFile('index.html', 'utf8', (err, data) => {
|
||||
res.end(data);
|
||||
});
|
||||
};
|
||||
|
@ -1,41 +0,0 @@
|
||||
package np.something.controllers;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class MathController {
|
||||
@GetMapping("/sum")
|
||||
public int sum(
|
||||
@RequestParam(value = "num1", defaultValue = "0") int num1,
|
||||
@RequestParam(value = "num2", defaultValue = "0") int num2
|
||||
) {
|
||||
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
@GetMapping("/sub")
|
||||
public int sub(
|
||||
@RequestParam(value = "num1", defaultValue = "0") int num1,
|
||||
@RequestParam(value = "num2", defaultValue = "0") int num2
|
||||
) {
|
||||
return num1 - num2;
|
||||
}
|
||||
|
||||
@GetMapping("/mul")
|
||||
public int mul(
|
||||
@RequestParam(value = "num1", defaultValue = "0") int num1,
|
||||
@RequestParam(value = "num2", defaultValue = "0") int num2
|
||||
) {
|
||||
return num1 * num2;
|
||||
}
|
||||
|
||||
@GetMapping("/div")
|
||||
public int div(
|
||||
@RequestParam(value = "num1", defaultValue = "0") int num1,
|
||||
@RequestParam(value = "num2", defaultValue = "0") int num2
|
||||
) {
|
||||
return num1 / num2;
|
||||
}
|
||||
}
|
68
src/main/java/np/something/model/Comment.java
Normal file
68
src/main/java/np/something/model/Comment.java
Normal file
@ -0,0 +1,68 @@
|
||||
package np.something.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Comment {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@Column
|
||||
private String content;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@JoinColumn(name="customer_fk")
|
||||
private Customer customer;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@JoinColumn(name="post_fk")
|
||||
private Post post;
|
||||
|
||||
public Comment() {
|
||||
|
||||
}
|
||||
|
||||
public Comment(Customer customer, Post post, String content) {
|
||||
this.customer = customer;
|
||||
this.post = post;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Post getPost() {
|
||||
return post;
|
||||
}
|
||||
|
||||
public Customer getCustomer() {
|
||||
return customer;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Comment comment = (Comment) o;
|
||||
|
||||
return Objects.equals(id, comment.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
}
|
72
src/main/java/np/something/model/Customer.java
Normal file
72
src/main/java/np/something/model/Customer.java
Normal file
@ -0,0 +1,72 @@
|
||||
package np.something.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Entity
|
||||
public class Customer {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@Column
|
||||
private String username;
|
||||
@Column
|
||||
private String hashedPassword;
|
||||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL)
|
||||
private List<Comment> comments;
|
||||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL)
|
||||
private List<Post> posts;
|
||||
|
||||
public Customer() {
|
||||
|
||||
}
|
||||
|
||||
public Customer(String username, String hashedPassword) {
|
||||
this.username = username;
|
||||
this.hashedPassword = hashedPassword;
|
||||
this.comments = new ArrayList<>();
|
||||
this.posts = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getHashedPassword() {
|
||||
return hashedPassword;
|
||||
}
|
||||
|
||||
public List<Comment> getComments() {
|
||||
return comments;
|
||||
}
|
||||
|
||||
public List<Post> getPosts() {
|
||||
return posts;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public void setHashedPassword(String hashedPassword) {
|
||||
this.hashedPassword = hashedPassword;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null || getClass() != obj.getClass()) return false;
|
||||
Customer customer = (Customer) obj;
|
||||
return Objects.equals(id, customer.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
}
|
80
src/main/java/np/something/model/Post.java
Normal file
80
src/main/java/np/something/model/Post.java
Normal file
@ -0,0 +1,80 @@
|
||||
package np.something.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Post {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@Column
|
||||
private String title;
|
||||
|
||||
@Column
|
||||
private String content;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@JoinColumn(name = "customer_fk")
|
||||
private Customer customer;
|
||||
|
||||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "post", cascade = CascadeType.ALL)
|
||||
private List<Comment> comments;
|
||||
|
||||
public Post() {
|
||||
|
||||
}
|
||||
|
||||
public Post(Customer customer, String title, String content) {
|
||||
this.customer = customer;
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
this.comments = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Post post = (Post) o;
|
||||
|
||||
return id.equals(post.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return id.hashCode();
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public List<Comment> getComments() {
|
||||
return comments;
|
||||
}
|
||||
|
||||
public Customer getCustomer() {
|
||||
return customer;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
72
src/main/java/np/something/services/CommentService.java
Normal file
72
src/main/java/np/something/services/CommentService.java
Normal file
@ -0,0 +1,72 @@
|
||||
package np.something.services;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.transaction.Transactional;
|
||||
import np.something.model.Comment;
|
||||
import np.something.model.Customer;
|
||||
import np.something.model.Post;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class CommentService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Comment findComment(Long id) {
|
||||
final Comment comment = em.find(Comment.class, id);
|
||||
if (comment == null) {
|
||||
throw new EntityNotFoundException(String.format("Comment with id [%s] is not found", id));
|
||||
}
|
||||
return comment;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Comment> findAllComments() {
|
||||
return em.createQuery("select c from Comment c", Comment.class).getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Comment addComment(Customer customer, Post post, String content) {
|
||||
if (customer == null || post == null) {
|
||||
throw new IllegalArgumentException("Invalid customer or post");
|
||||
}
|
||||
if (!StringUtils.hasText(content)) {
|
||||
throw new IllegalArgumentException("Invalid comment's content");
|
||||
}
|
||||
final Comment comment = new Comment(customer, post, content);
|
||||
comment.getCustomer().getComments().add(comment);
|
||||
comment.getPost().getComments().add(comment);
|
||||
em.persist(comment);
|
||||
return comment;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Comment updateComment(Long id, String content) {
|
||||
if (!StringUtils.hasText(content)) {
|
||||
throw new IllegalArgumentException("Comment's content is empty");
|
||||
}
|
||||
final Comment comment = findComment(id);
|
||||
comment.setContent(content);
|
||||
return em.merge(comment);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Comment deleteComment(Long id) {
|
||||
final Comment currentComment = findComment(id);
|
||||
currentComment.getPost().getComments().remove(currentComment);
|
||||
currentComment.getCustomer().getComments().remove(currentComment);
|
||||
em.remove(currentComment);
|
||||
return currentComment;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllComment() {
|
||||
em.createQuery("delete from Comment").executeUpdate();
|
||||
}
|
||||
}
|
64
src/main/java/np/something/services/CustomerService.java
Normal file
64
src/main/java/np/something/services/CustomerService.java
Normal file
@ -0,0 +1,64 @@
|
||||
package np.something.services;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.transaction.Transactional;
|
||||
import np.something.model.Customer;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class CustomerService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Customer findCustomer(Long id) {
|
||||
final Customer customer = em.find(Customer.class, id);
|
||||
if (customer == null) {
|
||||
throw new EntityNotFoundException(String.format("User with id [%s] is not found", id));
|
||||
}
|
||||
return customer;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Customer> findAllCustomers() {
|
||||
return em.createQuery("select c from Customer c", Customer.class).getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Customer addCustomer(String username, String password) {
|
||||
if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
|
||||
throw new IllegalArgumentException("Customer's username or password is empty");
|
||||
}
|
||||
final Customer customer = new Customer(username, password);
|
||||
em.persist(customer);
|
||||
return customer;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Customer updateCustomer(Long id, String username, String password) {
|
||||
if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
|
||||
throw new IllegalArgumentException("Customer's username or password is empty");
|
||||
}
|
||||
final Customer customer = findCustomer(id);
|
||||
customer.setUsername(username);
|
||||
customer.setHashedPassword(password);
|
||||
return em.merge(customer);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Customer deleteCustomer(Long id) {
|
||||
final Customer currentCustomer = findCustomer(id);
|
||||
em.remove(currentCustomer);
|
||||
return currentCustomer;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllCustomers() {
|
||||
em.createQuery("delete from Customer").executeUpdate();
|
||||
}
|
||||
}
|
71
src/main/java/np/something/services/PostService.java
Normal file
71
src/main/java/np/something/services/PostService.java
Normal file
@ -0,0 +1,71 @@
|
||||
package np.something.services;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.transaction.Transactional;
|
||||
import np.something.model.Comment;
|
||||
import np.something.model.Customer;
|
||||
import np.something.model.Post;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class PostService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Post findPost(Long id) {
|
||||
final Post post = em.find(Post.class, id);
|
||||
if (post == null) {
|
||||
throw new EntityNotFoundException(String.format("Post with id [%s] is not found", id));
|
||||
}
|
||||
return post;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Post> findAllPosts() {
|
||||
return em.createQuery("select p from Post p", Post.class).getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Post addPost(Customer customer, String title, String content) {
|
||||
if (customer == null) {
|
||||
throw new IllegalArgumentException("Invalid customer");
|
||||
}
|
||||
if (!StringUtils.hasText(title) | !StringUtils.hasText(content)) {
|
||||
throw new IllegalArgumentException("Invalid post's content or title");
|
||||
}
|
||||
final Post post = new Post(customer, title, content);
|
||||
post.getCustomer().getPosts().add(post);
|
||||
em.persist(post);
|
||||
return post;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Post updatePost(Long id, String title, String content) {
|
||||
if (!StringUtils.hasText(content) | !StringUtils.hasText(title)) {
|
||||
throw new IllegalArgumentException("Post's content or title is empty");
|
||||
}
|
||||
final Post post = findPost(id);
|
||||
post.setTitle(title);
|
||||
post.setContent(content);
|
||||
return em.merge(post);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Post deletePost(Long id) {
|
||||
final Post currentPost = findPost(id);
|
||||
currentPost.getCustomer().getPosts().remove(currentPost);
|
||||
em.remove(currentPost);
|
||||
return currentPost;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllPosts() {
|
||||
em.createQuery("delete from Post").executeUpdate();
|
||||
}
|
||||
}
|
@ -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
|
||||
|
123
src/test/java/np/something/SocialNetworkTest.java
Normal file
123
src/test/java/np/something/SocialNetworkTest.java
Normal file
@ -0,0 +1,123 @@
|
||||
package np.something;
|
||||
|
||||
import np.something.model.*;
|
||||
import np.something.services.CommentService;
|
||||
import np.something.services.CustomerService;
|
||||
import np.something.services.PostService;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
public class SocialNetworkTest {
|
||||
@Autowired
|
||||
CustomerService customerService;
|
||||
|
||||
@Autowired
|
||||
CommentService commentService;
|
||||
|
||||
@Autowired
|
||||
PostService postService;
|
||||
|
||||
@Test
|
||||
void testCustomers() {
|
||||
commentService.deleteAllComment();
|
||||
postService.deleteAllPosts();
|
||||
customerService.deleteAllCustomers();
|
||||
|
||||
Customer c1 = customerService.addCustomer("first", "1");
|
||||
Customer c2 = customerService.addCustomer("second", "2");
|
||||
Customer c3 = customerService.addCustomer("third", "3");
|
||||
|
||||
Assertions.assertEquals("first", c1.getUsername());
|
||||
Assertions.assertEquals("second", c2.getUsername());
|
||||
Assertions.assertEquals("third", c3.getUsername());
|
||||
|
||||
Assertions.assertEquals(c1, customerService.findCustomer(c1.getId()));
|
||||
|
||||
customerService.deleteCustomer(c2.getId());
|
||||
|
||||
Assertions.assertEquals(2, customerService.findAllCustomers().size());
|
||||
|
||||
Customer c4 = customerService.updateCustomer(c3.getId(), "fourth", "4");
|
||||
|
||||
Assertions.assertNotEquals(c3.getUsername(), c4.getUsername());
|
||||
Assertions.assertNotEquals(c3.getHashedPassword(), c4.getHashedPassword());
|
||||
|
||||
commentService.deleteAllComment();
|
||||
postService.deleteAllPosts();
|
||||
customerService.deleteAllCustomers();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPost() {
|
||||
commentService.deleteAllComment();
|
||||
postService.deleteAllPosts();
|
||||
customerService.deleteAllCustomers();
|
||||
|
||||
Customer c1 = customerService.addCustomer("first", "1");
|
||||
Customer c2 = customerService.addCustomer("second", "2");
|
||||
|
||||
Post p1 = postService.addPost(c1, "first title", "nonsense");
|
||||
Post p2 = postService.addPost(c2, "second title", "ordinal");
|
||||
|
||||
Assertions.assertEquals(2, postService.findAllPosts().size());
|
||||
|
||||
Assertions.assertEquals(p1.getCustomer(), c1);
|
||||
Assertions.assertEquals(p2.getCustomer(), c2);
|
||||
|
||||
Assertions.assertEquals(c1.getPosts().get(0), p1);
|
||||
Assertions.assertEquals(c2.getPosts().get(0), p2);
|
||||
|
||||
Assertions.assertEquals(p1, postService.findPost(p1.getId()));
|
||||
Assertions.assertEquals(p2, postService.findPost(p2.getId()));
|
||||
|
||||
Post p3 = postService.addPost(c1, "asdf", "asd");
|
||||
postService.deletePost(p1.getId());
|
||||
Assertions.assertEquals(1, customerService.findCustomer(c1.getId()).getPosts().size());
|
||||
|
||||
Post p4 = postService.updatePost(p2.getId(), "third title", "wow");
|
||||
|
||||
Assertions.assertNotEquals(p2.getTitle(), p4.getTitle());
|
||||
Assertions.assertNotEquals(p2.getContent(), p4.getContent());
|
||||
|
||||
commentService.deleteAllComment();
|
||||
postService.deleteAllPosts();
|
||||
customerService.deleteAllCustomers();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testComment() {
|
||||
commentService.deleteAllComment();
|
||||
postService.deleteAllPosts();
|
||||
customerService.deleteAllCustomers();
|
||||
|
||||
Customer c1 = customerService.addCustomer("first", "1");
|
||||
Customer c2 = customerService.addCustomer("second", "2");
|
||||
|
||||
Post p1 = postService.addPost(c1, "first title", "nonsense");
|
||||
Post p2 = postService.addPost(c2, "second title", "ordinal");
|
||||
|
||||
Assertions.assertEquals(2, postService.findAllPosts().size());
|
||||
|
||||
Comment com1 = commentService.addComment(c1, p2, "What");
|
||||
Comment com2 = commentService.addComment(c2, p1, "How");
|
||||
|
||||
Assertions.assertEquals(c1, p2.getComments().get(0).getCustomer());
|
||||
Assertions.assertEquals(c2, p1.getComments().get(0).getCustomer());
|
||||
|
||||
Comment com3 = commentService.addComment(c1, p1, "Really");
|
||||
|
||||
Assertions.assertEquals(com2, commentService.findComment(p1.getComments().get(0).getId()));
|
||||
|
||||
Comment com4 = commentService.updateComment(com3.getId(), "Not really");
|
||||
|
||||
Assertions.assertNotEquals(com3.getContent(), com4.getContent());
|
||||
Assertions.assertEquals(com3.getCustomer().getId(), com4.getCustomer().getId());
|
||||
|
||||
commentService.deleteAllComment();
|
||||
postService.deleteAllPosts();
|
||||
customerService.deleteAllCustomers();
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package np.something;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class SomethingApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
6
src/test/resources/application.properties
Normal file
6
src/test/resources/application.properties
Normal 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
|
Loading…
Reference in New Issue
Block a user