1   /* Copyright 2022-2025 Luc Maisonobe
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.analytical.gnss;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.analysis.differentiation.FieldGradient;
21  import org.hipparchus.util.Binary64;
22  import org.hipparchus.util.Binary64Field;
23  import org.junit.jupiter.api.Assertions;
24  import org.orekit.propagation.analytical.gnss.data.FieldGnssOrbitalElements;
25  import org.orekit.propagation.analytical.gnss.data.GNSSOrbitalElements;
26  import org.orekit.time.AbsoluteDate;
27  import org.orekit.time.FieldAbsoluteDate;
28  
29  import java.lang.reflect.InvocationTargetException;
30  import java.lang.reflect.Method;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  public class GnssTestUtils {
35  
36      @SuppressWarnings("unchecked")
37      public static <O extends GNSSOrbitalElements<O>>
38      void checkFieldConversion(final O message) {
39          try {
40              // looping over several types to check conversion functions
41              FieldGnssOrbitalElements<Binary64, ?> intermediate1 =
42                  message.toField(Binary64Field.getInstance());
43              FieldGnssOrbitalElements<? extends FieldGradient<?>, ?> intermediate2 =
44                  intermediate1.changeField(t -> FieldGradient.constant(6, t));
45              final O rebuilt = (O) intermediate2.toNonField();
46  
47              for (final Method getter : getGetters(message, Integer.TYPE)) {
48                  final Method fieldGetter = intermediate2.getClass().getMethod(getter.getName());
49                  Assertions.assertEquals(getter.invoke(message), fieldGetter.invoke(intermediate2));
50                  Assertions.assertEquals(getter.invoke(message), getter.invoke(rebuilt));
51              }
52              for (final Method getter : getGetters(message, Double.TYPE)) {
53                  final Method fieldGetter = intermediate2.getClass().getMethod(getter.getName());
54                  final double f = fieldGetter.getReturnType().equals(Double.TYPE) ?
55                                   (Double) fieldGetter.invoke(intermediate2) :
56                                   ((CalculusFieldElement<?>) fieldGetter.invoke(intermediate2)).getReal();
57                  Assertions.assertEquals((Double) getter.invoke(message), f, 1.0e-15);
58                  Assertions.assertEquals((Double) getter.invoke(message), (Double) getter.invoke(rebuilt), 1.0e-15, message.getClass().getName() + "." +getter.getName());
59              }
60              for (final Method getter : getGetters(message, AbsoluteDate.class)) {
61                  final Method               fieldGetter = intermediate2.getClass().getMethod(getter.getName());
62                  final AbsoluteDate         date        = (AbsoluteDate) getter.invoke(message);
63                  final FieldAbsoluteDate<?> fieldDate   = (FieldAbsoluteDate<?>) fieldGetter.invoke(intermediate2);
64                  final AbsoluteDate         rebuiltDate = (AbsoluteDate) getter.invoke(rebuilt);
65                  Assertions.assertEquals(0.0, date.durationFrom(fieldDate.toAbsoluteDate()), 1.0e-15);
66                  Assertions.assertEquals(0.0, date.durationFrom(rebuiltDate),                1.0e-15);
67              }
68          } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException nsme) {
69              Assertions.fail(nsme.getLocalizedMessage());
70          }
71      }
72  
73      private static List<Method> getGetters(final Object o, final Class<?> returnType) {
74          final List<Method> getters = new ArrayList<>();
75          for (Class<?> cls = o.getClass();
76               cls.getName().startsWith("org.orekit") ;
77               cls = cls.getSuperclass()) {
78              for (final Method method : cls.getDeclaredMethods()) {
79                  if (method.getName().startsWith("get") && returnType.equals(method.getReturnType())) {
80                      getters.add(method);
81                  }
82              }
83          }
84          return getters;
85      }
86  
87  }