LabWork03 тесты поломанные

This commit is contained in:
Overlordddd 2023-03-13 18:17:31 +04:00
parent 1ae60aad92
commit f1a90f39c0
17 changed files with 423 additions and 215 deletions

View File

@ -18,8 +18,6 @@ dependencies {
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'
compileOnly('javax.persistence:javax.persistence-api:2.2')
implementation 'org.springframework.boot:spring-boot-starter-web'
}
tasks.named('test') {

View File

@ -2,11 +2,9 @@ package com.LabWork.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class AppApplication {
public static void main(String[] args) {

View File

@ -5,9 +5,9 @@ import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
class WebConfiguration implements WebMvcConfigurer {
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry){
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*");
}
}
}

View File

@ -1,19 +0,0 @@
package com.LabWork.app.calc.configuration;
import com.LabWork.app.calc.domain.MethodInteger;
import com.LabWork.app.calc.domain.MethodString;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MethodConfiguration {
public MethodInteger createIntegerMethods() {
return new MethodInteger();
}
public MethodString createStringMethods() {
return new MethodString();
}
}

View File

@ -1,51 +0,0 @@
package com.LabWork.app.calc.controller;
import com.LabWork.app.calc.service.MethodService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MethodController {
private final MethodService methodService;
public MethodController(MethodService methodService) {
this.methodService = methodService;
}
@GetMapping("/sum")
@ResponseBody
public String sum(@RequestParam(value = "first", defaultValue = "0") String first,
@RequestParam(value = "second", defaultValue = "0") String second,
@RequestParam(value = "type", defaultValue = "int") String type) {
Object a = first;
Object b = second;
System.out.println(a.getClass());
return methodService.Sum(a, b, type);
}
@GetMapping("/multiplication")
@ResponseBody
public String mul(@RequestParam(value = "first", defaultValue = "0") Object first,
@RequestParam(value = "second", defaultValue = "0") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return methodService.Multiplication(first, second, type);
}
@GetMapping("/difference")
@ResponseBody
public String difference(@RequestParam(value = "first", defaultValue = "0") Object first,
@RequestParam(value = "second", defaultValue = "0") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return methodService.Difference(first, second, type);
}
@GetMapping("/cont")
@ResponseBody
public String cont(@RequestParam(value = "first", defaultValue = "0") Object first,
@RequestParam(value = "second", defaultValue = "0") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return methodService.Contains(first, second, type);
}
}

View File

@ -1,12 +0,0 @@
package com.LabWork.app.calc.domain;
public interface IMethods<T> {
T Sum(T first, T second);
T Multiplication(T first, T second);
T Difference(T first, T second);
T Contains(T first, T second);
}

View File

@ -1,30 +0,0 @@
package com.LabWork.app.calc.domain;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component(value = "int")
public class MethodInteger implements IMethods<Integer> {
@Override
public Integer Sum(Integer first, Integer second) {
return first + second;
}
@Override
public Integer Multiplication(Integer first, Integer second) {
return first * second;
}
@Override
public Integer Difference(Integer first, Integer second) {
return first - second;
}
@Override
public Integer Contains(Integer first, Integer second) {
if (second==0){
return second;
}else{
return first/second;
}
}
}

View File

@ -1,37 +0,0 @@
package com.LabWork.app.calc.domain;
import org.springframework.stereotype.Component;
@Component(value = "string")
public class MethodString implements IMethods<String>{
@Override
public String Sum(String first, String second) {
return first.concat(second);
}
@Override
public String Multiplication(String first, String second) {
String res = first;
for (int i = 0; i < Integer.parseInt(second) - 1; i++) {
res = Sum(res, first);
}
return res;
}
@Override
public String Difference(String first, String second) {
if (first.contains(second)) {
return (first).replace(second, "");
}
return first;
}
@Override
public String Contains(String first, String second) {
if ((first).contains(second)) {
return "true";
} else{
return "false";
}
}
}

View File

@ -1,55 +0,0 @@
package com.LabWork.app.calc.service;
import com.LabWork.app.calc.domain.IMethods;
import com.LabWork.app.calc.domain.MethodInteger;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class MethodService {
private final ApplicationContext applicationContext;
public MethodService(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public String Sum(Object first, Object second, String type) {
final IMethods method = (IMethods) applicationContext.getBean(type);
if (method instanceof MethodInteger) {
return String.format("%s", method.Sum(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
else {
return String.format("%s", method.Sum(first, second));
}
}
public String Multiplication(Object first, Object second, String type) {
final IMethods method = (IMethods) applicationContext.getBean(type);
if (method instanceof MethodInteger) {
return String.format("%s", method.Multiplication(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
else {
return String.format("%s", method.Multiplication(first, second));
}
}
public String Difference(Object first, Object second, String type) {
final IMethods method = (IMethods) applicationContext.getBean(type);
if (method instanceof MethodInteger) {
return String.format("%s", method.Difference(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
else {
return String.format("%s", method.Difference(first, second));
}
}
public String Contains(Object first, Object second, String type) {
final IMethods method = (IMethods) applicationContext.getBean(type);
if (method instanceof MethodInteger) {
return String.format("%s", method.Contains(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
else {
return String.format("%s", method.Contains(first, second));
}
}
}

View File

@ -0,0 +1,72 @@
package com.LabWork.app.student.model;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
public class Creator {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String creatorName;
@Column
private String hashedPassword;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "creator")
private List<Manga> mangs;
public Creator() {
}
public Creator(String creatorName, String hashedPassword) {
this.creatorName = creatorName;
this.hashedPassword = hashedPassword;
this.mangs = new ArrayList<>();
}
public Long getId() {
return id;
}
public List<Manga> getMangs() {
return mangs;
}
public String getCreatorName() {
return creatorName;
}
public String getHashedPassword() {
return hashedPassword;
}
public void setMangs(List<Manga> mangs) {
this.mangs = mangs;
}
public void setCreatorName(String creatorName) {
this.creatorName = creatorName;
}
public void setHashedPassword(String hashedPassword) {
this.hashedPassword = hashedPassword;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Creator creator = (Creator) o;
return Objects.equals(id, creator.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}

View File

@ -0,0 +1,65 @@
package com.LabWork.app.student.service;
import com.LabWork.app.student.model.Creator;
import com.LabWork.app.student.model.Manga;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
@Service
public class CreatorService {
@PersistenceContext
private EntityManager em;
@Transactional
public Creator findCreator(Long id) {
final Creator creator = em.find(Creator.class, id);
if (creator == null) {
throw new EntityNotFoundException(String.format("Creator with id [%s] is not found", id));
}
return creator;
}
@Transactional
public List<Creator> findAllCreators() {
return em.createQuery("select c from Creator c", Creator.class).getResultList();
}
@Transactional
public Creator addCreator(String username, String password) {
if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
throw new IllegalArgumentException("Creator's username or password is empty");
}
final Creator creator = new Creator(username, password);
em.persist(creator);
return creator;
}
@Transactional
public Creator updateCreator(Long id, String username, String password) {
if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
throw new IllegalArgumentException("Creator's username or password is empty");
}
final Creator customer = findCreator(id);
customer.setCreatorName(username);
customer.setHashedPassword(password);
return em.merge(customer);
}
@Transactional
public Creator deleteCreator(Long id) {
final Creator currentCustomer = findCreator(id);
em.remove(currentCustomer);
return currentCustomer;
}
@Transactional
public void deleteAllCreators() {
em.createQuery("delete from Creator").executeUpdate();
}
}

View File

@ -0,0 +1,76 @@
package com.LabWork.app.student.service;
import com.LabWork.app.student.model.Creator;
import com.LabWork.app.student.model.Manga;
import com.LabWork.app.student.model.User;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
@Service
public class MangaService {
@PersistenceContext
private EntityManager em;
@Transactional
public Manga findManga(Long id) {
final Manga manga = em.find(Manga.class, id);
if (manga == null) {
throw new EntityNotFoundException(String.format("Manga with id [%s] is not found", id));
}
return manga;
}
@Transactional
public List<Manga> findAllMangs() {
return em.createQuery("select c from Manga c", Manga.class).getResultList();
}
@Transactional
public Manga addManga(Creator creator, Integer chapterCount, String mangaName) {
if (creator == null) {
throw new IllegalArgumentException("Invalid creator");
}
if (chapterCount < 0 || chapterCount == null) {
throw new IllegalArgumentException("Invalid chapterCount");
}
if (!StringUtils.hasText(mangaName)) {
throw new IllegalArgumentException("Invalid mangaName");
}
final Manga manga = new Manga(chapterCount, mangaName, creator);
manga.getCreator().getMangs().add(manga);
em.persist(manga);
return manga;
}
@Transactional
public Manga updateManga(Long id, Integer chapterCount) {
if (chapterCount < 0 || chapterCount == null) {
throw new IllegalArgumentException("Invalid chapterCount");
}
final Manga manga = findManga(id);
manga.setChapterCount(chapterCount);
return em.merge(manga);
}
@Transactional
public Manga deleteManga(Long id) {
final Manga currentManga = findManga(id);
List<User> userList= currentManga.getUsers();
for (User user : userList) {
user.getMangs().remove(currentManga);
}
em.remove(currentManga);
return currentManga;
}
@Transactional
public void deleteAllMangs() {
em.createQuery("delete from Manga").executeUpdate();
}
}

View File

@ -0,0 +1,64 @@
package com.LabWork.app.student.service;
import com.LabWork.app.student.model.User;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
@Service
public class UserService {
@PersistenceContext
private EntityManager em;
@Transactional
public User findUser(Long id) {
final User user = em.find(User.class, id);
if (user == null) {
throw new EntityNotFoundException(String.format("User with id [%s] is not found", id));
}
return user;
}
@Transactional
public List<User> findAllUsers() {
return em.createQuery("select c from User c", User.class).getResultList();
}
@Transactional
public User addUser(String username, String password) {
if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
throw new IllegalArgumentException("Customer's username or password is empty");
}
final User user = new User(username, password);
em.persist(user);
return user;
}
@Transactional
public User updateUser(Long id, String username, String password) {
if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
throw new IllegalArgumentException("Customer's username or password is empty");
}
final User user = findUser(id);
user.setUserName(username);
user.setHashedPassword(password);
return em.merge(user);
}
@Transactional
public User deleteUser(Long id) {
final User currentCustomer = findUser(id);
em.remove(currentCustomer);
return currentCustomer;
}
@Transactional
public void deleteAllUsers() {
em.createQuery("delete from User").executeUpdate();
}
}

View File

@ -1 +1,11 @@
spring.main.banner-mode=off
#server.port=8080
spring.datasource.url=jdbc:h2:file:./data
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

View File

@ -1,6 +1,6 @@
package com.LabWork.app;
import com.LabWork.app.calc.service.MethodService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@ -9,7 +9,7 @@ import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class AppApplicationTests {
@Autowired
/* @Autowired
MethodService methodService;
@Test
@ -58,5 +58,5 @@ class AppApplicationTests {
void testMethodContainsString() {
final String res = methodService.Contains("1", "2", "string");
Assertions.assertEquals("false", res);
}
}*/
}

View File

@ -0,0 +1,123 @@
package com.LabWork.app;
import com.LabWork.app.student.model.Creator;
import com.LabWork.app.student.model.Manga;
import com.LabWork.app.student.service.CreatorService;
import com.LabWork.app.student.service.MangaService;
import com.LabWork.app.student.service.UserService;
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 ReMangaTest {
@Autowired
CreatorService creatorService;
@Autowired
MangaService mangaService;
@Autowired
UserService userService;
@Test
void testCreator() {
creatorService.deleteAllCreators();
mangaService.deleteAllMangs();
userService.deleteAllUsers();
Creator c1 = creatorService.addCreator("first", "1");
Creator c2 = creatorService.addCreator("second", "2");
Creator c3 = creatorService.addCreator("third", "3");
Assertions.assertEquals("first", c1.getCreatorName());
Assertions.assertEquals("second", c2.getCreatorName());
Assertions.assertEquals("third", c3.getCreatorName());
Assertions.assertEquals(c1, creatorService.findCreator(c1.getId()));
creatorService.deleteCreator(c2.getId());
Assertions.assertEquals(2, creatorService.findAllCreators().size());
Creator c4 = creatorService.updateCreator(c3.getId(), "fourth", "4");
Assertions.assertNotEquals(c3.getCreatorName(), c4.getCreatorName());
Assertions.assertNotEquals(c3.getHashedPassword(), c4.getHashedPassword());
creatorService.deleteAllCreators();
mangaService.deleteAllMangs();
userService.deleteAllUsers();
}
@Test
void testManga() {
creatorService.deleteAllCreators();
mangaService.deleteAllMangs();
userService.deleteAllUsers();
Creator c1 = creatorService.addCreator("first", "1");
Creator c2 = creatorService.addCreator("second", "2");
Manga p1 = mangaService.addManga(c1, 0, "Vagrant");
Manga p2 = mangaService.addManga(c2, 10, "Berserk");
Assertions.assertEquals(2, mangaService.findAllMangs().size());
Assertions.assertEquals(p1.getCreator(), c1);
Assertions.assertEquals(p2.getCreator(), c2);
Assertions.assertEquals(c1.getMangs().get(0), p1);
Assertions.assertEquals(c2.getMangs().get(0), p2);
Assertions.assertEquals(p1, mangaService.findManga(p1.getId()));
Assertions.assertEquals(p2, mangaService.findManga(p2.getId()));
Manga p3 = mangaService.addManga(c1, 10, "Solo Leveling");
mangaService.deleteManga(p1.getId());
Assertions.assertEquals(1, creatorService.findCreator(c1.getId()).getMangs().size());
Manga p4 = mangaService.updateManga(p2.getId(), 100);
Assertions.assertNotEquals(p2.getChapterCount(), p4.getChapterCount());
creatorService.deleteAllCreators();
mangaService.deleteAllMangs();
userService.deleteAllUsers();
}
/* @Test
void testUser() {
creatorService.deleteAllCreators();
mangaService.deleteAllMangs();
userService.deleteAllUsers();
Creator c1 = creatorService.addCreator("first", "1");
Creator c2 = creatorService.addCreator("second", "2");
Post p1 = postService.addPost(c1, "first title", "nonsense");
Post p2 = postService.addPost(c2, "second title", "ordinal");
Assertions.assertEquals(2, postService.findAllPosts().size());
User com1 = commentService.addComment(c1, p2, "What");
User 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());
creatorService.deleteAllCreators();
mangaService.deleteAllMangs();
userService.deleteAllUsers();
}*/
}

View File

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