package org.docksidestage.remote.harbor;

import javax.annotation.Resource;

import org.dbflute.remoteapi.mock.MockHttpClient;
import org.docksidestage.remote.harbor.base.RemoteHbPagingReturn;
import org.docksidestage.remote.harbor.product.RemoteHbProductRowReturn;
import org.docksidestage.remote.harbor.product.RemoteHbProductSearchParam;
import org.docksidestage.unit.UnitFortressWebTestCase;
import org.lastaflute.web.servlet.request.RequestManager;

/**
 * @author jflute
 */
public class RemoteHarborBhvTest extends UnitFortressWebTestCase {

    @Resource
    private RequestManager requestManager;

    // ===================================================================================
    //                                                                     for Application
    //                                                                     ===============
    public void test_requestProductList_basic() {
        // ## Arrange ##
        RemoteHbProductSearchParam param = new RemoteHbProductSearchParam();
        param.productName = "S";
        String json = "{pageSize=4, currentPageNumber=1, allRecordCount=20, allPageCount=5, rows=[]}";
        MockHttpClient client = MockHttpClient.create(response -> {
            response.peekRequest(request -> {
                assertContainsAll(request.getBody().get(), "productName", param.productName);
            });
            response.asJsonDirectly(json, request -> true);
        });
        registerMock(client);
        RemoteHarborBhv bhv = new RemoteHarborBhv(requestManager);
        inject(bhv);

        // ## Act ##
        RemoteHbPagingReturn<RemoteHbProductRowReturn> ret = bhv.requestProductList(param);

        // ## Assert ##
        assertEquals(4, ret.pageSize);
        assertEquals(5, ret.allPageCount);
        assertEquals(20, ret.allRecordCount);
        assertEquals(5, ret.allPageCount);
        assertEquals(0, ret.rows.size());
    }

    // ===================================================================================
    //                                                                       for Framework
    //                                                                       =============
    public void test_framework_validationError_basic() {
        // ## Arrange ##
        RemoteHbProductSearchParam param = new RemoteHbProductSearchParam();
        String json = "{cause=VALIDATION_ERROR, errors : [{field=productName, messages=[\"sea land piari\"]}]}";
        MockHttpClient client = MockHttpClient.create(resopnse -> {
            resopnse.asJsonDirectly(json, request -> true).httpStatus(400);
        });
        registerMock(client);
        RemoteHarborBhv bhv = new RemoteHarborBhv(requestManager);
        inject(bhv);

        // ## Act ##
        // ## Assert ##
        mockHtmlValidateCall();
        assertValidationError(() -> bhv.requestProductList(param)).handle(data -> {
            assertTrue(data.requiredMessages().hasMessageOf("productName"));
            data.requiredMessageOfDirectly("productName", "land");
        });
    }
}

@@ -70,16 +70,98 @@
 ource;%0A%0A
+import org.dbflute.remoteapi.exception.RemoteApiResponseValidationErrorException;%0A
 import o
@@ -202,16 +202,16 @@
 Client;%0A
-
 import o
@@ -528,16 +528,63 @@
 Manager;
+%0Aimport org.lastaflute.web.validation.Required;
 %0A%0A/**%0A *
@@ -1042,21 +1042,21 @@
 uctList_
-basic
+empty
 () %7B%0A   
@@ -1201,24 +1201,68 @@
   String
- json = 
+Builder sb = new StringBuilder();%0A        sb.append(
 %22%7BpageSi
@@ -1332,12 +1332,153 @@
 ows=
-%5B%5D%7D%22
+%22);%0A        sb.append(%22%5B%7BproductId=7, productName=%5C%22sea%5C%22, productStatusName=%5C%22land%5C%22, regularPrice=100%7D%5D%7D%22);%0A        String json = sb.toString()
 ;%0A  
@@ -2224,26 +2224,1289 @@
 als(
-0, ret.rows.size()
+1, ret.rows.size());%0A    %7D%0A%0A    public void test_requestProductList_return_validtionError() %7B%0A        // ## Arrange ##%0A        RemoteHbProductSearchParam param = new RemoteHbProductSearchParam();%0A        param.productName = %22S%22;%0A        StringBuilder sb = new StringBuilder();%0A        sb.append(%22%7BpageSize=4, currentPageNumber=1, allRecordCount=20, allPageCount=5, rows=%22);%0A        sb.append(%22%5B%7BproductId=7, productName=%5C%22sea%5C%22%7D%5D%7D%22);%0A        String json = sb.toString();%0A        MockHttpClient client = MockHttpClient.create(response -%3E %7B%0A            response.peekRequest(request -%3E %7B%0A                assertContainsAll(request.getBody().get(), %22productName%22, param.productName);%0A            %7D);%0A            response.asJsonDirectly(json, request -%3E true);%0A        %7D);%0A        registerMock(client);%0A        RemoteHarborBhv bhv = new RemoteHarborBhv(requestManager);%0A        inject(bhv);%0A%0A        // ## Act ##%0A        assertException(RemoteApiResponseValidationErrorException.class, () -%3E bhv.requestProductList(param)).handle(cause -%3E %7B%0A            // ## Assert ##%0A            String errorMsg = cause.getCause().getMessage();%0A            assertContainsAll(errorMsg, %22productStatusName%22, %22regularPrice%22);%0A            assertContains(errorMsg, Required.class.getSimpleName());%0A        %7D
 );%0A 

make test_requestProductList_return_validtionError()