3232import se .bjurr .prnfb .settings .ValidationException ;
3333
3434import com .atlassian .bitbucket .auth .AuthenticationContext ;
35+ import com .atlassian .bitbucket .project .Project ;
3536import com .atlassian .bitbucket .pull .PullRequest ;
37+ import com .atlassian .bitbucket .pull .PullRequestRef ;
3638import com .atlassian .bitbucket .pull .PullRequestService ;
39+ import com .atlassian .bitbucket .repository .Repository ;
3740import com .atlassian .bitbucket .repository .RepositoryService ;
3841import com .atlassian .bitbucket .server .ApplicationPropertiesService ;
42+ import com .google .common .base .Optional ;
3943import com .google .common .collect .Lists ;
4044
4145public class ButtonsServiceTest {
@@ -44,8 +48,10 @@ public class ButtonsServiceTest {
4448 private AuthenticationContext authenticationContext ;
4549 private PrnfbButton button1 ;
4650 private PrnfbButton button2 ;
51+ private PrnfbButton button3 ;
4752 private ButtonDTO buttonDto1 ;
4853 private ButtonDTO buttonDto2 ;
54+ private ButtonDTO buttonDto3 ;
4955 @ Mock
5056 private ClientKeyStore clientKeyStore ;
5157 private PrnfbNotification notification1 ;
@@ -74,6 +80,14 @@ public class ButtonsServiceTest {
7480 private ButtonsService sut ;
7581 @ Mock
7682 private UserCheckService userCheckService ;
83+ @ Mock
84+ private PullRequestRef prRef ;
85+ @ Mock
86+ private Repository repository ;
87+ @ Mock
88+ private Repository originRepo ;
89+ @ Mock
90+ private Project project ;
7791
7892 @ SuppressWarnings ("unchecked" )
7993 @ Before
@@ -87,16 +101,22 @@ public void before() throws ValidationException {
87101 .thenReturn (this .renderer );
88102
89103 this .buttonDto1 = populatedInstanceOf (ButtonDTO .class );
90- this .buttonDto1 .setProjectKey ("a" );
104+ this .buttonDto1 .setProjectKey (null );
105+ this .buttonDto1 .setRepositorySlug (null );
91106 this .button1 = toPrnfbButton (this .buttonDto1 );
92107 this .buttonDto2 = populatedInstanceOf (ButtonDTO .class );
93- this .buttonDto2 .setProjectKey ("b" );
108+ this .buttonDto2 .setProjectKey (null );
109+ this .buttonDto2 .setRepositorySlug (null );
94110 this .button2 = toPrnfbButton (this .buttonDto2 );
111+ this .buttonDto3 = populatedInstanceOf (ButtonDTO .class );
112+ this .button3 = toPrnfbButton (this .buttonDto3 );
95113
96114 when (this .settingsService .getButton (this .button1 .getUuid ()))//
97115 .thenReturn (this .button1 );
98116 when (this .settingsService .getButton (this .button2 .getUuid ()))//
99- .thenReturn (this .button2 );
117+ .thenReturn (this .button2 );
118+ when (this .settingsService .getButton (this .button3 .getUuid ()))//
119+ .thenReturn (this .button3 );
100120
101121 this .notificationDto1 = populatedInstanceOf (NotificationDTO .class );
102122 this .notificationDto1 .setUrl ("http://hej.com" );
@@ -117,14 +137,15 @@ public void before() throws ValidationException {
117137
118138 @ Test
119139 public void testThatButtonsCanBeRetrievedWhenAllAllowed () {
120-
121- List <PrnfbButton > candidates = newArrayList (this .button1 , this .button2 );
140+ List <PrnfbButton > candidates = newArrayList (this .button1 , this .button2 , this .button3 );
122141 when (this .settingsService .getButtons ())//
123142 .thenReturn (candidates );
124143 when (this .userCheckService .isAllowedUseButton (this .button1 ))//
125144 .thenReturn (true );
126145 when (this .userCheckService .isAllowedUseButton (this .button2 ))//
127- .thenReturn (true );
146+ .thenReturn (true );
147+ when (this .userCheckService .isAllowedUseButton (this .button3 ))//
148+ .thenReturn (true );
128149 when (
129150 this .prnfbPullRequestEventListener .isNotificationTriggeredByAction (this .notification1 , this .pullRequestAction ,
130151 this .renderer , this .pullRequest , this .clientKeyStore , this .shouldAcceptAnyCertificate ))//
@@ -133,12 +154,32 @@ public void testThatButtonsCanBeRetrievedWhenAllAllowed() {
133154 this .prnfbPullRequestEventListener .isNotificationTriggeredByAction (this .notification2 , this .pullRequestAction ,
134155 this .renderer , this .pullRequest , this .clientKeyStore , this .shouldAcceptAnyCertificate ))//
135156 .thenReturn (true );
157+ when (this .pullRequest .getToRef ()).thenReturn (prRef );
158+ when (this .prRef .getRepository ()).thenReturn (repository );
159+ when (this .repository .getSlug ()).thenReturn (button3 .getRepositorySlug ().get ());
160+ when (this .repository .getProject ()).thenReturn (project );
161+ when (this .project .getKey ()).thenReturn (button3 .getProjectKey ().get ());
136162
137163 List <PrnfbButton > actual = this .sut .doGetButtons (this .notifications , this .clientKeyStore , this .pullRequest ,
138164 this .shouldAcceptAnyCertificate );
165+ assertThat (actual )//
166+ .containsOnly (this .button1 , this .button2 , this .button3 );
167+
168+ // Now do the same with another repository - button3 should disappear
169+ when (this .repository .getSlug ()).thenReturn ("otherrepository" );
170+ actual = this .sut .doGetButtons (this .notifications , this .clientKeyStore , this .pullRequest ,
171+ this .shouldAcceptAnyCertificate );
172+ assertThat (actual )//
173+ .containsOnly (this .button1 , this .button2 );
139174
175+ // Now check if the button is inherited from the origin repo
176+ when (this .repository .getOrigin ()).thenReturn (originRepo );
177+ when (this .originRepo .getSlug ()).thenReturn (button3 .getRepositorySlug ().get ());
178+ when (this .originRepo .getProject ()).thenReturn (project );
179+ actual = this .sut .doGetButtons (this .notifications , this .clientKeyStore , this .pullRequest ,
180+ this .shouldAcceptAnyCertificate );
140181 assertThat (actual )//
141- .containsExactly (this .button1 , this .button2 );
182+ .containsOnly (this .button1 , this .button2 , this . button3 );
142183 }
143184
144185 @ Test
0 commit comments