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.files.ccsds.ndm.cdm;
18  
19  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.BeforeEach;
21  import org.junit.jupiter.api.Test;
22  import org.orekit.Utils;
23  import org.orekit.annotation.DefaultDataContext;
24  import org.orekit.bodies.CelestialBodyFactory;
25  import org.orekit.errors.OrekitException;
26  import org.orekit.errors.OrekitMessages;
27  import org.orekit.files.ccsds.definitions.BodyFacade;
28  import org.orekit.files.ccsds.definitions.CenterName;
29  import org.orekit.files.ccsds.definitions.FrameFacade;
30  import org.orekit.frames.Frame;
31  import org.orekit.frames.FramesFactory;
32  import org.orekit.utils.IERSConventions;
33  
34  /** Tests for CdmMetaData class.
35   * These tests are used to increase condition coverage, other global tests are in CdmParser/WriterTest classes.
36   */
37  public class CdmMetaDataTest {
38  
39      @BeforeEach
40      public void setUp() {
41          Utils.setDataRoot("regular-data");
42      }
43      
44      /** Condition coverage on the getFrame method. */
45      @Test
46      @DefaultDataContext
47      public void testGetFrame() {
48          
49          final CdmMetadata meta = new CdmMetadata();
50          
51          // refFrame == null
52          try {
53              meta.getFrame();
54              Assertions.fail("an exception should have been thrown");
55          } catch (OrekitException oe) {
56              Assertions.assertEquals(OrekitMessages.CCSDS_INVALID_FRAME, oe.getSpecifier());
57              Assertions.assertEquals("No reference frame", oe.getParts()[0]);
58          }
59          
60          // orbitCenter.getBody() == null
61          try {
62              meta.setOrbitCenter(new BodyFacade("dummy center", null));
63              meta.getFrame();
64              Assertions.fail("an exception should have been thrown");
65          } catch (OrekitException oe) {
66              Assertions.assertEquals(OrekitMessages.NO_DATA_LOADED_FOR_CELESTIAL_BODY, oe.getSpecifier());
67              Assertions.assertEquals("No Orbit center name", oe.getParts()[0]);
68          }
69          
70          // refFrame.asFrame() == null
71          meta.setOrbitCenter(BodyFacade.create(CenterName.MARS));
72          try {
73              meta.setRefFrame(new FrameFacade(null, null, null, null, "dummy frame"));
74              meta.getFrame();
75              Assertions.fail("an exception should have been thrown");
76          } catch (OrekitException oe) {
77              Assertions.assertEquals(OrekitMessages.CCSDS_INVALID_FRAME, oe.getSpecifier());
78              Assertions.assertEquals("dummy frame", oe.getParts()[0]);
79          }
80          
81          // refFrame.asCelestialBodyFrame() == CelestialBodyFrame.MCI
82          // AND
83          // CelestialBodyFactory.MARS.equals(orbitCenter.getBody().getName())
84          meta.setRefFrame(FrameFacade.map(CelestialBodyFactory.getMars().getInertiallyOrientedFrame()));
85          Assertions.assertEquals(CelestialBodyFactory.getMars().getInertiallyOrientedFrame(), meta.getFrame());
86          
87          // refFrame.asCelestialBodyFrame() == CelestialBodyFrame.ICRF
88          // AND
89          // isIcrf && isSolarSystemBarycenter
90          final Frame icrf = FramesFactory.getICRF();
91          meta.setOrbitCenter(BodyFacade.create(CenterName.SOLAR_SYSTEM_BARYCENTER));
92          meta.setRefFrame(FrameFacade.map(icrf));
93          Assertions.assertEquals(icrf, meta.getFrame());
94      }
95      
96      /** Condition coverage on the setAltCovRefFrame method. */
97      @Test
98      @DefaultDataContext
99      public void testSetAltCovRefFrame() {
100         
101         final CdmMetadata meta = new CdmMetadata();
102         final FrameFacade altCovRefFrame = new FrameFacade(null, null, null, null, null);
103 
104         // getAltCovType()
105         try {
106             meta.setAltCovRefFrame(altCovRefFrame);
107             Assertions.fail("an exception should have been thrown");
108         } catch (OrekitException oe) {
109             Assertions.assertEquals(OrekitMessages.CCSDS_MISSING_KEYWORD, oe.getSpecifier());
110             Assertions.assertEquals(CdmMetadataKey.ALT_COV_TYPE, oe.getParts()[0]);
111         }
112         
113         // altCovRefFrame.asFrame() == null
114         meta.setAltCovType(AltCovarianceType.CSIG3EIGVEC3);
115         try {
116             meta.setAltCovRefFrame(altCovRefFrame);
117             Assertions.fail("an exception should have been thrown");
118         } catch (OrekitException oe) {
119             Assertions.assertEquals(OrekitMessages.CCSDS_INVALID_FRAME, oe.getSpecifier());
120             Assertions.assertEquals(null, oe.getParts()[0]);
121         }
122         
123         // Frame not allowed (allowed frames are GCRF, EME2000, ITRF)
124         FrameFacade frameFacade = FrameFacade.map(FramesFactory.getICRF());
125         try {
126             meta.setAltCovRefFrame(frameFacade);
127             Assertions.fail("an exception should have been thrown");
128         } catch (OrekitException oe) {
129             Assertions.assertEquals(OrekitMessages.CCSDS_INVALID_FRAME, oe.getSpecifier());
130             Assertions.assertEquals(frameFacade.getName(), oe.getParts()[0]);
131         }
132         
133         // Test the 3 allowed frames
134         frameFacade = FrameFacade.map(FramesFactory.getGCRF());
135         meta.setAltCovRefFrame(frameFacade);
136         Assertions.assertEquals(frameFacade, meta.getAltCovRefFrame());
137         
138         frameFacade = FrameFacade.map(FramesFactory.getEME2000());
139         meta.setAltCovRefFrame(frameFacade);
140         Assertions.assertEquals(frameFacade, meta.getAltCovRefFrame());
141         
142         frameFacade = FrameFacade.map(FramesFactory.getITRF(IERSConventions.IERS_2010, true));
143         meta.setAltCovRefFrame(frameFacade);
144         Assertions.assertEquals(frameFacade, meta.getAltCovRefFrame());
145     }
146 }