1 package org.apache.archiva.metadata.repository;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import junit.framework.TestCase;
23 import org.apache.archiva.metadata.generic.GenericMetadataFacet;
24 import org.apache.archiva.metadata.generic.GenericMetadataFacetFactory;
25 import org.apache.archiva.metadata.model.*;
26 import org.apache.archiva.repository.Repository;
27 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.format.annotation.NumberFormat;
34 import org.springframework.test.context.ContextConfiguration;
36 import java.text.SimpleDateFormat;
39 import static org.assertj.core.api.Assertions.assertThat;
41 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
42 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml"} )
43 public abstract class AbstractMetadataRepositoryTest
46 protected static final String OTHER_REPO_ID = "other-repo";
49 protected static final String TEST_REPO_ID = "test";
51 protected static final String TEST_PROJECT = "myproject";
53 protected static final String TEST_NAMESPACE = "mytest";
55 protected static final String TEST_PROJECT_VERSION = "1.0";
57 private static final String TEST_PROJECT_VERSION_2_0 = "2.0";
59 protected static final String TEST_URL = "http://archiva.apache.org";
61 private static final Organization TEST_ORGANIZATION = new Organization( "Apache", "http://apache.org" );
63 private static final String TEST_FACET_ID = "test-facet-id";
65 private static final String TEST_NAME = "test/name";
67 private static final String TEST_VALUE = "test-value";
69 private static final String UNKNOWN = "unknown";
71 private static final String TEST_MD5 = "bd4a9b642562547754086de2dab26b7d";
73 private static final String TEST_SHA1 = "2e5daf0201ddeb068a62d5e08da18657ab2c6be9";
75 private static final String TEST_METADATA_KEY = "testkey";
77 private static final String TEST_METADATA_VALUE = "testmetadata";
79 protected Logger log = LoggerFactory.getLogger( getClass( ) );
82 * Used by tryAssert to allow to throw exceptions in the lambda expression.
85 protected interface AssertFunction
87 void accept( ) throws Exception;
90 protected void tryAssert( AssertFunction func ) throws Exception
92 tryAssert( func, 20, 500 );
96 protected abstract RepositorySessionFactory getSessionFactory( );
98 protected abstract MetadataRepository getRepository( );
101 * Runs the assert method until the assert is successful or the number of retries
102 * is reached. This is needed because the JCR Oak index update is asynchronous, so updates
103 * may not be visible immediately after the modification.
105 private void tryAssert( AssertFunction func, int retries, int sleepMillis ) throws Exception
109 while ( retry-- > 0 )
116 catch ( Exception | AssertionError e )
119 Thread.currentThread( ).sleep( sleepMillis );
120 log.warn( "Retrying assert {}: {}", retry, e.getMessage( ) );
123 log.warn( "Retries: {}, Exception: {}", retry, t.getMessage( ) );
124 if ( retry <= 0 && t != null )
126 if ( t instanceof RuntimeException )
128 throw (RuntimeException) t;
130 else if ( t instanceof Exception )
134 else if ( t instanceof Error )
142 public static Map<String, MetadataFacetFactory> createTestMetadataFacetFactories( )
144 Map<String, MetadataFacetFactory> factories = new HashMap<>( );
145 factories.put( TEST_FACET_ID, new MetadataFacetFactory( )
148 public MetadataFacet createMetadataFacet( )
150 return new TestMetadataFacet( TEST_METADATA_VALUE );
154 public MetadataFacet createMetadataFacet( String repositoryId, String name )
156 return new TestMetadataFacet( TEST_METADATA_VALUE );
160 // add to ensure we don't accidentally create an empty facet ID.
161 factories.put( "", new MetadataFacetFactory( )
164 public MetadataFacet createMetadataFacet( )
166 return new TestMetadataFacet( "", TEST_VALUE );
170 public MetadataFacet createMetadataFacet( String repositoryId, String name )
172 return new TestMetadataFacet( "", TEST_VALUE );
176 // for the getArtifactsByProjectVersionMetadata tests
177 factories.put( GenericMetadataFacet.FACET_ID, new GenericMetadataFacetFactory( ) );
183 public void testRootNamespaceWithNoMetadataRepository( )
186 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
189 Collection<String> namespaces = getRepository( ).getRootNamespaces( session, TEST_REPO_ID );
190 assertThat( namespaces ).isNotNull( ).isEmpty( );
196 public void testGetNamespaceOnly( )
199 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
202 assertThat( getRepository( ).getRootNamespaces( session, TEST_REPO_ID ) ).isNotNull( ).isEmpty( );
204 getRepository( ).updateNamespace( session, TEST_REPO_ID, TEST_NAMESPACE );
207 assertThat( getRepository( ).getRootNamespaces( session, TEST_REPO_ID ) ).isNotNull( ).isNotEmpty( ).contains(
208 TEST_NAMESPACE ).hasSize( 1 );
210 getRepository( ).removeNamespace( session, TEST_REPO_ID, TEST_NAMESPACE );
213 assertThat( getRepository( ).getRootNamespaces( session, TEST_REPO_ID ) ).isNotNull( ).isEmpty( );
219 public void testGetProjectOnly( )
222 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
226 assertNull( getRepository( ).getProject( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
227 assertThat( getRepository( ).getRootNamespaces( session, TEST_REPO_ID ) ).isNotNull( ).isEmpty( );
230 ProjectMetadata project = new ProjectMetadata( );
231 project.setId( TEST_PROJECT );
232 project.setNamespace( TEST_NAMESPACE );
234 getRepository( ).updateProject( session, TEST_REPO_ID, project );
237 ProjectMetadata proj = getRepository( ).getProject( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
238 assertEquals( TEST_PROJECT, proj.getId( ) );
239 assertEquals( TEST_NAMESPACE, proj.getNamespace( ) );
242 // test that namespace is also constructed
246 Collection<String> namespaces = getRepository( ).getRootNamespaces( session, TEST_REPO_ID );
248 assertThat( namespaces ).isNotNull( ).isNotEmpty( ).contains( TEST_NAMESPACE ).hasSize( 1 );
254 public void testGetProjectVersionOnly( )
257 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
261 assertNull( getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
262 assertNull( getRepository( ).getProject( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
263 assertThat( getRepository( ).getRootNamespaces( session, TEST_REPO_ID ) ).isNotNull( ).isEmpty( );
266 ProjectVersionMetadata metadata = new ProjectVersionMetadata( );
267 metadata.setId( TEST_PROJECT_VERSION );
269 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
271 metadata = getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
272 assertEquals( TEST_PROJECT_VERSION, metadata.getId( ) );
276 // test that namespace and project is also constructed
277 Collection<String> namespaces = getRepository( ).getRootNamespaces( session, TEST_REPO_ID );
279 assertThat( namespaces ).isNotNull( ).isNotEmpty( ).hasSize( 1 ).contains( TEST_NAMESPACE );
285 ProjectMetadata projectMetadata = getRepository( ).getProject( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
286 assertNotNull( projectMetadata );
287 assertEquals( TEST_PROJECT, projectMetadata.getId( ) );
288 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace( ) );
294 public void testGetArtifactOnly( )
297 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
301 assertThat( new ArrayList<>(
302 getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE,
303 TEST_PROJECT, TEST_PROJECT_VERSION ) ) ).isNotNull( ).isEmpty( );
305 getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ).isNull( );
306 assertThat( getRepository( ).getProject( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) ).isNull( );
308 assertThat( getRepository( ).getRootNamespaces( session, TEST_REPO_ID ) ).isNotNull( ).isEmpty( );
312 ArtifactMetadata metadata = createArtifact( );
314 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
317 Collection<ArtifactMetadata> artifacts =
318 getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
319 //assertEquals( Collections.singletonList( metadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
320 assertThat( artifacts ).containsExactly( metadata );
321 // test that namespace, project and project version is also constructed
323 assertThat( getRepository( ).getRootNamespaces( session, TEST_REPO_ID ) ).isNotNull( ).isNotEmpty( ).contains(
324 TEST_NAMESPACE ).hasSize( 1 );
326 ProjectMetadata projectMetadata = getRepository( ).getProject( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
327 assertEquals( TEST_PROJECT, projectMetadata.getId( ) );
328 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace( ) );
330 ProjectVersionMetadata projectVersionMetadata =
331 getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
332 assertEquals( TEST_PROJECT_VERSION, projectVersionMetadata.getId( ) );
338 public void testUpdateProjectVersionMetadataWithNoOtherArchives( )
341 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
344 ProjectVersionMetadata metadata = new ProjectVersionMetadata( );
345 metadata.setId( TEST_PROJECT_VERSION );
346 MailingList mailingList = new MailingList( );
347 mailingList.setName( "Foo List" );
348 mailingList.setOtherArchives( Collections.<String>emptyList( ) );
349 metadata.setMailingLists( Arrays.asList( mailingList ) );
350 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
352 metadata = getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
353 assertEquals( TEST_PROJECT_VERSION, metadata.getId( ) );
355 List<MailingList> mailingLists = metadata.getMailingLists( );
357 assertThat( mailingLists ).isNotNull( ).isNotEmpty( ).hasSize( 1 );
359 mailingList = metadata.getMailingLists( ).get( 0 );
360 assertEquals( "Foo List", mailingList.getName( ) );
362 List<String> others = mailingList.getOtherArchives( );
363 assertThat( others ).isNotNull( ).isEmpty( );
368 public void testUpdateProjectVersionMetadataWithAllElements( )
372 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
375 ProjectVersionMetadata metadata = new ProjectVersionMetadata( );
376 metadata.setId( TEST_PROJECT_VERSION );
378 metadata.setName( "project name" );
379 metadata.setDescription( "project description" );
380 metadata.setUrl( "the url" );
382 MailingList mailingList = new MailingList( );
383 mailingList.setName( "Foo List" );
384 mailingList.setUnsubscribeAddress( "UnsubscribeAddress" );
385 mailingList.setSubscribeAddress( "SubscribeAddress" );
386 mailingList.setPostAddress( "PostAddress" );
387 mailingList.setMainArchiveUrl( "MainArchiveUrl" );
388 mailingList.setOtherArchives( Arrays.asList( "other archive" ) );
389 metadata.setMailingLists( Arrays.asList( mailingList ) );
391 Scm scm = new Scm( );
392 scm.setConnection( "connection" );
393 scm.setDeveloperConnection( "dev conn" );
395 metadata.setScm( scm );
397 CiManagement ci = new CiManagement( );
398 ci.setSystem( "system" );
399 ci.setUrl( "ci url" );
400 metadata.setCiManagement( ci );
402 IssueManagement tracker = new IssueManagement( );
403 tracker.setSystem( "system" );
404 tracker.setUrl( "issue tracker url" );
405 metadata.setIssueManagement( tracker );
407 metadata.setOrganization( TEST_ORGANIZATION );
409 License l = new License( );
410 l.setName( "license name" );
411 l.setUrl( "license url" );
412 metadata.addLicense( l );
414 Dependency d = new Dependency( );
415 d.setArtifactId( "artifactId" );
416 d.setClassifier( "classifier" );
417 d.setGroupId( "groupId" );
418 d.setScope( "scope" );
419 d.setSystemPath( "system path" );
421 d.setVersion( "version" );
422 d.setOptional( true );
423 metadata.addDependency( d );
425 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
427 metadata = getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
428 assertEquals( TEST_PROJECT_VERSION, metadata.getId( ) );
429 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion( ) );
430 assertEquals( "project name", metadata.getName( ) );
431 assertEquals( "project description", metadata.getDescription( ) );
432 assertEquals( "the url", metadata.getUrl( ) );
434 assertEquals( "connection", metadata.getScm( ).getConnection( ) );
435 assertEquals( "dev conn", metadata.getScm( ).getDeveloperConnection( ) );
436 assertEquals( "url", metadata.getScm( ).getUrl( ) );
438 assertEquals( "system", metadata.getCiManagement( ).getSystem( ) );
439 assertEquals( "ci url", metadata.getCiManagement( ).getUrl( ) );
441 assertEquals( "system", metadata.getIssueManagement( ).getSystem( ) );
442 assertEquals( "issue tracker url", metadata.getIssueManagement( ).getUrl( ) );
444 assertEquals( TEST_ORGANIZATION.getName( ), metadata.getOrganization( ).getName( ) );
445 assertEquals( TEST_ORGANIZATION.getUrl( ), metadata.getOrganization( ).getUrl( ) );
447 assertEquals( 1, metadata.getMailingLists( ).size( ) );
448 MailingList retrievedMailingList = metadata.getMailingLists( ).get( 0 );
449 assertEquals( mailingList.getName( ), retrievedMailingList.getName( ) );
450 assertEquals( mailingList.getMainArchiveUrl( ), retrievedMailingList.getMainArchiveUrl( ) );
451 assertEquals( mailingList.getPostAddress( ), retrievedMailingList.getPostAddress( ) );
452 assertEquals( mailingList.getSubscribeAddress( ), retrievedMailingList.getSubscribeAddress( ) );
453 assertEquals( mailingList.getUnsubscribeAddress( ), retrievedMailingList.getUnsubscribeAddress( ) );
454 assertThat( retrievedMailingList.getOtherArchives( ) ) //
458 .contains( "other archive" );
460 assertEquals( 1, metadata.getLicenses( ).size( ) );
461 l = metadata.getLicenses( ).get( 0 );
462 assertEquals( "license name", l.getName( ) );
463 assertEquals( "license url", l.getUrl( ) );
465 assertEquals( 1, metadata.getDependencies( ).size( ) );
466 d = metadata.getDependencies( ).get( 0 );
467 assertEquals( "artifactId", d.getArtifactId( ) );
468 assertEquals( "classifier", d.getClassifier( ) );
469 assertEquals( "groupId", d.getGroupId( ) );
470 assertEquals( "scope", d.getScope( ) );
471 assertEquals( "system path", d.getSystemPath( ) );
472 assertEquals( "type", d.getType( ) );
473 assertEquals( "version", d.getVersion( ) );
474 assertTrue( d.isOptional( ) );
479 public void testUpdateProjectVersionMetadataIncomplete( )
482 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
485 ProjectVersionMetadata metadata = new ProjectVersionMetadata( );
486 metadata.setId( TEST_PROJECT_VERSION );
487 metadata.setIncomplete( true );
488 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
491 ProjectVersionMetadata metadata1 = getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
492 assertEquals( true, metadata1.isIncomplete( ) );
493 assertNull( metadata1.getCiManagement( ) );
494 assertNull( metadata1.getScm( ) );
495 assertNull( metadata1.getIssueManagement( ) );
496 assertNull( metadata1.getOrganization( ) );
497 assertTrue( metadata1.getDescription( )==null || "".equals(metadata1.getDescription()) );
498 assertTrue( metadata1.getName( )==null || "".equals(metadata1.getName()) );
499 assertEquals( TEST_PROJECT_VERSION, metadata1.getId( ) );
500 assertEquals( TEST_PROJECT_VERSION, metadata1.getVersion( ) );
501 assertTrue( metadata1.getMailingLists( ).isEmpty( ) );
502 assertTrue( metadata1.getLicenses( ).isEmpty( ) );
503 assertTrue( metadata1.getDependencies( ).isEmpty( ) );
509 public void testUpdateProjectVersionMetadataWithExistingFacets( )
512 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
515 ProjectVersionMetadata metadata = new ProjectVersionMetadata( );
516 metadata.setId( TEST_PROJECT_VERSION );
517 MetadataFacet facet = new TestMetadataFacet( "baz" );
518 metadata.addFacet( facet );
519 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
523 ProjectVersionMetadata metadata1 = getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
524 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata1.getFacetIds( ) );
527 metadata = new ProjectVersionMetadata( );
528 metadata.setId( TEST_PROJECT_VERSION );
529 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
533 ProjectVersionMetadata metadata2 = getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
534 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata2.getFacetIds( ) );
535 TestMetadataFacet testFacet = (TestMetadataFacet) metadata2.getFacet( TEST_FACET_ID );
536 assertEquals( "baz", testFacet.getValue( ) );
542 public void testUpdateProjectVersionMetadataWithNoExistingFacets( )
545 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
548 ProjectVersionMetadata metadata = new ProjectVersionMetadata( );
549 metadata.setId( TEST_PROJECT_VERSION );
550 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
554 ProjectVersionMetadata metadata1 = getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
556 assertThat( metadata1.getFacetIds( ) ).isNotNull( ).isEmpty( );
559 ProjectVersionMetadata metadata1 = getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
560 metadata = new ProjectVersionMetadata( );
561 metadata.setId( TEST_PROJECT_VERSION );
562 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
565 ProjectVersionMetadata metadata2 = getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
566 assertThat( metadata2.getFacetIds( ) ).isNotNull( ).isEmpty( );
572 public void testUpdateProjectVersionMetadataWithExistingFacetsFacetPropertyWasRemoved( )
575 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
578 ProjectVersionMetadata metadata = new ProjectVersionMetadata( );
579 metadata.setId( TEST_PROJECT_VERSION );
581 Map<String, String> additionalProps = new HashMap<>( );
582 additionalProps.put( "deleteKey", "deleteValue" );
584 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
585 metadata.addFacet( facet );
586 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
591 ProjectVersionMetadata metad = getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
593 assertThat( metad.getFacetIds( ) ).isNotNull( ).isNotEmpty( ).hasSize( 1 ).contains( TEST_FACET_ID );
598 ProjectVersionMetadata metad = getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
599 TestMetadataFacet testFacet = (TestMetadataFacet) metad.getFacet( TEST_FACET_ID );
600 Map<String, String> facetProperties = testFacet.toProperties( );
602 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
604 facetProperties.remove( "deleteKey" );
606 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue( ), facetProperties );
607 metadata.addFacet( newTestFacet );
609 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
613 ProjectVersionMetadata metad2 = getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
615 assertThat( metad2.getFacetIds( ) ).isNotNull( ).isNotEmpty( ).hasSize( 1 ).contains( TEST_FACET_ID );
616 TestMetadataFacet testFacet2 = (TestMetadataFacet) metad2.getFacet( TEST_FACET_ID );
617 assertFalse( testFacet2.toProperties( ).containsKey( "deleteKey" ) );
623 public void testGetArtifactsDoesntReturnProjectVersionMetadataFacets( )
626 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
629 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata( );
630 versionMetadata.setId( TEST_PROJECT_VERSION );
632 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz" );
633 versionMetadata.addFacet( facet );
634 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, versionMetadata );
635 ArtifactMetadata artifactMetadata = createArtifact( );
636 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifactMetadata );
642 Collection<ArtifactMetadata> artifacts =
643 getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
644 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<>( artifacts ) );
646 artifacts = getRepository( ).getArtifacts( session, TEST_REPO_ID );
647 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<>( artifacts ) );
649 artifacts = getRepository( ).getArtifactsByChecksum( session, TEST_REPO_ID, TEST_SHA1 );
650 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<>( artifacts ) );
652 artifacts = getRepository( ).getArtifactsByChecksum( session, TEST_REPO_ID, TEST_MD5 );
653 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<>( artifacts ) );
655 artifacts = getRepository( ).getArtifactsByDateRange( session, TEST_REPO_ID, null, null );
656 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<>( artifacts ) );
662 public void testUpdateArtifactMetadataWithExistingFacetsFacetPropertyWasRemoved( )
665 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
668 ArtifactMetadata metadata = createArtifact( );
670 Map<String, String> additionalProps = new HashMap<>( );
671 additionalProps.put( "deleteKey", "deleteValue" );
673 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
674 metadata.addFacet( facet );
675 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
678 Collection<ArtifactMetadata> artifacts =
679 getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
681 assertThat( artifacts ).isNotNull( ).isNotEmpty( ).hasSize( 1 );
684 Collection<ArtifactMetadata> artifacts =
685 getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
686 metadata = artifacts.iterator( ).next( );
688 Collection<String> ids = metadata.getFacetIds( );
689 assertThat( ids ).isNotNull( ).isNotEmpty( ).hasSize( 1 ).contains( TEST_FACET_ID );
691 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
692 Map<String, String> facetProperties = testFacet.toProperties( );
694 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
696 facetProperties.remove( "deleteKey" );
698 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue( ), facetProperties );
699 metadata.addFacet( newTestFacet );
701 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
705 Collection<ArtifactMetadata> artifacts1 = getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
707 assertThat( artifacts1 ).isNotNull( ).isNotEmpty( ).hasSize( 1 );
709 Collection<ArtifactMetadata> artifacts1 = getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
710 metadata = artifacts.iterator( ).next( );
712 ids = metadata.getFacetIds( );
713 assertThat( ids ).isNotNull( ).isNotEmpty( ).hasSize( 1 ).contains( TEST_FACET_ID );
715 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
717 Map<String, String> props = testFacet.toProperties( );
718 assertThat( props ).isNotNull( ).doesNotContainKey( "deleteKey" );
723 public void testUpdateArtifactMetadataWithExistingFacets( )
726 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
729 ArtifactMetadata metadata = createArtifact( );
730 MetadataFacet facet = new TestMetadataFacet( "baz" );
731 metadata.addFacet( facet );
732 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
734 metadata = getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE,
735 TEST_PROJECT, TEST_PROJECT_VERSION ).iterator( ).next( );
736 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds( ) );
738 metadata = createArtifact( );
739 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
741 metadata = getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE,
742 TEST_PROJECT, TEST_PROJECT_VERSION ).iterator( ).next( );
743 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds( ) );
744 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
745 assertEquals( "baz", testFacet.getValue( ) );
750 public void testUpdateArtifactMetadataWithNoExistingFacets( )
753 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
755 ArtifactMetadata metadata = createArtifact( );
756 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
760 ArtifactMetadata metadata2 = getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE,
761 TEST_PROJECT, TEST_PROJECT_VERSION ).iterator( ).next( );
762 assertEquals( Collections.<String>emptyList( ), new ArrayList<String>( metadata2.getFacetIds( ) ) );
766 metadata = getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE,
767 TEST_PROJECT, TEST_PROJECT_VERSION ).iterator( ).next( );
768 metadata = createArtifact( );
769 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
773 ArtifactMetadata metadata3 = getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE,
774 TEST_PROJECT, TEST_PROJECT_VERSION ).iterator( ).next( );
775 assertEquals( Collections.<String>emptyList( ), new ArrayList<String>( metadata3.getFacetIds( ) ) );
781 public void testGetMetadataFacet( )
784 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
786 getRepository( ).addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
788 TestMetadataFacet test =
789 (TestMetadataFacet) getRepository( ).getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
791 assertEquals( new TestMetadataFacet( TEST_VALUE ), test );
797 public void testGetMetadataFacetWhenEmpty( )
800 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
802 tryAssert( ( ) -> assertNull( getRepository( ).getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) ) );
807 public void testGetMetadataFacetWhenUnknownName( )
810 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
812 getRepository( ).addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
814 tryAssert( ( ) -> assertNull( getRepository( ).getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, UNKNOWN ) ) );
819 public void testGetMetadataFacetWhenDefaultValue( )
822 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
824 getRepository( ).addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet( null ) );
827 MetadataFacet metadataFacet = getRepository( ).getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
829 assertEquals( new TestMetadataFacet( TEST_METADATA_VALUE ), metadataFacet );
836 public void testGetMetadataFacetWhenUnknownFacetId( )
839 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
841 assertNull( getRepository( ).getMetadataFacet( session, TEST_REPO_ID, UNKNOWN, TEST_NAME ) );
847 public void testGetMetadataFacets( )
850 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
852 getRepository( ).addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
854 assertEquals( Collections.singletonList( TEST_NAME ),
855 getRepository( ).getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID ) );
861 public void testGetMetadataFacetsWhenEmpty( )
865 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
869 List<String> facets = getRepository( ).getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID );
870 assertTrue( facets.isEmpty( ) );
876 public void testRemoveFacets( )
879 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
881 getRepository( ).addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
883 List<String> facets = getRepository( ).getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID );
884 assertFalse( facets.isEmpty( ) );
886 getRepository( ).removeMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID );
888 facets = getRepository( ).getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID );
889 assertTrue( facets.isEmpty( ) );
895 public void testRemoveFacetsWhenEmpty( )
898 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
900 List<String> facets = getRepository( ).getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID );
901 assertTrue( facets.isEmpty( ) );
903 getRepository( ).removeMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID );
906 List<String> facets1 = getRepository( ).getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID );
907 assertTrue( facets1.isEmpty( ) );
913 public void testRemoveFacetsWhenUnknown( )
916 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
918 // testing no exception
919 getRepository( ).removeMetadataFacets( session, TEST_REPO_ID, UNKNOWN );
925 public void testRemoveFacetWhenUnknown( )
928 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
930 // testing no exception
931 getRepository( ).removeMetadataFacet( session, TEST_REPO_ID, UNKNOWN, TEST_NAME );
937 public void testRemoveFacet( )
940 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
942 TestMetadataFacet metadataFacet = new TestMetadataFacet( TEST_VALUE );
943 getRepository( ).addMetadataFacet( session, TEST_REPO_ID, metadataFacet );
945 assertEquals( metadataFacet, getRepository( ).getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
946 List<String> facets = getRepository( ).getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID );
947 assertFalse( facets.isEmpty( ) );
949 getRepository( ).removeMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
951 assertNull( getRepository( ).getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
952 facets = getRepository( ).getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID );
953 assertTrue( facets.isEmpty( ) );
959 public void testRemoveFacetWhenEmpty( )
962 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
966 List<String> facets = getRepository( ).getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID );
967 assertThat( facets ).isNotNull( ).isEmpty( );
968 assertThat( getRepository( ).getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) ).isNull( );
971 getRepository( ).removeMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
975 List<String> facets2 = getRepository( ).getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID );
976 assertThat( facets2 ).isNotNull( ).isEmpty( );
977 assertThat( getRepository( ).getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) ).isNull( );
984 public void hasMetadataFacetStart( )
987 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
989 assertFalse( getRepository( ).hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName( ) ) );
996 public void hasMetadataFacet( )
1000 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1002 assertFalse( getRepository( ).hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName( ) ) );
1004 Calendar cal = Calendar.getInstance( );
1006 getRepository( ).addMetadataFacet( session, TEST_REPO_ID, new KindOfRepositoryStatistics( "first", cal.getTime( ) ) );
1008 assertTrue( getRepository( ).hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName( ) ) );
1010 cal.add( Calendar.MINUTE, 2 );
1012 getRepository( ).addMetadataFacet( session, TEST_REPO_ID, new KindOfRepositoryStatistics( "second", cal.getTime( ) ) );
1014 cal.add( Calendar.MINUTE, 2 );
1016 getRepository( ).addMetadataFacet( session, TEST_REPO_ID, new KindOfRepositoryStatistics( "third", cal.getTime( ) ) );
1018 List<String> facets = getRepository( ).getMetadataFacets( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName( ) );
1020 assertThat( facets ).isNotNull( ).isNotEmpty( ).hasSize( 3 );
1022 assertTrue( getRepository( ).hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName( ) ) );
1024 getRepository( ).removeMetadataFacets( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName( ) );
1026 assertFalse( getRepository( ).hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName( ) ) );
1028 facets = getRepository( ).getMetadataFacets( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName( ) );
1030 assertThat( facets ).isNotNull( ).isEmpty( );
1038 public void testGetArtifacts( )
1041 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1043 ArtifactMetadata artifact1 = createArtifact( );
1044 ArtifactMetadata artifact2 = createArtifact( "pom" );
1045 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
1046 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
1049 Collection<ArtifactMetadata> artifacts =
1050 getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1051 ArrayList<ArtifactMetadata> actual = new ArrayList<>( artifacts );
1052 Collections.sort( actual, ( o1, o2 ) -> o1.getId( ).compareTo( o2.getId( ) ) );
1053 assertEquals( Arrays.asList( artifact1, artifact2 ), actual );
1060 public void testGetArtifactVersions( )
1063 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1065 ArtifactMetadata artifact1 = createArtifact( );
1066 String version1 = "1.0-20091212.012345-1";
1067 artifact1.setId( artifact1.getProject( ) + "-" + version1 + ".jar" );
1068 artifact1.setVersion( version1 );
1069 ArtifactMetadata artifact2 = createArtifact( );
1070 String version2 = "1.0-20091212.123456-2";
1071 artifact2.setId( artifact2.getProject( ) + "-" + version2 + ".jar" );
1072 artifact2.setVersion( version2 );
1073 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
1074 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
1077 Collection<String> versions =
1078 getRepository( ).getArtifactVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1080 assertThat( versions ).isNotNull( ).isNotEmpty( ).contains( version1, version2 );
1087 public void testGetArtifactVersionsMultipleArtifactsSingleVersion( )
1090 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1092 ArtifactMetadata artifact1 = createArtifact( );
1093 artifact1.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" );
1094 ArtifactMetadata artifact2 = createArtifact( );
1095 artifact2.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "-sources.jar" );
1096 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
1097 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
1100 Collection<String> versions =
1101 getRepository( ).getArtifactVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1103 assertThat( versions ).isNotNull( ).isNotEmpty( ).hasSize( 1 ).containsExactly( TEST_PROJECT_VERSION );
1111 public void testGetArtifactsByDateRangeOpen( )
1114 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1116 ArtifactMetadata artifact = createArtifact( );
1117 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1121 List<ArtifactMetadata> artifacts = getRepository( ).getArtifactsByDateRange( session, TEST_REPO_ID, null, null );
1123 assertEquals( Collections.singletonList( artifact ), artifacts );
1129 public void testGetArtifactsByDateRangeSparseNamespace( )
1132 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1134 String namespace = "org.apache.archiva";
1135 ArtifactMetadata artifact = createArtifact( );
1136 artifact.setNamespace( namespace );
1137 getRepository( ).updateArtifact( session, TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1141 List<ArtifactMetadata> artifacts = getRepository( ).getArtifactsByDateRange( session, TEST_REPO_ID, null, null );
1143 tryAssert( ( ) -> assertEquals( Collections.singletonList( artifact ), artifacts ) );
1149 public void testGetArtifactsByDateRangeLowerBound( )
1152 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1154 ArtifactMetadata artifact = createArtifact( );
1155 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1158 Date date = new Date( artifact.getWhenGathered( ).getTime( ) - 10000 );
1161 List<ArtifactMetadata> artifacts = getRepository( ).getArtifactsByDateRange( session, TEST_REPO_ID, date, null );
1163 assertEquals( Collections.singletonList( artifact ), artifacts );
1169 public void testGetArtifactsByDateRangeLowerBoundOutOfRange( )
1172 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1174 ArtifactMetadata artifact = createArtifact( );
1175 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1177 Date date = new Date( artifact.getWhenGathered( ).getTime( ) + 10000 );
1180 List<ArtifactMetadata> artifacts = getRepository( ).getArtifactsByDateRange( session, TEST_REPO_ID, date, null );
1182 assertThat( artifacts ).isNotNull( ).isEmpty( );
1188 public void testGetArtifactsByDateRangeLowerAndUpperBound( )
1191 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1193 ArtifactMetadata artifact = createArtifact( );
1194 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1197 Date lower = new Date( artifact.getWhenGathered( ).getTime( ) - 10000 );
1198 Date upper = new Date( artifact.getWhenGathered( ).getTime( ) + 10000 );
1201 List<ArtifactMetadata> artifacts = getRepository( ).getArtifactsByDateRange( session, TEST_REPO_ID, lower, upper );
1203 assertEquals( Collections.singletonList( artifact ), artifacts );
1209 public void testGetArtifactsByDateRangeUpperBound( )
1212 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1214 ArtifactMetadata artifact = createArtifact( );
1215 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1218 Date upper = new Date( artifact.getWhenGathered( ).getTime( ) + 10000 );
1221 List<ArtifactMetadata> artifacts = getRepository( ).getArtifactsByDateRange( session, TEST_REPO_ID, null, upper );
1223 assertEquals( Collections.singletonList( artifact ), artifacts );
1229 public void testGetArtifactsByDateRangeUpperBoundOutOfRange( )
1232 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1234 ArtifactMetadata artifact = createArtifact( );
1235 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1238 Date upper = new Date( artifact.getWhenGathered( ).getTime( ) - 10000 );
1241 List<ArtifactMetadata> artifacts = getRepository( ).getArtifactsByDateRange( session, TEST_REPO_ID, null, upper );
1243 assertThat( artifacts ).isNotNull( ).isEmpty( );
1249 public void testGetArtifactsByRepoId( )
1252 ArtifactMetadata artifact;
1253 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1255 artifact = createArtifact( );
1256 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1259 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1262 session.refreshAndDiscard( );
1263 List<ArtifactMetadata> artifacts = getRepository( ).getArtifacts( session, TEST_REPO_ID );
1264 assertEquals( Collections.singletonList( artifact ), artifacts );
1271 public void testGetArtifactsByRepoIdMultipleCopies( )
1274 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1276 ArtifactMetadata artifact = createArtifact( );
1277 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1279 ArtifactMetadata secondArtifact = createArtifact( );
1280 secondArtifact.setRepositoryId( OTHER_REPO_ID );
1281 getRepository( ).updateArtifact( session, OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
1284 // test it restricts to the appropriate repository
1285 tryAssert( ( ) -> assertEquals( Collections.singletonList( artifact ), getRepository( ).getArtifacts( session, TEST_REPO_ID ) ) );
1286 tryAssert( ( ) -> assertEquals( Collections.singletonList( secondArtifact ), getRepository( ).getArtifacts( session, OTHER_REPO_ID ) ) );
1293 public void testGetArtifactsByDateRangeMultipleCopies( )
1296 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1298 ArtifactMetadata artifact = createArtifact( );
1299 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1301 ArtifactMetadata secondArtifact = createArtifact( );
1302 secondArtifact.setRepositoryId( OTHER_REPO_ID );
1303 getRepository( ).updateArtifact( session, OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
1307 // test it restricts to the appropriate repository
1308 assertEquals( Collections.singletonList( artifact ),
1309 getRepository( ).getArtifactsByDateRange( session, TEST_REPO_ID, null, null ) );
1312 assertEquals( Collections.singletonList( secondArtifact ),
1313 getRepository( ).getArtifactsByDateRange( session, OTHER_REPO_ID, null, null ) );
1319 public void testGetArtifactsByChecksumMultipleCopies( )
1322 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1324 ArtifactMetadata artifact = createArtifact( );
1325 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1327 ArtifactMetadata secondArtifact = createArtifact( );
1328 secondArtifact.setRepositoryId( OTHER_REPO_ID );
1329 getRepository( ).updateArtifact( session, OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
1333 // test it restricts to the appropriate repository
1334 assertEquals( Collections.singletonList( artifact ),
1335 new ArrayList<>( getRepository( ).getArtifactsByChecksum( session, TEST_REPO_ID, TEST_SHA1 ) ) );
1338 assertEquals( Collections.singletonList( secondArtifact ), new ArrayList<>(
1339 getRepository( ).getArtifactsByChecksum( session, OTHER_REPO_ID, TEST_SHA1 ) ) );
1342 assertEquals( Collections.singletonList( artifact ),
1343 new ArrayList<>( getRepository( ).getArtifactsByChecksum( session, TEST_REPO_ID, TEST_MD5 ) ) );
1346 assertEquals( Collections.singletonList( secondArtifact ),
1347 new ArrayList<>( getRepository( ).getArtifactsByChecksum( session, OTHER_REPO_ID, TEST_MD5 ) ) );
1354 public void testGetNamespacesWithSparseDepth( )
1357 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1359 getRepository( ).updateNamespace( session, TEST_REPO_ID, "org.apache.maven.shared" );
1363 Collection<String> namespaces = getRepository( ).getRootNamespaces( session, TEST_REPO_ID );
1365 assertThat( namespaces ).isNotNull( ).isNotEmpty( ).hasSize( 1 ).contains( "org" );
1367 namespaces = getRepository( ).getNamespaces( session, TEST_REPO_ID, "org" );
1368 assertThat( namespaces ).isNotNull( ).isNotEmpty( ).hasSize( 1 ).contains( "apache" );
1370 namespaces = getRepository( ).getNamespaces( session, TEST_REPO_ID, "org.apache" );
1371 assertThat( namespaces ).isNotNull( ).isNotEmpty( ).hasSize( 1 ).contains( "maven" );
1373 namespaces = getRepository( ).getNamespaces( session, TEST_REPO_ID, "org.apache.maven" );
1374 assertThat( namespaces ).isNotNull( ).isNotEmpty( ).hasSize( 1 ).contains( "shared" );
1381 public void testGetNamespacesWithProjectsPresent( )
1384 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1386 String namespace = "org.apache.maven.shared";
1387 getRepository( ).updateNamespace( session, TEST_REPO_ID, namespace );
1389 ProjectVersionMetadata metadata = new ProjectVersionMetadata( );
1390 metadata.setId( TEST_PROJECT_VERSION );
1391 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, namespace, TEST_PROJECT, metadata );
1393 Collection<String> namespaces = getRepository( ).getNamespaces( session, TEST_REPO_ID, namespace );
1395 assertThat( namespaces ).isNotNull( ).isEmpty( );
1402 public void testGetProjectsWithOtherNamespacesPresent( )
1405 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1407 ProjectMetadata projectMetadata = new ProjectMetadata( );
1408 projectMetadata.setId( TEST_PROJECT );
1409 projectMetadata.setNamespace( "org.apache.maven" );
1410 getRepository( ).updateProject( session, TEST_REPO_ID, projectMetadata );
1412 getRepository( ).updateNamespace( session, TEST_REPO_ID, "org.apache.maven.shared" );
1414 Collection<String> projects = getRepository( ).getProjects( session, TEST_REPO_ID, "org.apache.maven" );
1416 assertThat( projects ).isNotNull( ).isNotEmpty( ).hasSize( 1 ).contains( TEST_PROJECT );
1422 public void testGetProjectVersionsWithOtherNamespacesPresent( )
1425 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1427 // an unusual case but technically possible where a project namespace matches another project's name
1429 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata( );
1430 versionMetadata.setId( TEST_PROJECT_VERSION );
1431 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, "org.apache.maven", TEST_PROJECT, versionMetadata );
1433 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT,
1434 "other-project", versionMetadata );
1436 Collection<String> versions =
1437 getRepository( ).getProjectVersions( session, TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, "other-project" );
1438 assertThat( versions ).isNotNull( ).isNotEmpty( ).contains( TEST_PROJECT_VERSION );
1440 versions = getRepository( ).getProjectVersions( session, TEST_REPO_ID, "org.apache.maven", TEST_PROJECT );
1442 assertThat( versions ).isNotNull( ).isNotEmpty( ).contains( TEST_PROJECT_VERSION );
1448 public void testGetArtifactsByChecksumSingleResultMd5( )
1451 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1453 ArtifactMetadata artifact = createArtifact( );
1454 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1457 assertEquals( Collections.singletonList( artifact ),
1458 new ArrayList<>( getRepository( ).getArtifactsByChecksum( session, TEST_REPO_ID, TEST_MD5 ) ) );
1464 public void testGetArtifactsByChecksumSingleResultSha1( )
1467 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1469 ArtifactMetadata artifact = createArtifact( );
1470 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1473 assertEquals( Collections.singletonList( artifact ),
1474 new ArrayList<>( getRepository( ).getArtifactsByChecksum( session, TEST_REPO_ID, TEST_SHA1 ) ) );
1480 public void testGetArtifactsByChecksumDeepNamespace( )
1483 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1485 ArtifactMetadata artifact = createArtifact( );
1486 String namespace = "multi.level.ns";
1487 artifact.setNamespace( namespace );
1488 getRepository( ).updateArtifact( session, TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1491 tryAssert( ( ) -> assertEquals( Collections.singletonList( artifact ),
1492 new ArrayList<>( getRepository( ).getArtifactsByChecksum( session, TEST_REPO_ID, TEST_SHA1 ) ) ) );
1493 tryAssert( ( ) -> assertEquals( Collections.singletonList( artifact ),
1494 new ArrayList<>( getRepository( ).getArtifactsByChecksum( session, TEST_REPO_ID, TEST_MD5 ) ) ) );
1500 public void testGetArtifactsByChecksumMultipleResult( )
1504 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1506 ArtifactMetadata artifact1 = createArtifact( );
1507 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
1509 String newProjectId = "another-project";
1510 ArtifactMetadata artifact2 = createArtifact( );
1511 artifact2.setProject( newProjectId );
1512 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 );
1516 List<ArtifactMetadata> artifacts =
1517 new ArrayList<>( getRepository( ).getArtifactsByChecksum( session, TEST_REPO_ID, TEST_SHA1 ) );
1518 Collections.sort( artifacts, new ArtifactMetadataComparator( ) );
1519 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1523 ArrayList<ArtifactMetadata> artifacts = new ArrayList<>( getRepository( ).getArtifactsByChecksum( session, TEST_REPO_ID, TEST_MD5 ) );
1524 Collections.sort( artifacts, new ArtifactMetadataComparator( ) );
1525 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1531 public void testGetArtifactsByChecksumNoResult( )
1534 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1536 ArtifactMetadata artifact = createArtifact( );
1537 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1540 Collection<ArtifactMetadata> artifactsByChecksum =
1541 getRepository( ).getArtifactsByChecksum( session, TEST_REPO_ID, "not checksum" );
1543 assertThat( artifactsByChecksum ).isNotNull( ).isEmpty( );
1550 public void testGetArtifactsByProjectVersionMetadata( )
1553 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1555 createArtifactWithGenericMetadataFacet( session, 10 );
1558 Collection<ArtifactMetadata> artifactsByMetadata =
1559 getRepository( ).getArtifactsByProjectVersionMetadata( session, TEST_METADATA_KEY, TEST_METADATA_VALUE, TEST_REPO_ID );
1561 assertThat( artifactsByMetadata ).hasSize( 1 );
1562 ArtifactMetadata artifactMetadata = artifactsByMetadata.iterator( ).next( );
1563 assertThat( artifactMetadata.getId( ) ).isEqualTo( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" );
1564 assertThat( artifactMetadata.getSha1( ) ).isEqualTo( TEST_SHA1 );
1565 assertThat( artifactMetadata.getRepositoryId( ) ).isEqualTo( TEST_REPO_ID );
1571 public void testGetArtifactsByProjectVersionMetadataNoRepository( )
1574 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1576 createArtifactWithGenericMetadataFacet( session );
1579 Collection<ArtifactMetadata> artifactsByMetadata =
1580 getRepository( ).getArtifactsByProjectVersionMetadata( session, TEST_METADATA_KEY, TEST_METADATA_VALUE, null );
1581 assertThat( artifactsByMetadata ).hasSize( 1 );
1582 assertThat( artifactsByMetadata.iterator( ).next( ).getRepositoryId( ) ).isNotNull( ).isNotEmpty( );
1588 public void testGetArtifactsByProjectVersionMetadataAllRepositories( )
1591 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1593 createArtifactWithGenericMetadataFacet( session );
1596 Collection<ArtifactMetadata> artifactsByMetadata =
1597 getRepository( ).getArtifactsByProjectVersionMetadata( session, TEST_METADATA_KEY, TEST_METADATA_VALUE, null );
1598 assertThat( artifactsByMetadata ).hasSize( 1 );
1604 public void testGetArtifactsByMetadataAllRepositories( )
1607 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1609 createArtifactWithMavenArtifactFacet( session );
1611 Collection<ArtifactMetadata> artifactsByMetadata =
1612 getRepository( ).getArtifactsByMetadata( session, "foo", TEST_METADATA_VALUE, null );
1613 assertThat( artifactsByMetadata ).hasSize( 1 );
1614 ArtifactMetadata artifactMetadata = artifactsByMetadata.iterator( ).next( );
1615 assertThat( artifactMetadata.getId( ) ).isEqualTo( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" );
1616 assertThat( artifactMetadata.getSha1( ) ).isEqualTo( TEST_SHA1 );
1617 assertThat( artifactMetadata.getRepositoryId( ) ).isEqualTo( TEST_REPO_ID );
1618 MetadataFacet facet = artifactMetadata.getFacet( TEST_FACET_ID );
1619 assertThat( facet ).isNotNull( );
1620 assertThat( facet.toProperties( ) ).isEqualTo( Collections.singletonMap( "foo", TEST_METADATA_VALUE ) );
1626 public void testGetArtifactsByPropertySingleResult( )
1629 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1631 createArtifactWithData( session );
1632 // only works on JCR implementation
1633 // Collection<ArtifactMetadata> artifactsByProperty = getRepository().getArtifactsByProperty( "org.name", TEST_ORGANIZATION.getName(), TEST_REPO_ID );
1636 Collection<ArtifactMetadata> artifactsByProperty = getRepository( ).getArtifactsByProperty( session, "url", TEST_URL, TEST_REPO_ID );
1637 assertThat( artifactsByProperty ).hasSize( 1 );
1638 ArtifactMetadata artifactMetadata = artifactsByProperty.iterator( ).next( );
1639 assertThat( artifactMetadata.getId( ) ).isEqualTo( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" );
1640 assertThat( artifactMetadata.getSha1( ) ).isEqualTo( TEST_SHA1 );
1641 assertThat( artifactMetadata.getRepositoryId( ) ).isEqualTo( TEST_REPO_ID );
1648 public void testDeleteRepository( )
1651 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1654 getRepository( ).updateNamespace( session, TEST_REPO_ID, TEST_NAMESPACE );
1656 ProjectMetadata project1 = new ProjectMetadata( );
1657 project1.setNamespace( TEST_NAMESPACE );
1658 project1.setId( "project1" );
1659 getRepository( ).updateProject( session, TEST_REPO_ID, project1 );
1660 ProjectMetadata project2 = new ProjectMetadata( );
1661 project2.setNamespace( TEST_NAMESPACE );
1662 project2.setId( "project2" );
1663 getRepository( ).updateProject( session, TEST_REPO_ID, project2 );
1665 ArtifactMetadata artifact1 = createArtifact( );
1666 artifact1.setProject( "project1" );
1667 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, "project1", TEST_PROJECT_VERSION, artifact1 );
1668 ArtifactMetadata artifact2 = createArtifact( );
1669 artifact2.setProject( "project2" );
1670 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, "project2", TEST_PROJECT_VERSION, artifact2 );
1673 List<ArtifactMetadata> expected = Arrays.asList( artifact1, artifact2 );
1674 Collections.sort( expected, new ArtifactMetadataComparator( ) );
1678 List<ArtifactMetadata> actual =
1679 new ArrayList<>( getRepository( ).getArtifactsByDateRange( session, TEST_REPO_ID, null, null ) );
1680 Collections.sort( actual, new ArtifactMetadataComparator( ) );
1681 assertEquals( expected, actual );
1684 getRepository( ).removeRepository( session, TEST_REPO_ID );
1687 assertTrue( getRepository( ).getArtifacts( session, TEST_REPO_ID ).isEmpty( ) );
1688 assertTrue( getRepository( ).getRootNamespaces( session, TEST_REPO_ID ).isEmpty( ) );
1695 public void testDeleteArtifact( )
1698 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1701 ArtifactMetadata artifact = createArtifact( );
1702 artifact.addFacet( new TestMetadataFacet( "value" ) );
1704 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1706 assertThat( getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE,
1707 TEST_PROJECT, TEST_PROJECT_VERSION ) ).containsExactly( artifact );
1709 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION_2_0, artifact );
1711 Collection<String> versions = getRepository( ).getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1713 log.info( "versions {}", versions );
1715 assertThat( versions ).isNotNull( ).isNotEmpty( ).hasSize( 2 ).contains( "1.0", "2.0" );
1717 getRepository( ).removeArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId( ) );
1719 versions = getRepository( ).getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1721 log.info( "versions {}", versions );
1723 assertThat( versions ).isNotNull( ).isNotEmpty( ).hasSize( 1 ).contains( "2.0" );
1725 assertThat( getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE,
1726 TEST_PROJECT, TEST_PROJECT_VERSION ) ).isNotNull( ).isEmpty( );
1728 assertThat( getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE,
1729 TEST_PROJECT, TEST_PROJECT_VERSION_2_0 ) ).isNotEmpty( ).hasSize( 1 );
1734 public void deleteArtifact( )
1737 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1739 ArtifactMetadata artifact = createArtifact( );
1740 artifact.addFacet( new TestMetadataFacet( "value" ) );
1742 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1744 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1747 Collection<ArtifactMetadata> artifacts =
1748 getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1750 assertEquals( Collections.singletonList( artifact ), new ArrayList<>( artifacts ) );
1754 getRepository( ).removeArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId( ) );
1757 Collection<ArtifactMetadata> artifacts = getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1759 assertThat( artifacts ).isNotNull( ).isEmpty( );
1766 public void deleteVersion( )
1769 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1771 ArtifactMetadata artifact = createArtifact( );
1772 artifact.addFacet( new TestMetadataFacet( "value" ) );
1774 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1776 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1780 Collection<String> versions = getRepository( ).getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1782 assertThat( versions ).isNotNull( ).isNotEmpty( ).hasSize( 1 );
1785 getRepository( ).removeProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1789 Collection<String> versions1 = getRepository( ).getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1791 assertThat( versions1 ).isNotNull( ).isEmpty( );
1797 public void deleteProject( )
1800 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1802 ArtifactMetadata artifact = createArtifact( );
1803 artifact.addFacet( new TestMetadataFacet( "value" ) );
1805 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1807 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1809 assertEquals( 1, getRepository( ).getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size( ) );
1811 getRepository( ).removeProject( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1813 Collection<String> versions = getRepository( ).getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1815 assertThat( versions ).isNotNull( ).isEmpty( );
1822 public void deleteSnapshotVersion( )
1825 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1828 ArtifactMetadata artifactOne = createArtifact( );
1829 artifactOne.setVersion( "2.0-20120618.214127-1" );
1830 artifactOne.setProjectVersion( "2.0-SNAPSHOT" );
1831 artifactOne.addFacet( new TestMetadataFacet( "value" ) );
1832 artifactOne.setId( TEST_PROJECT + "-" + "2.0-20120618.214127-1" + "." + "jar" );
1834 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactOne );
1836 ArtifactMetadata artifactTwo = createArtifact( );
1837 artifactTwo.setVersion( "2.0-20120618.214135-2" );
1838 artifactTwo.setProjectVersion( "2.0-SNAPSHOT" );
1839 artifactTwo.addFacet( new TestMetadataFacet( "value" ) );
1840 artifactTwo.setId( TEST_PROJECT + "-" + "2.0-20120618.214135-2" + "." + "jar" );
1842 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactTwo );
1844 Collection<ArtifactMetadata> artifactMetadatas =
1845 getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1847 assertThat( artifactMetadatas ).isNotNull( ).isNotEmpty( ).hasSize( 2 );
1849 log.info( "artifactMetadatas: {}", artifactMetadatas );
1851 getRepository( ).removeArtifact( session, artifactOne, "2.0-SNAPSHOT" );
1853 artifactMetadatas = getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1855 assertThat( artifactMetadatas ).isNotNull( ).isNotEmpty( ).hasSize( 1 );
1857 getRepository( ).removeArtifact( session, artifactTwo, "2.0-SNAPSHOT" );
1859 artifactMetadatas = getRepository( ).getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1861 assertThat( artifactMetadatas ).isNotNull( ).isEmpty( );
1867 public void testgetProjectReferences( )
1870 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1873 ProjectVersionMetadata metadata = new ProjectVersionMetadata( );
1874 metadata.setId( TEST_PROJECT_VERSION );
1876 metadata.setName( "project name" );
1877 metadata.setDescription( "project description" );
1878 metadata.setUrl( "the url" );
1880 Dependency d = new Dependency( );
1881 d.setArtifactId( "artifactId" );
1882 d.setClassifier( "classifier" );
1883 d.setGroupId( "groupId" );
1884 d.setScope( "scope" );
1885 d.setSystemPath( "system path" );
1886 d.setType( "type" );
1887 d.setVersion( "version" );
1888 d.setOptional( true );
1889 metadata.addDependency( d );
1891 d = new Dependency( );
1892 d.setArtifactId( "artifactId1" );
1893 d.setClassifier( "classifier" );
1894 d.setGroupId( "groupId" );
1895 d.setScope( "scope" );
1896 d.setSystemPath( "system path" );
1897 d.setType( "type" );
1898 d.setVersion( "version1" );
1899 d.setOptional( true );
1900 metadata.addDependency( d );
1902 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
1906 metadata = getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1907 final Dependency dd = d;
1912 Collection<ProjectVersionReference> references =
1913 getRepository( ).getProjectReferences( session, TEST_REPO_ID, dd.getGroupId( ), dd.getArtifactId( ), dd.getVersion( ) );
1914 log.info( "references: {}", references );
1915 assertThat( references ).isNotNull( ).hasSize( 1 ).contains(
1916 new ProjectVersionReference( ProjectVersionReference.ReferenceType.DEPENDENCY, TEST_PROJECT, TEST_NAMESPACE,
1917 TEST_PROJECT_VERSION ));
1925 public void testSearchArtifactsByKey( )
1928 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1930 createArtifactWithData( session );
1933 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1935 session.refreshAndDiscard( );
1937 Collection<ArtifactMetadata> artifactsByProperty = getRepository( ).searchArtifacts( session, TEST_REPO_ID, "url", TEST_URL, false );
1938 assertThat( artifactsByProperty ).isNotNull( ).isNotEmpty( );
1944 public void testSearchArtifactsByKeyExact( )
1947 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1949 createArtifactWithData( session );
1951 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1953 session.refreshAndDiscard( );
1955 Collection<ArtifactMetadata> artifactsByProperty = getRepository( ).searchArtifacts( session, TEST_REPO_ID, "url", TEST_URL, true );
1956 assertThat( artifactsByProperty ).describedAs( "Artifact search by url=%s must give a result.", TEST_URL ).isNotNull( ).isNotEmpty( );
1957 artifactsByProperty = getRepository( ).searchArtifacts( session, TEST_REPO_ID, "org.name", "pache", true );
1958 assertThat( artifactsByProperty ).describedAs( "Artifact search by text org.name='pache' must be empty" ).isNotNull( ).isEmpty( );
1964 public void testSearchArtifactsByFacetKey( )
1967 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1969 createArtifactWithGenericMetadataFacet( session );
1971 Collection<ArtifactMetadata> artifactsByProperty = getRepository( ).searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_KEY, TEST_METADATA_VALUE, false );
1972 assertThat( artifactsByProperty ).isNotNull( ).isNotEmpty( );
1979 public void testSearchArtifactsByFacetKeyAllRepos( )
1982 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1985 createArtifactWithGenericMetadataFacet( session );
1987 Collection<ArtifactMetadata> artifactsByProperty = getRepository( ).searchArtifacts( session, null, TEST_METADATA_KEY, TEST_METADATA_VALUE, false );
1988 assertThat( artifactsByProperty ).isNotNull( ).isNotEmpty( );
1994 public void testSearchArtifactsFullText( )
1997 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
1999 createArtifactWithGenericMetadataFacet( session );
2000 // only works in JCR
2001 // Collection<ArtifactMetadata> artifactsByProperty = getRepository().searchArtifacts( TEST_URL, TEST_REPO_ID, false );
2003 Collection<ArtifactMetadata> artifactsByProperty =
2004 getRepository( ).searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_VALUE, false );
2005 assertThat( artifactsByProperty ).isNotNull( ).isNotEmpty( );
2012 public void testSearchArtifactsFullTextExact( )
2015 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
2017 createArtifactWithGenericMetadataFacet( session );
2018 // only works in JCR
2019 // Collection<ArtifactMetadata> artifactsByProperty = getRepository().searchArtifacts( TEST_URL, TEST_REPO_ID, true );
2022 Collection<ArtifactMetadata> artifactsByProperty =
2023 getRepository( ).searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_VALUE, true );
2024 assertThat( artifactsByProperty ).isNotNull( ).isNotEmpty( );
2028 Collection<ArtifactMetadata> artifactsByProperty = getRepository( ).searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_VALUE.substring( 2 ), true );
2029 assertThat( artifactsByProperty ).isNotNull( ).isEmpty( );
2035 public void testSearchArtifactsFullTextByFacet( )
2038 try ( RepositorySession session = getSessionFactory( ).createSession( ) )
2040 createArtifactWithGenericMetadataFacet( session );
2042 Collection<ArtifactMetadata> artifactsByProperty = getRepository( ).searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_VALUE, false );
2043 assertThat( artifactsByProperty ).isNotNull( ).isNotEmpty( );
2049 private static ProjectMetadata createProject( )
2051 return createProject( TEST_NAMESPACE );
2054 private static ProjectMetadata createProject( String ns )
2056 ProjectMetadata project = new ProjectMetadata( );
2057 project.setId( TEST_PROJECT );
2058 project.setNamespace( ns );
2062 private void createArtifactWithGenericMetadataFacet( RepositorySession session )
2063 throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException
2065 createArtifactWithGenericMetadataFacet( session, 1 );
2068 private void createArtifactWithGenericMetadataFacet( RepositorySession session, int artifacts )
2069 throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException
2071 MetadataFacet metadataFacet = new GenericMetadataFacet( );
2072 Map<String, String> properties = new HashMap<>( );
2073 properties.put( TEST_METADATA_KEY, TEST_METADATA_VALUE );
2074 metadataFacet.fromProperties( properties );
2075 createArtifactWithFacet( session, artifacts, null, metadataFacet );
2078 private void createArtifactWithMavenArtifactFacet( RepositorySession session )
2079 throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException
2081 createArtifactWithMavenArtifactFacet( session, 1 );
2084 private void createArtifactWithMavenArtifactFacet( RepositorySession session, int artifacts )
2085 throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException
2087 TestMetadataFacet facet = new TestMetadataFacet( TEST_METADATA_VALUE );
2088 createArtifactWithFacet( session, artifacts, facet, null );
2091 private void createArtifactWithFacet( RepositorySession session, int artifacts, MetadataFacet artifactFacet,
2092 MetadataFacet projectVersionMetadataFacet )
2093 throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException
2095 for ( int i = 0; i < artifacts; i++ )
2097 ArtifactMetadata artifact = createArtifact( );
2098 if ( artifactFacet != null )
2100 artifact.addFacet( artifactFacet );
2102 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
2104 if ( projectVersionMetadataFacet != null )
2106 ProjectVersionMetadata metadata =
2107 getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
2108 metadata.addFacet( projectVersionMetadataFacet );
2109 metadata.setOrganization( TEST_ORGANIZATION );
2110 metadata.setUrl( TEST_URL );
2111 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
2116 protected void createArtifactWithData( RepositorySession session )
2117 throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException
2119 ArtifactMetadata artifact = createArtifact( );
2120 getRepository( ).updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
2121 ProjectVersionMetadata metadata =
2122 getRepository( ).getProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
2123 metadata.setOrganization( TEST_ORGANIZATION );
2124 metadata.setUrl( TEST_URL );
2126 getRepository( ).updateProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
2130 private static ArtifactMetadata createArtifact( )
2132 return createArtifact( "jar" );
2135 private static ArtifactMetadata createArtifact( String type )
2137 ArtifactMetadata artifact = new ArtifactMetadata( );
2138 artifact.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type );
2139 artifact.setWhenGathered( new Date( ) );
2140 artifact.setNamespace( TEST_NAMESPACE );
2141 artifact.setProject( TEST_PROJECT );
2142 artifact.setRepositoryId( TEST_REPO_ID );
2143 artifact.setFileLastModified( System.currentTimeMillis( ) );
2144 artifact.setVersion( TEST_PROJECT_VERSION );
2145 artifact.setProjectVersion( TEST_PROJECT_VERSION );
2146 artifact.setMd5( TEST_MD5 );
2147 artifact.setSha1( TEST_SHA1 );
2151 private static class ArtifactMetadataComparator
2152 implements Comparator<ArtifactMetadata>
2155 public final int compare( ArtifactMetadata a, ArtifactMetadata b )
2157 return a.getProject( ).compareTo( b.getProject( ) );
2161 private static class KindOfRepositoryStatistics
2162 implements MetadataFacet
2164 private String value;
2168 static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
2170 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
2172 private KindOfRepositoryStatistics( String value, Date date )
2179 public String getName( )
2181 return createNameFormat( ).format( date );
2184 private static SimpleDateFormat createNameFormat( )
2186 SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT );
2187 fmt.setTimeZone( UTC_TIME_ZONE );
2192 public String getFacetId( )
2194 return KindOfRepositoryStatistics.class.getName( );
2198 public Map<String, String> toProperties( )
2200 return Collections.emptyMap( );
2204 public void fromProperties( Map<String, String> properties )
2210 private static class TestMetadataFacet
2211 implements MetadataFacet
2213 private String testFacetId;
2215 private Map<String, String> additionalProps;
2217 private String value;
2219 private TestMetadataFacet( String value )
2222 testFacetId = TEST_FACET_ID;
2225 private TestMetadataFacet( String facetId, String value )
2228 testFacetId = facetId;
2231 private TestMetadataFacet( String facetId, String value, Map<String, String> additionalProps )
2233 this( facetId, value );
2234 this.additionalProps = additionalProps;
2238 public String getFacetId( )
2244 public String getName( )
2250 public Map<String, String> toProperties( )
2252 if ( value != null )
2254 if ( additionalProps == null )
2256 return Collections.singletonMap( "foo", value );
2260 Map<String, String> props = new HashMap<>( );
2261 props.put( "foo", value );
2263 for ( String key : additionalProps.keySet( ) )
2265 props.put( key, additionalProps.get( key ) );
2272 return Collections.emptyMap( );
2277 public void fromProperties( Map<String, String> properties )
2279 String value = properties.get( "foo" );
2280 if ( value != null )
2285 properties.remove( "foo" );
2287 if ( additionalProps == null )
2289 additionalProps = new HashMap<>( );
2292 for ( String key : properties.keySet( ) )
2294 additionalProps.put( key, properties.get( key ) );
2298 public String getValue( )
2304 public String toString( )
2306 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
2310 public boolean equals( Object o )
2316 if ( o == null || getClass( ) != o.getClass( ) )
2321 TestMetadataFacet that = (TestMetadataFacet) o;
2323 if ( value != null ? !value.equals( that.value ) : that.value != null )
2332 public int hashCode( )
2334 return value != null ? value.hashCode( ) : 0;