package no.cantara.jau.serviceconfig;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import no.cantara.jau.serviceconfig.dto.ServiceConfig;
import no.cantara.jau.serviceconfig.dto.ServiceConfigSerializer;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static com.jayway.restassured.RestAssured.given;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

/**
 * @author <a href="mailto:erik-dev@fjas.no">Erik Drolshammer</a> 2015-02-01
 */
public class ServiceConfigResourceTest {
    private Main main;

    @BeforeClass
    public void startServer() {
        main = new Main(6644);
        main.start();
        RestAssured.port = main.getPort();
        RestAssured.basePath = Main.CONTEXT_PATH;
    }

    @AfterClass
    public void stop() {
        if (main != null) {
            main.stop();
        }
    }

    //expect there to be a clientConfig with clientId=client1
    @Test
    public void testFindServiceConfigUnAuthorized() throws Exception {
        //GET
        String path = "/serviceconfig/query";
        Response response = given()
                .queryParam("clientid", "clientid1")
                .log().everything()
                .expect()
                .statusCode(401)
                .log().ifError()
                .when()
                .get(path);
    }

    @Test
    public void testFindServiceConfigOK() throws Exception {
        //GET
        String path = "/serviceconfig/query";
        Response response = given()
                .auth().basic("read", "baretillesing")
                .queryParam("clientid", "clientid1")
                .log().everything()
                .expect()
                .statusCode(200)
                .log().ifError()
                .when()
                .get(path);

        String jsonResponse = response.body().asString();
        ServiceConfig serviceConfig = ServiceConfigSerializer.fromJson(jsonResponse);
        assertEquals(serviceConfig.getName(), "Service1-1.23");
        assertNotNull(serviceConfig.getChangedTimestamp());
    }
}

@@ -669,58 +669,214 @@
 in;%0A
-%0A    @BeforeClass%0A    public void startServer() %7B%0A
+    private final String username = %22read%22;%0A    private final String password= %22baretillesing%22;%0A%0A%0A    @BeforeClass%0A    public void startServer() throws InterruptedException %7B%0A        new Thread(() -%3E %7B%0A    
     
@@ -906,24 +906,28 @@
 4);%0A        
+    
 main.start()
@@ -924,24 +924,72 @@
 in.start();%0A
+        %7D).start();%0A        Thread.sleep(1000);%0A
         Rest
@@ -1867,31 +1867,26 @@
 sic(
-%22read%22, %22baretillesing%22
+username, password
 )%0A  

fix ServiceConfigResourceTest