Search in sources :

Example 1 with CredentialsMatcher

use of com.cloudbees.plugins.credentials.CredentialsMatcher in project blueocean-plugin by jenkinsci.

the class GithubScmTest method mockCredentials.

void mockCredentials(String userId, String accessToken, String credentialId, String domainName) throws Exception {
    // Mock Credentials
    UsernamePasswordCredentialsImpl credentials = mock(UsernamePasswordCredentialsImpl.class);
    whenNew(UsernamePasswordCredentialsImpl.class).withAnyArguments().thenReturn(credentials);
    when(credentials.getId()).thenReturn(credentialId);
    when(credentials.getUsername()).thenReturn(userId);
    Secret secret = mock(Secret.class);
    when(secret.getPlainText()).thenReturn(accessToken);
    when(credentials.getPassword()).thenReturn(secret);
    CredentialsMatcher credentialsMatcher = mock(CredentialsMatcher.class);
    mockStatic(CredentialsMatchers.class);
    mockStatic(CredentialsProvider.class);
    when(CredentialsMatchers.withId(credentialId)).thenReturn(credentialsMatcher);
    BlueOceanDomainRequirement blueOceanDomainRequirement = mock(BlueOceanDomainRequirement.class);
    whenNew(BlueOceanDomainRequirement.class).withNoArguments().thenReturn(blueOceanDomainRequirement);
    when(CredentialsProvider.class, "lookupCredentials", StandardUsernamePasswordCredentials.class, jenkins, authentication, blueOceanDomainRequirement).thenReturn(Lists.newArrayList(credentials));
    when(CredentialsMatchers.class, "firstOrNull", Lists.newArrayList(credentials), credentialsMatcher).thenReturn(credentials);
    when(CredentialsMatchers.allOf(credentialsMatcher)).thenReturn(credentialsMatcher);
    // Mock credentials Domain
    Domain domain = mock(Domain.class);
    when(domain.getName()).thenReturn(domainName);
    // Mock credentials Store
    CredentialsStore credentialsStore = mock(CredentialsStore.class);
    when(credentialsStore.hasPermission(CredentialsProvider.CREATE)).thenReturn(true);
    when(credentialsStore.hasPermission(CredentialsProvider.UPDATE)).thenReturn(true);
    when(credentialsStore.getDomainByName(domainName)).thenReturn(domain);
    when(CredentialsProvider.class, "lookupStores", user).thenReturn(Lists.newArrayList(credentialsStore));
    when(credentialsStore.updateCredentials(domain, credentials, credentials)).thenReturn(true);
}
Also used : Secret(hudson.util.Secret) StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) CredentialsMatchers(com.cloudbees.plugins.credentials.CredentialsMatchers) CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) CredentialsMatcher(com.cloudbees.plugins.credentials.CredentialsMatcher) CredentialsProvider(com.cloudbees.plugins.credentials.CredentialsProvider) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) Domain(com.cloudbees.plugins.credentials.domains.Domain)

Aggregations

CredentialsMatcher (com.cloudbees.plugins.credentials.CredentialsMatcher)1 CredentialsMatchers (com.cloudbees.plugins.credentials.CredentialsMatchers)1 CredentialsProvider (com.cloudbees.plugins.credentials.CredentialsProvider)1 CredentialsStore (com.cloudbees.plugins.credentials.CredentialsStore)1 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1 Domain (com.cloudbees.plugins.credentials.domains.Domain)1 UsernamePasswordCredentialsImpl (com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl)1 Secret (hudson.util.Secret)1 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)1