package io.syndesis.qe.templates;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import io.syndesis.qe.accounts.Account;
import io.syndesis.qe.accounts.AccountsDirectory;
import io.syndesis.qe.utils.OpenShiftUtils;
import io.syndesis.qe.utils.TestUtils;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HTTPEndpointsTemplate {
    private static final String TEMPLATE_URL = "https://raw.githubusercontent.com/avano/HTTPEndpoints/master/template.yml";

    public static void deploy() {
        if (!TestUtils.isDcDeployed("httpendpoints")) {
            try {
                OpenShiftUtils.client().load(new URL(TEMPLATE_URL).openStream()).createOrReplace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        addAccounts();
    }

    private static void addAccounts() {
        Account http = new Account();
        Map<String, String> params = new HashMap<>();
        params.put("baseUrl", "http://http-svc:8080");
        http.setService("http");
        http.setProperties(params);
        AccountsDirectory.getInstance().getAccounts().put("http", http);
        Account https = new Account();
        params = new HashMap<>();
        params.put("baseUrl", "https://https-svc:8443");
        https.setService("https");
        https.setProperties(params);
        AccountsDirectory.getInstance().getAccounts().put("https", https);
    }
}

@@ -124,16 +124,62 @@
 til.Map;
+%0Aimport java.util.concurrent.TimeoutException;
 %0A%0Aimport
@@ -341,24 +341,71 @@
 .TestUtils;%0A
+import io.syndesis.qe.wait.OpenShiftWaitUtils;%0A
 import lombo
@@ -838,24 +838,24 @@
 eption e) %7B%0A
-
             
@@ -884,32 +884,296 @@
 );%0A            %7D
+%0A%0A            try %7B%0A                OpenShiftWaitUtils.waitFor(OpenShiftWaitUtils.isAPodReady(%22app%22, %22httpendpoints%22));%0A            %7D catch (InterruptedException %7C TimeoutException e) %7B%0A                log.error(%22Wait for http endpoints failed %22, e);%0A            %7D
 %0A        %7D%0A     

Add wait for HTTP endpoints