|
| 1 | +package org.gridsuite.network.map.utils; |
| 2 | + |
| 3 | +import com.powsybl.iidm.network.CurrentLimits; |
| 4 | +import com.powsybl.iidm.network.LoadingLimits.TemporaryLimit; |
| 5 | +import com.powsybl.iidm.network.OperationalLimitsGroup; |
| 6 | +import org.assertj.core.api.SoftAssertions; |
| 7 | +import org.assertj.core.api.WithAssertions; |
| 8 | +import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension; |
| 9 | +import org.gridsuite.network.map.dto.common.CurrentLimitsData; |
| 10 | +import org.gridsuite.network.map.dto.common.CurrentLimitsData.Applicability; |
| 11 | +import org.gridsuite.network.map.dto.common.TemporaryLimitData; |
| 12 | +import org.gridsuite.network.map.dto.utils.ElementUtils; |
| 13 | +import org.junit.jupiter.api.Nested; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 16 | +import org.junit.jupiter.params.ParameterizedTest; |
| 17 | +import org.junit.jupiter.params.provider.Arguments; |
| 18 | +import org.junit.jupiter.params.provider.MethodSource; |
| 19 | +import org.mockito.Mockito; |
| 20 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 21 | + |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +@ExtendWith({ SoftAssertionsExtension.class, MockitoExtension.class }) |
| 28 | +class ElementUtilsTest implements WithAssertions { |
| 29 | + @Nested |
| 30 | + class MergeCurrentLimitsTest { |
| 31 | + private static TemporaryLimit mockTemporaryLimits(final int acceptableDuration, final String name, final double value) { |
| 32 | + TemporaryLimit mock = Mockito.mock(TemporaryLimit.class); |
| 33 | + Mockito.when(mock.getAcceptableDuration()).thenReturn(acceptableDuration); |
| 34 | + Mockito.when(mock.getName()).thenReturn(name); |
| 35 | + Mockito.when(mock.getValue()).thenReturn(value); |
| 36 | + return mock; |
| 37 | + } |
| 38 | + |
| 39 | + private static OperationalLimitsGroup mockOperationalLimitsGroup(final String id, double permanentLimit, List<TemporaryLimit> temporaryLimits) { |
| 40 | + CurrentLimits mock = Mockito.mock(CurrentLimits.class); |
| 41 | + //Mockito.when(mock.getLimitType()).thenCallRealMethod(); |
| 42 | + Mockito.when(mock.getPermanentLimit()).thenReturn(permanentLimit); |
| 43 | + Mockito.when(mock.getTemporaryLimits()).thenReturn(temporaryLimits); |
| 44 | + return mockOperationalLimitsGroup(id, mock); |
| 45 | + } |
| 46 | + |
| 47 | + private static OperationalLimitsGroup mockOperationalLimitsGroup(final String id, final CurrentLimits cl) { |
| 48 | + OperationalLimitsGroup mock = Mockito.mock(OperationalLimitsGroup.class); |
| 49 | + Mockito.when(mock.getId()).thenReturn(id); |
| 50 | + Mockito.when(mock.getCurrentLimits()).thenReturn(Optional.of(cl)); |
| 51 | + return mock; |
| 52 | + } |
| 53 | + |
| 54 | + @ParameterizedTest(name = ParameterizedTest.INDEX_PLACEHOLDER) |
| 55 | + @MethodSource("mergeCurrentLimitsTestData") |
| 56 | + void shouldNotThrow( |
| 57 | + final Collection<OperationalLimitsGroup> olg1, |
| 58 | + final Collection<OperationalLimitsGroup> olg2, |
| 59 | + final List<CurrentLimitsData> expected) { |
| 60 | + assertThat(ElementUtils.mergeCurrentLimits(olg1, olg2)).as("Result").isEqualTo(expected); |
| 61 | + } |
| 62 | + |
| 63 | + private static Stream<Arguments> mergeCurrentLimitsTestData() { |
| 64 | + return Stream.of( |
| 65 | + Arguments.of(List.of( |
| 66 | + mockOperationalLimitsGroup("group1", 220.0, List.of(mockTemporaryLimits(100, "temporary1", 50.0), mockTemporaryLimits(150, "temporary2", 70.0))) |
| 67 | + ), List.of( |
| 68 | + mockOperationalLimitsGroup("group1", 220.0, List.of(mockTemporaryLimits(100, "temporary1", 50.0), mockTemporaryLimits(150, "temporary2", 70.0))) |
| 69 | + ), List.of( |
| 70 | + CurrentLimitsData.builder().id("group1").permanentLimit(220.0).temporaryLimits(List.of( |
| 71 | + TemporaryLimitData.builder().acceptableDuration(100).name("temporary1").value(50.0).build(), |
| 72 | + TemporaryLimitData.builder().acceptableDuration(150).name("temporary2").value(70.0).build() |
| 73 | + )).applicability(Applicability.EQUIPMENT).build() |
| 74 | + )), |
| 75 | + Arguments.of(List.of(), List.of( |
| 76 | + mockOperationalLimitsGroup("group1", 220.0, List.of(mockTemporaryLimits(100, "temporary1", 50.0), mockTemporaryLimits(150, "temporary2", 70.0))) |
| 77 | + ), List.of( |
| 78 | + CurrentLimitsData.builder().id("group1").permanentLimit(220.0).temporaryLimits(List.of( |
| 79 | + TemporaryLimitData.builder().acceptableDuration(100).name("temporary1").value(50.0).build(), |
| 80 | + TemporaryLimitData.builder().acceptableDuration(150).name("temporary2").value(70.0).build() |
| 81 | + )).applicability(Applicability.SIDE2).build() |
| 82 | + )), |
| 83 | + Arguments.of(List.of( |
| 84 | + mockOperationalLimitsGroup("group1", 220.0, List.of(mockTemporaryLimits(100, "temporary1", 50.0), mockTemporaryLimits(150, "temporary2", 70.0))) |
| 85 | + ), List.of(), List.of( |
| 86 | + CurrentLimitsData.builder().id("group1").permanentLimit(220.0).temporaryLimits(List.of( |
| 87 | + TemporaryLimitData.builder().acceptableDuration(100).name("temporary1").value(50.0).build(), |
| 88 | + TemporaryLimitData.builder().acceptableDuration(150).name("temporary2").value(70.0).build() |
| 89 | + )).applicability(Applicability.SIDE1).build() |
| 90 | + )), |
| 91 | + Arguments.of(List.of( |
| 92 | + mockOperationalLimitsGroup("group1", Double.NaN, List.of()) |
| 93 | + ), List.of( |
| 94 | + mockOperationalLimitsGroup("group1", 220.0, List.of(mockTemporaryLimits(100, "temporary1", 50.0), mockTemporaryLimits(150, "temporary2", 70.0))) |
| 95 | + ), List.of( |
| 96 | + CurrentLimitsData.builder().id("group1").permanentLimit(220.0).temporaryLimits(List.of( |
| 97 | + TemporaryLimitData.builder().acceptableDuration(100).name("temporary1").value(50.0).build(), |
| 98 | + TemporaryLimitData.builder().acceptableDuration(150).name("temporary2").value(70.0).build() |
| 99 | + )).applicability(Applicability.SIDE2).build() |
| 100 | + )), |
| 101 | + Arguments.of(List.of( |
| 102 | + mockOperationalLimitsGroup("group1", 220.0, List.of(mockTemporaryLimits(100, "temporary1", 50.0), mockTemporaryLimits(150, "temporary2", 70.0))) |
| 103 | + ), List.of( |
| 104 | + mockOperationalLimitsGroup("group1", Double.NaN, List.of()) |
| 105 | + ), List.of( |
| 106 | + CurrentLimitsData.builder().id("group1").permanentLimit(220.0).temporaryLimits(List.of( |
| 107 | + TemporaryLimitData.builder().acceptableDuration(100).name("temporary1").value(50.0).build(), |
| 108 | + TemporaryLimitData.builder().acceptableDuration(150).name("temporary2").value(70.0).build() |
| 109 | + )).applicability(Applicability.SIDE1).build() |
| 110 | + )) |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void shouldThrowOnDuplicateIds(final SoftAssertions softly) { |
| 116 | + final var l1 = List.of(mockOperationalLimitsGroup("group1", 220.0, List.of(mockTemporaryLimits(100, "temporary1", 50.0), mockTemporaryLimits(150, "temporary2", 70.0)))); |
| 117 | + final var l2 = List.of( |
| 118 | + mockOperationalLimitsGroup("group1", 220.0, List.of(mockTemporaryLimits(100, "temporary1", 50.0), mockTemporaryLimits(150, "temporary2", 70.0))), |
| 119 | + mockOperationalLimitsGroup("group1", 220.0, List.of(mockTemporaryLimits(100, "temporary1", 50.0), mockTemporaryLimits(150, "temporary2", 70.0))) |
| 120 | + ); |
| 121 | + softly.assertThatThrownBy(() -> ElementUtils.mergeCurrentLimits(l1, l2)).isInstanceOf(UnsupportedOperationException.class).hasMessage("IDs are assumed unique in each lists"); |
| 122 | + softly.assertThatThrownBy(() -> ElementUtils.mergeCurrentLimits(l2, l1)).isInstanceOf(UnsupportedOperationException.class).hasMessage("IDs are assumed unique in each lists"); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments