package core.framework.internal.web.service;

import core.framework.http.HTTPMethod;
import core.framework.http.HTTPRequest;
import core.framework.internal.http.HTTPClientImpl;
import core.framework.internal.log.ActionLog;
import core.framework.internal.log.LogManager;
import core.framework.internal.web.HTTPHandler;
import core.framework.internal.web.bean.RequestBeanWriter;
import core.framework.internal.web.bean.ResponseBeanReader;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.Duration;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * @author neo
 */
class WebServiceClientLinkContextTest {
    private WebServiceClient webServiceClient;

    @BeforeEach
    void createWebServiceClient() {
        var httpClient = new HTTPClientImpl(null, null, Duration.ofSeconds(10), Duration.ofSeconds(20));
        webServiceClient = new WebServiceClient("http://localhost", httpClient, new RequestBeanWriter(), new ResponseBeanReader());
    }

    @Test
    void linkContext() {
        var logManager = new LogManager();
        ActionLog actionLog = logManager.begin("begin", null);
        actionLog.maxProcessTimeInNano = Duration.ofSeconds(1).toNanos();

        var request = new HTTPRequest(HTTPMethod.GET, "http://localhost");
        webServiceClient.linkContext(request);
        assertThat(request.headers).containsKeys(HTTPHandler.HEADER_CLIENT.toString(),
                HTTPHandler.HEADER_CORRELATION_ID.toString(),
                HTTPHandler.HEADER_REF_ID.toString(),
                HTTPHandler.HEADER_TIMEOUT.toString());
        assertThat(Long.parseLong(request.headers.get(HTTPHandler.HEADER_TIMEOUT.toString()))).isGreaterThan(0).isLessThanOrEqualTo(Duration.ofSeconds(1).toNanos());

        actionLog.maxProcessTimeInNano = Duration.ofSeconds(60).toNanos();
        request = new HTTPRequest(HTTPMethod.GET, "http://localhost");
        webServiceClient.linkContext(request);
        assertThat(request.headers)
                .as("should use http client timeout if it is shorter than max process time")
                .containsEntry(HTTPHandler.HEADER_TIMEOUT.toString(), "20000000000");

        logManager.end("end");
    }
}

@@ -703,16 +703,51 @@
 eClient;
+%0A    private LogManager logManager;
 %0A%0A    @B
@@ -788,24 +788,63 @@
 eClient() %7B%0A
+        logManager = new LogManager();%0A
         var 
@@ -1114,51 +1114,8 @@
 ) %7B%0A
-        var logManager = new LogManager();%0A
     
@@ -1225,33 +1225,34 @@
 ation.ofSeconds(
-1
+60
 ).toNanos();%0A%0A  
@@ -1564,33 +1564,164 @@
 EF_ID.toString()
-,
+);%0A%0A        assertThat(request.headers)%0A                .as(%22should use http client timeout if it is shorter than max process time%22)
 %0A               
@@ -1713,32 +1713,47 @@
                 
+.containsEntry(
 HTTPHandler.HEAD
@@ -1773,27 +1773,43 @@
 String()
+, %2220000000000%22
 );%0A
+%0A
         
 assertTh
@@ -1804,33 +1804,276 @@
     
-assertThat(Long.parseLong
+logManager.end(%22end%22);%0A    %7D%0A%0A    @Test%0A    void linkContextWithoutMaxProcessTime() %7B%0A        logManager.begin(%22begin%22, null);%0A%0A        var request = new HTTPRequest(HTTPMethod.GET, %22http://localhost%22);%0A        webServiceClient.linkContext(request);%0A        assertThat
 (req
@@ -2088,12 +2088,23 @@
 ders
-.get
+).containsEntry
 (HTT
@@ -2141,84 +2141,183 @@
 ng()
-))).isGreaterThan(0).isLessThanOrEqualTo(Duration.ofSeconds(1).toNanos()
+, %2220000000000%22);%0A%0A        logManager.end(%22end%22);%0A    %7D%0A%0A    @Test%0A    void linkContextWithShortProcessTime() %7B%0A        ActionLog actionLog = logManager.begin(%22begin%22, null
 );%0A
-%0A
     
@@ -2364,34 +2364,33 @@
 ation.ofSeconds(
-60
+1
 ).toNanos();%0A   
@@ -2385,16 +2385,92 @@
 Nanos();
+%0A%0A        var request = new HTTPRequest(HTTPMethod.GET, %22http://localhost%22);
 %0A       
@@ -2603,116 +2603,116 @@
 hat(
-request.headers)%0A                .as(%22should use http client timeout if it is shorter than max process time%22
+Long.parseLong(request.headers.get(HTTPHandler.HEADER_TIMEOUT.toString())))%0A                .isGreaterThan(0
 )%0A  
@@ -2730,74 +2730,59 @@
    .
-containsEntry(HTTPHandler.HEADER_TIMEOUT.toString(), %2220000000000%22
+isLessThanOrEqualTo(Duration.ofSeconds(1).toNanos()
 );%0A%0A

refactor unit test