de.elomagic.carafile.server.CloudTest.java Source code

Java tutorial

Introduction

Here is the source code for de.elomagic.carafile.server.CloudTest.java

Source

/*
 * Copyright 2014 Carsten Rambow, elomagic.
 *
 * 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 de.elomagic.carafile.server;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
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.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import de.elomagic.carafile.client.CaraCloud;
import de.elomagic.carafile.client.CaraFileClient;
import static de.elomagic.carafile.server.BaseTest.createDefaultDeployment;
import de.elomagic.carafile.share.CloudFileData;

/**
 *
 */
@RunWith(Arquillian.class)
public class CloudTest extends BaseTest {

    private final CaraCloud cloud = new CaraCloud();
    private URI registryURI;
    private Path basePath;
    private Exception cex = null;

    @Deployment
    public static Archive createDeployment() throws IOException {
        return createDefaultDeployment();
    }

    public void beforeClass() {
        Logger.getRootLogger().addAppender(new ConsoleAppender(new SimpleLayout()));
    }

    @Before
    public void before() throws Exception {
        registryURI = new URI("http://localhost:8080/carafile");

        basePath = Files.createTempDirectory("test_");

        CaraFileClient client = new CaraFileClient().setRegistryURI(registryURI).auth("user",
                "user123".toCharArray());

        cloud.setBasePath(basePath).setClient(client);
    }

    @After
    public void after() throws Exception {
        if (basePath != null) {
            FileUtils.deleteDirectory(basePath.toFile());
        }
    }

    @RunAsClient
    @Test
    public void testCloudUpload() throws Exception {
        Runnable r = () -> {
            try {
                cloud.start();
            } catch (Exception ex) {
                cex = ex;
            }
        };
        new Thread(r).start();

        // Create file
        Thread.sleep(2000);
        Path file = Files.createTempFile(basePath, "TESTFILE_", ".txt");

        // Check in the repository folder
        int loop = 10;
        while (loop-- > 0) {
            Thread.sleep(2000);
            Set<CloudFileData> set = cloud.list(Paths.get(""));
            if (set.size() == 1) {
                System.out.println("One item found in the folder. Bingo !!!");
                break;
            }
        }
        if (loop <= 0) {
            Assert.fail("Remote folder not equals one");
        }

        // Delete file
        Files.delete(file);

        // Check in the repository folder
        loop = 10;
        while (loop-- > 0) {
            Thread.sleep(2000);
            Set<CloudFileData> set = cloud.list(Paths.get(""));
            if (set.isEmpty()) {
                System.out.println("No item found in the folder. Bingo !!!");
                break;
            }
        }
        if (loop <= 0) {
            Assert.fail("Remote folder not empty");
        }

        // Check in the repository folder
        if (cex != null) {
            throw cex;
        }

        cloud.stop();
    }
}