11package com .google .jenkins .plugins .k8sengine ;
22
3- import static org .junit .Assert .assertNotNull ;
3+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
4+ import static org .junit .jupiter .api .Assertions .assertThrows ;
45
56import com .cloudbees .plugins .credentials .CredentialsStore ;
67import com .cloudbees .plugins .credentials .SecretBytes ;
78import com .cloudbees .plugins .credentials .SystemCredentialsProvider ;
89import com .cloudbees .plugins .credentials .domains .Domain ;
9- import com .cloudbees .plugins .credentials .domains .DomainRequirement ;
1010import com .google .api .client .auth .oauth2 .Credential ;
1111import com .google .common .collect .ImmutableList ;
1212import com .google .jenkins .plugins .credentials .oauth .GoogleRobotCredentials ;
1313import com .google .jenkins .plugins .credentials .oauth .GoogleRobotPrivateKeyCredentials ;
1414import com .google .jenkins .plugins .credentials .oauth .JsonServiceAccountConfig ;
1515import hudson .AbortException ;
16- import java .io .IOException ;
1716import java .nio .charset .StandardCharsets ;
18- import org .junit .ClassRule ;
19- import org .junit .Test ;
17+ import org .junit .jupiter . api . BeforeAll ;
18+ import org .junit .jupiter . api . Test ;
2019import org .jvnet .hudson .test .JenkinsRule ;
20+ import org .jvnet .hudson .test .junit .jupiter .WithJenkins ;
21+
22+ @ WithJenkins
23+ class CredentialsUtilTest {
2124
22- public class CredentialsUtilTest {
2325 private static final String TEST_CREDENTIALS_ID = "test-credentials-id" ;
2426 private static final String TEST_INVALID_CREDENTIALS_ID = "test-invalid-credentials-id" ;
25- private static final String TEST_ACCESS_TOKEN = "test-access-token" ;
2627
27- @ ClassRule
28- public static JenkinsRule r = new JenkinsRule ();
28+ private static JenkinsRule r ;
29+
30+ @ BeforeAll
31+ static void init (JenkinsRule rule ) {
32+ r = rule ;
33+ }
2934
30- @ Test (expected = AbortException .class )
31- public void testGetRobotCredentialsInvalidCredentialsIdAbortException () throws AbortException {
32- CredentialsUtil .getRobotCredentials (
33- r .jenkins , ImmutableList .<DomainRequirement >of (), TEST_INVALID_CREDENTIALS_ID );
35+ @ Test
36+ void testGetRobotCredentialsInvalidCredentialsIdAbortException () {
37+ assertThrows (
38+ AbortException .class ,
39+ () -> CredentialsUtil .getRobotCredentials (r .jenkins , ImmutableList .of (), TEST_INVALID_CREDENTIALS_ID ));
3440 }
3541
36- @ Test ( expected = GoogleRobotPrivateKeyCredentials . PrivateKeyNotSetException . class )
37- public void testGetGoogleCredentialAbortException () throws Exception {
42+ @ Test
43+ void testGetGoogleCredentialAbortException () throws Exception {
3844 SecretBytes bytes =
3945 SecretBytes .
fromBytes (
"{\" client_email\" : \" [email protected] \" }" .
getBytes (
StandardCharsets .
UTF_8 ));
4046 JsonServiceAccountConfig serviceAccountConfig = new JsonServiceAccountConfig ();
@@ -44,42 +50,52 @@ public void testGetGoogleCredentialAbortException() throws Exception {
4450 new GoogleRobotPrivateKeyCredentials (TEST_INVALID_CREDENTIALS_ID , serviceAccountConfig , null );
4551 CredentialsStore store = new SystemCredentialsProvider .ProviderImpl ().getStore (r .jenkins );
4652 store .addCredentials (Domain .global (), robotCreds );
47- CredentialsUtil .getGoogleCredential (robotCreds );
53+ assertThrows (
54+ GoogleRobotPrivateKeyCredentials .PrivateKeyNotSetException .class ,
55+ () -> CredentialsUtil .getGoogleCredential (robotCreds ));
4856 }
4957
50- @ Test (expected = NullPointerException .class )
51- public void testGetRobotCredentialsWithEmptyItemGroup () throws AbortException {
52- CredentialsUtil .getRobotCredentials (null , ImmutableList .<DomainRequirement >of (), TEST_CREDENTIALS_ID );
58+ @ Test
59+ void testGetRobotCredentialsWithEmptyItemGroup () {
60+ assertThrows (
61+ NullPointerException .class ,
62+ () -> CredentialsUtil .getRobotCredentials (null , ImmutableList .of (), TEST_CREDENTIALS_ID ));
5363 }
5464
55- @ Test (expected = NullPointerException .class )
56- public void testGetRobotCredentialsWithEmptyDomainRequirements () throws AbortException {
57- CredentialsUtil .getRobotCredentials (r .jenkins , null , TEST_CREDENTIALS_ID );
65+ @ Test
66+ void testGetRobotCredentialsWithEmptyDomainRequirements () {
67+ assertThrows (
68+ NullPointerException .class ,
69+ () -> CredentialsUtil .getRobotCredentials (r .jenkins , null , TEST_CREDENTIALS_ID ));
5870 }
5971
60- @ Test (expected = IllegalArgumentException .class )
61- public void testGetRobotCredentialsWithNullCredentialsId () throws AbortException {
62- CredentialsUtil .getRobotCredentials (r .jenkins , ImmutableList .<DomainRequirement >of (), null );
72+ @ Test
73+ void testGetRobotCredentialsWithNullCredentialsId () {
74+ assertThrows (
75+ IllegalArgumentException .class ,
76+ () -> CredentialsUtil .getRobotCredentials (r .jenkins , ImmutableList .of (), null ));
6377 }
6478
65- @ Test (expected = IllegalArgumentException .class )
66- public void testGetRobotCredentialsWithEmptyCredentialsId () throws AbortException {
67- CredentialsUtil .getRobotCredentials (r .jenkins , ImmutableList .<DomainRequirement >of (), "" );
79+ @ Test
80+ void testGetRobotCredentialsWithEmptyCredentialsId () {
81+ assertThrows (
82+ IllegalArgumentException .class ,
83+ () -> CredentialsUtil .getRobotCredentials (r .jenkins , ImmutableList .of (), "" ));
6884 }
6985
70- @ Test ( expected = IllegalArgumentException . class )
71- public void testGetAccessTokenWithEmptyCredentialsId () throws IOException {
72- CredentialsUtil .getAccessToken ("" );
86+ @ Test
87+ void testGetAccessTokenWithEmptyCredentialsId () {
88+ assertThrows ( IllegalArgumentException . class , () -> CredentialsUtil .getAccessToken ("" ) );
7389 }
7490
75- @ Test ( expected = NullPointerException . class )
76- public void testGetAccessTokenWithNullItemGroup () throws IOException {
77- CredentialsUtil .getAccessToken (null , TEST_CREDENTIALS_ID );
91+ @ Test
92+ void testGetAccessTokenWithNullItemGroup () {
93+ assertThrows ( NullPointerException . class , () -> CredentialsUtil .getAccessToken (null , TEST_CREDENTIALS_ID ) );
7894 }
7995
80- @ Test ( expected = NullPointerException . class )
81- public void testGetAccessTokenWithNullGoogleCredential () throws IOException {
96+ @ Test
97+ void testGetAccessTokenWithNullGoogleCredential () {
8298 Credential googleCredential = null ;
83- CredentialsUtil .getAccessToken (googleCredential );
99+ assertThrows ( NullPointerException . class , () -> CredentialsUtil .getAccessToken (googleCredential ) );
84100 }
85101}
0 commit comments