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.utils;
18  
19  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.BeforeEach;
21  import org.junit.jupiter.api.Test;
22  import org.orekit.Utils;
23  import org.orekit.errors.OrekitIllegalStateException;
24  import org.orekit.errors.OrekitMessages;
25  import org.orekit.time.AbsoluteDate;
26  import org.orekit.time.TimeScalesFactory;
27  import org.orekit.utils.TimeSpanMap.Span;
28  
29  public class ParameterDriverTest {
30  
31  	@Test
32      public void testPDriverConstruction(){
33          ParameterDriver p1 = new ParameterDriver("p1", 0.0, 1.0, -10.0, +10.0);
34          AbsoluteDate date = new AbsoluteDate(2010, 11, 02, 03, 0, 0, TimeScalesFactory.getUTC());
35  
36          p1.addSpanAtDate(date);
37          p1.setValue(3.0, date.shiftedBy(10));
38          Assertions.assertEquals(3.0, p1.getValue(date.shiftedBy(10)), 1e-10);
39          Assertions.assertEquals(0.0, p1.getValue(date.shiftedBy(-10)), 1e-10);
40          Assertions.assertEquals("Span" + p1.getName() + Integer.toString(0), p1.getNameSpan(date.shiftedBy(-10)));
41          Assertions.assertEquals("Span" + p1.getName() + Integer.toString(1), p1.getNameSpan(date.shiftedBy(10)));
42  
43          p1.addSpanAtDate(date.shiftedBy(2 * 24 * 3600));
44          p1.setValue(6.0, date.shiftedBy(2 * 24 * 3600));
45          Assertions.assertEquals(p1.getValue(date.shiftedBy(2 * 24 * 3600 + 10)), 6.0, 1e-10);
46          int nb = 0;
47          for (Span<String> span = p1.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
48          	Assertions.assertEquals(span.getData(),"Span" + p1.getName() + Integer.toString(nb++));
49          }
50          
51          p1.setName("p1_new");
52          nb = 0;
53          for (Span<String> span = p1.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
54          	Assertions.assertEquals(span.getData(),"Span" + p1.getName() + Integer.toString(nb++));
55          }
56          
57          
58  	}
59  
60  	@Test
61      public void testExceptionSetPeriod(){
62          ParameterDriver p1 = new ParameterDriver("p1", 0.0, 1.0, -1.0, +1.0);
63          AbsoluteDate date = new AbsoluteDate(2010, 11, 02, 03, 0, 0, TimeScalesFactory.getUTC());
64          p1.addSpans(date, date.shiftedBy(15 * 3600), 3 * 3600);
65          int nb = 0;
66          for (Span<String> span = p1.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
67          	Assertions.assertEquals(span.getData(),"Span" + p1.getName() + Integer.toString(nb++));
68          }
69          try {
70              p1.addSpans(date, date.shiftedBy(15 * 3600), 5*3600);
71              Assertions.fail("an exception should have been thrown");
72          } catch (OrekitIllegalStateException oe) {
73          	Assertions.assertEquals(OrekitMessages.PARAMETER_PERIODS_HAS_ALREADY_BEEN_SET, oe.getSpecifier());
74          	Assertions.assertEquals(p1.getName(), oe.getParts()[0]);
75          }
76          
77  	}
78  
79  	@Test
80      public void testExceptiongetValue(){
81          ParameterDriver p1 = new ParameterDriver("p1", 0.0, 1.0, -1.0, +1.0);
82          AbsoluteDate date = new AbsoluteDate(2010, 11, 02, 03, 0, 0, TimeScalesFactory.getUTC());
83          p1.addSpans(date, date.shiftedBy(15 * 3600), 3 * 3600);
84          try {
85              p1.getNormalizedValue();
86              Assertions.fail("an exception should have been thrown");
87          } catch (OrekitIllegalStateException oe) {
88          	Assertions.assertEquals(OrekitMessages.PARAMETER_WITH_SEVERAL_ESTIMATED_VALUES, oe.getSpecifier());
89          	Assertions.assertEquals(p1.getName(), oe.getParts()[0]);
90          	Assertions.assertEquals("getValue(date)", oe.getParts()[1]);
91          }
92          
93      }
94  
95  	@Test
96      public void testExceptionsetValue(){
97          ParameterDriver p1 = new ParameterDriver("p1", 0.0, 1.0, -1.0, +1.0);
98          AbsoluteDate date = new AbsoluteDate(2010, 11, 02, 03, 0, 0, TimeScalesFactory.getUTC());
99          p1.addSpans(date, date.shiftedBy(15 * 3600), 3 * 3600);
100         p1.setValue(30., date.shiftedBy(-100));
101         Assertions.assertEquals(1.0, p1.getValue(date.shiftedBy(-500)), 0);
102         p1.setValue(0.8, date.shiftedBy(-100));
103         Assertions.assertEquals(0.8, p1.getValue(date.shiftedBy(-500)), 0);
104         try {
105             p1.setNormalizedValue(2.0);
106             Assertions.fail("an exception should have been thrown");
107         } catch (OrekitIllegalStateException oe) {
108         	Assertions.assertEquals(OrekitMessages.PARAMETER_WITH_SEVERAL_ESTIMATED_VALUES, oe.getSpecifier());
109         	Assertions.assertEquals(p1.getName(), oe.getParts()[0]);
110         	Assertions.assertEquals("setValue(date)", oe.getParts()[1]);
111         }
112         
113     }
114 	
115     @BeforeEach
116     public void setUp() {
117         Utils.setDataRoot("regular-data");
118     }
119     
120 }