1   /* Contributed in the public domain.
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 java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collections;
22  import java.util.List;
23  import java.util.stream.Collectors;
24  
25  import org.hamcrest.CoreMatchers;
26  import org.hamcrest.MatcherAssert;
27  import org.junit.Assert;
28  import org.junit.Before;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  import org.orekit.Utils;
32  import org.orekit.errors.TimeStampedCacheException;
33  import org.orekit.time.AbsoluteDate;
34  
35  /**
36   * Unit tests for {@link ImmutableTimeStampedCache}.
37   *
38   * @author Evan Ward
39   */
40  public class ImmutableTimeStampedCacheTest {
41  
42      /**
43       * arbitrary date
44       */
45      private static final AbsoluteDate date = AbsoluteDate.CCSDS_EPOCH;
46  
47      /**
48       * set Orekit data for useful debugging messages from dates.
49       */
50      @BeforeClass
51      public static void setUpBefore() {
52          Utils.setDataRoot("regular-data");
53      }
54  
55      /**
56       * data provided to {@link #cache}
57       */
58      private List<AbsoluteDate> data;
59  
60      /**
61       * subject under test
62       */
63      private ImmutableTimeStampedCache<AbsoluteDate> cache;
64  
65      /**
66       * create {@link #cache} and {@link #data} with neighborsSize = 3
67       */
68      @Before
69      public void setUp() {
70          data = Arrays.asList(date, date.shiftedBy(1), date.shiftedBy(2),
71                               date.shiftedBy(3), date.shiftedBy(4),
72                               date.shiftedBy(5));
73          cache = new ImmutableTimeStampedCache<AbsoluteDate>(3, data);
74      }
75  
76      /**
77       * check
78       * {@link ImmutableTimeStampedCache#ImmutableTimeStampedCache(int, java.util.Collection)}
79       */
80      @Test
81      public void testImmutableTimeStampedCache() {
82          // exception for neighborsSize > data.size()
83          try {
84              new ImmutableTimeStampedCache<AbsoluteDate>(data.size() + 1, data);
85              Assert.fail("Expected Exception");
86          } catch (IllegalArgumentException e) {
87              // expected
88          }
89  
90          // exception for non-positive neighborsSize
91          try {
92              new ImmutableTimeStampedCache<AbsoluteDate>(0, data);
93              Assert.fail("Expected Exception");
94          } catch (IllegalArgumentException e) {
95              // expected
96          }
97  
98          // exception for null data
99          try {
100             new ImmutableTimeStampedCache<AbsoluteDate>(1, null);
101             Assert.fail("Expected Exception");
102         } catch (NullPointerException e) {
103             // expected
104         }
105 
106         // exception for zero data
107         try {
108             new ImmutableTimeStampedCache<AbsoluteDate>(
109                                                         1,
110                                                         Collections
111                                                             .<AbsoluteDate> emptyList());
112             Assert.fail("Expected Exception");
113         } catch (IllegalArgumentException e) {
114             // expected
115         }
116     }
117 
118     /**
119      * check {@link ImmutableTimeStampedCache#getNeighbors(AbsoluteDate)} at a
120      * series of different dates designed to test all logic paths.
121      *
122      * @throws TimeStampedCacheException
123      */
124     @Test
125     public void testGetNeighbors()
126         throws TimeStampedCacheException {
127         // setup
128         int size = data.size();
129 
130         // actions + verify
131 
132         // before fist data
133         try {
134             cache.getNeighbors(data.get(0).shiftedBy(-1));
135             Assert.fail("Expected Exception");
136         } catch (TimeStampedCacheException e) {
137             // expected
138         }
139 
140         // on fist date
141         Assert.assertArrayEquals(cache.getNeighbors(data.get(0)).toArray(), data
142             .subList(0, 3).toArray());
143         // between fist and second date
144         Assert.assertArrayEquals(cache.getNeighbors(data.get(0).shiftedBy(0.5))
145             .toArray(), data.subList(0, 3).toArray());
146         // in the middle on a date
147         Assert.assertArrayEquals(cache.getNeighbors(data.get(2)).toArray(), data
148             .subList(1, 4).toArray());
149         // in the middle between dates
150         Assert.assertArrayEquals(cache.getNeighbors(data.get(2).shiftedBy(0.5))
151             .toArray(), data.subList(1, 4).toArray());
152         // just before last date
153         Assert.assertArrayEquals(cache
154             .getNeighbors(data.get(size - 1).shiftedBy(-0.5)).toArray(), data
155             .subList(size - 3, size).toArray());
156         // on last date
157         Assert.assertArrayEquals(cache.getNeighbors(data.get(size - 1)).toArray(),
158                           data.subList(size - 3, size).toArray());
159 
160         // after last date
161         AbsoluteDate central = data.get(size - 1).shiftedBy(1);
162         try {
163             cache.getNeighbors(central);
164             Assert.fail("Expected Exception");
165         } catch (TimeStampedCacheException e) {
166             // expected
167             MatcherAssert.assertThat(e.getMessage(),
168                     CoreMatchers.containsString(central.toString()));
169         }
170     }
171 
172     /**
173      * check {@link ImmutableTimeStampedCache#getNeighborsSize()}
174      */
175     @Test
176     public void testGetNeighborsSize() {
177         Assert.assertEquals(cache.getNeighborsSize(), 3);
178     }
179 
180     /**
181      * check {@link ImmutableTimeStampedCache#getEarliest()}
182      */
183     @Test
184     public void testGetEarliest() {
185         Assert.assertEquals(cache.getEarliest(), data.get(0));
186     }
187 
188     /**
189      * check {@link ImmutableTimeStampedCache#getLatest()}
190      */
191     @Test
192     public void testGetLatest() {
193         Assert.assertEquals(cache.getLatest(), data.get(data.size() - 1));
194     }
195 
196     /**
197      * check {@link ImmutableTimeStampedCache#getAll()}
198      */
199     @Test
200     public void testGetAll() {
201         Assert.assertArrayEquals(cache.getAll().toArray(), data.toArray());
202     }
203 
204     /**
205      * check that the cache is immutable by changing the data passed in the
206      * constructor and returned from methods.
207      *
208      * @throws TimeStampedCacheException
209      */
210     @Test
211     public void testImmutable()
212         throws TimeStampedCacheException {
213         // setup
214         List<AbsoluteDate> actuals;
215         List<AbsoluteDate> expecteds = new ArrayList<AbsoluteDate>(data);
216         AbsoluteDate different = date.shiftedBy(-50);
217 
218         // actions + verify
219 
220         // check constructor
221         data.set(0, different);
222         Assert.assertArrayEquals(cache.getAll().toArray(), expecteds.toArray());
223 
224         // check getAll
225         actuals = cache.getAll();
226         try {
227             actuals.set(0, different);
228         } catch (UnsupportedOperationException e) {
229             // exception ok
230         }
231         Assert.assertArrayEquals(cache.getAll().toArray(), expecteds.toArray());
232 
233         // check getNeighbors
234         actuals = cache.getNeighbors(date).collect(Collectors.toList());
235         Assert.assertArrayEquals(cache.getAll().toArray(), expecteds.toArray());
236     }
237 
238     /**
239      * check {@link ImmutableTimeStampedCache#emptyCache()}.
240      */
241     @Test
242     public void testEmptyCache() {
243         // setup
244         cache = ImmutableTimeStampedCache.emptyCache();
245 
246         // actions + verify
247         try {
248             cache.getNeighbors(date);
249             Assert.fail("Expected Exception");
250         } catch (TimeStampedCacheException e) {
251             // expected
252         }
253         try {
254             cache.getEarliest();
255             Assert.fail("Expected Exception");
256         } catch (IllegalStateException e) {
257             // expected
258         }
259         try {
260             cache.getLatest();
261             Assert.fail("Expected Exception");
262         } catch (IllegalStateException e) {
263             // expected
264         }
265         Assert.assertEquals(cache.getAll().size(), 0);
266         Assert.assertEquals(cache.getNeighborsSize(), 0);
267     }
268 }