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.complex.Complex;
20  import org.hipparchus.complex.ComplexField;
21  import org.hipparchus.ode.events.Action;
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.Test;
24  import org.mockito.Mockito;
25  import org.orekit.propagation.FieldSpacecraftState;
26  import org.orekit.propagation.events.FieldEventDetector;
27  import org.orekit.time.AbsoluteDate;
28  import org.orekit.time.FieldAbsoluteDate;
29  
30  class FieldRecallLastOccurrenceTest {
31  
32      private static final Action ACTION = Action.CONTINUE;
33  
34      @Test
35      void testEventOccurred() {
36          // GIVEN
37          final TestHandler testHandler = new TestHandler();
38          final FieldRecallLastOccurrence<Complex> recallLastOccurrence = new FieldRecallLastOccurrence<>(testHandler);
39          final FieldAbsoluteDate<Complex> expectedDate = FieldAbsoluteDate.getArbitraryEpoch(ComplexField.getInstance());
40          final FieldSpacecraftState<Complex> mockedState = mockState(expectedDate);
41          // WHEN
42          final Action action = recallLastOccurrence.eventOccurred(mockedState, null, true);
43          // THEN
44          Assertions.assertEquals(expectedDate, recallLastOccurrence.getLastOccurrence());
45          Assertions.assertEquals(ACTION, action);
46      }
47  
48      @Test
49      void testFinish() {
50          // GIVEN
51          final TestHandler testHandler = new TestHandler();
52          final FieldRecallLastOccurrence<Complex> recallLastOccurrence = new FieldRecallLastOccurrence<>(testHandler);
53          final FieldAbsoluteDate<Complex> expectedDate = FieldAbsoluteDate.getArbitraryEpoch(ComplexField.getInstance());
54          final FieldSpacecraftState<Complex> mockedState = mockState(expectedDate);
55          // WHEN
56          recallLastOccurrence.finish(mockedState, null);
57          // THEN
58          Assertions.assertTrue(testHandler.isFinished);
59      }
60  
61      @Test
62      void testResetState() {
63          // GIVEN
64          final TestHandler testHandler = new TestHandler();
65          final FieldRecallLastOccurrence<Complex> recallLastOccurrence = new FieldRecallLastOccurrence<>(testHandler);
66          final FieldSpacecraftState<Complex> mockedState = mockState(FieldAbsoluteDate.getArbitraryEpoch(ComplexField.getInstance()));
67          // WHEN
68          final FieldSpacecraftState<Complex> actualState = recallLastOccurrence.resetState(null, mockedState);
69          // THEN
70          Assertions.assertEquals(mockedState, actualState);
71          Assertions.assertNull(recallLastOccurrence.getLastOccurrence());
72      }
73  
74      @Test
75      void testInit() {
76          // GIVEN
77          final TestHandler testHandler = new TestHandler();
78          final FieldRecallLastOccurrence<Complex> recallLastOccurrence = new FieldRecallLastOccurrence<>(testHandler);
79          final FieldSpacecraftState<Complex> mockedState = mockState(new FieldAbsoluteDate<>(ComplexField.getInstance(),
80              AbsoluteDate.FUTURE_INFINITY));
81          // WHEN
82          recallLastOccurrence.init(mockedState, FieldAbsoluteDate.getArbitraryEpoch(ComplexField.getInstance()), null);
83          // THEN
84          Assertions.assertTrue(testHandler.isInitialized);
85      }
86  
87      @SuppressWarnings("unchecked")
88      private FieldSpacecraftState<Complex> mockState(final FieldAbsoluteDate<Complex> date) {
89          final FieldSpacecraftState<Complex> mockedState = Mockito.mock(FieldSpacecraftState.class);
90          Mockito.when(mockedState.getDate()).thenReturn(date);
91          return mockedState;
92      }
93  
94      private static class TestHandler implements FieldEventHandler<Complex> {
95  
96          boolean isInitialized = false;
97          boolean isFinished = false;
98  
99          @Override
100         public void init(FieldSpacecraftState<Complex> initialState, FieldAbsoluteDate<Complex> target, FieldEventDetector<Complex> detector) {
101             isInitialized = true;
102         }
103 
104         @Override
105         public Action eventOccurred(FieldSpacecraftState<Complex> s, FieldEventDetector<Complex> detector, boolean increasing) {
106             return ACTION;
107         }
108 
109         @Override
110         public void finish(FieldSpacecraftState<Complex> finalState, FieldEventDetector<Complex> detector) {
111             isFinished = true;
112         }
113     }
114 
115 }