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

Commit 704ca1b

Browse files
authored
FF-219 Implemented dummy Responses for ShowContents. (#50)
* Implemented dummy Responses for ShowContents. * Fixed Pipeline * Fixed a bug with wrong paths, added one more layer.
1 parent b4b3e53 commit 704ca1b

File tree

10 files changed

+119
-8
lines changed

10 files changed

+119
-8
lines changed

.run/Run Tests with Coverage.run.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<option name="goals">
1212
<list>
1313
<option value="clean" />
14-
<option value="test" />
14+
<option value="verify" />
1515
</list>
1616
</option>
1717
<option name="pomFileName" value="pom.xml" />

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@
171171
</includes>
172172
<excludes>
173173
<!-- NOT IMPLEMENTED YET -->
174+
<!-- TODO: remove fs -->
174175
<exclude>*FileSystemRestService</exclude>
175176
<exclude>*PermissionRestService</exclude>
177+
<exclude>*FileSystemBusinessService</exclude>
176178
</excludes>
177179
<limits>
178180
<limit>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,60 @@
11
package de.filefighter.rest.domain.filesystem.business;
22

3+
import de.filefighter.rest.domain.filesystem.data.dto.File;
4+
import de.filefighter.rest.domain.filesystem.data.dto.Folder;
5+
import de.filefighter.rest.domain.filesystem.data.dto.FolderContents;
6+
import de.filefighter.rest.domain.filesystem.exceptions.FileSystemContentsNotAccessibleException;
7+
import de.filefighter.rest.domain.filesystem.type.FileSystemType;
8+
import de.filefighter.rest.domain.user.data.dto.User;
39
import org.springframework.stereotype.Service;
410

11+
import java.time.Instant;
12+
513
@Service
614
public class FileSystemBusinessService {
15+
16+
public FileSystemBusinessService() {
17+
18+
}
19+
20+
public static FolderContents getContentsOfFolder(String path, User authenticatedUser) {
21+
FolderContents folderContents;
22+
switch (path) {
23+
case "/":
24+
folderContents = FolderContents.builder()
25+
.files(new File[]{new File(0, "DummyFileInRoot.txt", 420, 0, Instant.now().getEpochSecond(), FileSystemType.TEXT, null)})
26+
.folders(new Folder[]{
27+
new Folder(1, "/bla", "bla", 12345, 0, Instant.now().getEpochSecond(), null),
28+
new Folder(2, "/fasel", "fasel", 12345, 0, Instant.now().getEpochSecond(), null)
29+
})
30+
.build();
31+
break;
32+
case "/bla":
33+
folderContents = FolderContents.builder()
34+
.files(new File[]{
35+
new File(3, "DummyFileInBla.pdf", 42, 0, Instant.now().getEpochSecond(), FileSystemType.PDF, null),
36+
new File(4, "DummyFileInBla1.jpg", 1234321, 0, Instant.now().getEpochSecond(), FileSystemType.PICTURE, null)
37+
})
38+
.build();
39+
break;
40+
case "/fasel":
41+
folderContents = FolderContents.builder()
42+
.files(new File[]{new File(5, "DummyFileInFasel.txt", 420, 0, Instant.now().getEpochSecond(), FileSystemType.TEXT, null)})
43+
.folders(new Folder[]{new Folder(6, "/fasel/johndoessecretchamber", "JohnDoesSecretChamber", 12345, 0, Instant.now().getEpochSecond(), null)})
44+
.build();
45+
break;
46+
case "/fasel/johndoessecretchamber":
47+
folderContents = FolderContents.builder()
48+
.folders(new Folder[]{new Folder(7, "/fasel/johndoessecretchamber/empty", "Empty", 12345, 0, Instant.now().getEpochSecond(), null)})
49+
.build();
50+
break;
51+
case "/fasel/johndoessecretchamber/empty":
52+
folderContents = FolderContents.builder().build();
53+
break;
54+
default:
55+
throw new FileSystemContentsNotAccessibleException();
56+
57+
}
58+
return folderContents;
59+
}
760
}

src/main/java/de/filefighter/rest/domain/filesystem/data/dto/Folder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public class Folder extends FileSystemItem {
99
public Folder() {
1010
}
1111

12-
public Folder(long fileSystemId, String name, double size, long createdByUserId, long lastUpdated, FileSystemType type, PermissionSet permissionSet, String path) {
13-
super(fileSystemId, name, size, createdByUserId, lastUpdated, type, permissionSet);
12+
public Folder(long id, String path, String name, double size, long createdByUserId, long lastUpdated, PermissionSet permissionSet) {
13+
super(id, name, size, createdByUserId, lastUpdated, FileSystemType.FOLDER, permissionSet);
1414
this.path = path;
1515
}
1616

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package de.filefighter.rest.domain.filesystem.exceptions;
2+
3+
import de.filefighter.rest.rest.ServerResponse;
4+
import org.slf4j.LoggerFactory;
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+
@ControllerAdvice
13+
public class FileSystemContentsNotAccessibleAdvise {
14+
15+
@ResponseBody
16+
@ExceptionHandler(FileSystemContentsNotAccessibleException.class)
17+
@ResponseStatus(HttpStatus.BAD_REQUEST)
18+
ResponseEntity<ServerResponse> fileSystemContentsNotAccessibleAdvise(FileSystemContentsNotAccessibleException ex) {
19+
LoggerFactory.getLogger(FileSystemContentsNotAccessibleException.class).warn(ex.getMessage());
20+
return new ResponseEntity<>(new ServerResponse(HttpStatus.BAD_REQUEST, ex.getMessage()), HttpStatus.BAD_REQUEST);
21+
}
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package de.filefighter.rest.domain.filesystem.exceptions;
2+
3+
public class FileSystemContentsNotAccessibleException extends RuntimeException {
4+
5+
public FileSystemContentsNotAccessibleException() {
6+
super("Folder does not exist, or you are not allowed to see the folder.");
7+
}
8+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public ResponseEntity<FolderContents> getContentsOfFolder(
3232
) {
3333

3434
LOG.info("Requested Folder contents of folder with path {}.", path);
35-
return fileSystemRestService.getContentsOfFolderByIdAndAccessToken(path, accessToken);
35+
return fileSystemRestService.getContentsOfFolderByPathAndAccessToken(path, accessToken);
3636
}
3737

3838
@GetMapping(FS_BASE_URI + "{fsItemId}/info")

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,44 @@
11
package de.filefighter.rest.domain.filesystem.rest;
22

3+
import de.filefighter.rest.configuration.RestConfiguration;
4+
import de.filefighter.rest.domain.common.InputSanitizerService;
5+
import de.filefighter.rest.domain.filesystem.business.FileSystemBusinessService;
36
import de.filefighter.rest.domain.filesystem.data.dto.FileSystemItem;
47
import de.filefighter.rest.domain.filesystem.data.dto.FileSystemItemUpdate;
58
import de.filefighter.rest.domain.filesystem.data.dto.FolderContents;
9+
import de.filefighter.rest.domain.token.business.AccessTokenBusinessService;
10+
import de.filefighter.rest.domain.token.data.dto.AccessToken;
11+
import de.filefighter.rest.domain.user.business.UserAuthorizationService;
12+
import de.filefighter.rest.domain.user.data.dto.User;
613
import de.filefighter.rest.rest.ServerResponse;
14+
import org.springframework.http.HttpStatus;
715
import org.springframework.http.ResponseEntity;
816
import org.springframework.stereotype.Service;
917

1018
@Service
1119
public class FileSystemRestService implements FileSystemRestServiceInterface {
1220

21+
private final UserAuthorizationService userAuthorizationService;
22+
private final InputSanitizerService inputSanitizerService;
23+
private final AccessTokenBusinessService accessTokenBusinessService;
24+
private final FileSystemBusinessService fileSystemBusinessService;
25+
26+
public FileSystemRestService(UserAuthorizationService userAuthorizationService, InputSanitizerService inputSanitizerService, AccessTokenBusinessService accessTokenBusinessService, FileSystemBusinessService fileSystemBusinessService) {
27+
this.userAuthorizationService = userAuthorizationService;
28+
this.inputSanitizerService = inputSanitizerService;
29+
this.accessTokenBusinessService = accessTokenBusinessService;
30+
this.fileSystemBusinessService = fileSystemBusinessService;
31+
}
32+
1333
@Override
14-
public ResponseEntity<FolderContents> getContentsOfFolderByIdAndAccessToken(String path, String accessToken) {
15-
return null;
34+
public ResponseEntity<FolderContents> getContentsOfFolderByPathAndAccessToken(String path, String accessTokenValue) {
35+
String cleanHeader = inputSanitizerService.sanitizeRequestHeader(RestConfiguration.AUTHORIZATION_BEARER_PREFIX, accessTokenValue);
36+
String cleanValue = inputSanitizerService.sanitizeTokenValue(cleanHeader);
37+
AccessToken accessToken = accessTokenBusinessService.findAccessTokenByValue(cleanValue);
38+
User authenticatedUser = userAuthorizationService.authenticateUserWithAccessToken(accessToken);
39+
40+
FolderContents folderContents = FileSystemBusinessService.getContentsOfFolder(path, authenticatedUser);
41+
return new ResponseEntity<>(folderContents, HttpStatus.OK);
1642
}
1743

1844
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.springframework.http.ResponseEntity;
88

99
public interface FileSystemRestServiceInterface {
10-
ResponseEntity<FolderContents> getContentsOfFolderByIdAndAccessToken(String path, String accessToken);
10+
ResponseEntity<FolderContents> getContentsOfFolderByPathAndAccessToken(String path, String accessToken);
1111
ResponseEntity<FileSystemItem> getInfoAboutFileOrFolderByIdAndAccessToken(long fsItemId, String accessToken);
1212
ResponseEntity<FileSystemItem> findFileOrFolderByNameAndAccessToken(String name, String accessToken);
1313
ResponseEntity<FileSystemItem> uploadFileSystemItemWithAccessToken(FileSystemItemUpdate fileSystemItemUpdate, String accessToken);

src/test/java/de/filefighter/rest/domain/filesystem/rest/FileSystemRestControllerUnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void getContentsOfFolder() {
3333
String path= "/root/data.txt";
3434
String token = "token";
3535

36-
when(fileSystemRestServiceMock.getContentsOfFolderByIdAndAccessToken(path, token)).thenReturn(expectedModel);
36+
when(fileSystemRestServiceMock.getContentsOfFolderByPathAndAccessToken(path, token)).thenReturn(expectedModel);
3737

3838
ResponseEntity<FolderContents> actualModel = fileSystemRestController.getContentsOfFolder(path, token);
3939
assertEquals(expectedModel, actualModel);

0 commit comments

Comments
 (0)