|
3 | 3 |
|
4 | 4 | import static org.junit.jupiter.api.Assertions.assertEquals; |
5 | 5 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 6 | +import static org.mockito.ArgumentMatchers.any; |
| 7 | +import static org.mockito.ArgumentMatchers.anyString; |
| 8 | +import static org.mockito.Mockito.when; |
6 | 9 |
|
7 | 10 | import java.util.ArrayList; |
| 11 | +import java.util.Date; |
8 | 12 | import java.util.HashMap; |
9 | 13 | import java.util.List; |
10 | 14 | import java.util.Map; |
11 | 15 |
|
12 | 16 | import org.junit.jupiter.api.Test; |
13 | | - |
| 17 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 18 | +import org.mockito.Mock; |
| 19 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 20 | +import org.springframework.http.HttpEntity; |
| 21 | +import org.springframework.http.HttpHeaders; |
| 22 | +import org.springframework.http.HttpMethod; |
| 23 | +import org.springframework.http.HttpStatus; |
| 24 | +import org.springframework.http.ResponseEntity; |
| 25 | +import org.springframework.web.client.RestTemplate; |
| 26 | + |
| 27 | +@ExtendWith(MockitoExtension.class) |
14 | 28 | class SqgraphApplicationTests { |
15 | 29 |
|
16 | 30 | @Test |
@@ -231,4 +245,51 @@ void populateMetricsHasSyntheticPerH() { |
231 | 245 | assertEquals(synths.get("something__PER_H_otherthing").getRealMetrics().get(1), "otherthing"); |
232 | 246 | assertEquals(200.0, synths.get("something__PER_H_otherthing").calculate(metrics), 0); |
233 | 247 | } |
| 248 | + |
| 249 | + @Mock |
| 250 | + RestTemplate restTemplate; |
| 251 | + |
| 252 | + @Test |
| 253 | + void getHistory() { |
| 254 | + final Config config = new Config(); |
| 255 | + config.setUrl("prefix"); |
| 256 | + |
| 257 | + SearchHistory sh = new SearchHistory(); |
| 258 | + Paging paging = new Paging(); |
| 259 | + paging.setTotal(2); |
| 260 | + Measures[] measuresArray = new Measures[2]; |
| 261 | + sh.setMeasures(measuresArray); |
| 262 | + measuresArray [0] = new Measures(); |
| 263 | + measuresArray [0].setMetric("first"); |
| 264 | + measuresArray [0].setHistory(new History[1]); |
| 265 | + measuresArray [0].history[0] = new History(); |
| 266 | + measuresArray [0].history[0].setDate(new Date("Tue, 1 Aug 1995 13:30:00 GMT")); |
| 267 | + measuresArray [0].history[0].setValue(1.0); |
| 268 | + |
| 269 | + measuresArray [1] = new Measures(); |
| 270 | + measuresArray [1].setMetric("second"); |
| 271 | + measuresArray [1].setHistory(new History[1]); |
| 272 | + measuresArray [1].history[0] = new History(); |
| 273 | + measuresArray [1].history[0].setDate(new Date("Wed, 2 Aug 1995 13:30:00 GMT")); |
| 274 | + measuresArray [1].history[0].setValue(2.0); |
| 275 | + |
| 276 | + sh.setPaging(paging); |
| 277 | + |
| 278 | + ResponseEntity<SearchHistory> rsh = new ResponseEntity<>(sh, null, HttpStatus.OK); |
| 279 | + HttpHeaders httpHeaders = new HttpHeaders(); |
| 280 | + HttpEntity<String> hes = new HttpEntity<>(httpHeaders); |
| 281 | + when (restTemplate.exchange("prefix/api/measures/search_history?from=blah&p=1&ps=999&component=blah&metrics=blah",HttpMethod.GET,hes,SearchHistory.class)).thenReturn(rsh); |
| 282 | + when (restTemplate.exchange("prefix/api/measures/search_history?from=blah&p=2&ps=999&component=blah&metrics=blah",HttpMethod.GET,hes,SearchHistory.class)).thenReturn(rsh); |
| 283 | + AssembledSearchHistory ash = SqgraphApplication.getHistory(config, "blah", "blah", "blah", httpHeaders, restTemplate); |
| 284 | + |
| 285 | + assertEquals(4, ash.getMeasures().size()); |
| 286 | + assertEquals("first", ash.getMeasures().get(0).getMetric()); |
| 287 | + assertEquals("second", ash.getMeasures().get(1).getMetric()); |
| 288 | + assertEquals("first", ash.getMeasures().get(2).getMetric()); |
| 289 | + assertEquals("second", ash.getMeasures().get(3).getMetric()); |
| 290 | + assertEquals(1, ash.getMeasures().get(0).history.length); |
| 291 | + assertEquals(1, ash.getMeasures().get(1).history.length); |
| 292 | + assertEquals(1, ash.getMeasures().get(2).history.length); |
| 293 | + assertEquals(1, ash.getMeasures().get(3).history.length); |
| 294 | + } |
234 | 295 | } |
0 commit comments