/*
 * The MIT License (MIT)
 * <p/>
 * Copyright (c) 2016-2017 Bertrand Martel
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package fr.bmartel.speedtest.test;

import fr.bmartel.speedtest.SpeedTestReport;
import fr.bmartel.speedtest.inter.ISpeedTestListener;
import fr.bmartel.speedtest.model.SpeedTestError;
import fr.bmartel.speedtest.test.utils.TestCommon;
import net.jodah.concurrentunit.Waiter;
import org.junit.Test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Test Redirect & Moved URI.
 *
 * @author Bertrand Martel
 */
public class SpeedTestRedirect extends AbstractTest {

    /**
     * Waiter for speed test listener callback.
     */
    private static Waiter mWaiter;

    @Test
    public void redirectTest() throws TimeoutException {

        mWaiter = new Waiter();

        mSocket.setSocketTimeout(TestCommon.DEFAULT_SOCKET_TIMEOUT);

        mSocket.addSpeedTestListener(new ISpeedTestListener() {
            @Override
            public void onCompletion(final SpeedTestReport report) {
                mWaiter.resume();
            }

            @Override
            public void onProgress(final float percent, final SpeedTestReport report) {

            }

            @Override
            public void onError(final SpeedTestError speedTestError, final String errorMessage) {

            }
        });
        mSocket.startDownload("http://bmartel.fr/test2");
        mWaiter.await(TestCommon.WAITING_TIMEOUT_DEFAULT_SEC, TimeUnit.SECONDS);
        mSocket.forceStopTask();
        mSocket.clearListeners();
    }

    @Test
    public void redirectErrorTest() throws TimeoutException {

        mWaiter = new Waiter();

        mSocket.setSocketTimeout(TestCommon.DEFAULT_SOCKET_TIMEOUT);

        mSocket.addSpeedTestListener(new ISpeedTestListener() {
            @Override
            public void onCompletion(final SpeedTestReport report) {
                mWaiter.fail("error expected");
            }

            @Override
            public void onProgress(final float percent, final SpeedTestReport report) {

            }

            @Override
            public void onError(final SpeedTestError speedTestError, final String errorMessage) {
                if (speedTestError == SpeedTestError.UNSUPPORTED_PROTOCOL) {
                    mWaiter.resume();
                } else {
                    mWaiter.fail("expected UNSUPPORTED_PROTOCOL, got : " + speedTestError);
                }
            }
        });
        mSocket.startDownload("http://bmartel.fr");
        mWaiter.await(TestCommon.WAITING_TIMEOUT_DEFAULT_SEC, TimeUnit.SECONDS);
        mSocket.forceStopTask();
        mSocket.clearListeners();
    }
}

@@ -2637,1130 +2637,7 @@
    %7D
-%0A%0A    @Test%0A    public void redirectErrorTest() throws TimeoutException %7B%0A%0A        mWaiter = new Waiter();%0A%0A        mSocket.setSocketTimeout(TestCommon.DEFAULT_SOCKET_TIMEOUT);%0A%0A        mSocket.addSpeedTestListener(new ISpeedTestListener() %7B%0A            @Override%0A            public void onCompletion(final SpeedTestReport report) %7B%0A                mWaiter.fail(%22error expected%22);%0A            %7D%0A%0A            @Override%0A            public void onProgress(final float percent, final SpeedTestReport report) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void onError(final SpeedTestError speedTestError, final String errorMessage) %7B%0A                if (speedTestError == SpeedTestError.UNSUPPORTED_PROTOCOL) %7B%0A                    mWaiter.resume();%0A                %7D else %7B%0A                    mWaiter.fail(%22expected UNSUPPORTED_PROTOCOL, got : %22 + speedTestError);%0A                %7D%0A            %7D%0A        %7D);%0A        mSocket.startDownload(%22http://bmartel.fr%22);%0A        mWaiter.await(TestCommon.WAITING_TIMEOUT_DEFAULT_SEC, TimeUnit.SECONDS);%0A        mSocket.forceStopTask();%0A        mSocket.clearListeners();%0A    %7D
 %0A%7D%0A

remove useless redirect error test