From 3271d319ebee41e62e2b98db1a9a26d1c5501a7a Mon Sep 17 00:00:00 2001 From: Jonathan Lermitage Date: Sat, 25 Nov 2023 00:59:09 +0100 Subject: [PATCH] remove useless stuff --- src/main/java/manon/api/user/UserWS.java | 8 ------- .../document/user/FriendshipEventEntity.java | 3 +++ .../java/manon/document/user/UserEntity.java | 19 ++++----------- .../document/user/UserSnapshotEntity.java | 3 --- .../document/user/UserVersionProjection.java | 7 ------ .../java/manon/dto/user/AbstractUserDto.java | 5 +--- .../java/manon/mapper/user/UserMapper.java | 1 - .../manon/repository/user/UserRepository.java | 4 ---- .../java/manon/service/user/UserService.java | 3 --- .../service/user/impl/UserServiceImpl.java | 6 ----- .../hsqldb/main/V1.1__generated_by_JPA.sql | 3 --- .../main/V1.1__create_application_tables.sql | 3 --- .../main/V1.1__create_application_tables.sql | 4 +--- .../java/manon/api/user/UserWSCtrlIT.java | 10 -------- src/test/java/manon/api/user/UserWSIT.java | 13 ---------- .../manon/document/user/UserEntityTest.java | 24 ++----------------- .../document/user/UserSnapshotEntityTest.java | 3 +-- src/test/java/manon/dto/user/UserDtoTest.java | 2 +- .../dto/user/UserWithSnapshotsDtoTest.java | 2 +- .../service/user/impl/UserServiceIT.java | 13 ---------- .../manon/util/mapper/UserMapperDataset.java | 5 ---- 21 files changed, 15 insertions(+), 126 deletions(-) delete mode 100644 src/main/java/manon/document/user/UserVersionProjection.java diff --git a/src/main/java/manon/api/user/UserWS.java b/src/main/java/manon/api/user/UserWS.java index c91bc70f..2755ee14 100644 --- a/src/main/java/manon/api/user/UserWS.java +++ b/src/main/java/manon/api/user/UserWS.java @@ -75,14 +75,6 @@ public UserWithSnapshotsDto readAndIncludeUserSnapshots(@AuthenticationPrincipal return userService.readOneAndFetchUserSnapshotDtos(user.getUserId()); } - /** Get user's version. */ - @Operation(summary = "Get my user version number.") - @GetMapping("/version") - public long readVersion(@AuthenticationPrincipal UserSimpleDetails user) { - log.debug("user {} reads his version", user.getIdentity()); - return userService.readVersionById(user.getUserId()).getVersion(); - } - /** Update one user's user field. */ @Operation(summary = "Update my user data.") @PutMapping(value = "/field", consumes = JSON) diff --git a/src/main/java/manon/document/user/FriendshipEventEntity.java b/src/main/java/manon/document/user/FriendshipEventEntity.java index 5ad832f5..73667c87 100644 --- a/src/main/java/manon/document/user/FriendshipEventEntity.java +++ b/src/main/java/manon/document/user/FriendshipEventEntity.java @@ -23,6 +23,8 @@ import lombok.ToString; import manon.model.user.FriendshipEventCode; import manon.util.Tools; +import org.hibernate.annotations.JdbcTypeCode; +import org.hibernate.type.SqlTypes; import java.io.Serializable; import java.time.LocalDateTime; @@ -53,6 +55,7 @@ public class FriendshipEventEntity implements Serializable { @Column(nullable = false) @Enumerated(EnumType.STRING) + @JdbcTypeCode(SqlTypes.VARCHAR) private FriendshipEventCode code; @JsonSerialize(using = LocalDateTimeSerializer.class) diff --git a/src/main/java/manon/document/user/UserEntity.java b/src/main/java/manon/document/user/UserEntity.java index 531044bd..b60453fa 100644 --- a/src/main/java/manon/document/user/UserEntity.java +++ b/src/main/java/manon/document/user/UserEntity.java @@ -28,7 +28,8 @@ import lombok.ToString; import manon.model.user.RegistrationState; import manon.util.Tools; -import org.springframework.data.annotation.Version; +import org.hibernate.annotations.JdbcTypeCode; +import org.hibernate.type.SqlTypes; import java.io.Serializable; import java.time.LocalDateTime; @@ -55,7 +56,7 @@ @QueryEntity @Getter @ToString(exclude = "userSnapshots") -@EqualsAndHashCode(exclude = {"id", "userSnapshots", "version", "creationDate", "updateDate"}) +@EqualsAndHashCode(exclude = {"id", "userSnapshots", "creationDate"}) @Builder(toBuilder = true) @AllArgsConstructor @NoArgsConstructor @@ -86,6 +87,7 @@ public class UserEntity implements Serializable { @Column(nullable = false) @Enumerated(EnumType.STRING) + @JdbcTypeCode(SqlTypes.VARCHAR) private RegistrationState registrationState; /** A non-unique name that can change, NOT mandatory. */ @@ -102,27 +104,16 @@ public class UserEntity implements Serializable { @OneToMany(fetch = FetchType.LAZY, mappedBy = "user") private List userSnapshots; - @Version - @Column(nullable = false) - private long version; - @JsonSerialize(using = LocalDateTimeSerializer.class) @JsonDeserialize(using = LocalDateTimeDeserializer.class) @Column(nullable = false) private LocalDateTime creationDate; - @JsonSerialize(using = LocalDateTimeSerializer.class) - @JsonDeserialize(using = LocalDateTimeDeserializer.class) - @Column(nullable = false) - private LocalDateTime updateDate; - @PrePersist public void prePersist() { - LocalDateTime now = Tools.now(); if (creationDate == null) { - creationDate = now; + creationDate = Tools.now(); } - updateDate = now; } /** {@link UserEntity} fields validation rules. */ diff --git a/src/main/java/manon/document/user/UserSnapshotEntity.java b/src/main/java/manon/document/user/UserSnapshotEntity.java index 8041404c..5b902aeb 100644 --- a/src/main/java/manon/document/user/UserSnapshotEntity.java +++ b/src/main/java/manon/document/user/UserSnapshotEntity.java @@ -65,9 +65,6 @@ public class UserSnapshotEntity implements Serializable { @Column(updatable = false) private String userEmail; - @Column(updatable = false) - private long userVersion; - @JsonSerialize(using = LocalDateTimeSerializer.class) @JsonDeserialize(using = LocalDateTimeDeserializer.class) @Column(nullable = false) diff --git a/src/main/java/manon/document/user/UserVersionProjection.java b/src/main/java/manon/document/user/UserVersionProjection.java deleted file mode 100644 index 283cc1ab..00000000 --- a/src/main/java/manon/document/user/UserVersionProjection.java +++ /dev/null @@ -1,7 +0,0 @@ -package manon.document.user; - -/** User version projection. */ -public interface UserVersionProjection { - - long getVersion(); -} diff --git a/src/main/java/manon/dto/user/AbstractUserDto.java b/src/main/java/manon/dto/user/AbstractUserDto.java index 9aa30ef7..ea067659 100644 --- a/src/main/java/manon/dto/user/AbstractUserDto.java +++ b/src/main/java/manon/dto/user/AbstractUserDto.java @@ -24,7 +24,7 @@ * declare any field because everything comes from the ancestor: this is not perfect, but it works. */ @Data -@EqualsAndHashCode(exclude = {"id", "creationDate", "updateDate"}) +@EqualsAndHashCode(exclude = {"id", "creationDate"}) public abstract class AbstractUserDto { private long id; @@ -41,7 +41,4 @@ public abstract class AbstractUserDto { @JsonSerialize(using = LocalDateTimeSerializer.class) private LocalDateTime creationDate; - - @JsonSerialize(using = LocalDateTimeSerializer.class) - private LocalDateTime updateDate; } diff --git a/src/main/java/manon/mapper/user/UserMapper.java b/src/main/java/manon/mapper/user/UserMapper.java index 19ae8df7..2bdbe1d3 100644 --- a/src/main/java/manon/mapper/user/UserMapper.java +++ b/src/main/java/manon/mapper/user/UserMapper.java @@ -36,7 +36,6 @@ public abstract class UserMapper { @Mapping(source = "registrationState", target = "userRegistrationState") @Mapping(source = "nickname", target = "userNickname") @Mapping(source = "email", target = "userEmail") - @Mapping(source = "version", target = "userVersion") @Mapping(source = "from", target = "user") public abstract UserSnapshotEntity toUserSnapshotEntity(UserEntity from); diff --git a/src/main/java/manon/repository/user/UserRepository.java b/src/main/java/manon/repository/user/UserRepository.java index eecca03d..6e917ff8 100644 --- a/src/main/java/manon/repository/user/UserRepository.java +++ b/src/main/java/manon/repository/user/UserRepository.java @@ -2,7 +2,6 @@ import manon.document.user.UserEntity; import manon.document.user.UserIdProjection; -import manon.document.user.UserVersionProjection; import manon.model.user.RegistrationState; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; @@ -18,9 +17,6 @@ public interface UserRepository extends JpaRepository, Queryds Optional findByUsername(String username); - @Query("select u from User u where u.id = :id") - Optional findVersionById(@Param("id") long id); - @Query("select u from User u where u.username = :username") Optional findVersionByUsername(@Param("username") String username); diff --git a/src/main/java/manon/service/user/UserService.java b/src/main/java/manon/service/user/UserService.java index 80e28ce8..447b5778 100644 --- a/src/main/java/manon/service/user/UserService.java +++ b/src/main/java/manon/service/user/UserService.java @@ -3,7 +3,6 @@ import com.querydsl.core.types.Predicate; import manon.document.user.UserEntity; import manon.document.user.UserIdProjection; -import manon.document.user.UserVersionProjection; import manon.dto.user.UserWithSnapshotsDto; import manon.err.user.PasswordNotMatchException; import manon.model.user.RegistrationState; @@ -24,8 +23,6 @@ public interface UserService { Optional findByUsername(String username); - UserVersionProjection readVersionById(long id); - /** * Update a user's data. * @param userId user id. diff --git a/src/main/java/manon/service/user/impl/UserServiceImpl.java b/src/main/java/manon/service/user/impl/UserServiceImpl.java index 7b494b36..f2d3f82c 100644 --- a/src/main/java/manon/service/user/impl/UserServiceImpl.java +++ b/src/main/java/manon/service/user/impl/UserServiceImpl.java @@ -6,7 +6,6 @@ import manon.document.user.QUserEntity; import manon.document.user.UserEntity; import manon.document.user.UserIdProjection; -import manon.document.user.UserVersionProjection; import manon.dto.user.UserWithSnapshotsDto; import manon.err.user.PasswordNotMatchException; import manon.err.user.UserExistsException; @@ -65,11 +64,6 @@ public UserEntity readByUsername(String username) { return userRepository.findByUsername(username).orElseThrow(UserNotFoundException::new); } - @Override - public UserVersionProjection readVersionById(long id) { - return userRepository.findVersionById(id).orElseThrow(UserNotFoundException::new); - } - @Override public UserEntity create(UserEntity user) { if (userRepository.existsByUsername(user.getUsername())) { diff --git a/src/main/resources/db/migration/hsqldb/main/V1.1__generated_by_JPA.sql b/src/main/resources/db/migration/hsqldb/main/V1.1__generated_by_JPA.sql index 8f3d3218..a5af58d5 100644 --- a/src/main/resources/db/migration/hsqldb/main/V1.1__generated_by_JPA.sql +++ b/src/main/resources/db/migration/hsqldb/main/V1.1__generated_by_JPA.sql @@ -49,9 +49,7 @@ create table user_ nickname varchar(24), password varchar(256) not null, registration_state varchar(255) not null, - update_date timestamp not null, username varchar(24) not null, - version bigint not null, primary key (id) ); create table user_snapshot @@ -64,7 +62,6 @@ create table user_snapshot user_password varchar(255), user_registration_state integer, user_username varchar(255), - user_version bigint, user_id bigint, primary key (id) ); diff --git a/src/main/resources/db/migration/mariadb/main/V1.1__create_application_tables.sql b/src/main/resources/db/migration/mariadb/main/V1.1__create_application_tables.sql index 50fc6019..748954ac 100644 --- a/src/main/resources/db/migration/mariadb/main/V1.1__create_application_tables.sql +++ b/src/main/resources/db/migration/mariadb/main/V1.1__create_application_tables.sql @@ -14,9 +14,7 @@ create table user_ nickname varchar(24) null, password varchar(256) not null, registration_state varchar(255) not null, - update_date datetime(6) not null, username varchar(24) not null, - version bigint not null, constraint uk__user__username unique (username) ); @@ -72,7 +70,6 @@ create table user_snapshot user_password varchar(255) null, user_registration_state int null, user_username varchar(255) null, - user_version bigint null, user_id bigint null, constraint fk__user_snapshot__user_id foreign key (user_id) references user_ (id) diff --git a/src/main/resources/db/migration/postgres/main/V1.1__create_application_tables.sql b/src/main/resources/db/migration/postgres/main/V1.1__create_application_tables.sql index 2c223270..0b324501 100644 --- a/src/main/resources/db/migration/postgres/main/V1.1__create_application_tables.sql +++ b/src/main/resources/db/migration/postgres/main/V1.1__create_application_tables.sql @@ -13,11 +13,9 @@ create table user_ nickname varchar(24), password varchar(256) not null, registration_state varchar(255) not null, - update_date timestamp not null, username varchar(24) not null constraint uk_wqsqlvajcne4rlyosglqglhk - unique, - version bigint not null + unique ); create table friendship diff --git a/src/test/java/manon/api/user/UserWSCtrlIT.java b/src/test/java/manon/api/user/UserWSCtrlIT.java index 13197244..778917ed 100644 --- a/src/test/java/manon/api/user/UserWSCtrlIT.java +++ b/src/test/java/manon/api/user/UserWSCtrlIT.java @@ -65,16 +65,6 @@ void shouldVerifyReadAndIncludeUserSnapshots(Rs rs, Integer status) { verify(userWs, status).readAndIncludeUserSnapshots(any()); } - @ParameterizedTest - @MethodSource(DP_ALLOW_AUTHENTICATED) - void shouldVerifyReadVersion(Rs rs, Integer status) { - rs.getSpec() - .get(API_USER + "/version") - .then() - .statusCode(status); - verify(userWs, status).readVersion(any()); - } - @ParameterizedTest @MethodSource(DP_ALLOW_AUTHENTICATED) void shouldVerifyUpdateField(Rs rs, Integer status) { diff --git a/src/test/java/manon/api/user/UserWSIT.java b/src/test/java/manon/api/user/UserWSIT.java index c2e6bc78..06044a85 100644 --- a/src/test/java/manon/api/user/UserWSIT.java +++ b/src/test/java/manon/api/user/UserWSIT.java @@ -54,7 +54,6 @@ void shouldRegister(String name, String pwd) { UserEntity user = userService.readByUsername(name); assertThat(user.getUsername()).isEqualTo(name); assertThat(user.getAuthorities()).isEqualTo(PLAYER.getAuthority()); - assertThat(user.getVersion()).isGreaterThanOrEqualTo(0L); assertThat(user.getRegistrationState()).isEqualTo(ACTIVE); assertThat(pwd).as("don't store raw passwords!") .isNotEqualTo(user.getPassword()) @@ -145,17 +144,6 @@ void shouldReadAndIncludeUserSnapshotsWhenUserHasSnapshots() { assertThat(webUser.getUserSnapshots()).hasSize(2).isEqualTo(dbUser.getUserSnapshots()); } - @Test - void shouldReadVersion() { - Response res = whenP1().getSpec() - .get(API_USER + "/version"); - res.then() - .statusCode(SC_OK); - Long webUserVersion = readValue(res, Long.class); - Long dbUserVersion = userService.readOne(userId(1)).getVersion(); - assertThat(webUserVersion).isEqualTo(dbUserVersion); - } - Object[][] dataProviderShouldUpdate() { return new Object[][]{ {"", ""}, @@ -177,7 +165,6 @@ void shouldUpdate(String nickname, String email) { UserEntity userExpected = userBefore.toBuilder() .email(email) .nickname(nickname) - .version(userBefore.getVersion() + 1) .build(); assertThat(userAfter).isEqualTo(userExpected); } diff --git a/src/test/java/manon/document/user/UserEntityTest.java b/src/test/java/manon/document/user/UserEntityTest.java index c7bb2667..508f1579 100644 --- a/src/test/java/manon/document/user/UserEntityTest.java +++ b/src/test/java/manon/document/user/UserEntityTest.java @@ -13,33 +13,13 @@ void shouldVerifyToString() { Assertions.assertThat(UserEntity.builder().build().toString()).contains( "id", "username", "authorities", "password", "registrationState", - "nickname", "email", - "version", "creationDate", "updateDate"); - } - - @Test - void shouldVerifyPrePersistOnNew() { - UserEntity o = UserEntity.builder().build(); - o.prePersist(); - Assertions.assertThat(o.getCreationDate()).isNotNull(); - Assertions.assertThat(o.getUpdateDate()).isEqualTo(o.getCreationDate()); - } - - @Test - void shouldVerifyPrePersistOnExisting() { - UserEntity o = UserEntity.builder().build(); - o.prePersist(); - LocalDateTime creationDate = o.getCreationDate(); - - o.prePersist(); - Assertions.assertThat(o.getCreationDate()).isEqualTo(creationDate); - Assertions.assertThat(o.getUpdateDate()).isAfterOrEqualTo(creationDate); + "nickname", "email", "creationDate"); } @Test void shouldVerifyEqualsContract() { EqualsVerifier.forClass(UserEntity.class) - .withIgnoredFields("id", "userSnapshots", "version", "creationDate", "updateDate") + .withIgnoredFields("id", "userSnapshots", "creationDate") .withPrefabValues(UserEntity.class, UserEntity.builder().nickname("n1").build(), UserEntity.builder().nickname("n2").build()) diff --git a/src/test/java/manon/document/user/UserSnapshotEntityTest.java b/src/test/java/manon/document/user/UserSnapshotEntityTest.java index 3f4e345b..bf1ee2ff 100644 --- a/src/test/java/manon/document/user/UserSnapshotEntityTest.java +++ b/src/test/java/manon/document/user/UserSnapshotEntityTest.java @@ -13,8 +13,7 @@ void shouldVerifyToString() { Assertions.assertThat(UserSnapshotEntity.builder().build().toString()).contains( "id", "userUsername", "userAuthorities", "userPassword", "userRegistrationState", - "userNickname", "userEmail", - "userVersion", "creationDate"); + "userNickname", "userEmail", "creationDate"); } @Test diff --git a/src/test/java/manon/dto/user/UserDtoTest.java b/src/test/java/manon/dto/user/UserDtoTest.java index 36b491db..82c14b2c 100644 --- a/src/test/java/manon/dto/user/UserDtoTest.java +++ b/src/test/java/manon/dto/user/UserDtoTest.java @@ -8,7 +8,7 @@ class UserDtoTest { @Test void shouldVerifyEqualsContract() { EqualsVerifier.simple().forClass(UserDto.class) - .withIgnoredFields("id", "creationDate", "updateDate") + .withIgnoredFields("id", "creationDate") .verify(); } } diff --git a/src/test/java/manon/dto/user/UserWithSnapshotsDtoTest.java b/src/test/java/manon/dto/user/UserWithSnapshotsDtoTest.java index 88238ec6..4ba1dfb5 100644 --- a/src/test/java/manon/dto/user/UserWithSnapshotsDtoTest.java +++ b/src/test/java/manon/dto/user/UserWithSnapshotsDtoTest.java @@ -8,7 +8,7 @@ class UserWithSnapshotsDtoTest { @Test void shouldVerifyEqualsContract() { EqualsVerifier.simple().forClass(UserWithSnapshotsDto.class) - .withIgnoredFields("id", "creationDate", "updateDate") + .withIgnoredFields("id", "creationDate") .verify(); } } diff --git a/src/test/java/manon/service/user/impl/UserServiceIT.java b/src/test/java/manon/service/user/impl/UserServiceIT.java index d090a4d1..ceca8f30 100644 --- a/src/test/java/manon/service/user/impl/UserServiceIT.java +++ b/src/test/java/manon/service/user/impl/UserServiceIT.java @@ -130,17 +130,6 @@ void shouldFailReadByUsernameUnknown() { .isInstanceOf(UserNotFoundException.class); } - @Test - void shouldReadVersionById() { - Assertions.assertThat(userService.readVersionById(userId(2)).getVersion()).isGreaterThanOrEqualTo(0L); - } - - @Test - void shouldFailReadVersionByIdUnknown() { - Assertions.assertThatThrownBy(() -> userService.readVersionById(UNKNOWN_ID)) - .isInstanceOf(UserNotFoundException.class); - } - @Test void shouldReadIdByUsername() { Assertions.assertThat(userService.readIdByUsername(name(1)).getId()).isEqualTo(userId(1)); @@ -262,7 +251,5 @@ void shouldSave(String validUsername) { UserEntity user = userService.readByUsername(validUsername); Assertions.assertThat(user.getCreationDate()).isBetween(before, after); - Assertions.assertThat(user.getUpdateDate()).isBetween(before, after); - Assertions.assertThat(user.getVersion()).isNotNegative(); } } diff --git a/src/test/java/manon/util/mapper/UserMapperDataset.java b/src/test/java/manon/util/mapper/UserMapperDataset.java index b04b5b25..dfa516e5 100644 --- a/src/test/java/manon/util/mapper/UserMapperDataset.java +++ b/src/test/java/manon/util/mapper/UserMapperDataset.java @@ -26,9 +26,7 @@ public static UserEntity toUserEntity(int id, UserSnapshotEntity... userSnapshot .registrationState(RegistrationState.ACTIVE) .nickname("nickname" + id) .email("u" + id + "@localhost.net") - .version(1) .creationDate(now) - .updateDate(now) .userSnapshots(Arrays.asList(userSnapshotEntityList)) .build(); } @@ -42,7 +40,6 @@ public static UserSnapshotEntity toUserSnapshotEntity(UserEntity user) { .userRegistrationState(user.getRegistrationState()) .userNickname(user.getNickname()) .userEmail(user.getEmail()) - .userVersion(1) .creationDate(now) .build(); } @@ -60,7 +57,6 @@ public static UserDto toUserDto(UserEntity user) { userDto.setNickname(user.getNickname()); userDto.setEmail(user.getEmail()); userDto.setCreationDate(user.getCreationDate()); - userDto.setUpdateDate(user.getUpdateDate()); return userDto; } @@ -86,7 +82,6 @@ public static UserWithSnapshotsDto toUserWithSnapshotsDto(UserDto user, UserSnap userWithSnapshotsDto.setNickname(user.getNickname()); userWithSnapshotsDto.setEmail(user.getEmail()); userWithSnapshotsDto.setCreationDate(user.getCreationDate()); - userWithSnapshotsDto.setUpdateDate(user.getUpdateDate()); userWithSnapshotsDto.setUserSnapshots(Arrays.asList(userSnapshots)); return userWithSnapshotsDto; }