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