package de.superx.bianalysis; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Optional; import org.junit.After; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.init.ScriptException; import com.fasterxml.jackson.databind.ObjectMapper; import de.superx.BIATestUtils; import de.superx.BaseDbTest; import de.superx.bianalysis.models.Dimension; import de.superx.bianalysis.models.FactTable; import de.superx.bianalysis.models.Right; import de.superx.bianalysis.models.RightParam; import de.superx.bianalysis.repository.dto.MeasureDto; import de.superx.bianalysis.rest.BiAnalysisApi; import de.superx.bianalysis.service.DbMetaAdapter; import de.superx.common.AccessDeniedException; import de.superx.common.NotYetImplementedException; public class BiAnalysisApiTest extends BaseDbTest { @Autowired BiAnalysisApi biAnalysis; @Autowired DbMetaAdapter dbAdapter; public void init() throws ScriptException { user.setRights(new HashMap<>(), null); } @After public void after() { user.getRightsMap().clear(); } @Test public void noAccess() { user.setRights(new HashMap<>(), null); String json = """ { "factTableId": "res_pub:1", "filters": [ { "dimensionAttributeId": "conf_res:20", "filterValues": [ "ja", "nein" ] }, { "dimensionAttributeId": "conf_res:21", "filterValues": [ "Fachzeitschrift für Produktions- und Verfahrenstechnik", "Financial Review", "Finanzwissenschaft", "Genetic Review", "International Journal of Financial Studies" ] }, { "dimensionAttributeId": "res_pub:38", "filterValues": [ "2021", "2022", "2023" ] } ], "leftDimensionAttributeIds": [ "res_pub:38" ], "measureIds": [ "res_pub:21037", "res_pub:21031" ], "topDimensionAttributeIds": [ "conf_res:20", "conf_res:26" ] } """; assertThrows(AccessDeniedException.class, () -> { ReportDefinition definition = new ObjectMapper().readValue(json, ReportDefinition.class); biAnalysis.getReport(definition); }); } @Test public void noPersistAccess() { String file = "storedReport"; assertThrows(AccessDeniedException.class, () -> { StoredReport expectedStoredReport = BIATestUtils.readStoredReportFromJson(file); biAnalysis.persistReportDefinition(expectedStoredReport); }); } @Test public void showOnlyThemesForSachgebiet() throws NotYetImplementedException { setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC_AREA, "441,450"); List facts = biAnalysis.listFactTables(); assertTrue(!facts.isEmpty()); for (FactTable fact : facts) { assertTrue(fact.getSachgebiettid() == 441 || fact.getSachgebiettid() == 450); } } @Test public void failOnUnallowedSachgebietDimension() { setRight(Right.CREATE_ANALYSIS); setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC_AREA, "130"); List dimensions = biAnalysis.listDimensions("res_nom:1"); assertEquals(0, dimensions.size()); setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC_AREA, "130,240"); dimensions = biAnalysis.listDimensions("res_nom:1"); assertTrue(dimensions.size() > 0); } @Test public void failOnUnallowedSachgebietMeasure() { setRight(Right.CREATE_ANALYSIS); setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC_AREA, "240"); List measures = biAnalysis.listMeasures("app_basic:1"); assertEquals(0, measures.size()); setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC_AREA, "130,240"); measures = biAnalysis.listMeasures("app_basic:1"); assertTrue(measures.size() > 0); } @Test public void testValidFactTables() throws NotYetImplementedException { setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC_AREA, "130,240"); List factTables = dbAdapter.getFactTables(); for (FactTable factTable : factTables) { int tid = factTable.getSachgebiettid(); boolean isValidSachgebiet = (tid == 130 || tid == 240); assertTrue("Invalid Sachgebiet " + tid + " for Fact Table " + "'" + factTable.getCaption() + "'", isValidSachgebiet); } } @Test public void showOnlySearchResultsForValidFactTables() { truncateSavedReports(); saveDummyReportForFactTable("res_nom:1", "my report"); saveDummyReportForFactTable("res_nom:1", "huhu"); saveDummyReportForFactTable("res_pub:1", "was"); saveDummyReportForFactTable("app_basic:1", "ist report"); saveDummyReportForFactTable("app_basic:1", "test for das is here"); setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC_AREA, "240,130"); List reports; reports = biAnalysis.findReportDefinition( Optional.of("das"), Optional.ofNullable(null), Optional.ofNullable(null)); assertEquals(1, reports.size()); reports = biAnalysis.findReportDefinition( Optional.of("das"), Optional.of(Integer.valueOf(130)), Optional.of(Arrays.asList("app_basic:1"))); assertEquals(1, reports.size()); setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC_AREA, "240"); setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC, "res_nom:1"); reports = biAnalysis.findReportDefinition( Optional.of("das"), Optional.of(Integer.valueOf(130)), Optional.of(Arrays.asList("app_basic:1"))); assertEquals(0, reports.size()); reports = biAnalysis.findReportDefinition( Optional.of("report"), Optional.ofNullable(null), Optional.ofNullable(null)); assertEquals(1, reports.size()); setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC, "res_nom:1,app_basic:1"); reports = biAnalysis.findReportDefinition( Optional.of("report"), Optional.ofNullable(null), Optional.ofNullable(null)); assertEquals(2, reports.size()); } @Test public void showOnlySearchResultsForValidSachgebiete() { truncateSavedReports(); saveDummyReportForFactTable("res_nom:1", "my report"); saveDummyReportForFactTable("res_nom:1", "huhu"); saveDummyReportForFactTable("res_pub:1", "was"); saveDummyReportForFactTable("app_basic:1", "ist report"); saveDummyReportForFactTable("app_basic:1", "test for das is here"); assertThrows(AccessDeniedException.class, () -> { biAnalysis.findReportDefinition( Optional.ofNullable(null), Optional.ofNullable(null), Optional.of(Arrays.asList("res_pub:1"))); }); assertThrows(AccessDeniedException.class, () -> { biAnalysis.findReportDefinition( Optional.ofNullable(null), Optional.of(Integer.valueOf(240)), Optional.ofNullable(null)); }); setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC_AREA, "240"); List reports; reports = biAnalysis.findReportDefinition( Optional.ofNullable(null), Optional.ofNullable(null), Optional.of(Arrays.asList("res_pub:1"))); assertEquals(1, reports.size()); reports = biAnalysis.findReportDefinition( Optional.ofNullable(null), Optional.ofNullable(null), Optional.of(Arrays.asList("res_pub:1", "app_basic:1"))); assertEquals(1, reports.size()); reports = biAnalysis.findReportDefinition( Optional.ofNullable(null), Optional.of(Integer.valueOf(240)), Optional.ofNullable(null)); assertEquals(3, reports.size()); reports = biAnalysis.findReportDefinition( Optional.of("das"), Optional.ofNullable(null), Optional.ofNullable(null)); assertEquals(0, reports.size()); } @Test public void showFactTablesForValidTopicsAndSachgebiete() throws NotYetImplementedException { setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC_AREA, "240,450"); setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC, "exa:25", false); List facts = biAnalysis.listFactTables(); assertEquals(11, facts.size()); // 1 für exa, 5 für res setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC, "exa:25,res:105,res_nom:1"); facts = biAnalysis.listFactTables(); assertEquals(3, facts.size()); // 1 für exa, 2 für res } private int saveDummyReportForFactTable(String factId, String name) { StoredReport report = new StoredReport(); report.definition = "{\"factTableId\": \"" + factId + "\" }"; report.name = name; return dbAdapter.saveReportDefinition(report); } private void truncateSavedReports() { new JdbcTemplate(dataSource).execute("truncate table metadata.rw_report_definitions"); } }