3232import org .dspace .content .factory .ContentServiceFactory ;
3333import org .dspace .content .service .ItemService ;
3434import org .dspace .content .service .RelationshipService ;
35+ import org .dspace .identifier .factory .IdentifierServiceFactory ;
36+ import org .dspace .identifier .service .IdentifierService ;
3537import org .dspace .services .ConfigurationService ;
3638import org .dspace .services .factory .DSpaceServicesFactory ;
3739import org .flywaydb .core .internal .util .ExceptionUtils ;
@@ -55,6 +57,7 @@ public class ItemImportCLIIT extends AbstractIntegrationTestWithDatabase {
5557 private ItemService itemService = ContentServiceFactory .getInstance ().getItemService ();
5658 private RelationshipService relationshipService = ContentServiceFactory .getInstance ().getRelationshipService ();
5759 private ConfigurationService configurationService = DSpaceServicesFactory .getInstance ().getConfigurationService ();
60+ private IdentifierService identifierService = IdentifierServiceFactory .getInstance ().getIdentifierService ();
5861 private Collection collection ;
5962 private Path tempDir ;
6063 private Path workDir ;
@@ -114,6 +117,26 @@ public void importItemBySafWithMetadataOnly() throws Exception {
114117 checkMetadata ();
115118 }
116119
120+ @ Test
121+ public void importItemBySafWithExistingHandle () throws Exception {
122+ // create simple SAF
123+ Path safDir = Files .createDirectory (Path .of (tempDir .toString () + "/test" ));
124+ Path itemDir = Files .createDirectory (Path .of (safDir .toString () + "/item_000" ));
125+ Files .copy (getClass ().getResourceAsStream ("dublin_core.xml" ),
126+ Path .of (itemDir + "/dublin_core.xml" ));
127+ Files .copy (getClass ().getResourceAsStream ("handle" ),
128+ Path .of (itemDir + "/handle" ));
129+
130+ String [] args = new String [] { "import" , "-a" , "-e" , admin .getEmail (), "-c" , collection .getID ().toString (),
131+ "-s" , safDir .toString (), "-m" , tempDir .toString () + "/mapfile.out" };
132+ perfomImportScript (args );
133+
134+ Item item = findItemByTitle (publicationTitle );
135+ assertEquals ("123456789/3900021-03" , item .getHandle ());
136+ checkMetadata ();
137+ identifierService .delete (context , item , item .getHandle ());
138+ }
139+
117140 @ Test
118141 public void importItemBySafWithBitstreams () throws Exception {
119142 // create simple SAF
@@ -549,7 +572,8 @@ public void deleteItemByMapFile() throws Exception {
549572 * @throws Exception
550573 */
551574 private void checkMetadata () throws Exception {
552- Item item = itemService .findByMetadataField (context , "dc" , "title" , null , publicationTitle ).next ();
575+ Item item = findItemByTitle (publicationTitle );
576+ assertEquals (1 , item .getHandles ().size ());
553577 assertEquals (item .getName (), publicationTitle );
554578 assertEquals (itemService .getMetadata (item , "dc.date.issued" ), "1990" );
555579 assertEquals (itemService .getMetadata (item , "dc.title.alternative" ), "J'aime les Printemps" );
@@ -560,7 +584,7 @@ private void checkMetadata() throws Exception {
560584 * @throws Exception
561585 */
562586 private void checkMetadataWithAnotherSchema () throws Exception {
563- Item item = itemService . findByMetadataField ( context , "dc" , "title" , null , publicationTitle ). next ( );
587+ Item item = findItemByTitle ( publicationTitle );
564588 assertEquals (item .getName (), publicationTitle );
565589 assertEquals (itemService .getMetadata (item , "dcterms.title" ), publicationTitle );
566590 }
@@ -570,8 +594,7 @@ private void checkMetadataWithAnotherSchema() throws Exception {
570594 * @throws Exception
571595 */
572596 private void checkBitstream () throws Exception {
573- Bitstream bitstream = itemService .findByMetadataField (context , "dc" , "title" , null , publicationTitle ).next ()
574- .getBundles ("ORIGINAL" ).get (0 ).getBitstreams ().get (0 );
597+ Bitstream bitstream = findItemByTitle (publicationTitle ).getBundles ("ORIGINAL" ).get (0 ).getBitstreams ().get (0 );
575598 assertEquals (bitstream .getName (), "file1.txt" );
576599 }
577600
@@ -589,8 +612,8 @@ private void checkItemDeletion() throws Exception {
589612 * @throws Exception
590613 */
591614 private void checkRelationship () throws Exception {
592- Item item = itemService . findByMetadataField ( context , "dc" , "title" , null , publicationTitle ). next ( );
593- Item author = itemService . findByMetadataField ( context , "dc" , "title" , null , personTitle ). next ( );
615+ Item item = findItemByTitle ( publicationTitle );
616+ Item author = findItemByTitle ( personTitle );
594617 List <Relationship > relationships = relationshipService .findByItem (context , item );
595618 assertEquals (1 , relationships .size ());
596619 assertEquals (author .getID (), relationships .get (0 ).getRightItem ().getID ());
@@ -601,4 +624,8 @@ private void perfomImportScript(String[] args)
601624 throws Exception {
602625 runDSpaceScript (args );
603626 }
627+
628+ private Item findItemByTitle (String title ) throws Exception {
629+ return itemService .findByMetadataField (context , "dc" , "title" , null , title ).next ();
630+ }
604631}
0 commit comments