1   package org.orekit.files.ccsds.definitions;
2   
3   import org.hamcrest.MatcherAssert;
4   import org.hamcrest.Matchers;
5   import org.hipparchus.geometry.euclidean.threed.Rotation;
6   import org.junit.jupiter.api.Assertions;
7   import org.junit.jupiter.api.Test;
8   import org.orekit.OrekitMatchers;
9   import org.orekit.Utils;
10  import org.orekit.bodies.CelestialBody;
11  import org.orekit.bodies.CelestialBodyFactory;
12  import org.orekit.data.DataContext;
13  import org.orekit.errors.OrekitException;
14  import org.orekit.frames.Frame;
15  import org.orekit.frames.Frames;
16  import org.orekit.frames.Transform;
17  import org.orekit.time.AbsoluteDate;
18  import org.orekit.utils.PVCoordinates;
19  
20  /**
21   * Unit tests for {@link OrekitCcsdsFrameMapper}.
22   *
23   * @author Evan Ward
24   */
25  public class OrekitCcsdsFrameMapperTest {
26  
27      /** Data context for these tests. */
28      private final DataContext context = Utils.newDataContext("regular-data");
29  
30      /** Check that Earth-centered ICRF is GCRF, for #1914. */
31      @Test
32      public void testBuildCcsdsFrameEarthIcrfIsGcrf() {
33          // setup
34          CcsdsFrameMapper mapper = new OrekitCcsdsFrameMapper();
35          final Frames frames = context.getFrames();
36          final Frame icrf = frames.getICRF();
37          final Frame gcrf = frames.getGCRF();
38          final CelestialBody earth = context.getCelestialBodies().getEarth();
39  
40          // action
41          final Frame actual = mapper.buildCcsdsFrame(
42                  new BodyFacade("EARTH", earth),
43                  new FrameFacade(icrf, CelestialBodyFrame.ICRF, null, null, "ICRF"),
44                  null);
45  
46          // verify
47          MatcherAssert.assertThat(actual, Matchers.sameInstance(gcrf));
48      }
49  
50      /** Check building a translated ICRF frame. */
51      @Test
52      public void testBuildCcsdsFrameIcrfNotSsbOrEarth() {
53          // setup
54          CcsdsFrameMapper mapper = new OrekitCcsdsFrameMapper();
55          final Frames frames = context.getFrames();
56          final Frame icrf = frames.getICRF();
57          final CelestialBody emb = context.getCelestialBodies()
58                  .getEarthMoonBarycenter();
59          final AbsoluteDate date = context.getTimeScales().getJ2000Epoch();
60  
61          // action
62          final Frame actual = mapper.buildCcsdsFrame(
63                  new BodyFacade(CelestialBodyFactory.EARTH_MOON, emb),
64                  new FrameFacade(icrf, CelestialBodyFrame.ICRF, null, null, "ICRF"),
65                  null);
66  
67          // verify - EMB centered ICRF
68          Transform actualTransform = actual.getTransformTo(icrf, date);
69          MatcherAssert.assertThat(actualTransform.getRotation(),
70                  OrekitMatchers.distanceIs(Rotation.IDENTITY, Matchers.closeTo(0, 0.0)));
71          PVCoordinates actualPv = emb.getPVCoordinates(date, actual);
72          MatcherAssert.assertThat(actualPv,
73                  OrekitMatchers.pvCloseTo(PVCoordinates.ZERO, 0.0));
74      }
75  
76      /**
77       * Check
78       * {@link OrekitCcsdsFrameMapper#buildCcsdsFrame(FrameFacade,
79       * AbsoluteDate)}.
80       */
81      @Test
82      public void testBuildOrientationOnly() {
83          // setup
84          CcsdsFrameMapper mapper = new OrekitCcsdsFrameMapper();
85          Frame frame = context.getFrames().getVeis1950();
86          FrameFacade orientation =
87                  new FrameFacade(frame, null, null, null, null);
88  
89          // action
90          final Frame actual = mapper.buildCcsdsFrame(orientation, null);
91  
92          // verify
93          // just check it returns frame.
94          MatcherAssert.assertThat(actual, Matchers.sameInstance(frame));
95  
96          // check error cases
97          try {
98              mapper.buildCcsdsFrame(null, null);
99              Assertions.fail("Expected exception");
100         } catch (OrekitException e) {
101             // expected
102         }
103 
104         final String name = "Bill";
105         orientation = new FrameFacade(null, null, null, null, name);
106         try {
107             mapper.buildCcsdsFrame(orientation, null);
108             Assertions.fail("Expected exception");
109         } catch (OrekitException e) {
110             // expected
111             MatcherAssert.assertThat(e.getMessage(),
112                     Matchers.containsString(name));
113         }
114     }
115 
116     /** Exception (not NPE) for a null center. */
117     @Test
118     public void testBuildNullCenter() {
119         // setup
120         CcsdsFrameMapper mapper = new OrekitCcsdsFrameMapper();
121 
122         // action
123         try {
124             mapper.buildCcsdsFrame(null, null, null);
125             Assertions.fail("Expected exception");
126         } catch (OrekitException e) {
127             // expected
128         }
129     }
130 
131     /** All instances are equivalent. */
132     @Test
133     public void testInstancesAreEqual() {
134         // setup
135         CcsdsFrameMapper m1 = new OrekitCcsdsFrameMapper();
136         CcsdsFrameMapper m2 = new OrekitCcsdsFrameMapper();
137 
138         // verify
139         MatcherAssert.assertThat(m1.hashCode(), Matchers.is(m2.hashCode()));
140         MatcherAssert.assertThat(m1, Matchers.is(m2));
141         MatcherAssert.assertThat(m2, Matchers.is(m1));
142     }
143 
144     /** Assumes a subclass implements a different mapping. */
145     @Test
146     public void testSubclassesNotEqual() {
147         // setup
148         CcsdsFrameMapper m1 = new OrekitCcsdsFrameMapper();
149         CcsdsFrameMapper m2 = new OrekitCcsdsFrameMapper(){};
150 
151         // verify
152         MatcherAssert.assertThat(m1.hashCode(), Matchers.not(m2.hashCode()));
153         MatcherAssert.assertThat(m1, Matchers.not(m2));
154         MatcherAssert.assertThat(m2, Matchers.not(m1));
155     }
156 
157 }