001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.integration; 019 020import static java.util.UUID.randomUUID; 021import static javax.ws.rs.core.Response.Status.CREATED; 022import static javax.ws.rs.core.Response.Status.OK; 023import static org.fcrepo.transform.transformations.LDPathTransform.APPLICATION_RDF_LDPATH; 024import static org.junit.Assert.assertEquals; 025import static org.junit.Assert.assertNotNull; 026import static org.junit.Assert.assertTrue; 027 028import java.io.ByteArrayInputStream; 029import java.io.IOException; 030import java.io.InputStream; 031import java.util.UUID; 032 033import org.apache.http.HttpResponse; 034import org.apache.http.ParseException; 035import org.apache.http.client.methods.CloseableHttpResponse; 036import org.apache.http.client.methods.HttpGet; 037import org.apache.http.client.methods.HttpPost; 038import org.apache.http.client.methods.HttpPut; 039import org.apache.http.entity.BasicHttpEntity; 040import org.apache.http.entity.InputStreamEntity; 041import org.apache.http.util.EntityUtils; 042import org.junit.Test; 043import org.springframework.test.annotation.DirtiesContext; 044import org.springframework.test.annotation.DirtiesContext.ClassMode; 045import org.springframework.test.context.ContextConfiguration; 046 047import com.fasterxml.jackson.core.JsonFactory; 048import com.fasterxml.jackson.databind.JsonNode; 049import com.fasterxml.jackson.databind.ObjectMapper; 050 051 052/** 053 * <p>FedoraTransformIT class.</p> 054 * 055 * @author cbeer 056 */ 057@ContextConfiguration({"/spring-test/test-container.xml"}) 058@DirtiesContext(classMode = ClassMode.AFTER_CLASS) 059public class FedoraTransformIT extends AbstractResourceIT { 060 061 // This regex represents the following pattern: yyyy-MM-dd'T'HH:mm:ss.SSS'Z' 062 // see: JsonObjectProvider.DATE_FORMAT 063 private final String DATE_TIME_REGEX = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z"; 064 065 @Test 066 public void testLdpathWithDefaultProgram() throws IOException { 067 068 final String pid = "testLdpathWithConfiguredProgram-" + randomUUID(); 069 createObject(pid); 070 final HttpGet postLdpathProgramRequest 071 = new HttpGet(serverAddress + "/" + pid + "/fcr:transform/default"); 072 final HttpResponse response = client.execute(postLdpathProgramRequest); 073 assertEquals(200, response.getStatusLine().getStatusCode()); 074 final String content = EntityUtils.toString(response.getEntity()); 075 logger.debug("Retrieved ldpath feed:\n" + content); 076 077 final JsonNode rootNode = new ObjectMapper().readTree(new JsonFactory().createParser(content)); 078 079 assertEquals("Failed to retrieve correct identifier in JSON!", serverAddress + "/" + pid, 080 rootNode.get(0).get("id").elements().next().asText()); 081 082 final JsonNode creationDateJson = rootNode.get(0).get("created"); 083 assertNotNull(creationDateJson); 084 085 final JsonNode dateNode = creationDateJson.get(0); 086 assertNotNull(dateNode); 087 assertTrue(dateNode.asText() + " should be of format: " + DATE_TIME_REGEX, 088 dateNode.asText().matches(DATE_TIME_REGEX)); 089 090 } 091 092 @Test 093 public void testLdpathWithDeluxeProgram() throws IOException { 094 095 final String pid = "testLdpathWithDeluxeProgram-" + randomUUID(); 096 createObject(pid); 097 final HttpGet postLdpathProgramRequest 098 = new HttpGet(serverAddress + "/" + pid + "/fcr:transform/deluxe"); 099 final HttpResponse response = client.execute(postLdpathProgramRequest); 100 assertEquals(200, response.getStatusLine().getStatusCode()); 101 final String content = EntityUtils.toString(response.getEntity()); 102 logger.debug("Retrieved ldpath feed:\n" + content); 103 104 final JsonNode rootNode = new ObjectMapper().readTree(new JsonFactory().createParser(content)); 105 106 assertEquals("Failed to retrieve correct identifier in JSON!", serverAddress + "/" + pid, 107 rootNode.get(0).get("id").elements().next().asText()); 108 109 assertNotNull(rootNode.get(0).get("createdBy")); 110 assertNotNull(rootNode.get(0).get("hasParent")); 111 assertNotNull(rootNode.get(0).get("hasVersions")); 112 assertNotNull(rootNode.get(0).get("lastModified")); 113 assertNotNull(rootNode.get(0).get("lastModifiedBy")); 114 assertNotNull(rootNode.get(0).get("numberOfChildren")); 115 assertNotNull(rootNode.get(0).get("type")); 116 assertNotNull(rootNode.get(0).get("prefLabel")); 117 assertNotNull(rootNode.get(0).get("altLabel")); 118 119 final JsonNode creationDateJson = rootNode.get(0).get("created"); 120 assertNotNull(creationDateJson); 121 122 final JsonNode dateNode = creationDateJson.get(0); 123 assertNotNull(dateNode); 124 assertTrue(dateNode.asText() + " should be of format: " + DATE_TIME_REGEX, 125 dateNode.asText().matches(DATE_TIME_REGEX)); 126 127 } 128 129 @Test 130 public void testLdpathWithProgramBody() throws ParseException, IOException { 131 132 final String pid = UUID.randomUUID().toString(); 133 createObject(pid); 134 135 final HttpPost postLdpathProgramRequest = new HttpPost(serverAddress + "/" + pid + "/fcr:transform"); 136 final BasicHttpEntity e = new BasicHttpEntity(); 137 138 final String s = "id = . :: xsd:string ;\n"; 139 140 e.setContent(new ByteArrayInputStream(s.getBytes())); 141 142 postLdpathProgramRequest.setEntity(e); 143 postLdpathProgramRequest.setHeader("Content-Type", APPLICATION_RDF_LDPATH); 144 final HttpResponse response = client.execute(postLdpathProgramRequest); 145 assertEquals(200, response.getStatusLine().getStatusCode()); 146 final String content = EntityUtils.toString(response.getEntity()); 147 logger.debug("Retrieved LDPath result:\n" + content); 148 149 final JsonNode rootNode = new ObjectMapper().readTree(new JsonFactory().createParser(content)); 150 151 assertEquals("Failed to retrieve correct identifier in JSON!", serverAddress + "/" + pid, rootNode 152 .get(0).get("id").elements().next().asText()); 153 154 } 155 156 @Test 157 public void testMakeReferenceToTransformSpace() throws IOException { 158 final String pid = UUID.randomUUID().toString(); 159 createObject(pid); 160 161 final HttpGet getTransformRequest = new HttpGet(serverAddress + "/" + pid + "/fcr:transform/default"); 162 try (final CloseableHttpResponse getResponse = (CloseableHttpResponse) client.execute(getTransformRequest)) { 163 assertEquals("Can't get default transform", OK.getStatusCode(), 164 getResponse.getStatusLine().getStatusCode()); 165 } 166 167 final String pid2 = UUID.randomUUID().toString(); 168 final HttpPut putReferenceRequest = new HttpPut(serverAddress + "/" + pid2); 169 170 final InputStream turtleFile = this.getClass().getResourceAsStream("/referenceTest.ttl"); 171 final InputStreamEntity entity = new InputStreamEntity(turtleFile); 172 putReferenceRequest.setEntity(entity); 173 putReferenceRequest.setHeader("Content-type", "text/turtle"); 174 175 try (final CloseableHttpResponse putResponse = (CloseableHttpResponse) client.execute(putReferenceRequest)) { 176 assertEquals("Can't make reference to /fedora:system/fedora:transform", CREATED.getStatusCode(), 177 putResponse.getStatusLine().getStatusCode()); 178 } 179 } 180}