-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get Deposit application running against f4 #538
Changes from 1 commit
2b94f56
49705ab
efefb61
07950bc
e4229bd
0238789
f82b1b1
7f480b6
bd255d7
2715a19
a00df3e
0bae7d8
4ffacee
168a482
905e468
be927d3
0aac666
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/** | ||
* Copyright 2016 The University of North Carolina at Chapel Hill | ||
* | ||
* 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 edu.unc.lib.dl.fcrepo4; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.fcrepo.client.FcrepoClient; | ||
import org.fcrepo.client.FcrepoOperationFailedException; | ||
import org.fcrepo.client.FcrepoResponse; | ||
|
||
import com.hp.hpl.jena.rdf.model.Model; | ||
import com.hp.hpl.jena.rdf.model.ModelFactory; | ||
import com.hp.hpl.jena.rdf.model.Resource; | ||
import com.hp.hpl.jena.vocabulary.DC; | ||
|
||
import edu.unc.lib.dl.fedora.FedoraException; | ||
import edu.unc.lib.dl.util.URIUtil; | ||
|
||
/** | ||
* Initializes the structure of the repository | ||
* | ||
* @author bbpennel | ||
* | ||
*/ | ||
public class RepositoryInitializer { | ||
|
||
private Repository repository; | ||
|
||
private RepositoryObjectFactory objFactory; | ||
|
||
private FcrepoClient fcrepoClient; | ||
|
||
public RepositoryInitializer() { | ||
} | ||
|
||
/** | ||
* Initializes objects required for the base functionality of the repository | ||
*/ | ||
public void initializeRepository() { | ||
// Initialize the content base container | ||
URI contentUri = createContainer(RepositoryPathConstants.CONTENT_BASE, | ||
"Content Tree"); | ||
|
||
// Add the content tree root object | ||
createContentRoot(contentUri); | ||
|
||
// Initialize the path where deposit records are stored | ||
createContainer(RepositoryPathConstants.DEPOSIT_RECORD_BASE, | ||
"Deposit Records"); | ||
} | ||
|
||
private URI createContainer(String id, String title) { | ||
String containerString = URIUtil.join(repository.getFedoraBase(), id); | ||
URI containerUri = URI.create(containerString); | ||
|
||
// Abort initialization of already present container | ||
if (objectExists(containerUri)) { | ||
return containerUri; | ||
} | ||
|
||
Model model = ModelFactory.createDefaultModel(); | ||
Resource resc = model.createResource(containerString); | ||
resc.addProperty(DC.title, title); | ||
|
||
objFactory.createObject(containerUri, model); | ||
|
||
return containerUri; | ||
} | ||
|
||
private URI createContentRoot(URI contentUri) { | ||
String contentRootString = URIUtil.join( | ||
contentUri, RepositoryPathConstants.CONTENT_ROOT_ID); | ||
URI contentRootUri = URI.create(contentRootString); | ||
|
||
// Don't initialize the object if it is already present. | ||
if (objectExists(contentRootUri)) { | ||
return contentUri; | ||
} | ||
|
||
Model model = ModelFactory.createDefaultModel(); | ||
Resource resc = model.createResource(contentRootString); | ||
resc.addProperty(DC.title, "Content Collections Root"); | ||
|
||
|
||
objFactory.createContentRootObject(contentRootUri, model); | ||
|
||
return contentRootUri; | ||
} | ||
|
||
private boolean objectExists(URI uri) { | ||
try (FcrepoResponse response = fcrepoClient.head(uri) | ||
.perform()) { | ||
return true; | ||
} catch (IOException e) { | ||
throw new FedoraException("Failed to close HEAD response for " + uri, e); | ||
} catch (FcrepoOperationFailedException e) { | ||
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) { | ||
return false; | ||
} | ||
throw new FedoraException("Failed to check on object " + uri | ||
+ " during initialization", e); | ||
} | ||
} | ||
|
||
public void setRepository(Repository repository) { | ||
this.repository = repository; | ||
} | ||
|
||
public void setObjFactory(RepositoryObjectFactory objFactory) { | ||
this.objFactory = objFactory; | ||
} | ||
|
||
public void setFcrepoClient(FcrepoClient fcrepoClient) { | ||
this.fcrepoClient = fcrepoClient; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package edu.unc.lib.dl.fcrepo4; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.net.URI; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.fcrepo.client.FcrepoResponse; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import com.hp.hpl.jena.rdf.model.Model; | ||
import com.hp.hpl.jena.rdf.model.Resource; | ||
import com.hp.hpl.jena.vocabulary.RDF; | ||
|
||
import edu.unc.lib.dl.rdf.Cdr; | ||
import edu.unc.lib.dl.util.URIUtil; | ||
|
||
public class RepositoryInitializerIT extends AbstractFedoraIT { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does the IT stand for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Integration Test" |
||
|
||
private RepositoryInitializer repoInitializer; | ||
|
||
@Autowired | ||
private Repository repository; | ||
|
||
@Autowired | ||
private RepositoryObjectFactory objFactory; | ||
|
||
@Before | ||
public void init() { | ||
repoInitializer = new RepositoryInitializer(); | ||
repoInitializer.setObjFactory(objFactory); | ||
repoInitializer.setRepository(repository); | ||
repoInitializer.setFcrepoClient(client); | ||
} | ||
|
||
/** | ||
* Ensure that expected objects were initialized | ||
* | ||
* @throws Exception | ||
*/ | ||
@Test | ||
public void fullInitializationTest() throws Exception { | ||
repoInitializer.initializeRepository(); | ||
|
||
URI contentContainerUri = getContainerUri(RepositoryPathConstants.CONTENT_BASE); | ||
assertObjectExists(contentContainerUri); | ||
|
||
String contentRootString = URIUtil.join( | ||
contentContainerUri, RepositoryPathConstants.CONTENT_ROOT_ID); | ||
URI contentRootUri = URI.create(contentRootString); | ||
assertObjectExists(contentRootUri); | ||
Model crModel = repository.getObjectModel(contentRootUri); | ||
Resource crResc = crModel.getResource(contentRootUri.toString()); | ||
assertTrue(crResc.hasProperty(RDF.type, Cdr.ContentRoot)); | ||
|
||
URI depositContainerUri = getContainerUri(RepositoryPathConstants.DEPOSIT_RECORD_BASE); | ||
assertObjectExists(depositContainerUri); | ||
} | ||
|
||
/** | ||
* Show that additional initialization calls after the first do no cause | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...not cause... |
||
* objects to be modified or recreated | ||
* | ||
* @throws Exception | ||
*/ | ||
@Test | ||
public void multipleInitializeTest() throws Exception { | ||
repoInitializer.initializeRepository(); | ||
|
||
URI contentContainerUri = getContainerUri(RepositoryPathConstants.CONTENT_BASE); | ||
String contentContainerEtag = getEtag(contentContainerUri); | ||
|
||
String contentRootString = URIUtil.join( | ||
contentContainerUri, RepositoryPathConstants.CONTENT_ROOT_ID); | ||
URI contentRootUri = URI.create(contentRootString); | ||
String contentRootEtag = getEtag(contentRootUri); | ||
|
||
URI depositContainerUri = getContainerUri(RepositoryPathConstants.DEPOSIT_RECORD_BASE); | ||
String depositContainerEtag = getEtag(depositContainerUri); | ||
|
||
repoInitializer.initializeRepository(); | ||
|
||
assertEquals("Content Container object changed after second initialization", | ||
contentContainerEtag, getEtag(contentContainerUri)); | ||
assertEquals("Content Root object changed after second initialization", | ||
contentRootEtag, getEtag(contentRootUri)); | ||
assertEquals("Deposit Container object changed after second initialization", | ||
depositContainerEtag, getEtag(depositContainerUri)); | ||
} | ||
|
||
private String getEtag(URI uri) throws Exception { | ||
try (FcrepoResponse response = client.head(uri).perform()) { | ||
assertEquals(HttpStatus.SC_OK, response.getStatusCode()); | ||
|
||
String etag = response.getHeaderValue("ETag"); | ||
return etag.substring(1, etag.length() - 1); | ||
} | ||
} | ||
|
||
private URI getContainerUri(String id) { | ||
String containerString = URIUtil.join(repository.getFedoraBase(), id); | ||
return URI.create(containerString); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this return contentRootUri?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, it totally should