Starting from 2021-07-01, all LRZ GitLab users will be required to explicitly accept the GitLab Terms of Service. Please see the detailed information at https://doku.lrz.de/display/PUBLIC/GitLab and make sure that your projects conform to the requirements.

Commit acf108eb authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

Add EvacuationTimeProcessorTest

parent 160c1b99
package org.vadere.simulator.projects.dataprocessing.processor;
import org.junit.Before;
import org.junit.Test;
public class EvacuationTimeProcessorTest extends ProcessorTest {
@Before
public void setup() {
processorTestEnv = new EvacuationTimeProcessorTestEnv();
super.setup();
}
@Test
public void doUpdate() throws Exception {
super.doUpdate();
}
@Test
public void doUpdateNaN() throws Exception {
((EvacuationTimeProcessorTestEnv) processorTestEnv).loadSimulationStateMocksNaN();
super.doUpdate();
}
@Test
public void init() throws Exception {
super.init();
}
}
\ No newline at end of file
package org.vadere.simulator.projects.dataprocessing.processor;
import org.mockito.Mockito;
import org.vadere.simulator.projects.dataprocessing.datakey.NoDataKey;
import org.vadere.simulator.projects.dataprocessing.writer.VadereWriterFactory;
import org.vadere.state.attributes.processor.AttributesEvacuationTimeProcessor;
import org.vadere.state.scenario.Pedestrian;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import static org.mockito.Mockito.when;
public class EvacuationTimeProcessorTestEnv extends ProcessorTestEnv<NoDataKey, Double> {
private PedestrianListBuilder b = new PedestrianListBuilder();
@SuppressWarnings("unchecked")
EvacuationTimeProcessorTestEnv() {
testedProcessor = processorFactory.createDataProcessor(EvacuationTimeProcessor.class);
testedProcessor.setId(nextProcessorId());
DataProcessor pedEvacTimeProc;
PedestrianEvacuationTimeProcessorTestEnv pedEvacTimeProcEnv;
int pedEvacTimeProcId = nextProcessorId();
//add ProcessorId of required Processors to current Processor under test
AttributesEvacuationTimeProcessor attr = (AttributesEvacuationTimeProcessor) testedProcessor.getAttributes();
attr.setPedestrianEvacuationTimeProcessorId(pedEvacTimeProcId);
//create required Processor enviroment and add it to current Processor under test
pedEvacTimeProcEnv = new PedestrianEvacuationTimeProcessorTestEnv(pedEvacTimeProcId);
pedEvacTimeProc = pedEvacTimeProcEnv.getTestedProcessor();
Mockito.when(manager.getProcessor(pedEvacTimeProcId)).thenReturn(pedEvacTimeProc);
addRequiredProcessors(pedEvacTimeProcEnv);
//setup output file with different VadereWriter impl for test
outputFile = outputFileFactory.createDefaultOutputfileByDataKey(
NoDataKey.class,
testedProcessor.getId()
);
outputFile.setVadereWriterFactory(VadereWriterFactory.getStringWriterFactory());
}
void loadSimulationStateMocksNaN() {
clearStates();
loadDefaultSimulationStateMocks();
removeState(3);
addSimState(new SimulationStateMock(4) {
@Override
public void mockIt() {
b.clear().add(new Integer[]{1});
when(state.getTopography().getElements(Pedestrian.class)).thenReturn(b.getList());
when(state.getSimTimeInSec()).thenReturn(40.0);
addToExpectedOutput(NoDataKey.key(), Double.NaN);
}
});
}
@Override
public void loadDefaultSimulationStateMocks() {
addSimState(new SimulationStateMock(1) {
@Override
public void mockIt() {
b.clear().add(1, 3, 4);
when(state.getTopography().getElements(Pedestrian.class)).thenReturn(b.getList());
when(state.getSimTimeInSec()).thenReturn(0.4);
}
});
addSimState(new SimulationStateMock(2) {
@Override
public void mockIt() {
b.clear().add(1, 3, 4, 5, 8);
when(state.getTopography().getElements(Pedestrian.class)).thenReturn(b.getList());
when(state.getSimTimeInSec()).thenReturn(12.8);
}
});
addSimState(new SimulationStateMock(3) {
@Override
public void mockIt() {
b.clear().add(1, 5, 8);
when(state.getTopography().getElements(Pedestrian.class)).thenReturn(b.getList());
when(state.getSimTimeInSec()).thenReturn(34.7);
}
});
addSimState(new SimulationStateMock(4) {
@Override
public void mockIt() {
b.clear();
when(state.getTopography().getElements(Pedestrian.class)).thenReturn(b.getList());
when(state.getSimTimeInSec()).thenReturn(40.0);
addToExpectedOutput(NoDataKey.key(), 34.7 - 0.4);
}
});
}
@Override
List<String> getExpectedOutputAsList() {
List<String> outputList = new ArrayList<>();
expectedOutput.entrySet()
.stream()
.sorted(Comparator.comparing(Map.Entry::getKey))
.forEach(e -> {
StringJoiner sj = new StringJoiner(getDelimiter());
sj.add(Double.toString(e.getValue()));
outputList.add(sj.toString());
});
return outputList;
}
}
...@@ -3,10 +3,10 @@ package org.vadere.simulator.projects.dataprocessing.processor; ...@@ -3,10 +3,10 @@ package org.vadere.simulator.projects.dataprocessing.processor;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.vadere.simulator.control.SimulationState; import org.vadere.simulator.control.SimulationState;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager; import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.writer.VadereStringWriter;
import org.vadere.simulator.projects.dataprocessing.datakey.DataKey; import org.vadere.simulator.projects.dataprocessing.datakey.DataKey;
import org.vadere.simulator.projects.dataprocessing.outputfile.OutputFile; import org.vadere.simulator.projects.dataprocessing.outputfile.OutputFile;
import org.vadere.simulator.projects.dataprocessing.outputfile.OutputFileFactory; import org.vadere.simulator.projects.dataprocessing.outputfile.OutputFileFactory;
import org.vadere.simulator.projects.dataprocessing.writer.VadereStringWriter;
import org.vadere.tests.reflection.ReflectionHelper; import org.vadere.tests.reflection.ReflectionHelper;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -31,10 +31,6 @@ public abstract class ProcessorTestEnv<K extends DataKey<K>, V> { ...@@ -31,10 +31,6 @@ public abstract class ProcessorTestEnv<K extends DataKey<K>, V> {
* processor under test * processor under test
*/ */
DataProcessor<?, ?> testedProcessor; DataProcessor<?, ?> testedProcessor;
/**
* List of {@link SimulationState}s used for test. (mocked)
*/
private List<SimulationStateMock> states;
/** /**
* Ids of {@link DataProcessor}s * Ids of {@link DataProcessor}s
*/ */
...@@ -53,6 +49,10 @@ public abstract class ProcessorTestEnv<K extends DataKey<K>, V> { ...@@ -53,6 +49,10 @@ public abstract class ProcessorTestEnv<K extends DataKey<K>, V> {
* Needed for DataProcessor doUpdate call. (mocked) * Needed for DataProcessor doUpdate call. (mocked)
*/ */
ProcessorManager manager; ProcessorManager manager;
/**
* List of {@link SimulationState}s used for test. (mocked)
*/
private List<SimulationStateMock> states;
/** /**
* If {@link #testedProcessor} has dependencies to other {@link DataProcessor}s * If {@link #testedProcessor} has dependencies to other {@link DataProcessor}s
*/ */
...@@ -75,14 +75,15 @@ public abstract class ProcessorTestEnv<K extends DataKey<K>, V> { ...@@ -75,14 +75,15 @@ public abstract class ProcessorTestEnv<K extends DataKey<K>, V> {
} }
/** /**
* Initialize {@link DataProcessor}, {@link OutputFile} and initialize all requiredProcessors * Initialize {@link DataProcessor}, {@link OutputFile} and initialize all requiredProcessors if
* if needed. * needed.
*/ */
@SuppressWarnings("unchecked")
void init() { void init() {
delimiter = outputFile.getSeparator(); delimiter = outputFile.getSeparator();
outputFile.init(getProcessorMap()); outputFile.init(getProcessorMap());
testedProcessor.init(manager); testedProcessor.init(manager);
requiredProcessors.forEach(env -> env.init()); requiredProcessors.forEach(ProcessorTestEnv::init);
} }
/** /**
...@@ -91,14 +92,12 @@ public abstract class ProcessorTestEnv<K extends DataKey<K>, V> { ...@@ -91,14 +92,12 @@ public abstract class ProcessorTestEnv<K extends DataKey<K>, V> {
public abstract void loadDefaultSimulationStateMocks(); public abstract void loadDefaultSimulationStateMocks();
/** /**
* Add Mocked SimulationsState to current Processor under test and all its required * Add Mocked SimulationsState to current Processor under test and all its required {@link
* {@link DataProcessor} * DataProcessor}
* @param mock
*/ */
public void addSimState(SimulationStateMock mock){ public void addSimState(SimulationStateMock mock) {
states.add(mock); states.add(mock);
if (!requiredProcessors.isEmpty()) requiredProcessors.forEach(e -> e.addSimState(mock));
requiredProcessors.forEach(e -> e.addSimState(mock));
} }
List<SimulationState> getSimStates() { List<SimulationState> getSimStates() {
...@@ -127,18 +126,22 @@ public abstract class ProcessorTestEnv<K extends DataKey<K>, V> { ...@@ -127,18 +126,22 @@ public abstract class ProcessorTestEnv<K extends DataKey<K>, V> {
return delimiter; return delimiter;
} }
public void clearStates(){ void removeState(int index) {
states.remove(index);
requiredProcessors.forEach(env -> env.removeState(index));
}
void clearStates() {
states.clear(); states.clear();
requiredProcessors.forEach(env -> env.clearStates()); requiredProcessors.forEach(ProcessorTestEnv::clearStates);
} }
public void addRequiredProcessors(ProcessorTestEnv env){ void addRequiredProcessors(ProcessorTestEnv env) {
requiredProcessors.add(env); requiredProcessors.add(env);
} }
/** /**
* Return the ProcessorMap for the current Test. * Return the ProcessorMap for the current Test.
* @return
*/ */
private Map<Integer, DataProcessor<?, ?>> getProcessorMap() { private Map<Integer, DataProcessor<?, ?>> getProcessorMap() {
Map<Integer, DataProcessor<?, ?>> processorMap = new LinkedHashMap<>(); Map<Integer, DataProcessor<?, ?>> processorMap = new LinkedHashMap<>();
...@@ -156,13 +159,13 @@ public abstract class ProcessorTestEnv<K extends DataKey<K>, V> { ...@@ -156,13 +159,13 @@ public abstract class ProcessorTestEnv<K extends DataKey<K>, V> {
List<String> getOutput() throws NoSuchFieldException, IllegalAccessException { List<String> getOutput() throws NoSuchFieldException, IllegalAccessException {
ReflectionHelper r = ReflectionHelper.create(outputFile); ReflectionHelper r = ReflectionHelper.create(outputFile);
VadereStringWriter writer = (VadereStringWriter) r.valOfField("writer"); VadereStringWriter writer = r.valOfField("writer");
return writer.getOutput().subList(1, writer.getOutput().size()); return writer.getOutput().subList(1, writer.getOutput().size());
} }
String getHeader() throws NoSuchFieldException, IllegalAccessException { String getHeader() throws NoSuchFieldException, IllegalAccessException {
ReflectionHelper r = ReflectionHelper.create(outputFile); ReflectionHelper r = ReflectionHelper.create(outputFile);
VadereStringWriter writer = (VadereStringWriter) r.valOfField("writer"); VadereStringWriter writer = r.valOfField("writer");
return writer.getOutput().get(0); return writer.getOutput().get(0);
} }
......
...@@ -2,13 +2,17 @@ package org.vadere.state.attributes.processor; ...@@ -2,13 +2,17 @@ package org.vadere.state.attributes.processor;
/** /**
* @author Mario Teixeira Parente * @author Mario Teixeira Parente
*
*/ */
public class AttributesEvacuationTimeProcessor extends AttributesProcessor { public class AttributesEvacuationTimeProcessor extends AttributesProcessor {
private int pedestrianEvacuationTimeProcessorId; private int pedestrianEvacuationTimeProcessorId;
public int getPedestrianEvacuationTimeProcessorId() { public int getPedestrianEvacuationTimeProcessorId() {
return this.pedestrianEvacuationTimeProcessorId; return this.pedestrianEvacuationTimeProcessorId;
} }
public void setPedestrianEvacuationTimeProcessorId(int id) {
checkSealed();
this.pedestrianEvacuationTimeProcessorId = id;
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment