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.files.ccsds.ndm.adm.apm;
18  
19  import java.io.CharArrayWriter;
20  import java.io.IOException;
21  
22  import org.junit.Assert;
23  import org.junit.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.AdmMetadata;
32  import org.orekit.files.ccsds.section.Header;
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  
38  public class ApmWriterTest extends AbstractWriterTest<Header, Segment<AdmMetadata, ApmData>, Apm> {
39  
40      protected ApmParser getParser() {
41          return new ParserBuilder().
42                 withParsedUnitsBehavior(ParsedUnitsBehavior.STRICT_COMPLIANCE).
43                 buildApmParser();
44      }
45  
46      protected ApmWriter getWriter() {
47          return new WriterBuilder().buildApmWriter();
48      }
49  
50      @Test
51      public void testWriteExample1() {
52          doTest("/ccsds/adm/apm/APMExample1.txt");
53      }
54  
55      @Test
56      public void testWriteKvnExample2() {
57          doTest("/ccsds/adm/apm/APMExample2.txt");
58      }
59  
60      @Test
61      public void testWriteXmlExample2() {
62          doTest("/ccsds/adm/apm/APMExample2.xml");
63      }
64  
65      @Test
66      public void testWriteExample3() {
67          doTest("/ccsds/adm/apm/APMExample3.txt");
68      }
69  
70      @Test
71      public void testWriteExample4() {
72          doTest("/ccsds/adm/apm/APMExample4.txt");
73      }
74  
75      @Test
76      public void testWriteExample5() {
77          doTest("/ccsds/adm/apm/APMExample5.txt");
78      }
79  
80      @Test
81      public void testWriteExample6() {
82          doTest("/ccsds/adm/apm/APMExample6.txt");
83      }
84  
85      @Test
86      public void testWrongVersion() throws IOException {
87          final String  name = "/ccsds/adm/apm/APMExample1.txt";
88          final Apm file = new ParserBuilder().
89                               buildApmParser().
90                               parseMessage(new DataSource(name, () -> getClass().getResourceAsStream(name)));
91          file.getHeader().setFormatVersion(1.0);
92          file.getHeader().setMessageId("this message is only allowed in format version 2.0 and later");
93          try (Generator generator = new XmlGenerator(new CharArrayWriter(), XmlGenerator.DEFAULT_INDENT, "", false)) {
94              new WriterBuilder().buildApmWriter().writeMessage(generator, file);
95              Assert.fail("an exception should heave been thrown");
96          } catch (OrekitException oe) {
97              Assert.assertEquals(OrekitMessages.CCSDS_KEYWORD_NOT_ALLOWED_IN_VERSION, oe.getSpecifier());
98              Assert.assertEquals(HeaderKey.MESSAGE_ID.name(), oe.getParts()[0]);
99          }
100         
101     }
102 
103 }