Skip to content

#7 Implement testing and JaCoCo #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<checkstyle.version>3.2.2</checkstyle.version>
<graphql-java-kickstart.version>15.0.0</graphql-java-kickstart.version>
<graphql-java-extended-scalars.version>20.2</graphql-java-extended-scalars.version>
<junit-jupiter.version>5.10.0</junit-jupiter.version>
<mockito.version>5.4.0</mockito.version>
<jacoco.version>0.8.8</jacoco.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -65,6 +68,12 @@
<artifactId>spring-boot-starter-graphql</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-java-kickstart</artifactId>
Expand Down Expand Up @@ -164,6 +173,20 @@
<version>${minio.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down Expand Up @@ -223,6 +246,49 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>PACKAGE</element>
<limits>
<limit implementation="org.jacoco.report.check.Limit">
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.60</minimum>
</limit>
</limits>
<includes>
<include>com.example.tasklist.service.impl</include>
</includes>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
103 changes: 103 additions & 0 deletions src/test/java/com/example/tasklist/config/TestConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.example.tasklist.config;

import com.example.tasklist.repository.TaskRepository;
import com.example.tasklist.repository.UserRepository;
import com.example.tasklist.service.AuthService;
import com.example.tasklist.service.ImageService;
import com.example.tasklist.service.TaskService;
import com.example.tasklist.service.UserService;
import com.example.tasklist.service.impl.AuthServiceImpl;
import com.example.tasklist.service.impl.ImageServiceImpl;
import com.example.tasklist.service.impl.TaskServiceImpl;
import com.example.tasklist.service.impl.UserServiceImpl;
import com.example.tasklist.service.props.JwtProperties;
import com.example.tasklist.service.props.MinioProperties;
import com.example.tasklist.web.security.JwtTokenProvider;
import com.example.tasklist.web.security.JwtUserDetailsService;
import io.minio.MinioClient;
import lombok.RequiredArgsConstructor;
import org.mockito.Mockito;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@TestConfiguration
@RequiredArgsConstructor
public class TestConfig {

private final UserRepository userRepository;
private final TaskRepository taskRepository;
private final AuthenticationManager authenticationManager;

@Bean
@Primary
public PasswordEncoder testPasswordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public JwtProperties jwtProperties() {
JwtProperties jwtProperties = new JwtProperties();
jwtProperties.setSecret(
"dmdqdmphdmpndmVjdmplZ2VzamdjdnJzY2p2cnZyY2dzYWNia2hh"
);
return jwtProperties;
}

@Bean
@Primary
public UserDetailsService userDetailsService() {
return new JwtUserDetailsService(userService());
}

@Bean
public MinioClient minioClient() {
return Mockito.mock(MinioClient.class);
}

@Bean
public MinioProperties minioProperties() {
MinioProperties properties = new MinioProperties();
properties.setBucket("images");
return properties;
}

@Bean
@Primary
public ImageService imageService() {
return new ImageServiceImpl(minioClient(), minioProperties());
}

@Bean
public JwtTokenProvider tokenProvider() {
return new JwtTokenProvider(jwtProperties(),
userDetailsService(),
userService());
}

@Bean
@Primary
public UserService userService() {
return new UserServiceImpl(userRepository, testPasswordEncoder());
}

@Bean
@Primary
public TaskService taskService() {
return new TaskServiceImpl(taskRepository,
imageService());
}

@Bean
@Primary
public AuthService authService() {
return new AuthServiceImpl(authenticationManager,
userService(),
tokenProvider());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.example.tasklist.service.impl;

import com.example.tasklist.config.TestConfig;
import com.example.tasklist.domain.exception.ResourceNotFoundException;
import com.example.tasklist.domain.user.Role;
import com.example.tasklist.domain.user.User;
import com.example.tasklist.repository.TaskRepository;
import com.example.tasklist.repository.UserRepository;
import com.example.tasklist.service.UserService;
import com.example.tasklist.web.dto.auth.JwtRequest;
import com.example.tasklist.web.dto.auth.JwtResponse;
import com.example.tasklist.web.security.JwtTokenProvider;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.Collections;
import java.util.Set;

@ExtendWith(SpringExtension.class)
@ActiveProfiles("test")
@Import(TestConfig.class)
@ExtendWith(MockitoExtension.class)
public class AuthServiceImplTest {

@MockBean
private AuthenticationManager authenticationManager;

@MockBean
private UserService userService;

@MockBean
private UserRepository userRepository;

@MockBean
private TaskRepository taskRepository;

@MockBean
private JwtTokenProvider tokenProvider;

@Autowired
private AuthServiceImpl authService;

@Test
void login() {
Long userId = 1L;
String username = "username";
String password = "password";
Set<Role> roles = Collections.emptySet();
String accessToken = "accessToken";
String refreshToken = "refreshToken";
JwtRequest request = new JwtRequest();
request.setUsername(username);
request.setPassword(password);
User user = new User();
user.setId(userId);
user.setUsername(username);
user.setRoles(roles);
Mockito.when(userService.getByUsername(username))
.thenReturn(user);
Mockito.when(tokenProvider.createAccessToken(userId, username, roles))
.thenReturn(accessToken);
Mockito.when(tokenProvider.createRefreshToken(userId, username))
.thenReturn(refreshToken);
JwtResponse response = authService.login(request);
Mockito.verify(authenticationManager)
.authenticate(
new UsernamePasswordAuthenticationToken(
request.getUsername(),
request.getPassword()
)
);
Assertions.assertEquals(username, response.getUsername());
Assertions.assertEquals(userId, response.getId());
Assertions.assertNotNull(response.getAccessToken());
Assertions.assertNotNull(response.getRefreshToken());
}

@Test
void loginWithIncorrectUsername() {
String username = "username";
String password = "password";
JwtRequest request = new JwtRequest();
request.setUsername(username);
request.setPassword(password);
Mockito.when(userService.getByUsername(username))
.thenThrow(ResourceNotFoundException.class);
Mockito.verifyNoInteractions(tokenProvider);
Assertions.assertThrows(ResourceNotFoundException.class,
() -> authService.login(request));
}

@Test
void refresh() {
String accessToken = "accessToken";
String refreshToken = "refreshToken";
String newRefreshToken = "newRefreshToken";
JwtResponse response = new JwtResponse();
response.setAccessToken(accessToken);
response.setRefreshToken(newRefreshToken);
Mockito.when(tokenProvider.refreshUserTokens(refreshToken))
.thenReturn(response);
JwtResponse testResponse = authService.refresh(refreshToken);
Mockito.verify(tokenProvider).refreshUserTokens(refreshToken);
Assertions.assertEquals(response, testResponse);
}

}
Loading