Merge pull request #31 from HideyoshiNakazone/devel
Devel - Improves MicroService Implementation, Tests and etc
This commit is contained in:
@@ -155,9 +155,10 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
user.setProvider(Provider.LOCAL);
|
user.setProvider(Provider.LOCAL);
|
||||||
|
|
||||||
UserDTO authenticatedUser = this.userService.saveUser(user);
|
UserDTO authenticatedUser = this.userService.saveUser(user);
|
||||||
authenticatedUser.setProfilePictureUrl(
|
|
||||||
this.storageService.getFileUrl(authenticatedUser.getUsername(), "profile")
|
var profilePicture = this.storageService.getFileUrl(authenticatedUser.getUsername(), "profile");
|
||||||
.getPresignedUrl()
|
profilePicture.ifPresent(
|
||||||
|
storageServiceDownloadResponse -> authenticatedUser.setProfilePictureUrl(storageServiceDownloadResponse.getPresignedUrl())
|
||||||
);
|
);
|
||||||
|
|
||||||
return this.generateUserWithTokens(
|
return this.generateUserWithTokens(
|
||||||
@@ -169,9 +170,9 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void loginUser(HttpServletRequest request, HttpServletResponse response, @Valid UserDTO user) throws IOException {
|
public void loginUser(HttpServletRequest request, HttpServletResponse response, @Valid UserDTO user) throws IOException {
|
||||||
user.setProfilePictureUrl(
|
var profilePicture = this.storageService.getFileUrl(user.getUsername(), "profile");
|
||||||
this.storageService.getFileUrl(user.getUsername(), "profile")
|
profilePicture.ifPresent(
|
||||||
.getPresignedUrl()
|
storageServiceDownloadResponse -> user.setProfilePictureUrl(storageServiceDownloadResponse.getPresignedUrl())
|
||||||
);
|
);
|
||||||
|
|
||||||
AuthDTO authObject = this.generateUserWithTokens(
|
AuthDTO authObject = this.generateUserWithTokens(
|
||||||
@@ -202,9 +203,10 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
DecodedJWT decodedJWT = verifier.verify(refreshToken);
|
DecodedJWT decodedJWT = verifier.verify(refreshToken);
|
||||||
|
|
||||||
UserDTO user = this.userService.getUser(decodedJWT.getSubject());
|
UserDTO user = this.userService.getUser(decodedJWT.getSubject());
|
||||||
user.setProfilePictureUrl(
|
|
||||||
this.storageService.getFileUrl(user.getUsername(), "profile")
|
var profilePicture = this.storageService.getFileUrl(user.getUsername(), "profile");
|
||||||
.getPresignedUrl()
|
profilePicture.ifPresent(
|
||||||
|
storageServiceDownloadResponse -> user.setProfilePictureUrl(storageServiceDownloadResponse.getPresignedUrl())
|
||||||
);
|
);
|
||||||
|
|
||||||
HttpSession httpSession = request.getSession();
|
HttpSession httpSession = request.getSession();
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import com.hideyoshi.backendportfolio.base.user.service.UserService;
|
|||||||
import com.hideyoshi.backendportfolio.microservice.storageService.enums.FileTypeEnum;
|
import com.hideyoshi.backendportfolio.microservice.storageService.enums.FileTypeEnum;
|
||||||
import com.hideyoshi.backendportfolio.microservice.storageService.model.StorageServiceUploadResponse;
|
import com.hideyoshi.backendportfolio.microservice.storageService.model.StorageServiceUploadResponse;
|
||||||
import com.hideyoshi.backendportfolio.microservice.storageService.service.StorageService;
|
import com.hideyoshi.backendportfolio.microservice.storageService.service.StorageService;
|
||||||
|
import com.hideyoshi.backendportfolio.util.exception.BadRequestException;
|
||||||
import com.hideyoshi.backendportfolio.util.guard.UserResourceGuard;
|
import com.hideyoshi.backendportfolio.util.guard.UserResourceGuard;
|
||||||
import com.hideyoshi.backendportfolio.util.guard.UserResourceGuardEnum;
|
import com.hideyoshi.backendportfolio.util.guard.UserResourceGuardEnum;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@@ -95,7 +96,7 @@ public class UserController {
|
|||||||
user.getUsername(),
|
user.getUsername(),
|
||||||
"profile",
|
"profile",
|
||||||
fileType
|
fileType
|
||||||
);
|
).orElseThrow(() -> new BadRequestException("File not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/profile-picture")
|
@DeleteMapping("/profile-picture")
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.hideyoshi.backendportfolio.base.user.service;
|
package com.hideyoshi.backendportfolio.base.user.service;
|
||||||
|
|
||||||
|
import com.hideyoshi.backendportfolio.base.user.entity.Provider;
|
||||||
import com.hideyoshi.backendportfolio.base.user.entity.Role;
|
import com.hideyoshi.backendportfolio.base.user.entity.Role;
|
||||||
import com.hideyoshi.backendportfolio.base.user.entity.User;
|
import com.hideyoshi.backendportfolio.base.user.entity.User;
|
||||||
import com.hideyoshi.backendportfolio.base.user.model.UserDTO;
|
import com.hideyoshi.backendportfolio.base.user.model.UserDTO;
|
||||||
@@ -34,19 +35,13 @@ public class UserServiceImpl implements UserService {
|
|||||||
throw new BadRequestException(String.format("User %s already exists. Try another UserName.", userOnDB.getUsername()));
|
throw new BadRequestException(String.format("User %s already exists. Try another UserName.", userOnDB.getUsername()));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
user.setPassword(this.validatePassword(user));
|
||||||
|
|
||||||
|
user.setRoles(this.validateRoles(user.getRoles()));
|
||||||
|
|
||||||
log.info(String.format("Saving to the database user of name: %s", user.getName()));
|
log.info(String.format("Saving to the database user of name: %s", user.getName()));
|
||||||
|
|
||||||
if (Objects.nonNull(user.getPassword())) {
|
|
||||||
user.setPassword(passwordEncoder.encode(user.getPassword()));
|
|
||||||
} else {
|
|
||||||
user.setPassword("");
|
|
||||||
}
|
|
||||||
UserDTO userSaved = new UserDTO(userRepo.save(user.toEntity()));
|
UserDTO userSaved = new UserDTO(userRepo.save(user.toEntity()));
|
||||||
|
|
||||||
if (!userSaved.getRoles().contains(Role.USER)) {
|
|
||||||
userSaved.getRoles().add(Role.USER);
|
|
||||||
}
|
|
||||||
|
|
||||||
return userSaved;
|
return userSaved;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,4 +142,31 @@ public class UserServiceImpl implements UserService {
|
|||||||
public UserDetails loadUserByUsername(String username) {
|
public UserDetails loadUserByUsername(String username) {
|
||||||
return this.getUser(username);
|
return this.getUser(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String validatePassword(UserDTO user) {
|
||||||
|
String password = null;
|
||||||
|
if (Objects.nonNull(user.getPassword())) {
|
||||||
|
password = passwordEncoder.encode(user.getPassword());
|
||||||
|
} else if (!user.getProvider().equals(Provider.LOCAL)) {
|
||||||
|
password = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Objects.isNull(password)) {
|
||||||
|
throw new BadRequestException("Password cannot be empty.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Role> validateRoles(List<Role> roles) {
|
||||||
|
if (Objects.isNull(roles)) {
|
||||||
|
roles = List.of(Role.USER);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!roles.contains(Role.USER)) {
|
||||||
|
roles.add(Role.USER);
|
||||||
|
}
|
||||||
|
|
||||||
|
return roles;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,10 @@ package com.hideyoshi.backendportfolio.microservice.storageService.model;
|
|||||||
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.*;
|
||||||
import lombok.Data;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.NonNull;
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class StorageServiceUploadResponse {
|
public class StorageServiceUploadResponse {
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.hideyoshi.backendportfolio.microservice.storageService.service;
|
package com.hideyoshi.backendportfolio.microservice.storageService.service;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.hideyoshi.backendportfolio.microservice.storageService.config.StorageServiceConfig;
|
import com.hideyoshi.backendportfolio.microservice.storageService.config.StorageServiceConfig;
|
||||||
import com.hideyoshi.backendportfolio.microservice.storageService.enums.FileTypeEnum;
|
import com.hideyoshi.backendportfolio.microservice.storageService.enums.FileTypeEnum;
|
||||||
@@ -24,6 +25,7 @@ import java.io.UnsupportedEncodingException;
|
|||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Log4j2
|
@Log4j2
|
||||||
@Service
|
@Service
|
||||||
@@ -40,49 +42,28 @@ public class StorageService {
|
|||||||
|
|
||||||
private final String PARAMETER_FILE_TYPE = "file_type";
|
private final String PARAMETER_FILE_TYPE = "file_type";
|
||||||
|
|
||||||
private final String PARAMETER_KEY_STRING = "string_url";
|
public Optional<StorageServiceUploadResponse> getNewFileUrl(String username, String filePostfix, FileTypeEnum fileTypeEnum) {
|
||||||
|
|
||||||
public StorageServiceUploadResponse getNewFileUrl(String username, String filePostfix, FileTypeEnum fileTypeEnum) {
|
|
||||||
HashMap<String, String> values = new HashMap<>() {{
|
HashMap<String, String> values = new HashMap<>() {{
|
||||||
put(PARAMETER_USERNAME, username);
|
put(PARAMETER_USERNAME, username);
|
||||||
put(PARAMETER_FILE_POSTFIX, filePostfix);
|
put(PARAMETER_FILE_POSTFIX, filePostfix);
|
||||||
put(PARAMETER_FILE_TYPE, fileTypeEnum.getFileExtension());
|
put(PARAMETER_FILE_TYPE, fileTypeEnum.getFileExtension());
|
||||||
}};
|
}};
|
||||||
|
|
||||||
String requestBody = null;
|
URI uri = URI.create(storageServiceConfig.getFileServicePath() + "/file");
|
||||||
|
String requestBody = this.writeToRequestBody(values);
|
||||||
|
|
||||||
|
StorageServiceUploadResponse uploadResponse = null;
|
||||||
try {
|
try {
|
||||||
requestBody = objectMapper
|
var response = this.postRequest(uri, requestBody);
|
||||||
.writeValueAsString(values);
|
uploadResponse = objectMapper.readValue(response, StorageServiceUploadResponse.class);
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpPost request = new HttpPost(URI.create(storageServiceConfig.getFileServicePath() + "/file"));
|
|
||||||
request.setHeader("Content-Type", "application/json");
|
|
||||||
|
|
||||||
try {
|
|
||||||
request.setEntity(new ByteArrayEntity(requestBody.getBytes("UTF-8")));
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
CloseableHttpClient httpClient = HttpClientBuilder.create()
|
|
||||||
.setRedirectStrategy(new LaxRedirectStrategy()).build();
|
|
||||||
|
|
||||||
try {
|
|
||||||
return httpClient.execute(
|
|
||||||
request,
|
|
||||||
response -> {
|
|
||||||
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
||||||
return objectMapper.readValue(responseString, StorageServiceUploadResponse.class);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
log.warn("File not found: " + username + "/" + filePostfix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Optional.ofNullable(uploadResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
public StorageServiceDownloadResponse getFileUrl(String username, String filePostfix) {
|
public Optional<StorageServiceDownloadResponse> getFileUrl(String username, String filePostfix) {
|
||||||
URI uri = null;
|
URI uri = null;
|
||||||
try {
|
try {
|
||||||
uri = new URIBuilder(storageServiceConfig.getFileServicePath() + "/file")
|
uri = new URIBuilder(storageServiceConfig.getFileServicePath() + "/file")
|
||||||
@@ -90,26 +71,20 @@ public class StorageService {
|
|||||||
.addParameter(PARAMETER_FILE_POSTFIX, filePostfix)
|
.addParameter(PARAMETER_FILE_POSTFIX, filePostfix)
|
||||||
.build();
|
.build();
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
throw new RuntimeException(e);
|
log.warn("Invalid File: " + username + "/" + filePostfix);
|
||||||
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpGet request = new HttpGet(uri);
|
|
||||||
request.setHeader("Content-Type", "application/json");
|
|
||||||
|
|
||||||
CloseableHttpClient httpClient = HttpClientBuilder.create()
|
|
||||||
.setRedirectStrategy(new LaxRedirectStrategy()).build();
|
|
||||||
|
|
||||||
|
StorageServiceDownloadResponse downloadResponse = null;
|
||||||
try {
|
try {
|
||||||
return httpClient.execute(
|
var responseString = this.getRequest(uri);
|
||||||
request,
|
downloadResponse = objectMapper.readValue(responseString, StorageServiceDownloadResponse.class);
|
||||||
response -> {
|
|
||||||
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
||||||
return objectMapper.readValue(responseString, StorageServiceDownloadResponse.class);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
log.warn("File not found: " + username + "/" + filePostfix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Optional.ofNullable(downloadResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteFile(String username, String filePostfix) {
|
public void deleteFile(String username, String filePostfix) {
|
||||||
@@ -120,24 +95,13 @@ public class StorageService {
|
|||||||
.addParameter(PARAMETER_FILE_POSTFIX, filePostfix)
|
.addParameter(PARAMETER_FILE_POSTFIX, filePostfix)
|
||||||
.build();
|
.build();
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
throw new RuntimeException(e);
|
log.warn("File not found: " + username + "/" + filePostfix);
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpDelete request = new HttpDelete(uri);
|
|
||||||
request.setHeader("Content-Type", "application/json");
|
|
||||||
|
|
||||||
CloseableHttpClient httpClient = HttpClientBuilder.create()
|
|
||||||
.setRedirectStrategy(new LaxRedirectStrategy()).build();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
httpClient.execute(
|
this.deleteRequest(uri);
|
||||||
request,
|
|
||||||
response -> {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
log.warn("File not found: " + username + "/" + filePostfix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,17 +111,31 @@ public class StorageService {
|
|||||||
put(PARAMETER_FILE_POSTFIX, filePostfix);
|
put(PARAMETER_FILE_POSTFIX, filePostfix);
|
||||||
}};
|
}};
|
||||||
|
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
URI uri = URI.create(storageServiceConfig.getFileServicePath() + "/file/process");
|
||||||
|
String requestBody = this.writeToRequestBody(values);
|
||||||
|
|
||||||
String requestBody = null;
|
|
||||||
try {
|
try {
|
||||||
requestBody = objectMapper
|
this.postRequest(uri, requestBody);
|
||||||
.writeValueAsString(values);
|
} catch (IOException e) {
|
||||||
} catch (JsonProcessingException e) {
|
log.warn("File not found: " + username + "/" + filePostfix);
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
HttpPost request = new HttpPost(URI.create(storageServiceConfig.getFileServicePath() + "/file/process"));
|
protected String getRequest(URI requestURI) throws IOException {
|
||||||
|
HttpGet request = new HttpGet(requestURI);
|
||||||
|
request.setHeader("Content-Type", "application/json");
|
||||||
|
|
||||||
|
CloseableHttpClient httpClient = HttpClientBuilder.create()
|
||||||
|
.setRedirectStrategy(new LaxRedirectStrategy()).build();
|
||||||
|
|
||||||
|
return httpClient.execute(
|
||||||
|
request,
|
||||||
|
response -> EntityUtils.toString(response.getEntity(), "UTF-8")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String postRequest(URI requestURI, String requestBody) throws IOException {
|
||||||
|
HttpPost request = new HttpPost(requestURI);
|
||||||
request.setHeader("Content-Type", "application/json");
|
request.setHeader("Content-Type", "application/json");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -169,17 +147,36 @@ public class StorageService {
|
|||||||
CloseableHttpClient httpClient = HttpClientBuilder.create()
|
CloseableHttpClient httpClient = HttpClientBuilder.create()
|
||||||
.setRedirectStrategy(new LaxRedirectStrategy()).build();
|
.setRedirectStrategy(new LaxRedirectStrategy()).build();
|
||||||
|
|
||||||
|
return httpClient.execute(
|
||||||
|
request,
|
||||||
|
response -> EntityUtils.toString(response.getEntity(), "UTF-8")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void deleteRequest(URI requestURI) throws IOException {
|
||||||
|
HttpDelete request = new HttpDelete(requestURI);
|
||||||
|
request.setHeader("Content-Type", "application/json");
|
||||||
|
|
||||||
|
CloseableHttpClient httpClient = HttpClientBuilder.create()
|
||||||
|
.setRedirectStrategy(new LaxRedirectStrategy()).build();
|
||||||
|
|
||||||
|
httpClient.execute(
|
||||||
|
request,
|
||||||
|
response -> {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String writeToRequestBody(HashMap<String, String> values) {
|
||||||
|
String requestBody = null;
|
||||||
try {
|
try {
|
||||||
httpClient.execute(
|
requestBody = objectMapper
|
||||||
request,
|
.writeValueAsString(values);
|
||||||
response -> {
|
} catch (JsonProcessingException e) {
|
||||||
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
||||||
return objectMapper.readValue(responseString, StorageServiceUploadResponse.class);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
return requestBody;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,19 +32,16 @@ import static org.mockito.Mockito.verify;
|
|||||||
@DirtiesContext(classMode= DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
|
@DirtiesContext(classMode= DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
|
||||||
class UserServiceImplTest {
|
class UserServiceImplTest {
|
||||||
|
|
||||||
@InjectMocks
|
|
||||||
private UserServiceImpl underTest;
|
private UserServiceImpl underTest;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private UserRepository userRepository;
|
private UserRepository userRepository;
|
||||||
|
|
||||||
private PasswordEncoder passwordEncoder;
|
|
||||||
|
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
this.passwordEncoder = new BCryptPasswordEncoder();
|
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||||
this.underTest = new UserServiceImpl(userRepository,passwordEncoder);
|
this.underTest = new UserServiceImpl(userRepository, passwordEncoder);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -58,8 +55,10 @@ class UserServiceImplTest {
|
|||||||
|
|
||||||
// Given
|
// Given
|
||||||
UserDTO user = this.createUser();
|
UserDTO user = this.createUser();
|
||||||
|
|
||||||
// When
|
// When
|
||||||
UserDTO userSaved = this.underTest.saveUser(user);
|
UserDTO userSaved = this.underTest.saveUser(user);
|
||||||
|
|
||||||
//Then
|
//Then
|
||||||
ArgumentCaptor<User> userArgumentCaptor = ArgumentCaptor.forClass(User.class);
|
ArgumentCaptor<User> userArgumentCaptor = ArgumentCaptor.forClass(User.class);
|
||||||
|
|
||||||
@@ -68,6 +67,30 @@ class UserServiceImplTest {
|
|||||||
assertThat(userSaved).isInstanceOf(UserDTO.class);
|
assertThat(userSaved).isInstanceOf(UserDTO.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void canSaveOAuthUser() {
|
||||||
|
|
||||||
|
BDDMockito.when(userRepository.findByUsername(ArgumentMatchers.any(String.class)))
|
||||||
|
.thenReturn(Optional.ofNullable(null));
|
||||||
|
|
||||||
|
BDDMockito.when(userRepository.save(ArgumentMatchers.any(User.class)))
|
||||||
|
.thenReturn(createOAuthUser().toEntity());
|
||||||
|
|
||||||
|
// Given
|
||||||
|
UserDTO user = this.createOAuthUser();
|
||||||
|
|
||||||
|
// When
|
||||||
|
UserDTO userSaved = this.underTest.saveUser(user);
|
||||||
|
|
||||||
|
//Then
|
||||||
|
ArgumentCaptor<User> userArgumentCaptor = ArgumentCaptor.forClass(User.class);
|
||||||
|
|
||||||
|
verify(userRepository).save(userArgumentCaptor.capture());
|
||||||
|
assertThat(userArgumentCaptor.getValue()).isEqualTo(user.toEntity());
|
||||||
|
assertThat(userArgumentCaptor.getValue().getPassword()).isEmpty();
|
||||||
|
assertThat(userSaved).isInstanceOf(UserDTO.class);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void cannotSaveUser() {
|
void cannotSaveUser() {
|
||||||
|
|
||||||
@@ -77,7 +100,6 @@ class UserServiceImplTest {
|
|||||||
|
|
||||||
// Given
|
// Given
|
||||||
UserDTO user = this.createUser();
|
UserDTO user = this.createUser();
|
||||||
// When
|
|
||||||
//Then
|
//Then
|
||||||
assertThrows(
|
assertThrows(
|
||||||
BadRequestException.class,
|
BadRequestException.class,
|
||||||
@@ -88,6 +110,29 @@ class UserServiceImplTest {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void cannotSaveUserWithEmptyPassword() {
|
||||||
|
|
||||||
|
BDDMockito.when(userRepository.findByUsername(ArgumentMatchers.any(String.class)))
|
||||||
|
.thenReturn(Optional.ofNullable(null));
|
||||||
|
|
||||||
|
|
||||||
|
// Given
|
||||||
|
UserDTO user = this.createUser();
|
||||||
|
|
||||||
|
// When
|
||||||
|
user.setPassword(null);
|
||||||
|
|
||||||
|
//Then
|
||||||
|
assertThrows(
|
||||||
|
BadRequestException.class,
|
||||||
|
() -> {
|
||||||
|
this.underTest.saveUser(user);
|
||||||
|
},
|
||||||
|
"Password cannot be empty."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void canAlterUser() {
|
void canAlterUser() {
|
||||||
BDDMockito.when(userRepository.findById(ArgumentMatchers.any(Long.class)))
|
BDDMockito.when(userRepository.findById(ArgumentMatchers.any(Long.class)))
|
||||||
@@ -125,6 +170,7 @@ class UserServiceImplTest {
|
|||||||
@Test
|
@Test
|
||||||
void canAddRoleToUser() {
|
void canAddRoleToUser() {
|
||||||
UserDTO user = this.createUser();
|
UserDTO user = this.createUser();
|
||||||
|
user.setRoles(List.of());
|
||||||
|
|
||||||
BDDMockito.when(userRepository.findById(ArgumentMatchers.any(Long.class)))
|
BDDMockito.when(userRepository.findById(ArgumentMatchers.any(Long.class)))
|
||||||
.thenReturn(Optional.ofNullable(user.toEntity()));
|
.thenReturn(Optional.ofNullable(user.toEntity()));
|
||||||
@@ -347,6 +393,40 @@ class UserServiceImplTest {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void canDeleteUser() {
|
||||||
|
UserDTO user = this.createUser();
|
||||||
|
|
||||||
|
BDDMockito.when(userRepository.findById(ArgumentMatchers.any(Long.class)))
|
||||||
|
.thenReturn(Optional.ofNullable(user.toEntity()));
|
||||||
|
|
||||||
|
// When
|
||||||
|
this.underTest.deleteUser(user.getId());
|
||||||
|
// Then
|
||||||
|
ArgumentCaptor<User> argumentCaptor = ArgumentCaptor.forClass(User.class);
|
||||||
|
|
||||||
|
verify(userRepository).delete(argumentCaptor.capture());
|
||||||
|
assertThat(argumentCaptor.getValue().getId()).isEqualTo(user.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void cannotDeleteUser() {
|
||||||
|
UserDTO user = this.createUser();
|
||||||
|
|
||||||
|
BDDMockito.when(userRepository.findById(ArgumentMatchers.any(Long.class)))
|
||||||
|
.thenReturn(Optional.ofNullable(null));
|
||||||
|
|
||||||
|
// When
|
||||||
|
// Then
|
||||||
|
assertThrows(
|
||||||
|
BadRequestException.class,
|
||||||
|
() -> {
|
||||||
|
this.underTest.deleteUser(user.getId());
|
||||||
|
},
|
||||||
|
"User doesn't exist."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private UserDTO createUser() {
|
private UserDTO createUser() {
|
||||||
return UserDTO.builder()
|
return UserDTO.builder()
|
||||||
.id(1L)
|
.id(1L)
|
||||||
@@ -355,8 +435,16 @@ class UserServiceImplTest {
|
|||||||
.username("Superman")
|
.username("Superman")
|
||||||
.password("password")
|
.password("password")
|
||||||
.provider(Provider.LOCAL)
|
.provider(Provider.LOCAL)
|
||||||
.roles(List.of(Role.USER))
|
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private UserDTO createOAuthUser() {
|
||||||
|
return UserDTO.builder()
|
||||||
|
.id(1L)
|
||||||
|
.name("Clark Kent")
|
||||||
|
.email("superman@gmail.com")
|
||||||
|
.username("Superman")
|
||||||
|
.provider(Provider.GOOGLE)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,198 @@
|
|||||||
|
package com.hideyoshi.backendportfolio.microservice.storageService.service;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.hideyoshi.backendportfolio.microservice.storageService.config.StorageServiceConfig;
|
||||||
|
import com.hideyoshi.backendportfolio.microservice.storageService.enums.FileTypeEnum;
|
||||||
|
import com.hideyoshi.backendportfolio.microservice.storageService.model.StorageServiceDownloadResponse;
|
||||||
|
import com.hideyoshi.backendportfolio.microservice.storageService.model.StorageServiceUploadResponse;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
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.test.annotation.DirtiesContext;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
@DirtiesContext(classMode= DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
|
||||||
|
public class StorageServiceTest {
|
||||||
|
|
||||||
|
private StorageService storageService;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
StorageServiceConfig config = new StorageServiceConfig();
|
||||||
|
|
||||||
|
this.storageService = new StorageService(new ObjectMapper(), config);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetNewFileUrlIfFileExists() {
|
||||||
|
StorageService storageService = Mockito.spy(this.storageService);
|
||||||
|
|
||||||
|
// Given
|
||||||
|
String username = "test";
|
||||||
|
String filePostfix = "test";
|
||||||
|
FileTypeEnum fileTypeEnum = FileTypeEnum.JPEG;
|
||||||
|
|
||||||
|
// When
|
||||||
|
try {
|
||||||
|
String responseString = "{\"presigned_url\":\"https://test.com\", \"file_key\":\"test\"}";
|
||||||
|
Mockito.doReturn(responseString).when(storageService).postRequest(Mockito.any(), Mockito.any());
|
||||||
|
} catch (IOException e) {
|
||||||
|
assert false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then
|
||||||
|
var response = storageService.getNewFileUrl(username, filePostfix, fileTypeEnum);
|
||||||
|
|
||||||
|
assertThat(response).isPresent();
|
||||||
|
assertThat(response.get()).isInstanceOf(StorageServiceUploadResponse.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetNewFileUrlIfFileDoesNotExist() {
|
||||||
|
StorageService storageService = Mockito.spy(this.storageService);
|
||||||
|
|
||||||
|
// Given
|
||||||
|
String username = "test";
|
||||||
|
String filePostfix = "test";
|
||||||
|
FileTypeEnum fileTypeEnum = FileTypeEnum.JPEG;
|
||||||
|
|
||||||
|
// When
|
||||||
|
try {
|
||||||
|
Mockito.doThrow(new IOException()).when(storageService).postRequest(Mockito.any(), Mockito.any());
|
||||||
|
} catch (IOException e) {
|
||||||
|
assert false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then
|
||||||
|
var response = storageService.getNewFileUrl(username, filePostfix, fileTypeEnum);
|
||||||
|
|
||||||
|
assertThat(response).isNotPresent();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getFileUrlIfExists() {
|
||||||
|
StorageService storageService = Mockito.spy(this.storageService);
|
||||||
|
|
||||||
|
// Given
|
||||||
|
String username = "test";
|
||||||
|
String filePostfix = "test";
|
||||||
|
|
||||||
|
// When
|
||||||
|
try {
|
||||||
|
String responseString = "{\"presigned_url\":\"http://test.com\"}";
|
||||||
|
Mockito.doReturn(responseString).when(storageService).getRequest(Mockito.any());
|
||||||
|
} catch (IOException e) {
|
||||||
|
assert false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then
|
||||||
|
var response = storageService.getFileUrl(username, filePostfix);
|
||||||
|
|
||||||
|
assertThat(response).isPresent();
|
||||||
|
assertThat(response.get()).isInstanceOf(StorageServiceDownloadResponse.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getFileUrlIfNotExists() {
|
||||||
|
StorageService storageService = Mockito.spy(this.storageService);
|
||||||
|
|
||||||
|
// Given
|
||||||
|
String username = "test";
|
||||||
|
String filePostfix = "test";
|
||||||
|
|
||||||
|
// When
|
||||||
|
try {
|
||||||
|
Mockito.doThrow(new IOException()).when(storageService).getRequest(Mockito.any());
|
||||||
|
} catch (IOException e) {
|
||||||
|
assert false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then
|
||||||
|
var response = storageService.getFileUrl(username, filePostfix);
|
||||||
|
|
||||||
|
assertThat(response).isNotPresent();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void deleteFileIfExists() {
|
||||||
|
StorageService storageService = Mockito.spy(this.storageService);
|
||||||
|
|
||||||
|
// Given
|
||||||
|
String username = "test";
|
||||||
|
String filePostfix = "test";
|
||||||
|
|
||||||
|
// When
|
||||||
|
try {
|
||||||
|
Mockito.doNothing().when(storageService).deleteRequest(Mockito.any());
|
||||||
|
} catch (IOException e) {
|
||||||
|
assert false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then
|
||||||
|
storageService.deleteFile(username, filePostfix);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void deleteFileIfNotExists() {
|
||||||
|
StorageService storageService = Mockito.spy(this.storageService);
|
||||||
|
|
||||||
|
// Given
|
||||||
|
String username = "test";
|
||||||
|
String filePostfix = "test";
|
||||||
|
|
||||||
|
// When
|
||||||
|
try {
|
||||||
|
Mockito.doThrow(new IOException()).when(storageService).deleteRequest(Mockito.any());
|
||||||
|
} catch (IOException e) {
|
||||||
|
assert false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then
|
||||||
|
storageService.deleteFile(username, filePostfix);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void processFileIfExists() {
|
||||||
|
StorageService storageService = Mockito.spy(this.storageService);
|
||||||
|
|
||||||
|
// Given
|
||||||
|
String username = "test";
|
||||||
|
String filePostfix = "test";
|
||||||
|
|
||||||
|
// When
|
||||||
|
try {
|
||||||
|
Mockito.doNothing().when(storageService).deleteRequest(Mockito.any());
|
||||||
|
} catch (IOException e) {
|
||||||
|
assert false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then
|
||||||
|
storageService.deleteFile(username, filePostfix);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void processFileIfNotExists() {
|
||||||
|
StorageService storageService = Mockito.spy(this.storageService);
|
||||||
|
|
||||||
|
// Given
|
||||||
|
String username = "test";
|
||||||
|
String filePostfix = "test";
|
||||||
|
|
||||||
|
// When
|
||||||
|
try {
|
||||||
|
Mockito.doThrow(new IOException()).when(storageService).deleteRequest(Mockito.any());
|
||||||
|
} catch (IOException e) {
|
||||||
|
assert false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then
|
||||||
|
storageService.deleteFile(username, filePostfix);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -13,6 +13,9 @@ com:
|
|||||||
username: "YoshiUnfriendly"
|
username: "YoshiUnfriendly"
|
||||||
password: "passwd"
|
password: "passwd"
|
||||||
|
|
||||||
|
microservice:
|
||||||
|
storageServicePath: undertest
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
|
|
||||||
liquibase:
|
liquibase:
|
||||||
|
|||||||
Reference in New Issue
Block a user