diff --git a/.vscode/settings.json b/.vscode/settings.json index e012065..92b13ab 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,5 @@ { "java.compile.nullAnalysis.mode": "automatic", - "java.configuration.updateBuildConfiguration": "interactive" + "java.configuration.updateBuildConfiguration": "interactive", + "java.dependency.packagePresentation": "hierarchical" } \ No newline at end of file diff --git a/demo/build.gradle b/demo/build.gradle index b7867b9..92a39ba 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -17,6 +17,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0' implementation 'org.modelmapper:modelmapper:3.2.0' diff --git a/demo/src/main/java/com/example/demo/groups/api/GroupController.java b/demo/src/main/java/com/example/demo/groups/api/GroupController.java index f3624ea..98105fe 100644 --- a/demo/src/main/java/com/example/demo/groups/api/GroupController.java +++ b/demo/src/main/java/com/example/demo/groups/api/GroupController.java @@ -65,7 +65,7 @@ public class GroupController { } @DeleteMapping("/{id}") - public GroupDto delete(@PathVariable(name = "id") Long id) { + public GroupDto delete(@PathVariable(name = "id") @Valid Long id) { return toDto(groupService.delete(id)); } } diff --git a/demo/src/main/java/com/example/demo/groups/service/GroupService.java b/demo/src/main/java/com/example/demo/groups/service/GroupService.java index 4d06899..1198001 100644 --- a/demo/src/main/java/com/example/demo/groups/service/GroupService.java +++ b/demo/src/main/java/com/example/demo/groups/service/GroupService.java @@ -48,4 +48,8 @@ public class GroupService { final GroupEntity existsEntity = get(id); return repository.delete(existsEntity); } + + public void clear(){ + repository.deleteAll(); + } } diff --git a/demo/src/main/java/com/example/demo/members/api/MemberController.java b/demo/src/main/java/com/example/demo/members/api/MemberController.java index 0f4f76e..13e1b63 100644 --- a/demo/src/main/java/com/example/demo/members/api/MemberController.java +++ b/demo/src/main/java/com/example/demo/members/api/MemberController.java @@ -64,7 +64,7 @@ public class MemberController { } @DeleteMapping("/{id}") - public MemberDto delete(@PathVariable(name = "id") Long id) { + public MemberDto delete(@PathVariable(name = "id") @Valid Long id) { return toDto(memberService.delete(id)); } } diff --git a/demo/src/main/java/com/example/demo/members/service/MemberService.java b/demo/src/main/java/com/example/demo/members/service/MemberService.java index 5443d30..6bae3c8 100644 --- a/demo/src/main/java/com/example/demo/members/service/MemberService.java +++ b/demo/src/main/java/com/example/demo/members/service/MemberService.java @@ -49,4 +49,8 @@ public class MemberService { final MemberEntity existsEntity = get(id); return repository.delete(existsEntity); } + + public void clear(){ + repository.deleteAll(); + } } diff --git a/demo/src/main/java/com/example/demo/users/api/UserController.java b/demo/src/main/java/com/example/demo/users/api/UserController.java index 1d00e35..31a5e18 100644 --- a/demo/src/main/java/com/example/demo/users/api/UserController.java +++ b/demo/src/main/java/com/example/demo/users/api/UserController.java @@ -58,7 +58,7 @@ public class UserController { } @DeleteMapping("/{id}") - public UserDto delete(@PathVariable(name = "id") Long id) { + public UserDto delete(@PathVariable(name = "id") @Valid Long id) { return toDto(userService.delete(id)); } } diff --git a/demo/src/main/java/com/example/demo/users/model/UserEntity.java b/demo/src/main/java/com/example/demo/users/model/UserEntity.java index 3a1ee38..d1947c4 100644 --- a/demo/src/main/java/com/example/demo/users/model/UserEntity.java +++ b/demo/src/main/java/com/example/demo/users/model/UserEntity.java @@ -7,7 +7,9 @@ import com.example.demo.core.model.BaseEntity; import jakarta.validation.OverridesAttribute; public class UserEntity extends BaseEntity{ - private String handle, email, password; + private String handle; + private String email; + private String password; public UserEntity(){ super(); diff --git a/demo/src/main/java/com/example/demo/users/service/UserService.java b/demo/src/main/java/com/example/demo/users/service/UserService.java index 44559bc..1c2453a 100644 --- a/demo/src/main/java/com/example/demo/users/service/UserService.java +++ b/demo/src/main/java/com/example/demo/users/service/UserService.java @@ -43,4 +43,8 @@ public class UserService { final UserEntity existsEntity = get(id); return repository.delete(existsEntity); } + + public void clear(){ + repository.deleteAll(); + } } diff --git a/demo/src/test/java/com/example/demo/DemoApplicationTests.java b/demo/src/test/java/com/example/demo/DemoApplicationTests.java deleted file mode 100644 index 2778a6a..0000000 --- a/demo/src/test/java/com/example/demo/DemoApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.example.demo; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class DemoApplicationTests { - - @Test - void contextLoads() { - } - -} diff --git a/demo/src/test/java/com/example/demo/GroupServiceTests.java b/demo/src/test/java/com/example/demo/GroupServiceTests.java new file mode 100644 index 0000000..bba8a53 --- /dev/null +++ b/demo/src/test/java/com/example/demo/GroupServiceTests.java @@ -0,0 +1,69 @@ +package com.example.demo; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.security.SecurityProperties.User; +import org.springframework.boot.test.context.SpringBootTest; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.groups.model.GroupEntity; +import com.example.demo.groups.service.GroupService; +import com.example.demo.users.model.UserEntity; +import com.example.demo.users.service.UserService; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +class GroupServiceTests { + @Autowired + private UserService userService; + @Autowired + private GroupService groupService; + + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> groupService.get(0L)); + } + + @Test + @Order(1) + void createTest() { + var user = userService.create(new UserEntity(null, "Oleja123", "bebrus@mail.ru", "1234")); + groupService.create(new GroupEntity(null, user, "1")); + final GroupEntity last = groupService.create(new GroupEntity(null, user, "2")); + Assertions.assertEquals(2, groupService.getAll(1L).size()); + Assertions.assertEquals(last, groupService.get(2L)); + } + + @Test + @Order(2) + void updateTest() { + final String test = "TEST"; + final GroupEntity entity = groupService.get(2L); + final String oldName = entity.getName(); + final GroupEntity newEntity = groupService.update(2L, new GroupEntity(null, userService.get(1L), test)); + Assertions.assertEquals(2, groupService.getAll(1L).size()); + Assertions.assertEquals(newEntity, groupService.get(2L)); + Assertions.assertNotEquals(oldName, newEntity.getName()); + + } + + @Test + @Order(3) + void deleteTest() { + final String test = "TEST"; + groupService.delete(2L); + Assertions.assertEquals(1, groupService.getAll(1L).size()); + final GroupEntity last = groupService.get(1L); + Assertions.assertEquals(1L, last.getId()); + + final GroupEntity newEntity = groupService.create(new GroupEntity(null, userService.get(1L), test)); + Assertions.assertEquals(2, groupService.getAll(1L).size()); + Assertions.assertEquals(3, newEntity.getId()); + userService.clear(); + groupService.clear(); + } +} \ No newline at end of file diff --git a/demo/src/test/java/com/example/demo/MemberServiceTests.java b/demo/src/test/java/com/example/demo/MemberServiceTests.java new file mode 100644 index 0000000..46ed52e --- /dev/null +++ b/demo/src/test/java/com/example/demo/MemberServiceTests.java @@ -0,0 +1,79 @@ +package com.example.demo; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.security.SecurityProperties.User; +import org.springframework.boot.test.context.SpringBootTest; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.groups.model.GroupEntity; +import com.example.demo.groups.service.GroupService; +import com.example.demo.members.model.MemberEntity; +import com.example.demo.members.service.MemberService; +import com.example.demo.users.model.UserEntity; +import com.example.demo.users.service.UserService; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +class MemberServiceTests { + @Autowired + private UserService userService; + @Autowired + private GroupService groupService; + @Autowired + private MemberService memberService; + + + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> memberService.get(0L)); + } + + @Test + @Order(1) + void createTest() { + var user = userService.create(new UserEntity(null, "Oleja123", "bebrus@mail.ru", "1234")); + var group = groupService.create(new GroupEntity(null, user, "1")); + memberService.create(new MemberEntity(null, group, "1", "1", "1")); + var last = memberService.create(new MemberEntity(null, group, "2", "2", "2")); + Assertions.assertEquals(2, memberService.getAll(1L).size()); + Assertions.assertEquals(last, memberService.get(2L)); + } + + @Test + @Order(2) + void updateTest() { + final String test = "TEST"; + final MemberEntity entity = memberService.get(2L); + final String oldName = entity.getName(); + final String oldHandle = entity.getHandle(); + final String oldRank = entity.getRank(); + final MemberEntity newEntity = memberService.update(2L, new MemberEntity(null, groupService.get(1L), test,test,test)); + Assertions.assertEquals(2, memberService.getAll(1L).size()); + Assertions.assertEquals(newEntity, memberService.get(2L)); + Assertions.assertNotEquals(oldName, newEntity.getName()); + Assertions.assertNotEquals(oldHandle, newEntity.getHandle()); + Assertions.assertNotEquals(oldRank, newEntity.getRank()); + } + + @Test + @Order(3) + void deleteTest() { + final String test = "TEST"; + memberService.delete(2L); + Assertions.assertEquals(1, memberService.getAll(1L).size()); + final MemberEntity last = memberService.get(1L); + Assertions.assertEquals(1L, last.getId()); + + final MemberEntity newEntity = memberService.create(new MemberEntity(null, groupService.get(1L), test,test,test)); + Assertions.assertEquals(2, memberService.getAll(1L).size()); + Assertions.assertEquals(3, newEntity.getId()); + userService.clear(); + groupService.clear(); + memberService.clear(); + } +} \ No newline at end of file diff --git a/demo/src/test/java/com/example/demo/UserServiceTests.java b/demo/src/test/java/com/example/demo/UserServiceTests.java new file mode 100644 index 0000000..ba70209 --- /dev/null +++ b/demo/src/test/java/com/example/demo/UserServiceTests.java @@ -0,0 +1,71 @@ +package com.example.demo; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.security.SecurityProperties.User; +import org.springframework.boot.test.context.SpringBootTest; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.users.model.UserEntity; +import com.example.demo.users.service.UserService; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +class UserServiceTests { + @Autowired + private UserService userService; + + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> userService.get(0L)); + } + + @Test + @Order(1) + void createTest() { + userService.create(new UserEntity(null, "Oleja123", "bebrus@mail.ru", "1234")); + userService.create(new UserEntity(null, "Vk1d2004", "bebrus2@mail.ru", "12345")); + final UserEntity last = userService.create(new UserEntity(null, "Cat_cheshir", "cat@mail.ru", "1234")); + Assertions.assertEquals(3, userService.getAll().size()); + Assertions.assertEquals(last, userService.get(3L)); + } + + @Test + @Order(2) + void updateTest() { + final String test = "TEST"; + final UserEntity entity = userService.get(3L); + final String oldEmail = entity.getEmail(); + final String oldHandle = entity.getHandle(); + final String oldPassword = entity.getPassword(); + final UserEntity newEntity = userService.update(3L, new UserEntity(null, test, test, test)); + Assertions.assertEquals(3, userService.getAll().size()); + Assertions.assertEquals(newEntity, userService.get(3L)); + Assertions.assertEquals(test, newEntity.getEmail()); + Assertions.assertEquals(test, newEntity.getHandle()); + Assertions.assertEquals(test, newEntity.getPassword()); + Assertions.assertNotEquals(oldEmail, newEntity.getEmail()); + Assertions.assertNotEquals(oldHandle, newEntity.getHandle()); + Assertions.assertNotEquals(oldPassword, newEntity.getPassword()); + + + } + + @Test + @Order(3) + void deleteTest() { + userService.delete(3L); + Assertions.assertEquals(2, userService.getAll().size()); + final UserEntity last = userService.get(2L); + Assertions.assertEquals(2L, last.getId()); + + final UserEntity newEntity = userService.create(new UserEntity(null, "Cat_cheshir", "cat@mail.ru", "1234")); + Assertions.assertEquals(3, userService.getAll().size()); + Assertions.assertEquals(4L, newEntity.getId()); + userService.clear(); + } +}