1 package org.apache.archiva.metadata.repository;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import junit.framework.TestCase;
23 import org.apache.archiva.metadata.generic.GenericMetadataFacet;
24 import org.apache.archiva.metadata.generic.GenericMetadataFacetFactory;
25 import org.apache.archiva.metadata.model.*;
26 import org.apache.archiva.repository.Repository;
27 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.test.context.ContextConfiguration;
34 import java.text.SimpleDateFormat;
37 import static org.assertj.core.api.Assertions.assertThat;
39 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
40 @ContextConfiguration(locations = {"classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml"})
41 public abstract class AbstractMetadataRepositoryTest
43 protected static final String OTHER_REPO_ID = "other-repo";
45 protected MetadataRepository repository;
46 protected RepositorySessionFactory sessionFactory;
48 protected static final String TEST_REPO_ID = "test";
50 protected static final String TEST_PROJECT = "myproject";
52 protected static final String TEST_NAMESPACE = "mytest";
54 protected static final String TEST_PROJECT_VERSION = "1.0";
56 private static final String TEST_PROJECT_VERSION_2_0 = "2.0";
58 private static final String TEST_URL = "http://archiva.apache.org";
60 private static final Organization TEST_ORGANIZATION = new Organization("Apache", "http://apache.org");
62 private static final String TEST_FACET_ID = "test-facet-id";
64 private static final String TEST_NAME = "test/name";
66 private static final String TEST_VALUE = "test-value";
68 private static final String UNKNOWN = "unknown";
70 private static final String TEST_MD5 = "bd4a9b642562547754086de2dab26b7d";
72 private static final String TEST_SHA1 = "2e5daf0201ddeb068a62d5e08da18657ab2c6be9";
74 private static final String TEST_METADATA_KEY = "testkey";
76 private static final String TEST_METADATA_VALUE = "testmetadata";
78 protected Logger log = LoggerFactory.getLogger(getClass());
81 * Used by tryAssert to allow to throw exceptions in the lambda expression.
84 private interface AssertFunction {
85 void accept() throws Exception;
88 private void tryAssert(AssertFunction func) throws Exception {
89 tryAssert(func, 5, 500);
93 * Runs the assert method until the assert is successful or the number of retries
94 * is reached. Needed because the JCR Oak index update is asynchronous, so updates
95 * may not be visible immediately after the modification.
97 private void tryAssert(AssertFunction func, int retries, int sleepMillis) throws Exception {
100 while (retry-- > 0) {
104 } catch (Exception | AssertionError e) {
106 Thread.currentThread().sleep(sleepMillis);
107 log.warn("Retrying assert " + retry);
110 if (retry <= 0 && t != null) {
111 if (t instanceof RuntimeException) {
112 throw (RuntimeException) t;
113 } else if (t instanceof Exception) {
120 public static Map<String, MetadataFacetFactory> createTestMetadataFacetFactories() {
121 Map<String, MetadataFacetFactory> factories = new HashMap<>();
122 factories.put(TEST_FACET_ID, new MetadataFacetFactory() {
124 public MetadataFacet createMetadataFacet() {
125 return new TestMetadataFacet(TEST_METADATA_VALUE);
129 public MetadataFacet createMetadataFacet(String repositoryId, String name) {
130 return new TestMetadataFacet(TEST_METADATA_VALUE);
134 // add to ensure we don't accidentally create an empty facet ID.
135 factories.put("", new MetadataFacetFactory() {
137 public MetadataFacet createMetadataFacet() {
138 return new TestMetadataFacet("", TEST_VALUE);
142 public MetadataFacet createMetadataFacet(String repositoryId, String name) {
143 return new TestMetadataFacet("", TEST_VALUE);
147 // for the getArtifactsByProjectVersionMetadata tests
148 factories.put(GenericMetadataFacet.FACET_ID, new GenericMetadataFacetFactory());
154 public void testRootNamespaceWithNoMetadataRepository()
156 try (RepositorySession session = sessionFactory.createSession()) {
157 Collection<String> namespaces = repository.getRootNamespaces(session, TEST_REPO_ID);
158 assertThat(namespaces).isNotNull().isEmpty();
163 public void testGetNamespaceOnly()
165 try (RepositorySession session = sessionFactory.createSession()) {
166 assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isEmpty();
168 repository.updateNamespace(session, TEST_REPO_ID, TEST_NAMESPACE);
170 assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isNotEmpty().contains(
171 TEST_NAMESPACE).hasSize(1);
173 repository.removeNamespace(session, TEST_REPO_ID, TEST_NAMESPACE);
175 assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isEmpty();
180 public void testGetProjectOnly()
182 try (RepositorySession session = sessionFactory.createSession()) {
184 assertNull(repository.getProject(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT));
185 assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isEmpty();
187 ProjectMetadata project = new ProjectMetadata();
188 project.setId(TEST_PROJECT);
189 project.setNamespace(TEST_NAMESPACE);
191 repository.updateProject(session, TEST_REPO_ID, project);
193 project = repository.getProject(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
194 assertEquals(TEST_PROJECT, project.getId());
195 assertEquals(TEST_NAMESPACE, project.getNamespace());
197 // test that namespace is also constructed
199 Collection<String> namespaces = repository.getRootNamespaces( session, TEST_REPO_ID);
201 assertThat(namespaces).isNotNull().isNotEmpty().contains(TEST_NAMESPACE).hasSize(1);
206 public void testGetProjectVersionOnly()
208 try (RepositorySession session = sessionFactory.createSession()) {
210 assertNull(repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION));
211 assertNull(repository.getProject(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT));
212 assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isEmpty();
214 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
215 metadata.setId(TEST_PROJECT_VERSION);
217 repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
219 metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
220 assertEquals(TEST_PROJECT_VERSION, metadata.getId());
222 // test that namespace and project is also constructed
223 Collection<String> namespaces = repository.getRootNamespaces(session, TEST_REPO_ID);
225 assertThat(namespaces).isNotNull().isNotEmpty().hasSize(1).contains(TEST_NAMESPACE);
227 ProjectMetadata projectMetadata = repository.getProject(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
228 assertNotNull(projectMetadata);
229 assertEquals(TEST_PROJECT, projectMetadata.getId());
230 assertEquals(TEST_NAMESPACE, projectMetadata.getNamespace());
235 public void testGetArtifactOnly()
237 try (RepositorySession session = sessionFactory.createSession()) {
239 assertThat(new ArrayList<>(
240 repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE,
241 TEST_PROJECT, TEST_PROJECT_VERSION))).isNotNull().isEmpty();
243 repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION)).isNull();
244 assertThat(repository.getProject(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT)).isNull();
246 assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isEmpty();
248 ArtifactMetadata metadata = createArtifact();
250 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata);
252 Collection<ArtifactMetadata> artifacts =
253 repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
254 //assertEquals( Collections.singletonList( metadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
255 assertThat(artifacts).containsExactly(metadata);
256 // test that namespace, project and project version is also constructed
258 assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isNotEmpty().contains(
259 TEST_NAMESPACE).hasSize(1);
261 ProjectMetadata projectMetadata = repository.getProject(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
262 assertEquals(TEST_PROJECT, projectMetadata.getId());
263 assertEquals(TEST_NAMESPACE, projectMetadata.getNamespace());
265 ProjectVersionMetadata projectVersionMetadata =
266 repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
267 assertEquals(TEST_PROJECT_VERSION, projectVersionMetadata.getId());
272 public void testUpdateProjectVersionMetadataWithNoOtherArchives()
274 try (RepositorySession session = sessionFactory.createSession()) {
276 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
277 metadata.setId(TEST_PROJECT_VERSION);
278 MailingList mailingList = new MailingList();
279 mailingList.setName("Foo List");
280 mailingList.setOtherArchives(Collections.<String>emptyList());
281 metadata.setMailingLists(Arrays.asList(mailingList));
282 repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
284 metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
285 assertEquals(TEST_PROJECT_VERSION, metadata.getId());
287 List<MailingList> mailingLists = metadata.getMailingLists();
289 assertThat(mailingLists).isNotNull().isNotEmpty().hasSize(1);
291 mailingList = metadata.getMailingLists().get(0);
292 assertEquals("Foo List", mailingList.getName());
294 List<String> others = mailingList.getOtherArchives();
295 assertThat(others).isNotNull().isEmpty();
300 public void testUpdateProjectVersionMetadataWithAllElements()
303 try (RepositorySession session = sessionFactory.createSession()) {
305 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
306 metadata.setId(TEST_PROJECT_VERSION);
308 metadata.setName("project name");
309 metadata.setDescription("project description");
310 metadata.setUrl("the url");
312 MailingList mailingList = new MailingList();
313 mailingList.setName("Foo List");
314 mailingList.setUnsubscribeAddress("UnsubscribeAddress");
315 mailingList.setSubscribeAddress("SubscribeAddress");
316 mailingList.setPostAddress("PostAddress");
317 mailingList.setMainArchiveUrl("MainArchiveUrl");
318 mailingList.setOtherArchives(Arrays.asList("other archive"));
319 metadata.setMailingLists(Arrays.asList(mailingList));
322 scm.setConnection("connection");
323 scm.setDeveloperConnection("dev conn");
325 metadata.setScm(scm);
327 CiManagement ci = new CiManagement();
328 ci.setSystem("system");
330 metadata.setCiManagement(ci);
332 IssueManagement tracker = new IssueManagement();
333 tracker.setSystem("system");
334 tracker.setUrl("issue tracker url");
335 metadata.setIssueManagement(tracker);
337 metadata.setOrganization(TEST_ORGANIZATION);
339 License l = new License();
340 l.setName("license name");
341 l.setUrl("license url");
342 metadata.addLicense(l);
344 Dependency d = new Dependency();
345 d.setArtifactId("artifactId");
346 d.setClassifier("classifier");
347 d.setGroupId("groupId");
349 d.setSystemPath("system path");
351 d.setVersion("version");
353 metadata.addDependency(d);
355 repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
357 metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
358 assertEquals(TEST_PROJECT_VERSION, metadata.getId());
359 assertEquals(TEST_PROJECT_VERSION, metadata.getVersion());
360 assertEquals("project name", metadata.getName());
361 assertEquals("project description", metadata.getDescription());
362 assertEquals("the url", metadata.getUrl());
364 assertEquals("connection", metadata.getScm().getConnection());
365 assertEquals("dev conn", metadata.getScm().getDeveloperConnection());
366 assertEquals("url", metadata.getScm().getUrl());
368 assertEquals("system", metadata.getCiManagement().getSystem());
369 assertEquals("ci url", metadata.getCiManagement().getUrl());
371 assertEquals("system", metadata.getIssueManagement().getSystem());
372 assertEquals("issue tracker url", metadata.getIssueManagement().getUrl());
374 assertEquals(TEST_ORGANIZATION.getName(), metadata.getOrganization().getName());
375 assertEquals(TEST_ORGANIZATION.getUrl(), metadata.getOrganization().getUrl());
377 assertEquals(1, metadata.getMailingLists().size());
378 MailingList retrievedMailingList = metadata.getMailingLists().get(0);
379 assertEquals(mailingList.getName(), retrievedMailingList.getName());
380 assertEquals(mailingList.getMainArchiveUrl(), retrievedMailingList.getMainArchiveUrl());
381 assertEquals(mailingList.getPostAddress(), retrievedMailingList.getPostAddress());
382 assertEquals(mailingList.getSubscribeAddress(), retrievedMailingList.getSubscribeAddress());
383 assertEquals(mailingList.getUnsubscribeAddress(), retrievedMailingList.getUnsubscribeAddress());
384 assertThat(retrievedMailingList.getOtherArchives()) //
388 .contains("other archive");
390 assertEquals(1, metadata.getLicenses().size());
391 l = metadata.getLicenses().get(0);
392 assertEquals("license name", l.getName());
393 assertEquals("license url", l.getUrl());
395 assertEquals(1, metadata.getDependencies().size());
396 d = metadata.getDependencies().get(0);
397 assertEquals("artifactId", d.getArtifactId());
398 assertEquals("classifier", d.getClassifier());
399 assertEquals("groupId", d.getGroupId());
400 assertEquals("scope", d.getScope());
401 assertEquals("system path", d.getSystemPath());
402 assertEquals("type", d.getType());
403 assertEquals("version", d.getVersion());
404 assertTrue(d.isOptional());
409 public void testUpdateProjectVersionMetadataIncomplete()
411 try (RepositorySession session = sessionFactory.createSession()) {
413 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
414 metadata.setId(TEST_PROJECT_VERSION);
415 metadata.setIncomplete(true);
416 repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
418 metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
419 assertEquals(true, metadata.isIncomplete());
420 assertNull(metadata.getCiManagement());
421 assertNull(metadata.getScm());
422 assertNull(metadata.getIssueManagement());
423 assertNull(metadata.getOrganization());
424 assertNull(metadata.getDescription());
425 assertNull(metadata.getName());
426 assertEquals(TEST_PROJECT_VERSION, metadata.getId());
427 assertEquals(TEST_PROJECT_VERSION, metadata.getVersion());
428 assertTrue(metadata.getMailingLists().isEmpty());
429 assertTrue(metadata.getLicenses().isEmpty());
430 assertTrue(metadata.getDependencies().isEmpty());
435 public void testUpdateProjectVersionMetadataWithExistingFacets()
437 try (RepositorySession session = sessionFactory.createSession()) {
439 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
440 metadata.setId(TEST_PROJECT_VERSION);
441 MetadataFacet facet = new TestMetadataFacet("baz");
442 metadata.addFacet(facet);
443 repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
445 metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
446 assertEquals(Collections.singleton(TEST_FACET_ID), metadata.getFacetIds());
448 metadata = new ProjectVersionMetadata();
449 metadata.setId(TEST_PROJECT_VERSION);
450 repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
452 metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
453 assertEquals(Collections.singleton(TEST_FACET_ID), metadata.getFacetIds());
454 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID);
455 assertEquals("baz", testFacet.getValue());
460 public void testUpdateProjectVersionMetadataWithNoExistingFacets()
462 try (RepositorySession session = sessionFactory.createSession()) {
464 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
465 metadata.setId(TEST_PROJECT_VERSION);
466 repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
468 metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
470 assertThat(metadata.getFacetIds()).isNotNull().isEmpty();
472 metadata = new ProjectVersionMetadata();
473 metadata.setId(TEST_PROJECT_VERSION);
474 repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
476 metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
477 assertThat(metadata.getFacetIds()).isNotNull().isEmpty();
482 public void testUpdateProjectVersionMetadataWithExistingFacetsFacetPropertyWasRemoved()
484 try (RepositorySession session = sessionFactory.createSession()) {
486 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
487 metadata.setId(TEST_PROJECT_VERSION);
489 Map<String, String> additionalProps = new HashMap<>();
490 additionalProps.put("deleteKey", "deleteValue");
492 MetadataFacet facet = new TestMetadataFacet(TEST_FACET_ID, "baz", additionalProps);
493 metadata.addFacet(facet);
494 repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
496 metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
498 assertThat(metadata.getFacetIds()).isNotNull().isNotEmpty().hasSize(1).contains(TEST_FACET_ID);
500 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID);
501 Map<String, String> facetProperties = testFacet.toProperties();
503 assertEquals("deleteValue", facetProperties.get("deleteKey"));
505 facetProperties.remove("deleteKey");
507 TestMetadataFacet newTestFacet = new TestMetadataFacet(TEST_FACET_ID, testFacet.getValue(), facetProperties);
508 metadata.addFacet(newTestFacet);
510 repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
512 metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
514 assertThat(metadata.getFacetIds()).isNotNull().isNotEmpty().hasSize(1).contains(TEST_FACET_ID);
515 testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID);
516 assertFalse(testFacet.toProperties().containsKey("deleteKey"));
521 public void testGetArtifactsDoesntReturnProjectVersionMetadataFacets()
523 try (RepositorySession session = sessionFactory.createSession()) {
525 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
526 versionMetadata.setId(TEST_PROJECT_VERSION);
528 MetadataFacet facet = new TestMetadataFacet(TEST_FACET_ID, "baz");
529 versionMetadata.addFacet(facet);
530 repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, versionMetadata);
532 ArtifactMetadata artifactMetadata = createArtifact();
533 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifactMetadata);
536 Collection<ArtifactMetadata> artifacts =
537 repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
538 assertEquals(Collections.singletonList(artifactMetadata), new ArrayList<>(artifacts));
540 artifacts = repository.getArtifacts(session, TEST_REPO_ID);
541 assertEquals(Collections.singletonList(artifactMetadata), new ArrayList<>(artifacts));
543 artifacts = repository.getArtifactsByChecksum(session, TEST_REPO_ID, TEST_SHA1);
544 assertEquals(Collections.singletonList(artifactMetadata), new ArrayList<>(artifacts));
546 artifacts = repository.getArtifactsByChecksum(session, TEST_REPO_ID, TEST_MD5);
547 assertEquals(Collections.singletonList(artifactMetadata), new ArrayList<>(artifacts));
549 artifacts = repository.getArtifactsByDateRange(session, TEST_REPO_ID, null, null);
550 assertEquals(Collections.singletonList(artifactMetadata), new ArrayList<>(artifacts));
555 public void testUpdateArtifactMetadataWithExistingFacetsFacetPropertyWasRemoved()
557 try (RepositorySession session = sessionFactory.createSession()) {
559 ArtifactMetadata metadata = createArtifact();
561 Map<String, String> additionalProps = new HashMap<>();
562 additionalProps.put("deleteKey", "deleteValue");
564 MetadataFacet facet = new TestMetadataFacet(TEST_FACET_ID, "baz", additionalProps);
565 metadata.addFacet(facet);
566 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata);
568 Collection<ArtifactMetadata> artifacts =
569 repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
571 assertThat(artifacts).isNotNull().isNotEmpty().hasSize(1);
572 metadata = artifacts.iterator().next();
574 Collection<String> ids = metadata.getFacetIds();
575 assertThat(ids).isNotNull().isNotEmpty().hasSize(1).contains(TEST_FACET_ID);
577 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID);
578 Map<String, String> facetProperties = testFacet.toProperties();
580 assertEquals("deleteValue", facetProperties.get("deleteKey"));
582 facetProperties.remove("deleteKey");
584 TestMetadataFacet newTestFacet = new TestMetadataFacet(TEST_FACET_ID, testFacet.getValue(), facetProperties);
585 metadata.addFacet(newTestFacet);
587 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata);
589 artifacts = repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
591 assertThat(artifacts).isNotNull().isNotEmpty().hasSize(1);
592 metadata = artifacts.iterator().next();
594 ids = metadata.getFacetIds();
595 assertThat(ids).isNotNull().isNotEmpty().hasSize(1).contains(TEST_FACET_ID);
597 testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID);
599 Map<String, String> props = testFacet.toProperties();
600 assertThat(props).isNotNull().doesNotContainKey("deleteKey");
605 public void testUpdateArtifactMetadataWithExistingFacets()
607 try (RepositorySession session = sessionFactory.createSession()) {
609 ArtifactMetadata metadata = createArtifact();
610 MetadataFacet facet = new TestMetadataFacet("baz");
611 metadata.addFacet(facet);
612 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata);
614 metadata = repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE,
615 TEST_PROJECT, TEST_PROJECT_VERSION).iterator().next();
616 assertEquals(Collections.singleton(TEST_FACET_ID), metadata.getFacetIds());
618 metadata = createArtifact();
619 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata);
621 metadata = repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE,
622 TEST_PROJECT, TEST_PROJECT_VERSION).iterator().next();
623 assertEquals(Collections.singleton(TEST_FACET_ID), metadata.getFacetIds());
624 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID);
625 assertEquals("baz", testFacet.getValue());
630 public void testUpdateArtifactMetadataWithNoExistingFacets()
632 try (RepositorySession session = sessionFactory.createSession()) {
633 ArtifactMetadata metadata = createArtifact();
634 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata);
636 metadata = repository.getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE,
637 TEST_PROJECT, TEST_PROJECT_VERSION).iterator().next();
638 assertEquals(Collections.<String>emptyList(), new ArrayList<String>(metadata.getFacetIds()));
640 metadata = createArtifact();
641 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata);
643 metadata = repository.getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE,
644 TEST_PROJECT, TEST_PROJECT_VERSION).iterator().next();
645 assertEquals(Collections.<String>emptyList(), new ArrayList<String>(metadata.getFacetIds()));
651 public void testGetMetadataFacet()
653 try (RepositorySession session = sessionFactory.createSession()) {
654 repository.addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet(TEST_VALUE));
656 TestMetadataFacet test =
657 (TestMetadataFacet) repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME);
659 assertEquals(new TestMetadataFacet(TEST_VALUE), test);
665 public void testGetMetadataFacetWhenEmpty()
667 try (RepositorySession session = sessionFactory.createSession()) {
668 assertNull(repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME));
674 public void testGetMetadataFacetWhenUnknownName()
676 try (RepositorySession session = sessionFactory.createSession()) {
677 repository.addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet(TEST_VALUE));
679 assertNull(repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, UNKNOWN));
685 public void testGetMetadataFacetWhenDefaultValue()
687 try (RepositorySession session = sessionFactory.createSession()) {
688 repository.addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet(null));
690 MetadataFacet metadataFacet = repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME);
692 assertEquals(new TestMetadataFacet(TEST_METADATA_VALUE), metadataFacet);
698 public void testGetMetadataFacetWhenUnknownFacetId()
700 try (RepositorySession session = sessionFactory.createSession()) {
701 assertNull(repository.getMetadataFacet( session, TEST_REPO_ID, UNKNOWN, TEST_NAME));
707 public void testGetMetadataFacets()
709 try (RepositorySession session = sessionFactory.createSession()) {
710 repository.addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet(TEST_VALUE));
712 assertEquals(Collections.singletonList(TEST_NAME),
713 repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID));
719 public void testGetMetadataFacetsWhenEmpty()
722 try (RepositorySession session = sessionFactory.createSession()) {
723 List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
724 assertTrue(facets.isEmpty());
730 public void testRemoveFacets()
732 try (RepositorySession session = sessionFactory.createSession()) {
733 repository.addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet(TEST_VALUE));
735 List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
736 assertFalse(facets.isEmpty());
738 repository.removeMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
740 facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
741 assertTrue(facets.isEmpty());
747 public void testRemoveFacetsWhenEmpty()
749 try (RepositorySession session = sessionFactory.createSession()) {
750 List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
751 assertTrue(facets.isEmpty());
753 repository.removeMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
755 facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
756 assertTrue(facets.isEmpty());
762 public void testRemoveFacetsWhenUnknown()
764 try (RepositorySession session = sessionFactory.createSession()) {
765 // testing no exception
766 repository.removeMetadataFacets( session, TEST_REPO_ID, UNKNOWN);
772 public void testRemoveFacetWhenUnknown()
774 try (RepositorySession session = sessionFactory.createSession()) {
775 // testing no exception
776 repository.removeMetadataFacet( session, TEST_REPO_ID, UNKNOWN, TEST_NAME);
782 public void testRemoveFacet()
784 try (RepositorySession session = sessionFactory.createSession()) {
785 TestMetadataFacet metadataFacet = new TestMetadataFacet(TEST_VALUE);
786 repository.addMetadataFacet( session, TEST_REPO_ID, metadataFacet);
788 assertEquals(metadataFacet, repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME));
789 List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
790 assertFalse(facets.isEmpty());
792 repository.removeMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME);
794 assertNull(repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME));
795 facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
796 assertTrue(facets.isEmpty());
802 public void testRemoveFacetWhenEmpty()
804 try (RepositorySession session = sessionFactory.createSession()) {
805 List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
806 assertThat(facets).isNotNull().isEmpty();
807 assertThat(repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME)).isNull();
809 repository.removeMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME);
811 facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
812 assertThat(facets).isNotNull().isEmpty();
813 assertThat(repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME)).isNull();
819 public void hasMetadataFacetStart()
821 try (RepositorySession session = sessionFactory.createSession()) {
822 assertFalse(repository.hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName()));
829 public void hasMetadataFacet()
832 try (RepositorySession session = sessionFactory.createSession()) {
833 assertFalse(repository.hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName()));
835 Calendar cal = Calendar.getInstance();
837 repository.addMetadataFacet( session, TEST_REPO_ID, new KindOfRepositoryStatistics("first", cal.getTime()));
839 assertTrue(repository.hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName()));
841 cal.add(Calendar.MINUTE, 2);
843 repository.addMetadataFacet( session, TEST_REPO_ID, new KindOfRepositoryStatistics("second", cal.getTime()));
845 cal.add(Calendar.MINUTE, 2);
847 repository.addMetadataFacet( session, TEST_REPO_ID, new KindOfRepositoryStatistics("third", cal.getTime()));
849 List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName());
851 assertThat(facets).isNotNull().isNotEmpty().hasSize(3);
853 assertTrue(repository.hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName()));
855 repository.removeMetadataFacets( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName());
857 assertFalse(repository.hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName()));
859 facets = repository.getMetadataFacets( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName());
861 assertThat(facets).isNotNull().isEmpty();
869 public void testGetArtifacts()
871 try (RepositorySession session = sessionFactory.createSession()) {
872 ArtifactMetadata artifact1 = createArtifact();
873 ArtifactMetadata artifact2 = createArtifact("pom");
874 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1);
875 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2);
877 Collection<ArtifactMetadata> artifacts =
878 repository.getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
879 ArrayList<ArtifactMetadata> actual = new ArrayList<>(artifacts);
880 Collections.sort(actual, (o1, o2) -> o1.getId().compareTo(o2.getId()));
881 assertEquals(Arrays.asList(artifact1, artifact2), actual);
887 public void testGetArtifactVersions()
889 try (RepositorySession session = sessionFactory.createSession()) {
890 ArtifactMetadata artifact1 = createArtifact();
891 String version1 = "1.0-20091212.012345-1";
892 artifact1.setId(artifact1.getProject() + "-" + version1 + ".jar");
893 artifact1.setVersion(version1);
894 ArtifactMetadata artifact2 = createArtifact();
895 String version2 = "1.0-20091212.123456-2";
896 artifact2.setId(artifact2.getProject() + "-" + version2 + ".jar");
897 artifact2.setVersion(version2);
898 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1);
899 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2);
901 Collection<String> versions =
902 repository.getArtifactVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
904 assertThat(versions).isNotNull().isNotEmpty().contains(version1, version2);
910 public void testGetArtifactVersionsMultipleArtifactsSingleVersion()
912 try (RepositorySession session = sessionFactory.createSession()) {
913 ArtifactMetadata artifact1 = createArtifact();
914 artifact1.setId(TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar");
915 ArtifactMetadata artifact2 = createArtifact();
916 artifact2.setId(TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "-sources.jar");
917 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1);
918 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2);
920 Collection<String> versions =
921 repository.getArtifactVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
923 assertThat(versions).isNotNull().isNotEmpty().hasSize(1).containsExactly(TEST_PROJECT_VERSION);
930 public void testGetArtifactsByDateRangeOpen()
932 try (RepositorySession session = sessionFactory.createSession()) {
933 ArtifactMetadata artifact = createArtifact();
934 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
937 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, null, null);
939 assertEquals(Collections.singletonList(artifact), artifacts);
945 public void testGetArtifactsByDateRangeSparseNamespace()
947 try (RepositorySession session = sessionFactory.createSession()) {
948 String namespace = "org.apache.archiva";
949 ArtifactMetadata artifact = createArtifact();
950 artifact.setNamespace(namespace);
951 repository.updateArtifact( session, TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
954 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, null, null);
956 tryAssert(() -> assertEquals(Collections.singletonList(artifact), artifacts));
962 public void testGetArtifactsByDateRangeLowerBound()
964 try (RepositorySession session = sessionFactory.createSession()) {
965 ArtifactMetadata artifact = createArtifact();
966 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
969 Date date = new Date(artifact.getWhenGathered().getTime() - 10000);
971 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, date, null);
973 assertEquals(Collections.singletonList(artifact), artifacts);
979 public void testGetArtifactsByDateRangeLowerBoundOutOfRange()
981 try (RepositorySession session = sessionFactory.createSession()) {
982 ArtifactMetadata artifact = createArtifact();
983 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
985 Date date = new Date(artifact.getWhenGathered().getTime() + 10000);
987 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, date, null);
989 assertThat(artifacts).isNotNull().isEmpty();
995 public void testGetArtifactsByDateRangeLowerAndUpperBound()
997 try (RepositorySession session = sessionFactory.createSession()) {
998 ArtifactMetadata artifact = createArtifact();
999 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1002 Date lower = new Date(artifact.getWhenGathered().getTime() - 10000);
1003 Date upper = new Date(artifact.getWhenGathered().getTime() + 10000);
1005 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, lower, upper);
1007 assertEquals(Collections.singletonList(artifact), artifacts);
1013 public void testGetArtifactsByDateRangeUpperBound()
1015 try (RepositorySession session = sessionFactory.createSession()) {
1016 ArtifactMetadata artifact = createArtifact();
1017 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1020 Date upper = new Date(artifact.getWhenGathered().getTime() + 10000);
1022 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, null, upper);
1024 assertEquals(Collections.singletonList(artifact), artifacts);
1030 public void testGetArtifactsByDateRangeUpperBoundOutOfRange()
1032 try (RepositorySession session = sessionFactory.createSession()) {
1033 ArtifactMetadata artifact = createArtifact();
1034 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1037 Date upper = new Date(artifact.getWhenGathered().getTime() - 10000);
1039 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, null, upper);
1041 assertThat(artifacts).isNotNull().isEmpty();
1047 public void testGetArtifactsByRepoId()
1049 try (RepositorySession session = sessionFactory.createSession()) {
1050 ArtifactMetadata artifact = createArtifact();
1051 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1055 List<ArtifactMetadata> artifacts = repository.getArtifacts(session, TEST_REPO_ID);
1056 assertEquals(Collections.singletonList(artifact), artifacts);
1063 public void testGetArtifactsByRepoIdMultipleCopies()
1065 try (RepositorySession session = sessionFactory.createSession()) {
1066 ArtifactMetadata artifact = createArtifact();
1067 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1069 ArtifactMetadata secondArtifact = createArtifact();
1070 secondArtifact.setRepositoryId(OTHER_REPO_ID);
1071 repository.updateArtifact( session, OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact);
1074 // test it restricts to the appropriate repository
1075 tryAssert(() -> assertEquals(Collections.singletonList(artifact), repository.getArtifacts( session, TEST_REPO_ID)));
1076 tryAssert(() -> assertEquals(Collections.singletonList(secondArtifact), repository.getArtifacts( session, OTHER_REPO_ID)));
1083 public void testGetArtifactsByDateRangeMultipleCopies()
1085 try (RepositorySession session = sessionFactory.createSession()) {
1086 ArtifactMetadata artifact = createArtifact();
1087 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1089 ArtifactMetadata secondArtifact = createArtifact();
1090 secondArtifact.setRepositoryId(OTHER_REPO_ID);
1091 repository.updateArtifact( session, OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact);
1094 // test it restricts to the appropriate repository
1095 assertEquals(Collections.singletonList(artifact),
1096 repository.getArtifactsByDateRange( session, TEST_REPO_ID, null, null));
1097 assertEquals(Collections.singletonList(secondArtifact),
1098 repository.getArtifactsByDateRange( session, OTHER_REPO_ID, null, null));
1104 public void testGetArtifactsByChecksumMultipleCopies()
1106 try (RepositorySession session = sessionFactory.createSession()) {
1107 ArtifactMetadata artifact = createArtifact();
1108 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1110 ArtifactMetadata secondArtifact = createArtifact();
1111 secondArtifact.setRepositoryId(OTHER_REPO_ID);
1112 repository.updateArtifact( session, OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact);
1115 // test it restricts to the appropriate repository
1116 assertEquals(Collections.singletonList(artifact),
1117 new ArrayList<>(repository.getArtifactsByChecksum( session, TEST_REPO_ID, TEST_SHA1)));
1118 assertEquals(Collections.singletonList(secondArtifact), new ArrayList<>(
1119 repository.getArtifactsByChecksum( session, OTHER_REPO_ID, TEST_SHA1)));
1120 assertEquals(Collections.singletonList(artifact),
1121 new ArrayList<>(repository.getArtifactsByChecksum( session, TEST_REPO_ID, TEST_MD5)));
1122 assertEquals(Collections.singletonList(secondArtifact),
1123 new ArrayList<>(repository.getArtifactsByChecksum( session, OTHER_REPO_ID, TEST_MD5)));
1129 public void testGetNamespacesWithSparseDepth()
1131 try (RepositorySession session = sessionFactory.createSession()) {
1132 repository.updateNamespace( session, TEST_REPO_ID, "org.apache.maven.shared");
1134 Collection<String> namespaces = repository.getRootNamespaces( session, TEST_REPO_ID);
1136 assertThat(namespaces).isNotNull().isNotEmpty().hasSize(1).contains("org");
1138 namespaces = repository.getNamespaces( session, TEST_REPO_ID, "org");
1139 assertThat(namespaces).isNotNull().isNotEmpty().hasSize(1).contains("apache");
1141 namespaces = repository.getNamespaces( session, TEST_REPO_ID, "org.apache");
1142 assertThat(namespaces).isNotNull().isNotEmpty().hasSize(1).contains("maven");
1144 namespaces = repository.getNamespaces( session, TEST_REPO_ID, "org.apache.maven");
1145 assertThat(namespaces).isNotNull().isNotEmpty().hasSize(1).contains("shared");
1151 public void testGetNamespacesWithProjectsPresent()
1153 try (RepositorySession session = sessionFactory.createSession()) {
1154 String namespace = "org.apache.maven.shared";
1155 repository.updateNamespace( session, TEST_REPO_ID, namespace);
1157 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
1158 metadata.setId(TEST_PROJECT_VERSION);
1159 repository.updateProjectVersion( session, TEST_REPO_ID, namespace, TEST_PROJECT, metadata);
1161 Collection<String> namespaces = repository.getNamespaces( session, TEST_REPO_ID, namespace);
1163 assertThat(namespaces).isNotNull().isEmpty();
1170 public void testGetProjectsWithOtherNamespacesPresent()
1172 try (RepositorySession session = sessionFactory.createSession()) {
1173 ProjectMetadata projectMetadata = new ProjectMetadata();
1174 projectMetadata.setId(TEST_PROJECT);
1175 projectMetadata.setNamespace("org.apache.maven");
1176 repository.updateProject( session, TEST_REPO_ID, projectMetadata);
1178 repository.updateNamespace( session, TEST_REPO_ID, "org.apache.maven.shared");
1180 Collection<String> projects = repository.getProjects( session, TEST_REPO_ID, "org.apache.maven");
1182 assertThat(projects).isNotNull().isNotEmpty().hasSize(1).contains(TEST_PROJECT);
1188 public void testGetProjectVersionsWithOtherNamespacesPresent()
1190 try (RepositorySession session = sessionFactory.createSession()) {
1191 // an unusual case but technically possible where a project namespace matches another project's name
1193 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
1194 versionMetadata.setId(TEST_PROJECT_VERSION);
1195 repository.updateProjectVersion( session, TEST_REPO_ID, "org.apache.maven", TEST_PROJECT, versionMetadata);
1197 repository.updateProjectVersion( session, TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT,
1198 "other-project", versionMetadata);
1200 Collection<String> versions =
1201 repository.getProjectVersions( session, TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, "other-project");
1202 assertThat(versions).isNotNull().isNotEmpty().contains(TEST_PROJECT_VERSION);
1204 versions = repository.getProjectVersions( session, TEST_REPO_ID, "org.apache.maven", TEST_PROJECT);
1206 assertThat(versions).isNotNull().isNotEmpty().contains(TEST_PROJECT_VERSION);
1212 public void testGetArtifactsByChecksumSingleResultMd5()
1214 try (RepositorySession session = sessionFactory.createSession()) {
1215 ArtifactMetadata artifact = createArtifact();
1216 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1219 assertEquals(Collections.singletonList(artifact),
1220 new ArrayList<>(repository.getArtifactsByChecksum( session, TEST_REPO_ID, TEST_MD5)));
1226 public void testGetArtifactsByChecksumSingleResultSha1()
1228 try (RepositorySession session = sessionFactory.createSession()) {
1229 ArtifactMetadata artifact = createArtifact();
1230 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1233 assertEquals(Collections.singletonList(artifact),
1234 new ArrayList<>(repository.getArtifactsByChecksum( session, TEST_REPO_ID, TEST_SHA1)));
1240 public void testGetArtifactsByChecksumDeepNamespace()
1242 try (RepositorySession session = sessionFactory.createSession()) {
1243 ArtifactMetadata artifact = createArtifact();
1244 String namespace = "multi.level.ns";
1245 artifact.setNamespace(namespace);
1246 repository.updateArtifact( session, TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1249 tryAssert(() -> assertEquals(Collections.singletonList(artifact),
1250 new ArrayList<>(repository.getArtifactsByChecksum( session, TEST_REPO_ID, TEST_SHA1))));
1251 tryAssert(() -> assertEquals(Collections.singletonList(artifact),
1252 new ArrayList<>(repository.getArtifactsByChecksum( session, TEST_REPO_ID, TEST_MD5))));
1258 public void testGetArtifactsByChecksumMultipleResult()
1261 try (RepositorySession session = sessionFactory.createSession()) {
1262 ArtifactMetadata artifact1 = createArtifact();
1263 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1);
1265 String newProjectId = "another-project";
1266 ArtifactMetadata artifact2 = createArtifact();
1267 artifact2.setProject(newProjectId);
1268 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2);
1272 List<ArtifactMetadata> artifacts =
1273 new ArrayList<>(repository.getArtifactsByChecksum(session, TEST_REPO_ID, TEST_SHA1));
1274 Collections.sort(artifacts, new ArtifactMetadataComparator());
1275 assertEquals(Arrays.asList(artifact2, artifact1), artifacts);
1279 ArrayList<ArtifactMetadata> artifacts = new ArrayList<>(repository.getArtifactsByChecksum(session, TEST_REPO_ID, TEST_MD5));
1280 Collections.sort(artifacts, new ArtifactMetadataComparator());
1281 assertEquals(Arrays.asList(artifact2, artifact1), artifacts);
1287 public void testGetArtifactsByChecksumNoResult()
1289 try (RepositorySession session = sessionFactory.createSession()) {
1290 ArtifactMetadata artifact = createArtifact();
1291 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1293 Collection<ArtifactMetadata> artifactsByChecksum =
1294 repository.getArtifactsByChecksum( session, TEST_REPO_ID, "not checksum");
1295 assertThat(artifactsByChecksum).isNotNull().isEmpty();
1301 public void testGetArtifactsByProjectVersionMetadata()
1303 try (RepositorySession session = sessionFactory.createSession()) {
1304 createArtifactWithGenericMetadataFacet( session,10);
1305 Collection<ArtifactMetadata> artifactsByMetadata =
1306 repository.getArtifactsByProjectVersionMetadata( session, TEST_METADATA_KEY, TEST_METADATA_VALUE, TEST_REPO_ID);
1307 assertThat(artifactsByMetadata).hasSize(1);
1308 ArtifactMetadata artifactMetadata = artifactsByMetadata.iterator().next();
1309 assertThat(artifactMetadata.getId()).isEqualTo("projectId-1.0.jar");
1310 assertThat(artifactMetadata.getSha1()).isEqualTo(TEST_SHA1);
1311 assertThat(artifactMetadata.getRepositoryId()).isEqualTo(TEST_REPO_ID);
1317 public void testGetArtifactsByProjectVersionMetadataNoRepository()
1319 try (RepositorySession session = sessionFactory.createSession()) {
1320 createArtifactWithGenericMetadataFacet(session);
1321 Collection<ArtifactMetadata> artifactsByMetadata =
1322 repository.getArtifactsByProjectVersionMetadata( session, TEST_METADATA_KEY, TEST_METADATA_VALUE, null);
1323 assertThat(artifactsByMetadata).hasSize(1);
1324 assertThat(artifactsByMetadata.iterator().next().getRepositoryId()).isNotNull().isNotEmpty();
1330 public void testGetArtifactsByProjectVersionMetadataAllRepositories()
1332 try (RepositorySession session = sessionFactory.createSession()) {
1333 createArtifactWithGenericMetadataFacet(session);
1334 Collection<ArtifactMetadata> artifactsByMetadata =
1335 repository.getArtifactsByProjectVersionMetadata( session, TEST_METADATA_KEY, TEST_METADATA_VALUE, null);
1336 assertThat(artifactsByMetadata).hasSize(1);
1342 public void testGetArtifactsByMetadataAllRepositories()
1344 try (RepositorySession session = sessionFactory.createSession()) {
1345 createArtifactWithMavenArtifactFacet(session);
1347 Collection<ArtifactMetadata> artifactsByMetadata =
1348 repository.getArtifactsByMetadata(session, "foo", TEST_METADATA_VALUE, null);
1349 assertThat(artifactsByMetadata).hasSize(1);
1350 ArtifactMetadata artifactMetadata = artifactsByMetadata.iterator().next();
1351 assertThat(artifactMetadata.getId()).isEqualTo("projectId-1.0.jar");
1352 assertThat(artifactMetadata.getSha1()).isEqualTo(TEST_SHA1);
1353 assertThat(artifactMetadata.getRepositoryId()).isEqualTo(TEST_REPO_ID);
1354 MetadataFacet facet = artifactMetadata.getFacet(TEST_FACET_ID);
1355 assertThat(facet).isNotNull();
1356 assertThat(facet.toProperties()).isEqualTo(Collections.singletonMap("foo", TEST_METADATA_VALUE));
1362 public void testGetArtifactsByPropertySingleResult()
1364 try (RepositorySession session = sessionFactory.createSession()) {
1365 createArtifactWithData(session);
1366 // only works on JCR implementation
1367 // Collection<ArtifactMetadata> artifactsByProperty = repository.getArtifactsByProperty( "org.name", TEST_ORGANIZATION.getName(), TEST_REPO_ID );
1368 Collection<ArtifactMetadata> artifactsByProperty = repository.getArtifactsByProperty( session, "url", TEST_URL, TEST_REPO_ID);
1369 assertThat(artifactsByProperty).hasSize(1);
1370 ArtifactMetadata artifactMetadata = artifactsByProperty.iterator().next();
1371 assertThat(artifactMetadata.getId()).isEqualTo("projectId-1.0.jar");
1372 assertThat(artifactMetadata.getSha1()).isEqualTo(TEST_SHA1);
1373 assertThat(artifactMetadata.getRepositoryId()).isEqualTo(TEST_REPO_ID);
1379 public void testDeleteRepository()
1381 try (RepositorySession session = sessionFactory.createSession()) {
1383 repository.updateNamespace(session, TEST_REPO_ID, TEST_NAMESPACE);
1385 ProjectMetadata project1 = new ProjectMetadata();
1386 project1.setNamespace(TEST_NAMESPACE);
1387 project1.setId("project1");
1388 repository.updateProject(session, TEST_REPO_ID, project1);
1389 ProjectMetadata project2 = new ProjectMetadata();
1390 project2.setNamespace(TEST_NAMESPACE);
1391 project2.setId("project2");
1392 repository.updateProject(session, TEST_REPO_ID, project2);
1394 ArtifactMetadata artifact1 = createArtifact();
1395 artifact1.setProject("project1");
1396 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, "project1", TEST_PROJECT_VERSION, artifact1);
1397 ArtifactMetadata artifact2 = createArtifact();
1398 artifact2.setProject("project2");
1399 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, "project2", TEST_PROJECT_VERSION, artifact2);
1402 List<ArtifactMetadata> expected = Arrays.asList(artifact1, artifact2);
1403 Collections.sort(expected, new ArtifactMetadataComparator());
1407 List<ArtifactMetadata> actual =
1408 new ArrayList<>(repository.getArtifactsByDateRange(session, TEST_REPO_ID, null, null));
1409 Collections.sort(actual, new ArtifactMetadataComparator());
1410 assertEquals(expected, actual);
1413 repository.removeRepository(session, TEST_REPO_ID);
1415 assertTrue(repository.getArtifacts(session, TEST_REPO_ID).isEmpty());
1416 assertTrue(repository.getRootNamespaces(session, TEST_REPO_ID).isEmpty());
1422 public void testDeleteArtifact()
1424 try (RepositorySession session = sessionFactory.createSession()) {
1426 ArtifactMetadata artifact = createArtifact();
1427 artifact.addFacet(new TestMetadataFacet("value"));
1429 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1431 assertThat(repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE,
1432 TEST_PROJECT, TEST_PROJECT_VERSION)).containsExactly(artifact);
1434 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION_2_0, artifact);
1436 Collection<String> versions = repository.getProjectVersions(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
1438 log.info("versions {}", versions);
1440 assertThat(versions).isNotNull().isNotEmpty().hasSize(2).contains("1.0", "2.0");
1442 repository.removeArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId());
1444 versions = repository.getProjectVersions(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
1446 log.info("versions {}", versions);
1448 assertThat(versions).isNotNull().isNotEmpty().hasSize(1).contains("2.0");
1450 assertThat(repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE,
1451 TEST_PROJECT, TEST_PROJECT_VERSION)).isNotNull().isEmpty();
1453 assertThat(repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE,
1454 TEST_PROJECT, TEST_PROJECT_VERSION_2_0)).isNotEmpty().hasSize(1);
1459 public void deleteArtifact()
1461 try (RepositorySession session = sessionFactory.createSession()) {
1462 ArtifactMetadata artifact = createArtifact();
1463 artifact.addFacet(new TestMetadataFacet("value"));
1465 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1467 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1469 Collection<ArtifactMetadata> artifacts =
1470 repository.getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
1472 assertEquals(Collections.singletonList(artifact), new ArrayList<>(artifacts));
1474 repository.removeArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId());
1476 artifacts = repository.getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
1478 assertThat(artifacts).isNotNull().isEmpty();
1484 public void deleteVersion()
1486 try (RepositorySession session = sessionFactory.createSession()) {
1487 ArtifactMetadata artifact = createArtifact();
1488 artifact.addFacet(new TestMetadataFacet("value"));
1490 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1492 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1494 Collection<String> versions = repository.getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
1496 assertThat(versions).isNotNull().isNotEmpty().hasSize(1);
1498 repository.removeProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
1500 versions = repository.getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
1502 assertThat(versions).isNotNull().isEmpty();
1508 public void deleteProject()
1510 try (RepositorySession session = sessionFactory.createSession()) {
1511 ArtifactMetadata artifact = createArtifact();
1512 artifact.addFacet(new TestMetadataFacet("value"));
1514 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1516 repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1518 assertEquals(1, repository.getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT).size());
1520 repository.removeProject( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
1522 Collection<String> versions = repository.getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
1524 assertThat(versions).isNotNull().isEmpty();
1531 public void deleteSnapshotVersion()
1533 try (RepositorySession session = sessionFactory.createSession()) {
1535 ArtifactMetadata artifactOne = createArtifact();
1536 artifactOne.setVersion("2.0-20120618.214127-1");
1537 artifactOne.setProjectVersion("2.0-SNAPSHOT");
1538 artifactOne.addFacet(new TestMetadataFacet("value"));
1539 artifactOne.setId(TEST_PROJECT + "-" + "2.0-20120618.214127-1" + "." + "jar");
1541 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactOne);
1543 ArtifactMetadata artifactTwo = createArtifact();
1544 artifactTwo.setVersion("2.0-20120618.214135-2");
1545 artifactTwo.setProjectVersion("2.0-SNAPSHOT");
1546 artifactTwo.addFacet(new TestMetadataFacet("value"));
1547 artifactTwo.setId(TEST_PROJECT + "-" + "2.0-20120618.214135-2" + "." + "jar");
1549 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactTwo);
1551 Collection<ArtifactMetadata> artifactMetadatas =
1552 repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT");
1554 assertThat(artifactMetadatas).isNotNull().isNotEmpty().hasSize(2);
1556 log.info("artifactMetadatas: {}", artifactMetadatas);
1558 repository.removeArtifact(session, artifactOne, "2.0-SNAPSHOT");
1560 artifactMetadatas = repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT");
1562 assertThat(artifactMetadatas).isNotNull().isNotEmpty().hasSize(1);
1564 repository.removeArtifact(session, artifactTwo, "2.0-SNAPSHOT");
1566 artifactMetadatas = repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT");
1568 assertThat(artifactMetadatas).isNotNull().isEmpty();
1574 public void testgetProjectReferences()
1576 try (RepositorySession session = sessionFactory.createSession()) {
1578 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
1579 metadata.setId(TEST_PROJECT_VERSION);
1581 metadata.setName("project name");
1582 metadata.setDescription("project description");
1583 metadata.setUrl("the url");
1585 Dependency d = new Dependency();
1586 d.setArtifactId("artifactId");
1587 d.setClassifier("classifier");
1588 d.setGroupId("groupId");
1589 d.setScope("scope");
1590 d.setSystemPath("system path");
1592 d.setVersion("version");
1593 d.setOptional(true);
1594 metadata.addDependency(d);
1596 d = new Dependency();
1597 d.setArtifactId("artifactId1");
1598 d.setClassifier("classifier");
1599 d.setGroupId("groupId");
1600 d.setScope("scope");
1601 d.setSystemPath("system path");
1603 d.setVersion("version1");
1604 d.setOptional(true);
1605 metadata.addDependency(d);
1607 repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
1611 metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
1613 Collection<ProjectVersionReference> references =
1614 repository.getProjectReferences(session, TEST_REPO_ID, d.getGroupId(), d.getArtifactId(), d.getVersion());
1616 log.info("references: {}", references);
1618 assertThat(references).isNotNull().hasSize(1).contains(
1619 new ProjectVersionReference(ProjectVersionReference.ReferenceType.DEPENDENCY, TEST_PROJECT, TEST_NAMESPACE,
1620 TEST_PROJECT_VERSION));
1626 public void testSearchArtifactsByKey()
1628 try (RepositorySession session = sessionFactory.createSession()) {
1629 createArtifactWithData(session);
1630 session.refreshAndDiscard();
1631 Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, "url", TEST_URL, false);
1632 assertThat(artifactsByProperty).isNotNull().isNotEmpty();
1638 public void testSearchArtifactsByKeyExact()
1640 try (RepositorySession session = sessionFactory.createSession()) {
1641 createArtifactWithData(session);
1642 Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, "url", TEST_URL, true);
1643 assertThat(artifactsByProperty).isNotNull().isNotEmpty();
1644 artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, "org.name", "pache", true);
1645 assertThat(artifactsByProperty).isNotNull().isEmpty();
1651 public void testSearchArtifactsByFacetKey()
1653 try (RepositorySession session = sessionFactory.createSession()) {
1654 createArtifactWithGenericMetadataFacet(session);
1656 // Thread.currentThread().sleep(5000);
1657 try (RepositorySession session = sessionFactory.createSession()) {
1659 System.out.println(repository.getRootNamespaces(session, TEST_REPO_ID));
1661 Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_KEY, TEST_METADATA_VALUE, false);
1662 assertThat(artifactsByProperty).isNotNull().isNotEmpty();
1668 public void testSearchArtifactsByFacetKeyAllRepos()
1670 try (RepositorySession session = sessionFactory.createSession()) {
1672 createArtifactWithGenericMetadataFacet(session);
1674 Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts(session, null, TEST_METADATA_KEY, TEST_METADATA_VALUE, false);
1675 assertThat(artifactsByProperty).isNotNull().isNotEmpty();
1681 public void testSearchArtifactsFullText()
1683 try (RepositorySession session = sessionFactory.createSession()) {
1684 createArtifactWithGenericMetadataFacet(session);
1685 // only works in JCR
1686 // Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( TEST_URL, TEST_REPO_ID, false );
1687 Collection<ArtifactMetadata> artifactsByProperty =
1688 repository.searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_VALUE, false);
1689 assertThat(artifactsByProperty).isNotNull().isNotEmpty();
1695 public void testSearchArtifactsFullTextExact()
1697 try (RepositorySession session = sessionFactory.createSession()) {
1698 createArtifactWithGenericMetadataFacet(session);
1699 // only works in JCR
1700 // Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( TEST_URL, TEST_REPO_ID, true );
1701 Collection<ArtifactMetadata> artifactsByProperty =
1702 repository.searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_VALUE, true);
1703 assertThat(artifactsByProperty).isNotNull().isNotEmpty();
1704 artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_VALUE.substring(2), true);
1705 assertThat(artifactsByProperty).isNotNull().isEmpty();
1711 public void testSearchArtifactsFullTextByFacet()
1713 try (RepositorySession session = sessionFactory.createSession()) {
1714 createArtifactWithGenericMetadataFacet(session);
1715 Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_VALUE, false);
1716 assertThat(artifactsByProperty).isNotNull().isNotEmpty();
1721 private static ProjectMetadata createProject() {
1722 return createProject(TEST_NAMESPACE);
1725 private static ProjectMetadata createProject(String ns) {
1726 ProjectMetadata project = new ProjectMetadata();
1727 project.setId(TEST_PROJECT);
1728 project.setNamespace(ns);
1732 private void createArtifactWithGenericMetadataFacet(RepositorySession session)
1733 throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException {
1734 createArtifactWithGenericMetadataFacet( session,1);
1737 private void createArtifactWithGenericMetadataFacet(RepositorySession session, int artifacts)
1738 throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException {
1739 MetadataFacet metadataFacet = new GenericMetadataFacet();
1740 Map<String, String> properties = new HashMap<>();
1741 properties.put(TEST_METADATA_KEY, TEST_METADATA_VALUE);
1742 metadataFacet.fromProperties(properties);
1743 createArtifactWithFacet(session, artifacts, null, metadataFacet);
1746 private void createArtifactWithMavenArtifactFacet(RepositorySession session)
1747 throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException {
1748 createArtifactWithMavenArtifactFacet(session, 1);
1751 private void createArtifactWithMavenArtifactFacet(RepositorySession session, int artifacts)
1752 throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException {
1753 TestMetadataFacet facet = new TestMetadataFacet(TEST_METADATA_VALUE);
1754 createArtifactWithFacet(session, artifacts, facet, null);
1757 private void createArtifactWithFacet(RepositorySession session, int artifacts, MetadataFacet artifactFacet,
1758 MetadataFacet projectVersionMetadataFacet)
1759 throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException {
1760 for (int i = 0; i < artifacts; i++) {
1761 ArtifactMetadata artifact = createArtifact();
1762 if (artifactFacet != null) {
1763 artifact.addFacet(artifactFacet);
1765 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1767 if (projectVersionMetadataFacet != null) {
1768 ProjectVersionMetadata metadata =
1769 repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
1770 metadata.addFacet(projectVersionMetadataFacet);
1771 metadata.setOrganization(TEST_ORGANIZATION);
1772 metadata.setUrl(TEST_URL);
1773 repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
1778 private void createArtifactWithData(RepositorySession session)
1779 throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException {
1780 ArtifactMetadata artifact = createArtifact();
1781 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1782 ProjectVersionMetadata metadata =
1783 repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
1784 metadata.setOrganization(TEST_ORGANIZATION);
1785 metadata.setUrl(TEST_URL);
1787 repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
1791 private static ArtifactMetadata createArtifact() {
1792 return createArtifact("jar");
1795 private static ArtifactMetadata createArtifact(String type) {
1796 ArtifactMetadata artifact = new ArtifactMetadata();
1797 artifact.setId(TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type);
1798 artifact.setWhenGathered(new Date());
1799 artifact.setNamespace(TEST_NAMESPACE);
1800 artifact.setProject(TEST_PROJECT);
1801 artifact.setRepositoryId(TEST_REPO_ID);
1802 artifact.setFileLastModified(System.currentTimeMillis());
1803 artifact.setVersion(TEST_PROJECT_VERSION);
1804 artifact.setProjectVersion(TEST_PROJECT_VERSION);
1805 artifact.setMd5(TEST_MD5);
1806 artifact.setSha1(TEST_SHA1);
1810 private static class ArtifactMetadataComparator
1811 implements Comparator<ArtifactMetadata> {
1813 public final int compare(ArtifactMetadata a, ArtifactMetadata b) {
1814 return a.getProject().compareTo(b.getProject());
1818 private static class KindOfRepositoryStatistics
1819 implements MetadataFacet {
1820 private String value;
1824 static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
1826 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC");
1828 private KindOfRepositoryStatistics(String value, Date date) {
1834 public String getName() {
1835 return createNameFormat().format(date);
1838 private static SimpleDateFormat createNameFormat() {
1839 SimpleDateFormat fmt = new SimpleDateFormat(SCAN_TIMESTAMP_FORMAT);
1840 fmt.setTimeZone(UTC_TIME_ZONE);
1845 public String getFacetId() {
1846 return KindOfRepositoryStatistics.class.getName();
1850 public Map<String, String> toProperties() {
1851 return Collections.emptyMap();
1855 public void fromProperties(Map<String, String> properties) {
1860 private static class TestMetadataFacet
1861 implements MetadataFacet {
1862 private String testFacetId;
1864 private Map<String, String> additionalProps;
1866 private String value;
1868 private TestMetadataFacet(String value) {
1870 testFacetId = TEST_FACET_ID;
1873 private TestMetadataFacet(String facetId, String value) {
1875 testFacetId = facetId;
1878 private TestMetadataFacet(String facetId, String value, Map<String, String> additionalProps) {
1879 this(facetId, value);
1880 this.additionalProps = additionalProps;
1884 public String getFacetId() {
1889 public String getName() {
1894 public Map<String, String> toProperties() {
1895 if (value != null) {
1896 if (additionalProps == null) {
1897 return Collections.singletonMap("foo", value);
1899 Map<String, String> props = new HashMap<>();
1900 props.put("foo", value);
1902 for (String key : additionalProps.keySet()) {
1903 props.put(key, additionalProps.get(key));
1908 return Collections.emptyMap();
1913 public void fromProperties(Map<String, String> properties) {
1914 String value = properties.get("foo");
1915 if (value != null) {
1919 properties.remove("foo");
1921 if (additionalProps == null) {
1922 additionalProps = new HashMap<>();
1925 for (String key : properties.keySet()) {
1926 additionalProps.put(key, properties.get(key));
1930 public String getValue() {
1935 public String toString() {
1936 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
1940 public boolean equals(Object o) {
1944 if (o == null || getClass() != o.getClass()) {
1948 TestMetadataFacet that = (TestMetadataFacet) o;
1950 if (value != null ? !value.equals(that.value) : that.value != null) {
1958 public int hashCode() {
1959 return value != null ? value.hashCode() : 0;