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.sampling;
18  
19  import org.junit.jupiter.api.Test;
20  import org.junit.jupiter.params.ParameterizedTest;
21  import org.junit.jupiter.params.provider.ValueSource;
22  import org.mockito.Mockito;
23  import org.orekit.propagation.SpacecraftState;
24  import org.orekit.time.AbsoluteDate;
25  
26  import java.util.List;
27  
28  import static org.junit.jupiter.api.Assertions.*;
29  
30  class PropagationStepRecorderTest {
31  
32      @ParameterizedTest
33      @ValueSource(booleans = {true, false})
34      void testSetter(final boolean resetAutomatically) {
35          // GIVEN
36          final PropagationStepRecorder recorder = new PropagationStepRecorder();
37          // WHEN
38          recorder.setResetAutomatically(resetAutomatically);
39          // THEN
40          assertEquals(resetAutomatically, recorder.isResetAutomatically());
41      }
42  
43      @ParameterizedTest
44      @ValueSource(booleans = {true, false})
45      void testGetter(final boolean resetAutomatically) {
46          // GIVEN
47  
48          // WHEN
49          final PropagationStepRecorder recorder = new PropagationStepRecorder(resetAutomatically);
50          // THEN
51          assertEquals(resetAutomatically, recorder.isResetAutomatically());
52      }
53  
54      @Test
55      void copyStatesAtConstructionTest() {
56          // GIVEN
57          final PropagationStepRecorder recorder = new PropagationStepRecorder();
58          // WHEN
59          final List<SpacecraftState> states = recorder.copyStates();
60          // THEN
61          assertEquals(0, states.size());
62      }
63  
64      @Test
65      void copyStatesTest() {
66          // GIVEN
67          final PropagationStepRecorder recorder = new PropagationStepRecorder();
68          recorder.handleStep(mockInterpolator());
69          // WHEN
70          recorder.init(mockState(), AbsoluteDate.ARBITRARY_EPOCH);
71          // THEN
72          final List<SpacecraftState> states = recorder.copyStates();
73          assertEquals(0, states.size());
74      }
75  
76      @ParameterizedTest
77      @ValueSource(booleans = {true, false})
78      void handleStepTest(final boolean resetAutomatically) {
79          // GIVEN
80          final PropagationStepRecorder recorder = new PropagationStepRecorder();
81          final OrekitStepInterpolator mockedInterpolator = mockInterpolator();
82          final int expectedSize = 10;
83          // WHEN
84          for (int i = 0; i < expectedSize; ++i) {
85              recorder.handleStep(mockedInterpolator);
86          }
87          // WHEN
88          final List<SpacecraftState> states = recorder.copyStates();
89          assertEquals(expectedSize, states.size());
90          recorder.setResetAutomatically(resetAutomatically);
91          recorder.init(mockState(), AbsoluteDate.ARBITRARY_EPOCH);
92          if (resetAutomatically) {
93              assertTrue(recorder.copyStates().isEmpty());
94          }
95      }
96  
97      private static OrekitStepInterpolator mockInterpolator() {
98          final SpacecraftState state = mockState();
99          final OrekitStepInterpolator mockedInterpolator = Mockito.mock();
100         Mockito.when(mockedInterpolator.getCurrentState()).thenReturn(state);
101         return mockedInterpolator;
102     }
103 
104     private static SpacecraftState mockState() {
105         return Mockito.mock(SpacecraftState.class);
106     }
107 }