001/*
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.integration;
017
018import static java.util.UUID.randomUUID;
019import static org.fcrepo.transform.transformations.LDPathTransform.APPLICATION_RDF_LDPATH;
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertTrue;
023
024import java.io.ByteArrayInputStream;
025import java.io.IOException;
026import java.util.UUID;
027
028import org.apache.http.HttpResponse;
029import org.apache.http.ParseException;
030import org.apache.http.client.methods.HttpGet;
031import org.apache.http.client.methods.HttpPost;
032import org.apache.http.entity.BasicHttpEntity;
033import org.apache.http.util.EntityUtils;
034import org.fcrepo.transform.http.responses.JsonObjectProvider;
035import org.junit.Test;
036import org.springframework.test.annotation.DirtiesContext;
037import org.springframework.test.annotation.DirtiesContext.ClassMode;
038import org.springframework.test.context.ContextConfiguration;
039
040import com.fasterxml.jackson.core.JsonFactory;
041import com.fasterxml.jackson.databind.JsonNode;
042import com.fasterxml.jackson.databind.ObjectMapper;
043
044
045/**
046 * <p>FedoraTransformIT class.</p>
047 *
048 * @author cbeer
049 */
050@ContextConfiguration({"/spring-test/test-container.xml"})
051@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
052public class FedoraTransformIT extends AbstractResourceIT {
053
054    // This regex represents the following pattern: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
055    //  see: JsonObjectProvider.DATE_FORMAT
056    private final String DATE_TIME_REGEX = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z";
057
058    @Test
059    public void testLdpathWithConfiguredProgram() throws IOException {
060
061        final String pid = "testLdpathWithConfiguredProgram-" + randomUUID();
062        createObject(pid);
063        final HttpGet postLdpathProgramRequest
064                = new HttpGet(serverAddress + "/" + pid + "/fcr:transform/default");
065        final HttpResponse response = client.execute(postLdpathProgramRequest);
066        assertEquals(200, response.getStatusLine().getStatusCode());
067        final String content = EntityUtils.toString(response.getEntity());
068        logger.debug("Retrieved ldpath feed:\n" + content);
069
070        final JsonNode rootNode = new ObjectMapper().readTree(new JsonFactory().createParser(content));
071
072        assertEquals("Failed to retrieve correct identifier in JSON!", serverAddress + "/" + pid,
073                rootNode.get(0).get("id").elements().next().asText());
074
075        final JsonNode creationDateJson = rootNode.get(0).get("created");
076        assertNotNull(creationDateJson);
077
078        final JsonNode dateNode = creationDateJson.get(0);
079        assertNotNull(dateNode);
080        assertTrue(dateNode.asText() + " should be of format: " + DATE_TIME_REGEX,
081                dateNode.asText().matches(DATE_TIME_REGEX));
082
083    }
084
085    @Test
086    public void testLdpathWithProgramBody() throws ParseException, IOException {
087
088        final String pid = UUID.randomUUID().toString();
089        createObject(pid);
090
091        final HttpPost postLdpathProgramRequest = new HttpPost(serverAddress + "/" + pid + "/fcr:transform");
092        final BasicHttpEntity e = new BasicHttpEntity();
093
094        final String s = "id = . :: xsd:string ;\n";
095
096        e.setContent(new ByteArrayInputStream(s.getBytes()));
097
098        postLdpathProgramRequest.setEntity(e);
099        postLdpathProgramRequest.setHeader("Content-Type", APPLICATION_RDF_LDPATH);
100        final HttpResponse response = client.execute(postLdpathProgramRequest);
101        assertEquals(200, response.getStatusLine().getStatusCode());
102        final String content = EntityUtils.toString(response.getEntity());
103        logger.debug("Retrieved LDPath result:\n" + content);
104
105        final JsonNode rootNode = new ObjectMapper().readTree(new JsonFactory().createParser(content));
106
107        assertEquals("Failed to retrieve correct identifier in JSON!", serverAddress + "/" + pid, rootNode
108                .get(0).get("id").elements().next().asText());
109
110    }
111}