Java tutorial
/* Copyright 2015 DataArt Apps, Inc 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 com.dataart.spreadsheetanalytics.test.functions.dslookup; import static org.assertj.core.api.Assertions.assertThat; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.cache.CacheManager; import javax.cache.Caching; import javax.cache.configuration.MutableConfiguration; import javax.cache.expiry.AccessedExpiryPolicy; import javax.cache.expiry.Duration; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import com.dataart.spreadsheetanalytics.api.engine.DataSetAccessor; import com.dataart.spreadsheetanalytics.api.engine.ExternalServices; import com.dataart.spreadsheetanalytics.api.model.ICellValue; import com.dataart.spreadsheetanalytics.api.model.IDataModel; import com.dataart.spreadsheetanalytics.api.model.IDataModelId; import com.dataart.spreadsheetanalytics.api.model.IDataSet; import com.dataart.spreadsheetanalytics.api.model.ILazyDataSet; import com.dataart.spreadsheetanalytics.engine.CacheBasedDataSetAccessor; import com.dataart.spreadsheetanalytics.engine.Converters; import com.dataart.spreadsheetanalytics.engine.DataSetOptimisationsCache; import com.dataart.spreadsheetanalytics.engine.DataSetOptimisationsCache.DsLookupParameters; import com.dataart.spreadsheetanalytics.engine.SpreadsheetEvaluator; import com.dataart.spreadsheetanalytics.model.A1Address; public class Monthly_Data_Query_Test { static String pathDataModel = "src/test/resources/datamodel/Monthly_Data_Query_Test.xlsx"; static String pathDataSet = "src/test/resources/dataset/Monthly_Data_Query.xlsx"; static Map<String, Object> expectedValues; static String toEvaluateColumn = "A"; static String expectedColumn = "B"; static int expectedRowStart = 2; static int expectedRowEnd = 16; static SpreadsheetEvaluator evaluator; static IDataModel dataModel; @BeforeClass public static void before() throws Exception { dataModel = Converters.toDataModel(new XSSFWorkbook(pathDataModel)); CacheManager cacheManager = Caching.getCachingProvider().getCacheManager(); MutableConfiguration config = new MutableConfiguration(); config.setStoreByValue(false).setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(Duration.ETERNAL)) .setStatisticsEnabled(false); cacheManager.createCache(CacheBasedDataSetAccessor.DATA_SET_TO_ID_CACHE_NAME, config.setTypes(IDataModelId.class, IDataSet.class)); cacheManager.createCache(CacheBasedDataSetAccessor.DATA_SET_TO_NAME_CACHE_NAME, config.setTypes(String.class, IDataSet.class)); cacheManager.createCache(DataSetOptimisationsCache.DATA_SET_TO_LAZY_PARAMETERS, config.setTypes(ILazyDataSet.Parameters.class, IDataSet.class)); cacheManager.createCache(DataSetOptimisationsCache.DATA_SET_DS_LOOKUP_PARAMETERS, config.setTypes(DsLookupParameters.class, List.class)); final ExternalServices external = ExternalServices.INSTANCE; DataSetAccessor dataSetStorage = new CacheBasedDataSetAccessor(); external.setDataSetAccessor(dataSetStorage); external.setDataSetOptimisationsCache(new DataSetOptimisationsCache()); final IDataSet dataSet = Converters.toDataSet(new XSSFWorkbook(pathDataSet)); dataSetStorage.add(dataSet); expectedValues = new HashMap<>(); evaluator = new SpreadsheetEvaluator(dataModel); for (int i = expectedRowStart; i <= expectedRowEnd; i++) { ICellValue value = evaluator.evaluate(A1Address.fromA1Address(expectedColumn + i)).getResult(); expectedValues.put(expectedColumn + i, value.get()); } } @AfterClass public static void after() throws Exception { CacheManager cacheManager = Caching.getCachingProvider().getCacheManager(); cacheManager.destroyCache(CacheBasedDataSetAccessor.DATA_SET_TO_ID_CACHE_NAME); cacheManager.destroyCache(CacheBasedDataSetAccessor.DATA_SET_TO_NAME_CACHE_NAME); cacheManager.destroyCache(DataSetOptimisationsCache.DATA_SET_TO_LAZY_PARAMETERS); cacheManager.destroyCache(DataSetOptimisationsCache.DATA_SET_DS_LOOKUP_PARAMETERS); } @Test public void compare_DsLookupFormula_ExpectedValueFromMap() { //given // Map with expected values: expectedValues for (int i = expectedRowStart; i <= expectedRowEnd; i++) { //when ICellValue value = evaluator.evaluate(A1Address.fromA1Address(toEvaluateColumn + i)).getResult(); //then assertThat(value).isNotNull(); assertThat(value.get()) .overridingErrorMessage("expected:<[%s]> but was:<[%s] at %s]>", expectedValues.get(expectedColumn + i), value.get(), toEvaluateColumn + i) .isEqualTo(expectedValues.get(expectedColumn + i)); } } }