Lab2 part 2 ready

This commit is contained in:
gg12 darfren 2024-03-15 19:53:59 +04:00
parent 2e2657558f
commit 3fe244e595
13 changed files with 240 additions and 18 deletions

View File

@ -1,4 +1,5 @@
{ {
"java.compile.nullAnalysis.mode": "automatic", "java.compile.nullAnalysis.mode": "automatic",
"java.configuration.updateBuildConfiguration": "interactive" "java.configuration.updateBuildConfiguration": "interactive",
"java.dependency.packagePresentation": "hierarchical"
} }

View File

@ -17,6 +17,7 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' 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.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
implementation 'org.modelmapper:modelmapper:3.2.0' implementation 'org.modelmapper:modelmapper:3.2.0'

View File

@ -65,7 +65,7 @@ public class GroupController {
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public GroupDto delete(@PathVariable(name = "id") Long id) { public GroupDto delete(@PathVariable(name = "id") @Valid Long id) {
return toDto(groupService.delete(id)); return toDto(groupService.delete(id));
} }
} }

View File

@ -48,4 +48,8 @@ public class GroupService {
final GroupEntity existsEntity = get(id); final GroupEntity existsEntity = get(id);
return repository.delete(existsEntity); return repository.delete(existsEntity);
} }
public void clear(){
repository.deleteAll();
}
} }

View File

@ -64,7 +64,7 @@ public class MemberController {
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public MemberDto delete(@PathVariable(name = "id") Long id) { public MemberDto delete(@PathVariable(name = "id") @Valid Long id) {
return toDto(memberService.delete(id)); return toDto(memberService.delete(id));
} }
} }

View File

@ -49,4 +49,8 @@ public class MemberService {
final MemberEntity existsEntity = get(id); final MemberEntity existsEntity = get(id);
return repository.delete(existsEntity); return repository.delete(existsEntity);
} }
public void clear(){
repository.deleteAll();
}
} }

View File

@ -58,7 +58,7 @@ public class UserController {
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public UserDto delete(@PathVariable(name = "id") Long id) { public UserDto delete(@PathVariable(name = "id") @Valid Long id) {
return toDto(userService.delete(id)); return toDto(userService.delete(id));
} }
} }

View File

@ -7,7 +7,9 @@ import com.example.demo.core.model.BaseEntity;
import jakarta.validation.OverridesAttribute; import jakarta.validation.OverridesAttribute;
public class UserEntity extends BaseEntity{ public class UserEntity extends BaseEntity{
private String handle, email, password; private String handle;
private String email;
private String password;
public UserEntity(){ public UserEntity(){
super(); super();

View File

@ -43,4 +43,8 @@ public class UserService {
final UserEntity existsEntity = get(id); final UserEntity existsEntity = get(id);
return repository.delete(existsEntity); return repository.delete(existsEntity);
} }
public void clear(){
repository.deleteAll();
}
} }

View File

@ -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() {
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}