/*  
Copyright [2013-2015] eBay Software Foundation
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 io.parallec.core.main.ping;

import io.parallec.core.ParallecResponseHandler;
import io.parallec.core.ParallelClient;
import io.parallec.core.ParallelTask;
import io.parallec.core.ResponseOnSingleTask;
import io.parallec.core.TestBase;
import io.parallec.core.bean.ping.PingMode;
import io.parallec.core.exception.ParallelTaskInvalidException;
import io.parallec.core.util.PcStringUtils;

import java.util.Map;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * The most basic test with hitting the same URL at 3 different websites.
 * require Internet access for testing.
 * 
 * <p>
 * This example shows 1. Basic request construction 2. how to use response
 * context to pass value during the response handler out to a global space
 * </p>
 */
public class PingTest extends TestBase {

    /** The pc. */
    private static ParallelClient pc;

    /**
     * Sets the up.
     *
     * @throws Exception
     *             the exception
     */
    @BeforeClass
    public static void setUp() throws Exception {
        pc = new ParallelClient();
    }

    /**
     * Shutdown.
     *
     * @throws Exception
     *             the exception
     */
    @AfterClass
    public static void shutdown() throws Exception {
        pc.releaseExternalResources();
    }

    /**
     * Ping websites min async. default mode need ROOT/Admin 
     */
    @Test
    public void pingWebsitesMinBasicSync() {

        ParallelTask task = pc.preparePing().setConcurrency(100)
                 .setTargetHostsFromString(
                 "www.parallec.io www.jeffpei.com www.restcommander.com bad.c21tom")
                .execute(new ParallecResponseHandler() {
                    @Override
                    public void onCompleted(ResponseOnSingleTask res,
                            Map<String, Object> responseContext) {
                        logger.info(res.toString());
                    }
                });

        logger.info("Task Pretty Print: \n{}",
                PcStringUtils.renderJson(task.getAggregateResultFullSummary()));
        logger.info("Total Duration: " + task.getDurationSec());
    }// end func
    
    @Test
    public void pingWebsitesMoreOptions() {

        ParallelTask task = pc.preparePing().setConcurrency(100)
                 .setTargetHostsFromString(
                 "www.parallec.io www.jeffpei.com www.restcommander.com 192.168.99.11")
                 .setPingMode(PingMode.PROCESS)
                 .setPingNumRetries(3)
                 .setPingTimeoutMillis(2000)
                 
                .execute(new ParallecResponseHandler() {
                    @Override
                    public void onCompleted(ResponseOnSingleTask res,
                            Map<String, Object> responseContext) {
                        logger.info(res.toString());
                    }
                });

        logger.info("Task Pretty Print: \n{}",
                PcStringUtils.renderJson(task.getAggregateResultFullSummary()));
        logger.info("Total Duration: " + task.getDurationSec());
    }// end func
    
    
    @Test
    public void testInvalidPoller() {
        try {
            pc.preparePing().setConcurrency(100)
            .setHttpPollable(true)
            .setTargetHostsFromString(
            "www.parallec.io www.jeffpei.com www.restcommander.com bad.c21tom")
           .execute(new ParallecResponseHandler() {
               @Override
               public void onCompleted(ResponseOnSingleTask res,
                       Map<String, Object> responseContext) {
                   
               }
           });
        } catch (ParallelTaskInvalidException e) {

            logger.info("EXPECTED Exception {}", e.getLocalizedMessage());
        }
    }
 
}

@@ -1046,24 +1046,49 @@
 eforeClass;%0A
+import org.junit.Ignore;%0A
 import org.j
@@ -2553,32 +2553,1107 @@
            %7D);%0A%0A
+        logger.info(%22Task Pretty Print: %5Cn%7B%7D%22,%0A                PcStringUtils.renderJson(task.getAggregateResultFullSummary()));%0A        logger.info(%22Total Duration: %22 + task.getDurationSec());%0A    %7D// end func%0A    %0A    %0A    @Ignore%0A    public void pingWebsitesMoreOptionsForever() %7B%0A        for(int i=1; i%3C100; i++)%7B%0A            pingWebsitesMoreOptionsHelper();%0A        %7D%0A    %7D%0A    %0A    public void pingWebsitesMoreOptionsHelper() %7B%0A%0A        ParallelTask task = pc.preparePing().setConcurrency(100)%0A                .setTargetHostsFromLineByLineText(FILEPATH_TOP_100,%0A                        SOURCE_LOCAL)%0A                 .setPingMode(PingMode.PROCESS)%0A                 .setPingNumRetries(1)%0A                 .setPingTimeoutMillis(2000)%0A                 %0A                .execute(new ParallecResponseHandler() %7B%0A                    @Override%0A                    public void onCompleted(ResponseOnSingleTask res,%0A                            Map%3CString, Object%3E responseContext) %7B%0A                        logger.info(res.toString());%0A                    %7D%0A                %7D);%0A%0A
         logger.i
@@ -3853,32 +3853,32 @@
 %0A    %0A    @Test%0A
-
     public void 
@@ -4187,17 +4187,17 @@
 Retries(
-3
+2
 )%0A      

update ping test