25
25
26
26
import com .cloudbees .jenkins .plugins .kubernetes_credentials_provider .CredentialsConvertionException ;
27
27
import com .openshift .jenkins .plugins .OpenShiftTokenCredentials ;
28
-
29
- import hudson .Extension ;
30
28
import hudson .util .HistoricalSecrets ;
31
29
import io .fabric8 .kubernetes .api .model .Secret ;
32
30
import io .fabric8 .kubernetes .client .utils .Serialization ;
33
31
import jenkins .security .ConfidentialStore ;
34
- import org .junit .Before ;
35
- import org .junit .BeforeClass ;
36
- import org .junit .Test ;
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 ;
37
36
import org .mockito .ArgumentMatchers ;
37
+ import org .mockito .MockedStatic ;
38
38
import org .mockito .Mockito ;
39
+
39
40
import java .io .InputStream ;
41
+
40
42
import static org .hamcrest .CoreMatchers .is ;
41
43
import static org .hamcrest .CoreMatchers .notNullValue ;
42
44
import static org .hamcrest .MatcherAssert .assertThat ;
43
- import static org .junit .Assert .fail ;
45
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
46
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
44
47
45
48
/**
46
49
* Tests for {@link OpenshiftTokenCredentialConverter}.
47
50
*/
48
- @ Extension
49
- public class OpenshiftTokenCredentialConverterTest {
51
+ class OpenshiftTokenCredentialConverterTest {
52
+
53
+ private static MockedStatic <ConfidentialStore > confidentialStore ;
54
+ private static MockedStatic <HistoricalSecrets > historicalSecrets ;
50
55
51
- @ BeforeClass
52
- public static void beforeClass () {
53
- Mockito .mockStatic (ConfidentialStore .class );
54
- Mockito .mockStatic (HistoricalSecrets .class );
56
+ @ BeforeAll
57
+ static void beforeAll () {
58
+ confidentialStore = Mockito .mockStatic (ConfidentialStore .class );
59
+ historicalSecrets = Mockito .mockStatic (HistoricalSecrets .class );
55
60
}
56
61
57
- @ Before
58
- public void before () {
62
+ @ AfterAll
63
+ static void afterAll () {
64
+ confidentialStore .close ();
65
+ historicalSecrets .close ();
66
+ }
67
+
68
+ @ BeforeEach
69
+ void setUp () {
59
70
ConfidentialStore csMock = Mockito .mock (ConfidentialStore .class );
60
71
Mockito .when (ConfidentialStore .get ()).thenReturn (csMock );
61
- Mockito .when (csMock .randomBytes (ArgumentMatchers .anyInt ())).thenAnswer ( it -> new byte [ (Integer )(it .getArguments ()[0 ])] );
72
+ Mockito .when (csMock .randomBytes (ArgumentMatchers .anyInt ())).thenAnswer (it -> new byte [(Integer ) (it .getArguments ()[0 ])]);
62
73
}
63
74
64
75
@ Test
65
- public void canConvert () throws Exception {
76
+ void canConvert () {
66
77
OpenshiftTokenCredentialConverter converter = new OpenshiftTokenCredentialConverter ();
67
78
assertThat ("correct registration of valid type" , converter .canConvert ("openshiftToken" ), is (true ));
68
79
assertThat ("incorrect type is rejected" , converter .canConvert ("something" ), is (false ));
69
80
}
70
81
71
- @ Test ( expected = CredentialsConvertionException . class )
72
- public void failsToConvertASecretMissingText () throws Exception {
82
+ @ Test
83
+ void failsToConvertASecretMissingText () {
73
84
OpenshiftTokenCredentialConverter converter = new OpenshiftTokenCredentialConverter ();
74
- try (InputStream is = get ("missing-text.yaml" )) {
75
- Secret secret = Serialization .unmarshal (is , Secret .class );
76
- assertThat ("The Secret was loaded correctly from disk" , notNullValue ());
77
- converter .convert (secret );
78
- }
85
+ assertThrows (CredentialsConvertionException .class , () -> {
86
+ try (InputStream is = get ("missing-text.yaml" )) {
87
+ Secret secret = Serialization .unmarshal (is , Secret .class );
88
+ assertThat ("The Secret was loaded correctly from disk" , notNullValue ());
89
+ converter .convert (secret );
90
+ }
91
+ });
79
92
}
80
93
81
- @ Test ( expected = CredentialsConvertionException . class )
82
- public void failsToConvertWithNonBase64EncodedText () throws Exception {
94
+ @ Test
95
+ void failsToConvertWithNonBase64EncodedText () {
83
96
OpenshiftTokenCredentialConverter converter = new OpenshiftTokenCredentialConverter ();
84
- try (InputStream is = get ("text-isnt-base64.yaml" )) {
85
- Secret secret = Serialization .unmarshal (is , Secret .class );
86
- assertThat ("The Secret was loaded correctly from disk" , notNullValue ());
87
- converter .convert (secret );
88
- }
97
+ assertThrows (CredentialsConvertionException .class , () -> {
98
+ try (InputStream is = get ("text-isnt-base64.yaml" )) {
99
+ Secret secret = Serialization .unmarshal (is , Secret .class );
100
+ assertThat ("The Secret was loaded correctly from disk" , notNullValue ());
101
+ converter .convert (secret );
102
+ }
103
+ });
89
104
}
90
105
91
106
@ Test
92
- public void canConvertAValidSecret () throws Exception {
107
+ void canConvertAValidSecret () throws Exception {
93
108
ConfidentialStore .get ();
94
109
OpenshiftTokenCredentialConverter converter = new OpenshiftTokenCredentialConverter ();
95
110
try (InputStream is = get ("valid.yaml" )) {
@@ -107,9 +122,7 @@ public void canConvertAValidSecret() throws Exception {
107
122
108
123
private static InputStream get (String resource ) {
109
124
InputStream is = OpenshiftTokenCredentialConverterTest .class .getResourceAsStream (resource );
110
- if (is == null ) {
111
- fail ("failed to load resource " + resource );
112
- }
125
+ assertNotNull (is , "failed to load resource " + resource );
113
126
return is ;
114
127
}
115
128
}
0 commit comments