1
+ package de .filefighter .rest .domain .filesystem .business ;
2
+
3
+ import de .filefighter .rest .domain .common .InputSanitizerService ;
4
+ import de .filefighter .rest .domain .filesystem .data .InteractionType ;
5
+ import de .filefighter .rest .domain .filesystem .data .dto .FileSystemItem ;
6
+ import de .filefighter .rest .domain .filesystem .data .dto .upload .CreateNewFolder ;
7
+ import de .filefighter .rest .domain .filesystem .data .persistence .FileSystemEntity ;
8
+ import de .filefighter .rest .domain .filesystem .data .persistence .FileSystemRepository ;
9
+ import de .filefighter .rest .domain .filesystem .exceptions .FileSystemItemCouldNotBeUploadedException ;
10
+ import de .filefighter .rest .domain .filesystem .type .FileSystemTypeRepository ;
11
+ import de .filefighter .rest .domain .user .business .UserBusinessService ;
12
+ import de .filefighter .rest .domain .user .data .dto .User ;
13
+ import org .junit .jupiter .api .Test ;
14
+ import org .springframework .data .mongodb .core .MongoTemplate ;
15
+
16
+ import java .util .Collections ;
17
+
18
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
19
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
20
+ import static org .mockito .ArgumentMatchers .any ;
21
+ import static org .mockito .ArgumentMatchers .eq ;
22
+ import static org .mockito .Mockito .mock ;
23
+ import static org .mockito .Mockito .when ;
24
+
25
+ class FileSystemUploadServiceUnitTest {
26
+
27
+ private final FileSystemRepository fileSystemRepositoryMock = mock (FileSystemRepository .class );
28
+ private final FileSystemHelperService fileSystemHelperServiceMock = mock (FileSystemHelperService .class );
29
+ private final InputSanitizerService inputSanitizerServiceMock = mock (InputSanitizerService .class );
30
+ private final MongoTemplate mongoTemplateMock = mock (MongoTemplate .class );
31
+ private final FileSystemTypeRepository fileSystemTypeRepositoryMock = mock (FileSystemTypeRepository .class );
32
+ private final UserBusinessService userBusinessServiceMock = mock (UserBusinessService .class );
33
+ private final IdGenerationService idGenerationServiceMock = mock (IdGenerationService .class );
34
+
35
+ private final FileSystemUploadService fileSystemUploadService = new FileSystemUploadService (fileSystemRepositoryMock , fileSystemHelperServiceMock , inputSanitizerServiceMock , fileSystemTypeRepositoryMock , mongoTemplateMock , userBusinessServiceMock , idGenerationServiceMock );
36
+
37
+ @ Test
38
+ void createNewFolderThrows () {
39
+ long parentId = 420 ;
40
+ String folderName = "Kevin" ;
41
+ CreateNewFolder createNewFolder = new CreateNewFolder (folderName );
42
+ User autheticatedUser = User .builder ().build ();
43
+
44
+ FileSystemItemCouldNotBeUploadedException ex = assertThrows (FileSystemItemCouldNotBeUploadedException .class ,
45
+ () -> fileSystemUploadService .createNewFolder (parentId , createNewFolder , autheticatedUser ));
46
+ assertEquals (FileSystemItemCouldNotBeUploadedException .getErrorMessagePrefix () + " Could not find parent entity or you are not allowed to see it." , ex .getMessage ());
47
+
48
+ FileSystemEntity parent = FileSystemEntity .builder ().build ();
49
+ when (fileSystemRepositoryMock .findByFileSystemId (parentId )).thenReturn (parent );
50
+
51
+ ex = assertThrows (FileSystemItemCouldNotBeUploadedException .class ,
52
+ () -> fileSystemUploadService .createNewFolder (parentId , createNewFolder , autheticatedUser ));
53
+ assertEquals (FileSystemItemCouldNotBeUploadedException .getErrorMessagePrefix () + " Could not find parent entity or you are not allowed to see it." , ex .getMessage ());
54
+
55
+ when (fileSystemHelperServiceMock .userIsAllowedToInteractWithFileSystemEntity (parent , autheticatedUser , InteractionType .READ )).thenReturn (true );
56
+
57
+ ex = assertThrows (FileSystemItemCouldNotBeUploadedException .class ,
58
+ () -> fileSystemUploadService .createNewFolder (parentId , createNewFolder , autheticatedUser ));
59
+ assertEquals (FileSystemItemCouldNotBeUploadedException .getErrorMessagePrefix () + " You dont have write permissions in that directory." , ex .getMessage ());
60
+
61
+ when (fileSystemHelperServiceMock .userIsAllowedToInteractWithFileSystemEntity (parent , autheticatedUser , InteractionType .CHANGE )).thenReturn (true );
62
+ when (fileSystemHelperServiceMock .getFolderContentsOfEntityAndPermissions (parent , autheticatedUser , false , false )).thenReturn (Collections .singletonList (FileSystemEntity .builder ().name (folderName .toUpperCase ()).build ()));
63
+
64
+ ex = assertThrows (FileSystemItemCouldNotBeUploadedException .class ,
65
+ () -> fileSystemUploadService .createNewFolder (parentId , createNewFolder , autheticatedUser ));
66
+ assertEquals (FileSystemItemCouldNotBeUploadedException .getErrorMessagePrefix () + " A Entity with the same name already exists in this directory." , ex .getMessage ());
67
+ }
68
+
69
+ @ Test
70
+ void createNewFolderWorks () {
71
+ long parentId = 420 ;
72
+ String folderName = "Kevin" ;
73
+ CreateNewFolder createNewFolder = new CreateNewFolder (folderName );
74
+ long userId = 420 ;
75
+ User autheticatedUser = User .builder ().build ();
76
+
77
+ FileSystemEntity parent = FileSystemEntity .builder ().path ("/parent" ).ownerId (userId ).build ();
78
+ when (fileSystemRepositoryMock .findByFileSystemId (parentId )).thenReturn (parent );
79
+ when (fileSystemHelperServiceMock .userIsAllowedToInteractWithFileSystemEntity (parent , autheticatedUser , InteractionType .READ )).thenReturn (true );
80
+ when (fileSystemHelperServiceMock .userIsAllowedToInteractWithFileSystemEntity (parent , autheticatedUser , InteractionType .CHANGE )).thenReturn (true );
81
+ when (fileSystemHelperServiceMock .getFolderContentsOfEntityAndPermissions (parent , autheticatedUser , false , false )).thenReturn (Collections .singletonList (FileSystemEntity .builder ().name ("a name" ).build ()));
82
+ when (userBusinessServiceMock .findUserById (userId )).thenReturn (User .builder ().username (folderName ).build ());
83
+
84
+ FileSystemItem item = FileSystemItem .builder ().build ();
85
+ String path = "/" + folderName + "/parent/" + folderName .toLowerCase ();
86
+ when (fileSystemHelperServiceMock .createDTO (any (), eq (autheticatedUser ), eq (path ))).thenReturn (item );
87
+
88
+ FileSystemItem actual = fileSystemUploadService .createNewFolder (parentId , createNewFolder , autheticatedUser );
89
+ assertEquals (item , actual );
90
+ }
91
+ }
0 commit comments