1   /* Copyright 2013-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.rugged.errors;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.io.BufferedReader;
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.IOException;
26  import java.io.InputStreamReader;
27  import java.io.PrintWriter;
28  import java.lang.reflect.InvocationTargetException;
29  import java.lang.reflect.Method;
30  import java.net.URISyntaxException;
31  import java.nio.charset.StandardCharsets;
32  
33  import org.junit.jupiter.api.Test;
34  import org.junit.jupiter.api.io.TempDir;
35  import org.orekit.bodies.GeodeticPoint;
36  import org.orekit.data.DataContext;
37  import org.orekit.data.DirectoryCrawler;
38  import org.orekit.frames.Frame;
39  import org.orekit.frames.FramesFactory;
40  import org.orekit.frames.Transform;
41  import org.orekit.rugged.api.Rugged;
42  import org.orekit.rugged.linesensor.SensorPixel;
43  
44  public class DumpTest {
45  
46      @TempDir
47      public File tempFolder;
48      
49      @Test
50      public void testGetKeyOrNameCoverage() throws NoSuchMethodException, SecurityException, IllegalAccessException, 
51                                            IllegalArgumentException, InvocationTargetException, IOException {
52          File tempFile = File.createTempFile("junit", null, tempFolder);
53          try (PrintWriter pw = new PrintWriter(tempFile, "UTF-8")) {
54  
55              Dump dump = new Dump(pw);
56  
57              Method getKeyOrName = dump.getClass().getDeclaredMethod("getKeyOrName", Frame.class);
58              getKeyOrName.setAccessible(true);
59  
60              String dummyName = "dummy";
61              Frame frame = new Frame(FramesFactory.getEME2000(), Transform.IDENTITY, dummyName);
62  
63              String foundName = (String) getKeyOrName.invoke(dump, frame);
64  
65              assertEquals(dummyName, foundName);
66          }
67      }
68      
69      @Test
70      public void testInverseLocNull() throws NoSuchMethodException, SecurityException, IllegalAccessException, 
71                                              IllegalArgumentException, InvocationTargetException, IOException {
72          File tempFile = File.createTempFile("junit", null, tempFolder);
73          PrintWriter pw = new PrintWriter(tempFile, "UTF-8");
74          Dump dump = new Dump(pw);
75          
76          Method dumpInverseLocationResult = dump.getClass().getDeclaredMethod("dumpInverseLocationResult", SensorPixel.class);
77          dumpInverseLocationResult.setAccessible(true);
78  
79          SensorPixel px = null;
80          // just to ensure the test coverage
81          dumpInverseLocationResult.invoke(dump, px);
82          
83          dump.deactivate();
84          
85          // Check that the created file contains the line "inverse location result: NULL"
86          try (FileInputStream   fis = new FileInputStream(tempFile);
87               InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
88               BufferedReader    br  = new BufferedReader(isr)) {
89                 for (String line = br.readLine(); line != null; line = br.readLine()) {
90                     final String trimmed = line.trim();
91                     if (!(trimmed.isEmpty() || trimmed.startsWith("#"))) {
92                         assertTrue(line.contains("inverse location result: NULL"));
93                     }
94                 }
95             }
96      }
97      
98      @Test
99      public void testDirectLocNull() throws NoSuchMethodException, SecurityException, IllegalAccessException, 
100                                             IllegalArgumentException, InvocationTargetException, IOException {
101         File tempFile = File.createTempFile("junit", null, tempFolder);
102         PrintWriter pw = new PrintWriter(tempFile, "UTF-8");
103         Dump dump = new Dump(pw);
104         
105         Method dumpDirectLocationResult = dump.getClass().getDeclaredMethod("dumpDirectLocationResult", GeodeticPoint.class);
106         dumpDirectLocationResult.setAccessible(true);
107 
108         GeodeticPoint gp = null;
109         // just to ensure the coverage
110         dumpDirectLocationResult.invoke(dump, gp);
111         dump.deactivate();
112         
113         // Check that the created file contains the line "inverse location result: NULL"
114         try (FileInputStream   fis = new FileInputStream(tempFile);
115              InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
116              BufferedReader    br  = new BufferedReader(isr)) {
117                for (String line = br.readLine(); line != null; line = br.readLine()) {
118                    final String trimmed = line.trim();
119                    if (!(trimmed.isEmpty() || trimmed.startsWith("#"))) {
120                        assertTrue(line.contains("direct location result: NULL"));
121                    }
122                }
123            }
124     }
125     
126     @Test
127     public void testSetMeanPlane() throws SecurityException,
128             IllegalArgumentException, IOException, URISyntaxException {
129 
130         String orekitPath = getClass().getClassLoader().getResource("orekit-data").toURI().getPath();
131         DataContext.getDefault().getDataProvidersManager().addProvider(new DirectoryCrawler(new File(orekitPath)));
132 
133         String dumpPath = getClass().getClassLoader().getResource("replay/replay-inverse-loc-02.txt").toURI().getPath();
134         
135         // Regenerate a dump in order to write the "sensor mean plane: sensorName xxx lineNumber xxx targetDirection xxx targetDirection xxx ...."
136         File dummyDump = File.createTempFile("junit", null, tempFolder);
137         DumpManager.activate(dummyDump);
138         
139         DumpReplayer replayer = new DumpReplayer();
140         replayer.parse(new File(dumpPath));
141         Rugged rugged = replayer.createRugged();
142         replayer.execute(rugged);
143         
144         DumpManager.deactivate();
145         
146         // Check that the created dump contains the line " lineNumber xxx targetDirection xxx targetDirection xxx ...."
147         try (FileInputStream   fis = new FileInputStream(dummyDump);
148              InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
149              BufferedReader    br  = new BufferedReader(isr)) {
150             for (String line = br.readLine(); line != null; line = br.readLine()) {
151                 final String trimmed = line.trim();
152                 if (!(trimmed.isEmpty() || trimmed.startsWith("#"))) {
153                     if (line.contains("lineNumber ")&& line.contains("targetDirection ")) {
154                         assertEquals(6, line.split("targetDirection").length);
155                     }
156                 }
157             }
158         }
159     }
160 }