package com.sumologic.jenkins.jenkinssumologicplugin.sender;

import com.sumologic.jenkins.jenkinssumologicplugin.PluginDescriptorImpl;
import com.sumologic.jenkins.jenkinssumologicplugin.model.ModelFactory;
import hudson.model.TaskListener;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.localserver.LocalServerTestBase;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.junit.*;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;

import static org.mockito.Mockito.atLeast;

public class SumoBuildNotifierTest extends LocalServerTestBase {
  @Rule
  public JenkinsRule j = new JenkinsRule();
  @Mock
  HttpRequestHandler handler;
  private String serverUrl;


 @Before
  public void setUp() throws Exception {
    super.setUp();
    handler = Mockito.mock(HttpRequestHandler.class);
    this.serverBootstrap.registerHandler("/jenkinstest/*", handler);
    start();

    serverUrl = "http://" + server.getInetAddress().getCanonicalHostName() + ":"
        + server.getLocalPort() + "/jenkinstest/123";

    // report how to access the server
    System.out.println("LocalTestServer available at " + serverUrl);
    j.get(PluginDescriptorImpl.class).setUrl(serverUrl);
    j.get(PluginDescriptorImpl.class).setPeriodicLogEnabled(true);
  }

  @After
  public void tearDown() throws Exception {
    super.shutDown();
  }



  @Test
  public void testSendJenkinsData() throws Exception {
    ArgumentCaptor<HttpRequest> captor = ArgumentCaptor.forClass(HttpRequest.class);
    SumoPeriodicPublisher publisher = new SumoPeriodicPublisher();
    publisher.execute(Mockito.mock(TaskListener.class));

    Mockito.verify(handler, atLeast(1)).handle(
        captor.capture(),
        Mockito.isA(HttpResponse.class),
        Mockito.isA(HttpContext.class));

    HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) captor.getValue();

    Assert.assertTrue("Wrong message length.", ModelFactory.createJenkinsModel(j.getInstance()).toJson().length() >= request.getEntity().getContentLength());
  }
}

@@ -399,30 +399,26 @@
 er.Local
+Test
 Server
-TestBase
 ;%0Aimport
@@ -742,38 +742,12 @@
 est 
-extends LocalServerTestBase 
 %7B%0A
+  
   @R
@@ -748,24 +748,26 @@
     @Rule%0A  
+ 
+ 
 public Jenki
@@ -800,16 +800,18 @@
 ();%0A
+  
   @Mock%0A
   Ht
@@ -806,16 +806,18 @@
   @Mock%0A
+  
   HttpRe
@@ -838,16 +838,61 @@
 andler;%0A
+    private LocalTestServer server = null;%0A  
   privat
@@ -913,16 +913,19 @@
 rUrl;%0A%0A%0A
+   
  @Before
@@ -925,16 +925,18 @@
 @Before%0A
+  
   public
@@ -976,23 +976,8 @@
     
-super.setUp();%0A
     
@@ -1034,28 +1034,68 @@
     
+  
-this.
+  
 server
-Bootstrap
+ = new LocalTestServer(null, null);%0A        server
 .reg
@@ -1099,23 +1099,16 @@
 register
-Handler
 (%22/jenki
@@ -1132,16 +1132,27 @@
 r);%0A    
+    server.
 start();
@@ -1149,24 +1149,28 @@
 r.start();%0A%0A
+    
     serverUr
@@ -1199,34 +1199,15 @@
 .get
-InetAddress().getCanonical
+Service
 Host
@@ -1227,16 +1227,24 @@
         
+       
+ 
 + server
@@ -1247,21 +1247,23 @@
 rver.get
-Local
+Service
 Port() +
@@ -1284,16 +1284,20 @@
 /123%22;%0A%0A
+    
     // r
@@ -1327,16 +1327,20 @@
  server%0A
+    
     Syst
@@ -1396,32 +1396,36 @@
 serverUrl);%0A    
+ 
+   
 j.get(PluginDesc
@@ -1457,24 +1457,28 @@
 serverUrl);%0A
+    
     j.get(Pl
@@ -1532,21 +1532,26 @@
 (true);%0A
+%0A  
   %7D%0A%0A
+  
   @After
@@ -1551,16 +1551,18 @@
  @After%0A
+  
   public
@@ -1601,30 +1601,35 @@
 n %7B%0A
+%0A    
     s
-up
+erv
 er.s
-hutDown
+top
 ();%0A
+%0A
+  
   %7D%0A
@@ -1637,14 +1637,18 @@
 %0A%0A  
+ 
+ 
 @Test%0A
+  
   pu
@@ -1694,24 +1694,28 @@
 Exception %7B%0A
+    
     Argument
@@ -1783,24 +1783,28 @@
 est.class);%0A
+    
     SumoPeri
@@ -1854,24 +1854,28 @@
 ublisher();%0A
+    
     publishe
@@ -1920,24 +1920,28 @@
 ass));%0A%0A    
+  
+  
 Mockito.veri
@@ -1972,16 +1972,24 @@
 handle(%0A
+        
         
@@ -2006,16 +2006,24 @@
 ture(),%0A
+        
         
@@ -2055,32 +2055,40 @@
 class),%0A        
+  
+      
 Mockito.isA(Http
@@ -2101,24 +2101,28 @@
 t.class));%0A%0A
+    
     HttpEnti
@@ -2195,24 +2195,28 @@
 etValue();%0A%0A
+    
     Assert.a
@@ -2365,10 +2365,14 @@
 ));%0A
+%0A%0A%0A  
   %7D%0A%7D
-%0A

test correction