forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathDOIIdentifierProviderTest.java
More file actions
947 lines (821 loc) · 40.1 KB
/
Copy pathDOIIdentifierProviderTest.java
File metadata and controls
947 lines (821 loc) · 40.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.identifier;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeNotNull;
import static org.mockito.Mockito.mock;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dspace.AbstractUnitTest;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.WorkspaceItem;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.logic.DefaultFilter;
import org.dspace.content.logic.LogicalStatement;
import org.dspace.content.logic.TrueFilter;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.WorkspaceItemService;
import org.dspace.identifier.doi.DOIConnector;
import org.dspace.identifier.doi.DOIIdentifierException;
import org.dspace.identifier.doi.DOIIdentifierNotApplicableException;
import org.dspace.identifier.factory.IdentifierServiceFactory;
import org.dspace.identifier.service.DOIService;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.dspace.workflow.WorkflowException;
import org.dspace.workflow.WorkflowItem;
import org.dspace.workflow.factory.WorkflowServiceFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Tests for {@link DOIIdentifierProvider}.
*
* @author Mark H. Wood
* @author Pascal-Nicolas Becker
*/
public class DOIIdentifierProviderTest
extends AbstractUnitTest {
/**
* log4j category
*/
private static final Logger log = LogManager.getLogger(DOIIdentifierProviderTest.class);
private static final String PREFIX = "10.5072";
private static final String NAMESPACE_SEPARATOR = "dspaceUnitTests-";
private static ConfigurationService config = null;
protected DOIService doiService = IdentifierServiceFactory.getInstance().getDOIService();
protected CommunityService communityService = ContentServiceFactory.getInstance().getCommunityService();
protected CollectionService collectionService = ContentServiceFactory.getInstance().getCollectionService();
protected ItemService itemService = ContentServiceFactory.getInstance().getItemService();
protected WorkspaceItemService workspaceItemService = ContentServiceFactory.getInstance().getWorkspaceItemService();
private static Community community;
private static Collection collection;
private static DOIConnector connector;
private DOIIdentifierProvider provider;
public DOIIdentifierProviderTest() {
}
/**
* This method will be run before every test as per @Before. It will
* initialize resources required for the tests.
*
* Other methods can be annotated with @Before here or in subclasses
* but no execution order is guaranteed
*/
@Before
@Override
public void init() {
super.init();
try {
context.turnOffAuthorisationSystem();
// Create an environment for our test objects to live in.
community = communityService.create(null, context);
communityService.setMetadataSingleValue(context, community,
CommunityService.MD_NAME, null, "A Test Community");
communityService.update(context, community);
collection = collectionService.create(context, community);
collectionService.setMetadataSingleValue(context, collection,
CollectionService.MD_NAME, null, "A Test Collection");
collectionService.update(context, collection);
//we need to commit the changes so we don't block the table for testing
context.restoreAuthSystemState();
config = DSpaceServicesFactory.getInstance().getConfigurationService();
// Configure the service under test.
config.setProperty(DOIIdentifierProvider.CFG_PREFIX, PREFIX);
config.setProperty(DOIIdentifierProvider.CFG_NAMESPACE_SEPARATOR,
NAMESPACE_SEPARATOR);
connector = mock(DOIConnector.class);
provider = new DOIIdentifierProvider();
provider.doiService = doiService;
provider.contentServiceFactory = ContentServiceFactory.getInstance();
provider.itemService = itemService;
provider.setConfigurationService(config);
provider.setDOIConnector(connector);
provider.setFilter(null);
} catch (AuthorizeException ex) {
log.error("Authorization Error in init", ex);
fail("Authorization Error in init: " + ex.getMessage());
} catch (SQLException ex) {
log.error("SQL Error in init", ex);
fail("SQL Error in init: " + ex.getMessage());
}
}
/**
* This method will be run after every test as per @After. It will
* clean resources initialized by the @Before methods.
*
* Other methods can be annotated with @After here or in subclasses
* but no execution order is guaranteed
*/
@After
@Override
public void destroy() {
community = null;
collection = null;
connector = null;
provider = null;
super.destroy();
}
/**
* Create a fresh Item, installed in the repository.
*
* @throws SQLException if database error
* @throws AuthorizeException if authorization error
* @throws IOException if IO error
*/
private Item newItem()
throws SQLException, AuthorizeException, IOException, IllegalAccessException, IdentifierException,
WorkflowException {
context.turnOffAuthorisationSystem();
WorkspaceItem wsItem = workspaceItemService.create(context, collection, false);
WorkflowItem wfItem = WorkflowServiceFactory.getInstance().getWorkflowService().start(context, wsItem);
Item item = wfItem.getItem();
itemService.addMetadata(context, item, "dc", "contributor", "author", null, "Author, A. N.");
itemService.addMetadata(context, item, "dc", "title", null, null, "A Test Object");
itemService.addMetadata(context, item, "dc", "publisher", null, null, "DSpace Test Harness");
// If DOIIdentifierProvider is configured
// (dspace/conf/spring/api/identifier-service.xml) the new created item
// gets automatically a DOI. We remove this DOI as it can make problems
// with the tests.
provider.delete(context, item);
List<MetadataValue> metadata = itemService.getMetadata(item,
DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
null);
List<String> remainder = new ArrayList<>();
for (MetadataValue id : metadata) {
if (!id.getValue().startsWith(doiService.getResolver())) {
remainder.add(id.getValue());
}
}
itemService.clearMetadata(context, item,
DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
null);
// There could be scenario when the `dc.identifier.doi` metadata field is empty, so do not try to remove it
if (CollectionUtils.isNotEmpty(remainder)) {
itemService.addMetadata(context, item, DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
null,
remainder);
}
itemService.update(context, item);
//we need to commit the changes so we don't block the table for testing
context.restoreAuthSystemState();
return item;
}
public String createDOI(Item item, Integer status, boolean metadata)
throws SQLException, IdentifierException, AuthorizeException {
return this.createDOI(item, status, metadata, null);
}
/**
* Create a DOI to an item.
*
* @param item Item the DOI should be created for.
* @param status The status of the DOI.
* @param metadata Whether the DOI should be included in the metadata of the item.
* @param doi The DOI or null if we should generate one.
* @return the DOI
* @throws SQLException if database error
* @throws org.dspace.identifier.IdentifierException passed through.
* @throws org.dspace.authorize.AuthorizeException passed through.
*/
public String createDOI(Item item, Integer status, boolean metadata, String doi)
throws SQLException, IdentifierException, AuthorizeException {
context.turnOffAuthorisationSystem();
// we need some random data. UUIDs would be bloated here
Random random = new Random();
if (null == doi) {
doi = DOI.SCHEME + PREFIX + "/" + NAMESPACE_SEPARATOR
+ Long.toHexString(new Date().getTime()) + "-"
+ random.nextInt(997);
}
DOI doiRow = doiService.create(context);
doiRow.setDoi(doi.substring(DOI.SCHEME.length()));
doiRow.setDSpaceObject(item);
doiRow.setStatus(status);
doiService.update(context, doiRow);
if (metadata) {
itemService.addMetadata(context, item, DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
null,
doiService.DOIToExternalForm(doi));
itemService.update(context, item);
}
//we need to commit the changes so we don't block the table for testing
context.restoreAuthSystemState();
return doi;
}
/**
* Test of supports method, of class DataCiteIdentifierProvider.
*/
@Test
public void testSupports_Class() {
Class<? extends Identifier> identifier = DOI.class;
assertTrue("DOI should be supported", provider.supports(identifier));
}
@Test
public void testSupports_valid_String() {
String[] validDOIs = new String[] {
"10.5072/123abc-lkj/kljl",
PREFIX + "/" + NAMESPACE_SEPARATOR + "lkjljasd1234",
DOI.SCHEME + "10.5072/123abc-lkj/kljl",
"http://dx.doi.org/10.5072/123abc-lkj/kljl",
doiService.getResolver() + "/10.5072/123abc-lkj/kljl"
};
for (String doi : validDOIs) {
assertTrue("DOI " + doi + " should be supported", provider.supports(doi));
}
}
@Test
public void testDoes_not_support_invalid_String() {
String[] invalidDOIs = new String[] {
"11.5072/123abc-lkj/kljl",
"http://hdl.handle.net/handle/10.5072/123abc-lkj/kljl",
"",
null
};
for (String notADoi : invalidDOIs) {
assertFalse("Invalid DOIs shouldn't be supported",
provider.supports(notADoi));
}
}
@Test
public void testStore_DOI_as_item_metadata()
throws SQLException, AuthorizeException, IOException, IdentifierException, IllegalAccessException,
WorkflowException {
Item item = newItem();
String doi = DOI.SCHEME + PREFIX + "/" + NAMESPACE_SEPARATOR
+ Long.toHexString(new Date().getTime());
context.turnOffAuthorisationSystem();
provider.saveDOIToObject(context, item, doi);
context.restoreAuthSystemState();
List<MetadataValue> metadata = itemService.getMetadata(item, DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
null);
boolean result = false;
for (MetadataValue id : metadata) {
if (id.getValue().equals(doiService.DOIToExternalForm(doi))) {
result = true;
}
}
assertTrue("Cannot store DOI as item metadata value.", result);
}
@Test
public void testStore_DOI_keeps_existing_different_doi_metadata() throws SQLException, AuthorizeException,
IOException, IdentifierException, IllegalAccessException, WorkflowException {
Item item = newItem();
// this checks that the method does not fail if there is already a *different* DOI in the metadata,
// here we verify that the existing DOI is preserved (not deleted) and the new one is added alongside it.
// Items with more than one DOI are reported by the ItemMetadataQAChecker curation task, not silently
// cleaned up here.
String oldDoi = DOI.SCHEME + PREFIX + "/" + NAMESPACE_SEPARATOR + "1234";
String newDoi = DOI.SCHEME + PREFIX + "/" + NAMESPACE_SEPARATOR + Long.toHexString(new Date().getTime());
context.turnOffAuthorisationSystem();
itemService.addMetadata(context, item, DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
null,
doiService.DOIToExternalForm(oldDoi));
provider.saveDOIToObject(context, item, newDoi);
context.restoreAuthSystemState();
checkDoiMetadata(item, oldDoi, newDoi);
}
@Test
public void testStore_DOI_check_single_doi_metadata() throws SQLException, AuthorizeException, IOException,
IdentifierException, IllegalAccessException, WorkflowException {
Item item = newItem();
// this checks that the method does not fail if there is already a DOI in the metadata,
// here we check if DOI metadata are not duplicated
String doi = DOI.SCHEME + PREFIX + "/" + NAMESPACE_SEPARATOR + Long.toHexString(new Date().getTime());
context.turnOffAuthorisationSystem();
itemService.addMetadata(context, item, DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
null,
doiService.DOIToExternalForm(doi));
provider.saveDOIToObject(context, item, doi);
context.restoreAuthSystemState();
checkSingleDoiMetadata(item, doi);
}
@Test
public void testGet_DOI_out_of_item_metadata()
throws SQLException, AuthorizeException, IOException, IdentifierException, IllegalAccessException,
WorkflowException {
Item item = newItem();
String doi = DOI.SCHEME + PREFIX + "/" + NAMESPACE_SEPARATOR
+ Long.toHexString(new Date().getTime());
context.turnOffAuthorisationSystem();
itemService.addMetadata(context, item, DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
null,
doiService.DOIToExternalForm(doi));
itemService.update(context, item);
context.restoreAuthSystemState();
assertEquals("Failed to recognize DOI in item metadata.",
doi, provider.getDOIOutOfObject(item));
}
@Test
public void testRemove_DOI_from_item_metadata()
throws SQLException, AuthorizeException, IOException, IdentifierException, WorkflowException,
IllegalAccessException {
Item item = newItem();
String doi = DOI.SCHEME + PREFIX + "/" + NAMESPACE_SEPARATOR
+ Long.toHexString(new Date().getTime());
context.turnOffAuthorisationSystem();
itemService.addMetadata(context, item, DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
null,
doiService.DOIToExternalForm(doi));
itemService.update(context, item);
provider.removeDOIFromObject(context, item, doi);
context.restoreAuthSystemState();
List<MetadataValue> metadata = itemService.getMetadata(item, DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
null);
boolean foundDOI = false;
for (MetadataValue id : metadata) {
if (id.getValue().equals(doiService.DOIToExternalForm(doi))) {
foundDOI = true;
}
}
assertFalse("Cannot remove DOI from item metadata.", foundDOI);
}
@Test
public void testGet_DOI_by_DSpaceObject()
throws SQLException, AuthorizeException, IOException,
IllegalArgumentException, IdentifierException, WorkflowException, IllegalAccessException {
Item item = newItem();
String doi = this.createDOI(item, DOIIdentifierProvider.IS_REGISTERED, false);
String retrievedDOI = provider.getDOIByObject(context, item);
assertNotNull("Failed to load DOI by DSpaceObject.", retrievedDOI);
assertTrue("Loaded wrong DOI by DSpaceObject.", doi.equals(retrievedDOI));
}
@Test
public void testGet_DOI_lookup()
throws SQLException, AuthorizeException, IOException,
IllegalArgumentException, IdentifierException, WorkflowException, IllegalAccessException {
Item item = newItem();
String doi = this.createDOI(item, DOIIdentifierProvider.IS_REGISTERED, false);
String retrievedDOI = provider.lookup(context, (DSpaceObject) item);
assertNotNull("Failed to loookup doi.", retrievedDOI);
assertTrue("Loaded wrong DOI on lookup.", doi.equals(retrievedDOI));
}
@Test
public void testGet_DSpaceObject_by_DOI()
throws SQLException, AuthorizeException, IOException,
IllegalArgumentException, IdentifierException, WorkflowException, IllegalAccessException {
Item item = newItem();
String doi = this.createDOI(item, DOIIdentifierProvider.IS_REGISTERED, false);
DSpaceObject dso = provider.getObjectByDOI(context, doi);
assertNotNull("Failed to load DSpaceObject by DOI.", dso);
if (item.getType() != dso.getType() || ObjectUtils.notEqual(item.getID(), dso.getID())) {
fail("Object loaded by DOI was another object then expected!");
}
}
@Test
public void testResolve_DOI()
throws SQLException, AuthorizeException, IOException,
IllegalArgumentException, IdentifierException, WorkflowException, IllegalAccessException {
Item item = newItem();
String doi = this.createDOI(item, DOIIdentifierProvider.IS_REGISTERED, false);
DSpaceObject dso = provider.resolve(context, doi);
assertNotNull("Failed to resolve DOI.", dso);
if (item.getType() != dso.getType() || ObjectUtils.notEqual(item.getID(), dso.getID())) {
fail("Object return by DOI lookup was another object then expected!");
}
}
/*
* The following test seems a bit silly, but it was helpful to debug some
* problems while deleting DOIs.
*/
@Test
public void testRemove_two_DOIs_from_item_metadata()
throws SQLException, AuthorizeException, IOException, IdentifierException, WorkflowException,
IllegalAccessException {
// add two DOIs.
Item item = newItem();
String doi1 = this.createDOI(item, DOIIdentifierProvider.IS_REGISTERED, true);
String doi2 = this.createDOI(item, DOIIdentifierProvider.IS_REGISTERED, true);
// remove one of it
context.turnOffAuthorisationSystem();
provider.removeDOIFromObject(context, item, doi1);
context.restoreAuthSystemState();
// assure that the right one was removed
List<MetadataValue> metadata = itemService.getMetadata(item, DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
null);
boolean foundDOI1 = false;
boolean foundDOI2 = false;
for (MetadataValue id : metadata) {
if (id.getValue().equals(doiService.DOIToExternalForm(doi1))) {
foundDOI1 = true;
}
if (id.getValue().equals(doiService.DOIToExternalForm(doi2))) {
foundDOI2 = true;
}
}
assertFalse("Cannot remove DOI from item metadata.", foundDOI1);
assertTrue("Removed wrong DOI from item metadata.", foundDOI2);
// remove the otherone as well.
context.turnOffAuthorisationSystem();
provider.removeDOIFromObject(context, item, doi2);
context.restoreAuthSystemState();
// check it
metadata = itemService.getMetadata(item, DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
null);
foundDOI1 = false;
foundDOI2 = false;
for (MetadataValue id : metadata) {
if (id.getValue().equals(doiService.DOIToExternalForm(doi1))) {
foundDOI1 = true;
}
if (id.getValue().equals(doiService.DOIToExternalForm(doi2))) {
foundDOI2 = true;
}
}
assertFalse("Cannot remove DOI from item metadata.", foundDOI1);
assertFalse("Cannot remove DOI from item metadata.", foundDOI2);
}
@Test
public void testMintDOI()
throws SQLException, AuthorizeException, IOException, IllegalAccessException, IdentifierException,
WorkflowException {
Item item = newItem();
String doi = null;
try {
// get a DOI (skipping any filters)
doi = provider.mint(context, item);
} catch (IdentifierException e) {
e.printStackTrace(System.err);
fail("Got an IdentifierException: " + e.getMessage());
}
assertNotNull("Minted DOI is null!", doi);
assertFalse("Minted DOI is empty!", doi.isEmpty());
try {
doiService.formatIdentifier(doi);
} catch (DOIIdentifierException e) {
e.printStackTrace(System.err);
fail("Minted an unrecognizable DOI: " + e.getMessage());
}
}
@Test
public void testMint_returns_existing_DOI()
throws SQLException, AuthorizeException, IOException, IdentifierException, WorkflowException,
IllegalAccessException {
Item item = newItem();
String doi = this.createDOI(item, null, true);
String retrievedDOI = provider.mint(context, item);
assertNotNull("Minted DOI is null?!", retrievedDOI);
assertEquals("Mint did not returned an existing DOI!", doi, retrievedDOI);
}
/**
* Test minting a DOI with a filter that always returns false and therefore never mints the DOI
*/
@Test
public void testMint_DOI_withNonMatchingFilter()
throws SQLException, AuthorizeException, IOException, IllegalAccessException, IdentifierException,
WorkflowException {
Item item = newItem();
boolean wasFiltered = false;
try {
// Mint this with the filter
DefaultFilter doiFilter = new DefaultFilter();
LogicalStatement alwaysFalse = (context, i) -> false;
doiFilter.setStatement(alwaysFalse);
// get a DOI with the method that applies filters by default
provider.mint(context, item, doiFilter);
} catch (DOIIdentifierNotApplicableException e) {
// This is what we wanted to see - we can return safely
wasFiltered = true;
} catch (IdentifierException e) {
e.printStackTrace();
fail("Got an IdentifierException: " + e.getMessage());
}
// Fail the test if the filter didn't throw a "not applicable" exception
assertTrue("DOI minting attempt was not filtered by filter service", wasFiltered);
}
/**
* Test minting a DOI with a filter that always returns true and therefore allows the DOI to be minted
* (this should have hte same results as base testMint_DOI, but here we use an explicit filter rather than null)
*/
@Test
public void testMint_DOI_withMatchingFilter()
throws SQLException, AuthorizeException, IOException, IllegalAccessException, IdentifierException,
WorkflowException {
Item item = newItem();
String doi = null;
boolean wasFiltered = false;
try {
// Temporarily set the provider to have a filter that always returns true for an item
// (therefore, the item is allowed to have a DOI minted)
DefaultFilter doiFilter = new DefaultFilter();
LogicalStatement alwaysTrue = (context, i) -> true;
doiFilter.setStatement(alwaysTrue);
// get a DOI with the method that applies filters by default
doi = provider.mint(context, item, doiFilter);
} catch (DOIIdentifierNotApplicableException e) {
// This is what we wanted to see - we can return safely
wasFiltered = true;
} catch (IdentifierException e) {
e.printStackTrace();
fail("Got an IdentifierException: " + e.getMessage());
}
// If the attempt was filtered, fail
assertFalse("DOI minting attempt was incorrectly filtered by filter service", wasFiltered);
// Continue with regular minting tests
assertNotNull("Minted DOI is null!", doi);
assertFalse("Minted DOI is empty!", doi.isEmpty());
try {
doiService.formatIdentifier(doi);
} catch (Exception e) {
e.printStackTrace();
fail("Minted an unrecognizable DOI: " + e.getMessage());
}
}
@Test
public void testReserve_DOI()
throws SQLException, SQLException, AuthorizeException, IOException,
IdentifierException, WorkflowException, IllegalAccessException {
Item item = newItem();
String doi = this.createDOI(item, null, true);
provider.reserve(context, item, doi);
DOI doiRow = doiService.findByDoi(context, doi.substring(DOI.SCHEME.length()));
assumeNotNull(doiRow);
assertTrue("Reservation of DOI did not set the corret DOI status.",
DOIIdentifierProvider.TO_BE_RESERVED.equals(doiRow.getStatus()));
}
@Test
public void testRegister_unreserved_DOI()
throws SQLException, SQLException, AuthorizeException, IOException,
IdentifierException, WorkflowException, IllegalAccessException {
Item item = newItem();
String doi = this.createDOI(item, null, true);
provider.register(context, item, doi);
DOI doiRow = doiService.findByDoi(context, doi.substring(DOI.SCHEME.length()));
assumeNotNull(doiRow);
assertTrue("Registration of DOI did not set the corret DOI status.",
DOIIdentifierProvider.TO_BE_REGISTERED.equals(doiRow.getStatus()));
}
@Test
public void testRegister_reserved_DOI()
throws SQLException, SQLException, AuthorizeException, IOException,
IdentifierException, WorkflowException, IllegalAccessException {
Item item = newItem();
String doi = this.createDOI(item, DOIIdentifierProvider.IS_RESERVED, true);
provider.register(context, item, doi);
DOI doiRow = doiService.findByDoi(context, doi.substring(DOI.SCHEME.length()));
assumeNotNull(doiRow);
assertTrue("Registration of DOI did not set the corret DOI status.",
DOIIdentifierProvider.TO_BE_REGISTERED.equals(doiRow.getStatus()));
}
@Test
public void testCreate_and_Register_DOI()
throws SQLException, SQLException, AuthorizeException, IOException,
IdentifierException, WorkflowException, IllegalAccessException {
Item item = newItem();
// Register, skipping the filter
String doi = provider.register(context, item,
DSpaceServicesFactory.getInstance().getServiceManager().getServiceByName(
"always_true_filter", TrueFilter.class));
// we want the created DOI to be returned in the following format:
// doi:10.<prefix>/<suffix>.
String formated_doi = doiService.formatIdentifier(doi);
assertTrue("DOI was not in the expected format!", doi.equals(formated_doi));
DOI doiRow = doiService.findByDoi(context, doi.substring(DOI.SCHEME.length()));
assertNotNull("Created DOI was not stored in database.", doiRow);
assertTrue("Registration of DOI did not set the corret DOI status.",
DOIIdentifierProvider.TO_BE_REGISTERED.equals(doiRow.getStatus()));
}
@Test
public void testDelete_specified_DOI()
throws SQLException, AuthorizeException, IOException, IdentifierException, WorkflowException,
IllegalAccessException {
Item item = newItem();
String doi1 = this.createDOI(item, DOIIdentifierProvider.IS_REGISTERED, true);
String doi2 = this.createDOI(item, DOIIdentifierProvider.IS_REGISTERED, true);
// remove one of it
context.turnOffAuthorisationSystem();
provider.delete(context, item, doi1);
context.restoreAuthSystemState();
// assure that the right one was removed
List<MetadataValue> metadata = itemService.getMetadata(item, DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
null);
boolean foundDOI1 = false;
boolean foundDOI2 = false;
for (MetadataValue id : metadata) {
if (id.getValue().equals(doiService.DOIToExternalForm(doi1))) {
foundDOI1 = true;
}
if (id.getValue().equals(doiService.DOIToExternalForm(doi2))) {
foundDOI2 = true;
}
}
assertFalse("Cannot remove DOI from item metadata.", foundDOI1);
assertTrue("Removed wrong DOI from item metadata.", foundDOI2);
DOI doiRow1 = doiService.findByDoi(context, doi1.substring(DOI.SCHEME.length()));
assumeNotNull(doiRow1);
assertTrue("Status of deleted DOI was not set correctly.",
DOIIdentifierProvider.TO_BE_DELETED.equals(doiRow1.getStatus()));
DOI doiRow2 = doiService.findByDoi(context, doi2.substring(DOI.SCHEME.length()));
assumeNotNull(doiRow2);
assertTrue("While deleting a DOI the status of another changed.",
DOIIdentifierProvider.IS_REGISTERED.equals(doiRow2.getStatus()));
}
@Test
public void testDelete_all_DOIs()
throws SQLException, AuthorizeException, IOException, IdentifierException, IllegalAccessException,
WorkflowException {
Item item = newItem();
String doi1 = this.createDOI(item, DOIIdentifierProvider.IS_REGISTERED, true);
String doi2 = this.createDOI(item, DOIIdentifierProvider.IS_REGISTERED, true);
// remove one of it
context.turnOffAuthorisationSystem();
provider.delete(context, item);
context.restoreAuthSystemState();
// assure that the right one was removed
List<MetadataValue> metadata = itemService.getMetadata(item, DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
null);
boolean foundDOI1 = false;
boolean foundDOI2 = false;
for (MetadataValue id : metadata) {
if (id.getValue().equals(doiService.DOIToExternalForm(doi1))) {
foundDOI1 = true;
}
if (id.getValue().equals(doiService.DOIToExternalForm(doi2))) {
foundDOI2 = true;
}
}
assertFalse("Cannot remove DOI from item metadata.", foundDOI1);
assertFalse("Did not removed all DOIs from item metadata.", foundDOI2);
DOI doiRow1 = doiService.findByDoi(context, doi1.substring(DOI.SCHEME.length()));
assumeNotNull(doiRow1);
assertTrue("Status of deleted DOI was not set correctly.",
DOIIdentifierProvider.TO_BE_DELETED.equals(doiRow1.getStatus()));
DOI doiRow2 = doiService.findByDoi(context, doi1.substring(DOI.SCHEME.length()));
assumeNotNull(doiRow2);
assertTrue("Did not set the status of all deleted DOIs as expected.",
DOIIdentifierProvider.TO_BE_DELETED.equals(doiRow2.getStatus()));
}
@Test
public void testUpdateMetadataSkippedForPending()
throws SQLException, AuthorizeException, IOException, IdentifierException, IllegalAccessException,
WorkflowException {
context.turnOffAuthorisationSystem();
Item item = newItem();
// Mint a new DOI with PENDING status
String doi1 = this.createDOI(item, DOIIdentifierProvider.PENDING, true);
// Update metadata for the item.
// This would normally shift status to UPDATE_REGISTERED, UPDATE_BEFORE_REGISTERING or UPDATE_RESERVED.
// But if the DOI is just pending, it should return without changing anything.
provider.updateMetadata(context, item, doi1);
// Get the DOI from the service
DOI doi = doiService.findDOIByDSpaceObject(context, item);
// Ensure it is still PENDING
assertEquals("Status of updated DOI did not remain PENDING",
DOIIdentifierProvider.PENDING, doi.getStatus());
context.restoreAuthSystemState();
}
@Test
public void testMintDoiAfterOrphanedPendingDOI()
throws SQLException, AuthorizeException, IOException, IdentifierException, IllegalAccessException,
WorkflowException {
context.turnOffAuthorisationSystem();
Item item1 = newItem();
// Mint a new DOI with PENDING status
String doi1 = this.createDOI(item1, DOIIdentifierProvider.PENDING, true);
// remove the item
itemService.delete(context, item1);
// Get the DOI from the service
DOI doi = doiService.findDOIByDSpaceObject(context, item1);
// ensure DOI has no state
assertNull("Orphaned DOI was not set deleted", doi);
// create a new item and a new DOI
Item item2 = newItem();
String doi2 = null;
try {
// get a DOI (skipping any filters)
doi2 = provider.mint(context, item2);
} catch (IdentifierException e) {
e.printStackTrace(System.err);
fail("Got an IdentifierException: " + e.getMessage());
}
assertNotNull("Minted DOI is null?!", doi2);
assertFalse("Minted DOI is empty!", doi2.isEmpty());
assertNotEquals("Minted DOI equals previously orphaned DOI.", doi1, doi2);
try {
doiService.formatIdentifier(doi2);
} catch (DOIIdentifierException e) {
e.printStackTrace(System.err);
fail("Minted an unrecognizable DOI: " + e.getMessage());
}
context.restoreAuthSystemState();
}
@Test
public void testUpdateMetadataSkippedForMinted()
throws SQLException, AuthorizeException, IOException, IdentifierException, IllegalAccessException,
WorkflowException {
context.turnOffAuthorisationSystem();
Item item = newItem();
// Mint a new DOI with MINTED status
String doi1 = this.createDOI(item, DOIIdentifierProvider.MINTED, true);
// Update metadata for the item.
// This would normally shift status to UPDATE_REGISTERED, UPDATE_BEFORE_REGISTERING or UPDATE_RESERVED.
// But if the DOI is just minted, it should return without changing anything.
provider.updateMetadata(context, item, doi1);
// Get the DOI from the service
DOI doi = doiService.findDOIByDSpaceObject(context, item);
// Ensure it is still MINTED
assertEquals("Status of updated DOI did not remain PENDING",
DOIIdentifierProvider.MINTED, doi.getStatus());
context.restoreAuthSystemState();
}
@Test
public void testLoadOrCreateDOIReturnsMintedStatus()
throws SQLException, AuthorizeException, IOException, IdentifierException, IllegalAccessException,
WorkflowException {
Item item = newItem();
// Mint a DOI without an explicit reserve or register context
String mintedDoi = provider.mint(context, item, DSpaceServicesFactory.getInstance()
.getServiceManager().getServiceByName("always_true_filter", TrueFilter.class));
DOI doi = doiService.findByDoi(context, mintedDoi.substring(DOI.SCHEME.length()));
// This should be minted
assertEquals("DOI is not of 'minted' status", DOIIdentifierProvider.MINTED, doi.getStatus());
provider.updateMetadata(context, item, mintedDoi);
DOI secondFind = doiService.findByDoi(context, mintedDoi.substring(DOI.SCHEME.length()));
// After an update, this should still be minted
assertEquals("DOI is not of 'minted' status",
DOIIdentifierProvider.MINTED, secondFind.getStatus());
}
// test the following methods using the MockDOIConnector.
// updateMetadataOnline
// registerOnline
// reserveOnline
private void checkSingleDoiMetadata(Item item, String doi) throws IdentifierException {
List<MetadataValue> metadata = itemService.getMetadata(item, DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
Item.ANY);
boolean result = false;
if (metadata.size() == 1 && metadata.get(0).getValue().equals(doiService.DOIToExternalForm(doi))) {
result = true;
}
assertTrue("Invalid or duplicate 'dc.identifier.doi' metadata value(s).", result);
}
private void checkDoiMetadata(Item item, String... dois) throws IdentifierException {
List<String> values = itemService.getMetadata(item, DOIIdentifierProvider.MD_SCHEMA,
DOIIdentifierProvider.DOI_ELEMENT,
DOIIdentifierProvider.DOI_QUALIFIER,
Item.ANY)
.stream()
.map(MetadataValue::getValue)
.collect(Collectors.toList());
List<String> expected = new ArrayList<>();
for (String doi : dois) {
expected.add(doiService.DOIToExternalForm(doi));
}
assertEquals("Unexpected number of 'dc.identifier.doi' metadata values.", expected.size(), values.size());
assertTrue("Expected 'dc.identifier.doi' metadata values are missing.", values.containsAll(expected));
}
}