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.odm.ocm;
18  
19  import static org.hamcrest.MatcherAssert.assertThat;
20  import static org.hamcrest.Matchers.is;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.InputStreamReader;
25  import java.io.Reader;
26  import java.io.StringWriter;
27  import java.nio.CharBuffer;
28  import java.nio.charset.StandardCharsets;
29  
30  import org.junit.jupiter.api.Test;
31  import org.orekit.data.DataSource;
32  import org.orekit.files.ccsds.ndm.AbstractWriterTest;
33  import org.orekit.files.ccsds.ndm.ParsedUnitsBehavior;
34  import org.orekit.files.ccsds.ndm.ParserBuilder;
35  import org.orekit.files.ccsds.ndm.WriterBuilder;
36  import org.orekit.files.ccsds.ndm.odm.OdmHeader;
37  import org.orekit.files.ccsds.section.Segment;
38  import org.orekit.files.ccsds.utils.generation.Generator;
39  import org.orekit.files.ccsds.utils.generation.KvnGenerator;
40  import org.orekit.utils.Constants;
41  
42  public class OcmWriterTest extends AbstractWriterTest<OdmHeader, Segment<OcmMetadata, OcmData>, Ocm> {
43  
44      protected OcmParser getParser() {
45          return new ParserBuilder().
46                 withParsedUnitsBehavior(ParsedUnitsBehavior.STRICT_COMPLIANCE).
47                 withEquatorialRadius(Constants.WGS84_EARTH_EQUATORIAL_RADIUS).
48                 withFlattening(Constants.WGS84_EARTH_FLATTENING).
49                 withMu(Constants.EIGEN5C_EARTH_MU).
50                 buildOcmParser();
51      }
52  
53      protected OcmWriter getWriter() {
54          return new WriterBuilder().
55                 withEquatorialRadius(Constants.WGS84_EARTH_EQUATORIAL_RADIUS).
56                 withFlattening(Constants.WGS84_EARTH_FLATTENING).
57                 buildOcmWriter();
58      }
59  
60      @Test
61      public void testWriteExample1() {
62          doTest("/ccsds/odm/ocm/OCMExample1.txt");
63      }
64  
65      @Test
66      public void testWriteKvnExample2() {
67          doTest("/ccsds/odm/ocm/OCMExample2.txt");
68      }
69  
70      @Test
71      public void testWriteXmlExample2() {
72          doTest("/ccsds/odm/ocm/OCMExample2.xml");
73      }
74  
75      @Test
76      public void testWriteExample3() {
77          doTest("/ccsds/odm/ocm/OCMExample3.txt");
78      }
79  
80      @Test
81      public void testWriteExample4() {
82          doTest("/ccsds/odm/ocm/OCMExample4.txt");
83      }
84  
85      @Test
86      public void testWriteExample5ITRF() {
87          doTest("/ccsds/odm/ocm/OCMExample5ITRF.txt");
88      }
89  
90      @Test
91      public void testWriteExample5Geodetic() {
92          doTest("/ccsds/odm/ocm/OCMExample5Geodetic.txt");
93      }
94  
95      /**
96       * Check that reading an OCM and writing it out doesn't add a bunch of optional fields
97       * without default values.
98       *
99       * @throws IOException on error.
100      */
101     @Test
102     public void testWriteMinimal1623() throws IOException {
103         // setup
104         // this file has every OCM section with all required values
105         String name = "/ccsds/odm/ocm/OCMMinimal.txt";
106         // this file also has the default values defined
107         String expectedName = "/ccsds/odm/ocm/OCMMinimalExpected.txt";
108         Ocm parsed = getParser().parse(
109                 new DataSource(name, () -> this.getClass().getResourceAsStream(name)));
110         StringWriter buffer = new StringWriter();
111         Generator generator = new KvnGenerator(buffer, 0, "memory", 0, 0);
112 
113         // action
114         getWriter().writeMessage(generator, parsed);
115 
116         // verify
117         String expected = fixture(expectedName);
118         String actual = buffer.toString();
119         assertThat(actual, is(expected));
120     }
121 
122     /**
123      * Read all characters from a class path resource idendified by
124      * {@code name}. Assumes UTF8. Not particularly efficient.
125      *
126      * @param name to read from. See {@link Class#getResourceAsStream(String)}.
127      * @return contents of the file.
128      * @throws IOException on error.
129      */
130     public static String fixture(String name) throws IOException {
131         try (InputStream is = OcmWriterTest.class.getResourceAsStream(name);
132              Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
133             CharBuffer buffer = CharBuffer.allocate(1024);
134             while (reader.read(buffer) != -1) {
135                 if (!buffer.hasRemaining()) {
136                     // needs larger buffer, double in size
137                     CharBuffer b = CharBuffer.allocate(2 * buffer.capacity());
138                     buffer.flip();
139                     b.put(buffer);
140                     buffer = b;
141                 }
142             }
143 
144             buffer.flip();
145             return buffer.toString();
146         }
147     }
148 
149 }