/*
 * Copyright 2015 Collective, Inc.
 *
 * 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 com.collective.celos;

import junit.framework.Assert;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.io.IOException;
import java.net.ServerSocket;

/**
 * Created by akonopko on 22.12.14.
 */
public class JettyServerTest {

    @Test
    public void testServerStartsSpecifyPort() throws Exception {

        JettyServer jettyServer = new JettyServer();
        int port = jettyServer.start();

        HttpClient httpClient = new DefaultHttpClient();
        HttpGet workflowListGet = new HttpGet("http://localhost:" + port  + "/workflow-list");
        HttpResponse response = httpClient.execute(workflowListGet);
        EntityUtils.consume(response.getEntity());

        Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);
    }


    @Test(expected = HttpHostConnectException.class)
    public void testServerStopsSpecifyPort() throws Exception {

        JettyServer jettyServer = new JettyServer();
        int port = jettyServer.start();

        HttpClient httpClient = new DefaultHttpClient();
        HttpGet workflowListGet = new HttpGet("http://localhost:" + port + "/workflow-list");
        HttpResponse response = httpClient.execute(workflowListGet);

        Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);
        EntityUtils.consume(response.getEntity());
        jettyServer.stop();

        httpClient.execute(workflowListGet);
    }

}

@@ -1,603 +1,4 @@
-/*%0A * Copyright 2015 Collective, Inc.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *   http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or%0A * implied.  See the License for the specific language governing%0A * permissions and limitations under the License.%0A */%0A
 pack
@@ -531,27 +531,553 @@
 erStarts
-SpecifyPort
+() throws Exception %7B%0A        JettyServer jettyServer = new JettyServer();%0A        int port = jettyServer.start();%0A%0A        HttpClient httpClient = new DefaultHttpClient();%0A        HttpGet workflowListGet = new HttpGet(%22http://localhost:%22 + port);%0A        HttpResponse response = httpClient.execute(workflowListGet);%0A        EntityUtils.consume(response.getEntity());%0A%0A        Assert.assertEquals(response.getStatusLine().getStatusCode(), 403);%0A    %7D%0A%0A%0A    @Test(expected = HttpHostConnectException.class)%0A    public void testServerStops
 () throw
@@ -1082,33 +1082,32 @@
 ows Exception %7B%0A
-%0A
         JettySer
@@ -1317,28 +1317,615 @@
 port
-  + %22/workflow-list%22
+);%0A        HttpResponse response = httpClient.execute(workflowListGet);%0A%0A        Assert.assertEquals(response.getStatusLine().getStatusCode(), 403);%0A        EntityUtils.consume(response.getEntity());%0A        jettyServer.stop();%0A%0A        httpClient.execute(workflowListGet);%0A    %7D%0A%0A    @Test%0A    public void testServerStartsSpecifyPort() throws Exception %7B%0A        int port = getFreePort();%0A%0A        JettyServer jettyServer = new JettyServer();%0A        jettyServer.start(port);%0A%0A        HttpClient httpClient = new DefaultHttpClient();%0A        HttpGet workflowListGet = new HttpGet(%22http://localhost:%22 + port
 );%0A 
@@ -2110,27 +2110,27 @@
 atusCode(), 
-200
+403
 );%0A    %7D%0A%0A%0A 
@@ -2244,16 +2244,50 @@
 eption %7B
+%0A        int port = getFreePort();
 %0A%0A      
@@ -2336,35 +2336,24 @@
 r();%0A       
- int port =
  jettyServer
@@ -2351,32 +2351,36 @@
 ttyServer.start(
+port
 );%0A%0A        Http
@@ -2500,27 +2500,8 @@
 port
- + %22/workflow-list%22
 );%0A 
@@ -2643,11 +2643,11 @@
 (), 
-200
+403
 );%0A 
@@ -2777,10 +2777,191 @@
 %0A    %7D%0A%0A
+    private int getFreePort() throws IOException %7B%0A        ServerSocket s = new ServerSocket(0);%0A        int port = s.getLocalPort();%0A        s.close();%0A        return port;%0A    %7D%0A%0A
 %7D%0A

fix jetty test