Skip to content

Commit

Permalink
Merge pull request #256 from darxriggs/inline-credentials-lookup
Browse files Browse the repository at this point in the history
Inline "lookupCredentials" methods
  • Loading branch information
scaytrase authored Jan 6, 2020
2 parents 6a531be + b2ffc5e commit c5916d9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
Expand All @@ -41,7 +40,6 @@
import jenkins.model.JenkinsLocationConfiguration;
import jenkins.tasks.SimpleBuildStep;
import net.sf.json.JSONObject;
import org.acegisecurity.Authentication;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
Expand Down Expand Up @@ -811,7 +809,7 @@ private <T extends Credentials> T getCredentials(final Class<T> clazz, final Ite
String credentialsId = getCredentialsId();
if (StringUtils.isNotBlank(credentialsId) && clazz != null && project != null) {
credentials = CredentialsMatchers.firstOrNull(
lookupCredentials(clazz, project, ACL.SYSTEM, new ArrayList<>()),
CredentialsProvider.lookupCredentials(clazz, project, ACL.SYSTEM, new ArrayList<>()),
CredentialsMatchers.withId(credentialsId));
}

Expand All @@ -822,44 +820,14 @@ private <T extends Credentials> T getCredentials(final Class<T> clazz, final Ite
}
if (StringUtils.isNotBlank(credentialsId) && clazz != null && project != null) {
credentials = CredentialsMatchers.firstOrNull(
lookupCredentials(clazz, Jenkins.getInstance(), ACL.SYSTEM, new ArrayList<>()),
CredentialsProvider.lookupCredentials(clazz, Jenkins.getInstance(), ACL.SYSTEM, new ArrayList<>()),
CredentialsMatchers.withId(credentialsId));
}
}

return credentials;
}

/**
* Returns all credentials which are available to the specified {@link Authentication}
* for use by the specified {@link Item}.
*
* @param type the type of credentials to get.
* @param authentication the authentication.
* @param item the item.
* @param domainRequirements the credential domains to match.
* @param <C> the credentials type.
* @return the list of credentials.
*/
protected <C extends Credentials> List<C> lookupCredentials(Class<C> type, Item item, Authentication authentication, ArrayList<DomainRequirement> domainRequirements) {
return CredentialsProvider.lookupCredentials(type, item, authentication, domainRequirements);
}

/**
* Returns all credentials which are available to the specified {@link Authentication}
* for use by the specified {@link Item}.
*
* @param type the type of credentials to get.
* @param authentication the authentication.
* @param itemGroup the item group.
* @param domainRequirements the credential domains to match.
* @param <C> the credentials type.
* @return the list of credentials.
*/
protected <C extends Credentials> List<C> lookupCredentials(Class<C> type, ItemGroup<?> itemGroup, Authentication authentication, ArrayList<DomainRequirement> domainRequirements) {
return CredentialsProvider.lookupCredentials(type, itemGroup, authentication, domainRequirements);
}

/**
* Returns the build state to be pushed. This will select the specifically overwritten build state
* or the current build state else.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
Expand Down Expand Up @@ -182,13 +181,6 @@ public void test_prebuild_null_revision() {
@Test
public void test_build_http_client_with_proxy() throws Exception {
//given
StashNotifier sn = spy(this.sn);
doReturn(new ArrayList<Credentials>()).when(sn).lookupCredentials(
Mockito.<Class>anyObject(),
Mockito.<Item>anyObject(),
Mockito.<Authentication>anyObject(),
Mockito.<ArrayList<DomainRequirement>>anyObject());

String address = "192.168.1.1";
int port = 8080;
String login = "admin";
Expand Down Expand Up @@ -230,7 +222,7 @@ public void test_build_http_client_with_proxy() throws Exception {
@Test
public void test_build_http_client_https() throws Exception {
//given
sn = spy(new StashNotifier(
sn = new StashNotifier(
"https://localhost",
"scot",
true,
Expand All @@ -242,13 +234,8 @@ public void test_build_http_client_https() throws Exception {
false,
false,
false,
mock(JenkinsLocationConfiguration.class)));
mock(JenkinsLocationConfiguration.class));

doReturn(new ArrayList<Credentials>()).when(sn).lookupCredentials(
Mockito.<Class>anyObject(),
Mockito.<Item>anyObject(),
Mockito.<Authentication>anyObject(),
Mockito.<ArrayList<DomainRequirement>>anyObject());
PrintStream logger = mock(PrintStream.class);

//when
Expand Down Expand Up @@ -576,15 +563,7 @@ public void test_getBuildDescription_state() {
@Test
public void test_createRequest() throws Exception {
//given
StashNotifier sn = spy(this.sn);
ArrayList<Credentials> credentialList = new ArrayList<>();
UsernamePasswordCredentialsImpl credential = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "", "", "admin", "tiger");
credentialList.add(credential);
doReturn(credentialList).when(sn).lookupCredentials(
Mockito.<Class>anyObject(),
Mockito.<Item>anyObject(),
Mockito.<Authentication>anyObject(),
Mockito.<ArrayList<DomainRequirement>>anyObject());
PowerMockito.mockStatic(CredentialsMatchers.class);
when(CredentialsMatchers.firstOrNull(anyCollection(), any(CredentialsMatcher.class))).thenReturn(credential);

Expand Down

0 comments on commit c5916d9

Please sign in to comment.