test.integ.be.fedict.hsm.rest.security.SecurityTest.java Source code

Java tutorial

Introduction

Here is the source code for test.integ.be.fedict.hsm.rest.security.SecurityTest.java

Source

/*
 * HSM Proxy Project.
 * Copyright (C) 2013 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package test.integ.be.fedict.hsm.rest.security;

import static org.junit.Assert.assertEquals;

import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import test.integ.be.fedict.hsm.ws.WebServiceSecurityTest;
import be.fedict.hsm.model.security.JBossRolesGroup;
import be.fedict.hsm.model.security.SimplePrincipal;

/**
 * Add the following security domain configuration to JBoss EAP 6.1
 * standalone.xml
 * 
 * <pre>
 * &lt;security-domain name="test-rest-security-domain" cache-type="default"&gt;
 *     &lt;authentication&gt;
 *         &lt;login-module code="test.integ.be.fedict.hsm.rest.security.SecurityTestLoginModule" flag="required"/&gt;
 *     &lt;/authentication&gt;
 * &lt;/security-domain&gt;
 * </pre>
 * 
 * @author fcorneli
 * 
 */
@RunWith(Arquillian.class)
@RunAsClient
public class SecurityTest {

    private static final Log LOG = LogFactory.getLog(SecurityTest.class);

    @ArquillianResource
    private URL baseURL;

    @Deployment
    public static WebArchive createTestArchive() {
        WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
                .addClasses(SecurityTestResource.class, SecurityTestLoginModule.class, SimplePrincipal.class,
                        JBossRolesGroup.class, SecurityTestBean.class)
                .addAsWebInfResource(WebServiceSecurityTest.class.getResource("/test-rest-security-web.xml"),
                        "/web.xml")
                .addAsWebInfResource(WebServiceSecurityTest.class.getResource("/test-rest-security-jboss-web.xml"),
                        "/jboss-web.xml");
        return war;
    }

    @Test
    public void testInvocation() throws Exception {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("username", "password"));
        HttpGet httpGet = new HttpGet(this.baseURL + "rest/security/test");
        HttpResponse httpResponse = httpClient.execute(httpGet);
        String result = EntityUtils.toString(httpResponse.getEntity());
        LOG.debug("result: " + result);
        StatusLine statusLine = httpResponse.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        assertEquals(200, statusCode);
    }
}