19
19
20
20
package org .commonwl .view .git ;
21
21
22
+ import java .io .File ;
23
+ import java .io .IOException ;
24
+
25
+ import org .eclipse .jgit .api .CheckoutCommand ;
26
+ import org .eclipse .jgit .api .Git ;
27
+ import org .eclipse .jgit .api .errors .GitAPIException ;
28
+ import org .eclipse .jgit .api .errors .RefNotFoundException ;
29
+ import org .junit .Before ;
22
30
import org .junit .Test ;
31
+ import org .mockito .Mockito ;
23
32
24
33
import static org .junit .Assert .assertEquals ;
34
+ import static org .junit .Assert .assertNotNull ;
35
+ import static org .junit .Assert .assertTrue ;
36
+ import static org .mockito .Mockito .doReturn ;
37
+ import static org .mockito .Mockito .mock ;
38
+ import static org .mockito .Mockito .spy ;
39
+ import static org .mockito .Mockito .when ;
25
40
26
41
public class GitServiceTest {
27
42
43
+ private final RefNotFoundException branchNotFoundException = new RefNotFoundException ("Branch not found" );
44
+ private final RefNotFoundException tagNotFoundException = new RefNotFoundException ("Tag not found" );
45
+
46
+ private GitService spyGitService ;
47
+ private Git mockGit ;
48
+ private CheckoutCommand mockGoodCheckoutCommand ;
49
+ private CheckoutCommand mockBranchNotFoundCommand ;
50
+ private CheckoutCommand mockTagNotFoundCommand ;
51
+ private CheckoutCommand mockCheckoutCommand ;
52
+
53
+ @ Before
54
+ public void setup () throws GitAPIException {
55
+ GitService gitService = new GitService (null , false );
56
+ this .spyGitService = spy (gitService );
57
+ this .mockGit = mock (Git .class );
58
+ this .mockGoodCheckoutCommand = mock (CheckoutCommand .class );
59
+ this .mockBranchNotFoundCommand = mock (CheckoutCommand .class );
60
+ this .mockTagNotFoundCommand = mock (CheckoutCommand .class );
61
+ this .mockCheckoutCommand = mock (CheckoutCommand .class );
62
+ when (this .mockGit .checkout ()).thenReturn (this .mockCheckoutCommand );
63
+ when (this .mockBranchNotFoundCommand .call ()).thenThrow (branchNotFoundException );
64
+ when (this .mockTagNotFoundCommand .call ()).thenThrow (tagNotFoundException );
65
+ }
66
+
28
67
@ Test
29
68
public void transferPathToBranch () throws Exception {
30
69
GitService gitService = new GitService (null , false );
@@ -39,4 +78,35 @@ public void transferPathToBranch() throws Exception {
39
78
assertEquals ("branchpart1/branchpart2/branchpart3" , step2 .getBranch ());
40
79
assertEquals ("workflowInRoot.cwl" , step2 .getPath ());
41
80
}
81
+
82
+ @ Test
83
+ public void checksOutTag () throws Exception {
84
+ when (mockCheckoutCommand .setName ("refs/remotes/origin/mytag" )).thenReturn (this .mockBranchNotFoundCommand );
85
+ when (mockCheckoutCommand .setName ("mytag" )).thenReturn (this .mockGoodCheckoutCommand );
86
+ doReturn (this .mockGit ).when (this .spyGitService ).cloneRepo (Mockito .any (String .class ), Mockito .any (File .class ));
87
+ assertNotNull (this .spyGitService .getRepository (new GitDetails (null , "mytag" , "foo" ), false ));
88
+ }
89
+
90
+ @ Test
91
+ public void checksOutBranch () throws GitAPIException , IOException {
92
+ when (mockCheckoutCommand .setName ("refs/remotes/origin/mybranch" )).thenReturn (this .mockGoodCheckoutCommand );
93
+ when (mockCheckoutCommand .setName ("mytag" )).thenReturn (this .mockTagNotFoundCommand );
94
+ doReturn (this .mockGit ).when (this .spyGitService ).cloneRepo (Mockito .any (String .class ), Mockito .any (File .class ));
95
+ assertNotNull (this .spyGitService .getRepository (new GitDetails (null , "mybranch" , "foo" ), false ));
96
+ }
97
+
98
+ @ Test ()
99
+ public void throwsFirstExceptionIfTagAndBranchFail () throws GitAPIException {
100
+ when (mockCheckoutCommand .setName ("refs/remotes/origin/mytag" )).thenReturn (this .mockBranchNotFoundCommand );
101
+ when (mockCheckoutCommand .setName ("mytag" )).thenReturn (this .mockTagNotFoundCommand );
102
+ doReturn (this .mockGit ).when (this .spyGitService ).cloneRepo (Mockito .any (String .class ), Mockito .any (File .class ));
103
+ boolean thrown = false ;
104
+ try {
105
+ this .spyGitService .getRepository (new GitDetails (null , "mytag" , "foo" ), false );
106
+ } catch (Exception e ) {
107
+ // Make sure it's not this.tagNotFoundException
108
+ thrown = (e == this .branchNotFoundException );
109
+ }
110
+ assertTrue (thrown );
111
+ }
42
112
}
0 commit comments