diff --git a/lab2/src/main/java/com/example/demo/DemoApplication.java b/lab2/src/main/java/com/example/demo/DemoApplication.java index 33c7203..aec787a 100644 --- a/lab2/src/main/java/com/example/demo/DemoApplication.java +++ b/lab2/src/main/java/com/example/demo/DemoApplication.java @@ -1,7 +1,9 @@ package com.example.demo; import java.util.Objects; +import java.util.Arrays; import java.util.Date; +import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -9,8 +11,13 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import com.example.demo.comment.model.CommentEntity; +import com.example.demo.comment.service.CommentService; import com.example.demo.news.model.NewEntity; import com.example.demo.news.service.NewService; +import com.example.demo.tage.model.TageEntity; +import com.example.demo.tage.service.TageService; +import com.example.demo.users.model.UserEntity; import com.example.demo.users.service.UserService; @SpringBootApplication @@ -19,10 +26,14 @@ public class DemoApplication implements CommandLineRunner { private final NewService newService; private final UserService userService; + private final CommentService commentService; + private final TageService tageService; - public DemoApplication(NewService newService, UserService userService) { + public DemoApplication(NewService newService, UserService userService, CommentService commentService, TageService tageService) { this.newService = newService; this.userService = userService; + this.commentService = commentService; + this.tageService = tageService; } public static void main(String[] args) { @@ -31,15 +42,31 @@ public class DemoApplication implements CommandLineRunner { @Override public void run(String... args) throws Exception { - if (args.length > 0 && Objects.equals("--populate", args[0])) { + // if (args.length > 0 && Objects.equals("--populate", args[0])) { log.info("Create default news values"); - newService.create(new NewEntity(null, "День знаний", new Date(2023 - 9 - 01), "праздник", - "Университет торжественно отпраздновал День Знаний. На мероприятии присутствовало более 900 абитуриентов.")); - newService.create( - new NewEntity(null, "Чемпионат ICPC", new Date(2023 - 10 - 11), "соревнование программирование", - "Студенты выступят на Чемпионате мира по программированию в Северной Евразии.")); - newService.create(new NewEntity(null, "Новый год 2024", new Date(2024 - 1 - 01), "праздник", - "Администрация ulstu поздравляет студентов и преподавателей с Новым годом и желает крепкого здоровья и успешного Года Дракона!")); - } + + // tage + final var tage1 = tageService.create(new TageEntity(null, "праздник")); + final var tage2 = tageService.create(new TageEntity(null, "сессия")); + final var tage3 = tageService.create(new TageEntity(null, "конкурс")); + final var tage4 = tageService.create(new TageEntity(null, "программирование")); + + // new + final NewEntity new1 = newService.create(new NewEntity(null, "Чемпионат ICPC", new Date(), Arrays.asList(tage1, tage2), + "Студенты выступят на Чемпионате мира по программированию в Северной Евразии.")); + final NewEntity new2 = newService.create(new NewEntity(null, "Новый год 2024", new Date(2024 - 1 - 01), Arrays.asList(tage3, tage4), + "Администрация ulstu поздравляет студентов и преподавателей с Новым годом и желает крепкого здоровья и успешного Года Дракона!")); + + + // user + final var user1 = userService.create(new UserEntity(null, "beko", "111", "ddwwdd", + "beko@mail.ru", new Date())); + final var user2 = userService.create(new UserEntity(null, "rara", "rara", "dererere", + "rara@mail.ru", new Date())); + + // comment + commentService.create(new CommentEntity(null, new Date(2024-4-04), "очень интересно", new1, user1)); + commentService.create(new CommentEntity(null, new Date(2024-4-04), "очень интересно", new2, user2)); + // } } } diff --git a/lab2/src/main/java/com/example/demo/comment/api/CommentController.java b/lab2/src/main/java/com/example/demo/comment/api/CommentController.java new file mode 100644 index 0000000..a5617de --- /dev/null +++ b/lab2/src/main/java/com/example/demo/comment/api/CommentController.java @@ -0,0 +1,70 @@ +package com.example.demo.comment.api; + +import java.util.List; + +import org.modelmapper.ModelMapper; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.example.demo.core.configuration.Constants; +import com.example.demo.news.service.NewService; +import com.example.demo.comment.model.CommentEntity; +import com.example.demo.comment.service.CommentService; + +import jakarta.validation.Valid; + +@RestController +@RequestMapping(Constants.API_URL + "/comment") +public class CommentController { + private final CommentService commentService; + private final NewService newService; + private final ModelMapper modelMapper; + + public CommentController(CommentService commentService, NewService newService, ModelMapper modelMapper) { + this.commentService = commentService; + this.newService = newService; + this.modelMapper = modelMapper; + } + + private CommentDto toDto(CommentEntity entity) { + return modelMapper.map(entity, CommentDto.class); + } + + private CommentEntity toEntity(CommentDto dto) { + final var entity = modelMapper.map(dto, CommentEntity.class); + entity.setNew(newService.get(dto.getId())); + return entity; + } + + @GetMapping + public List getAll(@RequestParam(name = "newId", defaultValue = "0") Long newId) { + return commentService.getAll(newId).stream().map(this::toDto).toList(); + } + + @GetMapping("/{id}") + public CommentDto get(@PathVariable(name = "id") Long id) { + return toDto(commentService.get(id)); + } + + @PostMapping + public CommentDto create(@RequestBody @Valid CommentDto dto) { + return toDto(commentService.create(toEntity(dto))); + } + + @PutMapping("/{id}") + public CommentDto update(@PathVariable(name = "id") Long id, @RequestBody CommentDto dto) { + return toDto(commentService.update(id, toEntity(dto))); + } + + @DeleteMapping("/{id}") + public CommentDto delete(@PathVariable(name = "id") Long id) { + return toDto(commentService.delete(id)); + } +} diff --git a/lab2/src/main/java/com/example/demo/comment/api/CommentDto.java b/lab2/src/main/java/com/example/demo/comment/api/CommentDto.java new file mode 100644 index 0000000..fbcb0c7 --- /dev/null +++ b/lab2/src/main/java/com/example/demo/comment/api/CommentDto.java @@ -0,0 +1,70 @@ +package com.example.demo.comment.api; + +import java.util.Date; + +import com.example.demo.news.model.NewEntity; +import com.example.demo.users.model.UserEntity; +import com.fasterxml.jackson.annotation.JsonProperty; + +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.PastOrPresent; +import jakarta.validation.constraints.Size; + +public class CommentDto { + private Long id; + @NotNull + @PastOrPresent + private Date date; + @NotBlank + @Size(min = 1, max = 200) + private String text; + @NotNull + @Min(1) + private Long userId; + @NotNull + @Min(1) + private Long newsId; + + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public void setNewId(Long newsId) { + this.newsId = newsId; + } + + public Long getNewId() { + return newsId; + } + + public void setUserId(Long userId) { + this.userId = userId; + } + + public Long getUserId() { + return userId; + } +} diff --git a/lab2/src/main/java/com/example/demo/comment/model/CommentEntity.java b/lab2/src/main/java/com/example/demo/comment/model/CommentEntity.java new file mode 100644 index 0000000..c452580 --- /dev/null +++ b/lab2/src/main/java/com/example/demo/comment/model/CommentEntity.java @@ -0,0 +1,78 @@ +package com.example.demo.comment.model; + +import java.util.Date; +import java.util.Objects; + +import com.example.demo.core.model.BaseEntity; +import com.example.demo.news.model.NewEntity; +import com.example.demo.users.model.UserEntity; + +public class CommentEntity extends BaseEntity { + private Date date; + private String text; + private NewEntity news; + private UserEntity user; + + public CommentEntity() { + super(); + } + + public CommentEntity(Long id, Date date, String text, NewEntity news, UserEntity user) { + super(id); + this.date = date; + this.text = text; + this.news = news; + this.user = user; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public void setNew(NewEntity news) { + this.news = news; + } + + public NewEntity getNew() { + return news; + } + + public void setUser(UserEntity user) { + this.user = user; + } + + public UserEntity getUser() { + return user; + } + + @Override + public int hashCode() { + return Objects.hash(id, date, text, user, news); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || getClass() != obj.getClass()) + return false; + final CommentEntity other = (CommentEntity) obj; + return Objects.equals(other.getId(), id) + && Objects.equals(other.getDate(), date) + && Objects.equals(other.getText(), text) + && Objects.equals(other.getNew(), news) + && Objects.equals(other.getUser(), user); + } +} diff --git a/lab2/src/main/java/com/example/demo/comment/repository/CommentRepository.java b/lab2/src/main/java/com/example/demo/comment/repository/CommentRepository.java new file mode 100644 index 0000000..dd09b57 --- /dev/null +++ b/lab2/src/main/java/com/example/demo/comment/repository/CommentRepository.java @@ -0,0 +1,10 @@ +package com.example.demo.comment.repository; + +import org.springframework.stereotype.Repository; + +import com.example.demo.comment.model.CommentEntity; +import com.example.demo.core.repository.MapRepository; + +@Repository +public class CommentRepository extends MapRepository { +} diff --git a/lab2/src/main/java/com/example/demo/comment/service/CommentService.java b/lab2/src/main/java/com/example/demo/comment/service/CommentService.java new file mode 100644 index 0000000..8e93ae2 --- /dev/null +++ b/lab2/src/main/java/com/example/demo/comment/service/CommentService.java @@ -0,0 +1,50 @@ +package com.example.demo.comment.service; + +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +import org.springframework.stereotype.Service; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.comment.model.CommentEntity; +import com.example.demo.comment.repository.CommentRepository; + +@Service +public class CommentService { + private final CommentRepository repository; + + public CommentService(CommentRepository repository) { + this.repository = repository; + } + + public List getAll(Long newId) { + if (Objects.equals(newId, 0L)) { + return repository.getAll(); + } + return repository.getAll().stream() + .filter(item -> item.getNew().getId().equals(newId)) + .toList(); + } + + public CommentEntity get(Long id) { + return Optional.ofNullable(repository.get(id)) + .orElseThrow(() -> new NotFoundException(id)); + } + + public CommentEntity create(CommentEntity entity) { + return repository.create(entity); + } + + public CommentEntity update(Long id, CommentEntity entity) { + final CommentEntity existsEntity = get(id); + existsEntity.setDate(entity.getDate()); + existsEntity.setText(entity.getText()); + return repository.update(existsEntity); + } + + public CommentEntity delete(Long id) { + final CommentEntity existsEntity = get(id); + return repository.delete(existsEntity); + } +} diff --git a/lab2/src/main/java/com/example/demo/news/api/NewController.java b/lab2/src/main/java/com/example/demo/news/api/NewController.java index becb9f9..404e542 100644 --- a/lab2/src/main/java/com/example/demo/news/api/NewController.java +++ b/lab2/src/main/java/com/example/demo/news/api/NewController.java @@ -1,8 +1,11 @@ package com.example.demo.news.api; +import java.util.Arrays; import java.util.List; import org.modelmapper.ModelMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -12,9 +15,12 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import com.example.demo.DemoApplication; import com.example.demo.core.configuration.Constants; import com.example.demo.news.model.NewEntity; import com.example.demo.news.service.NewService; +import com.example.demo.tage.model.TageEntity; +import com.example.demo.tage.service.TageService; import jakarta.validation.Valid; @@ -22,11 +28,14 @@ import jakarta.validation.Valid; @RequestMapping(Constants.API_URL + "/new") public class NewController { private final NewService newService; + private final TageService tageService; private final ModelMapper modelMapper; + private final Logger log = LoggerFactory.getLogger(NewController.class); - public NewController(NewService newService, ModelMapper modelMapper) { + public NewController(NewService newService, TageService tageService, ModelMapper modelMapper) { this.newService = newService; this.modelMapper = modelMapper; + this.tageService = tageService; } private NewDto toDto(NewEntity entity) { @@ -34,7 +43,15 @@ public class NewController { } private NewEntity toEntity(NewDto dto) { - return modelMapper.map(dto, NewEntity.class); + final var entity = modelMapper.map(dto, NewEntity.class); + List tagesId = dto.getTagesId(); + List tages = Arrays.asList(); + for (var tageId : tagesId) + { + tages.add(tageService.get(tageId)); + } + entity.setTage(tages); + return entity; } @GetMapping diff --git a/lab2/src/main/java/com/example/demo/news/api/NewDto.java b/lab2/src/main/java/com/example/demo/news/api/NewDto.java index 7a8c46d..3d384a8 100644 --- a/lab2/src/main/java/com/example/demo/news/api/NewDto.java +++ b/lab2/src/main/java/com/example/demo/news/api/NewDto.java @@ -6,6 +6,7 @@ import java.util.List; import com.example.demo.tage.model.TageEntity; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.PastOrPresent; @@ -19,7 +20,7 @@ public class NewDto { @NotNull @PastOrPresent private Date date; - private List tages; + private List tagesId; @NotBlank @Size(min = 1, max = 30) private String text; @@ -49,12 +50,12 @@ public class NewDto { this.date = date; } - public List getTage() { - return tages; + public List getTagesId() { + return tagesId; } - public void setTage(List tages) { - this.tages = tages; + public void setTagesId(List tagesId) { + this.tagesId = tagesId; } public String getText() { diff --git a/lab2/src/main/java/com/example/demo/news/model/NewEntity.java b/lab2/src/main/java/com/example/demo/news/model/NewEntity.java index 990f786..ae53cd7 100644 --- a/lab2/src/main/java/com/example/demo/news/model/NewEntity.java +++ b/lab2/src/main/java/com/example/demo/news/model/NewEntity.java @@ -2,9 +2,7 @@ package com.example.demo.news.model; import java.util.Date; import java.util.List; -import java.util.Map; import java.util.Objects; -import java.util.TreeMap; import com.example.demo.core.model.BaseEntity; import com.example.demo.tage.model.TageEntity; diff --git a/lab2/src/main/java/com/example/demo/tage/api/TageController.java b/lab2/src/main/java/com/example/demo/tage/api/TageController.java new file mode 100644 index 0000000..59b861f --- /dev/null +++ b/lab2/src/main/java/com/example/demo/tage/api/TageController.java @@ -0,0 +1,64 @@ +package com.example.demo.tage.api; + +import java.util.List; + +import org.modelmapper.ModelMapper; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.example.demo.core.configuration.Constants; +import com.example.demo.tage.model.TageEntity; +import com.example.demo.tage.service.TageService; + +import jakarta.validation.Valid; + +@RestController +@RequestMapping(Constants.API_URL + "/tage") +public class TageController { + private final TageService tageService; + private final ModelMapper modelMapper; + + public TageController(TageService tageService, ModelMapper modelMapper) { + this.tageService = tageService; + this.modelMapper = modelMapper; + } + + private TageDto toDto(TageEntity entity) { + return modelMapper.map(entity, TageDto.class); + } + + private TageEntity toEntity(TageDto dto) { + return modelMapper.map(dto, TageEntity.class); + } + + @GetMapping + public List getAll() { + return tageService.getAll().stream().map(this::toDto).toList(); + } + + @GetMapping("/{id}") + public TageDto get(@PathVariable(name = "id") Long id) { + return toDto(tageService.get(id)); + } + + @PostMapping + public TageDto create(@RequestBody @Valid TageDto dto) { + return toDto(tageService.create(toEntity(dto))); + } + + @PutMapping("/{id}") + public TageDto update(@PathVariable(name = "id") Long id, @RequestBody TageDto dto) { + return toDto(tageService.update(id, toEntity(dto))); + } + + @DeleteMapping("/{id}") + public TageDto delete(@PathVariable(name = "id") Long id) { + return toDto(tageService.delete(id)); + } +} diff --git a/lab2/src/main/java/com/example/demo/tage/api/TageDto.java b/lab2/src/main/java/com/example/demo/tage/api/TageDto.java new file mode 100644 index 0000000..3be894b --- /dev/null +++ b/lab2/src/main/java/com/example/demo/tage/api/TageDto.java @@ -0,0 +1,30 @@ +package com.example.demo.tage.api; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; + +public class TageDto { + private Long id; + @NotBlank + @Size(min = 1, max = 30) + private String text; + + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } +} diff --git a/lab2/src/main/java/com/example/demo/tage/model/TageEntity.java b/lab2/src/main/java/com/example/demo/tage/model/TageEntity.java new file mode 100644 index 0000000..3d67f0b --- /dev/null +++ b/lab2/src/main/java/com/example/demo/tage/model/TageEntity.java @@ -0,0 +1,43 @@ +package com.example.demo.tage.model; + +import java.util.Objects; + +import com.example.demo.core.model.BaseEntity; +import com.example.demo.news.model.NewEntity; + +public class TageEntity extends BaseEntity{ + private String text; + + public TageEntity() { + super(); + } + + public TageEntity(Long id, String text) { + super(id); + this.text = text; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + @Override + public int hashCode() { + return Objects.hash(id, text); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || getClass() != obj.getClass()) + return false; + final NewEntity other = (NewEntity) obj; + return Objects.equals(other.getId(), id) + && Objects.equals(other.getText(), text); + } +} diff --git a/lab2/src/main/java/com/example/demo/tage/repository/TageRepository.java b/lab2/src/main/java/com/example/demo/tage/repository/TageRepository.java new file mode 100644 index 0000000..3dad637 --- /dev/null +++ b/lab2/src/main/java/com/example/demo/tage/repository/TageRepository.java @@ -0,0 +1,10 @@ +package com.example.demo.tage.repository; + +import org.springframework.stereotype.Repository; + +import com.example.demo.core.repository.MapRepository; +import com.example.demo.tage.model.TageEntity; + +@Repository +public class TageRepository extends MapRepository { +} diff --git a/lab2/src/main/java/com/example/demo/tage/service/TageService.java b/lab2/src/main/java/com/example/demo/tage/service/TageService.java new file mode 100644 index 0000000..ce24920 --- /dev/null +++ b/lab2/src/main/java/com/example/demo/tage/service/TageService.java @@ -0,0 +1,43 @@ +package com.example.demo.tage.service; + +import java.util.List; +import java.util.Optional; + +import org.springframework.stereotype.Service; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.tage.model.TageEntity; +import com.example.demo.tage.repository.TageRepository; + +@Service +public class TageService { + private final TageRepository repository; + + public TageService(TageRepository repository) { + this.repository = repository; + } + + public List getAll() { + return repository.getAll(); + } + + public TageEntity get(Long id) { + return Optional.ofNullable(repository.get(id)) + .orElseThrow(() -> new NotFoundException(id)); + } + + public TageEntity create(TageEntity entity) { + return repository.create(entity); + } + + public TageEntity update(Long id, TageEntity entity) { + final TageEntity existsEntity = get(id); + existsEntity.setText(entity.getText()); + return repository.update(existsEntity); + } + + public TageEntity delete(Long id) { + final TageEntity existsEntity = get(id); + return repository.delete(existsEntity); + } +} diff --git a/lab2/src/test/java/com/example/demo/CommentServiceTest.java b/lab2/src/test/java/com/example/demo/CommentServiceTest.java new file mode 100644 index 0000000..76d2299 --- /dev/null +++ b/lab2/src/test/java/com/example/demo/CommentServiceTest.java @@ -0,0 +1,51 @@ +package com.example.demo; + +import java.util.Arrays; +import java.util.Date; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import com.example.demo.comment.model.CommentEntity; +import com.example.demo.comment.service.CommentService; +import com.example.demo.core.error.NotFoundException; +import com.example.demo.news.model.NewEntity; +import com.example.demo.news.service.NewService; +import com.example.demo.tage.model.TageEntity; +import com.example.demo.tage.service.TageService; +import com.example.demo.users.model.UserEntity; +import com.example.demo.users.service.UserService; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +public class CommentServiceTest { + + @Autowired + private CommentService commentService; + private NewService newService; + private UserService userService; + private TageService tageService; + + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> commentService.get(0L)); + } + + @Test + void createTest() { + var tage1 = tageService.create(new TageEntity(null, "праздник")); + + NewEntity new1 = newService.create(new NewEntity(null, "Чемпионат ICPC", new Date(), Arrays.asList(tage1), + "Студенты выступят на Чемпионате мира по программированию в Северной Евразии.")); + var user1 = userService.create(new UserEntity(null, "beko", "111", "ddwwdd", + "beko@mail.ru", new Date())); + commentService.create(new CommentEntity(null, new Date(), "как красиво весной", new1, user1)); + var last = commentService.create(new CommentEntity(null, new Date(), "и ещё птицы поют", new1, user1)); + Assertions.assertEquals(2L, commentService.getAll(null).size()); + Assertions.assertEquals(last, commentService.get(2L)); + } +} diff --git a/lab2/src/test/java/com/example/demo/NewsServiceTest.java b/lab2/src/test/java/com/example/demo/NewsServiceTest.java index 8c880df..360ec00 100644 --- a/lab2/src/test/java/com/example/demo/NewsServiceTest.java +++ b/lab2/src/test/java/com/example/demo/NewsServiceTest.java @@ -1,6 +1,9 @@ package com.example.demo; +import java.util.Arrays; import java.util.Date; +import java.util.List; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import org.junit.jupiter.api.Test; @@ -11,6 +14,7 @@ import org.springframework.boot.test.context.SpringBootTest; import com.example.demo.core.error.NotFoundException; import com.example.demo.news.model.NewEntity; import com.example.demo.news.service.NewService; +import com.example.demo.tage.model.TageEntity;; @SpringBootTest @TestMethodOrder(OrderAnnotation.class) @@ -25,10 +29,16 @@ class NewsServiceTest { @Test void createTest() { - newsService.create(new NewEntity(null, "Название", new Date(), "тэг", "текст новости")); - final NewEntity last = newsService - .create(new NewEntity(null, "9 апреля", new Date(), "весна", "просто наступил апрель")); - Assertions.assertEquals(2L, newsService.getAll().size()); - Assertions.assertEquals(last, newsService.get(2L)); + List list1 = Arrays.asList + (new TageEntity(null, "праздник"), new TageEntity(null, "сессия")); + List list2 = Arrays.asList + (new TageEntity(null, "конкурс"), new TageEntity(null, "программирование")); + + newsService.create(new NewEntity(null, "Название", new Date(), list1, "текст новости")); + final NewEntity last = newsService.create + (new NewEntity(null, "9 апреля", new Date(), list2, "просто наступил апрель")); + + Assertions.assertEquals(4L, newsService.getAll().size()); + Assertions.assertEquals(last, newsService.get(4L)); } } \ No newline at end of file diff --git a/lab2/src/test/java/com/example/demo/TageServiceTest.java b/lab2/src/test/java/com/example/demo/TageServiceTest.java new file mode 100644 index 0000000..01df544 --- /dev/null +++ b/lab2/src/test/java/com/example/demo/TageServiceTest.java @@ -0,0 +1,33 @@ +package com.example.demo; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.tage.model.TageEntity; +import com.example.demo.tage.service.TageService; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +public class TageServiceTest { + @Autowired + private TageService tageService; + + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> tageService.get(0L)); + } + + @Test + void createTest() { + tageService.create(new TageEntity(null, "праздник")); + final var last = tageService.create(new TageEntity(null, "конкурс")); + + Assertions.assertEquals(2L, tageService.getAll().size()); + Assertions.assertEquals(last, tageService.get(2L)); + } +}