25
25
26
26
import com .atlassian .bitbucket .jenkins .internal .config .BitbucketTokenCredentialsImpl ;
27
27
import com .cloudbees .jenkins .plugins .kubernetes_credentials_provider .CredentialsConvertionException ;
28
- import hudson .Extension ;
29
28
import hudson .util .HistoricalSecrets ;
30
29
import io .fabric8 .kubernetes .api .model .Secret ;
31
30
import io .fabric8 .kubernetes .client .utils .Serialization ;
32
31
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 ;
32
+ import org .junit .jupiter . api . AfterAll ;
33
+ import org .junit .jupiter . api . BeforeAll ;
34
+ import org .junit .jupiter . api . BeforeEach ;
35
+ import org .junit .jupiter . api . Test ;
36
+ import org .mockito .MockedStatic ;
37
+
39
38
import java .io .InputStream ;
40
39
import static org .hamcrest .CoreMatchers .is ;
41
40
import static org .hamcrest .CoreMatchers .notNullValue ;
42
- import static org .junit .Assert .assertThat ;
43
- import static org .junit .Assert .fail ;
41
+ import static org .hamcrest .MatcherAssert .assertThat ;
42
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
43
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
44
+ import static org .mockito .ArgumentMatchers .anyInt ;
45
+ import static org .mockito .Mockito .mock ;
46
+ import static org .mockito .Mockito .mockStatic ;
47
+ import static org .mockito .Mockito .when ;
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 ()))
68
+ .thenAnswer (it -> new byte [(Integer ) (it .getArguments ()[0 ])]);
69
+ }
70
+
71
+ @ AfterAll
72
+ static void resetMockStatic () {
73
+ csMockStatic .close ();
74
+ hsMockStatic .close ();
62
75
}
63
76
64
77
@ Test
65
- public void canConvert () throws Exception {
78
+ void canConvert () {
66
79
BitbucketCredentialConverter converter = new BitbucketCredentialConverter ();
67
80
assertThat ("correct registration of valid type" , converter .canConvert ("bitbucketToken" ), is (true ));
68
81
assertThat ("incorrect type is rejected" , converter .canConvert ("something" ), is (false ));
69
82
}
70
83
71
- @ Test ( expected = CredentialsConvertionException . class )
72
- public void failsToConvertASecretMissingText () throws Exception {
84
+ @ Test
85
+ void failsToConvertASecretMissingText () throws Exception {
73
86
BitbucketCredentialConverter converter = new BitbucketCredentialConverter ();
74
87
75
88
try (InputStream is = get ("missing-text.yaml" )) {
76
89
Secret secret = Serialization .unmarshal (is , Secret .class );
77
- assertThat ("The Secret was loaded correctly from disk" , notNullValue ());
90
+ assertThat ("The Secret was loaded correctly from disk" , secret , notNullValue ());
78
91
79
- BitbucketTokenCredentialsImpl credential = converter .convert (secret );
92
+ assertThrows ( CredentialsConvertionException . class , () -> converter .convert (secret ) );
80
93
}
81
94
}
82
95
83
- @ Test ( expected = CredentialsConvertionException . class )
84
- public void failsToConvertWithNonBase64EncodedText () throws Exception {
96
+ @ Test
97
+ void failsToConvertWithNonBase64EncodedText () throws Exception {
85
98
BitbucketCredentialConverter converter = new BitbucketCredentialConverter ();
86
99
87
100
try (InputStream is = get ("text-isnt-base64.yaml" )) {
88
101
Secret secret = Serialization .unmarshal (is , Secret .class );
89
- assertThat ("The Secret was loaded correctly from disk" , notNullValue ());
102
+ assertThat ("The Secret was loaded correctly from disk" , secret , notNullValue ());
90
103
91
- BitbucketTokenCredentialsImpl credential = converter .convert (secret );
104
+ assertThrows ( CredentialsConvertionException . class , () -> converter .convert (secret ) );
92
105
}
93
106
}
94
107
95
108
@ Test
96
- public void canConvertAValidSecret () throws Exception {
109
+ void canConvertAValidSecret () throws Exception {
97
110
ConfidentialStore .get ();
98
111
BitbucketCredentialConverter converter = new BitbucketCredentialConverter ();
99
112
100
113
try (InputStream is = get ("valid.yaml" )) {
101
114
Secret secret = Serialization .unmarshal (is , Secret .class );
102
- assertThat ("The Secret was loaded correctly from disk" , notNullValue ());
115
+ assertThat ("The Secret was loaded correctly from disk" , secret , notNullValue ());
103
116
104
117
BitbucketTokenCredentialsImpl credential = converter .convert (secret );
105
118
assertThat (credential , notNullValue ());
@@ -112,9 +125,7 @@ public void canConvertAValidSecret() throws Exception {
112
125
113
126
private static InputStream get (String resource ) {
114
127
InputStream is = BitbucketCredentialConverterTest .class .getResourceAsStream (resource );
115
- if (is == null ) {
116
- fail ("failed to load resource " + resource );
117
- }
128
+ assertNotNull (is , "failed to load resource " + resource );
118
129
return is ;
119
130
}
120
131
}
0 commit comments