11package jenkins .plugins .github .api ;
22
33import java .io .IOException ;
4- import java .util .ArrayList ;
54import java .util .Set ;
65import java .util .TreeSet ;
6+ import java .util .stream .Stream ;
77
88import jenkins .plugins .github .api .mock .MockGitHub ;
99import jenkins .plugins .github .api .mock .MockOrganization ;
1010import jenkins .plugins .github .api .mock .MockUser ;
1111import okhttp3 .OkHttpClient ;
12- import org .junit .Test ;
13- import org .junit .runner .RunWith ;
14- import org .junit .runners .Parameterized ;
12+ import org .junit .jupiter .params .ParameterizedTest ;
13+ import org .junit .jupiter .params .provider .MethodSource ;
1514import org .kohsuke .github .GHOrganization ;
1615import org .kohsuke .github .GHRepository ;
1716import org .kohsuke .github .GHUser ;
1817import org .kohsuke .github .GitHub ;
1918import org .kohsuke .github .GitHubBuilder ;
20- import org .kohsuke .github .HttpConnector ;
2119import org .kohsuke .github .extras .okhttp3 .OkHttpConnector ;
2220
23- import edu .umd .cs .findbugs .annotations .NonNull ;
21+ import static org .hamcrest .MatcherAssert .assertThat ;
22+ import static org .hamcrest .Matchers .*;
2423
25- import static org .hamcrest .Matchers .contains ;
26- import static org .hamcrest .Matchers .is ;
27- import static org .junit .Assert .assertThat ;
28-
29- @ RunWith (Parameterized .class )
30- public class SmokeTest {
24+ class SmokeTest {
3125
3226 @ FunctionalInterface
3327 public interface IOFunction {
@@ -41,50 +35,43 @@ public interface IOFunction {
4135 GitHub apply (MockGitHub t ) throws IOException ;
4236 }
4337
44- @ NonNull
45- IOFunction connectFunction ;
46-
47- public SmokeTest (IOFunction connectFunction ) {
48- this .connectFunction = connectFunction ;
49- }
50-
51- @ Parameterized .Parameters (name = "connectFunction={index}" )
52- public static IOFunction [] connectFunctions () {
53- HttpConnector okHttp3Connector = new OkHttpConnector (new OkHttpClient ());
54- ArrayList <IOFunction > list = new ArrayList <>();
55- list .add ((mock ) -> GitHub .connectToEnterpriseAnonymously (mock .open ()));
56- list .add ((mock ) -> new GitHubBuilder ().withConnector (okHttp3Connector ).withEndpoint (mock .open ()).build ());
57-
58- return list .toArray (new IOFunction [] {});
38+ static Stream <IOFunction > connectFunctions () {
39+ return Stream .of (
40+ mock -> GitHub .connectToEnterpriseAnonymously (mock .open ()),
41+ mock -> new GitHubBuilder ().withConnector (new OkHttpConnector (new OkHttpClient ())).withEndpoint (mock .open ()).build ()
42+ );
5943 }
6044
61- public GitHub openAndConnect (MockGitHub mock ) throws IOException {
45+ private GitHub openAndConnect (IOFunction connectFunction , MockGitHub mock ) throws IOException {
6246 return connectFunction .apply (mock );
6347 }
6448
65- @ Test
66- public void given__veryBasicMockGitHub__when__connectingAnonymously__then__apiUrlValid () throws Exception {
49+ @ ParameterizedTest (name = "connectFunction={index}" )
50+ @ MethodSource ("connectFunctions" )
51+ void given__veryBasicMockGitHub__when__connectingAnonymously__then__apiUrlValid (IOFunction connectFunction ) throws Exception {
6752 try (MockGitHub mock = new MockGitHub ()) {
68- openAndConnect (mock ).checkApiUrlValidity ();
53+ openAndConnect (connectFunction , mock ).checkApiUrlValidity ();
6954 }
7055 }
7156
72- @ Test
73- public void given__veryBasicMockGitHub__when__listingRepos__then__reposListed () throws Exception {
57+ @ ParameterizedTest (name = "connectFunction={index}" )
58+ @ MethodSource ("connectFunctions" )
59+ void given__veryBasicMockGitHub__when__listingRepos__then__reposListed (IOFunction connectFunction ) throws Exception {
7460 try (MockGitHub mock = new MockGitHub ()) {
7561 mock .withOrg ("org1" ).withPublicRepo ("repo1" ).withPrivateRepo ("repo2" );
7662 mock .withOrg ("org2" ).withPublicRepo ("repo3" );
7763 mock .withUser ("user1" ).withPublicRepo ("repo4" ).withPrivateRepo ("repo5" );
7864 Set <String > names = new TreeSet <>();
79- for (GHRepository r : openAndConnect (mock ).listAllPublicRepositories ()) {
65+ for (GHRepository r : openAndConnect (connectFunction , mock ).listAllPublicRepositories ()) {
8066 names .add (r .getFullName ());
8167 }
8268 assertThat (names , contains ("org1/repo1" , "org2/repo3" , "user1/repo4" ));
8369 }
8470 }
8571
86- @ Test
87- public void given__veryBasicMockGitHub__when__listingManyRepos__then__reposListed () throws Exception {
72+ @ ParameterizedTest (name = "connectFunction={index}" )
73+ @ MethodSource ("connectFunctions" )
74+ void given__veryBasicMockGitHub__when__listingManyRepos__then__reposListed (IOFunction connectFunction ) throws Exception {
8875 try (MockGitHub mock = new MockGitHub ()) {
8976 MockOrganization org1 = mock .withOrg ("org1" );
9077 Set <String > expected = new TreeSet <>();
@@ -94,15 +81,16 @@ public void given__veryBasicMockGitHub__when__listingManyRepos__then__reposListe
9481
9582 }
9683 Set <String > actual = new TreeSet <>();
97- for (GHRepository r : openAndConnect (mock ).listAllPublicRepositories ()) {
84+ for (GHRepository r : openAndConnect (connectFunction , mock ).listAllPublicRepositories ()) {
9885 actual .add (r .getFullName ());
9986 }
100- assertThat (actual , is (actual ));
87+ assertThat (actual , is (expected ));
10188 }
10289 }
10390
104- @ Test
105- public void given__veryBasicMockGitHub__when__gettingUser__then__userReturned () throws Exception {
91+ @ ParameterizedTest (name = "connectFunction={index}" )
92+ @ MethodSource ("connectFunctions" )
93+ void given__veryBasicMockGitHub__when__gettingUser__then__userReturned (IOFunction connectFunction ) throws Exception {
10694 try (MockGitHub mock = new MockGitHub ()) {
10795 MockUser expected = mock .withUser ("user1" )
10896 .withAvatarUrl ("http://avatar.test/user1" )
@@ -114,18 +102,20 @@ public void given__veryBasicMockGitHub__when__gettingUser__then__userReturned()
114102 .withPrivateRepo ("repo1" )
115103 .withPublicRepo ("repo2" )
116104 .withPublicRepo ("repo3" );
117- GHUser actual = openAndConnect (mock ).getUser ("user1" );
105+ GHUser actual = openAndConnect (connectFunction , mock ).getUser ("user1" );
118106 assertThat (actual .getLogin (), is (expected .getLogin ()));
119107 assertThat (actual .getName (), is (expected .getName ()));
120108 assertThat (actual .getAvatarUrl (), is (expected .getAvatarUrl ()));
121109 assertThat (actual .getBlog (), is (expected .getBlog ()));
122110 assertThat (actual .getCompany (), is (expected .getCompany ()));
123- assertThat (actual .getId (), is (( long ) expected .getId ()));
111+ assertThat (actual .getId (), is (expected .getId ()));
124112 assertThat (actual .getPublicRepoCount (), is (expected .getPublicRepos ()));
125113 }
126114 }
127- @ Test
128- public void given__veryBasicMockGitHub__when__gettingOrg__then__orgReturned () throws Exception {
115+
116+ @ ParameterizedTest (name = "connectFunction={index}" )
117+ @ MethodSource ("connectFunctions" )
118+ void given__veryBasicMockGitHub__when__gettingOrg__then__orgReturned (IOFunction connectFunction ) throws Exception {
129119 try (MockGitHub mock = new MockGitHub ()) {
130120 MockOrganization expected = mock .withOrg ("org1" )
131121 .withAvatarUrl ("http://avatar.test/org1" )
@@ -136,12 +126,12 @@ public void given__veryBasicMockGitHub__when__gettingOrg__then__orgReturned() th
136126 .withPrivateRepo ("repo1" )
137127 .withPublicRepo ("repo2" )
138128 .withPublicRepo ("repo3" );
139- GHOrganization actual = openAndConnect (mock ).getOrganization ("org1" );
129+ GHOrganization actual = openAndConnect (connectFunction , mock ).getOrganization ("org1" );
140130 assertThat (actual .getLogin (), is (expected .getLogin ()));
141131 assertThat (actual .getName (), is (expected .getName ()));
142132 assertThat (actual .getAvatarUrl (), is (expected .getAvatarUrl ()));
143133 assertThat (actual .getBlog (), is (expected .getBlog ()));
144- assertThat (actual .getId (), is (( long ) expected .getId ()));
134+ assertThat (actual .getId (), is (expected .getId ()));
145135 assertThat (actual .getPublicRepoCount (), is (expected .getPublicRepos ()));
146136 }
147137 }
0 commit comments