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;
18  
19  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.Test;
21  import org.orekit.propagation.SpacecraftState;
22  import org.orekit.propagation.events.handlers.EventHandler;
23  import org.orekit.propagation.events.intervals.AdaptableInterval;
24  import org.orekit.time.AbsoluteDate;
25  import org.orekit.time.TimeInterval;
26  
27  import static org.junit.jupiter.api.Assertions.assertTrue;
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  import static org.mockito.Mockito.mock;
30  import static org.mockito.Mockito.when;
31  
32  class TimeIntervalDetectorTest {
33  
34      @Test
35      void testGetter() {
36          // GIVEN
37          final AbsoluteDate startDate = AbsoluteDate.ARBITRARY_EPOCH;
38          final TimeInterval interval = TimeInterval.of(startDate, startDate.shiftedBy(1.));
39          final TimeIntervalDetector detector = new TimeIntervalDetector(mock(EventHandler.class), interval);
40          // WHEN
41          final TimeInterval actualInterval = detector.getTimeInterval();
42          // THEN
43          assertEquals(interval, actualInterval);
44      }
45  
46      @Test
47      void testDependsOnlyOnTime() {
48          // GIVEN
49          final AbsoluteDate startDate = AbsoluteDate.ARBITRARY_EPOCH;
50          final TimeInterval interval = TimeInterval.of(startDate, startDate.shiftedBy(1.));
51          final TimeIntervalDetector detector = new TimeIntervalDetector(mock(EventHandler.class), interval);
52          // WHEN
53          final boolean value = detector.dependsOnTimeOnly();
54          // THEN
55          Assertions.assertTrue(value);
56      }
57  
58      @Test
59      void testGValue() {
60          // GIVEN
61          final AbsoluteDate startDate = AbsoluteDate.ARBITRARY_EPOCH;
62          final TimeInterval interval = TimeInterval.of(startDate, startDate.shiftedBy(1.));
63          final TimeIntervalDetector detector = new TimeIntervalDetector(mock(EventHandler.class), interval);
64          // WHEN & THEN
65          final double expectedG = 0.;
66          assertEquals(expectedG, detector.g(mockState(interval.getStartDate())));
67          assertEquals(expectedG, detector.g(mockState(interval.getEndDate())));
68      }
69  
70      @Test
71      void testGSign() {
72          // GIVEN
73          final AbsoluteDate startDate = AbsoluteDate.ARBITRARY_EPOCH;
74          final double dt = 1;
75          final TimeInterval interval = TimeInterval.of(startDate, startDate.shiftedBy(dt));
76          final TimeIntervalDetector detector = new TimeIntervalDetector(mock(EventHandler.class), interval);
77          // WHEN & THEN
78          assertTrue(detector.g(mockState(interval.getStartDate().shiftedBy(-dt))) < 0.);
79          assertTrue(detector.g(mockState(interval.getStartDate().shiftedBy(dt / 2))) > 0.);
80          assertTrue(detector.g(mockState(interval.getEndDate().shiftedBy(dt))) < 0.);
81      }
82  
83      @Test
84      void testCreate() {
85          // GIVEN
86          final AbsoluteDate startDate = AbsoluteDate.ARBITRARY_EPOCH;
87          final TimeInterval interval = TimeInterval.of(startDate, startDate.shiftedBy(1));
88          final TimeIntervalDetector detector = new TimeIntervalDetector(mock(EventHandler.class), interval);
89          final EventDetectionSettings detectionSettings = new EventDetectionSettings(mock(AdaptableInterval.class),
90                  1., 1);
91          final EventHandler mockedHandler = mock();
92          // WHEN
93          final TimeIntervalDetector createdDetector = detector.create(detectionSettings, mockedHandler);
94          // THEN
95          assertEquals(detectionSettings, createdDetector.getDetectionSettings());
96          assertEquals(mockedHandler, createdDetector.getHandler());
97      }
98  
99      private static SpacecraftState mockState(final AbsoluteDate date) {
100         final SpacecraftState state = mock();
101         when(state.getDate()).thenReturn(date);
102         return state;
103     }
104 }