Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 4b6508c

Browse files
authored
FF-245 Implement getFileSystemItemInfo (#56)
* FF-245 Implemented Logic and Unit Tests. * FF-245 Implemented Cucumber Tests, added Advise and fixed StatusCode
1 parent 94cbdad commit 4b6508c

File tree

7 files changed

+149
-10
lines changed

7 files changed

+149
-10
lines changed

src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemBusinessService.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import de.filefighter.rest.domain.filesystem.data.persistence.FileSystemEntity;
66
import de.filefighter.rest.domain.filesystem.data.persistence.FileSystemRepository;
77
import de.filefighter.rest.domain.filesystem.exceptions.FileSystemContentsNotAccessibleException;
8+
import de.filefighter.rest.domain.filesystem.exceptions.FileSystemItemNotFoundException;
89
import de.filefighter.rest.domain.filesystem.type.FileSystemType;
910
import de.filefighter.rest.domain.filesystem.type.FileSystemTypeRepository;
1011
import de.filefighter.rest.domain.user.business.UserBusinessService;
@@ -86,6 +87,17 @@ public List<FileSystemItem> getFolderContentsOfEntities(List<FileSystemEntity> l
8687
return fileSystemItems;
8788
}
8889

90+
public FileSystemItem getFileSystemItemInfo(long fsItemId, User authenticatedUser) {
91+
FileSystemEntity fileSystemEntity = fileSystemRepository.findByFileSystemId(fsItemId);
92+
if (null == fileSystemEntity)
93+
throw new FileSystemItemNotFoundException(fsItemId);
94+
95+
if (!userIsAllowedToSeeFileSystemEntity(fileSystemEntity, authenticatedUser))
96+
throw new FileSystemItemNotFoundException(fsItemId);
97+
98+
return createDTO(fileSystemEntity, authenticatedUser, null);
99+
}
100+
89101
public String removeTrailingBackSlashes(String pathToFind) {
90102
char[] chars = pathToFind.toCharArray();
91103
// for the case of "/"
@@ -133,7 +145,7 @@ public FileSystemItem createDTO(FileSystemEntity fileSystemEntity, User authenti
133145
.name(fileSystemEntity.getName())
134146
.size(fileSystemEntity.getSize())
135147
.type(isAFolder ? FileSystemType.FOLDER : type)
136-
.path(basePath + fileSystemEntity.getName())
148+
.path(null == basePath ? null : basePath + fileSystemEntity.getName())
137149
.isShared(isShared)
138150
.build();
139151
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package de.filefighter.rest.domain.filesystem.exceptions;
2+
3+
import de.filefighter.rest.rest.ServerResponse;
4+
import lombok.extern.log4j.Log4j2;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.ControllerAdvice;
8+
import org.springframework.web.bind.annotation.ExceptionHandler;
9+
import org.springframework.web.bind.annotation.ResponseBody;
10+
import org.springframework.web.bind.annotation.ResponseStatus;
11+
12+
@Log4j2
13+
@ControllerAdvice
14+
public class FileSystemItemNotFoundAdvise {
15+
16+
@ResponseBody
17+
@ExceptionHandler(FileSystemItemNotFoundException.class)
18+
@ResponseStatus(HttpStatus.BAD_REQUEST)
19+
ResponseEntity<ServerResponse> fileSystemContentsNotAccessibleAdvise(FileSystemItemNotFoundException ex) {
20+
log.warn(ex.getMessage());
21+
return new ResponseEntity<>(new ServerResponse(HttpStatus.BAD_REQUEST, ex.getMessage()), HttpStatus.BAD_REQUEST);
22+
}
23+
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package de.filefighter.rest.domain.filesystem.exceptions;
2+
3+
public class FileSystemItemNotFoundException extends RuntimeException {
4+
5+
public FileSystemItemNotFoundException() {
6+
super("FileSystemItem could not be found or you are not allowed to view it.");
7+
}
8+
9+
public FileSystemItemNotFoundException(long fsItemId) {
10+
super("FileSystemItem with id " + fsItemId + " could not be found or you are not allowed to view it.");
11+
}
12+
}

src/main/java/de/filefighter/rest/domain/filesystem/rest/FileSystemRestService.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package de.filefighter.rest.domain.filesystem.rest;
22

3-
import de.filefighter.rest.configuration.RestConfiguration;
43
import de.filefighter.rest.domain.common.InputSanitizerService;
54
import de.filefighter.rest.domain.filesystem.business.FileSystemBusinessService;
65
import de.filefighter.rest.domain.filesystem.data.dto.FileSystemItem;
@@ -16,6 +15,8 @@
1615

1716
import java.util.ArrayList;
1817

18+
import static de.filefighter.rest.configuration.RestConfiguration.AUTHORIZATION_BEARER_PREFIX;
19+
1920
@Service
2021
public class FileSystemRestService implements FileSystemRestServiceInterface {
2122

@@ -33,7 +34,7 @@ public FileSystemRestService(UserAuthorizationService userAuthorizationService,
3334

3435
@Override
3536
public ResponseEntity<ArrayList<FileSystemItem>> getContentsOfFolderByPathAndAccessToken(String path, String accessTokenValue) {
36-
String cleanHeader = inputSanitizerService.sanitizeRequestHeader(RestConfiguration.AUTHORIZATION_BEARER_PREFIX, accessTokenValue);
37+
String cleanHeader = inputSanitizerService.sanitizeRequestHeader(AUTHORIZATION_BEARER_PREFIX, accessTokenValue);
3738
String cleanValue = inputSanitizerService.sanitizeTokenValue(cleanHeader);
3839
AccessToken accessToken = accessTokenBusinessService.findAccessTokenByValue(cleanValue);
3940
User authenticatedUser = userAuthorizationService.authenticateUserWithAccessToken(accessToken);
@@ -44,8 +45,13 @@ public ResponseEntity<ArrayList<FileSystemItem>> getContentsOfFolderByPathAndAcc
4445
}
4546

4647
@Override
47-
public ResponseEntity<FileSystemItem> getInfoAboutFileOrFolderByIdAndAccessToken(long fsItemId, String accessToken) {
48-
return null;
48+
public ResponseEntity<FileSystemItem> getInfoAboutFileOrFolderByIdAndAccessToken(long fsItemId, String accessTokenValue) {
49+
String cleanHeader = inputSanitizerService.sanitizeRequestHeader(AUTHORIZATION_BEARER_PREFIX, accessTokenValue);
50+
String cleanValue = inputSanitizerService.sanitizeTokenValue(cleanHeader);
51+
AccessToken accessToken = accessTokenBusinessService.findAccessTokenByValue(cleanValue);
52+
User authenticatedUser = userAuthorizationService.authenticateUserWithAccessToken(accessToken);
53+
54+
return new ResponseEntity<>(fileSystemBusinessService.getFileSystemItemInfo(fsItemId, authenticatedUser), HttpStatus.OK);
4955
}
5056

5157
@Override
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package de.filefighter.rest.cucumber;
2+
3+
import de.filefighter.rest.RestApplicationIntegrationTest;
4+
import io.cucumber.java.en.When;
5+
import org.springframework.http.HttpMethod;
6+
7+
import java.util.HashMap;
8+
9+
import static de.filefighter.rest.configuration.RestConfiguration.*;
10+
11+
public class CrudFileSystemSteps extends RestApplicationIntegrationTest {
12+
13+
@When("user requests fileSystemInfo with fileSystemId {long} and accessTokenValue {string}")
14+
public void userRequestsFileSystemInfoWithFileSystemIdAndUserId(long fileSystemId, String accessTokenValue) {
15+
String authHeaderString = AUTHORIZATION_BEARER_PREFIX + accessTokenValue;
16+
String url = BASE_API_URI + USER_BASE_URI + "auth";
17+
18+
HashMap<String, String> authHeader = new HashMap<>();
19+
authHeader.put("Authorization", authHeaderString);
20+
21+
executeRestApiCall(HttpMethod.GET, BASE_API_URI + FS_BASE_URI + fileSystemId + "/info", authHeader);
22+
}
23+
}

src/test/java/de/filefighter/rest/domain/filesystem/business/FileSystemBusinessServiceUnitTest.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import de.filefighter.rest.domain.filesystem.data.persistence.FileSystemEntity;
55
import de.filefighter.rest.domain.filesystem.data.persistence.FileSystemRepository;
66
import de.filefighter.rest.domain.filesystem.exceptions.FileSystemContentsNotAccessibleException;
7+
import de.filefighter.rest.domain.filesystem.exceptions.FileSystemItemNotFoundException;
78
import de.filefighter.rest.domain.filesystem.type.FileSystemType;
89
import de.filefighter.rest.domain.filesystem.type.FileSystemTypeRepository;
910
import de.filefighter.rest.domain.user.business.UserBusinessService;
@@ -21,9 +22,9 @@
2122
class FileSystemBusinessServiceUnitTest {
2223

2324
private final FileSystemRepository fileSystemRepositoryMock = mock(FileSystemRepository.class);
24-
private final UserBusinessService userBusinessService = mock(UserBusinessService.class);
25+
private final UserBusinessService userBusinessServiceMock = mock(UserBusinessService.class);
2526
private final FileSystemTypeRepository fileSystemTypeRepository = mock(FileSystemTypeRepository.class);
26-
private final FileSystemBusinessService fileSystemBusinessService = new FileSystemBusinessService(fileSystemRepositoryMock, userBusinessService, fileSystemTypeRepository);
27+
private final FileSystemBusinessService fileSystemBusinessService = new FileSystemBusinessService(fileSystemRepositoryMock, userBusinessServiceMock, fileSystemTypeRepository);
2728

2829
@Test
2930
void getFolderContentsByPathThrows() {
@@ -77,7 +78,7 @@ void getFolderContentsByPathWorks() {
7778

7879
when(fileSystemRepositoryMock.findByPath(path)).thenReturn(entities);
7980
when(fileSystemRepositoryMock.findByFileSystemId(fileIdInFolder)).thenReturn(FileSystemEntity.builder().createdByUserId(userId).build());
80-
when(userBusinessService.getUserById(userId)).thenReturn(User.builder().build());
81+
when(userBusinessServiceMock.getUserById(userId)).thenReturn(User.builder().build());
8182

8283
ArrayList<FileSystemItem> fileSystemItems = (ArrayList<FileSystemItem>) fileSystemBusinessService.getFolderContentsByPath(pathToRequest, user);
8384
assertEquals(1, fileSystemItems.size());
@@ -113,12 +114,46 @@ void getFolderContentsOfEntityWorks() {
113114
when(fileSystemRepositoryMock.findByFileSystemId(2)).thenReturn(dummyEntity);
114115
when(fileSystemRepositoryMock.findByFileSystemId(3)).thenReturn(dummyEntity);
115116
when(fileSystemRepositoryMock.findByFileSystemId(4)).thenReturn(FileSystemEntity.builder().createdByUserId(userId + 1).build());
116-
when(userBusinessService.getUserById(userId)).thenReturn(User.builder().userId(userId).build());
117+
when(userBusinessServiceMock.getUserById(userId)).thenReturn(User.builder().userId(userId).build());
117118

118119
ArrayList<FileSystemItem> actual = (ArrayList<FileSystemItem>) fileSystemBusinessService.getFolderContentsOfEntities(arrayList, authenticatedUser, "/");
119120
assertEquals(4, actual.size());
120121
}
121122

123+
@Test
124+
void getFileSystemItemInfoThrows() {
125+
long id = 420;
126+
User dummyUser = User.builder().userId(213421234).build();
127+
128+
when(fileSystemRepositoryMock.findByFileSystemId(id)).thenReturn(null);
129+
FileSystemItemNotFoundException ex = assertThrows(FileSystemItemNotFoundException.class, () ->
130+
fileSystemBusinessService.getFileSystemItemInfo(id, dummyUser));
131+
assertEquals("FileSystemItem with id " + id + " could not be found or you are not allowed to view it.", ex.getMessage());
132+
133+
when(fileSystemRepositoryMock.findByFileSystemId(id)).thenReturn(FileSystemEntity.builder().build());
134+
ex = assertThrows(FileSystemItemNotFoundException.class, () ->
135+
fileSystemBusinessService.getFileSystemItemInfo(id, dummyUser));
136+
assertEquals("FileSystemItem with id " + id + " could not be found or you are not allowed to view it.", ex.getMessage());
137+
}
138+
139+
@Test
140+
void getFileSystemItemInfoWorks() {
141+
long id = 420;
142+
long userId = 1234321;
143+
String name = "Folder";
144+
User dummyUser = User.builder().userId(userId).build();
145+
FileSystemEntity entity = FileSystemEntity.builder().name(name).createdByUserId(userId).build();
146+
147+
when(userBusinessServiceMock.getUserById(userId)).thenReturn(dummyUser);
148+
when(fileSystemRepositoryMock.findByFileSystemId(id)).thenReturn(entity);
149+
FileSystemItem fileSystemItem = fileSystemBusinessService.getFileSystemItemInfo(id, dummyUser);
150+
assertEquals(name, fileSystemItem.getName());
151+
assertEquals(userId, fileSystemItem.getCreatedByUserId());
152+
assertNull(fileSystemItem.getPath());
153+
assertFalse(fileSystemItem.isShared());
154+
}
155+
156+
122157
@Test
123158
void removeTrailingWhiteSpaces() {
124159
String doesNotRemove0 = "/";
@@ -188,7 +223,7 @@ void createDTOWorks() {
188223
.typeId(typeId)
189224
.build();
190225

191-
when(userBusinessService.getUserById(createdByUserId)).thenReturn(userThatCreatedFile);
226+
when(userBusinessServiceMock.getUserById(createdByUserId)).thenReturn(userThatCreatedFile);
192227
when(fileSystemTypeRepository.findFileSystemTypeById(typeId)).thenReturn(FileSystemType.UNDEFINED);
193228

194229
FileSystemItem actual = fileSystemBusinessService.createDTO(fileSystemEntity, authenticatedUser, basePath);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Feature: FileSystem CRUD
2+
As a user i want to create, read, update and delete FileSystemItems.
3+
4+
Background:
5+
Given database is empty
6+
And user 1234 exists
7+
And accessToken with value "900000" exists for user 1234
8+
9+
Scenario: Get FileSystemItem works
10+
Given fileSystemItem with the fileSystemId 1234 exists, was created by user with userId 1234 and has the name "dummyFile.pdf"
11+
When user requests fileSystemInfo with fileSystemId 1234 and accessTokenValue "900000"
12+
Then response status code is 200
13+
And response contains key "fileSystemId" and value "1234"
14+
And response contains key "name" and value "dummyFile.pdf"
15+
16+
Scenario: Get FileSystemItem does not work, because it doesn't exist
17+
When user requests fileSystemInfo with fileSystemId 1234 and accessTokenValue "900000"
18+
Then response status code is 400
19+
And response contains key "status" and value "Bad Request"
20+
And response contains key "message" and value "FileSystemItem with id 1234 could not be found or you are not allowed to view it."
21+
22+
Scenario: Get FileSystemItem does not work, because user is not allowed
23+
Given fileSystemItem with the fileSystemId 1234 exists, was created by user with userId 9999999 and has the name "dummyFile.pdf"
24+
When user requests fileSystemInfo with fileSystemId 1234 and accessTokenValue "900000"
25+
Then response status code is 400
26+
And response contains key "status" and value "Bad Request"
27+
And response contains key "message" and value "FileSystemItem with id 1234 could not be found or you are not allowed to view it."

0 commit comments

Comments
 (0)