1
1
package pl .damianszczepanik .jenkins .buildhistorymanager .model .actions ;
2
2
3
+ import static org .assertj .core .api .Assertions .assertThat ;
4
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
3
5
import static org .mockito .Mockito .mock ;
6
+ import static org .mockito .Mockito .mockStatic ;
7
+ import static org .mockito .Mockito .times ;
4
8
import static org .mockito .Mockito .when ;
5
9
6
10
import java .io .File ;
7
11
import java .io .IOException ;
12
+ import java .nio .file .Files ;
13
+ import java .nio .file .Path ;
8
14
15
+ import edu .umd .cs .findbugs .annotations .NonNull ;
9
16
import org .junit .jupiter .api .Test ;
17
+ import org .mockito .MockedStatic ;
10
18
import pl .damianszczepanik .jenkins .buildhistorymanager .utils .RunStub ;
11
19
12
20
/**
@@ -16,40 +24,56 @@ class DeleteLogFileActionTest {
16
24
17
25
@ Test
18
26
void perform_OnExistingLogFile_DeletesLogFile () throws IOException , InterruptedException {
19
-
20
27
// given
28
+ File logFile = mock (File .class );
29
+ Path logFilePath = mock (Path .class );
30
+ when (logFile .exists ()).thenReturn (true );
31
+ when (logFile .toPath ()).thenReturn (logFilePath );
32
+
21
33
Action action = new DeleteLogFileAction ();
22
- RunStub run = new RunStub (RunStub .LogFileAvailability .PRESENT );
23
-
24
- // when
25
- action .perform (run );
26
-
27
- // then
28
- run .assertLogFileIsNotAvailable ();
34
+ RunStub run = new RunStub (logFile );
35
+
36
+
37
+ try (MockedStatic <Files > mockFiles = mockStatic (Files .class )) {
38
+ // when
39
+ action .perform (run );
40
+ // then
41
+ mockFiles .verify (() -> Files .delete (logFilePath ), times (1 ));
42
+ }
29
43
}
30
44
31
45
@ Test
32
46
void perform_OnMissingLogFile_SkipDeletion () throws IOException , InterruptedException {
33
47
34
48
// given
49
+ File logFile = mock (File .class );
50
+ Path logFilePath = mock (Path .class );
51
+ when (logFile .exists ()).thenReturn (false );
52
+ when (logFile .toPath ()).thenReturn (logFilePath );
53
+
35
54
Action action = new DeleteLogFileAction ();
36
- RunStub run = new RunStub (RunStub . LogFileAvailability . ABSENT );
55
+ RunStub run = new RunStub (logFile );
37
56
38
- // when
39
- action .perform (run );
57
+ try (MockedStatic <Files > mockFiles = mockStatic (Files .class )) {
58
+ // when
59
+ action .perform (run );
60
+ // then
61
+ mockFiles .verify (() -> Files .delete (logFilePath ), times (0 ));
62
+ }
40
63
41
- // then
42
- run .assertLogFileIsNotAvailable ();
43
64
}
44
65
45
66
@ Test
46
67
void perform_OnGetLogFileThrowsUnsupportedOperationException_CatchesExceptionAndDoesNothing () throws IOException , InterruptedException {
47
-
48
68
// given
69
+ final boolean [] exceptionWasThrown = {false };
49
70
Action action = new DeleteLogFileAction ();
50
- RunStub run = new RunStub (RunStub .LogFileAvailability .PRESENT ) {
71
+ RunStub run = new RunStub () {
72
+ @ NonNull
51
73
@ Override
74
+ @ Deprecated
52
75
public File getLogFile () {
76
+ exceptionWasThrown [0 ] = true ;
53
77
throw new UnsupportedOperationException ("Operation not supported" );
54
78
}
55
79
};
@@ -58,29 +82,26 @@ public File getLogFile() {
58
82
action .perform (run );
59
83
60
84
// then
61
- run . assertLogFileIsAvailable ();
85
+ assertThat ( exceptionWasThrown [ 0 ]). isTrue ();
62
86
}
63
87
64
88
@ Test
65
- void perform_DeleteLogFileDoesNotWork_IgnoresFailure () throws IOException , InterruptedException {
66
-
89
+ void perform_DeleteLogFileDoesNotWork_Throws () throws IOException {
67
90
// given
91
+ Path logFilePath = mock (Path .class );
92
+ File logFile = mock (File .class );
93
+ when (logFile .exists ()).thenReturn (true );
94
+ when (logFile .toPath ()).thenReturn (logFilePath );
95
+
68
96
Action action = new DeleteLogFileAction ();
69
- RunStub run = new RunStub (RunStub .LogFileAvailability .PRESENT ) {
70
- @ Override
71
- public File getLogFile () {
72
- File logFile = mock (File .class );
73
- when (logFile .exists ()).thenReturn (true );
74
- when (logFile .delete ()).thenReturn (false );
75
- return logFile ;
76
- }
77
- };
78
-
79
- // when
80
- action .perform (run );
81
-
82
- // then
83
- run .assertLogFileIsAvailable ();
97
+ RunStub run = new RunStub (logFile );
98
+
99
+ try (MockedStatic <Files > mockFiles = mockStatic (Files .class )) {
100
+ mockFiles .when (() -> Files .delete (logFilePath )).thenThrow (new IOException ("File deletion failed" ));
101
+
102
+ // when / then
103
+ assertThrows (IOException .class , () -> action .perform (run ));
104
+ }
84
105
}
85
106
86
107
}
0 commit comments