23
23
*/
24
24
package co .johnrowley .jenkins .bitbucketcredentialsk8s ;
25
25
26
+ import static org .hamcrest .CoreMatchers .is ;
27
+ import static org .hamcrest .CoreMatchers .notNullValue ;
28
+ import static org .hamcrest .MatcherAssert .assertThat ;
29
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
30
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
31
+ import static org .mockito .ArgumentMatchers .anyInt ;
32
+ import static org .mockito .Mockito .mock ;
33
+ import static org .mockito .Mockito .mockStatic ;
34
+ import static org .mockito .Mockito .when ;
35
+
26
36
import com .atlassian .bitbucket .jenkins .internal .config .BitbucketTokenCredentialsImpl ;
27
37
import com .cloudbees .jenkins .plugins .kubernetes_credentials_provider .CredentialsConvertionException ;
28
- import hudson .Extension ;
29
38
import hudson .util .HistoricalSecrets ;
30
39
import io .fabric8 .kubernetes .api .model .Secret ;
31
40
import io .fabric8 .kubernetes .client .utils .Serialization ;
32
- import jenkins .security .ConfidentialStore ;
33
- import org .junit .BeforeClass ;
34
- import org .junit .Before ;
35
- import org .junit .Test ;
36
- import org .junit .runner .RunWith ;
37
- import org .mockito .ArgumentMatchers ;
38
- import org .mockito .Mockito ;
39
41
import java .io .InputStream ;
40
- import static org .hamcrest .CoreMatchers .is ;
41
- import static org .hamcrest .CoreMatchers .notNullValue ;
42
- import static org .junit .Assert .assertThat ;
43
- import static org .junit .Assert .fail ;
42
+ import jenkins .security .ConfidentialStore ;
43
+ import org .junit .jupiter .api .AfterAll ;
44
+ import org .junit .jupiter .api .BeforeAll ;
45
+ import org .junit .jupiter .api .BeforeEach ;
46
+ import org .junit .jupiter .api .Test ;
47
+ import org .mockito .MockedStatic ;
44
48
45
49
/**
46
50
* Tests for {@link BitbucketCredentialConverter}.
47
51
*/
48
- @ Extension
49
- public class BitbucketCredentialConverterTest {
52
+ class BitbucketCredentialConverterTest {
53
+
54
+ private static MockedStatic <ConfidentialStore > csMockStatic ;
55
+ private static MockedStatic <HistoricalSecrets > hsMockStatic ;
50
56
51
- @ BeforeClass
52
- public static void mockConfidentialStore () {
53
- Mockito . mockStatic (ConfidentialStore .class );
54
- Mockito . mockStatic (HistoricalSecrets .class );
57
+ @ BeforeAll
58
+ static void mockConfidentialStore () {
59
+ csMockStatic = mockStatic (ConfidentialStore .class );
60
+ hsMockStatic = mockStatic (HistoricalSecrets .class );
55
61
}
56
62
57
- @ Before
58
- public void before () {
59
- ConfidentialStore csMock = Mockito .mock (ConfidentialStore .class );
60
- Mockito .when (ConfidentialStore .get ()).thenReturn (csMock );
61
- Mockito .when (csMock .randomBytes (ArgumentMatchers .anyInt ())).thenAnswer ( it -> new byte [ (Integer )(it .getArguments ()[0 ])] );
63
+ @ BeforeEach
64
+ void before () {
65
+ ConfidentialStore csMock = mock (ConfidentialStore .class );
66
+ when (ConfidentialStore .get ()).thenReturn (csMock );
67
+ when (csMock .randomBytes (anyInt ())).thenAnswer (it -> new byte [(Integer ) (it .getArguments ()[0 ])]);
68
+ }
69
+
70
+ @ AfterAll
71
+ static void resetMockStatic () {
72
+ csMockStatic .close ();
73
+ hsMockStatic .close ();
62
74
}
63
75
64
76
@ Test
65
- public void canConvert () throws Exception {
77
+ void canConvert () {
66
78
BitbucketCredentialConverter converter = new BitbucketCredentialConverter ();
67
79
assertThat ("correct registration of valid type" , converter .canConvert ("bitbucketToken" ), is (true ));
68
80
assertThat ("incorrect type is rejected" , converter .canConvert ("something" ), is (false ));
69
81
}
70
82
71
- @ Test ( expected = CredentialsConvertionException . class )
72
- public void failsToConvertASecretMissingText () throws Exception {
83
+ @ Test
84
+ void failsToConvertASecretMissingText () throws Exception {
73
85
BitbucketCredentialConverter converter = new BitbucketCredentialConverter ();
74
86
75
87
try (InputStream is = get ("missing-text.yaml" )) {
76
88
Secret secret = Serialization .unmarshal (is , Secret .class );
77
- assertThat ("The Secret was loaded correctly from disk" , notNullValue ());
89
+ assertThat ("The Secret was loaded correctly from disk" , secret , notNullValue ());
78
90
79
- BitbucketTokenCredentialsImpl credential = converter .convert (secret );
91
+ assertThrows ( CredentialsConvertionException . class , () -> converter .convert (secret ) );
80
92
}
81
93
}
82
94
83
- @ Test ( expected = CredentialsConvertionException . class )
84
- public void failsToConvertWithNonBase64EncodedText () throws Exception {
95
+ @ Test
96
+ void failsToConvertWithNonBase64EncodedText () throws Exception {
85
97
BitbucketCredentialConverter converter = new BitbucketCredentialConverter ();
86
98
87
99
try (InputStream is = get ("text-isnt-base64.yaml" )) {
88
100
Secret secret = Serialization .unmarshal (is , Secret .class );
89
- assertThat ("The Secret was loaded correctly from disk" , notNullValue ());
101
+ assertThat ("The Secret was loaded correctly from disk" , secret , notNullValue ());
90
102
91
- BitbucketTokenCredentialsImpl credential = converter .convert (secret );
103
+ assertThrows ( CredentialsConvertionException . class , () -> converter .convert (secret ) );
92
104
}
93
105
}
94
106
95
107
@ Test
96
- public void canConvertAValidSecret () throws Exception {
108
+ void canConvertAValidSecret () throws Exception {
97
109
ConfidentialStore .get ();
98
110
BitbucketCredentialConverter converter = new BitbucketCredentialConverter ();
99
111
100
112
try (InputStream is = get ("valid.yaml" )) {
101
113
Secret secret = Serialization .unmarshal (is , Secret .class );
102
- assertThat ("The Secret was loaded correctly from disk" , notNullValue ());
114
+ assertThat ("The Secret was loaded correctly from disk" , secret , notNullValue ());
103
115
104
116
BitbucketTokenCredentialsImpl credential = converter .convert (secret );
105
117
assertThat (credential , notNullValue ());
106
118
107
119
assertThat ("credential id is mapped correctly" , credential .getId (), is ("a-test-secret" ));
108
- assertThat ("credential description is mapped correctly" , credential .getDescription (), is ("secret bitbucket personal token credential from Kubernetes" ));
109
- assertThat ("credential text mapped to the secret" , credential .getSecret ().getPlainText (), is ("someSuperDuperSecret" ));
120
+ assertThat (
121
+ "credential description is mapped correctly" ,
122
+ credential .getDescription (),
123
+ is ("secret bitbucket personal token credential from Kubernetes" ));
124
+ assertThat (
125
+ "credential text mapped to the secret" ,
126
+ credential .getSecret ().getPlainText (),
127
+ is ("someSuperDuperSecret" ));
110
128
}
111
129
}
112
130
113
131
private static InputStream get (String resource ) {
114
132
InputStream is = BitbucketCredentialConverterTest .class .getResourceAsStream (resource );
115
- if (is == null ) {
116
- fail ("failed to load resource " + resource );
117
- }
133
+ assertNotNull (is , "failed to load resource " + resource );
118
134
return is ;
119
135
}
120
- }
136
+ }
0 commit comments