1   /* Copyright 2002-2022 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 java.util.Collections;
20  import java.util.NoSuchElementException;
21  
22  import org.hipparchus.ode.events.Action;
23  import org.junit.Assert;
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.mockito.Mockito;
27  import org.orekit.propagation.SpacecraftState;
28  import org.orekit.propagation.events.handlers.EventHandler;
29  import org.orekit.time.AbsoluteDate;
30  
31  /**
32   * Unit tests for {@link BooleanDetector}.
33   *
34   * @author Evan Ward
35   */
36  public class AndDetectorTest {
37  
38      /** first operand. */
39      private MockDetector a;
40      /** second operand. */
41      private MockDetector b;
42      /** null */
43      private SpacecraftState s;
44      /** subject under test */
45      private BooleanDetector and;
46  
47      /** create subject under test and dependencies. */
48      @Before
49      public void setUp() {
50          a = new MockDetector();
51          b = new MockDetector();
52          s = null;
53          and = BooleanDetector.andCombine(a, b);
54      }
55  
56      /**
57       * check {@link BooleanDetector#g(SpacecraftState)}.
58       */
59      @Test
60      public void testG() {
61          // test both zero
62          a.g = b.g = 0.0;
63          Assert.assertEquals(0.0, and.g(s), 0);
64  
65          // test either zero
66          a.g = 1;
67          b.g = 0;
68          Assert.assertEquals(0.0, and.g(s), 0);
69          a.g = 0;
70          b.g = 1;
71          Assert.assertEquals(0.0, and.g(s), 0);
72  
73          // test either negative
74          a.g = 0;
75          b.g = -1;
76          Assert.assertTrue("negative", and.g(s) < 0);
77          a.g = 1;
78          b.g = -1;
79          Assert.assertTrue("negative", and.g(s) < 0);
80          a.g = -1;
81          b.g = 0;
82          Assert.assertTrue("negative", and.g(s) < 0);
83          a.g = -1;
84          b.g = 1;
85          Assert.assertTrue("negative", and.g(s) < 0);
86          a.g = -1;
87          b.g = -1;
88          Assert.assertTrue("negative", and.g(s) < 0);
89  
90          // test both positive
91          a.g = 1;
92          b.g = 1;
93          Assert.assertTrue("positive", and.g(s) > 0);
94  
95      }
96  
97      /**
98       * check {@link BooleanDetector} for cancellation.
99       */
100     @Test
101     public void testCancellation() {
102         a.g = -1e-10;
103         b.g = 1e10;
104         Assert.assertTrue("negative", and.g(s) < 0);
105         a.g = 1e10;
106         b.g = -1e-10;
107         Assert.assertTrue("negative", and.g(s) < 0);
108         a.g = 1e10;
109         b.g = 1e-10;
110         Assert.assertTrue("positive", and.g(s) > 0);
111         a.g = 1e-10;
112         b.g = 1e10;
113         Assert.assertTrue("positive", and.g(s) > 0);
114     }
115 
116     /**
117      * Check wrapped detectors are initialized.
118      */
119     @Test
120     public void testInit() {
121         // setup
122         EventDetector a = Mockito.mock(EventDetector.class);
123         EventDetector b = Mockito.mock(EventDetector.class);
124         @SuppressWarnings("unchecked")
125         EventHandler<EventDetector> c = Mockito.mock(EventHandler.class);
126         BooleanDetector and = BooleanDetector.andCombine(a, b).withHandler(c);
127         AbsoluteDate t = AbsoluteDate.CCSDS_EPOCH;
128         s = Mockito.mock(SpacecraftState.class);
129         Mockito.when(s.getDate()).thenReturn(t.shiftedBy(60.0));
130 
131         // action
132         and.init(s, t);
133 
134         // verify
135         Assert.assertEquals(2, and.getDetectors().size());
136         Mockito.verify(a).init(s, t);
137         Mockito.verify(b).init(s, t);
138         Mockito.verify(c).init(s, t, and);
139     }
140 
141     /** check when no operands are passed to the constructor. */
142     @Test
143     public void testZeroDetectors() {
144         // action
145         try {
146             BooleanDetector.andCombine(Collections.emptyList());
147             Assert.fail("Expected Exception");
148         } catch (NoSuchElementException e) {
149             // expected
150         }
151     }
152 
153     /** Mock detector to set the g function to arbitrary values. */
154     private static class MockDetector implements EventDetector {
155 
156         /** value to return from {@link #g(SpacecraftState)}. */
157         public double g = 0;
158 
159         @Override
160         public void init(SpacecraftState s0, AbsoluteDate t) {
161 
162         }
163 
164         @Override
165         public double g(SpacecraftState s) {
166             return this.g;
167         }
168 
169         @Override
170         public double getThreshold() {
171             return 0;
172         }
173 
174         @Override
175         public double getMaxCheckInterval() {
176             return 0;
177         }
178 
179         @Override
180         public int getMaxIterationCount() {
181             return 0;
182         }
183 
184         @Override
185         public Action eventOccurred(SpacecraftState s, boolean increasing) {
186             return null;
187         }
188 
189         @Override
190         public SpacecraftState resetState(SpacecraftState oldState) {
191             return null;
192         }
193     }
194 }