Skip to content

Commit

Permalink
fix: reduce long lines
Browse files Browse the repository at this point in the history
  • Loading branch information
markkovari committed Aug 15, 2024
1 parent dc11f2c commit d135c08
Showing 1 changed file with 75 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -63,20 +67,32 @@ 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)
);

}

@DisplayName("Login a user successfully")
@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
Expand All @@ -86,32 +102,53 @@ 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));

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

0 comments on commit d135c08

Please sign in to comment.