/**
 * Copyright (C) 2017 Hurence (support@hurence.com)
 * <p>
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 com.hurence.logisland.service.cache;

import com.hurence.logisland.component.InitializationException;
import com.hurence.logisland.controller.ControllerServiceInitializationContext;
import com.hurence.logisland.record.Record;
import com.hurence.logisland.util.runner.TestRunner;
import com.hurence.logisland.util.runner.TestRunners;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class CSVKeyValueCacheServiceTest {


    @Test
    public void testLookup() throws InitializationException, IOException {

        final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
        runner.setProperty(TestProcessor.CACHE_SERVICE, "lookup_service");


        File file = new File(getClass().getClassLoader().getResource("lookup.csv").getFile());
        String dbPath = file.getAbsolutePath();

        runner.setProperty(CSVKeyValueCacheService.DATABASE_FILE_PATH, dbPath);
        runner.setProperty(CSVKeyValueCacheService.CSV_FORMAT, "excel_fr");
        runner.setProperty(CSVKeyValueCacheService.FIRST_LINE_HEADER, "true");
        runner.setProperty(CSVKeyValueCacheService.ROW_KEY, "tagname");
        runner.setProperty(CacheService.CACHE_SIZE, "20000");

        // create the controller service and link it to the test processor
        final CSVKeyValueCacheService service = new CSVKeyValueCacheService();
        runner.addControllerService("lookup_service", service);
        runner.enableControllerService(service);

        final CSVKeyValueCacheService lookupService = runner.getProcessContext()
                .getPropertyValue(TestProcessor.CACHE_SERVICE)
                .asControllerService(CSVKeyValueCacheService.class);

        Record result = lookupService.get("D112.M_TI41.F_CV");


        // Compare maps
        assertEquals("Expected and result maps should be identical", result.getField("tagname").asString(), "D112.M_TI41.F_CV");
        assertEquals("Expected and result maps should be identical", result.getField("description").asString(), "Température amont Valco (F_CV)");
        assertEquals("Expected and result maps should be identical", result.getField("engunits").asString(), "DEG");
        assertEquals("Expected and result maps should be identical", result.getField("lastmodified").asString(), "03/06/2014 10:32:18");

    }


}

@@ -1817,24 +1817,103 @@
 %22tagname%22);%0A
+        runner.setProperty(CSVKeyValueCacheService.ENCODING_CHARSET, %22UTF-8%22);%0A
         runn
@@ -1958,24 +1958,24 @@
 , %2220000%22);%0A
-
 %0A        // 
@@ -2560,52 +2560,24 @@
 ls(%22
-Expected and result maps should be identical
+D112.M_TI41.F_CV
 %22, r
@@ -2616,28 +2616,8 @@
 ng()
-, %22D112.M_TI41.F_CV%22
 );%0A 
@@ -2641,199 +2641,339 @@
 ls(%22
-Expected and result maps should be identical%22, result.getField(%22description%22).asString(), %22Temp%C3%A9rature amont Valco (F_CV)%22);%0A        assertEquals(%22Expected and result maps should be identical
+Temp%C3%A9rature amont Valco (F_CV)%22, result.getField(%22description%22).asString());%0A        assertEquals(%22DEG%22, result.getField(%22engunits%22).asString());%0A        assertEquals(%2203/06/2014 10:32:18%22, result.getField(%22lastmodified%22).asString());%0A%0A%0A        result = lookupService.get(%22D112.M_TI15.F_CV%22);%0A        assertEquals(%22D112.M_TI15.F_CV
 %22, r
@@ -2984,32 +2984,31 @@
 t.getField(%22
-engunits
+tagname
 %22).asString(
@@ -3012,15 +3012,8 @@
 ng()
-, %22DEG%22
 );%0A 
@@ -3037,52 +3037,94 @@
 ls(%22
-Expected and result maps should be identical
+Tra%EF%BF%BDage (F_CV)%22, result.getField(%22description%22).asString());%0A        assertEquals(%22DEG
 %22, r
@@ -3135,36 +3135,32 @@
 t.getField(%22
-lastmodified
+engunits
 %22).asString(
@@ -3164,33 +3164,103 @@
 ng()
-, %2203/06/2014 10:32:18%22);
+);%0A        assertEquals(%2203/06/2014 10:32:18%22, result.getField(%22lastmodified%22).asString());%0A%0A%0A%0A
 %0A%0A  

fix date parsing bug