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.api.Test;
22  import org.mockito.Mockito;
23  import org.orekit.propagation.SpacecraftState;
24  import org.orekit.propagation.events.EventDetector;
25  import org.orekit.time.AbsoluteDate;
26  
27  class RecallLastOccurrenceTest {
28  
29      private static final Action ACTION = Action.CONTINUE;
30  
31      @Test
32      void testFinish() {
33          // GIVEN
34          final EventHandler mockedHandler = Mockito.mock(EventHandler.class);
35          final RecallLastOccurrence wrappingHandler = new RecallLastOccurrence(mockedHandler);
36          final SpacecraftState mockedState = Mockito.mock(SpacecraftState.class);
37          // WHEN
38          wrappingHandler.finish(mockedState, null);
39          // THEN
40          Mockito.verify(mockedHandler, Mockito.times(1)).finish(mockedState, null);
41      }
42  
43      @Test
44      void testEventOccurred() {
45          // GIVEN
46          final TestHandler testHandler = new TestHandler();
47          final RecallLastOccurrence recallLastOccurrence = new RecallLastOccurrence(testHandler);
48          final AbsoluteDate expectedDate = AbsoluteDate.J2000_EPOCH;
49          final SpacecraftState mockedState = mockState(expectedDate);
50          // WHEN
51          final Action action = recallLastOccurrence.eventOccurred(mockedState, null, true);
52          // THEN
53          Assertions.assertEquals(expectedDate, recallLastOccurrence.getLastOccurrence());
54          Assertions.assertEquals(ACTION, action);
55      }
56  
57      @Test
58      void testResetState() {
59          // GIVEN
60          final TestHandler testHandler = new TestHandler();
61          final RecallLastOccurrence recallLastOccurrence = new RecallLastOccurrence(testHandler);
62          final SpacecraftState mockedState = mockState(AbsoluteDate.ARBITRARY_EPOCH);
63          // WHEN
64          final SpacecraftState actualState = recallLastOccurrence.resetState(null, mockedState);
65          // THEN
66          Assertions.assertEquals(mockedState, actualState);
67          Assertions.assertNull(recallLastOccurrence.getLastOccurrence());
68      }
69  
70      @Test
71      void testInitBackward() {
72          // GIVEN
73          final TestHandler testHandler = new TestHandler();
74          final RecallLastOccurrence recallLastOccurrence = new RecallLastOccurrence(testHandler);
75          final SpacecraftState mockedState = mockState(AbsoluteDate.FUTURE_INFINITY);
76          // WHEN
77          recallLastOccurrence.init(mockedState, AbsoluteDate.ARBITRARY_EPOCH, null);
78          // THEN
79          Assertions.assertTrue(testHandler.isInitialized);
80          Assertions.assertEquals(AbsoluteDate.FUTURE_INFINITY, recallLastOccurrence.getLastOccurrence());
81      }
82  
83      @Test
84      void testInitForward() {
85          // GIVEN
86          final TestHandler testHandler = new TestHandler();
87          final RecallLastOccurrence recallLastOccurrence = new RecallLastOccurrence(testHandler);
88          final SpacecraftState mockedState = mockState(AbsoluteDate.PAST_INFINITY);
89          // WHEN
90          recallLastOccurrence.init(mockedState, AbsoluteDate.ARBITRARY_EPOCH, null);
91          // THEN
92          Assertions.assertTrue(testHandler.isInitialized);
93          Assertions.assertEquals(AbsoluteDate.PAST_INFINITY, recallLastOccurrence.getLastOccurrence());
94      }
95  
96      private SpacecraftState mockState(final AbsoluteDate date) {
97          final SpacecraftState mockedState = Mockito.mock(SpacecraftState.class);
98          Mockito.when(mockedState.getDate()).thenReturn(date);
99          return mockedState;
100     }
101 
102     private static class TestHandler implements EventHandler {
103 
104         boolean isInitialized = false;
105 
106         boolean isFinished = false;
107 
108         @Override
109         public void init(SpacecraftState initialState, AbsoluteDate target, EventDetector detector) {
110             isInitialized = true;
111         }
112 
113         @Override
114         public Action eventOccurred(SpacecraftState s, EventDetector detector, boolean increasing) {
115             return ACTION;
116         }
117 
118         @Override
119         public void finish(SpacecraftState finalState, EventDetector detector) {
120             isFinished = true;
121         }
122     }
123 
124 }