2727import se .bjurr .prnfb .listener .PrnfbPullRequestEventListener ;
2828import se .bjurr .prnfb .presentation .dto .ButtonDTO ;
2929import se .bjurr .prnfb .presentation .dto .NotificationDTO ;
30+ import se .bjurr .prnfb .presentation .dto .ON_OR_OFF ;
3031import se .bjurr .prnfb .settings .PrnfbButton ;
3132import se .bjurr .prnfb .settings .PrnfbNotification ;
33+ import se .bjurr .prnfb .settings .USER_LEVEL ;
3234import se .bjurr .prnfb .settings .ValidationException ;
3335
3436import com .atlassian .bitbucket .auth .AuthenticationContext ;
3941import com .atlassian .bitbucket .repository .Repository ;
4042import com .atlassian .bitbucket .repository .RepositoryService ;
4143import com .atlassian .bitbucket .server .ApplicationPropertiesService ;
42- import com .google .common .base .Optional ;
4344import com .google .common .collect .Lists ;
4445
4546public class ButtonsServiceTest {
@@ -54,40 +55,44 @@ public class ButtonsServiceTest {
5455 private ButtonDTO buttonDto3 ;
5556 @ Mock
5657 private ClientKeyStore clientKeyStore ;
58+ private final ON_OR_OFF confirmation = ON_OR_OFF .on ;
59+ private final String name = "name" ;
5760 private PrnfbNotification notification1 ;
5861 private PrnfbNotification notification2 ;
5962 private NotificationDTO notificationDto1 ;
6063 private NotificationDTO notificationDto2 ;
6164 private List <PrnfbNotification > notifications ;
6265 @ Mock
66+ private Repository originRepo ;
67+ @ Mock
6368 private PrnfbPullRequestEventListener prnfbPullRequestEventListener ;
6469 @ Mock
6570 private PrnfbRendererFactory prnfbRendererFactory ;
6671 @ Mock
72+ private Project project ;
73+ @ Mock
6774 private ApplicationPropertiesService propertiesService ;
6875 @ Mock
76+ private PullRequestRef prRef ;
77+ @ Mock
6978 private PullRequest pullRequest ;
7079 private final PrnfbPullRequestAction pullRequestAction = BUTTON_TRIGGER ;
7180 @ Mock
7281 private PullRequestService pullRequestService ;
7382 @ Mock
7483 private PrnfbRenderer renderer ;
7584 @ Mock
85+ private Repository repository ;
86+ @ Mock
7687 private RepositoryService repositoryService ;
7788 @ Mock
7889 private SettingsService settingsService ;
7990 private final Boolean shouldAcceptAnyCertificate = true ;
8091 private ButtonsService sut ;
8192 @ Mock
8293 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 ;
94+ private final USER_LEVEL userLevel = USER_LEVEL .ADMIN ;
95+ private final UUID uuid = UUID .randomUUID ();
9196
9297 @ SuppressWarnings ("unchecked" )
9398 @ Before
@@ -114,9 +119,9 @@ public void before() throws ValidationException {
114119 when (this .settingsService .getButton (this .button1 .getUuid ()))//
115120 .thenReturn (this .button1 );
116121 when (this .settingsService .getButton (this .button2 .getUuid ()))//
117- .thenReturn (this .button2 );
122+ .thenReturn (this .button2 );
118123 when (this .settingsService .getButton (this .button3 .getUuid ()))//
119- .thenReturn (this .button3 );
124+ .thenReturn (this .button3 );
120125
121126 this .notificationDto1 = populatedInstanceOf (NotificationDTO .class );
122127 this .notificationDto1 .setUrl ("http://hej.com" );
@@ -143,9 +148,9 @@ public void testThatButtonsCanBeRetrievedWhenAllAllowed() {
143148 when (this .userCheckService .isAllowedUseButton (this .button1 ))//
144149 .thenReturn (true );
145150 when (this .userCheckService .isAllowedUseButton (this .button2 ))//
146- .thenReturn (true );
151+ .thenReturn (true );
147152 when (this .userCheckService .isAllowedUseButton (this .button3 ))//
148- .thenReturn (true );
153+ .thenReturn (true );
149154 when (
150155 this .prnfbPullRequestEventListener .isNotificationTriggeredByAction (this .notification1 , this .pullRequestAction ,
151156 this .renderer , this .pullRequest , this .clientKeyStore , this .shouldAcceptAnyCertificate ))//
@@ -154,11 +159,11 @@ public void testThatButtonsCanBeRetrievedWhenAllAllowed() {
154159 this .prnfbPullRequestEventListener .isNotificationTriggeredByAction (this .notification2 , this .pullRequestAction ,
155160 this .renderer , this .pullRequest , this .clientKeyStore , this .shouldAcceptAnyCertificate ))//
156161 .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 ());
162+ when (this .pullRequest .getToRef ()).thenReturn (this . prRef );
163+ when (this .prRef .getRepository ()).thenReturn (this . repository );
164+ when (this .repository .getSlug ()).thenReturn (this . button3 .getRepositorySlug ().get ());
165+ when (this .repository .getProject ()).thenReturn (this . project );
166+ when (this .project .getKey ()).thenReturn (this . button3 .getProjectKey ().get ());
162167
163168 List <PrnfbButton > actual = this .sut .doGetButtons (this .notifications , this .clientKeyStore , this .pullRequest ,
164169 this .shouldAcceptAnyCertificate );
@@ -173,9 +178,9 @@ public void testThatButtonsCanBeRetrievedWhenAllAllowed() {
173178 .containsOnly (this .button1 , this .button2 );
174179
175180 // 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 );
181+ when (this .repository .getOrigin ()).thenReturn (this . originRepo );
182+ when (this .originRepo .getSlug ()).thenReturn (this . button3 .getRepositorySlug ().get ());
183+ when (this .originRepo .getProject ()).thenReturn (this . project );
179184 actual = this .sut .doGetButtons (this .notifications , this .clientKeyStore , this .pullRequest ,
180185 this .shouldAcceptAnyCertificate );
181186 assertThat (actual )//
@@ -221,4 +226,56 @@ public void testThatPressedButtonDoesTriggerIfMatchingNotification() {
221226 verify (this .prnfbPullRequestEventListener , times (2 ))//
222227 .notify (any (), any (), any (), any (), any (), any ());
223228 }
229+
230+ @ Test
231+ public void testVisibilityOnPullRequest () {
232+ String buttonProjectKey = "proj" ;
233+ String buttonRepositorySlug = "repo" ;
234+ String repoProjectKey = "proj" ;
235+ String repoRepositorySlug = "repo" ;
236+ testVisibilityOnRepository (buttonProjectKey , buttonRepositorySlug , repoProjectKey , repoRepositorySlug , true );
237+
238+ buttonProjectKey = "proj" ;
239+ buttonRepositorySlug = "repo" ;
240+ repoProjectKey = "proj2" ;
241+ repoRepositorySlug = "repo" ;
242+ testVisibilityOnRepository (buttonProjectKey , buttonRepositorySlug , repoProjectKey , repoRepositorySlug , false );
243+
244+ buttonProjectKey = "proj" ;
245+ buttonRepositorySlug = null ;
246+ repoProjectKey = "proj" ;
247+ repoRepositorySlug = "repo" ;
248+ testVisibilityOnRepository (buttonProjectKey , buttonRepositorySlug , repoProjectKey , repoRepositorySlug , true );
249+
250+ buttonProjectKey = "proj" ;
251+ buttonRepositorySlug = null ;
252+ repoProjectKey = "proj2" ;
253+ repoRepositorySlug = "repo" ;
254+ testVisibilityOnRepository (buttonProjectKey , buttonRepositorySlug , repoProjectKey , repoRepositorySlug , false );
255+
256+ buttonProjectKey = "proj" ;
257+ buttonRepositorySlug = "repo" ;
258+ repoProjectKey = "proj" ;
259+ repoRepositorySlug = "repo2" ;
260+ testVisibilityOnRepository (buttonProjectKey , buttonRepositorySlug , repoProjectKey , repoRepositorySlug , false );
261+
262+ buttonProjectKey = "proj" ;
263+ buttonRepositorySlug = "repo" ;
264+ repoProjectKey = "proj2" ;
265+ repoRepositorySlug = "repo2" ;
266+ testVisibilityOnRepository (buttonProjectKey , buttonRepositorySlug , repoProjectKey , repoRepositorySlug , false );
267+ }
268+
269+ private void testVisibilityOnRepository (String buttonProjectKey , String buttonRepositorySlug , String repoProjectKey ,
270+ String repoRepoSlug , boolean expected ) {
271+ PrnfbButton button = new PrnfbButton (this .uuid , this .name , this .userLevel , this .confirmation , buttonProjectKey ,
272+ buttonRepositorySlug );
273+ when (this .repository .getProject ()).thenReturn (this .project );
274+ when (this .repository .getProject ().getKey ())//
275+ .thenReturn (repoProjectKey );
276+ when (this .repository .getSlug ())//
277+ .thenReturn (repoRepoSlug );
278+ assertThat (this .sut .isVisibleOnRepository (button , this .repository ))//
279+ .isEqualTo (expected );
280+ }
224281}
0 commit comments