package net.openhft.chronicle.core.internal.analytics;

import net.openhft.chronicle.core.analytics.AnalyticsFacade;
import okhttp3.HttpUrl;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.Test;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class AnalyticsFacadeTest {

    private static final String TEST_RESPONSE = "A";

    @Test
    public void analytics() {
        final AnalyticsFacade.Builder builder = AnalyticsFacade.builder("measurementId", "apiSecret")
                .putEventParameter("e", "1")
                .putUserProperty("u","2")
                .withClientIdFileName("file_name")
                .withDebugLogger(System.out::println)
                .withErrorLogger(System.err::println)
                .withUrl("url")
                // .withReportDespiteJUnit() Not using this will produce a mute logger in test contexts
                .withFrequencyLimit(1, TimeUnit.SECONDS);

        final AnalyticsFacade analyticsFacade = builder.build();

        analyticsFacade.sendEvent("test");
    }

    @Test(timeout = 10_000L)
    public void analyticsWithRealWebServer() throws IOException {

        final List<String> debugResponses = new CopyOnWriteArrayList<>();
        final List<String> errorResponses = new CopyOnWriteArrayList<>();

        final MockWebServer server = new MockWebServer();
        server.enqueue(new MockResponse().setBody(TEST_RESPONSE));

        server.start();
        try {
            final HttpUrl url = server.url("mp/collect");

            final AnalyticsFacade.Builder builder = AnalyticsFacade.builder("measurementId", "apiSecret")
                    .putEventParameter("e", "1")
                    .putUserProperty("u","2")
                    .withClientIdFileName("file_name")
                    .withDebugLogger(debugResponses::add)
                    .withErrorLogger(errorResponses::add)
                    .withUrl(url.url().toString())
                    .withReportDespiteJUnit() // Run the real thing even though we are in test mode.
                    .withFrequencyLimit(1, TimeUnit.SECONDS);

            final AnalyticsFacade analyticsFacade = builder.build();

            analyticsFacade.sendEvent("test");

            while (debugResponses.isEmpty()) {
                // Await reporting thread
            }

            System.out.println("debugResponses = " + debugResponses);

            assertTrue(errorResponses.isEmpty());
            assertEquals(TEST_RESPONSE, debugResponses.get(0));
        } finally {
            server.shutdown();
        }
    }
}
@@ -49,16 +49,55 @@
 ytics;%0A%0A
+import net.openhft.chronicle.core.Jvm;%0A
 import n
@@ -280,24 +280,45 @@
 unit.Test;%0A%0A
+import java.io.File;%0A
 import java.
@@ -1415,24 +1415,334 @@
 xception %7B%0A%0A
+        final String clientIdFileName = %22client_id_file_name.txt%22;%0A%0A        // this makes sure the client id file is pre-created%0A        final AnalyticsFacade dummyFacade = AnalyticsFacade.builder(%22measurementId%22, %22apiSecret%22)%0A                .withClientIdFileName(clientIdFileName)%0A                .build();%0A%0A
         fina
@@ -2340,35 +2340,40 @@
 tIdFileName(
-%22file_n
+clientIdFileN
 ame
-%22
 )%0A          
@@ -2830,16 +2830,17 @@
  while (
+!
 debugRes
@@ -2846,24 +2846,55 @@
 sponses.
-isEmpty(
+stream().anyMatch(TEST_RESPONSE::equals
 )) %7B%0A   
@@ -3090,50 +3090,67 @@
 sert
-Equals(TEST_RESPONSE, debugResponses.get(0
+True(debugResponses.stream().anyMatch(TEST_RESPONSE::equals
 ));%0A
@@ -3196,24 +3196,73 @@
 shutdown();%0A
+            new File(clientIdFileName).delete();%0A
         %7D%0A  

Fix flaky test