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.adm.apm;
18  
19  import java.io.CharArrayWriter;
20  import java.io.IOException;
21  
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.Test;
24  import org.orekit.data.DataSource;
25  import org.orekit.errors.OrekitException;
26  import org.orekit.errors.OrekitMessages;
27  import org.orekit.files.ccsds.ndm.AbstractWriterTest;
28  import org.orekit.files.ccsds.ndm.ParsedUnitsBehavior;
29  import org.orekit.files.ccsds.ndm.ParserBuilder;
30  import org.orekit.files.ccsds.ndm.WriterBuilder;
31  import org.orekit.files.ccsds.ndm.adm.AdmHeader;
32  import org.orekit.files.ccsds.ndm.adm.AdmMetadata;
33  import org.orekit.files.ccsds.section.HeaderKey;
34  import org.orekit.files.ccsds.section.Segment;
35  import org.orekit.files.ccsds.utils.generation.Generator;
36  import org.orekit.files.ccsds.utils.generation.XmlGenerator;
37  import org.orekit.utils.Constants;
38  
39  public class ApmWriterTest extends AbstractWriterTest<AdmHeader, Segment<AdmMetadata, ApmData>, Apm> {
40  
41      protected ApmParser getParser() {
42          return new ParserBuilder().
43                 withParsedUnitsBehavior(ParsedUnitsBehavior.STRICT_COMPLIANCE).
44                 buildApmParser();
45      }
46  
47      protected ApmWriter getWriter() {
48          return new WriterBuilder().buildApmWriter();
49      }
50  
51      @Test
52      public void testWriteExample01() {
53          doTest("/ccsds/adm/apm/APMExample01.txt");
54      }
55  
56      @Test
57      public void testWriteKvnExample02() {
58          doTest("/ccsds/adm/apm/APMExample02.txt");
59      }
60  
61      @Test
62      public void testWriteXmlExample02() {
63          doTest("/ccsds/adm/apm/APMExample02.xml");
64      }
65  
66      @Test
67      public void testWriteExample03() {
68          doTest("/ccsds/adm/apm/APMExample03.txt");
69      }
70  
71      @Test
72      public void testWriteExample04() {
73          doTest("/ccsds/adm/apm/APMExample04.txt");
74      }
75  
76      @Test
77      public void testWriteExample05() {
78          doTest("/ccsds/adm/apm/APMExample05.txt");
79      }
80  
81      @Test
82      public void testWriteExample06() {
83          doTest("/ccsds/adm/apm/APMExample06.txt");
84      }
85  
86      @Test
87      public void testWriteExample07() {
88          doTest("/ccsds/adm/apm/APMExample07.txt");
89      }
90  
91      @Test
92      public void testWriteExample08() {
93          doTest("/ccsds/adm/apm/APMExample08.txt");
94      }
95  
96      @Test
97      public void testWriteExample09() {
98          doTest("/ccsds/adm/apm/APMExample09.txt");
99      }
100 
101     @Test
102     public void testWriteExample10() {
103         doTest("/ccsds/adm/apm/APMExample10.txt");
104     }
105 
106     @Test
107     public void testWriteExample11() {
108         doTest("/ccsds/adm/apm/APMExample11.txt");
109     }
110 
111     @Test
112     public void testWriteExample12() {
113         doTest("/ccsds/adm/apm/APMExample12.txt");
114     }
115 
116     @Test
117     public void testWrongVersion() throws IOException {
118         final String  name = "/ccsds/adm/apm/APMExample01.txt";
119         final Apm file = new ParserBuilder().
120                              buildApmParser().
121                              parseMessage(new DataSource(name, () -> getClass().getResourceAsStream(name)));
122         file.getHeader().setFormatVersion(1.0);
123         file.getHeader().setMessageId("this message is only allowed in format version 2.0 and later");
124         try (Generator generator = new XmlGenerator(new CharArrayWriter(), XmlGenerator.DEFAULT_INDENT, "",
125                                                     Constants.JULIAN_DAY, false, null)) {
126             new WriterBuilder().buildApmWriter().writeMessage(generator, file);
127             Assertions.fail("an exception should heave been thrown");
128         } catch (OrekitException oe) {
129             Assertions.assertEquals(OrekitMessages.CCSDS_KEYWORD_NOT_ALLOWED_IN_VERSION, oe.getSpecifier());
130             Assertions.assertEquals(HeaderKey.MESSAGE_ID.name(), oe.getParts()[0]);
131         }
132 
133     }
134 
135     @Test
136     public void testClassificationForbidden() throws IOException {
137         final String  name = "/ccsds/adm/apm/APMExample01.txt";
138         final Apm file = new ParserBuilder().
139                              buildApmParser().
140                              parseMessage(new DataSource(name, () -> getClass().getResourceAsStream(name)));
141         file.getHeader().setFormatVersion(1.0);
142         file.getHeader().setClassification("classification is not allowed in ADM");
143         try (Generator generator = new XmlGenerator(new CharArrayWriter(), XmlGenerator.DEFAULT_INDENT, "",
144                                                     Constants.JULIAN_DAY, false, null)) {
145             new WriterBuilder().buildApmWriter().writeMessage(generator, file);
146             Assertions.fail("an exception should heave been thrown");
147         } catch (OrekitException oe) {
148             Assertions.assertEquals(OrekitMessages.CCSDS_KEYWORD_NOT_ALLOWED_IN_VERSION, oe.getSpecifier());
149             Assertions.assertEquals(HeaderKey.CLASSIFICATION.name(), oe.getParts()[0]);
150         }
151 
152     }
153 
154     @Test
155     public void testException() {
156         try {
157             new ApmData(null, null, null, null, null, null, null).validate(1.0);
158             Assertions.fail("an exception should have been thrown");
159         } catch (OrekitException oe) {
160             Assertions.assertEquals(OrekitMessages.UNINITIALIZED_VALUE_FOR_KEY, oe.getSpecifier());
161             Assertions.assertEquals(ApmQuaternionKey.Q_FRAME_A.name(), oe.getParts()[0]);
162         }
163     }
164 
165 }