From d135c08674e06c69b29c5397f4e4db624ef04986 Mon Sep 17 00:00:00 2001 From: markkovari Date: Thu, 15 Aug 2024 23:57:29 +0200 Subject: [PATCH] fix: reduce long lines --- .../services/user/UserServiceImplTest.java | 92 +++++++++++++++---- 1 file changed, 75 insertions(+), 17 deletions(-) diff --git a/backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java b/backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java index 9d2bcb4a..bf8c93d8 100644 --- a/backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java +++ b/backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java @@ -49,7 +49,11 @@ void setUp() { void register() { // Given User asSaved = User.builder().id(1).build(); - RegisterRequestDto registerRequestDto = new RegisterRequestDto("fistName", "lastName", "some@email.com", "SomePassword123"); + RegisterRequestDto registerRequestDto = new RegisterRequestDto( + "fistName", + "lastName", + "some@email.com", + "SomePassword123"); // When when(userRepository.save(any())).thenReturn(asSaved); @@ -63,12 +67,20 @@ void register() { @Test void registerFails() { // Given - RegisterRequestDto registerRequestDto = new RegisterRequestDto("fistName", "lastName", "some@email.com", "SomePassword123"); + RegisterRequestDto registerRequestDto = new RegisterRequestDto( + "fistName", + "lastName", + "some@email.com", + "SomePassword123"); // When - when(userRepository.save(any())).thenThrow(new UserAlreadyExistsError("Email is already taken!")); + when(userRepository.save(any())) + .thenThrow(new UserAlreadyExistsError("Email is already taken!")); - Assertions.assertThrows(UserAlreadyExistsError.class, () -> userService.register(registerRequestDto)); + Assertions.assertThrows( + UserAlreadyExistsError.class, + () -> userService.register(registerRequestDto) + ); } @@ -76,7 +88,11 @@ void registerFails() { @Test void login() throws Exception { // Given - User user = User.builder().id(1).email("email").password(passwordEncoder.encode("password")).build(); + User user = User.builder() + .id(1) + .email("email") + .password(passwordEncoder.encode("password")) + .build(); LoginRequestDto userLoginRequestDto = new LoginRequestDto("email", "password"); // When @@ -86,15 +102,25 @@ void login() throws Exception { userService.login(userLoginRequestDto); // Then - Mockito.verify(userRepository, Mockito.times(1)).findByEmail(anyString()); + Mockito.verify( + userRepository, + Mockito.times(1)).findByEmail(anyString() + ); } @DisplayName("Login a user unsuccessfully") @Test void loginUnsuccessful() throws Exception { // Given - User user = User.builder().id(1).email("email").password(passwordEncoder.encode("passwordNOOP")).build(); - LoginRequestDto userLoginRequestDto = new LoginRequestDto("email", "password"); + User user = User.builder() + .id(1) + .email("email") + .password(passwordEncoder.encode("passwordNOOP")) + .build(); + LoginRequestDto userLoginRequestDto = new LoginRequestDto( + "email", + "password" + ); // When when(userRepository.findByEmail(anyString())).thenReturn(Optional.of(user)); @@ -102,16 +128,27 @@ void loginUnsuccessful() throws Exception { Assertions.assertThrows(Exception.class, () -> userService.login(userLoginRequestDto)); // Then - Mockito.verify(userRepository, Mockito.times(1)).findByEmail(anyString()); + Mockito.verify( + userRepository, + Mockito.times(1)).findByEmail(anyString() + ); } @DisplayName("Update a user profile successfully") @Test void profileUpdate() throws Exception { // Given - User user = User.builder().id(1).email("email").password(passwordEncoder.encode("password")).build(); + User user = User.builder() + .id(1) + .email("email") + .password(passwordEncoder.encode("password")) + .build(); String email = "email"; - ProfileUpdateRequestDto profileUpdateRequestDto = new ProfileUpdateRequestDto("newEmail", "newFirstName", "newLastName", "newPassword"); + ProfileUpdateRequestDto profileUpdateRequestDto = new ProfileUpdateRequestDto( + "newEmail", + "newFirstName", + "newLastName", + "newPassword"); // When when(userRepository.findByEmail(anyString())).thenReturn(Optional.of(user)); @@ -120,34 +157,55 @@ void profileUpdate() throws Exception { userService.profileUpdate(email, profileUpdateRequestDto); // Then - Mockito.verify(userRepository, Mockito.times(1)).findByEmail(anyString()); + Mockito + .verify(userRepository, Mockito.times(1)) + .findByEmail(anyString()); } @DisplayName("Update a user profile unsuccessfully") @Test void profileUpdateUnsuccessful() throws Exception { // Given - User user = User.builder().id(1).email("email").password(passwordEncoder.encode("password")).build(); + User user = User.builder().id(1).email("email") + .password(passwordEncoder.encode("password")) + .build(); String email = "email"; - ProfileUpdateRequestDto profileUpdateRequestDto = new ProfileUpdateRequestDto("newEmail", "newFirstName", "new@email.com", "newPassword"); + // @formatter:off + + ProfileUpdateRequestDto profileUpdateRequestDto = new ProfileUpdateRequestDto( + "newEmail", + "newFirstName", + "new@email.com", + "newPassword"); + // @formatter:on // When when(userRepository.findByEmail(anyString())).thenReturn(Optional.of(user)); when(userRepository.existsByEmail("new@email.com")).thenReturn(true); // Then - Assertions.assertThrows(UserAlreadyExistsError.class, () -> userService.profileUpdate(email, profileUpdateRequestDto)); + Assertions.assertThrows( + UserAlreadyExistsError.class, + () -> userService.profileUpdate(email, profileUpdateRequestDto) + ); } @Test void loadUserByUsername() { // Given - User user = User.builder().id(1).email("email").password(passwordEncoder.encode("password")).build(); + User user = User.builder() + .id(1) + .email("email") + .password(passwordEncoder.encode("password")) + .build(); String email = "email"; // When when(userRepository.findByEmail(anyString())).thenReturn(Optional.of(user)); userService.loadUserByUsername(email); // Then - Mockito.verify(userRepository, Mockito.times(1)).findByEmail(anyString()); + Mockito.verify( + userRepository, + Mockito.times(1) + ).findByEmail(anyString()); } } \ No newline at end of file