/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.kylin.metadata.model;

import java.io.IOException;

import org.apache.kylin.common.util.LocalFileMetadataTestCase;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class PartitionDescTest extends LocalFileMetadataTestCase {

    @Before
    public void setUp() throws Exception {
        this.createTestMetadata();
    }

    @After
    public void after() throws Exception {
        this.cleanupTestMetadata();
    }

    @Test
    public void testCustomYearMonthDayFieldPartitionConditionBuilder() throws IOException {
        DataModelManager dataModelManager = DataModelManager.getInstance(getTestConfig());
        DataModelDesc model = dataModelManager.getDataModelDesc("test_kylin_inner_join_model_desc");
        PartitionDesc yearMonthDayPartitionDesc = new PartitionDesc();
        yearMonthDayPartitionDesc.setPartitionDateColumn(
                "TEST_KYLIN_FACT.LSTG_SITE_ID,TEST_KYLIN_FACT.LEAF_CATEG_ID,TEST_KYLIN_FACT.SELLER_ID");
        yearMonthDayPartitionDesc.setPartitionConditionBuilderClz(
                "org.apache.kylin.metadata.model.PartitionDesc$CustomYearMonthDayFieldPartitionConditionBuilder");
        model.setPartitionDesc(yearMonthDayPartitionDesc);
        dataModelManager.updateDataModelDesc(model);
        PartitionDesc.CustomYearMonthDayFieldPartitionConditionBuilder builder = (PartitionDesc.CustomYearMonthDayFieldPartitionConditionBuilder) model
                .getPartitionDesc().getPartitionConditionBuilder();
        String dateRangeCondition = builder.buildDateRangeCondition(model.getPartitionDesc(), null,
                new SegmentRange(0l, 86400000l), null);
        Assert.assertEquals(
                "CONCAT(TEST_KYLIN_FACT.LSTG_SITE_ID,'-',TEST_KYLIN_FACT.LEAF_CATEG_ID,'-',TEST_KYLIN_FACT.SELLER_ID) < '1970-01-02'",
                dateRangeCondition);
    }

}

@@ -871,16 +871,64 @@
 ption;%0A%0A
+import org.apache.kylin.common.util.DateFormat;%0A
 import o
@@ -982,16 +982,16 @@
 stCase;%0A
-
 import o
@@ -1148,16 +1148,103 @@
 Case %7B%0A%0A
+    private PartitionDesc.DefaultPartitionConditionBuilder partitionConditionBuilder;%0A%0A
     @Bef
@@ -1317,32 +1317,122 @@
 TestMetadata();%0A
+        partitionConditionBuilder = new PartitionDesc.DefaultPartitionConditionBuilder();%0A
     %7D%0A%0A    @Afte
@@ -2925,10 +2925,1150 @@
 %0A    %7D%0A%0A
+    // %5BKYLIN-4495%5D Support custom date formats for partition date column%0A    @Test%0A    public void testCustomDateFormat() %7B%0A        PartitionDesc partitionDesc = new PartitionDesc();%0A        TblColRef col = TblColRef.mockup(TableDesc.mockup(%22DEFAULT.TABLE_NAME%22), 1, %22DATE_COLUMN%22, %22string%22);%0A        partitionDesc.setPartitionDateColumnRef(col);%0A        partitionDesc.setPartitionDateColumn(col.getCanonicalName());%0A        partitionDesc.setPartitionDateFormat(%22yyyy/MM/dd%22);%0A        SegmentRange.TSRange range = new SegmentRange.TSRange(%0A                DateFormat.stringToMillis(%222016-02-22%22),%0A                DateFormat.stringToMillis(%222016-02-23%22));%0A        String condition = partitionConditionBuilder.buildDateRangeCondition(partitionDesc, null, range, null);%0A        Assert.assertEquals(%0A                %22UNKNOWN_ALIAS.DATE_COLUMN %3E= '2016/02/22' AND UNKNOWN_ALIAS.DATE_COLUMN %3C '2016/02/23'%22,%0A                condition);%0A%0A        range = new SegmentRange.TSRange(0L, 0L);%0A        condition = partitionConditionBuilder.buildDateRangeCondition(partitionDesc, null, range, null);%0A        Assert.assertEquals(%221=0%22, condition);%0A    %7D%0A
 %7D%0A

add ut