net.bryansaunders.jee6divelog.service.rest.RestApiTest.java Source code

Java tutorial

Introduction

Here is the source code for net.bryansaunders.jee6divelog.service.rest.RestApiTest.java

Source

package net.bryansaunders.jee6divelog.service.rest;

/*
 * #%L
 * BSNet-DiveLog
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2012 Bryan Saunders
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import static com.jayway.restassured.RestAssured.given;
import static org.junit.Assert.assertNotNull;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;

import net.bryansaunders.jee6divelog.DeploymentFactory;
import net.bryansaunders.jee6divelog.model.UserAccount;
import net.bryansaunders.jee6divelog.security.Credentials;
import net.bryansaunders.jee6divelog.util.SecurityUtils;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.httpclient.HttpStatus;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Ignore;

import com.jayway.restassured.http.ContentType;

/**
 * Base Test class for REST API Tests.
 * 
 * @author Bryan Saunders <btsaunde@gmail.com>
 * 
 */
@Ignore
public abstract class RestApiTest {

    /**
     * Token Header Name.
     */
    protected static final String DL_TOKEN = "dl-token";

    /**
     * Username Header Name.
     */
    protected static final String DL_USERNAME = "dl-username";

    /**
     * Root URL.
     */
    protected static final String URL_ROOT = "http://localhost:8080/jee6divelog_test_restapi/REST";

    /**
     * Creates Arquillian Deployment Container.
     * 
     * @return deployment container
     */
    @Deployment
    public static WebArchive createDeployment() {
        return DeploymentFactory.getRestApiDeployment();
    }

    /**
     * Performs a Login Action and returns the Authorization Token.
     * 
     * @param userName
     *            User Name
     * @param password
     *            User Password
     * @return Authorization Token
     */
    protected UserAccount doLogin(final String userName, final String password) {
        final Credentials credentials = new Credentials();
        credentials.setUsername(userName);
        credentials.setPassword(password);

        final UserAccount user = given().contentType(ContentType.JSON).body(credentials).expect()
                .statusCode(HttpStatus.SC_ACCEPTED).when().post(RestApiTest.URL_ROOT + "/security/login/")
                .as(UserAccount.class);

        assertNotNull(user);
        return user;
    }

    /**
     * Generates Headers for the Logged In User.
     * 
     * @param requestMethod
     *            HTTP Request Method
     * @param requestUrl
     *            Request URL
     * @param privateApiKey
     *            Private API Key
     * @param publicApiKey
     *            Public API Key
     * @param jsonContent
     *            JSON Content String
     * @return Header Map
     */
    protected Map<String, String> generateLoginHeaders(final String requestMethod, final String requestUrl,
            final String jsonContent, final String privateApiKey, final String publicApiKey) {
        final Map<String, String> headerMap = new HashMap<String, String>();

        final String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        final String contentType = MediaType.APPLICATION_JSON;
        final String contentMd5 = "";
        if (jsonContent != null) {
            DigestUtils.md5(jsonContent);
        }

        final String signature = SecurityUtils.generateRestSignature(requestMethod, contentType, contentMd5, date,
                requestUrl, privateApiKey);

        headerMap.put(HttpHeaders.DATE, date);
        headerMap.put(HttpHeaders.CONTENT_TYPE, contentType);
        headerMap.put(RestApi.CONTENT_MD5_HEADER, contentMd5);
        headerMap.put(RestApi.PUBLIC_KEY_HEADER, publicApiKey);
        headerMap.put(RestApi.SIGNATURE_HEADER, signature);

        return headerMap;
    }

}