org.iavante.sling.s3backend.S3BackendTestIT.java Source code

Java tutorial

Introduction

Here is the source code for org.iavante.sling.s3backend.S3BackendTestIT.java

Source

/*
 * Digital Assets Management
 * =========================
 * 
 * Copyright 2009 Fundacin Iavante
 * 
 * Authors: 
 *   Francisco Jos Moreno Llorca <packo@assamita.net>
 *   Francisco Jess Gonzlez Mata <chuspb@gmail.com>
 *   Juan Antonio Guzmn Hidalgo <juan@guzmanhidalgo.com>
 *   Daniel de la Cuesta Navarrete <cues7a@gmail.com>
 *   Manuel Jos Cobo Fernndez <ranrrias@gmail.com>
 *
 * Licensed under the EUPL, Version 1.1 or  as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://ec.europa.eu/idabc/eupl
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and 
 * limitations under the Licence.
 * 
 */

package org.iavante.sling.s3backend;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileInputStream; //import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import junit.framework.TestCase;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Test validation filter
 * 
 */
public class S3BackendTestIT extends TestCase {

    private String CATALOG_URL = "/content/colecciones/";

    private String title;
    private String schema;
    private String slug;
    private Credentials defaultcreds;
    private List authPrefs = new ArrayList(2);

    private final String HOSTVAR = "SLINGHOST";
    private final String HOSTPREDEF = "localhost:8888";
    private String SLING_URL = "http://";
    private String APPS_URL = "/apps/gad/test3/GET.jsp";

    private static final Logger log = LoggerFactory.getLogger(S3BackendTestIT.class);

    /**
     * Log node
     */
    void log(String text) {
        log.info(text);
    }

    HttpClient client;

    protected void setUp() {

        Map<String, String> envs = System.getenv();
        Set<String> keys = envs.keySet();

        Iterator<String> it = keys.iterator();
        boolean hashost = false;
        while (it.hasNext()) {
            String key = (String) it.next();

            if (key.compareTo(HOSTVAR) == 0) {
                SLING_URL = SLING_URL + (String) envs.get(key);
                hashost = true;
            }
        }
        if (hashost == false)
            SLING_URL = SLING_URL + HOSTPREDEF;

        client = new HttpClient();
        title = "Test case content";
        schema = "default";
        slug = "test_case_contents3";
        authPrefs.add(AuthPolicy.DIGEST);
        authPrefs.add(AuthPolicy.BASIC);
        defaultcreds = new UsernamePasswordCredentials("admin", "admin");

        client.getParams().setAuthenticationPreemptive(true);
        client.getState().setCredentials(AuthScope.ANY, defaultcreds);
        client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);

        createTest3();
        try {
            uploadContent();
        } catch (FileNotFoundException e) {
            log.info("ERROR uploading File: " + e.toString());
            e.printStackTrace();
        }
        createContent();
    }

    protected void tearDown() {
        deleteContent(SLING_URL + APPS_URL);
        deleteContent(SLING_URL + "/apps/gad/test3");
        deleteContent(SLING_URL + CATALOG_URL + slug);
    }

    private void uploadContent() throws FileNotFoundException {

        PutMethod put_create_ok = new PutMethod(SLING_URL + APPS_URL);

        put_create_ok.setDoAuthentication(true);

        put_create_ok.setRequestBody(new FileInputStream("./src/test/resources/GET.jsp"));
        put_create_ok.setDoAuthentication(true);
        try {
            client.executeMethod(put_create_ok);
        } catch (HttpException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        // Content created
        log.info("Created ok: " + put_create_ok.getStatusCode());
        // assertEquals(201, put_create_ok.getStatusCode());
        put_create_ok.releaseConnection();
    }

    private void createContent() {

        PostMethod post_create_ok = new PostMethod(SLING_URL + CATALOG_URL + slug);
        post_create_ok.setDoAuthentication(true);
        NameValuePair[] data_create_ok = { new NameValuePair("sling:resourceType", "gad/test3"),
                new NameValuePair("title", this.title), new NameValuePair("schema", this.schema),
                new NameValuePair("description",
                        "Content description generated by test case. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur."),
                new NameValuePair("jcr:createdBy", ""), new NameValuePair("jcr:lastModifiedBy", "") };

        post_create_ok.setRequestBody(data_create_ok);
        post_create_ok.setDoAuthentication(true);
        try {
            client.executeMethod(post_create_ok);
        } catch (HttpException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        // Content created
        log.info("Created ok: " + post_create_ok.getStatusCode());
        assertEquals(201, post_create_ok.getStatusCode());
        post_create_ok.releaseConnection();
    }

    private void createTest3() {

        PostMethod post_create_ok = new PostMethod(SLING_URL + "/apps/gad/*");
        post_create_ok.setDoAuthentication(true);
        NameValuePair[] data_create_ok = { new NameValuePair("title", "test3"), };

        post_create_ok.setRequestBody(data_create_ok);
        post_create_ok.setDoAuthentication(true);
        try {
            client.executeMethod(post_create_ok);
        } catch (HttpException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        // Content created
        log.info("Created ok: " + post_create_ok.getStatusCode());
        assertEquals(201, post_create_ok.getStatusCode());
        post_create_ok.releaseConnection();
    }

    private void deleteContent(String content) {

        // Delete the content
        DeleteMethod post_delete = new DeleteMethod(content);

        post_delete.setDoAuthentication(true);
        // post_delete.setRequestBody(data_delete);

        try {
            client.executeMethod(post_delete);
        } catch (HttpException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        assertEquals(204, post_delete.getStatusCode());
        log.info("Borrado contenido: " + content);
        post_delete.releaseConnection();

    }

    public void test_prueba1() {
        GetMethod get_content = new GetMethod(SLING_URL + CATALOG_URL + slug);
        get_content.setDoAuthentication(true);
        try {
            client.executeMethod(get_content);
        } catch (HttpException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            log.info(get_content.getResponseBodyAsString());
        } catch (IOException e) {
            assert false;
            e.printStackTrace();
        }
        get_content.releaseConnection();
    }

}