/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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 jetbrains.buildServer.clouds.vmware.vmrun.remote;

import java.util.Collection;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import jetbrains.buildServer.BaseTestCase;
import jetbrains.buildServer.clouds.vmware.vmrun.remote.client.Client;
import jetbrains.buildServer.clouds.vmware.vmrun.remote.server.Server;
import jetbrains.buildServer.clouds.vmware.vmrun.remote.xmlrpc.XmRpcClient;
import jetbrains.buildServer.clouds.vmware.vmrun.remote.xmlrpc.XmlRpcBasedVMRunServer;
import jetbrains.buildServer.clouds.vmware.vmrun.remote.xmlrpc.XmlRpcVMRunServer;
import jetbrains.buildServer.messages.XStreamHolder;
import jetbrains.buildServer.util.WaitForAssert;
import org.apache.xmlrpc.WebServer2;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
 * @author Eugene Petrenko
 *         Created: 08.12.2009 13:15:07
 */
@Test
public class ClientServerTest extends BaseTestCase {
  public interface IFoo {
    int run(String s);
    int runZ(int s);
  }

  @Test
  public void testTransport() {
    final ScheduledExecutorService pool = Executors.newScheduledThreadPool(10);
    IFoo impl = new IFoo() {
      public int run(final String s) {
        return "zzz".equals(s) ? 0 : 1;
      }

      public int runZ(final int s) {
        return 0;
      }
    };

    final Server<IFoo> serv = new Server<IFoo>(IFoo.class, pool);
    final Client<IFoo> cli = new Client<IFoo>(impl, serv, pool, 10);

    new WaitForAssert(){
      @Override
      protected boolean condition() {
        return !serv.getAliveClients().isEmpty();
      }
    };

    final Collection<IFoo> iFoos = serv.getAliveClients();
    Assert.assertEquals(iFoos.size(), 1);

    int z = iFoos.iterator().next().run("zzz");
    Assert.assertEquals(0, z);

    z = iFoos.iterator().next().run("zz2z");
    Assert.assertEquals(1, z);

    pool.shutdownNow();
  }

  @Test
  public void testTransportXmlRpc() {
    WebServer2 ws = WebServer2.createStartedServer(2121, null);

    final ScheduledExecutorService pool = Executors.newScheduledThreadPool(10);
    IFoo impl = new IFoo() {
      public int run(final String s) {
        return "zzz".equals(s) ? 0 : 1;
      }

      public int runZ(final int s) {
        return 0;
      }
    };

    final Server<IFoo> serv = new Server<IFoo>(IFoo.class, pool);


    final XStreamHolder xs = new XStreamHolder();
    XmlRpcBasedVMRunServer s = new XmlRpcBasedVMRunServer(serv, xs);
    ws.addHandler(XmlRpcVMRunServer.HANDLER, s);

    final XmRpcClient rpc = new XmRpcClient(xs, "http://localhost:" + ws.getPort() + "/RPC2");
    final Client<IFoo> cli = new Client<IFoo>(impl, rpc, pool, 10);

    new WaitForAssert(){
      @Override
      protected boolean condition() {
        return !serv.getAliveClients().isEmpty();
      }
    };

    final Collection<IFoo> iFoos = serv.getAliveClients();
    Assert.assertEquals(iFoos.size(), 1);

    int z = iFoos.iterator().next().run("zzz");
    Assert.assertEquals(0, z);

    z = iFoos.iterator().next().run("zz2z");
    Assert.assertEquals(1, z);

    pool.shutdownNow();
    ws.shutdown();
  }
}
