Skip to content

Commit

Permalink
improve code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Franck LECUYER <[email protected]>
  • Loading branch information
FranckLecuyer committed Mar 15, 2024
1 parent 4a24073 commit 9ab498f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
47 changes: 29 additions & 18 deletions src/test/java/org/gridsuite/filter/FiltersUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.gridsuite.filter.identifierlistfilter.IdentifiableAttributes;
import org.gridsuite.filter.identifierlistfilter.IdentifierListFilter;
import org.gridsuite.filter.identifierlistfilter.IdentifierListFilterEquipmentAttributes;
import org.gridsuite.filter.scriptfilter.ScriptFilter;
import org.gridsuite.filter.utils.EquipmentType;
import org.gridsuite.filter.utils.FilterServiceUtils;
import org.gridsuite.filter.utils.FilterType;
Expand All @@ -46,7 +47,6 @@
import org.gridsuite.filter.utils.expertfilter.CombinatorType;
import org.gridsuite.filter.utils.expertfilter.FieldType;
import org.gridsuite.filter.utils.expertfilter.OperatorType;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -62,6 +62,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
* @author Franck Lecuyer <franck.lecuyer at rte-france.com>
Expand Down Expand Up @@ -102,7 +103,7 @@ public void testSubstationFilter() {
.build();

assertFalse(substationFilter.isEmpty());
Assert.assertEquals(EquipmentType.SUBSTATION, substationFilter.getEquipmentType());
assertEquals(EquipmentType.SUBSTATION, substationFilter.getEquipmentType());

CriteriaFilter substationCriteriaFilter = new CriteriaFilter(
UUID.randomUUID(),
Expand Down Expand Up @@ -151,7 +152,7 @@ public void testVoltageLevelFilter() {
.build();

assertFalse(voltageLevelFilter.isEmpty());
Assert.assertEquals(EquipmentType.VOLTAGE_LEVEL, voltageLevelFilter.getEquipmentType());
assertEquals(EquipmentType.VOLTAGE_LEVEL, voltageLevelFilter.getEquipmentType());

CriteriaFilter voltageLevelCriteriaFilter = new CriteriaFilter(
UUID.randomUUID(),
Expand Down Expand Up @@ -204,7 +205,7 @@ public void testLineFilter() {
.build();

assertFalse(lineFilter.isEmpty());
Assert.assertEquals(EquipmentType.LINE, lineFilter.getEquipmentType());
assertEquals(EquipmentType.LINE, lineFilter.getEquipmentType());

CriteriaFilter lineCriteriaFilter = new CriteriaFilter(
UUID.randomUUID(),
Expand Down Expand Up @@ -257,7 +258,7 @@ public void testTwoWindingsTransformerFilter() {
.build();

assertFalse(twoWindingsTransformerFilter.isEmpty());
Assert.assertEquals(EquipmentType.TWO_WINDINGS_TRANSFORMER, twoWindingsTransformerFilter.getEquipmentType());
assertEquals(EquipmentType.TWO_WINDINGS_TRANSFORMER, twoWindingsTransformerFilter.getEquipmentType());

CriteriaFilter twoWindingsTransformerCriteriaFilter = new CriteriaFilter(
UUID.randomUUID(),
Expand Down Expand Up @@ -307,7 +308,7 @@ public void testThreeWindingsTransformerFilter() {
.build();

assertFalse(threeWindingsTransformerFilter.isEmpty());
Assert.assertEquals(EquipmentType.THREE_WINDINGS_TRANSFORMER, threeWindingsTransformerFilter.getEquipmentType());
assertEquals(EquipmentType.THREE_WINDINGS_TRANSFORMER, threeWindingsTransformerFilter.getEquipmentType());

CriteriaFilter threeWindingsTransformerCriteriaFilter = new CriteriaFilter(
UUID.randomUUID(),
Expand Down Expand Up @@ -354,7 +355,7 @@ public void testGeneratorFilter() {
.build();

assertFalse(generatorFilter.isEmpty());
Assert.assertEquals(EquipmentType.GENERATOR, generatorFilter.getEquipmentType());
assertEquals(EquipmentType.GENERATOR, generatorFilter.getEquipmentType());

CriteriaFilter generatorCriteriaFilter = new CriteriaFilter(
UUID.randomUUID(),
Expand Down Expand Up @@ -406,7 +407,7 @@ public void testLoadFilter() {
.build();

assertFalse(loadFilter.isEmpty());
Assert.assertEquals(EquipmentType.LOAD, loadFilter.getEquipmentType());
assertEquals(EquipmentType.LOAD, loadFilter.getEquipmentType());

CriteriaFilter loadCriteriaFilter = new CriteriaFilter(
UUID.randomUUID(),
Expand Down Expand Up @@ -454,7 +455,7 @@ public void testBatteryFilter() {
.build();

assertFalse(batteryFilter.isEmpty());
Assert.assertEquals(EquipmentType.BATTERY, batteryFilter.getEquipmentType());
assertEquals(EquipmentType.BATTERY, batteryFilter.getEquipmentType());

CriteriaFilter batteryCriteriaFilter = new CriteriaFilter(
UUID.randomUUID(),
Expand Down Expand Up @@ -499,7 +500,7 @@ public void testShuntCompensatorFilter() {
.build();

assertFalse(shuntCompensatorFilter.isEmpty());
Assert.assertEquals(EquipmentType.SHUNT_COMPENSATOR, shuntCompensatorFilter.getEquipmentType());
assertEquals(EquipmentType.SHUNT_COMPENSATOR, shuntCompensatorFilter.getEquipmentType());

CriteriaFilter shuntCompensatorCriteriaFilter = new CriteriaFilter(
UUID.randomUUID(),
Expand Down Expand Up @@ -547,7 +548,7 @@ public void testStaticVarCompensatorFilter() {
.build();

assertFalse(staticVarCompensatorFilter.isEmpty());
Assert.assertEquals(EquipmentType.STATIC_VAR_COMPENSATOR, staticVarCompensatorFilter.getEquipmentType());
assertEquals(EquipmentType.STATIC_VAR_COMPENSATOR, staticVarCompensatorFilter.getEquipmentType());

CriteriaFilter staticVarCompensatorCriteriaFilter = new CriteriaFilter(
UUID.randomUUID(),
Expand Down Expand Up @@ -599,7 +600,7 @@ public void testDanglingLineFilter() {
.build();

assertFalse(danglingLineFilter.isEmpty());
Assert.assertEquals(EquipmentType.DANGLING_LINE, danglingLineFilter.getEquipmentType());
assertEquals(EquipmentType.DANGLING_LINE, danglingLineFilter.getEquipmentType());

CriteriaFilter danglingLineCriteriaFilter = new CriteriaFilter(
UUID.randomUUID(),
Expand Down Expand Up @@ -644,7 +645,7 @@ public void testBusbarSectionFilter() {
.build();

assertFalse(busbarSectionFilter.isEmpty());
Assert.assertEquals(EquipmentType.BUSBAR_SECTION, busbarSectionFilter.getEquipmentType());
assertEquals(EquipmentType.BUSBAR_SECTION, busbarSectionFilter.getEquipmentType());

CriteriaFilter busbarSectionCriteriaFilter = new CriteriaFilter(
UUID.randomUUID(),
Expand Down Expand Up @@ -703,7 +704,7 @@ public void testLccConverterStationFilter() {
.build();

assertFalse(lccConverterStationFilter.isEmpty());
Assert.assertEquals(EquipmentType.LCC_CONVERTER_STATION, lccConverterStationFilter.getEquipmentType());
assertEquals(EquipmentType.LCC_CONVERTER_STATION, lccConverterStationFilter.getEquipmentType());

CriteriaFilter lccConverterStationCriteriaFilter = new CriteriaFilter(
UUID.randomUUID(),
Expand Down Expand Up @@ -748,7 +749,7 @@ public void testVscConverterStationFilter() {
.build();

assertFalse(vscConverterStationFilter.isEmpty());
Assert.assertEquals(EquipmentType.VSC_CONVERTER_STATION, vscConverterStationFilter.getEquipmentType());
assertEquals(EquipmentType.VSC_CONVERTER_STATION, vscConverterStationFilter.getEquipmentType());

CriteriaFilter vscConverterStationCriteriaFilter = new CriteriaFilter(
UUID.randomUUID(),
Expand Down Expand Up @@ -798,7 +799,7 @@ public void testHvdcLineFilter() {
.build();

assertFalse(hvdcLineFilter.isEmpty());
Assert.assertEquals(EquipmentType.HVDC_LINE, hvdcLineFilter.getEquipmentType());
assertEquals(EquipmentType.HVDC_LINE, hvdcLineFilter.getEquipmentType());

CriteriaFilter hvdcLineCriteriaFilter = new CriteriaFilter(
UUID.randomUUID(),
Expand Down Expand Up @@ -849,8 +850,8 @@ public void testIdentifierListFilter() {
EquipmentType.GENERATOR,
filterEquipmentAttributes);

Assert.assertEquals(FilterType.IDENTIFIER_LIST, identifierListFilter.getType());
Assert.assertEquals(50., identifierListFilter.getDistributionKey("GEN2"), 0.001);
assertEquals(FilterType.IDENTIFIER_LIST, identifierListFilter.getType());
assertEquals(50., identifierListFilter.getDistributionKey("GEN2"), 0.001);

List<IdentifiableAttributes> identifiableAttributes = List.of(
new IdentifiableAttributes("GEN", IdentifiableType.GENERATOR, 50.),
Expand All @@ -865,6 +866,16 @@ public void testIdentifierListFilter() {
assertEquals("GEN2", identifiables.get(1).getId());
}

@Test
public void testScriptFilter() {
ScriptFilter scriptFilter = new ScriptFilter(
UUID.randomUUID(),
new Date(),
"");
assertEquals(FilterType.SCRIPT, scriptFilter.getType());
assertNull(scriptFilter.getEquipmentType());
}

@Test
public void testFilterLoader() {
// with identifier list filter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import org.gridsuite.filter.FilterLoader;
import org.gridsuite.filter.expertfilter.expertrule.*;
import org.gridsuite.filter.utils.expertfilter.CombinatorType;
import org.gridsuite.filter.utils.expertfilter.DataType;
import org.gridsuite.filter.utils.expertfilter.FieldType;
import org.gridsuite.filter.utils.expertfilter.OperatorType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -20,6 +22,7 @@
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class CombinatorExpertRuleTest {
FilterLoader filterLoader;
Expand All @@ -29,11 +32,18 @@ public void setUp() {
filterLoader = uuids -> null;
}

@Test
public void testStringValue() {
CombinatorExpertRule rule = CombinatorExpertRule.builder().combinator(CombinatorType.AND).rules(null).build();
assertNull(rule.getStringValue());
assertEquals(DataType.COMBINATOR, rule.getDataType());
}

@ParameterizedTest
@MethodSource({"provideArgumentsForTest"})
void testEvaluateRule(CombinatorType combinatorType, List<AbstractExpertRule> rules, Identifiable<?> equipment, boolean expected) {
CombinatorExpertRule filter = CombinatorExpertRule.builder().combinator(combinatorType).rules(rules).build();
assertEquals(expected, filter.evaluateRule(equipment, filterLoader, new HashMap<>()));
CombinatorExpertRule rule = CombinatorExpertRule.builder().combinator(combinatorType).rules(rules).build();
assertEquals(expected, rule.evaluateRule(equipment, filterLoader, new HashMap<>()));
}

private static Stream<Arguments> provideArgumentsForTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.gridsuite.filter.expertfilter.expertrule.StringExpertRule;
import org.gridsuite.filter.utils.expertfilter.FieldType;
import org.gridsuite.filter.utils.expertfilter.OperatorType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand All @@ -14,6 +15,7 @@

import java.util.HashMap;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Stream;

import static org.gridsuite.filter.utils.expertfilter.OperatorType.*;
Expand All @@ -28,6 +30,14 @@ public void setUp() {
filterLoader = uuids -> null;
}

@Test
public void testStringValue() {
StringExpertRule rule = StringExpertRule.builder().operator(IN).field(FieldType.ID).value(null).values(new TreeSet<>(Set.of("A", "B"))).build();
assertEquals("A,B", rule.getStringValue());
rule = StringExpertRule.builder().operator(IS).field(FieldType.ID).value("C").values(null).build();
assertEquals("C", rule.getStringValue());
}

@ParameterizedTest
@MethodSource({
"provideArgumentsForTestWithException"
Expand Down

0 comments on commit 9ab498f

Please sign in to comment.