Skip to content

Migrate tests to JUnit5 #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,98 +23,114 @@
*/
package co.johnrowley.jenkins.bitbucketcredentialsk8s;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

import com.atlassian.bitbucket.jenkins.internal.config.BitbucketTokenCredentialsImpl;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.CredentialsConvertionException;
import hudson.Extension;
import hudson.util.HistoricalSecrets;
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.client.utils.Serialization;
import jenkins.security.ConfidentialStore;
import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import java.io.InputStream;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import jenkins.security.ConfidentialStore;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

/**
* Tests for {@link BitbucketCredentialConverter}.
*/
@Extension
public class BitbucketCredentialConverterTest {
class BitbucketCredentialConverterTest {

private static MockedStatic<ConfidentialStore> csMockStatic;
private static MockedStatic<HistoricalSecrets> hsMockStatic;

@BeforeClass
public static void mockConfidentialStore() {
Mockito.mockStatic(ConfidentialStore.class);
Mockito.mockStatic(HistoricalSecrets.class);
@BeforeAll
static void mockConfidentialStore() {
csMockStatic = mockStatic(ConfidentialStore.class);
hsMockStatic = mockStatic(HistoricalSecrets.class);
}

@Before
public void before() {
ConfidentialStore csMock = Mockito.mock(ConfidentialStore.class);
Mockito.when(ConfidentialStore.get()).thenReturn(csMock);
Mockito.when(csMock.randomBytes(ArgumentMatchers.anyInt())).thenAnswer( it -> new byte[ (Integer)(it.getArguments()[0])] );
@BeforeEach
void before() {
ConfidentialStore csMock = mock(ConfidentialStore.class);
when(ConfidentialStore.get()).thenReturn(csMock);
when(csMock.randomBytes(anyInt())).thenAnswer(it -> new byte[(Integer) (it.getArguments()[0])]);
}

@AfterAll
static void resetMockStatic() {
csMockStatic.close();
hsMockStatic.close();
}

@Test
public void canConvert() throws Exception {
void canConvert() {
BitbucketCredentialConverter converter = new BitbucketCredentialConverter();
assertThat("correct registration of valid type", converter.canConvert("bitbucketToken"), is(true));
assertThat("incorrect type is rejected", converter.canConvert("something"), is(false));
}

@Test(expected = CredentialsConvertionException.class)
public void failsToConvertASecretMissingText() throws Exception {
@Test
void failsToConvertASecretMissingText() throws Exception {
BitbucketCredentialConverter converter = new BitbucketCredentialConverter();

try (InputStream is = get("missing-text.yaml")) {
Secret secret = Serialization.unmarshal(is, Secret.class);
assertThat("The Secret was loaded correctly from disk", notNullValue());
assertThat("The Secret was loaded correctly from disk", secret, notNullValue());

BitbucketTokenCredentialsImpl credential = converter.convert(secret);
assertThrows(CredentialsConvertionException.class, () -> converter.convert(secret));
}
}

@Test(expected = CredentialsConvertionException.class)
public void failsToConvertWithNonBase64EncodedText() throws Exception {
@Test
void failsToConvertWithNonBase64EncodedText() throws Exception {
BitbucketCredentialConverter converter = new BitbucketCredentialConverter();

try (InputStream is = get("text-isnt-base64.yaml")) {
Secret secret = Serialization.unmarshal(is, Secret.class);
assertThat("The Secret was loaded correctly from disk", notNullValue());
assertThat("The Secret was loaded correctly from disk", secret, notNullValue());

BitbucketTokenCredentialsImpl credential = converter.convert(secret);
assertThrows(CredentialsConvertionException.class, () -> converter.convert(secret));
}
}

@Test
public void canConvertAValidSecret() throws Exception {
void canConvertAValidSecret() throws Exception {
ConfidentialStore.get();
BitbucketCredentialConverter converter = new BitbucketCredentialConverter();

try (InputStream is = get("valid.yaml")) {
Secret secret = Serialization.unmarshal(is, Secret.class);
assertThat("The Secret was loaded correctly from disk", notNullValue());
assertThat("The Secret was loaded correctly from disk", secret, notNullValue());

BitbucketTokenCredentialsImpl credential = converter.convert(secret);
assertThat(credential, notNullValue());

assertThat("credential id is mapped correctly", credential.getId(), is("a-test-secret"));
assertThat("credential description is mapped correctly", credential.getDescription(), is("secret bitbucket personal token credential from Kubernetes"));
assertThat("credential text mapped to the secret", credential.getSecret().getPlainText(), is("someSuperDuperSecret"));
assertThat(
"credential description is mapped correctly",
credential.getDescription(),
is("secret bitbucket personal token credential from Kubernetes"));
assertThat(
"credential text mapped to the secret",
credential.getSecret().getPlainText(),
is("someSuperDuperSecret"));
}
}

private static InputStream get(String resource) {
InputStream is = BitbucketCredentialConverterTest.class.getResourceAsStream(resource);
if (is == null) {
fail("failed to load resource " + resource);
}
assertNotNull(is, "failed to load resource " + resource);
return is;
}
}
}