package javalin;

import org.junit.Test;

import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

import static javalin.ApiBuilder.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*;

public class TestApiBuilderTwoServices {

    @Test
    public void testApiBuilder_twoServices() throws Exception {
        Javalin app1 = Javalin.create().port(55555).start().awaitInitialization();
        Javalin app2 = Javalin.create().port(55556).start().awaitInitialization();
        app1.routes(() -> {
            get("/hello1", (req, res) -> res.body("Hello1"));
        });
        app2.routes(() -> {
            get("/hello1", (req, res) -> res.body("Hello1"));
        });
        app1.routes(() -> {
            get("/hello2", (req, res) -> res.body("Hello2"));
        });
        app2.routes(() -> {
            get("/hello2", (req, res) -> res.body("Hello2"));
        });
        assertThat(call(55555, "/hello1"), is("Hello1"));
        assertThat(call(55556, "/hello1"), is("Hello1"));
        assertThat(call(55555, "/hello2"), is("Hello2"));
        assertThat(call(55556, "/hello2"), is("Hello2"));
        app1.stop().awaitTermination();
        app2.stop().awaitTermination();
    }

    private String call(int port, String path) throws UnirestException {
        return Unirest.get("http://localhost:" + port + path).asString().getBody();
    }

}

@@ -423,21 +423,17 @@
 ().port(
-55555
+0
 ).start(
@@ -502,21 +502,17 @@
 ().port(
-55556
+0
 ).start(
@@ -960,37 +960,43 @@
 assertThat(call(
-55555
+app1.port()
 , %22/hello1%22), is
@@ -1024,37 +1024,43 @@
 assertThat(call(
-55556
+app2.port()
 , %22/hello1%22), is
@@ -1100,13 +1100,19 @@
 all(
-55555
+app1.port()
 , %22/
@@ -1164,13 +1164,19 @@
 all(
-55556
+app2.port()
 , %22/

Use random ports for api-builder test