/
FedoraNodesIT.java
1361 lines (1121 loc) · 58.8 KB
/
FedoraNodesIT.java
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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.integration.http.api;
import static com.hp.hpl.jena.graph.Node.ANY;
import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.graph.NodeFactory.createURI;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createModelForGraph;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createPlainLiteral;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static org.apache.jena.riot.WebContent.contentTypeSPARQLUpdate;
import static org.apache.jena.riot.WebContent.contentTypeTurtle;
import static org.apache.jena.riot.WebContent.contentTypeN3;
import static org.apache.jena.riot.WebContent.contentTypeN3Alt1;
import static org.apache.jena.riot.WebContent.contentTypeN3Alt2;
import static org.apache.jena.riot.WebContent.contentTypeRDFXML;
import static org.apache.jena.riot.WebContent.contentTypeNTriples;
import static java.util.regex.Pattern.DOTALL;
import static java.util.regex.Pattern.compile;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import static javax.ws.rs.core.Response.Status.NOT_MODIFIED;
import static javax.ws.rs.core.Response.Status.NO_CONTENT;
import static javax.ws.rs.core.Response.Status.OK;
import static nu.validator.htmlparser.common.DoctypeExpectation.NO_DOCTYPE_ERRORS;
import static nu.validator.htmlparser.common.XmlViolationPolicy.ALLOW;
import static org.apache.http.impl.client.cache.CacheConfig.DEFAULT;
import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE;
import static org.fcrepo.jcr.FedoraJcrTypes.ROOT;
import static org.fcrepo.kernel.RdfLexicon.CONTAINS;
import static org.fcrepo.kernel.RdfLexicon.DC_NAMESPACE;
import static org.fcrepo.kernel.RdfLexicon.DC_TITLE;
import static org.fcrepo.kernel.RdfLexicon.FIRST_PAGE;
import static org.fcrepo.kernel.RdfLexicon.NEXT_PAGE;
import static org.fcrepo.kernel.RdfLexicon.HAS_CHILD;
import static org.fcrepo.kernel.RdfLexicon.HAS_OBJECT_COUNT;
import static org.fcrepo.kernel.RdfLexicon.HAS_OBJECT_SIZE;
import static org.fcrepo.kernel.RdfLexicon.HAS_PRIMARY_IDENTIFIER;
import static org.fcrepo.kernel.RdfLexicon.HAS_PRIMARY_TYPE;
import static org.fcrepo.kernel.RdfLexicon.JCR_NT_NAMESPACE;
import static org.fcrepo.kernel.RdfLexicon.LDP_NAMESPACE;
import static org.fcrepo.kernel.RdfLexicon.MIX_NAMESPACE;
import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE;
import static org.fcrepo.kernel.RdfLexicon.RESTAPI_NAMESPACE;
import static org.fcrepo.kernel.RdfLexicon.RDF_NAMESPACE;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.map;
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.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Iterator;
import javax.ws.rs.core.Variant;
import nu.validator.htmlparser.sax.HtmlParser;
import nu.validator.saxtree.TreeBuilder;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.cache.CachingHttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.fcrepo.http.commons.domain.RDFMediaType;
import org.junit.Ignore;
import org.junit.Test;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.sparql.core.Quad;
import com.hp.hpl.jena.update.GraphStore;
public class FedoraNodesIT extends AbstractResourceIT {
@Test
public void testIngest() throws Exception {
final String pid = getRandomUniquePid();
final HttpResponse response = createObject(pid);
final String content = EntityUtils.toString(response.getEntity());
assertTrue("Response wasn't a PID", compile("[a-z]+").matcher(content)
.find());
assertTrue("Didn't find Last-Modified header!", response.containsHeader("Last-Modified"));
assertTrue("Didn't find ETag header!", response.containsHeader("ETag"));
assertTrue("Didn't find Location header!", response.containsHeader("Location"));
}
@Test
public void testIngestWithNewAndSparqlQuery() throws Exception {
final HttpPost method = postObjMethod("");
method.addHeader("Content-Type", "application/sparql-update");
final BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(new ByteArrayInputStream(
("INSERT { <> <http://purl.org/dc/elements/1.1/title> \"this is a title\" } WHERE {}")
.getBytes()));
method.setEntity(entity);
final HttpResponse response = client.execute(method);
final String content = EntityUtils.toString(response.getEntity());
final int status = response.getStatusLine().getStatusCode();
assertEquals("Didn't get a CREATED response! Got content:\n" + content,
CREATED.getStatusCode(), status);
final String location = response.getFirstHeader("Location").getValue();
final HttpGet httpGet = new HttpGet(location);
final GraphStore graphStore = getGraphStore(httpGet);
assertTrue(graphStore.contains(ANY, createResource(location).asNode(),
DC_TITLE.asNode(), createPlainLiteral("this is a title")
.asNode()));
assertTrue("Didn't find Last-Modified header!", response.containsHeader("Last-Modified"));
final String lastmod = response.getFirstHeader("Last-Modified").getValue();
assertNotNull("Should set Last-Modified for new nodes", lastmod);
assertNotEquals("Last-Modified should not be blank for new nodes", lastmod.trim(), "");
}
@Test
public void testIngestWithNewAndGraph() throws Exception {
final HttpPost method = postObjMethod("");
method.addHeader("Content-Type", "application/n3");
final BasicHttpEntity entity = new BasicHttpEntity();
final String rdf = "<> <http://purl.org/dc/elements/1.1/title> \"this is a title\".";
entity.setContent(new ByteArrayInputStream(rdf.getBytes()));
method.setEntity(entity);
final HttpResponse response = client.execute(method);
final String content = EntityUtils.toString(response.getEntity());
final int status = response.getStatusLine().getStatusCode();
assertEquals("Didn't get a CREATED response! Got content:\n" + content,
CREATED.getStatusCode(), status);
final String location = response.getFirstHeader("Location").getValue();
final HttpGet httpGet = new HttpGet(location);
final GraphStore graphStore = getGraphStore(httpGet);
assertTrue(graphStore.contains(ANY, createResource(location).asNode(),
DC_TITLE.asNode(), createPlainLiteral("this is a title")
.asNode()));
assertTrue("Didn't find Last-Modified header!", response.containsHeader("Last-Modified"));
final String lastmod = response.getFirstHeader("Last-Modified").getValue();
assertNotNull("Should set Last-Modified for new nodes", lastmod);
assertNotEquals("Last-Modified should not be blank for new nodes", lastmod.trim(), "");
}
@Test
public void testIngestWithSlug() throws Exception {
final HttpPost method = postObjMethod("");
method.addHeader("Slug", getRandomUniquePid());
final HttpResponse response = client.execute(method);
final String content = EntityUtils.toString(response.getEntity());
final int status = response.getStatusLine().getStatusCode();
assertEquals("Didn't get a CREATED response! Got content:\n" + content,
CREATED.getStatusCode(), status);
assertTrue("Response wasn't a PID", compile("[a-z]+").matcher(content)
.find());
final String location = response.getFirstHeader("Location").getValue();
assertNotEquals(serverAddress + "/objects", location);
assertEquals("Object wasn't created!", OK.getStatusCode(),
getStatus(new HttpGet(location)));
}
@Test
public void testIngestWithBinary() throws Exception {
final HttpPost method = postObjMethod("");
method.addHeader("Content-Type", "application/octet-stream");
final BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(new ByteArrayInputStream("xyz".getBytes()));
method.setEntity(entity);
final HttpResponse response = client.execute(method);
final String content = EntityUtils.toString(response.getEntity());
final int status = response.getStatusLine().getStatusCode();
assertEquals("Didn't get a CREATED response! Got content:\n" + content,
CREATED.getStatusCode(), status);
assertTrue("Response wasn't a PID", compile("[a-z]+").matcher(content)
.find());
final String location = response.getFirstHeader("Location").getValue();
assertNotEquals(serverAddress + "/objects", location);
assertTrue(location.endsWith("fcr:content"));
assertEquals("Object wasn't created!", OK.getStatusCode(),
getStatus(new HttpGet(location)));
}
@Test
public void testDeleteObject() throws Exception {
final HttpResponse response = createObject("");
final String location = response.getFirstHeader("Location").getValue();
assertEquals(204, getStatus(new HttpDelete(location)));
assertEquals("Object wasn't really deleted!", 404,
getStatus(new HttpGet(location)));
}
@Test
public void testDeleteWithBadEtag() throws Exception {
final HttpPost method = postObjMethod("");
final HttpResponse response = client.execute(method);
assertEquals(CREATED.getStatusCode(), response.getStatusLine()
.getStatusCode());
final String location = response.getFirstHeader("Location").getValue();
final HttpDelete request = new HttpDelete(location);
request.addHeader("If-Match", "\"doesnt-match\"");
final HttpResponse deleteResponse = client.execute(request);
assertEquals(412, deleteResponse.getStatusLine().getStatusCode());
}
@Test
public void testGetDatastream() throws Exception {
final String pid = getRandomUniquePid();
createObject(pid);
createDatastream(pid, "ds1", "foo");
final HttpResponse response =
execute(new HttpGet(serverAddress + pid + "/ds1"));
assertEquals(EntityUtils.toString(response.getEntity()), 200, response
.getStatusLine().getStatusCode());
assertEquals(TURTLE, response.getFirstHeader("Content-Type").getValue());
final Collection<String> links =
map(response.getHeaders("Link"), new Function<Header, String>() {
@Override
public String apply(final Header h) {
return h.getValue();
}
});
assertTrue("Didn't find 'describes' link header!",
links.contains("<" + serverAddress + pid + "/ds1/fcr:content>;rel=\"describes\""));
}
@Test
public void testDeleteDatastream() throws Exception {
final String pid = getRandomUniquePid();
createObject(pid);
createDatastream(pid, "ds1", "foo");
final HttpDelete dmethod =
new HttpDelete(serverAddress + pid + "/ds1");
assertEquals(204, getStatus(dmethod));
final HttpGet method_test_get =
new HttpGet(serverAddress + pid + "/ds1");
assertEquals(404, getStatus(method_test_get));
}
@Test
public void testHeadRepositoryGraph() throws Exception {
final HttpHead headObjMethod = new HttpHead(serverAddress);
assertEquals(200, getStatus(headObjMethod));
}
@Test
public void testGetRepositoryGraph() throws Exception {
final HttpGet getObjMethod = new HttpGet(serverAddress);
final GraphStore graphStore = getGraphStore(getObjMethod);
logger.trace("Retrieved repository graph:\n" + graphStore.toString());
assertTrue("expected to find the root node data", graphStore.contains(
ANY, ANY, HAS_PRIMARY_TYPE.asNode(), createLiteral(ROOT)));
}
@Test
public void testGetObjectGraphHtml() throws Exception {
final HttpResponse createResponse = createObject("");
final String location = createResponse.getFirstHeader("Location").getValue();
final HttpGet getObjMethod = new HttpGet(location);
getObjMethod.addHeader("Accept", "text/html");
final HttpResponse response = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
final String content = EntityUtils.toString(response.getEntity());
logger.trace("Retrieved: {}", content);
}
@Test
public void testGetObjectGraphVariants() throws Exception {
final HttpResponse createResponse = createObject("");
final String location = createResponse.getFirstHeader("Location").getValue();
for (final Variant variant : RDFMediaType.POSSIBLE_RDF_VARIANTS) {
final HttpGet getObjMethod =
new HttpGet(location);
final String type = variant.getMediaType().getType();
getObjMethod.addHeader("Accept", type);
final HttpResponse response = client.execute(getObjMethod);
final int expected = OK.getStatusCode();
final int found = response.getStatusLine().getStatusCode();
assertEquals("Expected: " + expected + ", recieved: " + found + ", " + type, expected, found);
}
}
@Test
public void testGetObjectGraph() throws Exception {
logger.debug("Entering testGetObjectGraph()...");
final HttpResponse createResponse = createObject("");
final String location = createResponse.getFirstHeader("Location").getValue();
final HttpGet getObjMethod =
new HttpGet(location);
final HttpResponse response = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
assertEquals("application/sparql-update", response.getFirstHeader(
"Accept-Patch").getValue());
final Collection<String> links =
map(response.getHeaders("Link"), new Function<Header, String>() {
@Override
public String apply(final Header h) {
return h.getValue();
}
});
assertTrue("Didn't find LDP link header!", links
.contains("<" + LDP_NAMESPACE + "Resource>;rel=\"type\""));
final GraphStore results = getGraphStore(getObjMethod);
final Model model = createModelForGraph(results.getDefaultGraph());
final Resource nodeUri = createResource(location);
assertTrue("Didn't find an expected triple!", model.contains(nodeUri,
createProperty(REPOSITORY_NAMESPACE + "mixinTypes"),
createPlainLiteral("fedora:object")));
logger.debug("Leaving testGetObjectGraph()...");
}
@Test
public void verifyFullSetOfRdfTypes() throws Exception {
logger.debug("Entering verifyFullSetOfRdfTypes()...");
final String pid = getRandomUniquePid();
createObject(pid);
addMixin( pid, MIX_NAMESPACE + "versionable" );
final HttpGet getObjMethod =
new HttpGet(serverAddress + pid);
final HttpResponse response = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
final GraphStore results = getGraphStore(getObjMethod);
final Model model = createModelForGraph(results.getDefaultGraph());
final Resource nodeUri = createResource(serverAddress + pid);
final Property rdfType = createProperty(RDF_NAMESPACE + "type");
//verifyResource based on the expection of these types on an out of the box fedora object:
/*
http://fedora.info/definitions/v4/rest-api#object
http://fedora.info/definitions/v4/rest-api#relations
http://fedora.info/definitions/v4/rest-api#resource
http://purl.org/dc/elements/1.1/describable
http://www.jcp.org/jcr/mix/1.0created
http://www.jcp.org/jcr/mix/1.0lastModified
http://www.jcp.org/jcr/mix/1.0lockable
http://www.jcp.org/jcr/mix/1.0referenceable
http://www.jcp.org/jcr/mix/1.0simpleVersionable
http://www.jcp.org/jcr/mix/1.0versionable
http://www.jcp.org/jcr/nt/1.0base
http://www.jcp.org/jcr/nt/1.0folder
http://www.jcp.org/jcr/nt/1.0hierarchyNode
http://www.w3.org/ns/ldp#Container
http://www.w3.org/ns/ldp#DirectContainer
http://www.w3.org/ns/ldp#Page
*/
verifyResource(model, nodeUri, rdfType, RESTAPI_NAMESPACE, "object");
verifyResource(model, nodeUri, rdfType, RESTAPI_NAMESPACE, "relations");
verifyResource(model, nodeUri, rdfType, RESTAPI_NAMESPACE, "resource");
verifyResource(model, nodeUri, rdfType, LDP_NAMESPACE, "DirectContainer");
verifyResource(model, nodeUri, rdfType, DC_NAMESPACE, "describable");
verifyResource(model, nodeUri, rdfType, MIX_NAMESPACE, "created");
verifyResource(model, nodeUri, rdfType, MIX_NAMESPACE, "lastModified");
verifyResource(model, nodeUri, rdfType, MIX_NAMESPACE, "lockable");
verifyResource(model, nodeUri, rdfType, MIX_NAMESPACE, "referenceable");
verifyResource(model, nodeUri, rdfType, MIX_NAMESPACE, "simpleVersionable");
verifyResource(model, nodeUri, rdfType, MIX_NAMESPACE, "versionable");
verifyResource(model, nodeUri, rdfType, JCR_NT_NAMESPACE, "base");
verifyResource(model, nodeUri, rdfType, JCR_NT_NAMESPACE, "folder");
verifyResource(model, nodeUri, rdfType, JCR_NT_NAMESPACE, "hierarchyNode");
logger.debug("Leaving verifyFullSetOfRdfTypes()...");
}
@Test
public void testGetObjectGraphWithChildren() throws Exception {
final String pid = getRandomUniquePid();
final HttpResponse createResponse = createObject(pid);
final String location = createResponse.getFirstHeader("Location").getValue();
createObject(pid + "/a");
createObject(pid + "/b");
createObject(pid + "/c");
final HttpGet getObjMethod = new HttpGet(serverAddress + pid);
getObjMethod.addHeader("Accept", "application/rdf+xml");
final HttpResponse response = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
final Model model = createDefaultModel();
model.read(response.getEntity().getContent(), null);
try (final Writer w = new StringWriter()) {
model.write(w);
logger.trace(
"Retrieved object graph:\n {}",
w);
}
final Resource subjectUri = createResource(location);
assertTrue(
"Didn't find child node!",
model.contains(
subjectUri,
createProperty(REPOSITORY_NAMESPACE + "hasChild"),
createResource(location + "/c")));
final Collection<String> links =
map(response.getHeaders("Link"), new Function<Header, String>() {
@Override
public String apply(final Header h) {
return h.getValue();
}
});
assertTrue("Didn't find LDP resource link header!", links.contains("<" + LDP_NAMESPACE + "Resource>;rel=\"type\""));
assertTrue("Didn't find LDP container link header!", links.contains("<" + LDP_NAMESPACE + "DirectContainer>;rel=\"type\""));
}
@Test
public void testGetObjectGraphMinimal() throws Exception {
final String pid = getRandomUniquePid();
createObject(pid);
createObject(pid + "/a");
final HttpGet getObjMethod =
new HttpGet(serverAddress + pid);
getObjMethod.addHeader("Prefer", "return=minimal");
getObjMethod.addHeader("Accept", "application/n-triples");
final HttpResponse response = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
final String content = EntityUtils.toString(response.getEntity());
logger.trace("Retrieved object graph:\n" + content);
assertFalse(
"Didn't expect member resources",
compile(
"<"
+ serverAddress
+ pid + "> <" + HAS_CHILD + ">",
DOTALL).matcher(content).find());
assertFalse("Didn't expect contained member resources",
compile(
"<"
+ serverAddress
+ pid + "> <" + CONTAINS + ">",
DOTALL).matcher(content).find());
}
@Test
public void testGetObjectOmitMembership() throws Exception {
final String pid = getRandomUniquePid();
createObject(pid);
createObject(pid + "/a");
final HttpGet getObjMethod =
new HttpGet(serverAddress + pid);
getObjMethod.addHeader("Prefer", "return=representation; omit=\"http://www.w3.org/ns/ldp#PreferContainment http://www.w3.org/ns/ldp#PreferMembership\"");
getObjMethod.addHeader("Accept", "application/n-triples");
final HttpResponse response = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
final String content = EntityUtils.toString(response.getEntity());
logger.trace("Retrieved object graph:\n" + content);
assertFalse(
"Didn't expect inlined member resources",
compile(
"<"
+ serverAddress
+ pid + "> <" + HAS_CHILD + ">",
DOTALL).matcher(content).find());
}
@Test
public void testGetObjectOmitContainment() throws Exception {
final String pid = getRandomUniquePid();
createObject(pid);
createObject(pid + "/a");
final HttpGet getObjMethod =
new HttpGet(serverAddress + pid);
getObjMethod.addHeader("Prefer", "return=representation; omit=\"http://www.w3.org/ns/ldp#PreferContainment\"");
getObjMethod.addHeader("Accept", "application/n-triples");
final HttpResponse response = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
final String content = EntityUtils.toString(response.getEntity());
logger.trace("Retrieved object graph:\n" + content);
assertTrue("Didn't find member resources",
compile(
"<"
+ serverAddress
+ pid + "> <" + HAS_CHILD + ">",
DOTALL).matcher(content).find());
assertFalse("Didn't expect contained member resources",
compile(
"<"
+ serverAddress
+ pid + "> <" + CONTAINS + ">",
DOTALL).matcher(content).find());
}
@Test
public void testGetObjectGraphByUUID() throws Exception {
final HttpResponse createResponse = createObject("");
final String location = createResponse.getFirstHeader("Location").getValue();
final HttpGet getObjMethod = new HttpGet(location);
final GraphStore graphStore = getGraphStore(getObjMethod);
final Iterator<Quad> iterator =
graphStore.find(ANY, createURI(location),
HAS_PRIMARY_IDENTIFIER.asNode(), ANY);
assertTrue("Expected graph to contain a UUID", iterator.hasNext());
final String uuid = iterator.next().getObject().getLiteralLexicalForm();
final HttpGet getObjMethodByUuid =
new HttpGet(serverAddress + "%5B" + uuid + "%5D");
getObjMethodByUuid.addHeader("Accept", "application/n3");
final HttpResponse uuidResponse = client.execute(getObjMethod);
assertEquals(200, uuidResponse.getStatusLine().getStatusCode());
}
@Test
public void testUpdateObjectGraph() throws Exception {
final HttpResponse createResponse = createObject("");
final String subjectURI = createResponse.getFirstHeader("Location").getValue();
final HttpPatch updateObjectGraphMethod =
new HttpPatch(subjectURI);
updateObjectGraphMethod.addHeader("Content-Type",
"application/sparql-update");
final BasicHttpEntity e = new BasicHttpEntity();
e.setContent(new ByteArrayInputStream(
("INSERT { <" + subjectURI + "> <http://purl.org/dc/elements/1.1/identifier> \"this is an identifier\" } WHERE {}")
.getBytes()));
updateObjectGraphMethod.setEntity(e);
final HttpResponse response = client.execute(updateObjectGraphMethod);
assertEquals(NO_CONTENT.getStatusCode(), response.getStatusLine()
.getStatusCode());
}
@Test
public void testUpdateAndReplaceObjectGraph() throws Exception {
final HttpResponse createResponse = createObject("");
final String subjectURI = createResponse.getFirstHeader("Location").getValue();
final HttpPatch updateObjectGraphMethod = new HttpPatch(subjectURI);
updateObjectGraphMethod.addHeader("Content-Type",
"application/sparql-update");
BasicHttpEntity e = new BasicHttpEntity();
e.setContent(new ByteArrayInputStream(
("INSERT { <" + subjectURI + "> <info:rubydora#label> \"asdfg\" } WHERE {}")
.getBytes()));
updateObjectGraphMethod.setEntity(e);
client.execute(updateObjectGraphMethod);
e = new BasicHttpEntity();
e.setContent(new ByteArrayInputStream(("DELETE { <" + subjectURI
+ "> <info:rubydora#label> ?p}\n" + "INSERT {<" + subjectURI
+ "> <info:rubydora#label> \"qwerty\"} \n" + "WHERE { <"
+ subjectURI + "> <info:rubydora#label> ?p}").getBytes()));
updateObjectGraphMethod.setEntity(e);
final HttpResponse response = client.execute(updateObjectGraphMethod);
assertEquals(NO_CONTENT.getStatusCode(), response.getStatusLine()
.getStatusCode());
assertTrue("Didn't find Last-Modified header!", response.containsHeader("Last-Modified"));
assertTrue("Didn't find ETag header!", response.containsHeader("ETag"));
final HttpGet getObjMethod = new HttpGet(subjectURI);
getObjMethod.addHeader("Accept", "application/rdf+xml");
final HttpResponse getResponse = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), getResponse.getStatusLine()
.getStatusCode());
final String content = EntityUtils.toString(getResponse.getEntity());
logger.trace("Retrieved object graph:\n" + content);
assertFalse("Found a triple we thought we deleted.", compile(
"<" + subjectURI + "> <info:rubydora#label> \"asdfg\" \\.",
DOTALL).matcher(content).find());
}
@Test
public void testUpdateObjectGraphWithProblems() throws Exception {
final HttpResponse createResponse = createObject("");
final String subjectURI = createResponse.getFirstHeader("Location").getValue();
final HttpPatch patchObjMethod = new HttpPatch(subjectURI);
patchObjMethod.addHeader("Content-Type", "application/sparql-update");
final BasicHttpEntity e = new BasicHttpEntity();
e.setContent(new ByteArrayInputStream(
("INSERT { <" + subjectURI + "> <" + REPOSITORY_NAMESPACE + "uuid> \"00e686e2-24d4-40c2-92ce-577c0165b158\" } WHERE {}\n")
.getBytes()));
patchObjMethod.setEntity(e);
final HttpResponse response = client.execute(patchObjMethod);
if (response.getStatusLine().getStatusCode() != 403
&& response.getEntity() != null) {
final String content = EntityUtils.toString(response.getEntity());
logger.trace("Got unexpected update response:\n" + content);
}
assertEquals(403, response.getStatusLine().getStatusCode());
}
@Test
public void testFilteredLDPTypes() throws Exception {
final String pid = getRandomUniquePid();
createObject(pid);
final HttpPut put = new HttpPut(serverAddress + pid);
put.addHeader("Content-Type", "text/rdf+n3");
final BasicHttpEntity e = new BasicHttpEntity();
e.setContent(new ByteArrayInputStream(
"<> a <http://www.w3.org/ns/ldp#IndirectContainer>".getBytes()));
put.setEntity(e);
assertEquals(204, getStatus(put));
}
@Test
public void testReplaceGraph() throws Exception {
final HttpResponse object = createObject("");
final String subjectURI = object.getFirstHeader("Location").getValue();
final HttpPut replaceMethod = new HttpPut(subjectURI);
replaceMethod.addHeader("Content-Type", "application/n3");
final BasicHttpEntity e = new BasicHttpEntity();
e.setContent(new ByteArrayInputStream(
("<" + subjectURI + "> <info:rubydora#label> \"asdfg\"")
.getBytes()));
replaceMethod.setEntity(e);
final HttpResponse response = client.execute(replaceMethod);
assertEquals(204, response.getStatusLine().getStatusCode());
assertTrue("Didn't find Last-Modified header!", response.containsHeader("Last-Modified"));
assertTrue("Didn't find ETag header!", response.containsHeader("ETag"));
final HttpGet getObjMethod = new HttpGet(subjectURI);
getObjMethod.addHeader("Accept", "application/rdf+xml");
final HttpResponse getResponse = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), getResponse.getStatusLine().getStatusCode());
final Model model = createDefaultModel();
model.read(getResponse.getEntity().getContent(), null);
try (final Writer w = new StringWriter()) {
model.write(w);
logger.trace(
"Retrieved object graph for testReplaceGraph():\n {}", w);
}
assertTrue("Didn't find a triple we tried to create!", model.contains(
createResource(subjectURI),
createProperty("info:rubydora#label"),
createPlainLiteral("asdfg")));
}
@Test
public void testCreateGraph() throws Exception {
final String subjectURI = serverAddress + getRandomUniquePid();
final HttpPut replaceMethod = new HttpPut(subjectURI);
replaceMethod.addHeader("Content-Type", "application/n3");
final BasicHttpEntity e = new BasicHttpEntity();
e.setContent(new ByteArrayInputStream(
("<" + subjectURI + "> <info:rubydora#label> \"asdfg\"")
.getBytes()));
replaceMethod.setEntity(e);
final HttpResponse response = client.execute(replaceMethod);
assertEquals(201, response.getStatusLine().getStatusCode());
final HttpGet getObjMethod = new HttpGet(subjectURI);
getObjMethod.addHeader("Accept", "application/rdf+xml");
final HttpResponse getResponse = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), getResponse.getStatusLine()
.getStatusCode());
final Model model = createDefaultModel();
model.read(getResponse.getEntity().getContent(), null);
try (final Writer w = new StringWriter()) {
model.write(w);
logger.trace("Retrieved object graph for testCreateGraph():\n {}",
w);
}
assertTrue("Didn't find a triple we tried to create!", model.contains(
createResource(subjectURI),
createProperty("info:rubydora#label"),
createPlainLiteral("asdfg")));
}
@Test
public void testRoundTripReplaceGraph() throws Exception {
final String pid = getRandomUniquePid();
final String subjectURI = serverAddress + pid;
createObject(pid);
final HttpGet getObjMethod = new HttpGet(subjectURI);
getObjMethod.addHeader("Accept", "text/turtle");
getObjMethod.addHeader("Prefer", "return=minimal");
final HttpResponse getResponse = client.execute(getObjMethod);
final BasicHttpEntity e = new BasicHttpEntity();
final Model model = createDefaultModel();
model.read(getResponse.getEntity().getContent(), subjectURI, "TURTLE");
try (final StringWriter w = new StringWriter()) {
model.write(w, "TURTLE");
e.setContent(new ByteArrayInputStream(w.toString().getBytes()));
logger.trace("Retrieved object graph for testRoundTripReplaceGraph():\n {}",
w);
}
final HttpPut replaceMethod = new HttpPut(subjectURI);
replaceMethod.addHeader("Content-Type", "text/turtle");
replaceMethod.setEntity(e);
final HttpResponse response = client.execute(replaceMethod);
assertEquals(204, response.getStatusLine().getStatusCode());
}
@Test
public void testRoundTripReplaceGraphForDatastream() throws Exception {
final String pid = getRandomUniquePid();
final String subjectURI = serverAddress + pid + "/ds1";
createDatastream(pid, "ds1", "some-content");
final HttpGet getObjMethod = new HttpGet(subjectURI);
getObjMethod.addHeader("Accept", "text/turtle");
getObjMethod.addHeader("Prefer", "return=minimal");
final HttpResponse getResponse = client.execute(getObjMethod);
final BasicHttpEntity e = new BasicHttpEntity();
final Model model = createDefaultModel();
model.read(getResponse.getEntity().getContent(), subjectURI, "TURTLE");
try (final StringWriter w = new StringWriter()) {
model.write(w, "TURTLE");
e.setContent(new ByteArrayInputStream(w.toString().getBytes()));
logger.trace("Retrieved object graph for testRoundTripReplaceGraphForDatastream():\n {}",
w);
}
final HttpPut replaceMethod = new HttpPut(subjectURI);
replaceMethod.addHeader("Content-Type", "text/turtle");
replaceMethod.setEntity(e);
final HttpResponse response = client.execute(replaceMethod);
assertEquals(204, response.getStatusLine().getStatusCode());
}
@Test
public void testDescribeSize() throws Exception {
final String sizeNode = getRandomUniquePid();
GraphStore graphStore = getGraphStore(new HttpGet(serverAddress + ""));
logger.trace("For testDescribeSize() first size retrieved repository graph:\n"
+ graphStore.toString());
Iterator<Triple> iterator =
graphStore.getDefaultGraph().find(ANY, HAS_OBJECT_SIZE.asNode(),
ANY);
final String oldSize = (String) iterator.next().getObject().getLiteralValue();
createObject(sizeNode);
createDatastream(sizeNode, "asdf", "1234");
graphStore = getGraphStore(new HttpGet(serverAddress + ""));
logger.trace("For testDescribeSize() new size retrieved repository graph:\n"
+ graphStore.toString());
iterator =
graphStore.getDefaultGraph().find(ANY, HAS_OBJECT_SIZE.asNode(),
ANY);
final String newSize = (String) iterator.next().getObject().getLiteralValue();
logger.trace("Old size was: " + oldSize + " and new size was: "
+ newSize);
assertTrue("No increment in size occurred when we expected one!",
Integer.parseInt(oldSize) < Integer.parseInt(newSize));
}
@Test
public void testDescribeCount() throws Exception {
logger.trace("Entering testDescribeCount()...");
GraphStore graphStore = getGraphStore(new HttpGet(serverAddress + ""));
logger.trace("For testDescribeCount() first count retrieved repository graph:\n"
+ graphStore.toString());
Iterator<Triple> iterator =
graphStore.getDefaultGraph().find(ANY, HAS_OBJECT_COUNT.asNode(),
ANY);
final String oldSize = (String) iterator.next().getObject().getLiteralValue();
createObject("");
final String countNode = getRandomUniquePid();
createDatastream(countNode, "asdf", "1234");
graphStore = getGraphStore(new HttpGet(serverAddress + ""));
logger.trace("For testDescribeCount() first count repository graph:\n"
+ graphStore.toString());
iterator =
graphStore.getDefaultGraph().find(ANY, HAS_OBJECT_COUNT.asNode(),
ANY);
final String newSize =
(String) iterator.next().getObject().getLiteralValue();
logger.debug("Old size was: " + oldSize + " and new size was: " +
newSize);
assertTrue("No increment in count occurred when we expected one!",
Integer.parseInt(oldSize) < Integer.parseInt(newSize));
}
/**
* Given a directory at: test-FileSystem1/ /ds1 /ds2 /TestSubdir/
* and a projection of test-objects as fedora:/files, then I should be able
* to retrieve an object from fedora:/files/FileSystem1 that lists a child
* object at fedora:/files/FileSystem1/TestSubdir and lists datastreams ds1
* and ds2
*/
@Test
public void testGetProjectedNode() throws Exception {
final HttpGet method = new HttpGet(serverAddress + "files/FileSystem1");
final Graph result = getGraphStore(method).getDefaultGraph();
final String subjectURI = serverAddress + "files/FileSystem1";
logger.trace("For testGetProjectedNode() retrieved graph:\n"
+ result.toString());
assertTrue("Didn't find the first datastream! ", result.contains(
createURI(subjectURI), ANY, createURI(subjectURI + "/ds1")));
assertTrue("Didn't find the second datastream! ", result.contains(
createURI(subjectURI), ANY, createURI(subjectURI + "/ds2")));
assertTrue("Didn't find the first object! ", result.contains(
createURI(subjectURI), ANY, createURI(subjectURI
+ "/TestSubdir")));
}
@Test
public void testDescribeRdfCached() throws IOException {
final CloseableHttpClient cachingClient =
CachingHttpClientBuilder.create().setCacheConfig(DEFAULT).build();
final HttpResponse createResponse = createObject("");
final String location = createResponse.getFirstHeader("Location").getValue();
final HttpGet getObjMethod = new HttpGet(location);
HttpResponse response = cachingClient.execute(getObjMethod);
assertEquals("Client didn't return a OK!", OK.getStatusCode(), response
.getStatusLine().getStatusCode());
logger.debug("Found HTTP headers:\n{}", Joiner.on('\n').join(
response.getAllHeaders()));
assertTrue("Didn't find Last-Modified header!", response
.containsHeader("Last-Modified"));
final String lastModed =
response.getFirstHeader("Last-Modified").getValue();
final String etag = response.getFirstHeader("ETag").getValue();
final HttpGet getObjMethod2 = new HttpGet(location);
getObjMethod2.setHeader("If-Modified-Since", lastModed);
getObjMethod2.setHeader("If-None-Match", etag);
response = cachingClient.execute(getObjMethod2);
assertEquals("Client didn't return a NOT_MODIFIED!", NOT_MODIFIED
.getStatusCode(), response.getStatusLine().getStatusCode());
}
@Test
public void testValidHTMLForRepo() throws Exception {
validateHTML("");
}
@Test
public void testValidHTMLForObject() throws Exception {
final String pid = getRandomUniquePid();
createObject(pid);
validateHTML(pid);
}
@Test
public void testValidHTMLForDS() throws Exception {
final String pid = getRandomUniquePid();
client.execute(new HttpPut(serverAddress
+ pid + "/ds/fcr:content"));
validateHTML(pid + "/ds");
}
@Test
public void testCopy() throws Exception {
final HttpResponse response = createObject("");
final String pid = getRandomUniquePid();
final String location = response.getFirstHeader("Location").getValue();
final HttpCopy request = new HttpCopy(location);
request.addHeader("Destination", serverAddress + pid);
client.execute(request);
final HttpGet httpGet = new HttpGet(serverAddress + pid);
final HttpResponse copiedResult = client.execute(httpGet);
assertEquals(OK.getStatusCode(), copiedResult.getStatusLine().getStatusCode());