package org.silverware.microservices.providers.http.invoker;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.silverware.microservices.MicroserviceMetaData;
import org.silverware.microservices.annotations.Microservice;
import org.silverware.microservices.providers.cdi.CdiMicroserviceProvider;
import org.silverware.microservices.providers.http.HttpServerMicroserviceProvider;
import org.silverware.microservices.silver.CdiSilverService;
import org.silverware.microservices.silver.HttpInvokerSilverService;
import org.silverware.microservices.silver.HttpServerSilverService;
import org.silverware.microservices.silver.cluster.Invocation;
import org.silverware.microservices.silver.cluster.ServiceHandle;
import org.silverware.microservices.util.BootUtil;
import org.silverware.microservices.util.Utils;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author Martin Večeřa <marvenec@gmail.com>
 */
public class HttpInvokerMicroserviceProviderTest {

   private HttpInvokerSilverService httpInvokerSilverService = null;
   private ObjectMapper mapper = new ObjectMapper();

   @Test
   public void testHttpInvoker() throws Exception {
      final BootUtil bootUtil = new BootUtil();
      final Map<String, Object> platformProperties = bootUtil.getContext().getProperties();
      final Thread platform = bootUtil.getMicroservicePlatform(this.getClass().getPackage().getName(), HttpServerMicroserviceProvider.class.getPackage().getName(), CdiMicroserviceProvider.class.getPackage().getName());
      platform.start();

      while (httpInvokerSilverService == null) {
         httpInvokerSilverService = (HttpInvokerSilverService) bootUtil.getContext().getProvider(HttpInvokerSilverService.class);
         Thread.sleep(200);
      }

      while (bootUtil.getContext().getProperties().get(CdiSilverService.BEAN_MANAGER) == null) {
         Thread.sleep(200);
      }

      String urlBase = "http://" + platformProperties.get(HttpServerSilverService.HTTP_SERVER_ADDRESS) + ":" +
            platformProperties.get(HttpServerSilverService.HTTP_SERVER_PORT) + "/" +
            platformProperties.get(HttpInvokerSilverService.INVOKER_URL) + "/";

      Assert.assertTrue(Utils.waitForHttp(urlBase, 204));

      HttpURLConnection con = (HttpURLConnection) new URL(urlBase + "query").openConnection();
      con.setRequestMethod("POST");
      con.setDoInput(true);
      con.setDoOutput(true);
      con.connect();

      final MicroserviceMetaData metaData = new MicroserviceMetaData("sumService", SumService.class, Collections.emptySet());
      mapper.writeValue(con.getOutputStream(), metaData);

      Assert.assertEquals(con.getResponseMessage(), "OK");
      final List<ServiceHandle> handles = mapper.readValue(con.getInputStream(), mapper.getTypeFactory().constructCollectionType(List.class, ServiceHandle.class));
      Assert.assertEquals(handles.size(), 1);

      con.disconnect();

      con = (HttpURLConnection) new URL(urlBase + "invoke").openConnection();
      con.setRequestMethod("POST");
      con.setDoInput(true);
      con.setDoOutput(true);
      con.connect();

      final Invocation invocation = new Invocation(handles.get(0).getHandle(), "sum", new Class[] { short.class, int.class }, new Object[] { (short) 3, 4 });
      mapper.writeValue(con.getOutputStream(), invocation);
      final Object response = mapper.readValue(con.getInputStream(), Long.class);

      Assert.assertEquals(response, 7L);

      Thread.sleep(10000);

      platform.interrupt();
      platform.join();
   }

   @Microservice
   public static class SumService {

      public long sum(short a, int b) {
         return a + b;
      }
   }

}
@@ -970,24 +970,117 @@
 Connection;%0A
+import java.net.Inet4Address;%0Aimport java.net.InetAddress;%0Aimport java.net.NetworkInterface;%0A
 import java.
@@ -1118,16 +1118,46 @@
 ctions;%0A
+import java.util.Enumeration;%0A
 import j
@@ -3865,16 +3865,549 @@
 ;%0A   %7D%0A%0A
+   @Test%0A   public void testIpAddresses() throws Exception %7B%0A      Enumeration e = NetworkInterface.getNetworkInterfaces();%0A      while(e.hasMoreElements())%0A      %7B%0A         NetworkInterface n = (NetworkInterface) e.nextElement();%0A         Enumeration ee = n.getInetAddresses();%0A         while (ee.hasMoreElements())%0A         %7B%0A            InetAddress i = (InetAddress) ee.nextElement();%0A            System.out.println(i.isLoopbackAddress() + %22 - %22 + (i instanceof Inet4Address) + %22 %22 + i.getHostAddress());%0A         %7D%0A      %7D%0A   %7D%0A%0A
    @Micr

test to obtain public ipv4 address