org.iavante.sling.commons.services.DistributionServerTestIT.java Source code

Java tutorial

Introduction

Here is the source code for org.iavante.sling.commons.services.DistributionServerTestIT.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.commons.services;

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

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.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;

import static junit.framework.Assert.*;

/**
 * DistributionServer bundle
 * 
 */
public class DistributionServerTestIT {

    private String CATALOG_URL = "/content/colecciones/";
    private String slug;
    private String title;
    private String schema;
    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";

    HttpClient client;

    @org.junit.Before
    public 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);

        //tearDown();
        createTest3();
        try {
            uploadContent();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        createContent();
    }

    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
        System.out.println("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
        System.out.println("Created ok: " + post_create_ok.getStatusCode());
        assertEquals(201, post_create_ok.getStatusCode());
        post_create_ok.releaseConnection();
    }

    @org.junit.After
    public void tearDown() {
        deleteContent(SLING_URL + APPS_URL);
        deleteContent(SLING_URL + "/apps/gad/test3");
        deleteContent(SLING_URL + CATALOG_URL + slug);
    }

    @org.junit.Test
    public void test_list_services() {
        GetMethod get_content = new GetMethod(SLING_URL + CATALOG_URL + slug + "?test=listdistributionservers");
        get_content.setDoAuthentication(true);
        try {
            client.executeMethod(get_content);
        } catch (HttpException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            System.out.println(get_content.getResponseBodyAsString());
        } catch (IOException e) {
            assert false;
            e.printStackTrace();
        }
        get_content.releaseConnection();

    }

    @org.junit.Test
    public void test_get_service() {
        GetMethod get_content = new GetMethod(SLING_URL + CATALOG_URL + slug + "?test=getdistributionserver");
        get_content.setDoAuthentication(true);
        try {
            client.executeMethod(get_content);
        } catch (HttpException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            System.out.println(get_content.getResponseBodyAsString());
        } catch (IOException e) {
            assert false;
            e.printStackTrace();
        }
        get_content.releaseConnection();

    }

    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
        System.out.println("Created ok: " + put_create_ok.getStatusCode());
        assertEquals(201, put_create_ok.getStatusCode());
        put_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();
        }
        System.out.println(post_delete.getStatusCode());
        assert 204 == post_delete.getStatusCode();
        System.out.println("Borrado contenido: " + content);
        post_delete.releaseConnection();

    }
}