package org.projectodd.restafari.mongo;

import io.netty.channel.nio.NioEventLoopGroup;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Test;
import org.projectodd.restafari.container.Container;
import org.projectodd.restafari.container.SimpleConfig;
import org.projectodd.restafari.container.UnsecureServer;

import java.net.InetAddress;
import java.util.Collections;

import static org.junit.Assert.*;

/**
 * @author <a href="mailto:nscavell@redhat.com">Nick Scavelli</a>
 */
public class MongoControllerTest {
    @Test
    public void testMongoController() throws Exception {
        Container container = new Container();
        container.registerResourceController("storage", new MongoController(), new SimpleConfig(Collections.<String, Object> singletonMap("db", "test")));

        UnsecureServer server = new UnsecureServer(container, InetAddress.getByName("localhost"), 8080, new NioEventLoopGroup());

        System.err.println("START SERVER");
        server.start();
        System.err.println("STARTED SERVER");

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet get = new HttpGet("http://localhost:8080/storage/movies");
        get.addHeader("Accept", "application/json");
        System.err.println("DO GET");
        try {
            CloseableHttpResponse result = httpClient.execute(get);
            System.err.println("=============>>>");
            System.err.println(result);
            result.getEntity().writeTo(System.err);
            System.err.println("\n<<<=============");
            assertEquals(200, result.getStatusLine().getStatusCode());
        } finally {

            System.err.println("closing");
            httpClient.close();
            System.err.println("closed");

            server.stop();
        }
    }
}

@@ -295,24 +295,54 @@
 entBuilder;%0A
+import org.junit.BeforeClass;%0A
 import org.j
@@ -724,16 +724,416 @@
 rTest %7B%0A
+%0A    private static SimpleConfig config;%0A%0A    @BeforeClass%0A    public static void init() %7B%0A        config = new SimpleConfig();%0A        config.put(%22db%22, System.getProperty(%22mongo.db%22));%0A        config.put(%22host%22, System.getProperty(%22mongo.host%22));%0A        String port = System.getProperty(%22mongo.port%22);%0A        if (port != null) %7B%0A            config.put(%22port%22, new Integer(port));%0A        %7D%0A    %7D%0A%0A
     @Tes
@@ -1191,16 +1191,66 @@
 ption %7B%0A
+        if (config.get(%22db%22, null) != null) %7B%0A    
         
@@ -1284,24 +1284,28 @@
 ontainer();%0A
+    
         cont
@@ -1375,85 +1375,22 @@
 (), 
-new SimpleConfig(Collections.%3CString, Object%3E singletonMap(%22db%22, %22test%22)));%0A%0A
+config);%0A%0A    
     
@@ -1516,32 +1516,36 @@
 up());%0A%0A        
+    
 System.err.print
@@ -1564,32 +1564,36 @@
 RVER%22);%0A        
+   
+ 
 server.start();%0A
@@ -1588,24 +1588,28 @@
 er.start();%0A
+    
         Syst
@@ -1643,32 +1643,36 @@
 VER%22);%0A%0A        
+ 
+   
 CloseableHttpCli
@@ -1732,16 +1732,20 @@
         
+    
 HttpGet 
@@ -1811,16 +1811,20 @@
         
+   
+ 
 get.addH
@@ -1856,24 +1856,28 @@
 ion/json%22);%0A
+    
         Syst
@@ -1910,16 +1910,20 @@
         
+    
 try %7B%0A  
@@ -1924,32 +1924,36 @@
 y %7B%0A            
+ 
+   
 CloseableHttpRes
@@ -1996,32 +1996,36 @@
 t);%0A            
+    
 System.err.print
@@ -2052,32 +2052,36 @@
 %22);%0A            
+   
+ 
 System.err.print
@@ -2088,24 +2088,28 @@
 ln(result);%0A
+    
             
@@ -2148,16 +2148,20 @@
 m.err);%0A
+    
         
@@ -2218,16 +2218,20 @@
         
+    
 assertEq
@@ -2289,16 +2289,20 @@
         
+    
 %7D finall
@@ -2310,32 +2310,36 @@
  %7B%0A%0A            
+  
+  
 System.err.print
@@ -2365,16 +2365,20 @@
         
+    
 httpClie
@@ -2393,32 +2393,36 @@
 ();%0A            
+   
+ 
 System.err.print
@@ -2440,32 +2440,36 @@
 );%0A%0A            
+    
 server.stop();%0A 
@@ -2463,24 +2463,38 @@
 ver.stop();%0A
+            %7D%0A
         %7D%0A  

Add system properties for mongo tests