package specs.response;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.HttpResponse;
import org.apache.http.entity.ContentType;
import org.concordion.api.MultiValueResult;
import org.concordion.integration.junit4.ConcordionRunner;
import org.junit.runner.RunWith;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static com.commercetools.util.HttpRequestUtil.executeGetRequest;
import static com.commercetools.util.HttpRequestUtil.responseToString;
import static java.util.Optional.ofNullable;

/**
 * Simple /health URL response checker
 */
@RunWith(ConcordionRunner.class)
public class HealthResponseFixture extends BasePaymentFixture {

    public String getUrl() throws Exception {
        return getHealthUrl();
    }

    public MultiValueResult handleHealthResponse() throws Exception {
        HttpResponse httpResponse = executeGetRequest(getHealthUrl());
        String responseString = responseToString(httpResponse);

        JsonParser parser = new JsonParser();

        JsonObject rootNode = parser.parse(responseString).getAsJsonObject();
        String bodyStatus = rootNode.get("status").getAsString();
        Optional<Set<Map.Entry<String, JsonElement>>> tenantNames = ofNullable(rootNode.get("tenants")).map(e -> e.getAsJsonObject().entrySet());
        Optional<JsonObject> applicationInfo = ofNullable(rootNode.getAsJsonObject("applicationInfo")).filter(JsonObject::isJsonObject);
        String title = applicationInfo.map(node -> node.get("title")).map(JsonElement::getAsString).orElse("");
        String version = applicationInfo.map(node -> node.get("version")).map(JsonElement::getAsString).orElse("");

        return MultiValueResult.multiValueResult()
                .with("statusCode", httpResponse.getStatusLine().getStatusCode())
                .with("mimeType", ContentType.getOrDefault(httpResponse.getEntity()).getMimeType())
                .with("bodyStatus", bodyStatus)
                .with("bodyTenants", tenantNames.map(names-> names.toString()).orElse("")) // info only
                .with("bodyTenantsSize", tenantNames.map(tenants->tenants.size()).orElse(0))
                .with("bodyApplicationName", title)
                .with("bodyApplicationVersion", version)
                .with("versionIsEmpty", version.isEmpty());
    }
}

@@ -22,42 +22,8 @@
 e;%0A%0A
-import com.google.gson.JsonArray;%0A
 impo
@@ -1266,25 +1266,16 @@
         
-Optional%3C
 Set%3CMap.
@@ -1301,17 +1301,16 @@
 lement%3E%3E
-%3E
  tenantN
@@ -1351,16 +1351,33 @@
 nants%22))
+%0A                
 .map(e -
@@ -1409,16 +1409,64 @@
 rySet())
+%0A                .orElse(Collections.emptySet())
 ;%0A      
@@ -1505,16 +1505,32 @@
 onInfo =
+%0A               
  ofNulla
@@ -2175,47 +2175,17 @@
 mes.
-map(names-%3E names.toString()).orElse(%22%22
+toString(
 )) /
@@ -2253,45 +2253,13 @@
 mes.
-map(tenants-%3Etenants.size()).orElse(0
+size(
 ))%0A 

Use set instead of optional.