1   /* Copyright 2022-2025 Romain Serra
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.orekit.propagation.events.handlers;
18  
19  import org.hipparchus.ode.events.Action;
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.params.ParameterizedTest;
22  import org.junit.jupiter.params.provider.EnumSource;
23  import org.junit.jupiter.params.provider.ValueSource;
24  import org.mockito.Mockito;
25  import org.orekit.propagation.SpacecraftState;
26  import org.orekit.propagation.events.EventDetector;
27  
28  class CountingHandlerTest {
29  
30      @ParameterizedTest
31      @ValueSource(booleans = {false, true})
32      void testEventOccurred(final boolean countAll) {
33          // GIVEN
34          final CountingHandler handler = new TestHandler(countAll, Action.CONTINUE);
35          final SpacecraftState mockedState = Mockito.mock(SpacecraftState.class);
36          final EventDetector mockedDetector = Mockito.mock(EventDetector.class);
37          final int calls = 42;
38          // WHEN
39          for (int i = 0; i < calls; i++) {
40              handler.eventOccurred(mockedState, mockedDetector, true);
41          }
42          // THEN
43          Assertions.assertEquals(countAll ? calls : 0, handler.getCount());
44      }
45  
46      @ParameterizedTest
47      @EnumSource(Action.class)
48      void testEventOccurred(final Action expectedAction) {
49          // GIVEN
50          final CountingHandler handler = new TestHandler(true, expectedAction);
51          final SpacecraftState mockedState = Mockito.mock(SpacecraftState.class);
52          final EventDetector mockedDetector = Mockito.mock(EventDetector.class);
53          // WHEN
54          final Action actualAction = handler.eventOccurred(mockedState, mockedDetector, true);
55          // THEN
56          Assertions.assertEquals(expectedAction, actualAction);
57      }
58  
59      private static class TestHandler extends CountingHandler {
60  
61          private final boolean countAll;
62  
63          TestHandler(final boolean countAll, final Action action) {
64              super(0, action);
65              this.countAll = countAll;
66          }
67  
68          @Override
69          protected boolean doesCount(SpacecraftState state, EventDetector detector, boolean increasing) {
70              return countAll;
71          }
72      }
73  
74  }