package org.lognet.springboot.grpc;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.examples.GreeterGrpc;
import io.grpc.examples.GreeterOuterClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.lognet.springboot.grpc.demo.DemoApp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.concurrent.ExecutionException;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApp.class, properties = {"spring.cloud.config.enabled:false"}
,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)

public class ConsulRegistrationTest {
//    @ClassRule
//    public static final ConsulResource consul(){
//        int port = SocketUtils.findAvailableTcpPort();
//        ConsulResource consulResource = new ConsulResource(port);
//        System.setProperty("spring.cloud.consul.port",String.valueOf(port));
//        return consulResource;
//    }
//    @AfterClass
//    public  static void clear(){
//        System.clearProperty("spring.cloud.consul.port");
//    }




    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    ConfigurableApplicationContext applicationContext;

    @Test
    public void contextLoads() throws ExecutionException, InterruptedException {
        List<ServiceInstance> instances = discoveryClient.getInstances("grpc-grpc-demo");
        assertFalse(instances.isEmpty());

        ServiceInstance serviceInstance = instances.get(0);
        ManagedChannel channel = ManagedChannelBuilder.forAddress(serviceInstance.getHost(), serviceInstance.getPort())
                .usePlaintext()
                .build();

        final GreeterGrpc.GreeterFutureStub greeterFutureStub = GreeterGrpc.newFutureStub(channel);
        final GreeterOuterClass.HelloRequest helloRequest =GreeterOuterClass.HelloRequest.newBuilder().setName("Bob").build();
        final String reply = greeterFutureStub.sayHello(helloRequest).get().getMessage();
        assertNotNull("Replay should not be null",reply);
        applicationContext.stop();
    }
}

@@ -30,16 +30,66 @@
 .grpc;%0A%0A
+import com.pszymczyk.consul.junit.ConsulResource;%0A
 import i
@@ -229,16 +229,73 @@
 rClass;%0A
+import org.junit.AfterClass;%0Aimport org.junit.ClassRule;%0A
 import o
@@ -765,16 +765,61 @@
 gRunner;
+%0Aimport org.springframework.util.SocketUtils;
 %0A%0Aimport
@@ -1100,69 +1100,8 @@
 se%22%7D
-%0A,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT%0A
 )%0A%0Ap
@@ -1137,18 +1137,16 @@
 nTest %7B%0A
-//
     @Cla
@@ -1152,18 +1152,16 @@
 assRule%0A
-//
     publ
@@ -1197,26 +1197,24 @@
 e consul()%7B%0A
-//
         int 
@@ -1256,18 +1256,16 @@
 Port();%0A
-//
         
@@ -1322,18 +1322,16 @@
 (port);%0A
-//
         
@@ -1399,18 +1399,16 @@
 port));%0A
-//
         
@@ -1434,18 +1434,14 @@
 ce;%0A
-//
     %7D%0A
-//
     
@@ -1452,18 +1452,16 @@
 erClass%0A
-//
     publ
@@ -1485,18 +1485,16 @@
 lear()%7B%0A
-//
         
@@ -1547,10 +1547,8 @@
 %22);%0A
-//
     
@@ -1613,16 +1613,17 @@
 Client;%0A
+%0A
     @Aut

test with embedded consul