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.utils.generation;
18  
19  import org.hipparchus.util.FastMath;
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.Test;
22  import org.orekit.utils.Constants;
23  import org.orekit.utils.units.Unit;
24  
25  import java.io.CharArrayWriter;
26  import java.io.IOException;
27  
28  public class KvnGeneratorTest {
29  
30      @Test
31      public void testSection() throws IOException {
32          CharArrayWriter caw = new CharArrayWriter();
33          try (Generator generator = new KvnGenerator(caw, 10, "", Constants.JULIAN_DAY, 25)) {
34              generator.startMessage("abc", "CCSDS_ABC_VERSION", 99.0);
35              generator.enterSection("BLOCK");
36              generator.writeEntry("KEY", 1234567.8, Unit.parse("Hz"), false);
37              generator.exitSection();
38              generator.endMessage("abc");
39              Assertions.assertEquals("CCSDS_ABC_VERSION = 99.0\n" +
40                                  "BLOCK_START\n" +
41                                  "KEY        = 1234567.8   [Hz]\n" +
42                                  "BLOCK_STOP\n",
43                                  caw.toString());
44          }
45      }
46  
47      @Test
48      public void testCcsdsUnits() throws IOException {
49          CharArrayWriter caw = new CharArrayWriter();
50          try (Generator generator = new KvnGenerator(caw, 10, "", Constants.JULIAN_DAY, 25)) {
51              generator.writeEntry("KEY_1",    1234567.8,   Unit.parse("km.kg³/√s"), false);
52              generator.writeEntry("KEY_2",    1234567.8,   Unit.parse("n/a"),       false);
53              generator.writeEntry("KEY_3",    1234567.8,   Unit.parse("1"),         false);
54              generator.writeEntry("LOOOOONG", "1234567.8", null,                    false);
55              Assertions.assertEquals("KEY_1      = 1234.5678   [km*kg**3/s**0.5]\n" +
56                                  "KEY_2      = 1234567.8\n" +
57                                  "KEY_3      = 1234567.8\n" +
58                                  "LOOOOONG   = 1234567.8\n",
59                                  caw.toString());
60          }
61      }
62  
63      @Test
64      public void testUnitsPadding() throws IOException {
65          CharArrayWriter caw = new CharArrayWriter();
66          try (Generator generator = new KvnGenerator(caw, 10, "", Constants.JULIAN_DAY, 20)) {
67              generator.writeEntry("KEY_1", 0.5 * FastMath.PI, Unit.parse("°"), false);
68              generator.writeEntry("KEY_2", FastMath.PI, Unit.parse("◦"), false);
69              generator.writeEntry("PERCENT", 0.25, Unit.parse("%"), false);
70              Assertions.assertEquals("KEY_1      = 90.0   [deg]\n" +
71                                  "KEY_2      = 180.0  [deg]\n" +
72                                  "PERCENT    = 25.0   [%]\n",
73                                  caw.toString());
74          }
75      }
76  
77      @Test
78      public void testNoUnits() throws IOException {
79          CharArrayWriter caw = new CharArrayWriter();
80          try (Generator generator = new KvnGenerator(caw, 10, "", Constants.JULIAN_DAY, 0)) {
81              generator.writeEntry("KEY_1", 0.5 * FastMath.PI, Unit.parse("°"), false);
82              generator.writeEntry("KEY_2", FastMath.PI, Unit.parse("◦"), true);
83              Assertions.assertEquals("KEY_1      = 90.0\n" +
84                                  "KEY_2      = 180.0\n", caw.toString());
85          }
86      }
87  
88  }