001package org.w3.ldp.testsuite.test;
002
003import com.hp.hpl.jena.rdf.model.Model;
004import com.jayway.restassured.response.Response;
005import org.apache.http.HttpStatus;
006import org.testng.annotations.AfterSuite;
007import org.testng.annotations.BeforeSuite;
008import org.testng.annotations.Optional;
009import org.testng.annotations.Parameters;
010import org.w3.ldp.testsuite.exception.SkipException;
011import org.w3.ldp.testsuite.mapper.RdfObjectMapper;
012
013import java.io.IOException;
014
015import static org.w3.ldp.testsuite.http.HttpHeaders.LOCATION;
016import static org.w3.ldp.testsuite.http.MediaTypes.TEXT_TURTLE;
017
018/**
019 * Tests that run on an LDP-RS that is not a container.
020 */
021public class MemberResourceTest extends RdfSourceTest {
022        private static final String SETUP_ERROR = "ERROR: Could not create test resource for MemberResourceTest. Skipping tests.";
023
024        private String container;
025        private String memberResource;
026
027        @Parameters("auth")
028        public MemberResourceTest(@Optional String auth) throws IOException {
029                super(auth);
030        }
031
032        /*
033         * Creates a resource to test if there's no memberResource test parameter.
034         */
035        @Parameters({"memberResource", "directContainer", "indirectContainer", "basicContainer", "memberTtl"})
036        @BeforeSuite(alwaysRun = true)
037        public void createTestResource(@Optional String memberResource, @Optional String directContainer,
038                        @Optional String indirectContainer, @Optional String basicContainer,
039                        @Optional String memberTtl) {
040                // If resource is defined, use that. Otherwise, fall back to creating one from one of the containers.
041                if (memberResource != null) {
042                        this.memberResource = memberResource;
043                } else if (directContainer != null) {
044                        this.container = directContainer;
045                } else if (indirectContainer != null) {
046                        this.container = indirectContainer;
047                } else if (basicContainer != null) {
048                        this.container = basicContainer;
049                } else {
050                        throw new SkipException(Thread.currentThread().getStackTrace()[1].getMethodName(),
051                                        "No memberResource or container parameters defined in testng.xml", skipLog);
052                }
053
054                if (this.memberResource == null) {
055                        try {
056                                Model model = this.readModel(memberTtl);
057                                if (model == null) {
058                                        model = this.getDefaultModel();
059                                }
060
061                                Response postResponse = buildBaseRequestSpecification()
062                                                .contentType(TEXT_TURTLE)
063                                                .body(model, new RdfObjectMapper())
064                                                .post(this.container);
065                                if (postResponse.getStatusCode() != HttpStatus.SC_CREATED) {
066                                        System.err.println(SETUP_ERROR);
067                                        System.err.println("POST failed with status code: " + postResponse.getStatusCode());
068                                        System.err.println();
069                                        return;
070                                }
071
072                                this.memberResource = postResponse.getHeader(LOCATION);
073                                if (this.memberResource == null) {
074                                        System.err.println(SETUP_ERROR);
075                                        System.err.println("Location response header missing");
076                                        System.err.println();
077                                        return;
078                                }
079                        } catch (Exception e) {
080                                System.err.println(SETUP_ERROR);
081                                e.printStackTrace();
082                        }
083                }
084        }
085
086        @Override
087        protected String getResourceUri() {
088                if (memberResource == null) {
089                        throw new SkipException(Thread.currentThread().getStackTrace()[2].getMethodName(),
090                                        "Skipping test because test resource is null.", skipLog);
091                }
092                return memberResource;
093        }
094
095        /*
096         * Deletes the test resource to clean up if it's wasn't provided using the
097         * memberResource test parameter.
098         */
099        @AfterSuite(alwaysRun = true)
100        public void deleteTestResource() {
101                // If container isn't null, we created the resource ourselves. To clean up, delete the resource.
102                if (container != null) {
103                        buildBaseRequestSpecification().delete(memberResource);
104                }
105        }
106}