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.handlers;
18  
19  import org.hipparchus.ode.events.Action;
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.Test;
22  import org.mockito.Mockito;
23  import org.orekit.propagation.SpacecraftState;
24  import org.orekit.propagation.events.EventDetectionSettings;
25  import org.orekit.propagation.events.EventDetector;
26  import org.orekit.time.AbsoluteDate;
27  
28  public class EventHandlerTest {
29  
30      @Test
31      public void testEnums() {
32          // this test is here only for test coverage ...
33  
34          Assertions.assertEquals(5, Action.values().length);
35          Assertions.assertSame(Action.STOP,              Action.valueOf("STOP"));
36          Assertions.assertSame(Action.RESET_STATE,       Action.valueOf("RESET_STATE"));
37          Assertions.assertSame(Action.RESET_DERIVATIVES, Action.valueOf("RESET_DERIVATIVES"));
38          Assertions.assertSame(Action.RESET_EVENTS,      Action.valueOf("RESET_EVENTS"));
39          Assertions.assertSame(Action.CONTINUE,          Action.valueOf("CONTINUE"));
40  
41      }
42  
43      @Test
44      public void testIssue721() {
45  
46          // Create detector
47          final Detector detector = new Detector();
48          Assertions.assertFalse(detector.isInitialized());
49  
50          // Create handler
51          final Handler handler = new Handler();
52          handler.init(null, null, detector);
53          Assertions.assertTrue(detector.isInitialized());
54  
55      }
56  
57      @Test
58      void testFinish() {
59          // GIVEN
60          final Handler handler = new Handler();
61          // WHEN & THEN
62          Assertions.assertDoesNotThrow(() -> handler.finish(Mockito.mock(SpacecraftState.class),
63                  Mockito.mock(EventDetector.class)));
64      }
65  
66      private static class Detector implements EventDetector {
67  
68          private boolean initialized;
69  
70          public Detector() {
71              this.initialized = false;
72          }
73  
74          public boolean isInitialized() {
75              return initialized;
76          }
77  
78          @Override
79          public void init(SpacecraftState s0, AbsoluteDate t) {
80              initialized = true;
81          }
82  
83          @Override
84          public double g(SpacecraftState s) {
85              return 0;
86          }
87  
88          @Override
89          public EventDetectionSettings getDetectionSettings() {
90              return new EventDetectionSettings(0, 0, 0);
91          }
92  
93          @Override
94          public EventHandler getHandler() {
95              return new Handler();
96          }
97      }
98  
99      private static class Handler implements EventHandler {
100 
101         @Override
102         public void init(SpacecraftState initialState, AbsoluteDate target, EventDetector detector) {
103             detector.init(initialState, target);
104         }
105 
106         @Override
107         public Action eventOccurred(SpacecraftState s, EventDetector detector, boolean increasing) {
108             return Action.CONTINUE;
109         }
110 
111     }
112 }
113