Explorar el Código

Add unit tests for UserService

Daniel Bohry hace 8 meses
padre
commit
a93a5700fc

+ 1 - 0
build.gradle

@@ -33,6 +33,7 @@ dependencies {
     compileOnly 'org.projectlombok:lombok'
     annotationProcessor 'org.projectlombok:lombok'
     testImplementation 'org.springframework.boot:spring-boot-starter-test'
+    testImplementation 'org.mockito:mockito-core:4.0.0'
 }
 
 tasks.named('test') {

+ 1 - 1
src/main/java/com/danielbohry/authservice/service/auth/AuthService.java

@@ -36,7 +36,7 @@ public class AuthService implements UserDetailsService {
 
     public AuthenticationResponse signup(AuthenticationRequest request) {
         var user = User.builder().username(request.getUsername()).password(passwordEncoder.encode(request.getPassword())).build();
-        ApplicationUser saved = service.save(convert(user));
+        ApplicationUser saved = service.create(convert(user));
         var authentication = jwtService.generateToken(saved);
         return buildResponse(authentication);
     }

+ 1 - 1
src/main/java/com/danielbohry/authservice/service/user/UserService.java

@@ -29,7 +29,7 @@ public class UserService {
             .orElseThrow(() -> new NotFoundException("User not found"));
     }
 
-    public ApplicationUser save(ApplicationUser applicationUser) {
+    public ApplicationUser create(ApplicationUser applicationUser) {
         validateUsername(applicationUser);
 
         applicationUser.setId(UUID.randomUUID().toString());

+ 96 - 0
src/test/java/com/danielbohry/authservice/UserServiceTest.java

@@ -0,0 +1,96 @@
+package com.danielbohry.authservice;
+
+import com.danielbohry.authservice.domain.ApplicationUser;
+import com.danielbohry.authservice.exceptions.BadRequestException;
+import com.danielbohry.authservice.exceptions.NotFoundException;
+import com.danielbohry.authservice.repository.UserRepository;
+import com.danielbohry.authservice.service.user.UserService;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.UUID;
+
+import static com.danielbohry.authservice.domain.Role.USER;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class UserServiceTest {
+
+    private UserService service;
+    private UserRepository repository;
+
+    public static final String USERNAME = "username";
+    public static final String PASSWORD = "password";
+    public static final String MAIL = "[email protected]";
+
+    @BeforeEach
+    public void setup() {
+        repository = mock(UserRepository.class);
+        service = new UserService(repository);
+    }
+
+    @Test
+    public void shouldCreateApplicationUser() {
+        //given
+        ApplicationUser user = someUser();
+
+        when(repository.save(user)).thenReturn(user);
+
+        //when
+        ApplicationUser result = service.create(user);
+
+        //then
+        assertEquals(user, result);
+    }
+
+    @Test
+    public void shouldFindUserByUsername() {
+        //given
+        ApplicationUser user = someUser();
+
+        when(repository.findByUsernameAndActiveTrue(USERNAME)).thenReturn(Optional.of(user));
+
+        //when
+        ApplicationUser result = service.findByUsername(USERNAME);
+
+        //then
+        assertEquals(user, result);
+    }
+
+    @Test
+    public void shouldThrowNotFoundExceptionWhenUserNotFound() {
+        // given
+        String unknownUsername = "unknown_user";
+        when(repository.findByUsernameAndActiveTrue(unknownUsername)).thenReturn(Optional.empty());
+
+        // then
+        assertThrows(NotFoundException.class, () -> service.findByUsername(unknownUsername));
+    }
+
+    @Test
+    public void shouldValidateUsername() {
+        //given
+        ApplicationUser user = someUser();
+
+        when(repository.existsByUsername(USERNAME)).thenReturn(true);
+
+        //then
+        assertThrows(BadRequestException.class, () -> service.create(user));
+    }
+
+    private static ApplicationUser someUser() {
+        return ApplicationUser.builder()
+            .id(UUID.randomUUID().toString())
+            .username(USERNAME)
+            .password(PASSWORD)
+            .email(MAIL)
+            .roles(List.of(USER))
+            .active(true)
+            .build();
+    }
+
+}