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;
021
022import java.io.ByteArrayInputStream;
023import java.io.IOException;
024import java.util.UUID;
025
026import org.apache.http.HttpResponse;
027import org.apache.http.ParseException;
028import org.apache.http.client.methods.HttpGet;
029import org.apache.http.client.methods.HttpPost;
030import org.apache.http.entity.BasicHttpEntity;
031import org.apache.http.util.EntityUtils;
032import org.junit.Test;
033import org.springframework.test.annotation.DirtiesContext;
034import org.springframework.test.annotation.DirtiesContext.ClassMode;
035import org.springframework.test.context.ContextConfiguration;
036
037import com.fasterxml.jackson.core.JsonFactory;
038import com.fasterxml.jackson.databind.JsonNode;
039import com.fasterxml.jackson.databind.ObjectMapper;
040
041
042/**
043 * <p>FedoraTransformIT class.</p>
044 *
045 * @author cbeer
046 */
047@ContextConfiguration({"/spring-test/test-container.xml"})
048@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
049public class FedoraTransformIT extends AbstractResourceIT {
050
051    @Test
052    public void testLdpathWithConfiguredProgram() throws IOException {
053
054        final String pid = "testLdpathWithConfiguredProgram-" + randomUUID();
055        createObject(pid);
056        final HttpGet postLdpathProgramRequest
057                = new HttpGet(serverAddress + "/" + pid + "/fcr:transform/default");
058        final HttpResponse response = client.execute(postLdpathProgramRequest);
059        assertEquals(200, response.getStatusLine().getStatusCode());
060        final String content = EntityUtils.toString(response.getEntity());
061        logger.debug("Retrieved ldpath feed:\n" + content);
062
063        final JsonNode rootNode = new ObjectMapper().readTree(new JsonFactory().createParser(content));
064
065        assertEquals("Failed to retrieve correct identifier in JSON!", serverAddress + "/" + pid,
066                rootNode.get(0).get("id").elements().next().asText());
067
068    }
069
070    @Test
071    public void testLdpathWithProgramBody() throws ParseException, IOException {
072
073        final String pid = UUID.randomUUID().toString();
074        createObject(pid);
075
076        final HttpPost postLdpathProgramRequest = new HttpPost(serverAddress + "/" + pid + "/fcr:transform");
077        final BasicHttpEntity e = new BasicHttpEntity();
078
079        final String s = "id = . :: xsd:string ;\n";
080
081        e.setContent(new ByteArrayInputStream(s.getBytes()));
082
083        postLdpathProgramRequest.setEntity(e);
084        postLdpathProgramRequest.setHeader("Content-Type", APPLICATION_RDF_LDPATH);
085        final HttpResponse response = client.execute(postLdpathProgramRequest);
086        assertEquals(200, response.getStatusLine().getStatusCode());
087        final String content = EntityUtils.toString(response.getEntity());
088        logger.debug("Retrieved LDPath result:\n" + content);
089
090        final JsonNode rootNode = new ObjectMapper().readTree(new JsonFactory().createParser(content));
091
092        assertEquals("Failed to retrieve correct identifier in JSON!", serverAddress + "/" + pid, rootNode
093                .get(0).get("id").elements().next().asText());
094
095    }
096}