/
FedoraLdpTest.java
705 lines (578 loc) · 31 KB
/
FedoraLdpTest.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
/**
* Copyright 2014 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.http.api;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import org.apache.commons.io.IOUtils;
import org.fcrepo.http.commons.api.rdf.HttpResourceConverter;
import org.fcrepo.http.commons.domain.Prefer;
import org.fcrepo.kernel.Datastream;
import org.fcrepo.kernel.FedoraBinary;
import org.fcrepo.kernel.FedoraObject;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.kernel.services.BinaryService;
import org.fcrepo.kernel.services.NodeService;
import org.fcrepo.kernel.services.ObjectService;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.mock.web.MockHttpServletResponse;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.ClientErrorException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import java.io.InputStream;
import java.util.List;
import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM;
import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM_TYPE;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.NO_CONTENT;
import static javax.ws.rs.core.Response.Status.OK;
import static org.apache.commons.io.IOUtils.toInputStream;
import static org.apache.jena.riot.WebContent.contentTypeSPARQLUpdate;
import static org.fcrepo.http.api.ContentExposingResource.getSimpleContentType;
import static org.fcrepo.http.commons.domain.RDFMediaType.NTRIPLES_TYPE;
import static org.fcrepo.http.commons.test.util.TestHelpers.getUriInfoImpl;
import static org.fcrepo.http.commons.test.util.TestHelpers.mockSession;
import static org.fcrepo.kernel.RdfLexicon.INBOUND_REFERENCES;
import static org.fcrepo.kernel.RdfLexicon.LDP_NAMESPACE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.springframework.test.util.ReflectionTestUtils.setField;
/**
* @author cabeer
*/
public class FedoraLdpTest {
private String path = "/some/path";
private String binaryPath = "/some/binary/path";
private String binaryDescriptionPath = "/some/other/path";
private FedoraLdp testObj;
@Mock
private Request mockRequest;
private HttpServletResponse mockResponse;
private Session mockSession;
@Mock
private Node mockNode;
@Mock
private FedoraObject mockObject;
@Mock
private Datastream mockDatastream;
@Mock
private FedoraBinary mockBinary;
private IdentifierConverter<Resource, FedoraResource> identifierConverter;
@Mock
private NodeService mockNodeService;
@Mock
private ObjectService mockObjectService;
@Mock
private BinaryService mockBinaryService;
@Mock
private FedoraHttpConfiguration mockHttpConfiguration;
@Before
public void setUp() throws Exception {
initMocks(this);
testObj = spy(new FedoraLdp(path));
mockResponse = new MockHttpServletResponse();
mockSession = mockSession(testObj);
setField(testObj, "session", mockSession);
identifierConverter = new HttpResourceConverter(mockSession,
UriBuilder.fromUri("http://localhost/fcrepo/{path: .*}"));
setField(testObj, "request", mockRequest);
setField(testObj, "servletResponse", mockResponse);
setField(testObj, "uriInfo", getUriInfoImpl());
setField(testObj, "identifierTranslator", identifierConverter);
setField(testObj, "nodeService", mockNodeService);
setField(testObj, "objectService", mockObjectService);
setField(testObj, "binaryService", mockBinaryService);
setField(testObj, "httpConfiguration", mockHttpConfiguration);
when(mockHttpConfiguration.putRequiresIfMatch()).thenReturn(false);
when(mockObject.getEtagValue()).thenReturn("");
when(mockObject.getPath()).thenReturn(path);
when(mockDatastream.getEtagValue()).thenReturn("");
when(mockDatastream.getPath()).thenReturn(binaryDescriptionPath);
when(mockDatastream.getBinary()).thenReturn(mockBinary);
when(mockBinary.getEtagValue()).thenReturn("");
when(mockBinary.getPath()).thenReturn(binaryPath);
when(mockBinary.getDescription()).thenReturn(mockDatastream);
}
private FedoraResource setResource(final Class<? extends FedoraResource> klass) throws RepositoryException {
final FedoraResource mockResource = mock(klass);
doReturn(mockResource).when(testObj).resource();
when(mockResource.getNode()).thenReturn(mockNode);
when(mockResource.getPath()).thenReturn(path);
when(mockNode.getPath()).thenReturn(path);
when(mockResource.getEtagValue()).thenReturn("");
when(mockResource.getTriples(eq(identifierConverter), any(Class.class))).thenAnswer(new Answer<RdfStream>() {
@Override
public RdfStream answer(final InvocationOnMock invocationOnMock) throws Throwable {
return new RdfStream(Triple.create(NodeFactory.createURI(invocationOnMock.getMock().toString()),
NodeFactory.createURI("called"),
NodeFactory.createLiteral(
invocationOnMock.getArguments()[1].toString())
));
}
});
return mockResource;
}
@Test
public void testHead() throws Exception {
setResource(FedoraResource.class);
final Response actual = testObj.head();
assertEquals(OK.getStatusCode(), actual.getStatus());
assertTrue("Should have a Link header", mockResponse.containsHeader("Link"));
assertTrue("Should have an Allow header", mockResponse.containsHeader("Allow"));
assertTrue("Should be an LDP Resource",
mockResponse.getHeaders("Link").contains("<" + LDP_NAMESPACE + "Resource>;rel=\"type\""));
}
@Test
public void testHeadWithObject() throws Exception {
setResource(FedoraObject.class);
final Response actual = testObj.head();
assertEquals(OK.getStatusCode(), actual.getStatus());
assertTrue("Should be an LDP DirectContainer",
mockResponse.getHeaders("Link").contains("<" + LDP_NAMESPACE + "DirectContainer>;rel=\"type\""));
assertTrue("Should advertise Accept-Post flavors", mockResponse.containsHeader("Accept-Post"));
assertTrue("Should advertise Accept-Patch flavors", mockResponse.containsHeader("Accept-Patch"));
}
@Test
public void testHeadWithBinary() throws Exception {
final FedoraBinary mockResource = (FedoraBinary)setResource(FedoraBinary.class);
when(mockResource.getDescription()).thenReturn(mockDatastream);
final Response actual = testObj.head();
assertEquals(OK.getStatusCode(), actual.getStatus());
assertTrue("Should be an LDP NonRDFSource",
mockResponse.getHeaders("Link").contains("<" + LDP_NAMESPACE + "NonRDFSource>;rel=\"type\""));
assertFalse("Should not advertise Accept-Post flavors", mockResponse.containsHeader("Accept-Post"));
assertFalse("Should not advertise Accept-Patch flavors", mockResponse.containsHeader("Accept-Patch"));
assertTrue("Should contain a link to the binary description",
mockResponse.getHeaders("Link")
.contains("<" + identifierConverter.toDomain(binaryDescriptionPath + "/fcr:metadata")
+ ">;rel=\"describedby\""));
}
@Test
public void testHeadWithBinaryDescription() throws Exception {
final Datastream mockResource = (Datastream)setResource(Datastream.class);
when(mockResource.getBinary()).thenReturn(mockBinary);
final Response actual = testObj.head();
assertEquals(OK.getStatusCode(), actual.getStatus());
assertTrue("Should be an LDP RDFSource",
mockResponse.getHeaders("Link").contains("<" + LDP_NAMESPACE + "RDFSource>;rel=\"type\""));
assertFalse("Should not advertise Accept-Post flavors", mockResponse.containsHeader("Accept-Post"));
assertTrue("Should advertise Accept-Patch flavors", mockResponse.containsHeader("Accept-Patch"));
assertTrue("Should contain a link to the binary",
mockResponse.getHeaders("Link")
.contains("<" + identifierConverter.toDomain(binaryPath) + ">;rel=\"describes\""));
}
@Test
public void testOption() throws Exception {
setResource(FedoraResource.class);
final Response actual = testObj.options();
assertEquals(OK.getStatusCode(), actual.getStatus());
assertTrue("Should have an Allow header", mockResponse.containsHeader("Allow"));
}
@Test
public void testOptionWithObject() throws Exception {
setResource(FedoraObject.class);
final Response actual = testObj.options();
assertEquals(OK.getStatusCode(), actual.getStatus());
assertTrue("Should advertise Accept-Post flavors", mockResponse.containsHeader("Accept-Post"));
assertTrue("Should advertise Accept-Patch flavors", mockResponse.containsHeader("Accept-Patch"));
}
@Test
public void testOptionWithBinary() throws Exception {
final FedoraBinary mockResource = (FedoraBinary)setResource(FedoraBinary.class);
when(mockResource.getDescription()).thenReturn(mockDatastream);
final Response actual = testObj.options();
assertEquals(OK.getStatusCode(), actual.getStatus());
assertFalse("Should not advertise Accept-Post flavors", mockResponse.containsHeader("Accept-Post"));
assertFalse("Should not advertise Accept-Patch flavors", mockResponse.containsHeader("Accept-Patch"));
assertTrue("Should contain a link to the binary description",
mockResponse.getHeaders("Link")
.contains("<" + identifierConverter.toDomain(binaryDescriptionPath + "/fcr:metadata")
+ ">;rel=\"describedby\""));
}
@Test
public void testOptionWithBinaryDescription() throws Exception {
final Datastream mockResource = (Datastream)setResource(Datastream.class);
when(mockResource.getBinary()).thenReturn(mockBinary);
final Response actual = testObj.options();
assertEquals(OK.getStatusCode(), actual.getStatus());
assertFalse("Should not advertise Accept-Post flavors", mockResponse.containsHeader("Accept-Post"));
assertTrue("Should advertise Accept-Patch flavors", mockResponse.containsHeader("Accept-Patch"));
assertTrue("Should contain a link to the binary",
mockResponse.getHeaders("Link")
.contains("<" + identifierConverter.toDomain(binaryPath) + ">;rel=\"describes\""));
}
@Test
public void testGet() throws Exception {
setResource(FedoraResource.class);
final Response actual = testObj.describe(new Prefer(""), null);
assertEquals(OK.getStatusCode(), actual.getStatus());
assertTrue("Should have a Link header", mockResponse.containsHeader("Link"));
assertTrue("Should have an Allow header", mockResponse.containsHeader("Allow"));
assertTrue("Should be an LDP Resource",
mockResponse.getHeaders("Link").contains("<" + LDP_NAMESPACE + "Resource>;rel=\"type\""));
final RdfStream entity = (RdfStream) actual.getEntity();
final Model model = entity.asModel();
final List<String> rdfNodes = Lists.transform(Lists.newArrayList(model.listObjects()),
new Function<RDFNode, String>() {
@Override
public String apply(final RDFNode input) {
return input.toString();
}
});
assertTrue("Expected RDF contexts missing", rdfNodes.containsAll(ImmutableSet.of(
"class org.fcrepo.kernel.impl.rdf.impl.LdpContainerRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.LdpIsMemberOfRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ReferencesRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.TypeRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ContainerRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.PropertiesRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ChildrenRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.AclRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ParentRdfContext"
)));
}
@Test
public void testGetWithObject() throws Exception {
setResource(FedoraObject.class);
final Response actual = testObj.describe(new Prefer(""), null);
assertEquals(OK.getStatusCode(), actual.getStatus());
assertTrue("Should be an LDP DirectContainer",
mockResponse.getHeaders("Link").contains("<" + LDP_NAMESPACE + "DirectContainer>;rel=\"type\""));
assertTrue("Should advertise Accept-Post flavors", mockResponse.containsHeader("Accept-Post"));
assertTrue("Should advertise Accept-Patch flavors", mockResponse.containsHeader("Accept-Patch"));
final RdfStream entity = (RdfStream) actual.getEntity();
final Model model = entity.asModel();
final List<String> rdfNodes = Lists.transform(Lists.newArrayList(model.listObjects()),
new Function<RDFNode, String>() {
@Override
public String apply(final RDFNode input) {
return input.toString();
}
});
assertTrue("Expected RDF contexts missing", rdfNodes.containsAll(ImmutableSet.of(
"class org.fcrepo.kernel.impl.rdf.impl.LdpContainerRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.LdpIsMemberOfRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ReferencesRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.TypeRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ContainerRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.PropertiesRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ChildrenRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.AclRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ParentRdfContext"
)));
}
@Test
public void testGetWithObjectPreferMinimal() throws Exception {
setResource(FedoraObject.class);
final Response actual = testObj.describe(new Prefer("return=minimal"), null);
assertEquals(OK.getStatusCode(), actual.getStatus());
final RdfStream entity = (RdfStream) actual.getEntity();
final Model model = entity.asModel();
final List<String> rdfNodes = Lists.transform(Lists.newArrayList(model.listObjects()),
new Function<RDFNode, String>() {
@Override
public String apply(final RDFNode input) {
return input.toString();
}
});
assertTrue("Expected RDF contexts missing", rdfNodes.containsAll(ImmutableSet.of(
"class org.fcrepo.kernel.impl.rdf.impl.TypeRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.PropertiesRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.AclRdfContext"
)));
assertFalse("Included non-minimal contexts",
rdfNodes.contains("class org.fcrepo.kernel.impl.rdf.impl.LdpContainerRdfContext"));
assertFalse("Included non-minimal contexts",
rdfNodes.contains("class org.fcrepo.kernel.impl.rdf.impl.ChildrenRdfContext"));
}
@Test
public void testGetWithObjectOmitContainment() throws Exception {
setResource(FedoraObject.class);
final Response actual = testObj.describe(
new Prefer("return=representation; omit=\"" + LDP_NAMESPACE + "PreferContainment\""),
null);
assertEquals(OK.getStatusCode(), actual.getStatus());
final RdfStream entity = (RdfStream) actual.getEntity();
final Model model = entity.asModel();
final List<String> rdfNodes = Lists.transform(Lists.newArrayList(model.listObjects()),
new Function<RDFNode, String>() {
@Override
public String apply(final RDFNode input) {
return input.toString();
}
});
assertTrue("Should include membership contexts",
rdfNodes.contains("class org.fcrepo.kernel.impl.rdf.impl.LdpContainerRdfContext"));
assertFalse("Should not include containment contexts",
rdfNodes.contains("class org.fcrepo.kernel.impl.rdf.impl.ChildrenRdfContext"));
}
@Test
public void testGetWithObjectOmitMembership() throws Exception {
setResource(FedoraObject.class);
final Response actual = testObj.describe(
new Prefer("return=representation; omit=\"" + LDP_NAMESPACE + "PreferMembership\""),
null);
assertEquals(OK.getStatusCode(), actual.getStatus());
final RdfStream entity = (RdfStream) actual.getEntity();
final Model model = entity.asModel();
final List<String> rdfNodes = Lists.transform(Lists.newArrayList(model.listObjects()),
new Function<RDFNode, String>() {
@Override
public String apply(final RDFNode input) {
return input.toString();
}
});
assertFalse("Should not include membership contexts",
rdfNodes.contains("class org.fcrepo.kernel.impl.rdf.impl.LdpContainerRdfContext"));
assertFalse("Should not include membership contexts",
rdfNodes.contains("class org.fcrepo.kernel.impl.rdf.impl.LdpIsMemberOfRdfContext"));
assertTrue("Should include containment contexts",
rdfNodes.contains("class org.fcrepo.kernel.impl.rdf.impl.ChildrenRdfContext"));
}
@Test
public void testGetWithObjectOmitReferences() throws Exception {
setResource(FedoraObject.class);
final Response actual = testObj.describe(
new Prefer("return=representation; omit=\"" + INBOUND_REFERENCES + "\""),
null);
assertEquals(OK.getStatusCode(), actual.getStatus());
final RdfStream entity = (RdfStream) actual.getEntity();
final Model model = entity.asModel();
final List<String> rdfNodes = Lists.transform(Lists.newArrayList(model.listObjects()),
new Function<RDFNode, String>() {
@Override
public String apply(final RDFNode input) {
return input.toString();
}
});
assertFalse("Should not include references contexts",
rdfNodes.contains("class org.fcrepo.kernel.impl.rdf.impl.ReferencesRdfContext"));
}
@Test
public void testGetWithBinary() throws Exception {
final FedoraBinary mockResource = (FedoraBinary)setResource(FedoraBinary.class);
when(mockResource.getDescription()).thenReturn(mockDatastream);
when(mockResource.getMimeType()).thenReturn("text/plain");
when(mockResource.getContent()).thenReturn(toInputStream("xyz"));
final Response actual = testObj.describe(new Prefer(""), null);
assertEquals(OK.getStatusCode(), actual.getStatus());
assertTrue("Should be an LDP NonRDFSource",
mockResponse.getHeaders("Link").contains("<" + LDP_NAMESPACE + "NonRDFSource>;rel=\"type\""));
assertFalse("Should not advertise Accept-Post flavors", mockResponse.containsHeader("Accept-Post"));
assertFalse("Should not advertise Accept-Patch flavors", mockResponse.containsHeader("Accept-Patch"));
assertTrue("Should contain a link to the binary description",
mockResponse.getHeaders("Link")
.contains("<" + identifierConverter.toDomain(binaryDescriptionPath + "/fcr:metadata")
+ ">;rel=\"describedby\""));
assertTrue(IOUtils.toString((InputStream)actual.getEntity()).equals("xyz"));
}
@Test
public void testGetWithBinaryDescription() throws Exception {
final Datastream mockResource = (Datastream)setResource(Datastream.class);
when(mockResource.getBinary()).thenReturn(mockBinary);
final Response actual = testObj.describe(new Prefer(""), null);
assertEquals(OK.getStatusCode(), actual.getStatus());
assertTrue("Should be an LDP RDFSource",
mockResponse.getHeaders("Link").contains("<" + LDP_NAMESPACE + "RDFSource>;rel=\"type\""));
assertFalse("Should not advertise Accept-Post flavors", mockResponse.containsHeader("Accept-Post"));
assertTrue("Should advertise Accept-Patch flavors", mockResponse.containsHeader("Accept-Patch"));
assertTrue("Should contain a link to the binary",
mockResponse.getHeaders("Link")
.contains("<" + identifierConverter.toDomain(binaryPath) + ">;rel=\"describes\""));
final RdfStream entity = (RdfStream) actual.getEntity();
final Model model = entity.asModel();
final List<String> rdfNodes = Lists.transform(Lists.newArrayList(model.listObjects()),
new Function<RDFNode, String>() {
@Override
public String apply(final RDFNode input) {
return input.toString();
}
});
assertTrue("Expected RDF contexts missing", rdfNodes.containsAll(ImmutableSet.of(
"class org.fcrepo.kernel.impl.rdf.impl.LdpContainerRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.LdpIsMemberOfRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ReferencesRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.TypeRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ContainerRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.PropertiesRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ChildrenRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.AclRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ParentRdfContext"
)));
}
@Test
public void testDelete() throws Exception {
final FedoraResource fedoraResource = setResource(FedoraResource.class);
final Response actual = testObj.deleteObject();
assertEquals(NO_CONTENT.getStatusCode(), actual.getStatus());
verify(fedoraResource).delete();
}
@Test
public void testPutNewObject() throws Exception {
setField(testObj, "externalPath", "some/path");
when(mockObject.isNew()).thenReturn(true);
when(mockNodeService.exists(mockSession, "/some/path")).thenReturn(false);
when(mockObjectService.findOrCreateObject(mockSession, "/some/path")).thenReturn(mockObject);
final Response actual = testObj.createOrReplaceObjectRdf(null, null, null, null, null);
assertEquals(CREATED.getStatusCode(), actual.getStatus());
}
@Test
public void testPutNewObjectWithRdf() throws Exception {
setField(testObj, "externalPath", "some/path");
when(mockObject.isNew()).thenReturn(true);
when(mockNodeService.exists(mockSession, "/some/path")).thenReturn(false);
when(mockObjectService.findOrCreateObject(mockSession, "/some/path")).thenReturn(mockObject);
final Response actual = testObj.createOrReplaceObjectRdf(NTRIPLES_TYPE,
toInputStream("_:a <info:x> _:c ."), null, null, null);
assertEquals(CREATED.getStatusCode(), actual.getStatus());
verify(mockObject).replaceProperties(eq(identifierConverter), any(Model.class), any(RdfStream.class));
}
@Test
public void testPutNewBinary() throws Exception {
setField(testObj, "externalPath", "some/path");
when(mockBinary.isNew()).thenReturn(true);
when(mockNodeService.exists(mockSession, "/some/path")).thenReturn(false);
when(mockBinaryService.findOrCreateBinary(mockSession, "/some/path")).thenReturn(mockBinary);
final Response actual = testObj.createOrReplaceObjectRdf(TEXT_PLAIN_TYPE,
toInputStream("xyz"), null, null, null);
assertEquals(CREATED.getStatusCode(), actual.getStatus());
}
@Test
public void testPutReplaceRdfObject() throws Exception {
setField(testObj, "externalPath", "some/path");
final FedoraObject mockObject = (FedoraObject)setResource(FedoraObject.class);
doReturn(mockObject).when(testObj).resource();
when(mockObject.isNew()).thenReturn(false);
when(mockNodeService.exists(mockSession, "/some/path")).thenReturn(true);
when(mockObjectService.findOrCreateObject(mockSession, "/some/path")).thenReturn(mockObject);
final Response actual = testObj.createOrReplaceObjectRdf(NTRIPLES_TYPE,
toInputStream("_:a <info:x> _:c ."), null, null, null);
assertEquals(NO_CONTENT.getStatusCode(), actual.getStatus());
verify(mockObject).replaceProperties(eq(identifierConverter), any(Model.class), any(RdfStream.class));
}
@Test(expected = ClientErrorException.class)
public void testPutWithStrictIfMatchHandling() throws Exception {
when(mockHttpConfiguration.putRequiresIfMatch()).thenReturn(true);
final FedoraObject mockObject = (FedoraObject)setResource(FedoraObject.class);
doReturn(mockObject).when(testObj).resource();
when(mockObject.isNew()).thenReturn(false);
when(mockNodeService.exists(mockSession, "/some/path")).thenReturn(true);
when(mockObjectService.findOrCreateObject(mockSession, "/some/path")).thenReturn(mockObject);
testObj.createOrReplaceObjectRdf(NTRIPLES_TYPE,
toInputStream("_:a <info:x> _:c ."), null, null, null);
}
@Test
public void testPatchObject() throws Exception {
final FedoraResource mockObject = setResource(FedoraObject.class);
testObj.updateSparql(toInputStream("xyz"));
}
@Test
public void testPatchBinaryDescription() throws Exception {
final Datastream mockObject = (Datastream)setResource(Datastream.class);
doReturn(mockObject).when(testObj).resource();
testObj.updateSparql(toInputStream("xyz"));
}
@Test(expected = BadRequestException.class)
public void testPatchWithoutContent() throws Exception {
testObj.updateSparql(null);
}
@Test(expected = BadRequestException.class)
public void testPatchWithMissingContent() throws Exception {
setResource(FedoraObject.class);
testObj.updateSparql(toInputStream(""));
}
@Test(expected = BadRequestException.class)
public void testPatchBinary() throws Exception {
setResource(FedoraBinary.class);
testObj.updateSparql(toInputStream(""));
}
@Test
public void testCreateNewObject() throws Exception {
setResource(FedoraObject.class);
when(mockObjectService.findOrCreateObject(mockSession, "/b")).thenReturn(mockObject);
final Response actual = testObj.createObject(null, null, null, null, "b", null);
assertEquals(CREATED.getStatusCode(), actual.getStatus());
}
@Test
public void testCreateNewObjectWithSparql() throws Exception {
setResource(FedoraObject.class);
when(mockObjectService.findOrCreateObject(mockSession, "/b")).thenReturn(mockObject);
final Response actual = testObj.createObject(null, null, null,
MediaType.valueOf(contentTypeSPARQLUpdate), "b", toInputStream("x"));
assertEquals(CREATED.getStatusCode(), actual.getStatus());
verify(mockObject).updateProperties(eq(identifierConverter), eq("x"), any(RdfStream.class));
}
@Test
public void testCreateNewObjectWithRdf() throws Exception {
setResource(FedoraObject.class);
when(mockObjectService.findOrCreateObject(mockSession, "/b")).thenReturn(mockObject);
final Response actual = testObj.createObject(null, null, null, NTRIPLES_TYPE, "b",
toInputStream("_:a <info:b> _:c ."));
assertEquals(CREATED.getStatusCode(), actual.getStatus());
verify(mockObject).replaceProperties(eq(identifierConverter), any(Model.class), any(RdfStream.class));
}
@Test
public void testCreateNewBinary() throws Exception {
setResource(FedoraObject.class);
when(mockBinaryService.findOrCreateBinary(mockSession, "/b")).thenReturn(mockBinary);
final InputStream content = toInputStream("x");
final Response actual = testObj.createObject(null, null, null, APPLICATION_OCTET_STREAM_TYPE, "b",
content);
assertEquals(CREATED.getStatusCode(), actual.getStatus());
verify(mockBinary).setContent(content, APPLICATION_OCTET_STREAM, null, "", null);
}
@Test(expected = ClientErrorException.class)
public void testPostToBinary() throws Exception {
final FedoraBinary mockObject = (FedoraBinary)setResource(FedoraBinary.class);
doReturn(mockObject).when(testObj).resource();
testObj.createObject(null, null, null, null, null, null);
}
@Test
public void testGetSimpleContentType() {
final MediaType mediaType = new MediaType("text", "plain", ImmutableMap.of("charset", "UTF-8"));
final MediaType sanitizedMediaType = getSimpleContentType(mediaType);
assertEquals("text/plain", sanitizedMediaType.toString());
}
}