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);
}
Aggregations