Skip to content

Commit 6493fdd

Browse files
Fix:Tests, modules User and TaskStatus
1 parent d17ac7a commit 6493fdd

File tree

6 files changed

+22
-58
lines changed

6 files changed

+22
-58
lines changed

src/main/java/hexlet/code/model/TaskStatus.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package hexlet.code.model;
22

3+
import java.time.LocalDate;
34

45
import jakarta.persistence.Column;
56
import jakarta.persistence.Entity;
@@ -10,27 +11,28 @@
1011
import jakarta.persistence.Table;
1112
import jakarta.validation.constraints.NotBlank;
1213
import jakarta.validation.constraints.Size;
14+
import lombok.EqualsAndHashCode;
1315
import org.springframework.data.annotation.CreatedDate;
1416
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
1517

1618
import lombok.Getter;
1719
import lombok.Setter;
1820

19-
import java.time.LocalDate;
20-
2121
@Getter
2222
@Setter
23-
23+
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
2424
@EntityListeners(AuditingEntityListener.class)
2525
@Entity
2626
@Table(name = "taskstatuses")
2727
public class TaskStatus {
2828
@Id
2929
@GeneratedValue(strategy = GenerationType.IDENTITY)
30+
@EqualsAndHashCode.Include
3031
private Long id;
3132

3233
@NotBlank
3334
@Size(min = 1)
35+
@EqualsAndHashCode.Include
3436
private String name;
3537

3638
@NotBlank

src/main/java/hexlet/code/model/User.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import jakarta.persistence.Table;
1111
import jakarta.validation.constraints.Email;
1212
import jakarta.validation.constraints.NotBlank;
13+
import lombok.EqualsAndHashCode;
1314
import lombok.Getter;
1415
import lombok.RequiredArgsConstructor;
1516
import lombok.Setter;
@@ -27,22 +28,25 @@
2728
@Getter
2829
@Setter
2930
@RequiredArgsConstructor
31+
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
3032
@EntityListeners(AuditingEntityListener.class)
3133
@Table(name = "users")
3234
@Schema(description = "User data model")
33-
public class User implements BaseEntity, UserDetails {
35+
public class User implements BaseEntity, UserDetails {
3436

3537
@Id
3638
@Schema(description = "User ID",
3739
example = "123")
3840
@GeneratedValue(strategy = GenerationType.IDENTITY)
41+
@EqualsAndHashCode.Include
3942
@Column(name = "id", nullable = false)
4043
private Long id;
4144

4245
@Schema(description = "User first name",
4346
example = "Tom",
4447
requiredMode = Schema.RequiredMode.REQUIRED)
4548
@NotBlank
49+
@EqualsAndHashCode.Include
4650
private String firstName;
4751
@NotBlank
4852
private String lastName;
@@ -65,7 +69,6 @@ public class User implements BaseEntity, UserDetails {
6569
private LocalDate updatedAt;
6670

6771
/**
68-
*
6972
* @return String
7073
*/
7174
@Override
@@ -74,7 +77,6 @@ public String getPassword() {
7477
}
7578

7679
/**
77-
*
7880
* @return String
7981
*/
8082
@Override
@@ -83,7 +85,6 @@ public String getUsername() {
8385
}
8486

8587
/**
86-
*
8788
* @return GrantedAuthority
8889
*/
8990
@Override

src/test/java/hexlet/code/controller/TestLabelController.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.assertj.core.api.Assertions;
1010
import org.instancio.Instancio;
1111
import org.instancio.Select;
12-
import org.junit.jupiter.api.AfterEach;
1312
import org.junit.jupiter.api.BeforeEach;
1413
import org.junit.jupiter.api.DisplayName;
1514
import org.junit.jupiter.api.Test;
@@ -34,15 +33,13 @@
3433
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
3534
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
3635

37-
3836
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
3937

4038
@SpringBootTest(webEnvironment = RANDOM_PORT)
4139
@AutoConfigureMockMvc
4240
class TestLabelController {
4341
@LocalServerPort
4442
private int port;
45-
4643
@Autowired
4744
private MockMvc mockMvc;
4845
@Autowired
@@ -59,6 +56,7 @@ class TestLabelController {
5956
*/
6057
@BeforeEach
6158
void initTests() {
59+
labelRepository.deleteAll();
6260
testLabel = Instancio.of(Label.class)
6361
.ignore(Select.field(Label::getId))
6462
.set(Select.field("name"), "testLabel")
@@ -67,14 +65,6 @@ void initTests() {
6765
labelRepository.save(testLabel);
6866
}
6967

70-
/**
71-
* afterEach.
72-
*/
73-
@AfterEach
74-
void clean() {
75-
labelRepository.deleteAll();
76-
}
77-
7868
@Test
7969
@DisplayName("R - Test get all")
8070
void testIndex() throws Exception {
@@ -87,7 +77,6 @@ void testIndex() throws Exception {
8777
var actual = labelDTOS.stream().map(labelMapper::map).toList();
8878
var expected = labelRepository.findAll();
8979
Assertions.assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
90-
9180
}
9281

9382
@Test

src/test/java/hexlet/code/controller/TestTaskController.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.assertj.core.api.Assertions;
1616
import org.instancio.Instancio;
1717
import org.instancio.Select;
18-
import org.junit.jupiter.api.AfterEach;
1918
import org.junit.jupiter.api.BeforeEach;
2019
import org.junit.jupiter.api.DisplayName;
2120
import org.junit.jupiter.api.Test;
@@ -61,12 +60,13 @@ class TestTaskController {
6160
private Task testTask;
6261
private TaskStatus taskStatus;
6362

64-
6563
/**
6664
* setUp.
6765
*/
6866
@BeforeEach
6967
void setUp() {
68+
taskRepository.deleteAll();
69+
taskStatusRepository.deleteAll();
7070
taskStatus = Instancio.of(TaskStatus.class)
7171
.ignore(Select.field(TaskStatus::getId))
7272
.supply(Select.field(TaskStatus::getName), () -> "Move1")
@@ -86,15 +86,6 @@ void setUp() {
8686
taskRepository.save(testTask);
8787
}
8888

89-
/**
90-
* afterEach.
91-
*/
92-
@AfterEach
93-
void clean() {
94-
taskRepository.deleteAll();
95-
taskStatusRepository.deleteAll();
96-
}
97-
9889
@Test
9990
@DisplayName("R - Test get all")
10091
void testIndex() throws Exception {

src/test/java/hexlet/code/controller/TestTaskStatusController.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import hexlet.code.exeption.ResourceNotFoundException;
77
import hexlet.code.mapper.TaskStatusMapper;
88
import hexlet.code.model.TaskStatus;
9+
import hexlet.code.repository.TaskRepository;
910
import hexlet.code.repository.TaskStatusRepository;
1011
import org.assertj.core.api.Assertions;
1112
import org.instancio.Instancio;
1213
import org.instancio.Select;
13-
import org.junit.jupiter.api.AfterEach;
1414
import org.junit.jupiter.api.BeforeEach;
1515
import org.junit.jupiter.api.DisplayName;
1616
import org.junit.jupiter.api.Test;
@@ -50,13 +50,17 @@ class TestTaskStatusController {
5050
private TaskStatusRepository taskStatusRepository;
5151
@Autowired
5252
private TaskStatusMapper taskStatusMapper;
53+
@Autowired
54+
private TaskRepository taskRepository;
55+
5356
private TaskStatus testTaskStatus;
5457

5558
/**
5659
* setUp.
5760
*/
5861
@BeforeEach
5962
void setUp() {
63+
taskRepository.deleteAll();
6064
taskStatusRepository.deleteAll();
6165
testTaskStatus = Instancio.of(TaskStatus.class)
6266
.ignore(Select.field(TaskStatus::getId))
@@ -67,14 +71,6 @@ void setUp() {
6771
taskStatusRepository.save(testTaskStatus);
6872
}
6973

70-
/**
71-
* afterEach.
72-
*/
73-
@AfterEach
74-
void clean() {
75-
taskStatusRepository.deleteAll();
76-
}
77-
7874
@Test
7975
@DisplayName("R - Test get all")
8076
void testIndex() throws Exception {
@@ -88,11 +84,7 @@ void testIndex() throws Exception {
8884
var actual = taskStatusDTOS.stream().map(taskStatusMapper::map).toList();
8985
var expected = taskStatusRepository.findAll();
9086

91-
Assertions.assertThat(actual)
92-
.usingRecursiveComparison()
93-
.ignoringFields("createdAt")
94-
.isEqualTo(expected);
95-
// Assertions.assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
87+
Assertions.assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
9688
}
9789

9890
@Test

src/test/java/hexlet/code/controller/TestUserController.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import hexlet.code.repository.UserRepository;
1212
import hexlet.code.util.JWTUtils;
1313
import net.datafaker.Faker;
14+
import org.assertj.core.api.Assertions;
1415
import org.instancio.Instancio;
1516
import org.instancio.Select;
16-
import org.junit.jupiter.api.AfterEach;
1717
import org.junit.jupiter.api.BeforeEach;
1818
import org.junit.jupiter.api.DisplayName;
1919
import org.junit.jupiter.api.Test;
@@ -67,6 +67,7 @@ class TestUserController {
6767
*/
6868
@BeforeEach
6969
void setUp() {
70+
userRepository.deleteAll();
7071
testUser = new User();
7172
testUser = Instancio.of(User.class)
7273
.ignore(Select.field(User::getId))
@@ -78,14 +79,6 @@ void setUp() {
7879
token = jwtUtils.generateToken("hexlet@example.com");
7980
}
8081

81-
/**
82-
* afterEach.
83-
*/
84-
@AfterEach
85-
void clean() {
86-
userRepository.deleteAll();
87-
}
88-
8982
@Test
9083
@DisplayName("R - Test Welcome endpoint")
9184
void testWelcome() {
@@ -173,11 +166,7 @@ void testIndex() throws Exception {
173166
var actual = userDTOS.stream().map(userMapper::map).toList();
174167
var expected = userRepository.findAll();
175168

176-
assertThat(actual)
177-
.usingRecursiveComparison()
178-
.ignoringFields("passwordDigest", "createdAt", "updatedAt")
179-
.isEqualTo(expected);
180-
// Assertions.assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
169+
Assertions.assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
181170
}
182171

183172
@Test

0 commit comments

Comments
 (0)