2323 */
2424package com .cloudbees .jenkins .plugins .bitbucket ;
2525
26+ import com .google .common .collect .ImmutableList ;
2627import edu .umd .cs .findbugs .annotations .NonNull ;
2728import hudson .model .TaskListener ;
2829import java .io .IOException ;
2930import java .util .ArrayList ;
31+ import java .util .Collections ;
3032import java .util .List ;
3133import jenkins .scm .api .SCMSource ;
3234import jenkins .scm .api .SCMSourceObserver ;
3335import jenkins .scm .api .SCMSourceObserver .ProjectObserver ;
3436import jenkins .scm .api .SCMSourceOwner ;
37+ import jenkins .scm .impl .NullSCMSource ;
3538import org .junit .ClassRule ;
3639import org .junit .Test ;
3740import org .jvnet .hudson .test .JenkinsRule ;
3841import org .mockito .Mockito ;
3942
4043import static org .junit .Assert .assertEquals ;
4144import static org .junit .Assert .assertTrue ;
45+ import static org .mockito .Mockito .when ;
4246
4347public class SCMNavigatorTest {
4448
@@ -52,8 +56,10 @@ public void teamRepositoriesDiscovering() throws IOException, InterruptedExcepti
5256 BitbucketSCMNavigator navigator = new BitbucketSCMNavigator ("myteam" , null , null );
5357 navigator .setPattern ("repo(.*)" );
5458 navigator .setBitbucketServerUrl ("http://bitbucket.test" );
59+ final SCMSourceOwner mock = Mockito .mock (SCMSourceOwner .class );
60+ when (mock .getSCMSources ()).thenReturn (Collections .singletonList (new BitbucketSCMSource ("myteam" , "repo1" )));
5561 SCMSourceObserverImpl observer = new SCMSourceObserverImpl (BitbucketClientMockUtils .getTaskListenerMock (),
56- Mockito . mock ( SCMSourceOwner . class ) );
62+ mock );
5763 navigator .visitSources (observer );
5864
5965 assertEquals ("myteam" , navigator .getRepoOwner ());
@@ -62,8 +68,44 @@ public void teamRepositoriesDiscovering() throws IOException, InterruptedExcepti
6268 List <String > observed = observer .getObserved ();
6369 // Only 2 repositories match the pattern
6470 assertEquals ("There must be 2 repositories in the team" , 2 , observed .size ());
65- assertEquals ("repo1" , observed .get (0 ));
66- assertEquals ("repo2" , observed .get (1 ));
71+ assertEquals ("repo2 should be first" , "repo2" , observed .get (0 ));
72+ assertEquals ("repo1 should be second" , "repo1" , observed .get (1 ));
73+
74+ List <ProjectObserver > observers = observer .getProjectObservers ();
75+ for (ProjectObserver obs : observers ) {
76+ List <SCMSource > sources = ((SCMSourceObserverImpl .ProjectObserverImpl ) obs ).getSources ();
77+ // It should contain only one source
78+ assertEquals ("Only one source must be created per observed repository" , 1 , sources .size ());
79+ SCMSource scmSource = sources .get (0 );
80+ assertTrue ("BitbucketSCMSource instances must be added" , scmSource instanceof BitbucketSCMSource );
81+ // Check correct repoOwner (team name in this case) was set
82+ assertEquals (((BitbucketSCMSource ) scmSource ).getRepoOwner (), "myteam" );
83+ }
84+ }
85+
86+ @ Test
87+ public void teamRepositoriesDiscoveringNullSource () throws IOException , InterruptedException {
88+ BitbucketMockApiFactory .add ("http://bitbucket.test" ,
89+ BitbucketClientMockUtils .getAPIClientMock (true , false ));
90+ BitbucketSCMNavigator navigator = new BitbucketSCMNavigator ("myteam" , null , null );
91+ navigator .setPattern ("repo(.*)" );
92+ navigator .setBitbucketServerUrl ("http://bitbucket.test" );
93+ final SCMSourceOwner mock = Mockito .mock (SCMSourceOwner .class );
94+ when (mock .getSCMSources ())
95+ .thenReturn (ImmutableList .of (new BitbucketSCMSource ("myteam" , "repo1" ),
96+ new NullSCMSource ()));
97+ SCMSourceObserverImpl observer = new SCMSourceObserverImpl (BitbucketClientMockUtils .getTaskListenerMock (),
98+ mock );
99+ navigator .visitSources (observer );
100+
101+ assertEquals ("myteam" , navigator .getRepoOwner ());
102+ assertEquals ("repo(.*)" , navigator .getPattern ());
103+
104+ List <String > observed = observer .getObserved ();
105+ // Only 2 repositories match the pattern
106+ assertEquals ("There must be 2 repositories in the team" , 2 , observed .size ());
107+ assertEquals ("repo2 should be first" , "repo2" , observed .get (0 ));
108+ assertEquals ("repo1 should be second" , "repo1" , observed .get (1 ));
67109
68110 List <ProjectObserver > observers = observer .getProjectObservers ();
69111 for (ProjectObserver obs : observers ) {
0 commit comments