]> source.dussan.org Git - archiva.git/blob
78b5b4d839b87324e02e3336145f33fcfe18ffb1
[archiva.git] /
1 package org.apache.archiva.metadata.repository;
2
3 /*
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
33
34 import java.text.SimpleDateFormat;
35 import java.util.*;
36
37 import static org.assertj.core.api.Assertions.assertThat;
38
39 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
40 @ContextConfiguration(locations = {"classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml"})
41 public abstract class AbstractMetadataRepositoryTest
42         extends TestCase {
43     protected static final String OTHER_REPO_ID = "other-repo";
44
45     protected MetadataRepository repository;
46     protected RepositorySessionFactory sessionFactory;
47
48     protected static final String TEST_REPO_ID = "test";
49
50     protected static final String TEST_PROJECT = "myproject";
51
52     protected static final String TEST_NAMESPACE = "mytest";
53
54     protected static final String TEST_PROJECT_VERSION = "1.0";
55
56     private static final String TEST_PROJECT_VERSION_2_0 = "2.0";
57
58     private static final String TEST_URL = "http://archiva.apache.org";
59
60     private static final Organization TEST_ORGANIZATION = new Organization("Apache", "http://apache.org");
61
62     private static final String TEST_FACET_ID = "test-facet-id";
63
64     private static final String TEST_NAME = "test/name";
65
66     private static final String TEST_VALUE = "test-value";
67
68     private static final String UNKNOWN = "unknown";
69
70     private static final String TEST_MD5 = "bd4a9b642562547754086de2dab26b7d";
71
72     private static final String TEST_SHA1 = "2e5daf0201ddeb068a62d5e08da18657ab2c6be9";
73
74     private static final String TEST_METADATA_KEY = "testkey";
75
76     private static final String TEST_METADATA_VALUE = "testmetadata";
77
78     protected Logger log = LoggerFactory.getLogger(getClass());
79
80     /*
81      * Used by tryAssert to allow to throw exceptions in the lambda expression.
82      */
83     @FunctionalInterface
84     private interface AssertFunction {
85         void accept() throws Exception;
86     }
87
88     private void tryAssert(AssertFunction func) throws Exception {
89         tryAssert(func, 5, 500);
90     }
91
92     /*
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.
96      */
97     private void tryAssert(AssertFunction func, int retries, int sleepMillis) throws Exception {
98         Throwable t = null;
99         int retry = retries;
100         while (retry-- > 0) {
101             try {
102                 func.accept();
103                 return;
104             } catch (Exception | AssertionError e) {
105                 t = e;
106                 Thread.currentThread().sleep(sleepMillis);
107                 log.warn("Retrying assert " + retry);
108             }
109         }
110         if (retry <= 0 && t != null) {
111             if (t instanceof RuntimeException) {
112                 throw (RuntimeException) t;
113             } else if (t instanceof Exception) {
114                 throw (Exception) t;
115             }
116         }
117     }
118
119
120     public static Map<String, MetadataFacetFactory> createTestMetadataFacetFactories() {
121         Map<String, MetadataFacetFactory> factories = new HashMap<>();
122         factories.put(TEST_FACET_ID, new MetadataFacetFactory() {
123             @Override
124             public MetadataFacet createMetadataFacet() {
125                 return new TestMetadataFacet(TEST_METADATA_VALUE);
126             }
127
128             @Override
129             public MetadataFacet createMetadataFacet(String repositoryId, String name) {
130                 return new TestMetadataFacet(TEST_METADATA_VALUE);
131             }
132         });
133
134         // add to ensure we don't accidentally create an empty facet ID.
135         factories.put("", new MetadataFacetFactory() {
136             @Override
137             public MetadataFacet createMetadataFacet() {
138                 return new TestMetadataFacet("", TEST_VALUE);
139             }
140
141             @Override
142             public MetadataFacet createMetadataFacet(String repositoryId, String name) {
143                 return new TestMetadataFacet("", TEST_VALUE);
144             }
145         });
146
147         // for the getArtifactsByProjectVersionMetadata tests
148         factories.put(GenericMetadataFacet.FACET_ID, new GenericMetadataFacetFactory());
149
150         return factories;
151     }
152
153     @Test
154     public void testRootNamespaceWithNoMetadataRepository()
155             throws Exception {
156         try (RepositorySession session = sessionFactory.createSession()) {
157             Collection<String> namespaces = repository.getRootNamespaces(session, TEST_REPO_ID);
158             assertThat(namespaces).isNotNull().isEmpty();
159         }
160     }
161
162     @Test
163     public void testGetNamespaceOnly()
164             throws Exception {
165         try (RepositorySession session = sessionFactory.createSession()) {
166             assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isEmpty();
167
168             repository.updateNamespace(session, TEST_REPO_ID, TEST_NAMESPACE);
169
170             assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isNotEmpty().contains(
171                     TEST_NAMESPACE).hasSize(1);
172
173             repository.removeNamespace(session, TEST_REPO_ID, TEST_NAMESPACE);
174
175             assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isEmpty();
176         }
177     }
178
179     @Test
180     public void testGetProjectOnly()
181             throws Exception {
182         try (RepositorySession session = sessionFactory.createSession()) {
183
184             assertNull(repository.getProject(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT));
185             assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isEmpty();
186
187             ProjectMetadata project = new ProjectMetadata();
188             project.setId(TEST_PROJECT);
189             project.setNamespace(TEST_NAMESPACE);
190
191             repository.updateProject(session, TEST_REPO_ID, project);
192
193             project = repository.getProject(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
194             assertEquals(TEST_PROJECT, project.getId());
195             assertEquals(TEST_NAMESPACE, project.getNamespace());
196
197             // test that namespace is also constructed
198
199             Collection<String> namespaces = repository.getRootNamespaces( session, TEST_REPO_ID);
200
201             assertThat(namespaces).isNotNull().isNotEmpty().contains(TEST_NAMESPACE).hasSize(1);
202         }
203     }
204
205     @Test
206     public void testGetProjectVersionOnly()
207             throws Exception {
208         try (RepositorySession session = sessionFactory.createSession()) {
209
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();
213
214             ProjectVersionMetadata metadata = new ProjectVersionMetadata();
215             metadata.setId(TEST_PROJECT_VERSION);
216
217             repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
218
219             metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
220             assertEquals(TEST_PROJECT_VERSION, metadata.getId());
221
222             // test that namespace and project is also constructed
223             Collection<String> namespaces = repository.getRootNamespaces(session, TEST_REPO_ID);
224
225             assertThat(namespaces).isNotNull().isNotEmpty().hasSize(1).contains(TEST_NAMESPACE);
226
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());
231         }
232     }
233
234     @Test
235     public void testGetArtifactOnly()
236             throws Exception {
237         try (RepositorySession session = sessionFactory.createSession()) {
238
239             assertThat(new ArrayList<>(
240                     repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE,
241                             TEST_PROJECT, TEST_PROJECT_VERSION))).isNotNull().isEmpty();
242             assertThat(
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();
245
246             assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isEmpty();
247
248             ArtifactMetadata metadata = createArtifact();
249
250             repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata);
251
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
257
258             assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isNotEmpty().contains(
259                     TEST_NAMESPACE).hasSize(1);
260
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());
264
265             ProjectVersionMetadata projectVersionMetadata =
266                     repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
267             assertEquals(TEST_PROJECT_VERSION, projectVersionMetadata.getId());
268         }
269     }
270
271     @Test
272     public void testUpdateProjectVersionMetadataWithNoOtherArchives()
273             throws Exception {
274         try (RepositorySession session = sessionFactory.createSession()) {
275
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);
283
284             metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
285             assertEquals(TEST_PROJECT_VERSION, metadata.getId());
286
287             List<MailingList> mailingLists = metadata.getMailingLists();
288
289             assertThat(mailingLists).isNotNull().isNotEmpty().hasSize(1);
290
291             mailingList = metadata.getMailingLists().get(0);
292             assertEquals("Foo List", mailingList.getName());
293
294             List<String> others = mailingList.getOtherArchives();
295             assertThat(others).isNotNull().isEmpty();
296         }
297     }
298
299     @Test
300     public void testUpdateProjectVersionMetadataWithAllElements()
301             throws Exception {
302
303         try (RepositorySession session = sessionFactory.createSession()) {
304
305             ProjectVersionMetadata metadata = new ProjectVersionMetadata();
306             metadata.setId(TEST_PROJECT_VERSION);
307
308             metadata.setName("project name");
309             metadata.setDescription("project description");
310             metadata.setUrl("the url");
311
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));
320
321             Scm scm = new Scm();
322             scm.setConnection("connection");
323             scm.setDeveloperConnection("dev conn");
324             scm.setUrl("url");
325             metadata.setScm(scm);
326
327             CiManagement ci = new CiManagement();
328             ci.setSystem("system");
329             ci.setUrl("ci url");
330             metadata.setCiManagement(ci);
331
332             IssueManagement tracker = new IssueManagement();
333             tracker.setSystem("system");
334             tracker.setUrl("issue tracker url");
335             metadata.setIssueManagement(tracker);
336
337             metadata.setOrganization(TEST_ORGANIZATION);
338
339             License l = new License();
340             l.setName("license name");
341             l.setUrl("license url");
342             metadata.addLicense(l);
343
344             Dependency d = new Dependency();
345             d.setArtifactId("artifactId");
346             d.setClassifier("classifier");
347             d.setGroupId("groupId");
348             d.setScope("scope");
349             d.setSystemPath("system path");
350             d.setType("type");
351             d.setVersion("version");
352             d.setOptional(true);
353             metadata.addDependency(d);
354
355             repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
356
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());
363
364             assertEquals("connection", metadata.getScm().getConnection());
365             assertEquals("dev conn", metadata.getScm().getDeveloperConnection());
366             assertEquals("url", metadata.getScm().getUrl());
367
368             assertEquals("system", metadata.getCiManagement().getSystem());
369             assertEquals("ci url", metadata.getCiManagement().getUrl());
370
371             assertEquals("system", metadata.getIssueManagement().getSystem());
372             assertEquals("issue tracker url", metadata.getIssueManagement().getUrl());
373
374             assertEquals(TEST_ORGANIZATION.getName(), metadata.getOrganization().getName());
375             assertEquals(TEST_ORGANIZATION.getUrl(), metadata.getOrganization().getUrl());
376
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()) //
385                     .isNotNull() //
386                     .isNotEmpty() //
387                     .hasSize(1) //
388                     .contains("other archive");
389
390             assertEquals(1, metadata.getLicenses().size());
391             l = metadata.getLicenses().get(0);
392             assertEquals("license name", l.getName());
393             assertEquals("license url", l.getUrl());
394
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());
405         }
406     }
407
408     @Test
409     public void testUpdateProjectVersionMetadataIncomplete()
410             throws Exception {
411         try (RepositorySession session = sessionFactory.createSession()) {
412
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);
417
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());
431         }
432     }
433
434     @Test
435     public void testUpdateProjectVersionMetadataWithExistingFacets()
436             throws Exception {
437         try (RepositorySession session = sessionFactory.createSession()) {
438
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);
444
445             metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
446             assertEquals(Collections.singleton(TEST_FACET_ID), metadata.getFacetIds());
447
448             metadata = new ProjectVersionMetadata();
449             metadata.setId(TEST_PROJECT_VERSION);
450             repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
451
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());
456         }
457     }
458
459     @Test
460     public void testUpdateProjectVersionMetadataWithNoExistingFacets()
461             throws Exception {
462         try (RepositorySession session = sessionFactory.createSession()) {
463
464             ProjectVersionMetadata metadata = new ProjectVersionMetadata();
465             metadata.setId(TEST_PROJECT_VERSION);
466             repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
467
468             metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
469
470             assertThat(metadata.getFacetIds()).isNotNull().isEmpty();
471
472             metadata = new ProjectVersionMetadata();
473             metadata.setId(TEST_PROJECT_VERSION);
474             repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
475
476             metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
477             assertThat(metadata.getFacetIds()).isNotNull().isEmpty();
478         }
479     }
480
481     @Test
482     public void testUpdateProjectVersionMetadataWithExistingFacetsFacetPropertyWasRemoved()
483             throws Exception {
484         try (RepositorySession session = sessionFactory.createSession()) {
485
486             ProjectVersionMetadata metadata = new ProjectVersionMetadata();
487             metadata.setId(TEST_PROJECT_VERSION);
488
489             Map<String, String> additionalProps = new HashMap<>();
490             additionalProps.put("deleteKey", "deleteValue");
491
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);
495
496             metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
497
498             assertThat(metadata.getFacetIds()).isNotNull().isNotEmpty().hasSize(1).contains(TEST_FACET_ID);
499
500             TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID);
501             Map<String, String> facetProperties = testFacet.toProperties();
502
503             assertEquals("deleteValue", facetProperties.get("deleteKey"));
504
505             facetProperties.remove("deleteKey");
506
507             TestMetadataFacet newTestFacet = new TestMetadataFacet(TEST_FACET_ID, testFacet.getValue(), facetProperties);
508             metadata.addFacet(newTestFacet);
509
510             repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
511
512             metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
513
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"));
517         }
518     }
519
520     @Test
521     public void testGetArtifactsDoesntReturnProjectVersionMetadataFacets()
522             throws Exception {
523         try (RepositorySession session = sessionFactory.createSession()) {
524
525             ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
526             versionMetadata.setId(TEST_PROJECT_VERSION);
527
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);
531
532             ArtifactMetadata artifactMetadata = createArtifact();
533             repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifactMetadata);
534             session.save();
535
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));
539
540             artifacts = repository.getArtifacts(session, TEST_REPO_ID);
541             assertEquals(Collections.singletonList(artifactMetadata), new ArrayList<>(artifacts));
542
543             artifacts = repository.getArtifactsByChecksum(session, TEST_REPO_ID, TEST_SHA1);
544             assertEquals(Collections.singletonList(artifactMetadata), new ArrayList<>(artifacts));
545
546             artifacts = repository.getArtifactsByChecksum(session, TEST_REPO_ID, TEST_MD5);
547             assertEquals(Collections.singletonList(artifactMetadata), new ArrayList<>(artifacts));
548
549             artifacts = repository.getArtifactsByDateRange(session, TEST_REPO_ID, null, null);
550             assertEquals(Collections.singletonList(artifactMetadata), new ArrayList<>(artifacts));
551         }
552     }
553
554     @Test
555     public void testUpdateArtifactMetadataWithExistingFacetsFacetPropertyWasRemoved()
556             throws Exception {
557         try (RepositorySession session = sessionFactory.createSession()) {
558
559             ArtifactMetadata metadata = createArtifact();
560
561             Map<String, String> additionalProps = new HashMap<>();
562             additionalProps.put("deleteKey", "deleteValue");
563
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);
567
568             Collection<ArtifactMetadata> artifacts =
569                     repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
570
571             assertThat(artifacts).isNotNull().isNotEmpty().hasSize(1);
572             metadata = artifacts.iterator().next();
573
574             Collection<String> ids = metadata.getFacetIds();
575             assertThat(ids).isNotNull().isNotEmpty().hasSize(1).contains(TEST_FACET_ID);
576
577             TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID);
578             Map<String, String> facetProperties = testFacet.toProperties();
579
580             assertEquals("deleteValue", facetProperties.get("deleteKey"));
581
582             facetProperties.remove("deleteKey");
583
584             TestMetadataFacet newTestFacet = new TestMetadataFacet(TEST_FACET_ID, testFacet.getValue(), facetProperties);
585             metadata.addFacet(newTestFacet);
586
587             repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata);
588
589             artifacts = repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
590
591             assertThat(artifacts).isNotNull().isNotEmpty().hasSize(1);
592             metadata = artifacts.iterator().next();
593
594             ids = metadata.getFacetIds();
595             assertThat(ids).isNotNull().isNotEmpty().hasSize(1).contains(TEST_FACET_ID);
596
597             testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID);
598
599             Map<String, String> props = testFacet.toProperties();
600             assertThat(props).isNotNull().doesNotContainKey("deleteKey");
601         }
602     }
603
604     @Test
605     public void testUpdateArtifactMetadataWithExistingFacets()
606             throws Exception {
607         try (RepositorySession session = sessionFactory.createSession()) {
608
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);
613
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());
617
618             metadata = createArtifact();
619             repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata);
620
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());
626         }
627     }
628
629     @Test
630     public void testUpdateArtifactMetadataWithNoExistingFacets()
631             throws Exception {
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);
635
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()));
639
640             metadata = createArtifact();
641             repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata);
642
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()));
646
647         }
648     }
649
650     @Test
651     public void testGetMetadataFacet()
652             throws Exception {
653         try (RepositorySession session = sessionFactory.createSession()) {
654             repository.addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet(TEST_VALUE));
655
656             TestMetadataFacet test =
657                     (TestMetadataFacet) repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME);
658
659             assertEquals(new TestMetadataFacet(TEST_VALUE), test);
660
661         }
662     }
663
664     @Test
665     public void testGetMetadataFacetWhenEmpty()
666             throws Exception {
667         try (RepositorySession session = sessionFactory.createSession()) {
668             assertNull(repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME));
669
670         }
671     }
672
673     @Test
674     public void testGetMetadataFacetWhenUnknownName()
675             throws Exception {
676         try (RepositorySession session = sessionFactory.createSession()) {
677             repository.addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet(TEST_VALUE));
678
679             assertNull(repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, UNKNOWN));
680
681         }
682     }
683
684     @Test
685     public void testGetMetadataFacetWhenDefaultValue()
686             throws Exception {
687         try (RepositorySession session = sessionFactory.createSession()) {
688             repository.addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet(null));
689
690             MetadataFacet metadataFacet = repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME);
691
692             assertEquals(new TestMetadataFacet(TEST_METADATA_VALUE), metadataFacet);
693
694         }
695     }
696
697     @Test
698     public void testGetMetadataFacetWhenUnknownFacetId()
699             throws Exception {
700         try (RepositorySession session = sessionFactory.createSession()) {
701             assertNull(repository.getMetadataFacet( session, TEST_REPO_ID, UNKNOWN, TEST_NAME));
702
703         }
704     }
705
706     @Test
707     public void testGetMetadataFacets()
708             throws Exception {
709         try (RepositorySession session = sessionFactory.createSession()) {
710             repository.addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet(TEST_VALUE));
711
712             assertEquals(Collections.singletonList(TEST_NAME),
713                     repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID));
714
715         }
716     }
717
718     @Test
719     public void testGetMetadataFacetsWhenEmpty()
720             throws Exception {
721
722         try (RepositorySession session = sessionFactory.createSession()) {
723             List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
724             assertTrue(facets.isEmpty());
725
726         }
727     }
728
729     @Test
730     public void testRemoveFacets()
731             throws Exception {
732         try (RepositorySession session = sessionFactory.createSession()) {
733             repository.addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet(TEST_VALUE));
734
735             List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
736             assertFalse(facets.isEmpty());
737
738             repository.removeMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
739
740             facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
741             assertTrue(facets.isEmpty());
742
743         }
744     }
745
746     @Test
747     public void testRemoveFacetsWhenEmpty()
748             throws Exception {
749         try (RepositorySession session = sessionFactory.createSession()) {
750             List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
751             assertTrue(facets.isEmpty());
752
753             repository.removeMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
754
755             facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID);
756             assertTrue(facets.isEmpty());
757
758         }
759     }
760
761     @Test
762     public void testRemoveFacetsWhenUnknown()
763             throws Exception {
764         try (RepositorySession session = sessionFactory.createSession()) {
765 // testing no exception
766             repository.removeMetadataFacets( session, TEST_REPO_ID, UNKNOWN);
767
768         }
769     }
770
771     @Test
772     public void testRemoveFacetWhenUnknown()
773             throws Exception {
774         try (RepositorySession session = sessionFactory.createSession()) {
775 // testing no exception
776             repository.removeMetadataFacet( session, TEST_REPO_ID, UNKNOWN, TEST_NAME);
777
778         }
779     }
780
781     @Test
782     public void testRemoveFacet()
783             throws Exception {
784         try (RepositorySession session = sessionFactory.createSession()) {
785             TestMetadataFacet metadataFacet = new TestMetadataFacet(TEST_VALUE);
786             repository.addMetadataFacet( session, TEST_REPO_ID, metadataFacet);
787
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());
791
792             repository.removeMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME);
793
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());
797
798         }
799     }
800
801     @Test
802     public void testRemoveFacetWhenEmpty()
803             throws Exception {
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();
808
809             repository.removeMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME);
810
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();
814
815         }
816     }
817
818     @Test
819     public void hasMetadataFacetStart()
820             throws Exception {
821         try (RepositorySession session = sessionFactory.createSession()) {
822             assertFalse(repository.hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName()));
823
824
825         }
826     }
827
828     @Test
829     public void hasMetadataFacet()
830             throws Exception {
831
832         try (RepositorySession session = sessionFactory.createSession()) {
833             assertFalse(repository.hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName()));
834
835             Calendar cal = Calendar.getInstance();
836
837             repository.addMetadataFacet( session, TEST_REPO_ID, new KindOfRepositoryStatistics("first", cal.getTime()));
838
839             assertTrue(repository.hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName()));
840
841             cal.add(Calendar.MINUTE, 2);
842
843             repository.addMetadataFacet( session, TEST_REPO_ID, new KindOfRepositoryStatistics("second", cal.getTime()));
844
845             cal.add(Calendar.MINUTE, 2);
846
847             repository.addMetadataFacet( session, TEST_REPO_ID, new KindOfRepositoryStatistics("third", cal.getTime()));
848
849             List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName());
850
851             assertThat(facets).isNotNull().isNotEmpty().hasSize(3);
852
853             assertTrue(repository.hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName()));
854
855             repository.removeMetadataFacets( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName());
856
857             assertFalse(repository.hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName()));
858
859             facets = repository.getMetadataFacets( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName());
860
861             assertThat(facets).isNotNull().isEmpty();
862
863
864         }
865     }
866
867
868     @Test
869     public void testGetArtifacts()
870             throws Exception {
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);
876
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);
882
883         }
884     }
885
886     @Test
887     public void testGetArtifactVersions()
888             throws Exception {
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);
900
901             Collection<String> versions =
902                     repository.getArtifactVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
903
904             assertThat(versions).isNotNull().isNotEmpty().contains(version1, version2);
905
906         }
907     }
908
909     @Test
910     public void testGetArtifactVersionsMultipleArtifactsSingleVersion()
911             throws Exception {
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);
919
920             Collection<String> versions =
921                     repository.getArtifactVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
922
923             assertThat(versions).isNotNull().isNotEmpty().hasSize(1).containsExactly(TEST_PROJECT_VERSION);
924
925
926         }
927     }
928
929     @Test
930     public void testGetArtifactsByDateRangeOpen()
931             throws Exception {
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);
935             session.save();
936
937             List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, null, null);
938
939             assertEquals(Collections.singletonList(artifact), artifacts);
940
941         }
942     }
943
944     @Test
945     public void testGetArtifactsByDateRangeSparseNamespace()
946             throws Exception {
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);
952             session.save();
953
954             List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, null, null);
955
956             tryAssert(() -> assertEquals(Collections.singletonList(artifact), artifacts));
957
958         }
959     }
960
961     @Test
962     public void testGetArtifactsByDateRangeLowerBound()
963             throws Exception {
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);
967             session.save();
968
969             Date date = new Date(artifact.getWhenGathered().getTime() - 10000);
970
971             List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, date, null);
972
973             assertEquals(Collections.singletonList(artifact), artifacts);
974
975         }
976     }
977
978     @Test
979     public void testGetArtifactsByDateRangeLowerBoundOutOfRange()
980             throws Exception {
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);
984
985             Date date = new Date(artifact.getWhenGathered().getTime() + 10000);
986
987             List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, date, null);
988
989             assertThat(artifacts).isNotNull().isEmpty();
990
991         }
992     }
993
994     @Test
995     public void testGetArtifactsByDateRangeLowerAndUpperBound()
996             throws Exception {
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);
1000             session.save();
1001
1002             Date lower = new Date(artifact.getWhenGathered().getTime() - 10000);
1003             Date upper = new Date(artifact.getWhenGathered().getTime() + 10000);
1004
1005             List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, lower, upper);
1006
1007             assertEquals(Collections.singletonList(artifact), artifacts);
1008
1009         }
1010     }
1011
1012     @Test
1013     public void testGetArtifactsByDateRangeUpperBound()
1014             throws Exception {
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);
1018             session.save();
1019
1020             Date upper = new Date(artifact.getWhenGathered().getTime() + 10000);
1021
1022             List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, null, upper);
1023
1024             assertEquals(Collections.singletonList(artifact), artifacts);
1025
1026         }
1027     }
1028
1029     @Test
1030     public void testGetArtifactsByDateRangeUpperBoundOutOfRange()
1031             throws Exception {
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);
1035             session.save();
1036
1037             Date upper = new Date(artifact.getWhenGathered().getTime() - 10000);
1038
1039             List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, null, upper);
1040
1041             assertThat(artifacts).isNotNull().isEmpty();
1042
1043         }
1044     }
1045
1046     @Test
1047     public void testGetArtifactsByRepoId()
1048             throws Exception {
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);
1052             session.save();
1053
1054             tryAssert(() -> {
1055                         List<ArtifactMetadata> artifacts = repository.getArtifacts(session, TEST_REPO_ID);
1056                         assertEquals(Collections.singletonList(artifact), artifacts);
1057                     }
1058             );
1059         }
1060     }
1061
1062     @Test
1063     public void testGetArtifactsByRepoIdMultipleCopies()
1064             throws Exception {
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);
1068
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);
1072             session.save();
1073
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)));
1077
1078         }
1079     }
1080
1081
1082     @Test
1083     public void testGetArtifactsByDateRangeMultipleCopies()
1084             throws Exception {
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);
1088
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);
1092             session.save();
1093
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));
1099
1100         }
1101     }
1102
1103     @Test
1104     public void testGetArtifactsByChecksumMultipleCopies()
1105             throws Exception {
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);
1109
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);
1113             session.save();
1114
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)));
1124
1125         }
1126     }
1127
1128     @Test
1129     public void testGetNamespacesWithSparseDepth()
1130             throws Exception {
1131         try (RepositorySession session = sessionFactory.createSession()) {
1132             repository.updateNamespace( session, TEST_REPO_ID, "org.apache.maven.shared");
1133
1134             Collection<String> namespaces = repository.getRootNamespaces( session, TEST_REPO_ID);
1135
1136             assertThat(namespaces).isNotNull().isNotEmpty().hasSize(1).contains("org");
1137
1138             namespaces = repository.getNamespaces( session, TEST_REPO_ID, "org");
1139             assertThat(namespaces).isNotNull().isNotEmpty().hasSize(1).contains("apache");
1140
1141             namespaces = repository.getNamespaces( session, TEST_REPO_ID, "org.apache");
1142             assertThat(namespaces).isNotNull().isNotEmpty().hasSize(1).contains("maven");
1143
1144             namespaces = repository.getNamespaces( session, TEST_REPO_ID, "org.apache.maven");
1145             assertThat(namespaces).isNotNull().isNotEmpty().hasSize(1).contains("shared");
1146
1147         }
1148     }
1149
1150     @Test
1151     public void testGetNamespacesWithProjectsPresent()
1152             throws Exception {
1153         try (RepositorySession session = sessionFactory.createSession()) {
1154             String namespace = "org.apache.maven.shared";
1155             repository.updateNamespace( session, TEST_REPO_ID, namespace);
1156
1157             ProjectVersionMetadata metadata = new ProjectVersionMetadata();
1158             metadata.setId(TEST_PROJECT_VERSION);
1159             repository.updateProjectVersion( session, TEST_REPO_ID, namespace, TEST_PROJECT, metadata);
1160
1161             Collection<String> namespaces = repository.getNamespaces( session, TEST_REPO_ID, namespace);
1162
1163             assertThat(namespaces).isNotNull().isEmpty();
1164
1165
1166         }
1167     }
1168
1169     @Test
1170     public void testGetProjectsWithOtherNamespacesPresent()
1171             throws Exception {
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);
1177
1178             repository.updateNamespace( session, TEST_REPO_ID, "org.apache.maven.shared");
1179
1180             Collection<String> projects = repository.getProjects( session, TEST_REPO_ID, "org.apache.maven");
1181
1182             assertThat(projects).isNotNull().isNotEmpty().hasSize(1).contains(TEST_PROJECT);
1183
1184         }
1185     }
1186
1187     @Test
1188     public void testGetProjectVersionsWithOtherNamespacesPresent()
1189             throws Exception {
1190         try (RepositorySession session = sessionFactory.createSession()) {
1191 // an unusual case but technically possible where a project namespace matches another project's name
1192
1193             ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
1194             versionMetadata.setId(TEST_PROJECT_VERSION);
1195             repository.updateProjectVersion( session, TEST_REPO_ID, "org.apache.maven", TEST_PROJECT, versionMetadata);
1196
1197             repository.updateProjectVersion( session, TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT,
1198                     "other-project", versionMetadata);
1199
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);
1203
1204             versions = repository.getProjectVersions( session, TEST_REPO_ID, "org.apache.maven", TEST_PROJECT);
1205
1206             assertThat(versions).isNotNull().isNotEmpty().contains(TEST_PROJECT_VERSION);
1207
1208         }
1209     }
1210
1211     @Test
1212     public void testGetArtifactsByChecksumSingleResultMd5()
1213             throws Exception {
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);
1217             session.save();
1218
1219             assertEquals(Collections.singletonList(artifact),
1220                     new ArrayList<>(repository.getArtifactsByChecksum( session, TEST_REPO_ID, TEST_MD5)));
1221
1222         }
1223     }
1224
1225     @Test
1226     public void testGetArtifactsByChecksumSingleResultSha1()
1227             throws Exception {
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);
1231             session.save();
1232
1233             assertEquals(Collections.singletonList(artifact),
1234                     new ArrayList<>(repository.getArtifactsByChecksum( session, TEST_REPO_ID, TEST_SHA1)));
1235
1236         }
1237     }
1238
1239     @Test
1240     public void testGetArtifactsByChecksumDeepNamespace()
1241             throws Exception {
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);
1247             session.save();
1248
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))));
1253
1254         }
1255     }
1256
1257     @Test
1258     public void testGetArtifactsByChecksumMultipleResult()
1259             throws Exception {
1260
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);
1264
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);
1269             session.save();
1270
1271             tryAssert(() -> {
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);
1276             });
1277
1278             tryAssert(() -> {
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);
1282             });
1283         }
1284     }
1285
1286     @Test
1287     public void testGetArtifactsByChecksumNoResult()
1288             throws Exception {
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);
1292
1293             Collection<ArtifactMetadata> artifactsByChecksum =
1294                     repository.getArtifactsByChecksum( session, TEST_REPO_ID, "not checksum");
1295             assertThat(artifactsByChecksum).isNotNull().isEmpty();
1296
1297         }
1298     }
1299
1300     @Test
1301     public void testGetArtifactsByProjectVersionMetadata()
1302             throws Exception {
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);
1312
1313         }
1314     }
1315
1316     @Test
1317     public void testGetArtifactsByProjectVersionMetadataNoRepository()
1318             throws Exception {
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();
1325
1326         }
1327     }
1328
1329     @Test
1330     public void testGetArtifactsByProjectVersionMetadataAllRepositories()
1331             throws Exception {
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);
1337
1338         }
1339     }
1340
1341     @Test
1342     public void testGetArtifactsByMetadataAllRepositories()
1343             throws Exception {
1344         try (RepositorySession session = sessionFactory.createSession()) {
1345             createArtifactWithMavenArtifactFacet(session);
1346             tryAssert(() -> {
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));
1357             });
1358         }
1359     }
1360
1361     @Test
1362     public void testGetArtifactsByPropertySingleResult()
1363             throws Exception {
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);
1374
1375         }
1376     }
1377
1378     @Test
1379     public void testDeleteRepository()
1380             throws Exception {
1381         try (RepositorySession session = sessionFactory.createSession()) {
1382
1383             repository.updateNamespace(session, TEST_REPO_ID, TEST_NAMESPACE);
1384
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);
1393
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);
1400             session.save();
1401
1402             List<ArtifactMetadata> expected = Arrays.asList(artifact1, artifact2);
1403             Collections.sort(expected, new ArtifactMetadataComparator());
1404
1405
1406             tryAssert(() -> {
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);
1411             });
1412
1413             repository.removeRepository(session, TEST_REPO_ID);
1414
1415             assertTrue(repository.getArtifacts(session, TEST_REPO_ID).isEmpty());
1416             assertTrue(repository.getRootNamespaces(session, TEST_REPO_ID).isEmpty());
1417         }
1418     }
1419
1420
1421     @Test
1422     public void testDeleteArtifact()
1423             throws Exception {
1424         try (RepositorySession session = sessionFactory.createSession()) {
1425
1426             ArtifactMetadata artifact = createArtifact();
1427             artifact.addFacet(new TestMetadataFacet("value"));
1428
1429             repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1430
1431             assertThat(repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE,
1432                     TEST_PROJECT, TEST_PROJECT_VERSION)).containsExactly(artifact);
1433
1434             repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION_2_0, artifact);
1435
1436             Collection<String> versions = repository.getProjectVersions(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
1437
1438             log.info("versions {}", versions);
1439
1440             assertThat(versions).isNotNull().isNotEmpty().hasSize(2).contains("1.0", "2.0");
1441
1442             repository.removeArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId());
1443
1444             versions = repository.getProjectVersions(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
1445
1446             log.info("versions {}", versions);
1447
1448             assertThat(versions).isNotNull().isNotEmpty().hasSize(1).contains("2.0");
1449
1450             assertThat(repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE,
1451                     TEST_PROJECT, TEST_PROJECT_VERSION)).isNotNull().isEmpty();
1452
1453             assertThat(repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE,
1454                     TEST_PROJECT, TEST_PROJECT_VERSION_2_0)).isNotEmpty().hasSize(1);
1455         }
1456     }
1457
1458     @Test
1459     public void deleteArtifact()
1460             throws Exception {
1461         try (RepositorySession session = sessionFactory.createSession()) {
1462             ArtifactMetadata artifact = createArtifact();
1463             artifact.addFacet(new TestMetadataFacet("value"));
1464
1465             repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1466
1467             repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1468
1469             Collection<ArtifactMetadata> artifacts =
1470                     repository.getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
1471
1472             assertEquals(Collections.singletonList(artifact), new ArrayList<>(artifacts));
1473
1474             repository.removeArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId());
1475
1476             artifacts = repository.getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
1477
1478             assertThat(artifacts).isNotNull().isEmpty();
1479
1480         }
1481     }
1482
1483     @Test
1484     public void deleteVersion()
1485             throws Exception {
1486         try (RepositorySession session = sessionFactory.createSession()) {
1487             ArtifactMetadata artifact = createArtifact();
1488             artifact.addFacet(new TestMetadataFacet("value"));
1489
1490             repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1491
1492             repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1493
1494             Collection<String> versions = repository.getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
1495
1496             assertThat(versions).isNotNull().isNotEmpty().hasSize(1);
1497
1498             repository.removeProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
1499
1500             versions = repository.getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
1501
1502             assertThat(versions).isNotNull().isEmpty();
1503
1504         }
1505     }
1506
1507     @Test
1508     public void deleteProject()
1509             throws Exception {
1510         try (RepositorySession session = sessionFactory.createSession()) {
1511             ArtifactMetadata artifact = createArtifact();
1512             artifact.addFacet(new TestMetadataFacet("value"));
1513
1514             repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1515
1516             repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1517
1518             assertEquals(1, repository.getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT).size());
1519
1520             repository.removeProject( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
1521
1522             Collection<String> versions = repository.getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
1523
1524             assertThat(versions).isNotNull().isEmpty();
1525
1526         }
1527     }
1528
1529
1530     @Test
1531     public void deleteSnapshotVersion()
1532             throws Exception {
1533         try (RepositorySession session = sessionFactory.createSession()) {
1534
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");
1540
1541             repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactOne);
1542
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");
1548
1549             repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactTwo);
1550
1551             Collection<ArtifactMetadata> artifactMetadatas =
1552                     repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT");
1553
1554             assertThat(artifactMetadatas).isNotNull().isNotEmpty().hasSize(2);
1555
1556             log.info("artifactMetadatas: {}", artifactMetadatas);
1557
1558             repository.removeArtifact(session, artifactOne, "2.0-SNAPSHOT");
1559
1560             artifactMetadatas = repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT");
1561
1562             assertThat(artifactMetadatas).isNotNull().isNotEmpty().hasSize(1);
1563
1564             repository.removeArtifact(session, artifactTwo, "2.0-SNAPSHOT");
1565
1566             artifactMetadatas = repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT");
1567
1568             assertThat(artifactMetadatas).isNotNull().isEmpty();
1569         }
1570     }
1571
1572
1573     @Test
1574     public void testgetProjectReferences()
1575             throws Exception {
1576         try (RepositorySession session = sessionFactory.createSession()) {
1577
1578             ProjectVersionMetadata metadata = new ProjectVersionMetadata();
1579             metadata.setId(TEST_PROJECT_VERSION);
1580
1581             metadata.setName("project name");
1582             metadata.setDescription("project description");
1583             metadata.setUrl("the url");
1584
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");
1591             d.setType("type");
1592             d.setVersion("version");
1593             d.setOptional(true);
1594             metadata.addDependency(d);
1595
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");
1602             d.setType("type");
1603             d.setVersion("version1");
1604             d.setOptional(true);
1605             metadata.addDependency(d);
1606
1607             repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
1608
1609             session.save();
1610
1611             metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
1612
1613             Collection<ProjectVersionReference> references =
1614                     repository.getProjectReferences(session, TEST_REPO_ID, d.getGroupId(), d.getArtifactId(), d.getVersion());
1615
1616             log.info("references: {}", references);
1617
1618             assertThat(references).isNotNull().hasSize(1).contains(
1619                     new ProjectVersionReference(ProjectVersionReference.ReferenceType.DEPENDENCY, TEST_PROJECT, TEST_NAMESPACE,
1620                             TEST_PROJECT_VERSION));
1621
1622         }
1623     }
1624
1625     @Test
1626     public void testSearchArtifactsByKey()
1627             throws Exception {
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();
1633
1634         }
1635     }
1636
1637     @Test
1638     public void testSearchArtifactsByKeyExact()
1639             throws Exception {
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();
1646
1647         }
1648     }
1649
1650     @Test
1651     public void testSearchArtifactsByFacetKey()
1652             throws Exception {
1653         try (RepositorySession session = sessionFactory.createSession()) {
1654             createArtifactWithGenericMetadataFacet(session);
1655         }
1656         // Thread.currentThread().sleep(5000);
1657         try (RepositorySession session = sessionFactory.createSession()) {
1658             session.refresh();
1659             System.out.println(repository.getRootNamespaces(session, TEST_REPO_ID));
1660
1661             Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_KEY, TEST_METADATA_VALUE, false);
1662             assertThat(artifactsByProperty).isNotNull().isNotEmpty();
1663
1664         }
1665     }
1666
1667     @Test
1668     public void testSearchArtifactsByFacetKeyAllRepos()
1669             throws Exception {
1670         try (RepositorySession session = sessionFactory.createSession()) {
1671
1672             createArtifactWithGenericMetadataFacet(session);
1673             tryAssert(() -> {
1674                 Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts(session, null, TEST_METADATA_KEY, TEST_METADATA_VALUE, false);
1675                 assertThat(artifactsByProperty).isNotNull().isNotEmpty();
1676             });
1677         }
1678     }
1679
1680     @Test
1681     public void testSearchArtifactsFullText()
1682             throws Exception {
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();
1690
1691         }
1692     }
1693
1694     @Test
1695     public void testSearchArtifactsFullTextExact()
1696             throws Exception {
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();
1706
1707         }
1708     }
1709
1710     @Test
1711     public void testSearchArtifactsFullTextByFacet()
1712             throws Exception {
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();
1717     
1718         }
1719     }
1720
1721     private static ProjectMetadata createProject() {
1722         return createProject(TEST_NAMESPACE);
1723     }
1724
1725     private static ProjectMetadata createProject(String ns) {
1726         ProjectMetadata project = new ProjectMetadata();
1727         project.setId(TEST_PROJECT);
1728         project.setNamespace(ns);
1729         return project;
1730     }
1731
1732     private void createArtifactWithGenericMetadataFacet(RepositorySession session)
1733             throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException {
1734         createArtifactWithGenericMetadataFacet( session,1);
1735     }
1736
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);
1744     }
1745
1746     private void createArtifactWithMavenArtifactFacet(RepositorySession session)
1747             throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException {
1748         createArtifactWithMavenArtifactFacet(session, 1);
1749     }
1750
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);
1755     }
1756
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);
1764                 }
1765                 repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact);
1766             }
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);
1774             }
1775             session.save();
1776     }
1777
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);
1786
1787             repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata);
1788             session.save();
1789     }
1790
1791     private static ArtifactMetadata createArtifact() {
1792         return createArtifact("jar");
1793     }
1794
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);
1807         return artifact;
1808     }
1809
1810     private static class ArtifactMetadataComparator
1811             implements Comparator<ArtifactMetadata> {
1812         @Override
1813         public final int compare(ArtifactMetadata a, ArtifactMetadata b) {
1814             return a.getProject().compareTo(b.getProject());
1815         }
1816     }
1817
1818     private static class KindOfRepositoryStatistics
1819             implements MetadataFacet {
1820         private String value;
1821
1822         private Date date;
1823
1824         static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
1825
1826         private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC");
1827
1828         private KindOfRepositoryStatistics(String value, Date date) {
1829             this.value = value;
1830             this.date = date;
1831         }
1832
1833         @Override
1834         public String getName() {
1835             return createNameFormat().format(date);
1836         }
1837
1838         private static SimpleDateFormat createNameFormat() {
1839             SimpleDateFormat fmt = new SimpleDateFormat(SCAN_TIMESTAMP_FORMAT);
1840             fmt.setTimeZone(UTC_TIME_ZONE);
1841             return fmt;
1842         }
1843
1844         @Override
1845         public String getFacetId() {
1846             return KindOfRepositoryStatistics.class.getName();
1847         }
1848
1849         @Override
1850         public Map<String, String> toProperties() {
1851             return Collections.emptyMap();
1852         }
1853
1854         @Override
1855         public void fromProperties(Map<String, String> properties) {
1856             // no op
1857         }
1858     }
1859
1860     private static class TestMetadataFacet
1861             implements MetadataFacet {
1862         private String testFacetId;
1863
1864         private Map<String, String> additionalProps;
1865
1866         private String value;
1867
1868         private TestMetadataFacet(String value) {
1869             this.value = value;
1870             testFacetId = TEST_FACET_ID;
1871         }
1872
1873         private TestMetadataFacet(String facetId, String value) {
1874             this.value = value;
1875             testFacetId = facetId;
1876         }
1877
1878         private TestMetadataFacet(String facetId, String value, Map<String, String> additionalProps) {
1879             this(facetId, value);
1880             this.additionalProps = additionalProps;
1881         }
1882
1883         @Override
1884         public String getFacetId() {
1885             return testFacetId;
1886         }
1887
1888         @Override
1889         public String getName() {
1890             return TEST_NAME;
1891         }
1892
1893         @Override
1894         public Map<String, String> toProperties() {
1895             if (value != null) {
1896                 if (additionalProps == null) {
1897                     return Collections.singletonMap("foo", value);
1898                 } else {
1899                     Map<String, String> props = new HashMap<>();
1900                     props.put("foo", value);
1901
1902                     for (String key : additionalProps.keySet()) {
1903                         props.put(key, additionalProps.get(key));
1904                     }
1905                     return props;
1906                 }
1907             } else {
1908                 return Collections.emptyMap();
1909             }
1910         }
1911
1912         @Override
1913         public void fromProperties(Map<String, String> properties) {
1914             String value = properties.get("foo");
1915             if (value != null) {
1916                 this.value = value;
1917             }
1918
1919             properties.remove("foo");
1920
1921             if (additionalProps == null) {
1922                 additionalProps = new HashMap<>();
1923             }
1924
1925             for (String key : properties.keySet()) {
1926                 additionalProps.put(key, properties.get(key));
1927             }
1928         }
1929
1930         public String getValue() {
1931             return value;
1932         }
1933
1934         @Override
1935         public String toString() {
1936             return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
1937         }
1938
1939         @Override
1940         public boolean equals(Object o) {
1941             if (this == o) {
1942                 return true;
1943             }
1944             if (o == null || getClass() != o.getClass()) {
1945                 return false;
1946             }
1947
1948             TestMetadataFacet that = (TestMetadataFacet) o;
1949
1950             if (value != null ? !value.equals(that.value) : that.value != null) {
1951                 return false;
1952             }
1953
1954             return true;
1955         }
1956
1957         @Override
1958         public int hashCode() {
1959             return value != null ? value.hashCode() : 0;
1960         }
1961     }
1962 }