<commit_before>/**
 * Copyright 2015-2016 Red Hat, Inc, and individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wildfly.swarm.keycloak;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.net.URL;
import java.util.UUID;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

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.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.swarm.Swarm;
import org.wildfly.swarm.arquillian.CreateSwarm;
import org.wildfly.swarm.jaxrs.JAXRSArchive;

/**
 * @author <NAME>
 */
@RunWith(Arquillian.class)
public class KeycloakArquillianTest {

    @Deployment
    public static Archive<?> createDeployment() throws Exception {
        JAXRSArchive deployment = ShrinkWrap.create(JAXRSArchive.class);
        deployment.addResource(SecuredApplication.class);
        deployment.addResource(SecuredResource.class);
        deployment.addAsResource("wildfly-swarm-keycloak-example-realm.json");
        deployment.addAsResource("keycloak.json");
        return deployment;
    }

    @CreateSwarm
    public static Swarm newContainer() throws Exception {
        String warName = getWarName();
        File file = File.createTempFile("project-defaults", ".yaml");
        file.deleteOnExit();
        createSwarmConfiguration(file, warName);

        URL keyCloakJsonUrl = KeycloakArquillianTest.class.getResource("/keycloak.json");
        System.setProperty("swarm.keycloak.json.path", stripLeadingSlashIfWin(keyCloakJsonUrl.toURI().getPath()));
        URL migrationRealmUrl = KeycloakArquillianTest.class.getResource("/wildfly-swarm-keycloak-example-realm.json");
        System.setProperty("keycloak.migration.file", stripLeadingSlashIfWin(migrationRealmUrl.toURI().getPath()));
        System.setProperty("keycloak.migration.provider", "singleFile");
        System.setProperty("keycloak.migration.action", "import");
        return new Swarm("-s" + stripLeadingSlashIfWin(file.toURI().getPath()));
    }

    private static String stripLeadingSlashIfWin(String path) {
        return System.getProperty("os.name").toLowerCase().indexOf("win") >= 0 && path.startsWith("/") 
            ? path.substring(1) : path;
    }

    @Test
    @RunAsClient
    public void testResourceIsSecured() throws Exception {
        // Check 401 is returned without the token
        Assert.assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(),
           ClientBuilder.newClient().target("http://localhost:8080/secured").request().get().getStatus());
        
        final String tokenUri = <commit_msg>[SWARM-1934] Simplifying the test set up a bit<commit_after>/**
 * Copyright 2015-2016 Red Hat, Inc, and individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wildfly.swarm.keycloak;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.net.URL;
import java.util.UUID;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

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.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.swarm.Swarm;
import org.wildfly.swarm.arquillian.CreateSwarm;
import org.wildfly.swarm.jaxrs.JAXRSArchive;

/**
 * @author <NAME>
 */
@RunWith(Arquillian.class)
public class KeycloakArquillianTest {

    @Deployment
    public static Archive<?> createDeployment() throws Exception {
        JAXRSArchive deployment = ShrinkWrap.create(JAXRSArchive.class);
        deployment.addResource(SecuredApplication.class);
        deployment.addResource(SecuredResource.class);
        deployment.addAsResource("wildfly-swarm-keycloak-example-realm.json");
        deployment.addAsResource("keycloak.json");
        return deployment;
    }

    @CreateSwarm
    public static Swarm newContainer() throws Exception {
        String warName = getWarName();
        File file = File.createTempFile("project-defaults", ".yaml");
        file.deleteOnExit();
        createSwarmConfiguration(file, warName);

        URL keyCloakJsonUrl = KeycloakArquillianTest.class.getResource("/keycloak.json");
        System.setProperty("swarm.keycloak.json.path", keyCloakJsonUrl.toURI().toString());
        URL migrationRealmUrl = KeycloakArquillianTest.class.getResource("/wildfly-swarm-keycloak-example-realm.json");
        System.setProperty("keycloak.migration.file", migrationRealmUrl.toURI().getPath());
        System.setProperty("keycloak.migration.provider", "singleFile");
        System.setProperty("keycloak.migration.action", "import");
        return new Swarm("-s" + file.toURI().toString());
    }

    @Test
    @RunAsClient
    public void testResourceIsSecured() throws Exception {
        // Check 401 is returned without the token
        Assert.assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(),
           ClientBuilder.newClient().target("http://localhost:8080/secured").request().get().getStatus());
        
        final String tokenUri = 
{'fraction_non_alphanumeric': 0.11427294398092967, 'fraction_numerical': 0.006555423122765197, 'mean_word_length': 4.028464419475656, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2021208', 'n_tokens_mistral': 2078, 'n_tokens_neox': 1975, 'n_words': 432}