|
24 | 24 | package com.cloudbees.plugins.credentials; |
25 | 25 |
|
26 | 26 | import com.cloudbees.plugins.credentials.common.IdCredentials; |
| 27 | +import com.cloudbees.plugins.credentials.domains.Domain; |
| 28 | +import com.cloudbees.plugins.credentials.domains.DomainCredentials; |
27 | 29 | import com.cloudbees.plugins.credentials.impl.DummyCredentials; |
28 | 30 | import com.cloudbees.plugins.credentials.impl.DummyIdCredentials; |
29 | 31 | import edu.umd.cs.findbugs.annotations.NonNull; |
|
39 | 41 | import hudson.tasks.BuildStepDescriptor; |
40 | 42 | import hudson.tasks.Builder; |
41 | 43 |
|
| 44 | +import java.util.Arrays; |
| 45 | +import java.util.Collections; |
42 | 46 | import java.util.HashMap; |
| 47 | +import java.util.List; |
| 48 | +import java.util.Map; |
| 49 | +import java.util.Objects; |
| 50 | +import java.util.function.Function; |
| 51 | +import java.util.function.Supplier; |
| 52 | +import java.util.stream.Collectors; |
| 53 | + |
43 | 54 | import jenkins.security.QueueItemAuthenticatorConfiguration; |
44 | 55 | import org.acegisecurity.Authentication; |
45 | 56 | import org.apache.commons.io.FileUtils; |
| 57 | +import org.apache.commons.lang.StringUtils; |
46 | 58 | import org.junit.Rule; |
47 | 59 | import org.junit.Test; |
48 | 60 | import org.jvnet.hudson.test.JenkinsRule; |
|
51 | 63 | import org.jvnet.hudson.test.TestExtension; |
52 | 64 | import org.kohsuke.stapler.DataBoundConstructor; |
53 | 65 |
|
| 66 | +import static org.junit.Assert.assertEquals; |
54 | 67 | import static org.junit.Assert.assertFalse; |
55 | 68 | import static org.junit.Assert.assertNotNull; |
56 | 69 | import static org.junit.Assert.assertTrue; |
@@ -164,6 +177,72 @@ public void given_globalScopeCredential_when_builtAsUserWithoutUseItem_then_cred |
164 | 177 | r.assertBuildStatus(Result.FAILURE, prj.scheduleBuild2(0).get()); |
165 | 178 | } |
166 | 179 |
|
| 180 | + @Test |
| 181 | + public void mergeDomainCredentialsMap() { |
| 182 | + SystemCredentialsProvider provider = SystemCredentialsProvider.getInstance(); |
| 183 | + |
| 184 | + // initial creds |
| 185 | + Map<Domain, List<Credentials>> creds = new HashMap<>(); |
| 186 | + creds.put(null, Arrays.asList( |
| 187 | + new DummyIdCredentials("foo-manchu", CredentialsScope.GLOBAL, "foo", "manchu", "Dr. Fu Manchu"), |
| 188 | + new DummyIdCredentials("bar-manchu", CredentialsScope.GLOBAL, "bar", "manchu", "Dr. Bar Manchu") |
| 189 | + )); |
| 190 | + Domain catsDotCom = new Domain("cats.com", "cats dot com", Collections.emptyList()); |
| 191 | + creds.put(catsDotCom, Arrays.asList( |
| 192 | + new DummyIdCredentials("kitty-cat", CredentialsScope.GLOBAL, "kitty", "manchu", "Mrs. Kitty"), |
| 193 | + new DummyIdCredentials("garfield-cat", CredentialsScope.GLOBAL, "garfield", "manchu", "Garfield") |
| 194 | + )); |
| 195 | + provider.setDomainCredentialsMap(creds); |
| 196 | + |
| 197 | + // merge creds |
| 198 | + Map<Domain, List<Credentials>> update = new HashMap<>(); |
| 199 | + update.put(null, Arrays.asList( |
| 200 | + new DummyIdCredentials("foo-manchu", CredentialsScope.GLOBAL, "foo", "Man-chu", "Dr. Fu Manchu Phd"), |
| 201 | + new DummyIdCredentials("strange", CredentialsScope.GLOBAL, "strange", "manchu", "Dr. Strange") |
| 202 | + )); |
| 203 | + Domain catsDotCom2 = new Domain("cats.com", "cats.com domain for cats", Collections.emptyList()); |
| 204 | + update.put(catsDotCom2, Arrays.asList( |
| 205 | + new DummyIdCredentials("garfield-cat", CredentialsScope.GLOBAL, "garfield", "manchu", "Garfield the Cat"), |
| 206 | + new DummyIdCredentials("eek-cat", CredentialsScope.GLOBAL, "eek", "manchu", "Eek the Cat") |
| 207 | + )); |
| 208 | + Domain dogsDotCom = new Domain("dogs.com", "dogs.com domain for dogs", Collections.emptyList()); |
| 209 | + update.put(dogsDotCom, Arrays.asList( |
| 210 | + new DummyIdCredentials("snoopy", CredentialsScope.GLOBAL, "snoopy", "manchu", "Snoop-a-Loop") |
| 211 | + )); |
| 212 | + |
| 213 | + // do merge |
| 214 | + provider.mergeDomainCredentialsMap(update); |
| 215 | + |
| 216 | + // verify |
| 217 | + List<DomainCredentials> domainCreds = provider.getDomainCredentials(); |
| 218 | + assertEquals(3, domainCreds.size()); |
| 219 | + for (DomainCredentials dc : domainCreds) { |
| 220 | + if (dc.getDomain().isGlobal()) { |
| 221 | + assertEquals(3, dc.getCredentials().size()); |
| 222 | + assertDummyCreds(dc.getCredentials(), DummyIdCredentials::getUsername, "bar", "foo", "strange"); |
| 223 | + assertDummyCreds(dc.getCredentials(), DummyIdCredentials::getDescription, "Dr. Bar Manchu", "Dr. Fu Manchu Phd", "Dr. Strange"); |
| 224 | + } else if (StringUtils.equals(dc.getDomain().getName(), "cats.com")) { |
| 225 | + assertEquals("cats.com domain for cats", dc.getDomain().getDescription()); |
| 226 | + assertEquals(3, dc.getCredentials().size()); |
| 227 | + assertDummyCreds(dc.getCredentials(), DummyIdCredentials::getUsername, "eek", "garfield", "kitty"); |
| 228 | + assertDummyCreds(dc.getCredentials(), DummyIdCredentials::getDescription, "Eek the Cat", "Garfield the Cat", "Mrs. Kitty"); |
| 229 | + } else if (StringUtils.equals(dc.getDomain().getName(), "dogs.com")) { |
| 230 | + assertEquals("dogs.com domain for dogs", dc.getDomain().getDescription()); |
| 231 | + assertEquals(1, dc.getCredentials().size()); |
| 232 | + assertDummyCreds(dc.getCredentials(), DummyIdCredentials::getUsername, "snoopy"); |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + private <T> void assertDummyCreds(List<Credentials> creds, Function<DummyIdCredentials, T> valSupplier, T... expected) { |
| 238 | + List<T> vals = creds.stream() |
| 239 | + .filter(c -> c instanceof DummyIdCredentials) |
| 240 | + .map(c -> valSupplier.apply((DummyIdCredentials) c)) |
| 241 | + .sorted() |
| 242 | + .collect(Collectors.toList()); |
| 243 | + assertEquals(Arrays.asList(expected), vals); |
| 244 | + } |
| 245 | + |
167 | 246 | public static class HasCredentialBuilder extends Builder { |
168 | 247 |
|
169 | 248 | private final String id; |
|
0 commit comments