1   /* Copyright 2002-2025 CS GROUP
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.hamcrest.CoreMatchers;
20  import org.hamcrest.MatcherAssert;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.Test;
23  import org.mockito.Mockito;
24  import org.orekit.propagation.SpacecraftState;
25  import org.orekit.time.AbsoluteDate;
26  
27  /**
28   * Unit tests for {@link NegateDetector}.
29   *
30   * @author Evan Ward
31   */
32  class NegateDetectorTest {
33  
34      @Test
35      void testInitBackward() {
36          //setup
37          final DateDetector dateDetector = new DateDetector();
38          final NegateDetector negateDetector = new NegateDetector(dateDetector);
39          final SpacecraftState state = Mockito.mock(SpacecraftState.class);
40          Mockito.when(state.getDate()).thenReturn(AbsoluteDate.ARBITRARY_EPOCH);
41  
42          //action
43          negateDetector.init(state, AbsoluteDate.PAST_INFINITY);
44  
45          //verify
46          Assertions.assertEquals(dateDetector.isForward(), negateDetector.isForward());
47      }
48  
49      @Test
50      void testInitForward() {
51          //setup
52          final DateDetector dateDetector = new DateDetector();
53          final NegateDetector negateDetector = new NegateDetector(dateDetector);
54          final SpacecraftState state = Mockito.mock(SpacecraftState.class);
55          Mockito.when(state.getDate()).thenReturn(AbsoluteDate.ARBITRARY_EPOCH);
56  
57          //action
58          negateDetector.init(state, AbsoluteDate.FUTURE_INFINITY);
59  
60          //verify
61          Assertions.assertEquals(dateDetector.isForward(), negateDetector.isForward());
62      }
63  
64      @Test
65      void testGetDetector() {
66          //setup
67          EventDetector expectedDetector = new DateDetector();
68  
69          //action
70          NegateDetector detector = new NegateDetector(expectedDetector);
71  
72          //verify
73          Assertions.assertEquals(expectedDetector, detector.getDetector());
74          Assertions.assertEquals(expectedDetector, detector.getOriginal());
75      }
76  
77      /**
78       * check g function is negated.
79       */
80      @Test
81      void testG() {
82          //setup
83          EventDetector a = Mockito.mock(EventDetector.class);
84          Mockito.when(a.getDetectionSettings()).thenReturn(EventDetectionSettings.getDefaultEventDetectionSettings());
85          NegateDetector detector = new NegateDetector(a);
86          SpacecraftState s = Mockito.mock(SpacecraftState.class);
87  
88          // verify + to -
89          Mockito.when(a.g(s)).thenReturn(1.0);
90          MatcherAssert.assertThat(detector.g(s), CoreMatchers.is(-1.0));
91          // verify - to +
92          Mockito.when(a.g(s)).thenReturn(-1.0);
93          MatcherAssert.assertThat(detector.g(s), CoreMatchers.is(1.0));
94      }
95  
96      /** Check a with___ method. */
97      @Test
98      void testCreate() {
99          //setup
100         EventDetector a = Mockito.mock(EventDetector.class);
101         Mockito.when(a.getDetectionSettings()).thenReturn(EventDetectionSettings.getDefaultEventDetectionSettings());
102         NegateDetector detector = new NegateDetector(a);
103 
104         // action
105         NegateDetector actual = detector.withMaxCheck(100);
106 
107         //verify
108         MatcherAssert.assertThat(actual.getMaxCheckInterval().currentInterval(null, true), CoreMatchers.is(100.0));
109         Assertions.assertSame(actual.getOriginal(), a);
110     }
111 }