ScenarioCsvImportExport { public <T extends AbstractScesimData> AbstractScesimModel<T> importData(String raw, AbstractScesimModel<T> originalScesimModel) throws IOException { CSVParser csvParser = CSVFormat.DEFAULT.parse(new StringReader(raw)); AbstractScesimModel<T> toReturn = originalScesimModel.cloneModel(); toReturn.clearDatas(); List<FactMapping> factMappings = toReturn.getScesimModelDescriptor().getUnmodifiableFactMappings(); List<CSVRecord> csvRecords = csvParser.getRecords(); if (csvRecords.size() < HEADER_SIZE) { throw new IllegalArgumentException("Malformed file, missing header"); } csvRecords = csvRecords.subList(HEADER_SIZE, csvRecords.size()); for (CSVRecord csvRecord : csvRecords) { T scesimDataToFill = toReturn.addData(); if (csvRecord.size() != factMappings.size()) { throw new IllegalArgumentException("Malformed row " + csvRecord); } for (int i = 0; i < factMappings.size(); i += 1) { FactMapping factMapping = factMappings.get(i); String valueToImport = "".equals(csvRecord.get(i)) ? null : csvRecord.get(i); scesimDataToFill.addMappingValue(factMapping.getFactIdentifier(), factMapping.getExpressionIdentifier(), valueToImport); } } return toReturn; }  String exportData(AbstractScesimModel<? extends AbstractScesimData> scesimModel); AbstractScesimModel<T> importData(String raw, AbstractScesimModel<T> originalScesimModel); static final int HEADER_SIZE; }
@Test public void importData() throws IOException { Simulation originalSimulation = createDummySimulation(3, 1); assertEquals(1, originalSimulation.getUnmodifiableData().size()); String rawCSV = "OTHER,OTHER,GIVEN,GIVEN,GIVEN\r\n" + "#,Scenario description,instance1,instance2,instance3\r\n" + "Index,Description,property1,property2,property3\r\n" + "1,My Scenario,value1,value2,"; AbstractScesimModel retrieved = scenarioCsvImportExport.importData(rawCSV, originalSimulation); assertEquals(1, retrieved.getUnmodifiableData().size()); assertEquals("value1", retrieved.getDataByIndex(0).getFactMappingValue(retrieved.getScesimModelDescriptor().getFactMappingByIndex(2)).get().getRawValue()); assertEquals("value2", retrieved.getDataByIndex(0).getFactMappingValue(retrieved.getScesimModelDescriptor().getFactMappingByIndex(3)).get().getRawValue()); assertNull(retrieved.getDataByIndex(0).getFactMappingValue(retrieved.getScesimModelDescriptor().getFactMappingByIndex(4)).get().getRawValue()); assertThatThrownBy(() -> scenarioCsvImportExport.importData("", originalSimulation)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Malformed file, missing header"); }