/*
 * Copyright 2015-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.docksidestage.whitebox.core.json;

import javax.annotation.Resource;

import org.docksidestage.app.web.product.ProductSearchForm;
import org.docksidestage.app.web.product.ProductSearchRowBean;
import org.docksidestage.unit.UnitFortressBasicTestCase;
import org.lastaflute.core.json.JsonEngineResource;
import org.lastaflute.core.json.JsonManager;
import org.lastaflute.core.json.JsonMappingOption;
import org.lastaflute.core.json.engine.RealJsonEngine;

/**
 * @author jflute
 */
public class WxJsonTrickyOptionTest extends UnitFortressBasicTestCase {

    // ===================================================================================
    //                                                                           Attribute
    //                                                                           =========
    @Resource
    private JsonManager jsonManager;

    // ===================================================================================
    //                                                              asNullToEmptyWriting()
    //                                                              ======================
    public void test_asNullToEmptyWriting_typeable() {
        // ## Arrange ##
        JsonEngineResource resource = new JsonEngineResource();
        resource.acceptMappingOption(new JsonMappingOption().asNullToEmptyWriting(tp -> {
            return String.class.isAssignableFrom(tp);
        }));
        RealJsonEngine ruledEngine = jsonManager.newRuledEngine(resource);

        {
            // ## Act ##
            String json = ruledEngine.toJson(new ProductSearchRowBean());

            // ## Assert ##
            log(json);
            assertContains(json, "\"productId\": null");
            assertContains(json, "\"productName\": \"\"");
        }
        {
            // ## Act ##
            String json = ruledEngine.toJson(new ProductSearchForm());

            // ## Assert ##
            log(json);
            assertContains(json, "\"productName\": \"\"");
            assertContains(json, "\"productStatus\": null");
        }
    }
}

@@ -1503,16 +1503,1377 @@
 nager;%0A%0A
+    // ===================================================================================%0A    //                                                              asEmptyToNullReading()%0A    //                                                              ======================%0A    public void test_asEmptyToNullReading_typeable() %7B%0A        // ## Arrange ##%0A        JsonEngineResource resource = new JsonEngineResource();%0A        resource.acceptMappingOption(new JsonMappingOption().asEmptyToNullReading(tp -%3E %7B%0A            return String.class.isAssignableFrom(tp);%0A        %7D));%0A        RealJsonEngine ruledEngine = jsonManager.newRuledEngine(resource);%0A%0A        %7B%0A            // ## Act ##%0A            String json = %22%7B %5C%22productId%5C%22: null, %5C%22productName%5C%22: %5C%22%5C%22 %7D%22;%0A            ProductSearchRowBean bean = ruledEngine.fromJson(json, ProductSearchRowBean.class);%0A%0A            // ## Assert ##%0A            log(bean);%0A            assertNull(bean.productId);%0A            assertNull(bean.productName);%0A        %7D%0A        %7B%0A            // ## Act ##%0A            String json = %22%7B %5C%22productName%5C%22: null, %5C%22productStatus%5C%22: %5C%22%5C%22 %7D%22;%0A            ProductSearchForm form = ruledEngine.fromJson(json, ProductSearchForm.class);%0A%0A            // ## Assert ##%0A            log(form);%0A            assertNull(form.productName);%0A            assertNull(form.productStatus);%0A        %7D%0A    %7D%0A%0A
     // =

test asEmptyToNullReading()