diff options
author | Martin Stockhammer <martin_s@apache.org> | 2019-08-06 22:06:57 +0200 |
---|---|---|
committer | Martin Stockhammer <martin_s@apache.org> | 2019-08-06 22:06:57 +0200 |
commit | 21d5cf53fe45111fdb030b307715780703032de7 (patch) | |
tree | 8331f135e7349cad9996a9cd222d7fc401877fd2 /archiva-modules/metadata/metadata-repository-api | |
parent | eca0097d857aa4739a6387c67d22a9a355a46fa1 (diff) | |
download | archiva-21d5cf53fe45111fdb030b307715780703032de7.tar.gz archiva-21d5cf53fe45111fdb030b307715780703032de7.zip |
Refactoring repository metadata session handling
Diffstat (limited to 'archiva-modules/metadata/metadata-repository-api')
3 files changed, 1217 insertions, 1158 deletions
diff --git a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/RepositorySession.java b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/RepositorySession.java index 9ebde6a53..36cfcc3d3 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/RepositorySession.java +++ b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/RepositorySession.java @@ -69,18 +69,14 @@ public class RepositorySession return dirty; } - protected void setDirty(boolean value) { - this.dirty = value; - } - public void save() throws MetadataSessionException { - setDirty( false ); + this.dirty = false; } public void revert() throws MetadataSessionException { - setDirty( false ); + this.dirty = false; } /** @@ -127,4 +123,12 @@ public class RepositorySession { this.dirty = true; } + + public void refresh() throws MetadataSessionException { + + } + + public void refreshAndDiscard() throws MetadataSessionException { + + } } diff --git a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/RepositorySessionFactory.java b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/RepositorySessionFactory.java index 17e7c13d8..a4f3b8b55 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/RepositorySessionFactory.java +++ b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/RepositorySessionFactory.java @@ -28,4 +28,5 @@ public interface RepositorySessionFactory RepositorySession createSession() throws MetadataRepositoryException; void close(); + } diff --git a/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java b/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java index 59de1babb..78b5b4d83 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java +++ b/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java @@ -22,21 +22,9 @@ package org.apache.archiva.metadata.repository; import junit.framework.TestCase; import org.apache.archiva.metadata.generic.GenericMetadataFacet; import org.apache.archiva.metadata.generic.GenericMetadataFacetFactory; -import org.apache.archiva.metadata.model.ArtifactMetadata; -import org.apache.archiva.metadata.model.CiManagement; -import org.apache.archiva.metadata.model.Dependency; -import org.apache.archiva.metadata.model.IssueManagement; -import org.apache.archiva.metadata.model.License; -import org.apache.archiva.metadata.model.MailingList; -import org.apache.archiva.metadata.model.MetadataFacet; -import org.apache.archiva.metadata.model.MetadataFacetFactory; -import org.apache.archiva.metadata.model.Organization; -import org.apache.archiva.metadata.model.ProjectMetadata; -import org.apache.archiva.metadata.model.ProjectVersionMetadata; -import org.apache.archiva.metadata.model.ProjectVersionReference; -import org.apache.archiva.metadata.model.Scm; +import org.apache.archiva.metadata.model.*; +import org.apache.archiva.repository.Repository; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; -import org.assertj.core.util.Sets; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; @@ -44,35 +32,24 @@ import org.slf4j.LoggerFactory; import org.springframework.test.context.ContextConfiguration; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.HashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.TimeZone; +import java.util.*; import static org.assertj.core.api.Assertions.assertThat; -@RunWith( ArchivaSpringJUnit4ClassRunner.class ) -@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } ) +@RunWith(ArchivaSpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml"}) public abstract class AbstractMetadataRepositoryTest - extends TestCase -{ + extends TestCase { protected static final String OTHER_REPO_ID = "other-repo"; protected MetadataRepository repository; + protected RepositorySessionFactory sessionFactory; protected static final String TEST_REPO_ID = "test"; - protected static final String TEST_PROJECT = "projectId"; + protected static final String TEST_PROJECT = "myproject"; - protected static final String TEST_NAMESPACE = "namespace"; + protected static final String TEST_NAMESPACE = "mytest"; protected static final String TEST_PROJECT_VERSION = "1.0"; @@ -80,7 +57,7 @@ public abstract class AbstractMetadataRepositoryTest private static final String TEST_URL = "http://archiva.apache.org"; - private static final Organization TEST_ORGANIZATION = new Organization( "Apache", "http://apache.org" ); + private static final Organization TEST_ORGANIZATION = new Organization("Apache", "http://apache.org"); private static final String TEST_FACET_ID = "test-facet-id"; @@ -98,7 +75,7 @@ public abstract class AbstractMetadataRepositoryTest private static final String TEST_METADATA_VALUE = "testmetadata"; - protected Logger log = LoggerFactory.getLogger( getClass() ); + protected Logger log = LoggerFactory.getLogger(getClass()); /* * Used by tryAssert to allow to throw exceptions in the lambda expression. @@ -114,1785 +91,1863 @@ public abstract class AbstractMetadataRepositoryTest /* * Runs the assert method until the assert is successful or the number of retries - * is reached. Needed because the JCR Oak index update is asynchronous, so updates - * may not be visible immediately after the modification. + * is reached. Needed because the JCR Oak index update is asynchronous, so updates + * may not be visible immediately after the modification. */ private void tryAssert(AssertFunction func, int retries, int sleepMillis) throws Exception { Throwable t = null; int retry = retries; - while(retry-->0) { + while (retry-- > 0) { try { func.accept(); return; - } catch (Exception|AssertionError e ) { + } catch (Exception | AssertionError e) { t = e; Thread.currentThread().sleep(sleepMillis); - log.warn("Retrying assert "+retry); + log.warn("Retrying assert " + retry); } } - if (retry<=0 && t!=null) { + if (retry <= 0 && t != null) { if (t instanceof RuntimeException) { - throw (RuntimeException)t; + throw (RuntimeException) t; } else if (t instanceof Exception) { - throw (Exception)t; + throw (Exception) t; } } } - public static Map<String, MetadataFacetFactory> createTestMetadataFacetFactories() - { + public static Map<String, MetadataFacetFactory> createTestMetadataFacetFactories() { Map<String, MetadataFacetFactory> factories = new HashMap<>(); - factories.put( TEST_FACET_ID, new MetadataFacetFactory() - { + factories.put(TEST_FACET_ID, new MetadataFacetFactory() { @Override - public MetadataFacet createMetadataFacet() - { - return new TestMetadataFacet( TEST_METADATA_VALUE ); + public MetadataFacet createMetadataFacet() { + return new TestMetadataFacet(TEST_METADATA_VALUE); } @Override - public MetadataFacet createMetadataFacet( String repositoryId, String name ) - { - return new TestMetadataFacet( TEST_METADATA_VALUE ); + public MetadataFacet createMetadataFacet(String repositoryId, String name) { + return new TestMetadataFacet(TEST_METADATA_VALUE); } - } ); + }); // add to ensure we don't accidentally create an empty facet ID. - factories.put( "", new MetadataFacetFactory() - { + factories.put("", new MetadataFacetFactory() { @Override - public MetadataFacet createMetadataFacet() - { - return new TestMetadataFacet( "", TEST_VALUE ); + public MetadataFacet createMetadataFacet() { + return new TestMetadataFacet("", TEST_VALUE); } @Override - public MetadataFacet createMetadataFacet( String repositoryId, String name ) - { - return new TestMetadataFacet( "", TEST_VALUE ); + public MetadataFacet createMetadataFacet(String repositoryId, String name) { + return new TestMetadataFacet("", TEST_VALUE); } - } ); + }); // for the getArtifactsByProjectVersionMetadata tests - factories.put( GenericMetadataFacet.FACET_ID, new GenericMetadataFacetFactory() ); + factories.put(GenericMetadataFacet.FACET_ID, new GenericMetadataFacetFactory()); return factories; } @Test public void testRootNamespaceWithNoMetadataRepository() - throws Exception - { - Collection<String> namespaces = repository.getRootNamespaces( , TEST_REPO_ID ); - assertThat( namespaces ).isNotNull().isEmpty(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + Collection<String> namespaces = repository.getRootNamespaces(session, TEST_REPO_ID); + assertThat(namespaces).isNotNull().isEmpty(); + } } @Test public void testGetNamespaceOnly() - throws Exception - { - assertThat( repository.getRootNamespaces( , TEST_REPO_ID ) ).isNotNull().isEmpty(); - - repository.updateNamespace( , TEST_REPO_ID, TEST_NAMESPACE ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isEmpty(); - assertThat( repository.getRootNamespaces( , TEST_REPO_ID ) ).isNotNull().isNotEmpty().contains( - TEST_NAMESPACE ).hasSize( 1 ); + repository.updateNamespace(session, TEST_REPO_ID, TEST_NAMESPACE); - repository.removeNamespace( , TEST_REPO_ID, TEST_NAMESPACE ); + assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isNotEmpty().contains( + TEST_NAMESPACE).hasSize(1); - assertThat( repository.getRootNamespaces( , TEST_REPO_ID ) ).isNotNull().isEmpty(); + repository.removeNamespace(session, TEST_REPO_ID, TEST_NAMESPACE); + assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isEmpty(); + } } @Test public void testGetProjectOnly() - throws Exception - { - assertNull( repository.getProject( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) ); - assertThat( repository.getRootNamespaces( , TEST_REPO_ID ) ).isNotNull().isEmpty(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { - ProjectMetadata project = new ProjectMetadata(); - project.setId( TEST_PROJECT ); - project.setNamespace( TEST_NAMESPACE ); + assertNull(repository.getProject(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT)); + assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isEmpty(); + + ProjectMetadata project = new ProjectMetadata(); + project.setId(TEST_PROJECT); + project.setNamespace(TEST_NAMESPACE); - repository.updateProject( , TEST_REPO_ID, project ); + repository.updateProject(session, TEST_REPO_ID, project); - project = repository.getProject( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ); - assertEquals( TEST_PROJECT, project.getId() ); - assertEquals( TEST_NAMESPACE, project.getNamespace() ); + project = repository.getProject(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT); + assertEquals(TEST_PROJECT, project.getId()); + assertEquals(TEST_NAMESPACE, project.getNamespace()); - // test that namespace is also constructed + // test that namespace is also constructed - Collection<String> namespaces = repository.getRootNamespaces( , TEST_REPO_ID ); + Collection<String> namespaces = repository.getRootNamespaces( session, TEST_REPO_ID); - assertThat( namespaces ).isNotNull().isNotEmpty().contains( TEST_NAMESPACE ).hasSize( 1 ); + assertThat(namespaces).isNotNull().isNotEmpty().contains(TEST_NAMESPACE).hasSize(1); + } } @Test public void testGetProjectVersionOnly() - throws Exception - { - assertNull( repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ); - assertNull( repository.getProject( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) ); - assertThat( repository.getRootNamespaces( , TEST_REPO_ID ) ).isNotNull().isEmpty(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + + assertNull(repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION)); + assertNull(repository.getProject(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT)); + assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isEmpty(); - ProjectVersionMetadata metadata = new ProjectVersionMetadata(); - metadata.setId( TEST_PROJECT_VERSION ); + ProjectVersionMetadata metadata = new ProjectVersionMetadata(); + metadata.setId(TEST_PROJECT_VERSION); - repository.updateProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); + repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata); - metadata = repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); - assertEquals( TEST_PROJECT_VERSION, metadata.getId() ); + metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); + assertEquals(TEST_PROJECT_VERSION, metadata.getId()); - // test that namespace and project is also constructed - Collection<String> namespaces = repository.getRootNamespaces( , TEST_REPO_ID ); + // test that namespace and project is also constructed + Collection<String> namespaces = repository.getRootNamespaces(session, TEST_REPO_ID); - assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_NAMESPACE ); + assertThat(namespaces).isNotNull().isNotEmpty().hasSize(1).contains(TEST_NAMESPACE); - ProjectMetadata projectMetadata = repository.getProject( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ); - assertNotNull( projectMetadata ); - assertEquals( TEST_PROJECT, projectMetadata.getId() ); - assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() ); + ProjectMetadata projectMetadata = repository.getProject(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT); + assertNotNull(projectMetadata); + assertEquals(TEST_PROJECT, projectMetadata.getId()); + assertEquals(TEST_NAMESPACE, projectMetadata.getNamespace()); + } } @Test public void testGetArtifactOnly() - throws Exception - { - assertThat( new ArrayList<>( - repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, - TEST_PROJECT, TEST_PROJECT_VERSION ) ) ).isNotNull().isEmpty(); - assertThat( - repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ).isNull(); - assertThat( repository.getProject( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) ).isNull(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { - assertThat( repository.getRootNamespaces( , TEST_REPO_ID ) ).isNotNull().isEmpty(); + assertThat(new ArrayList<>( + repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, + TEST_PROJECT, TEST_PROJECT_VERSION))).isNotNull().isEmpty(); + assertThat( + repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION)).isNull(); + assertThat(repository.getProject(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT)).isNull(); - ArtifactMetadata metadata = createArtifact(); + assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isEmpty(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata ); + ArtifactMetadata metadata = createArtifact(); - Collection<ArtifactMetadata> artifacts = - repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); - //assertEquals( Collections.singletonList( metadata ), new ArrayList<ArtifactMetadata>( artifacts ) ); - assertThat( artifacts ).containsExactly( metadata ); - // test that namespace, project and project version is also constructed + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata); - assertThat( repository.getRootNamespaces( , TEST_REPO_ID ) ).isNotNull().isNotEmpty().contains( - TEST_NAMESPACE ).hasSize( 1 ); + Collection<ArtifactMetadata> artifacts = + repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); + //assertEquals( Collections.singletonList( metadata ), new ArrayList<ArtifactMetadata>( artifacts ) ); + assertThat(artifacts).containsExactly(metadata); + // test that namespace, project and project version is also constructed - ProjectMetadata projectMetadata = repository.getProject( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ); - assertEquals( TEST_PROJECT, projectMetadata.getId() ); - assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() ); + assertThat(repository.getRootNamespaces(session, TEST_REPO_ID)).isNotNull().isNotEmpty().contains( + TEST_NAMESPACE).hasSize(1); - ProjectVersionMetadata projectVersionMetadata = - repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); - assertEquals( TEST_PROJECT_VERSION, projectVersionMetadata.getId() ); + ProjectMetadata projectMetadata = repository.getProject(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT); + assertEquals(TEST_PROJECT, projectMetadata.getId()); + assertEquals(TEST_NAMESPACE, projectMetadata.getNamespace()); + + ProjectVersionMetadata projectVersionMetadata = + repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); + assertEquals(TEST_PROJECT_VERSION, projectVersionMetadata.getId()); + } } @Test public void testUpdateProjectVersionMetadataWithNoOtherArchives() - throws Exception - { - ProjectVersionMetadata metadata = new ProjectVersionMetadata(); - metadata.setId( TEST_PROJECT_VERSION ); - MailingList mailingList = new MailingList(); - mailingList.setName( "Foo List" ); - mailingList.setOtherArchives( Collections.<String>emptyList() ); - metadata.setMailingLists( Arrays.asList( mailingList ) ); - repository.updateProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + + ProjectVersionMetadata metadata = new ProjectVersionMetadata(); + metadata.setId(TEST_PROJECT_VERSION); + MailingList mailingList = new MailingList(); + mailingList.setName("Foo List"); + mailingList.setOtherArchives(Collections.<String>emptyList()); + metadata.setMailingLists(Arrays.asList(mailingList)); + repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata); - metadata = repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); - assertEquals( TEST_PROJECT_VERSION, metadata.getId() ); + metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); + assertEquals(TEST_PROJECT_VERSION, metadata.getId()); - List<MailingList> mailingLists = metadata.getMailingLists(); + List<MailingList> mailingLists = metadata.getMailingLists(); - assertThat( mailingLists ).isNotNull().isNotEmpty().hasSize( 1 ); + assertThat(mailingLists).isNotNull().isNotEmpty().hasSize(1); - mailingList = metadata.getMailingLists().get( 0 ); - assertEquals( "Foo List", mailingList.getName() ); + mailingList = metadata.getMailingLists().get(0); + assertEquals("Foo List", mailingList.getName()); - List<String> others = mailingList.getOtherArchives(); - assertThat( others ).isNotNull().isEmpty(); + List<String> others = mailingList.getOtherArchives(); + assertThat(others).isNotNull().isEmpty(); + } } @Test public void testUpdateProjectVersionMetadataWithAllElements() - throws Exception - { - ProjectVersionMetadata metadata = new ProjectVersionMetadata(); - metadata.setId( TEST_PROJECT_VERSION ); - - metadata.setName( "project name" ); - metadata.setDescription( "project description" ); - metadata.setUrl( "the url" ); - - MailingList mailingList = new MailingList(); - mailingList.setName( "Foo List" ); - mailingList.setUnsubscribeAddress( "UnsubscribeAddress" ); - mailingList.setSubscribeAddress( "SubscribeAddress" ); - mailingList.setPostAddress( "PostAddress" ); - mailingList.setMainArchiveUrl( "MainArchiveUrl" ); - mailingList.setOtherArchives( Arrays.asList( "other archive" ) ); - metadata.setMailingLists( Arrays.asList( mailingList ) ); - - Scm scm = new Scm(); - scm.setConnection( "connection" ); - scm.setDeveloperConnection( "dev conn" ); - scm.setUrl( "url" ); - metadata.setScm( scm ); - - CiManagement ci = new CiManagement(); - ci.setSystem( "system" ); - ci.setUrl( "ci url" ); - metadata.setCiManagement( ci ); - - IssueManagement tracker = new IssueManagement(); - tracker.setSystem( "system" ); - tracker.setUrl( "issue tracker url" ); - metadata.setIssueManagement( tracker ); - - metadata.setOrganization( TEST_ORGANIZATION ); - - License l = new License(); - l.setName( "license name" ); - l.setUrl( "license url" ); - metadata.addLicense( l ); - - Dependency d = new Dependency(); - d.setArtifactId( "artifactId" ); - d.setClassifier( "classifier" ); - d.setGroupId( "groupId" ); - d.setScope( "scope" ); - d.setSystemPath( "system path" ); - d.setType( "type" ); - d.setVersion( "version" ); - d.setOptional( true ); - metadata.addDependency( d ); - - repository.updateProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); - - metadata = repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); - assertEquals( TEST_PROJECT_VERSION, metadata.getId() ); - assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() ); - assertEquals( "project name", metadata.getName() ); - assertEquals( "project description", metadata.getDescription() ); - assertEquals( "the url", metadata.getUrl() ); - - assertEquals( "connection", metadata.getScm().getConnection() ); - assertEquals( "dev conn", metadata.getScm().getDeveloperConnection() ); - assertEquals( "url", metadata.getScm().getUrl() ); - - assertEquals( "system", metadata.getCiManagement().getSystem() ); - assertEquals( "ci url", metadata.getCiManagement().getUrl() ); - - assertEquals( "system", metadata.getIssueManagement().getSystem() ); - assertEquals( "issue tracker url", metadata.getIssueManagement().getUrl() ); - - assertEquals( TEST_ORGANIZATION.getName(), metadata.getOrganization().getName() ); - assertEquals( TEST_ORGANIZATION.getUrl(), metadata.getOrganization().getUrl() ); - - assertEquals( 1, metadata.getMailingLists().size() ); - MailingList retrievedMailingList = metadata.getMailingLists().get( 0 ); - assertEquals( mailingList.getName(), retrievedMailingList.getName() ); - assertEquals( mailingList.getMainArchiveUrl(), retrievedMailingList.getMainArchiveUrl() ); - assertEquals( mailingList.getPostAddress(), retrievedMailingList.getPostAddress() ); - assertEquals( mailingList.getSubscribeAddress(), retrievedMailingList.getSubscribeAddress() ); - assertEquals( mailingList.getUnsubscribeAddress(), retrievedMailingList.getUnsubscribeAddress() ); - assertThat( retrievedMailingList.getOtherArchives() ) // - .isNotNull() // - .isNotEmpty() // - .hasSize( 1 ) // - .contains( "other archive" ); - - assertEquals( 1, metadata.getLicenses().size() ); - l = metadata.getLicenses().get( 0 ); - assertEquals( "license name", l.getName() ); - assertEquals( "license url", l.getUrl() ); - - assertEquals( 1, metadata.getDependencies().size() ); - d = metadata.getDependencies().get( 0 ); - assertEquals( "artifactId", d.getArtifactId() ); - assertEquals( "classifier", d.getClassifier() ); - assertEquals( "groupId", d.getGroupId() ); - assertEquals( "scope", d.getScope() ); - assertEquals( "system path", d.getSystemPath() ); - assertEquals( "type", d.getType() ); - assertEquals( "version", d.getVersion() ); - assertTrue( d.isOptional() ); - } - - @Test - public void testGetRepositories() - throws Exception - { - // currently set up this way so the behaviour of both the test and the mock config return the same repository - // set as the File implementation just uses the config rather than the content - repository.updateNamespace( , TEST_REPO_ID, "namespace" ); - repository.updateNamespace( , OTHER_REPO_ID, "namespace" ); - - Collection<String> repositories = repository.getRepositories(); - - assertEquals( "repository.getRepositories() -> " + repositories, // - Sets.newLinkedHashSet( TEST_REPO_ID, OTHER_REPO_ID ), // - new LinkedHashSet<String>( repositories ) ); // + throws Exception { + + try (RepositorySession session = sessionFactory.createSession()) { + + ProjectVersionMetadata metadata = new ProjectVersionMetadata(); + metadata.setId(TEST_PROJECT_VERSION); + + metadata.setName("project name"); + metadata.setDescription("project description"); + metadata.setUrl("the url"); + + MailingList mailingList = new MailingList(); + mailingList.setName("Foo List"); + mailingList.setUnsubscribeAddress("UnsubscribeAddress"); + mailingList.setSubscribeAddress("SubscribeAddress"); + mailingList.setPostAddress("PostAddress"); + mailingList.setMainArchiveUrl("MainArchiveUrl"); + mailingList.setOtherArchives(Arrays.asList("other archive")); + metadata.setMailingLists(Arrays.asList(mailingList)); + + Scm scm = new Scm(); + scm.setConnection("connection"); + scm.setDeveloperConnection("dev conn"); + scm.setUrl("url"); + metadata.setScm(scm); + + CiManagement ci = new CiManagement(); + ci.setSystem("system"); + ci.setUrl("ci url"); + metadata.setCiManagement(ci); + + IssueManagement tracker = new IssueManagement(); + tracker.setSystem("system"); + tracker.setUrl("issue tracker url"); + metadata.setIssueManagement(tracker); + + metadata.setOrganization(TEST_ORGANIZATION); + + License l = new License(); + l.setName("license name"); + l.setUrl("license url"); + metadata.addLicense(l); + + Dependency d = new Dependency(); + d.setArtifactId("artifactId"); + d.setClassifier("classifier"); + d.setGroupId("groupId"); + d.setScope("scope"); + d.setSystemPath("system path"); + d.setType("type"); + d.setVersion("version"); + d.setOptional(true); + metadata.addDependency(d); + + repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata); + + metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); + assertEquals(TEST_PROJECT_VERSION, metadata.getId()); + assertEquals(TEST_PROJECT_VERSION, metadata.getVersion()); + assertEquals("project name", metadata.getName()); + assertEquals("project description", metadata.getDescription()); + assertEquals("the url", metadata.getUrl()); + + assertEquals("connection", metadata.getScm().getConnection()); + assertEquals("dev conn", metadata.getScm().getDeveloperConnection()); + assertEquals("url", metadata.getScm().getUrl()); + + assertEquals("system", metadata.getCiManagement().getSystem()); + assertEquals("ci url", metadata.getCiManagement().getUrl()); + + assertEquals("system", metadata.getIssueManagement().getSystem()); + assertEquals("issue tracker url", metadata.getIssueManagement().getUrl()); + + assertEquals(TEST_ORGANIZATION.getName(), metadata.getOrganization().getName()); + assertEquals(TEST_ORGANIZATION.getUrl(), metadata.getOrganization().getUrl()); + + assertEquals(1, metadata.getMailingLists().size()); + MailingList retrievedMailingList = metadata.getMailingLists().get(0); + assertEquals(mailingList.getName(), retrievedMailingList.getName()); + assertEquals(mailingList.getMainArchiveUrl(), retrievedMailingList.getMainArchiveUrl()); + assertEquals(mailingList.getPostAddress(), retrievedMailingList.getPostAddress()); + assertEquals(mailingList.getSubscribeAddress(), retrievedMailingList.getSubscribeAddress()); + assertEquals(mailingList.getUnsubscribeAddress(), retrievedMailingList.getUnsubscribeAddress()); + assertThat(retrievedMailingList.getOtherArchives()) // + .isNotNull() // + .isNotEmpty() // + .hasSize(1) // + .contains("other archive"); + + assertEquals(1, metadata.getLicenses().size()); + l = metadata.getLicenses().get(0); + assertEquals("license name", l.getName()); + assertEquals("license url", l.getUrl()); + + assertEquals(1, metadata.getDependencies().size()); + d = metadata.getDependencies().get(0); + assertEquals("artifactId", d.getArtifactId()); + assertEquals("classifier", d.getClassifier()); + assertEquals("groupId", d.getGroupId()); + assertEquals("scope", d.getScope()); + assertEquals("system path", d.getSystemPath()); + assertEquals("type", d.getType()); + assertEquals("version", d.getVersion()); + assertTrue(d.isOptional()); + } } @Test public void testUpdateProjectVersionMetadataIncomplete() - throws Exception - { - ProjectVersionMetadata metadata = new ProjectVersionMetadata(); - metadata.setId( TEST_PROJECT_VERSION ); - metadata.setIncomplete( true ); - repository.updateProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); - - metadata = repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); - assertEquals( true, metadata.isIncomplete() ); - assertNull( metadata.getCiManagement() ); - assertNull( metadata.getScm() ); - assertNull( metadata.getIssueManagement() ); - assertNull( metadata.getOrganization() ); - assertNull( metadata.getDescription() ); - assertNull( metadata.getName() ); - assertEquals( TEST_PROJECT_VERSION, metadata.getId() ); - assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() ); - assertTrue( metadata.getMailingLists().isEmpty() ); - assertTrue( metadata.getLicenses().isEmpty() ); - assertTrue( metadata.getDependencies().isEmpty() ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + + ProjectVersionMetadata metadata = new ProjectVersionMetadata(); + metadata.setId(TEST_PROJECT_VERSION); + metadata.setIncomplete(true); + repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata); + + metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); + assertEquals(true, metadata.isIncomplete()); + assertNull(metadata.getCiManagement()); + assertNull(metadata.getScm()); + assertNull(metadata.getIssueManagement()); + assertNull(metadata.getOrganization()); + assertNull(metadata.getDescription()); + assertNull(metadata.getName()); + assertEquals(TEST_PROJECT_VERSION, metadata.getId()); + assertEquals(TEST_PROJECT_VERSION, metadata.getVersion()); + assertTrue(metadata.getMailingLists().isEmpty()); + assertTrue(metadata.getLicenses().isEmpty()); + assertTrue(metadata.getDependencies().isEmpty()); + } } @Test public void testUpdateProjectVersionMetadataWithExistingFacets() - throws Exception - { - ProjectVersionMetadata metadata = new ProjectVersionMetadata(); - metadata.setId( TEST_PROJECT_VERSION ); - MetadataFacet facet = new TestMetadataFacet( "baz" ); - metadata.addFacet( facet ); - repository.updateProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); - - metadata = repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); - assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() ); - - metadata = new ProjectVersionMetadata(); - metadata.setId( TEST_PROJECT_VERSION ); - repository.updateProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); - - metadata = repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); - assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() ); - TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID ); - assertEquals( "baz", testFacet.getValue() ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + + ProjectVersionMetadata metadata = new ProjectVersionMetadata(); + metadata.setId(TEST_PROJECT_VERSION); + MetadataFacet facet = new TestMetadataFacet("baz"); + metadata.addFacet(facet); + repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata); + + metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); + assertEquals(Collections.singleton(TEST_FACET_ID), metadata.getFacetIds()); + + metadata = new ProjectVersionMetadata(); + metadata.setId(TEST_PROJECT_VERSION); + repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata); + + metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); + assertEquals(Collections.singleton(TEST_FACET_ID), metadata.getFacetIds()); + TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID); + assertEquals("baz", testFacet.getValue()); + } } @Test public void testUpdateProjectVersionMetadataWithNoExistingFacets() - throws Exception - { - ProjectVersionMetadata metadata = new ProjectVersionMetadata(); - metadata.setId( TEST_PROJECT_VERSION ); - repository.updateProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { - metadata = repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); + ProjectVersionMetadata metadata = new ProjectVersionMetadata(); + metadata.setId(TEST_PROJECT_VERSION); + repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata); - assertThat( metadata.getFacetIds() ).isNotNull().isEmpty(); + metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); - metadata = new ProjectVersionMetadata(); - metadata.setId( TEST_PROJECT_VERSION ); - repository.updateProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); + assertThat(metadata.getFacetIds()).isNotNull().isEmpty(); - metadata = repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); - assertThat( metadata.getFacetIds() ).isNotNull().isEmpty(); + metadata = new ProjectVersionMetadata(); + metadata.setId(TEST_PROJECT_VERSION); + repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata); + metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); + assertThat(metadata.getFacetIds()).isNotNull().isEmpty(); + } } @Test public void testUpdateProjectVersionMetadataWithExistingFacetsFacetPropertyWasRemoved() - throws Exception - { - ProjectVersionMetadata metadata = new ProjectVersionMetadata(); - metadata.setId( TEST_PROJECT_VERSION ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + + ProjectVersionMetadata metadata = new ProjectVersionMetadata(); + metadata.setId(TEST_PROJECT_VERSION); - Map<String, String> additionalProps = new HashMap<>(); - additionalProps.put( "deleteKey", "deleteValue" ); + Map<String, String> additionalProps = new HashMap<>(); + additionalProps.put("deleteKey", "deleteValue"); - MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps ); - metadata.addFacet( facet ); - repository.updateProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); + MetadataFacet facet = new TestMetadataFacet(TEST_FACET_ID, "baz", additionalProps); + metadata.addFacet(facet); + repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata); - metadata = repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); + metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); - assertThat( metadata.getFacetIds() ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID ); + assertThat(metadata.getFacetIds()).isNotNull().isNotEmpty().hasSize(1).contains(TEST_FACET_ID); - TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID ); - Map<String, String> facetProperties = testFacet.toProperties(); + TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID); + Map<String, String> facetProperties = testFacet.toProperties(); - assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) ); + assertEquals("deleteValue", facetProperties.get("deleteKey")); - facetProperties.remove( "deleteKey" ); + facetProperties.remove("deleteKey"); - TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties ); - metadata.addFacet( newTestFacet ); + TestMetadataFacet newTestFacet = new TestMetadataFacet(TEST_FACET_ID, testFacet.getValue(), facetProperties); + metadata.addFacet(newTestFacet); - repository.updateProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); + repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata); - metadata = repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); + metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); - assertThat( metadata.getFacetIds() ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID ); - testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID ); - assertFalse( testFacet.toProperties().containsKey( "deleteKey" ) ); + assertThat(metadata.getFacetIds()).isNotNull().isNotEmpty().hasSize(1).contains(TEST_FACET_ID); + testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID); + assertFalse(testFacet.toProperties().containsKey("deleteKey")); + } } @Test public void testGetArtifactsDoesntReturnProjectVersionMetadataFacets() - throws Exception - { - ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata(); - versionMetadata.setId( TEST_PROJECT_VERSION ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { - MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz" ); - versionMetadata.addFacet( facet ); - repository.updateProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, versionMetadata ); + ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata(); + versionMetadata.setId(TEST_PROJECT_VERSION); - ArtifactMetadata artifactMetadata = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifactMetadata ); - repository.save(); + MetadataFacet facet = new TestMetadataFacet(TEST_FACET_ID, "baz"); + versionMetadata.addFacet(facet); + repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, versionMetadata); - Collection<ArtifactMetadata> artifacts = - repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); - assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<>( artifacts ) ); + ArtifactMetadata artifactMetadata = createArtifact(); + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifactMetadata); + session.save(); - artifacts = repository.getArtifacts( , TEST_REPO_ID ); - assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<>( artifacts ) ); + Collection<ArtifactMetadata> artifacts = + repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); + assertEquals(Collections.singletonList(artifactMetadata), new ArrayList<>(artifacts)); - artifacts = repository.getArtifactsByChecksum( , TEST_REPO_ID, TEST_SHA1 ); - assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<>( artifacts ) ); + artifacts = repository.getArtifacts(session, TEST_REPO_ID); + assertEquals(Collections.singletonList(artifactMetadata), new ArrayList<>(artifacts)); - artifacts = repository.getArtifactsByChecksum( , TEST_REPO_ID, TEST_MD5 ); - assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<>( artifacts ) ); + artifacts = repository.getArtifactsByChecksum(session, TEST_REPO_ID, TEST_SHA1); + assertEquals(Collections.singletonList(artifactMetadata), new ArrayList<>(artifacts)); - artifacts = repository.getArtifactsByDateRange( , TEST_REPO_ID, null, null ); - assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<>( artifacts ) ); + artifacts = repository.getArtifactsByChecksum(session, TEST_REPO_ID, TEST_MD5); + assertEquals(Collections.singletonList(artifactMetadata), new ArrayList<>(artifacts)); + + artifacts = repository.getArtifactsByDateRange(session, TEST_REPO_ID, null, null); + assertEquals(Collections.singletonList(artifactMetadata), new ArrayList<>(artifacts)); + } } @Test public void testUpdateArtifactMetadataWithExistingFacetsFacetPropertyWasRemoved() - throws Exception - { - ArtifactMetadata metadata = createArtifact(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + + ArtifactMetadata metadata = createArtifact(); - Map<String, String> additionalProps = new HashMap<>(); - additionalProps.put( "deleteKey", "deleteValue" ); + Map<String, String> additionalProps = new HashMap<>(); + additionalProps.put("deleteKey", "deleteValue"); - MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps ); - metadata.addFacet( facet ); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata ); + MetadataFacet facet = new TestMetadataFacet(TEST_FACET_ID, "baz", additionalProps); + metadata.addFacet(facet); + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata); - Collection<ArtifactMetadata> artifacts = - repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); + Collection<ArtifactMetadata> artifacts = + repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); - assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 ); - metadata = artifacts.iterator().next(); + assertThat(artifacts).isNotNull().isNotEmpty().hasSize(1); + metadata = artifacts.iterator().next(); - Collection<String> ids = metadata.getFacetIds(); - assertThat( ids ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID ); + Collection<String> ids = metadata.getFacetIds(); + assertThat(ids).isNotNull().isNotEmpty().hasSize(1).contains(TEST_FACET_ID); - TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID ); - Map<String, String> facetProperties = testFacet.toProperties(); + TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID); + Map<String, String> facetProperties = testFacet.toProperties(); - assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) ); + assertEquals("deleteValue", facetProperties.get("deleteKey")); - facetProperties.remove( "deleteKey" ); + facetProperties.remove("deleteKey"); - TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties ); - metadata.addFacet( newTestFacet ); + TestMetadataFacet newTestFacet = new TestMetadataFacet(TEST_FACET_ID, testFacet.getValue(), facetProperties); + metadata.addFacet(newTestFacet); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata ); + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata); - artifacts = repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); + artifacts = repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); - assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 ); - metadata = artifacts.iterator().next(); + assertThat(artifacts).isNotNull().isNotEmpty().hasSize(1); + metadata = artifacts.iterator().next(); - ids = metadata.getFacetIds(); - assertThat( ids ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID ); + ids = metadata.getFacetIds(); + assertThat(ids).isNotNull().isNotEmpty().hasSize(1).contains(TEST_FACET_ID); - testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID ); + testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID); - Map<String, String> props = testFacet.toProperties(); - assertThat( props ).isNotNull().doesNotContainKey( "deleteKey" ); + Map<String, String> props = testFacet.toProperties(); + assertThat(props).isNotNull().doesNotContainKey("deleteKey"); + } } @Test public void testUpdateArtifactMetadataWithExistingFacets() - throws Exception - { - ArtifactMetadata metadata = createArtifact(); - MetadataFacet facet = new TestMetadataFacet( "baz" ); - metadata.addFacet( facet ); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata ); - - metadata = repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, - TEST_PROJECT, TEST_PROJECT_VERSION ).iterator().next(); - assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() ); - - metadata = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata ); - - metadata = repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, - TEST_PROJECT, TEST_PROJECT_VERSION ).iterator().next(); - assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() ); - TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID ); - assertEquals( "baz", testFacet.getValue() ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + + ArtifactMetadata metadata = createArtifact(); + MetadataFacet facet = new TestMetadataFacet("baz"); + metadata.addFacet(facet); + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata); + + metadata = repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, + TEST_PROJECT, TEST_PROJECT_VERSION).iterator().next(); + assertEquals(Collections.singleton(TEST_FACET_ID), metadata.getFacetIds()); + + metadata = createArtifact(); + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata); + + metadata = repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, + TEST_PROJECT, TEST_PROJECT_VERSION).iterator().next(); + assertEquals(Collections.singleton(TEST_FACET_ID), metadata.getFacetIds()); + TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID); + assertEquals("baz", testFacet.getValue()); + } } @Test public void testUpdateArtifactMetadataWithNoExistingFacets() - throws Exception - { - ArtifactMetadata metadata = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata metadata = createArtifact(); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata); + + metadata = repository.getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, + TEST_PROJECT, TEST_PROJECT_VERSION).iterator().next(); + assertEquals(Collections.<String>emptyList(), new ArrayList<String>(metadata.getFacetIds())); - metadata = repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, - TEST_PROJECT, TEST_PROJECT_VERSION ).iterator().next(); - assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) ); + metadata = createArtifact(); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata); - metadata = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata ); + metadata = repository.getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, + TEST_PROJECT, TEST_PROJECT_VERSION).iterator().next(); + assertEquals(Collections.<String>emptyList(), new ArrayList<String>(metadata.getFacetIds())); - metadata = repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, - TEST_PROJECT, TEST_PROJECT_VERSION ).iterator().next(); - assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) ); + } } @Test public void testGetMetadataFacet() - throws Exception - { - repository.addMetadataFacet( , TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + repository.addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet(TEST_VALUE)); - TestMetadataFacet test = - (TestMetadataFacet) repository.getMetadataFacet( , TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ); + TestMetadataFacet test = + (TestMetadataFacet) repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME); - assertEquals( new TestMetadataFacet( TEST_VALUE ), test ); + assertEquals(new TestMetadataFacet(TEST_VALUE), test); + + } } @Test public void testGetMetadataFacetWhenEmpty() - throws Exception - { - assertNull( repository.getMetadataFacet( , TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + assertNull(repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME)); + + } } @Test public void testGetMetadataFacetWhenUnknownName() - throws Exception - { - repository.addMetadataFacet( , TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + repository.addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet(TEST_VALUE)); + + assertNull(repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, UNKNOWN)); - assertNull( repository.getMetadataFacet( , TEST_REPO_ID, TEST_FACET_ID, UNKNOWN ) ); + } } @Test public void testGetMetadataFacetWhenDefaultValue() - throws Exception - { - repository.addMetadataFacet( , TEST_REPO_ID, new TestMetadataFacet( null ) ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + repository.addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet(null)); - MetadataFacet metadataFacet = repository.getMetadataFacet( , TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ); + MetadataFacet metadataFacet = repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME); - assertEquals( new TestMetadataFacet( TEST_METADATA_VALUE ), metadataFacet ); + assertEquals(new TestMetadataFacet(TEST_METADATA_VALUE), metadataFacet); + + } } @Test public void testGetMetadataFacetWhenUnknownFacetId() - throws Exception - { - assertNull( repository.getMetadataFacet( , TEST_REPO_ID, UNKNOWN, TEST_NAME ) ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + assertNull(repository.getMetadataFacet( session, TEST_REPO_ID, UNKNOWN, TEST_NAME)); + + } } @Test public void testGetMetadataFacets() - throws Exception - { - repository.addMetadataFacet( , TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + repository.addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet(TEST_VALUE)); + + assertEquals(Collections.singletonList(TEST_NAME), + repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID)); - assertEquals( Collections.singletonList( TEST_NAME ), - repository.getMetadataFacets( , TEST_REPO_ID, TEST_FACET_ID ) ); + } } @Test public void testGetMetadataFacetsWhenEmpty() - throws Exception - { + throws Exception { - List<String> facets = repository.getMetadataFacets( , TEST_REPO_ID, TEST_FACET_ID ); - assertTrue( facets.isEmpty() ); + try (RepositorySession session = sessionFactory.createSession()) { + List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID); + assertTrue(facets.isEmpty()); + + } } @Test public void testRemoveFacets() - throws Exception - { - repository.addMetadataFacet( , TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + repository.addMetadataFacet( session, TEST_REPO_ID, new TestMetadataFacet(TEST_VALUE)); + + List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID); + assertFalse(facets.isEmpty()); - List<String> facets = repository.getMetadataFacets( , TEST_REPO_ID, TEST_FACET_ID ); - assertFalse( facets.isEmpty() ); + repository.removeMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID); - repository.removeMetadataFacets( , TEST_REPO_ID, TEST_FACET_ID ); + facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID); + assertTrue(facets.isEmpty()); - facets = repository.getMetadataFacets( , TEST_REPO_ID, TEST_FACET_ID ); - assertTrue( facets.isEmpty() ); + } } @Test public void testRemoveFacetsWhenEmpty() - throws Exception - { - List<String> facets = repository.getMetadataFacets( , TEST_REPO_ID, TEST_FACET_ID ); - assertTrue( facets.isEmpty() ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID); + assertTrue(facets.isEmpty()); + + repository.removeMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID); - repository.removeMetadataFacets( , TEST_REPO_ID, TEST_FACET_ID ); + facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID); + assertTrue(facets.isEmpty()); - facets = repository.getMetadataFacets( , TEST_REPO_ID, TEST_FACET_ID ); - assertTrue( facets.isEmpty() ); + } } @Test public void testRemoveFacetsWhenUnknown() - throws Exception - { - // testing no exception - repository.removeMetadataFacets( , TEST_REPO_ID, UNKNOWN ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { +// testing no exception + repository.removeMetadataFacets( session, TEST_REPO_ID, UNKNOWN); + + } } @Test public void testRemoveFacetWhenUnknown() - throws Exception - { - // testing no exception - repository.removeMetadataFacet( , TEST_REPO_ID, UNKNOWN, TEST_NAME ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { +// testing no exception + repository.removeMetadataFacet( session, TEST_REPO_ID, UNKNOWN, TEST_NAME); + + } } @Test public void testRemoveFacet() - throws Exception - { - TestMetadataFacet metadataFacet = new TestMetadataFacet( TEST_VALUE ); - repository.addMetadataFacet( , TEST_REPO_ID, metadataFacet ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + TestMetadataFacet metadataFacet = new TestMetadataFacet(TEST_VALUE); + repository.addMetadataFacet( session, TEST_REPO_ID, metadataFacet); - assertEquals( metadataFacet, repository.getMetadataFacet( , TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) ); - List<String> facets = repository.getMetadataFacets( , TEST_REPO_ID, TEST_FACET_ID ); - assertFalse( facets.isEmpty() ); + assertEquals(metadataFacet, repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME)); + List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID); + assertFalse(facets.isEmpty()); - repository.removeMetadataFacet( , TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ); + repository.removeMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME); - assertNull( repository.getMetadataFacet( , TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) ); - facets = repository.getMetadataFacets( , TEST_REPO_ID, TEST_FACET_ID ); - assertTrue( facets.isEmpty() ); + assertNull(repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME)); + facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID); + assertTrue(facets.isEmpty()); + + } } @Test public void testRemoveFacetWhenEmpty() - throws Exception - { - List<String> facets = repository.getMetadataFacets( , TEST_REPO_ID, TEST_FACET_ID ); - assertThat( facets ).isNotNull().isEmpty(); - assertThat( repository.getMetadataFacet( , TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) ).isNull(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID); + assertThat(facets).isNotNull().isEmpty(); + assertThat(repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME)).isNull(); + + repository.removeMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME); - repository.removeMetadataFacet( , TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ); + facets = repository.getMetadataFacets( session, TEST_REPO_ID, TEST_FACET_ID); + assertThat(facets).isNotNull().isEmpty(); + assertThat(repository.getMetadataFacet( session, TEST_REPO_ID, TEST_FACET_ID, TEST_NAME)).isNull(); - facets = repository.getMetadataFacets( , TEST_REPO_ID, TEST_FACET_ID ); - assertThat( facets ).isNotNull().isEmpty(); - assertThat( repository.getMetadataFacet( , TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) ).isNull(); + } } @Test public void hasMetadataFacetStart() - throws Exception - { - assertFalse( repository.hasMetadataFacet( , TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + assertFalse(repository.hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName())); + + } } @Test public void hasMetadataFacet() - throws Exception - { + throws Exception { - assertFalse( repository.hasMetadataFacet( , TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) ); + try (RepositorySession session = sessionFactory.createSession()) { + assertFalse(repository.hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName())); - Calendar cal = Calendar.getInstance(); + Calendar cal = Calendar.getInstance(); - repository.addMetadataFacet( , TEST_REPO_ID, new KindOfRepositoryStatistics( "first", cal.getTime() ) ); + repository.addMetadataFacet( session, TEST_REPO_ID, new KindOfRepositoryStatistics("first", cal.getTime())); - assertTrue( repository.hasMetadataFacet( , TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) ); + assertTrue(repository.hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName())); - cal.add( Calendar.MINUTE, 2 ); + cal.add(Calendar.MINUTE, 2); - repository.addMetadataFacet( , TEST_REPO_ID, new KindOfRepositoryStatistics( "second", cal.getTime() ) ); + repository.addMetadataFacet( session, TEST_REPO_ID, new KindOfRepositoryStatistics("second", cal.getTime())); - cal.add( Calendar.MINUTE, 2 ); + cal.add(Calendar.MINUTE, 2); - repository.addMetadataFacet( , TEST_REPO_ID, new KindOfRepositoryStatistics( "third", cal.getTime() ) ); + repository.addMetadataFacet( session, TEST_REPO_ID, new KindOfRepositoryStatistics("third", cal.getTime())); - List<String> facets = repository.getMetadataFacets( , TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ); + List<String> facets = repository.getMetadataFacets( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName()); - assertThat( facets ).isNotNull().isNotEmpty().hasSize( 3 ); + assertThat(facets).isNotNull().isNotEmpty().hasSize(3); - assertTrue( repository.hasMetadataFacet( , TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) ); + assertTrue(repository.hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName())); - repository.removeMetadataFacets( , TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ); + repository.removeMetadataFacets( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName()); - assertFalse( repository.hasMetadataFacet( , TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) ); + assertFalse(repository.hasMetadataFacet( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName())); - facets = repository.getMetadataFacets( , TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ); + facets = repository.getMetadataFacets( session, TEST_REPO_ID, KindOfRepositoryStatistics.class.getName()); - assertThat( facets ).isNotNull().isEmpty(); + assertThat(facets).isNotNull().isEmpty(); + + } } @Test public void testGetArtifacts() - throws Exception - { - ArtifactMetadata artifact1 = createArtifact(); - ArtifactMetadata artifact2 = createArtifact( "pom" ); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 ); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact1 = createArtifact(); + ArtifactMetadata artifact2 = createArtifact("pom"); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2); + + Collection<ArtifactMetadata> artifacts = + repository.getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); + ArrayList<ArtifactMetadata> actual = new ArrayList<>(artifacts); + Collections.sort(actual, (o1, o2) -> o1.getId().compareTo(o2.getId())); + assertEquals(Arrays.asList(artifact1, artifact2), actual); - Collection<ArtifactMetadata> artifacts = - repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); - ArrayList<ArtifactMetadata> actual = new ArrayList<>( artifacts ); - Collections.sort( actual,( o1, o2 ) -> o1.getId().compareTo( o2.getId() ) ); - assertEquals( Arrays.asList( artifact1, artifact2 ), actual ); + } } @Test public void testGetArtifactVersions() - throws Exception - { - ArtifactMetadata artifact1 = createArtifact(); - String version1 = "1.0-20091212.012345-1"; - artifact1.setId( artifact1.getProject() + "-" + version1 + ".jar" ); - artifact1.setVersion( version1 ); - ArtifactMetadata artifact2 = createArtifact(); - String version2 = "1.0-20091212.123456-2"; - artifact2.setId( artifact2.getProject() + "-" + version2 + ".jar" ); - artifact2.setVersion( version2 ); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 ); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact1 = createArtifact(); + String version1 = "1.0-20091212.012345-1"; + artifact1.setId(artifact1.getProject() + "-" + version1 + ".jar"); + artifact1.setVersion(version1); + ArtifactMetadata artifact2 = createArtifact(); + String version2 = "1.0-20091212.123456-2"; + artifact2.setId(artifact2.getProject() + "-" + version2 + ".jar"); + artifact2.setVersion(version2); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2); + + Collection<String> versions = + repository.getArtifactVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); + + assertThat(versions).isNotNull().isNotEmpty().contains(version1, version2); - Collection<String> versions = - repository.getArtifactVersions( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); - - assertThat( versions ).isNotNull().isNotEmpty().contains( version1, version2 ); + } } @Test public void testGetArtifactVersionsMultipleArtifactsSingleVersion() - throws Exception - { - ArtifactMetadata artifact1 = createArtifact(); - artifact1.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" ); - ArtifactMetadata artifact2 = createArtifact(); - artifact2.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "-sources.jar" ); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 ); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact1 = createArtifact(); + artifact1.setId(TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar"); + ArtifactMetadata artifact2 = createArtifact(); + artifact2.setId(TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "-sources.jar"); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2); - Collection<String> versions = - repository.getArtifactVersions( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); + Collection<String> versions = + repository.getArtifactVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); - assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 ).containsExactly( TEST_PROJECT_VERSION ); + assertThat(versions).isNotNull().isNotEmpty().hasSize(1).containsExactly(TEST_PROJECT_VERSION); + + } } @Test public void testGetArtifactsByDateRangeOpen() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); - repository.save(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); + session.save(); + + List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, null, null); - List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( , TEST_REPO_ID, null, null ); + assertEquals(Collections.singletonList(artifact), artifacts); - assertEquals( Collections.singletonList( artifact ), artifacts ); + } } @Test public void testGetArtifactsByDateRangeSparseNamespace() - throws Exception - { - String namespace = "org.apache.archiva"; - ArtifactMetadata artifact = createArtifact(); - artifact.setNamespace( namespace ); - repository.updateArtifact( , TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); - repository.save(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + String namespace = "org.apache.archiva"; + ArtifactMetadata artifact = createArtifact(); + artifact.setNamespace(namespace); + repository.updateArtifact( session, TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); + session.save(); + + List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, null, null); - List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( , TEST_REPO_ID, null, null ); + tryAssert(() -> assertEquals(Collections.singletonList(artifact), artifacts)); - tryAssert(()->assertEquals( Collections.singletonList( artifact ), artifacts )); + } } @Test public void testGetArtifactsByDateRangeLowerBound() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); - repository.save(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); + session.save(); - Date date = new Date( artifact.getWhenGathered().getTime() - 10000 ); + Date date = new Date(artifact.getWhenGathered().getTime() - 10000); - List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( , TEST_REPO_ID, date, null ); + List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, date, null); - assertEquals( Collections.singletonList( artifact ), artifacts ); + assertEquals(Collections.singletonList(artifact), artifacts); + + } } @Test public void testGetArtifactsByDateRangeLowerBoundOutOfRange() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); + + Date date = new Date(artifact.getWhenGathered().getTime() + 10000); - Date date = new Date( artifact.getWhenGathered().getTime() + 10000 ); + List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, date, null); - List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( , TEST_REPO_ID, date, null ); + assertThat(artifacts).isNotNull().isEmpty(); - assertThat( artifacts ).isNotNull().isEmpty(); + } } @Test public void testGetArtifactsByDateRangeLowerAndUpperBound() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); - repository.save(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); + session.save(); - Date lower = new Date( artifact.getWhenGathered().getTime() - 10000 ); - Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 ); + Date lower = new Date(artifact.getWhenGathered().getTime() - 10000); + Date upper = new Date(artifact.getWhenGathered().getTime() + 10000); - List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( , TEST_REPO_ID, lower, upper ); + List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, lower, upper); - assertEquals( Collections.singletonList( artifact ), artifacts ); + assertEquals(Collections.singletonList(artifact), artifacts); + + } } @Test public void testGetArtifactsByDateRangeUpperBound() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); - repository.save(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); + session.save(); - Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 ); + Date upper = new Date(artifact.getWhenGathered().getTime() + 10000); - List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( , TEST_REPO_ID, null, upper ); + List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, null, upper); - assertEquals( Collections.singletonList( artifact ), artifacts ); + assertEquals(Collections.singletonList(artifact), artifacts); + + } } @Test public void testGetArtifactsByDateRangeUpperBoundOutOfRange() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); - repository.save(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); + session.save(); + + Date upper = new Date(artifact.getWhenGathered().getTime() - 10000); - Date upper = new Date( artifact.getWhenGathered().getTime() - 10000 ); + List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( session, TEST_REPO_ID, null, upper); - List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( , TEST_REPO_ID, null, upper ); + assertThat(artifacts).isNotNull().isEmpty(); - assertThat( artifacts ).isNotNull().isEmpty(); + } } @Test public void testGetArtifactsByRepoId() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); - repository.save(); - - tryAssert(()-> { - List<ArtifactMetadata> artifacts = repository.getArtifacts( , TEST_REPO_ID ); - assertEquals(Collections.singletonList(artifact), artifacts); - }); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); + session.save(); + + tryAssert(() -> { + List<ArtifactMetadata> artifacts = repository.getArtifacts(session, TEST_REPO_ID); + assertEquals(Collections.singletonList(artifact), artifacts); + } + ); + } } @Test public void testGetArtifactsByRepoIdMultipleCopies() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); + + ArtifactMetadata secondArtifact = createArtifact(); + secondArtifact.setRepositoryId(OTHER_REPO_ID); + repository.updateArtifact( session, OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact); + session.save(); - ArtifactMetadata secondArtifact = createArtifact(); - secondArtifact.setRepositoryId( OTHER_REPO_ID ); - repository.updateArtifact( , OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact ); - repository.save(); + // test it restricts to the appropriate repository + tryAssert(() -> assertEquals(Collections.singletonList(artifact), repository.getArtifacts( session, TEST_REPO_ID))); + tryAssert(() -> assertEquals(Collections.singletonList(secondArtifact), repository.getArtifacts( session, OTHER_REPO_ID))); - // test it restricts to the appropriate repository - tryAssert(() -> assertEquals( Collections.singletonList( artifact ), repository.getArtifacts( , TEST_REPO_ID ) )); - tryAssert(() -> assertEquals( Collections.singletonList( secondArtifact ), repository.getArtifacts( , OTHER_REPO_ID ) )); + } } @Test public void testGetArtifactsByDateRangeMultipleCopies() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); + + ArtifactMetadata secondArtifact = createArtifact(); + secondArtifact.setRepositoryId(OTHER_REPO_ID); + repository.updateArtifact( session, OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact); + session.save(); - ArtifactMetadata secondArtifact = createArtifact(); - secondArtifact.setRepositoryId( OTHER_REPO_ID ); - repository.updateArtifact( , OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact ); - repository.save(); + // test it restricts to the appropriate repository + assertEquals(Collections.singletonList(artifact), + repository.getArtifactsByDateRange( session, TEST_REPO_ID, null, null)); + assertEquals(Collections.singletonList(secondArtifact), + repository.getArtifactsByDateRange( session, OTHER_REPO_ID, null, null)); - // test it restricts to the appropriate repository - assertEquals( Collections.singletonList( artifact ), - repository.getArtifactsByDateRange( , TEST_REPO_ID, null, null ) ); - assertEquals( Collections.singletonList( secondArtifact ), - repository.getArtifactsByDateRange( , OTHER_REPO_ID, null, null ) ); + } } @Test public void testGetArtifactsByChecksumMultipleCopies() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); - - ArtifactMetadata secondArtifact = createArtifact(); - secondArtifact.setRepositoryId( OTHER_REPO_ID ); - repository.updateArtifact( , OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact ); - repository.save(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); + + ArtifactMetadata secondArtifact = createArtifact(); + secondArtifact.setRepositoryId(OTHER_REPO_ID); + repository.updateArtifact( session, OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact); + session.save(); + + // test it restricts to the appropriate repository + assertEquals(Collections.singletonList(artifact), + new ArrayList<>(repository.getArtifactsByChecksum( session, TEST_REPO_ID, TEST_SHA1))); + assertEquals(Collections.singletonList(secondArtifact), new ArrayList<>( + repository.getArtifactsByChecksum( session, OTHER_REPO_ID, TEST_SHA1))); + assertEquals(Collections.singletonList(artifact), + new ArrayList<>(repository.getArtifactsByChecksum( session, TEST_REPO_ID, TEST_MD5))); + assertEquals(Collections.singletonList(secondArtifact), + new ArrayList<>(repository.getArtifactsByChecksum( session, OTHER_REPO_ID, TEST_MD5))); - // test it restricts to the appropriate repository - assertEquals( Collections.singletonList( artifact ), - new ArrayList<>( repository.getArtifactsByChecksum( , TEST_REPO_ID, TEST_SHA1 ) ) ); - assertEquals( Collections.singletonList( secondArtifact ), new ArrayList<>( - repository.getArtifactsByChecksum( , OTHER_REPO_ID, TEST_SHA1 ) ) ); - assertEquals( Collections.singletonList( artifact ), - new ArrayList<>( repository.getArtifactsByChecksum( , TEST_REPO_ID, TEST_MD5 ) ) ); - assertEquals( Collections.singletonList( secondArtifact ), - new ArrayList<>( repository.getArtifactsByChecksum( , OTHER_REPO_ID, TEST_MD5 ) ) ); + } } @Test public void testGetNamespacesWithSparseDepth() - throws Exception - { - repository.updateNamespace( , TEST_REPO_ID, "org.apache.maven.shared" ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + repository.updateNamespace( session, TEST_REPO_ID, "org.apache.maven.shared"); + + Collection<String> namespaces = repository.getRootNamespaces( session, TEST_REPO_ID); - Collection<String> namespaces = repository.getRootNamespaces( , TEST_REPO_ID ); + assertThat(namespaces).isNotNull().isNotEmpty().hasSize(1).contains("org"); - assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "org" ); + namespaces = repository.getNamespaces( session, TEST_REPO_ID, "org"); + assertThat(namespaces).isNotNull().isNotEmpty().hasSize(1).contains("apache"); - namespaces = repository.getNamespaces( , TEST_REPO_ID, "org" ); - assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "apache" ); + namespaces = repository.getNamespaces( session, TEST_REPO_ID, "org.apache"); + assertThat(namespaces).isNotNull().isNotEmpty().hasSize(1).contains("maven"); - namespaces = repository.getNamespaces( , TEST_REPO_ID, "org.apache" ); - assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "maven" ); + namespaces = repository.getNamespaces( session, TEST_REPO_ID, "org.apache.maven"); + assertThat(namespaces).isNotNull().isNotEmpty().hasSize(1).contains("shared"); - namespaces = repository.getNamespaces( , TEST_REPO_ID, "org.apache.maven" ); - assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "shared" ); + } } @Test public void testGetNamespacesWithProjectsPresent() - throws Exception - { - String namespace = "org.apache.maven.shared"; - repository.updateNamespace( , TEST_REPO_ID, namespace ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + String namespace = "org.apache.maven.shared"; + repository.updateNamespace( session, TEST_REPO_ID, namespace); + + ProjectVersionMetadata metadata = new ProjectVersionMetadata(); + metadata.setId(TEST_PROJECT_VERSION); + repository.updateProjectVersion( session, TEST_REPO_ID, namespace, TEST_PROJECT, metadata); - ProjectVersionMetadata metadata = new ProjectVersionMetadata(); - metadata.setId( TEST_PROJECT_VERSION ); - repository.updateProjectVersion( , TEST_REPO_ID, namespace, TEST_PROJECT, metadata ); + Collection<String> namespaces = repository.getNamespaces( session, TEST_REPO_ID, namespace); - Collection<String> namespaces = repository.getNamespaces( , TEST_REPO_ID, namespace ); + assertThat(namespaces).isNotNull().isEmpty(); - assertThat( namespaces ).isNotNull().isEmpty(); + } } @Test public void testGetProjectsWithOtherNamespacesPresent() - throws Exception - { - ProjectMetadata projectMetadata = new ProjectMetadata(); - projectMetadata.setId( TEST_PROJECT ); - projectMetadata.setNamespace( "org.apache.maven" ); - repository.updateProject( , TEST_REPO_ID, projectMetadata ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ProjectMetadata projectMetadata = new ProjectMetadata(); + projectMetadata.setId(TEST_PROJECT); + projectMetadata.setNamespace("org.apache.maven"); + repository.updateProject( session, TEST_REPO_ID, projectMetadata); - repository.updateNamespace( , TEST_REPO_ID, "org.apache.maven.shared" ); + repository.updateNamespace( session, TEST_REPO_ID, "org.apache.maven.shared"); - Collection<String> projects = repository.getProjects( , TEST_REPO_ID, "org.apache.maven" ); + Collection<String> projects = repository.getProjects( session, TEST_REPO_ID, "org.apache.maven"); - assertThat( projects ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_PROJECT ); + assertThat(projects).isNotNull().isNotEmpty().hasSize(1).contains(TEST_PROJECT); + + } } @Test public void testGetProjectVersionsWithOtherNamespacesPresent() - throws Exception - { - // an unusual case but technically possible where a project namespace matches another project's name + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { +// an unusual case but technically possible where a project namespace matches another project's name + + ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata(); + versionMetadata.setId(TEST_PROJECT_VERSION); + repository.updateProjectVersion( session, TEST_REPO_ID, "org.apache.maven", TEST_PROJECT, versionMetadata); - ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata(); - versionMetadata.setId( TEST_PROJECT_VERSION ); - repository.updateProjectVersion( , TEST_REPO_ID, "org.apache.maven", TEST_PROJECT, versionMetadata ); + repository.updateProjectVersion( session, TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, + "other-project", versionMetadata); - repository.updateProjectVersion( , TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, - "other-project", versionMetadata ); + Collection<String> versions = + repository.getProjectVersions( session, TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, "other-project"); + assertThat(versions).isNotNull().isNotEmpty().contains(TEST_PROJECT_VERSION); - Collection<String> versions = - repository.getProjectVersions( , TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, "other-project" ); - assertThat( versions ).isNotNull().isNotEmpty().contains( TEST_PROJECT_VERSION ); + versions = repository.getProjectVersions( session, TEST_REPO_ID, "org.apache.maven", TEST_PROJECT); - versions = repository.getProjectVersions( , TEST_REPO_ID, "org.apache.maven", TEST_PROJECT ); + assertThat(versions).isNotNull().isNotEmpty().contains(TEST_PROJECT_VERSION); - assertThat( versions ).isNotNull().isNotEmpty().contains( TEST_PROJECT_VERSION ); + } } @Test public void testGetArtifactsByChecksumSingleResultMd5() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); - repository.save(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); + session.save(); - assertEquals( Collections.singletonList( artifact ), - new ArrayList<>( repository.getArtifactsByChecksum( , TEST_REPO_ID, TEST_MD5 ) ) ); + assertEquals(Collections.singletonList(artifact), + new ArrayList<>(repository.getArtifactsByChecksum( session, TEST_REPO_ID, TEST_MD5))); + + } } @Test public void testGetArtifactsByChecksumSingleResultSha1() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); - repository.save(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); + session.save(); + + assertEquals(Collections.singletonList(artifact), + new ArrayList<>(repository.getArtifactsByChecksum( session, TEST_REPO_ID, TEST_SHA1))); - assertEquals( Collections.singletonList( artifact ), - new ArrayList<>( repository.getArtifactsByChecksum( , TEST_REPO_ID, TEST_SHA1 ) ) ); + } } @Test public void testGetArtifactsByChecksumDeepNamespace() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - String namespace = "multi.level.ns"; - artifact.setNamespace( namespace ); - repository.updateArtifact( , TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); - repository.save(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + String namespace = "multi.level.ns"; + artifact.setNamespace(namespace); + repository.updateArtifact( session, TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); + session.save(); + + tryAssert(() -> assertEquals(Collections.singletonList(artifact), + new ArrayList<>(repository.getArtifactsByChecksum( session, TEST_REPO_ID, TEST_SHA1)))); + tryAssert(() -> assertEquals(Collections.singletonList(artifact), + new ArrayList<>(repository.getArtifactsByChecksum( session, TEST_REPO_ID, TEST_MD5)))); - tryAssert(()->assertEquals( Collections.singletonList( artifact ), - new ArrayList<>( repository.getArtifactsByChecksum( , TEST_REPO_ID, TEST_SHA1 ) ) )); - tryAssert(()->assertEquals( Collections.singletonList( artifact ), - new ArrayList<>( repository.getArtifactsByChecksum( , TEST_REPO_ID, TEST_MD5 ) ) ) ); + } } @Test public void testGetArtifactsByChecksumMultipleResult() - throws Exception - { - ArtifactMetadata artifact1 = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 ); - - String newProjectId = "another-project"; - ArtifactMetadata artifact2 = createArtifact(); - artifact2.setProject( newProjectId ); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 ); - repository.save(); - - tryAssert(()-> { - List<ArtifactMetadata> artifacts = - new ArrayList<>(repository.getArtifactsByChecksum( , TEST_REPO_ID, TEST_SHA1 )); - Collections.sort(artifacts, new ArtifactMetadataComparator()); - assertEquals(Arrays.asList(artifact2, artifact1), artifacts); - }); - - tryAssert(() -> { - ArrayList<ArtifactMetadata> artifacts = new ArrayList<>(repository.getArtifactsByChecksum( , TEST_REPO_ID, TEST_MD5 )); - Collections.sort(artifacts, new ArtifactMetadataComparator()); - assertEquals(Arrays.asList(artifact2, artifact1), artifacts); - }); + throws Exception { + + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact1 = createArtifact(); + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1); + + String newProjectId = "another-project"; + ArtifactMetadata artifact2 = createArtifact(); + artifact2.setProject(newProjectId); + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2); + session.save(); + + tryAssert(() -> { + List<ArtifactMetadata> artifacts = + new ArrayList<>(repository.getArtifactsByChecksum(session, TEST_REPO_ID, TEST_SHA1)); + Collections.sort(artifacts, new ArtifactMetadataComparator()); + assertEquals(Arrays.asList(artifact2, artifact1), artifacts); + }); + + tryAssert(() -> { + ArrayList<ArtifactMetadata> artifacts = new ArrayList<>(repository.getArtifactsByChecksum(session, TEST_REPO_ID, TEST_MD5)); + Collections.sort(artifacts, new ArtifactMetadataComparator()); + assertEquals(Arrays.asList(artifact2, artifact1), artifacts); + }); + } } @Test public void testGetArtifactsByChecksumNoResult() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); - Collection<ArtifactMetadata> artifactsByChecksum = - repository.getArtifactsByChecksum( , TEST_REPO_ID, "not checksum" ); - assertThat( artifactsByChecksum ).isNotNull().isEmpty(); + Collection<ArtifactMetadata> artifactsByChecksum = + repository.getArtifactsByChecksum( session, TEST_REPO_ID, "not checksum"); + assertThat(artifactsByChecksum).isNotNull().isEmpty(); + + } } @Test public void testGetArtifactsByProjectVersionMetadata() - throws Exception - { - createArtifactWithGenericMetadataFacet( 10 ); - Collection<ArtifactMetadata> artifactsByMetadata = - repository.getArtifactsByProjectVersionMetadata( , TEST_METADATA_KEY, TEST_METADATA_VALUE, TEST_REPO_ID ); - assertThat( artifactsByMetadata ).hasSize( 1 ); - ArtifactMetadata artifactMetadata = artifactsByMetadata.iterator().next(); - assertThat( artifactMetadata.getId() ).isEqualTo( "projectId-1.0.jar" ); - assertThat( artifactMetadata.getSha1() ).isEqualTo( TEST_SHA1 ); - assertThat( artifactMetadata.getRepositoryId() ).isEqualTo( TEST_REPO_ID ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + createArtifactWithGenericMetadataFacet( session,10); + Collection<ArtifactMetadata> artifactsByMetadata = + repository.getArtifactsByProjectVersionMetadata( session, TEST_METADATA_KEY, TEST_METADATA_VALUE, TEST_REPO_ID); + assertThat(artifactsByMetadata).hasSize(1); + ArtifactMetadata artifactMetadata = artifactsByMetadata.iterator().next(); + assertThat(artifactMetadata.getId()).isEqualTo("projectId-1.0.jar"); + assertThat(artifactMetadata.getSha1()).isEqualTo(TEST_SHA1); + assertThat(artifactMetadata.getRepositoryId()).isEqualTo(TEST_REPO_ID); + + } } @Test public void testGetArtifactsByProjectVersionMetadataNoRepository() - throws Exception - { - createArtifactWithGenericMetadataFacet(); - Collection<ArtifactMetadata> artifactsByMetadata = - repository.getArtifactsByProjectVersionMetadata( , TEST_METADATA_KEY, TEST_METADATA_VALUE, null ); - assertThat( artifactsByMetadata ).hasSize( 1 ); - assertThat( artifactsByMetadata.iterator().next().getRepositoryId() ).isNotNull().isNotEmpty(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + createArtifactWithGenericMetadataFacet(session); + Collection<ArtifactMetadata> artifactsByMetadata = + repository.getArtifactsByProjectVersionMetadata( session, TEST_METADATA_KEY, TEST_METADATA_VALUE, null); + assertThat(artifactsByMetadata).hasSize(1); + assertThat(artifactsByMetadata.iterator().next().getRepositoryId()).isNotNull().isNotEmpty(); + + } } @Test public void testGetArtifactsByProjectVersionMetadataAllRepositories() - throws Exception - { - createArtifactWithGenericMetadataFacet(); - Collection<ArtifactMetadata> artifactsByMetadata = - repository.getArtifactsByProjectVersionMetadata( , TEST_METADATA_KEY, TEST_METADATA_VALUE, null ); - assertThat( artifactsByMetadata ).hasSize( 1 ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + createArtifactWithGenericMetadataFacet(session); + Collection<ArtifactMetadata> artifactsByMetadata = + repository.getArtifactsByProjectVersionMetadata( session, TEST_METADATA_KEY, TEST_METADATA_VALUE, null); + assertThat(artifactsByMetadata).hasSize(1); + + } } @Test public void testGetArtifactsByMetadataAllRepositories() - throws Exception - { - createArtifactWithMavenArtifactFacet(); - tryAssert(()-> { - Collection<ArtifactMetadata> artifactsByMetadata = - repository.getArtifactsByMetadata( , "foo", TEST_METADATA_VALUE, null ); - assertThat(artifactsByMetadata).hasSize(1); - ArtifactMetadata artifactMetadata = artifactsByMetadata.iterator().next(); - assertThat(artifactMetadata.getId()).isEqualTo("projectId-1.0.jar"); - assertThat(artifactMetadata.getSha1()).isEqualTo(TEST_SHA1); - assertThat(artifactMetadata.getRepositoryId()).isEqualTo(TEST_REPO_ID); - MetadataFacet facet = artifactMetadata.getFacet(TEST_FACET_ID); - assertThat(facet).isNotNull(); - assertThat(facet.toProperties()).isEqualTo(Collections.singletonMap("foo", TEST_METADATA_VALUE)); - }); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + createArtifactWithMavenArtifactFacet(session); + tryAssert(() -> { + Collection<ArtifactMetadata> artifactsByMetadata = + repository.getArtifactsByMetadata(session, "foo", TEST_METADATA_VALUE, null); + assertThat(artifactsByMetadata).hasSize(1); + ArtifactMetadata artifactMetadata = artifactsByMetadata.iterator().next(); + assertThat(artifactMetadata.getId()).isEqualTo("projectId-1.0.jar"); + assertThat(artifactMetadata.getSha1()).isEqualTo(TEST_SHA1); + assertThat(artifactMetadata.getRepositoryId()).isEqualTo(TEST_REPO_ID); + MetadataFacet facet = artifactMetadata.getFacet(TEST_FACET_ID); + assertThat(facet).isNotNull(); + assertThat(facet.toProperties()).isEqualTo(Collections.singletonMap("foo", TEST_METADATA_VALUE)); + }); + } } @Test public void testGetArtifactsByPropertySingleResult() - throws Exception - { - createArtifactWithData(); - // only works on JCR implementation - // Collection<ArtifactMetadata> artifactsByProperty = repository.getArtifactsByProperty( "org.name", TEST_ORGANIZATION.getName(), TEST_REPO_ID ); - Collection<ArtifactMetadata> artifactsByProperty = repository.getArtifactsByProperty( , "url", TEST_URL, TEST_REPO_ID ); - assertThat( artifactsByProperty ).hasSize( 1 ); - ArtifactMetadata artifactMetadata = artifactsByProperty.iterator().next(); - assertThat( artifactMetadata.getId() ).isEqualTo( "projectId-1.0.jar" ); - assertThat( artifactMetadata.getSha1() ).isEqualTo( TEST_SHA1 ); - assertThat( artifactMetadata.getRepositoryId() ).isEqualTo( TEST_REPO_ID ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + createArtifactWithData(session); + // only works on JCR implementation + // Collection<ArtifactMetadata> artifactsByProperty = repository.getArtifactsByProperty( "org.name", TEST_ORGANIZATION.getName(), TEST_REPO_ID ); + Collection<ArtifactMetadata> artifactsByProperty = repository.getArtifactsByProperty( session, "url", TEST_URL, TEST_REPO_ID); + assertThat(artifactsByProperty).hasSize(1); + ArtifactMetadata artifactMetadata = artifactsByProperty.iterator().next(); + assertThat(artifactMetadata.getId()).isEqualTo("projectId-1.0.jar"); + assertThat(artifactMetadata.getSha1()).isEqualTo(TEST_SHA1); + assertThat(artifactMetadata.getRepositoryId()).isEqualTo(TEST_REPO_ID); + + } } @Test public void testDeleteRepository() - throws Exception - { - repository.updateNamespace( , TEST_REPO_ID, TEST_NAMESPACE ); - - ProjectMetadata project1 = new ProjectMetadata(); - project1.setNamespace( TEST_NAMESPACE ); - project1.setId( "project1" ); - repository.updateProject( , TEST_REPO_ID, project1 ); - ProjectMetadata project2 = new ProjectMetadata(); - project2.setNamespace( TEST_NAMESPACE ); - project2.setId( "project2" ); - repository.updateProject( , TEST_REPO_ID, project2 ); - - ArtifactMetadata artifact1 = createArtifact(); - artifact1.setProject( "project1" ); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, "project1", TEST_PROJECT_VERSION, artifact1 ); - ArtifactMetadata artifact2 = createArtifact(); - artifact2.setProject( "project2" ); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, "project2", TEST_PROJECT_VERSION, artifact2 ); - repository.save(); - - List<ArtifactMetadata> expected = Arrays.asList( artifact1, artifact2 ); - Collections.sort( expected, new ArtifactMetadataComparator() ); - - - tryAssert(()-> { - List<ArtifactMetadata> actual = - new ArrayList<>( repository.getArtifactsByDateRange( , TEST_REPO_ID, null, null ) ); - Collections.sort( actual, new ArtifactMetadataComparator() ); - assertEquals(expected, actual); - }); - - repository.removeRepository( , TEST_REPO_ID ); - - assertTrue( repository.getArtifacts( , TEST_REPO_ID ).isEmpty() ); - assertTrue( repository.getRootNamespaces( , TEST_REPO_ID ).isEmpty() ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + + repository.updateNamespace(session, TEST_REPO_ID, TEST_NAMESPACE); + + ProjectMetadata project1 = new ProjectMetadata(); + project1.setNamespace(TEST_NAMESPACE); + project1.setId("project1"); + repository.updateProject(session, TEST_REPO_ID, project1); + ProjectMetadata project2 = new ProjectMetadata(); + project2.setNamespace(TEST_NAMESPACE); + project2.setId("project2"); + repository.updateProject(session, TEST_REPO_ID, project2); + + ArtifactMetadata artifact1 = createArtifact(); + artifact1.setProject("project1"); + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, "project1", TEST_PROJECT_VERSION, artifact1); + ArtifactMetadata artifact2 = createArtifact(); + artifact2.setProject("project2"); + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, "project2", TEST_PROJECT_VERSION, artifact2); + session.save(); + + List<ArtifactMetadata> expected = Arrays.asList(artifact1, artifact2); + Collections.sort(expected, new ArtifactMetadataComparator()); + + + tryAssert(() -> { + List<ArtifactMetadata> actual = + new ArrayList<>(repository.getArtifactsByDateRange(session, TEST_REPO_ID, null, null)); + Collections.sort(actual, new ArtifactMetadataComparator()); + assertEquals(expected, actual); + }); + + repository.removeRepository(session, TEST_REPO_ID); + + assertTrue(repository.getArtifacts(session, TEST_REPO_ID).isEmpty()); + assertTrue(repository.getRootNamespaces(session, TEST_REPO_ID).isEmpty()); + } } @Test public void testDeleteArtifact() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - artifact.addFacet( new TestMetadataFacet( "value" ) ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + ArtifactMetadata artifact = createArtifact(); + artifact.addFacet(new TestMetadataFacet("value")); + + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); - assertThat( repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, - TEST_PROJECT, TEST_PROJECT_VERSION ) ).containsExactly( artifact ); + assertThat(repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, + TEST_PROJECT, TEST_PROJECT_VERSION)).containsExactly(artifact); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION_2_0, artifact ); + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION_2_0, artifact); - Collection<String> versions = repository.getProjectVersions( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ); + Collection<String> versions = repository.getProjectVersions(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT); - log.info( "versions {}", versions ); + log.info("versions {}", versions); - assertThat( versions ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "1.0", "2.0" ); + assertThat(versions).isNotNull().isNotEmpty().hasSize(2).contains("1.0", "2.0"); - repository.removeArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() ); + repository.removeArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId()); - versions = repository.getProjectVersions( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ); + versions = repository.getProjectVersions(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT); - log.info( "versions {}", versions ); + log.info("versions {}", versions); - assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "2.0" ); + assertThat(versions).isNotNull().isNotEmpty().hasSize(1).contains("2.0"); - assertThat( repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, - TEST_PROJECT, TEST_PROJECT_VERSION ) ).isNotNull().isEmpty(); + assertThat(repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, + TEST_PROJECT, TEST_PROJECT_VERSION)).isNotNull().isEmpty(); - assertThat( repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, - TEST_PROJECT, TEST_PROJECT_VERSION_2_0 ) ).isNotEmpty().hasSize( 1 ); + assertThat(repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, + TEST_PROJECT, TEST_PROJECT_VERSION_2_0)).isNotEmpty().hasSize(1); + } } @Test public void deleteArtifact() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - artifact.addFacet( new TestMetadataFacet( "value" ) ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + artifact.addFacet(new TestMetadataFacet("value")); + + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + Collection<ArtifactMetadata> artifacts = + repository.getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); - Collection<ArtifactMetadata> artifacts = - repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); + assertEquals(Collections.singletonList(artifact), new ArrayList<>(artifacts)); - assertEquals( Collections.singletonList( artifact ), new ArrayList<>( artifacts ) ); + repository.removeArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId()); - repository.removeArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() ); + artifacts = repository.getArtifacts( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); - artifacts = repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); + assertThat(artifacts).isNotNull().isEmpty(); - assertThat( artifacts ).isNotNull().isEmpty(); + } } @Test public void deleteVersion() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - artifact.addFacet( new TestMetadataFacet( "value" ) ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + artifact.addFacet(new TestMetadataFacet("value")); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); - Collection<String> versions = repository.getProjectVersions( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ); + Collection<String> versions = repository.getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT); - assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 ); + assertThat(versions).isNotNull().isNotEmpty().hasSize(1); - repository.removeProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); + repository.removeProjectVersion( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); - versions = repository.getProjectVersions( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ); + versions = repository.getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT); - assertThat( versions ).isNotNull().isEmpty(); + assertThat(versions).isNotNull().isEmpty(); + + } } @Test public void deleteProject() - throws Exception - { - ArtifactMetadata artifact = createArtifact(); - artifact.addFacet( new TestMetadataFacet( "value" ) ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + ArtifactMetadata artifact = createArtifact(); + artifact.addFacet(new TestMetadataFacet("value")); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + repository.updateArtifact( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); - assertEquals( 1, repository.getProjectVersions( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() ); + assertEquals(1, repository.getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT).size()); - repository.removeProject( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ); + repository.removeProject( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT); - Collection<String> versions = repository.getProjectVersions( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ); + Collection<String> versions = repository.getProjectVersions( session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT); - assertThat( versions ).isNotNull().isEmpty(); + assertThat(versions).isNotNull().isEmpty(); + + } } @Test public void deleteSnapshotVersion() - throws Exception - { - ArtifactMetadata artifactOne = createArtifact(); - artifactOne.setVersion( "2.0-20120618.214127-1" ); - artifactOne.setProjectVersion( "2.0-SNAPSHOT" ); - artifactOne.addFacet( new TestMetadataFacet( "value" ) ); - artifactOne.setId( TEST_PROJECT + "-" + "2.0-20120618.214127-1" + "." + "jar" ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + + ArtifactMetadata artifactOne = createArtifact(); + artifactOne.setVersion("2.0-20120618.214127-1"); + artifactOne.setProjectVersion("2.0-SNAPSHOT"); + artifactOne.addFacet(new TestMetadataFacet("value")); + artifactOne.setId(TEST_PROJECT + "-" + "2.0-20120618.214127-1" + "." + "jar"); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactOne ); + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactOne); - ArtifactMetadata artifactTwo = createArtifact(); - artifactTwo.setVersion( "2.0-20120618.214135-2" ); - artifactTwo.setProjectVersion( "2.0-SNAPSHOT" ); - artifactTwo.addFacet( new TestMetadataFacet( "value" ) ); - artifactTwo.setId( TEST_PROJECT + "-" + "2.0-20120618.214135-2" + "." + "jar" ); + ArtifactMetadata artifactTwo = createArtifact(); + artifactTwo.setVersion("2.0-20120618.214135-2"); + artifactTwo.setProjectVersion("2.0-SNAPSHOT"); + artifactTwo.addFacet(new TestMetadataFacet("value")); + artifactTwo.setId(TEST_PROJECT + "-" + "2.0-20120618.214135-2" + "." + "jar"); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactTwo ); + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactTwo); - Collection<ArtifactMetadata> artifactMetadatas = - repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" ); + Collection<ArtifactMetadata> artifactMetadatas = + repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT"); - assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 2 ); + assertThat(artifactMetadatas).isNotNull().isNotEmpty().hasSize(2); - log.info( "artifactMetadatas: {}", artifactMetadatas ); + log.info("artifactMetadatas: {}", artifactMetadatas); - repository.removeArtifact( , artifactOne, "2.0-SNAPSHOT" ); + repository.removeArtifact(session, artifactOne, "2.0-SNAPSHOT"); - artifactMetadatas = repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" ); + artifactMetadatas = repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT"); - assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 1 ); + assertThat(artifactMetadatas).isNotNull().isNotEmpty().hasSize(1); - repository.removeArtifact( , artifactTwo, "2.0-SNAPSHOT" ); + repository.removeArtifact(session, artifactTwo, "2.0-SNAPSHOT"); - artifactMetadatas = repository.getArtifacts( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" ); + artifactMetadatas = repository.getArtifacts(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT"); - assertThat( artifactMetadatas ).isNotNull().isEmpty(); + assertThat(artifactMetadatas).isNotNull().isEmpty(); + } } @Test public void testgetProjectReferences() - throws Exception - { - ProjectVersionMetadata metadata = new ProjectVersionMetadata(); - metadata.setId( TEST_PROJECT_VERSION ); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + + ProjectVersionMetadata metadata = new ProjectVersionMetadata(); + metadata.setId(TEST_PROJECT_VERSION); - metadata.setName( "project name" ); - metadata.setDescription( "project description" ); - metadata.setUrl( "the url" ); + metadata.setName("project name"); + metadata.setDescription("project description"); + metadata.setUrl("the url"); - Dependency d = new Dependency(); - d.setArtifactId( "artifactId" ); - d.setClassifier( "classifier" ); - d.setGroupId( "groupId" ); - d.setScope( "scope" ); - d.setSystemPath( "system path" ); - d.setType( "type" ); - d.setVersion( "version" ); - d.setOptional( true ); - metadata.addDependency( d ); + Dependency d = new Dependency(); + d.setArtifactId("artifactId"); + d.setClassifier("classifier"); + d.setGroupId("groupId"); + d.setScope("scope"); + d.setSystemPath("system path"); + d.setType("type"); + d.setVersion("version"); + d.setOptional(true); + metadata.addDependency(d); - d = new Dependency(); - d.setArtifactId( "artifactId1" ); - d.setClassifier( "classifier" ); - d.setGroupId( "groupId" ); - d.setScope( "scope" ); - d.setSystemPath( "system path" ); - d.setType( "type" ); - d.setVersion( "version1" ); - d.setOptional( true ); - metadata.addDependency( d ); + d = new Dependency(); + d.setArtifactId("artifactId1"); + d.setClassifier("classifier"); + d.setGroupId("groupId"); + d.setScope("scope"); + d.setSystemPath("system path"); + d.setType("type"); + d.setVersion("version1"); + d.setOptional(true); + metadata.addDependency(d); - repository.updateProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); + repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata); - repository.save(); + session.save(); - metadata = repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); + metadata = repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); - Collection<ProjectVersionReference> references = - repository.getProjectReferences( , TEST_REPO_ID, d.getGroupId(), d.getArtifactId(), d.getVersion() ); + Collection<ProjectVersionReference> references = + repository.getProjectReferences(session, TEST_REPO_ID, d.getGroupId(), d.getArtifactId(), d.getVersion()); - log.info( "references: {}", references ); + log.info("references: {}", references); - assertThat( references ).isNotNull().hasSize( 1 ).contains( - new ProjectVersionReference( ProjectVersionReference.ReferenceType.DEPENDENCY, TEST_PROJECT, TEST_NAMESPACE, - TEST_PROJECT_VERSION ) ); + assertThat(references).isNotNull().hasSize(1).contains( + new ProjectVersionReference(ProjectVersionReference.ReferenceType.DEPENDENCY, TEST_PROJECT, TEST_NAMESPACE, + TEST_PROJECT_VERSION)); + } } @Test public void testSearchArtifactsByKey() - throws Exception - { - createArtifactWithData(); - Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( , TEST_REPO_ID, "url", TEST_URL, false ); - assertThat( artifactsByProperty ).isNotNull().isNotEmpty(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + createArtifactWithData(session); + session.refreshAndDiscard(); + Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, "url", TEST_URL, false); + assertThat(artifactsByProperty).isNotNull().isNotEmpty(); + + } } @Test public void testSearchArtifactsByKeyExact() - throws Exception - { - createArtifactWithData(); - Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( , TEST_REPO_ID, "url", TEST_URL, true ); - assertThat( artifactsByProperty ).isNotNull().isNotEmpty(); - artifactsByProperty = repository.searchArtifacts( , TEST_REPO_ID, "org.name", "pache", true ); - assertThat( artifactsByProperty ).isNotNull().isEmpty(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + createArtifactWithData(session); + Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, "url", TEST_URL, true); + assertThat(artifactsByProperty).isNotNull().isNotEmpty(); + artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, "org.name", "pache", true); + assertThat(artifactsByProperty).isNotNull().isEmpty(); + + } } @Test public void testSearchArtifactsByFacetKey() - throws Exception - { - createArtifactWithGenericMetadataFacet(); - Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( , TEST_REPO_ID, TEST_METADATA_KEY, TEST_METADATA_VALUE, false ); - assertThat( artifactsByProperty ).isNotNull().isNotEmpty(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + createArtifactWithGenericMetadataFacet(session); + } + // Thread.currentThread().sleep(5000); + try (RepositorySession session = sessionFactory.createSession()) { + session.refresh(); + System.out.println(repository.getRootNamespaces(session, TEST_REPO_ID)); + + Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_KEY, TEST_METADATA_VALUE, false); + assertThat(artifactsByProperty).isNotNull().isNotEmpty(); + + } } @Test public void testSearchArtifactsByFacetKeyAllRepos() - throws Exception - { - createArtifactWithGenericMetadataFacet(); - tryAssert(()-> { - Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( , null, TEST_METADATA_KEY, TEST_METADATA_VALUE, false ); - assertThat( artifactsByProperty ).isNotNull().isNotEmpty(); - }); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + + createArtifactWithGenericMetadataFacet(session); + tryAssert(() -> { + Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts(session, null, TEST_METADATA_KEY, TEST_METADATA_VALUE, false); + assertThat(artifactsByProperty).isNotNull().isNotEmpty(); + }); + } } @Test public void testSearchArtifactsFullText() - throws Exception - { - createArtifactWithGenericMetadataFacet(); - // only works in JCR - // Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( TEST_URL, TEST_REPO_ID, false ); - Collection<ArtifactMetadata> artifactsByProperty = - repository.searchArtifacts( , TEST_REPO_ID, TEST_METADATA_VALUE, false ); - assertThat( artifactsByProperty ).isNotNull().isNotEmpty(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + createArtifactWithGenericMetadataFacet(session); + // only works in JCR + // Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( TEST_URL, TEST_REPO_ID, false ); + Collection<ArtifactMetadata> artifactsByProperty = + repository.searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_VALUE, false); + assertThat(artifactsByProperty).isNotNull().isNotEmpty(); + + } } @Test public void testSearchArtifactsFullTextExact() - throws Exception - { - createArtifactWithGenericMetadataFacet(); - // only works in JCR - // Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( TEST_URL, TEST_REPO_ID, true ); - Collection<ArtifactMetadata> artifactsByProperty = - repository.searchArtifacts( , TEST_REPO_ID, TEST_METADATA_VALUE, true ); - assertThat( artifactsByProperty ).isNotNull().isNotEmpty(); - artifactsByProperty = repository.searchArtifacts( , TEST_REPO_ID, TEST_METADATA_VALUE.substring( 2 ), true ); - assertThat( artifactsByProperty ).isNotNull().isEmpty(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + createArtifactWithGenericMetadataFacet(session); + // only works in JCR + // Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( TEST_URL, TEST_REPO_ID, true ); + Collection<ArtifactMetadata> artifactsByProperty = + repository.searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_VALUE, true); + assertThat(artifactsByProperty).isNotNull().isNotEmpty(); + artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_VALUE.substring(2), true); + assertThat(artifactsByProperty).isNotNull().isEmpty(); + + } } @Test public void testSearchArtifactsFullTextByFacet() - throws Exception - { - createArtifactWithGenericMetadataFacet(); - Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( , TEST_REPO_ID, TEST_METADATA_VALUE, false ); - assertThat( artifactsByProperty ).isNotNull().isNotEmpty(); + throws Exception { + try (RepositorySession session = sessionFactory.createSession()) { + createArtifactWithGenericMetadataFacet(session); + Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, TEST_METADATA_VALUE, false); + assertThat(artifactsByProperty).isNotNull().isNotEmpty(); + + } } - private static ProjectMetadata createProject() - { - return createProject( TEST_NAMESPACE ); + private static ProjectMetadata createProject() { + return createProject(TEST_NAMESPACE); } - private static ProjectMetadata createProject( String ns ) - { + private static ProjectMetadata createProject(String ns) { ProjectMetadata project = new ProjectMetadata(); - project.setId( TEST_PROJECT ); - project.setNamespace( ns ); + project.setId(TEST_PROJECT); + project.setNamespace(ns); return project; } - private void createArtifactWithGenericMetadataFacet() - throws MetadataRepositoryException, MetadataResolutionException - { - createArtifactWithGenericMetadataFacet( 1 ); + private void createArtifactWithGenericMetadataFacet(RepositorySession session) + throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException { + createArtifactWithGenericMetadataFacet( session,1); } - private void createArtifactWithGenericMetadataFacet( int artifacts ) - throws MetadataRepositoryException, MetadataResolutionException - { + private void createArtifactWithGenericMetadataFacet(RepositorySession session, int artifacts) + throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException { MetadataFacet metadataFacet = new GenericMetadataFacet(); Map<String, String> properties = new HashMap<>(); - properties.put( TEST_METADATA_KEY, TEST_METADATA_VALUE ); - metadataFacet.fromProperties( properties ); - createArtifactWithFacet( artifacts, null, metadataFacet ); + properties.put(TEST_METADATA_KEY, TEST_METADATA_VALUE); + metadataFacet.fromProperties(properties); + createArtifactWithFacet(session, artifacts, null, metadataFacet); } - private void createArtifactWithMavenArtifactFacet() - throws MetadataRepositoryException, MetadataResolutionException - { - createArtifactWithMavenArtifactFacet( 1 ); + private void createArtifactWithMavenArtifactFacet(RepositorySession session) + throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException { + createArtifactWithMavenArtifactFacet(session, 1); } - private void createArtifactWithMavenArtifactFacet( int artifacts ) - throws MetadataRepositoryException, MetadataResolutionException - { - TestMetadataFacet facet = new TestMetadataFacet( TEST_METADATA_VALUE ); - createArtifactWithFacet( artifacts, facet, null ); + private void createArtifactWithMavenArtifactFacet(RepositorySession session, int artifacts) + throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException { + TestMetadataFacet facet = new TestMetadataFacet(TEST_METADATA_VALUE); + createArtifactWithFacet(session, artifacts, facet, null); } - private void createArtifactWithFacet( int artifacts, MetadataFacet artifactFacet, - MetadataFacet projectVersionMetadataFacet ) - throws MetadataRepositoryException, MetadataResolutionException - { - for ( int i = 0; i < artifacts; i++ ) - { - ArtifactMetadata artifact = createArtifact(); - if ( artifactFacet != null ) - { - artifact.addFacet( artifactFacet ); + private void createArtifactWithFacet(RepositorySession session, int artifacts, MetadataFacet artifactFacet, + MetadataFacet projectVersionMetadataFacet) + throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException { + for (int i = 0; i < artifacts; i++) { + ArtifactMetadata artifact = createArtifact(); + if (artifactFacet != null) { + artifact.addFacet(artifactFacet); + } + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); } - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); - } - if ( projectVersionMetadataFacet != null ) - { - ProjectVersionMetadata metadata = - repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); - metadata.addFacet( projectVersionMetadataFacet ); - metadata.setOrganization( TEST_ORGANIZATION ); - metadata.setUrl( TEST_URL ); - repository.updateProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); - } - repository.save(); + if (projectVersionMetadataFacet != null) { + ProjectVersionMetadata metadata = + repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); + metadata.addFacet(projectVersionMetadataFacet); + metadata.setOrganization(TEST_ORGANIZATION); + metadata.setUrl(TEST_URL); + repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata); + } + session.save(); } - private void createArtifactWithData() - throws MetadataRepositoryException, MetadataResolutionException - { - ArtifactMetadata artifact = createArtifact(); - repository.updateArtifact( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); - ProjectVersionMetadata metadata = - repository.getProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ); - metadata.setOrganization( TEST_ORGANIZATION ); - metadata.setUrl( TEST_URL ); + private void createArtifactWithData(RepositorySession session) + throws MetadataRepositoryException, MetadataResolutionException, MetadataSessionException { + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact); + ProjectVersionMetadata metadata = + repository.getProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION); + metadata.setOrganization(TEST_ORGANIZATION); + metadata.setUrl(TEST_URL); - repository.updateProjectVersion( , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata ); - repository.save(); + repository.updateProjectVersion(session, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata); + session.save(); } - private static ArtifactMetadata createArtifact() - { - return createArtifact( "jar" ); + private static ArtifactMetadata createArtifact() { + return createArtifact("jar"); } - private static ArtifactMetadata createArtifact( String type ) - { + private static ArtifactMetadata createArtifact(String type) { ArtifactMetadata artifact = new ArtifactMetadata(); - artifact.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type ); - artifact.setWhenGathered( new Date() ); - artifact.setNamespace( TEST_NAMESPACE ); - artifact.setProject( TEST_PROJECT ); - artifact.setRepositoryId( TEST_REPO_ID ); - artifact.setFileLastModified( System.currentTimeMillis() ); - artifact.setVersion( TEST_PROJECT_VERSION ); - artifact.setProjectVersion( TEST_PROJECT_VERSION ); - artifact.setMd5( TEST_MD5 ); - artifact.setSha1( TEST_SHA1 ); + artifact.setId(TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type); + artifact.setWhenGathered(new Date()); + artifact.setNamespace(TEST_NAMESPACE); + artifact.setProject(TEST_PROJECT); + artifact.setRepositoryId(TEST_REPO_ID); + artifact.setFileLastModified(System.currentTimeMillis()); + artifact.setVersion(TEST_PROJECT_VERSION); + artifact.setProjectVersion(TEST_PROJECT_VERSION); + artifact.setMd5(TEST_MD5); + artifact.setSha1(TEST_SHA1); return artifact; } private static class ArtifactMetadataComparator - implements Comparator<ArtifactMetadata> - { + implements Comparator<ArtifactMetadata> { @Override - public final int compare( ArtifactMetadata a, ArtifactMetadata b ) - { - return a.getProject().compareTo( b.getProject() ); + public final int compare(ArtifactMetadata a, ArtifactMetadata b) { + return a.getProject().compareTo(b.getProject()); } } private static class KindOfRepositoryStatistics - implements MetadataFacet - { + implements MetadataFacet { private String value; private Date date; static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS"; - private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" ); + private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC"); - private KindOfRepositoryStatistics( String value, Date date ) - { + private KindOfRepositoryStatistics(String value, Date date) { this.value = value; this.date = date; } @Override - public String getName() - { - return createNameFormat().format( date ); + public String getName() { + return createNameFormat().format(date); } - private static SimpleDateFormat createNameFormat() - { - SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT ); - fmt.setTimeZone( UTC_TIME_ZONE ); + private static SimpleDateFormat createNameFormat() { + SimpleDateFormat fmt = new SimpleDateFormat(SCAN_TIMESTAMP_FORMAT); + fmt.setTimeZone(UTC_TIME_ZONE); return fmt; } @Override - public String getFacetId() - { + public String getFacetId() { return KindOfRepositoryStatistics.class.getName(); } @Override - public Map<String, String> toProperties() - { + public Map<String, String> toProperties() { return Collections.emptyMap(); } @Override - public void fromProperties( Map<String, String> properties ) - { + public void fromProperties(Map<String, String> properties) { // no op } } private static class TestMetadataFacet - implements MetadataFacet - { + implements MetadataFacet { private String testFacetId; private Map<String, String> additionalProps; private String value; - private TestMetadataFacet( String value ) - { + private TestMetadataFacet(String value) { this.value = value; testFacetId = TEST_FACET_ID; } - private TestMetadataFacet( String facetId, String value ) - { + private TestMetadataFacet(String facetId, String value) { this.value = value; testFacetId = facetId; } - private TestMetadataFacet( String facetId, String value, Map<String, String> additionalProps ) - { - this( facetId, value ); + private TestMetadataFacet(String facetId, String value, Map<String, String> additionalProps) { + this(facetId, value); this.additionalProps = additionalProps; } @Override - public String getFacetId() - { + public String getFacetId() { return testFacetId; } @Override - public String getName() - { + public String getName() { return TEST_NAME; } @Override - public Map<String, String> toProperties() - { - if ( value != null ) - { - if ( additionalProps == null ) - { - return Collections.singletonMap( "foo", value ); - } - else - { + public Map<String, String> toProperties() { + if (value != null) { + if (additionalProps == null) { + return Collections.singletonMap("foo", value); + } else { Map<String, String> props = new HashMap<>(); - props.put( "foo", value ); + props.put("foo", value); - for ( String key : additionalProps.keySet() ) - { - props.put( key, additionalProps.get( key ) ); + for (String key : additionalProps.keySet()) { + props.put(key, additionalProps.get(key)); } return props; } - } - else - { + } else { return Collections.emptyMap(); } } @Override - public void fromProperties( Map<String, String> properties ) - { - String value = properties.get( "foo" ); - if ( value != null ) - { + public void fromProperties(Map<String, String> properties) { + String value = properties.get("foo"); + if (value != null) { this.value = value; } - properties.remove( "foo" ); + properties.remove("foo"); - if ( additionalProps == null ) - { + if (additionalProps == null) { additionalProps = new HashMap<>(); } - for ( String key : properties.keySet() ) - { - additionalProps.put( key, properties.get( key ) ); + for (String key : properties.keySet()) { + additionalProps.put(key, properties.get(key)); } } - public String getValue() - { + public String getValue() { return value; } @Override - public String toString() - { + public String toString() { return "TestMetadataFacet{" + "value='" + value + '\'' + '}'; } @Override - public boolean equals( Object o ) - { - if ( this == o ) - { + public boolean equals(Object o) { + if (this == o) { return true; } - if ( o == null || getClass() != o.getClass() ) - { + if (o == null || getClass() != o.getClass()) { return false; } TestMetadataFacet that = (TestMetadataFacet) o; - if ( value != null ? !value.equals( that.value ) : that.value != null ) - { + if (value != null ? !value.equals(that.value) : that.value != null) { return false; } @@ -1900,8 +1955,7 @@ public abstract class AbstractMetadataRepositoryTest } @Override - public int hashCode() - { + public int hashCode() { return value != null ? value.hashCode() : 0; } } |