Skip to content

Commit af9940e

Browse files
committed
unit test for gethistory
1 parent 2bc4a36 commit af9940e

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ dependencies {
3636
implementation group: 'org.knowm.xchart', name: 'xchart', version: '3.8.5'
3737
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.13.0'
3838
implementation group: 'com.github.librepdf', name: 'openpdf', version: '1.3.30'
39-
4039
implementation("com.google.guava:guava:32.1.2-jre")
40+
4141
testImplementation 'com.tngtech.archunit:archunit-junit5:1.1.0'
42+
testImplementation 'org.mockito:mockito-junit-jupiter:5.4.0'
4243
}
4344

4445
tasks.named('test') {

src/main/java/com/ldiamond/sqgraph/SqgraphApplication.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,14 @@ public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
159159
return null;
160160
}
161161

162-
private static AssembledSearchHistory getHistory (final Config config, final String sdfsqString, final String key, final String metrics, final HttpHeaders headers, RestTemplate restTemplate) {
162+
public static AssembledSearchHistory getHistory (final Config config, final String sdfsqString, final String key, final String metrics,
163+
final HttpHeaders headers, RestTemplate restTemplate) {
163164
AssembledSearchHistory assembledSearchHistory = new AssembledSearchHistory();
164165
int page = 1;
165166
boolean notYetLastPage = true;
166167
do {
167168
final String uri = config.getUrl() + "/api/measures/search_history?from="+sdfsqString+"&p="+page+"&ps=999&component=" + key + "&metrics=" + metrics;
168-
// System.out.println (uri);
169+
System.out.println (uri);
169170
ResponseEntity<SearchHistory> response = restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<String>(headers), SearchHistory.class);
170171
SearchHistory result = response.getBody();
171172
if (result != null) {

src/test/java/com/ldiamond/sqgraph/ArchitectureUnitTests.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@
1010
public class ArchitectureUnitTests {
1111

1212
@ArchTest
13-
static final ArchRule no_vectors = noClasses().should().callConstructor(java.util.Vector.class);
13+
static final ArchRule no_vectors = noClasses().should().callConstructor(java.util.Vector.class).because("Use ArrayList");
1414

1515
@ArchTest
16-
static final ArchRule no_hashtable = noClasses().should().callConstructor(java.util.Hashtable.class);
16+
static final ArchRule no_hashtable = noClasses().should().callConstructor(java.util.Hashtable.class).because("Use HashSet");
1717

1818
@ArchTest
19-
static final ArchRule no_stringbuffer = noClasses().should().callConstructor(java.lang.StringBuffer.class);
19+
static final ArchRule no_stringbuffer = noClasses().should().callConstructor(java.lang.StringBuffer.class).because("Use StringBuilder");
20+
21+
@ArchTest
22+
static final ArchRule no_treeset = noClasses().should().callConstructor(java.util.TreeSet.class).because("Use ConcurrentSkipListSet");
23+
24+
@ArchTest
25+
static final ArchRule no_treemap = noClasses().should().callConstructor(java.util.TreeMap.class).because("Use ConcurrentSkipListMap");
2026

2127
@ArchTest
2228
private final ArchRule no_jodatime = NO_CLASSES_SHOULD_USE_JODATIME;

src/test/java/com/ldiamond/sqgraph/SqgraphApplicationTests.java

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,28 @@
33

44
import static org.junit.jupiter.api.Assertions.assertEquals;
55
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;
69

710
import java.util.ArrayList;
11+
import java.util.Date;
812
import java.util.HashMap;
913
import java.util.List;
1014
import java.util.Map;
1115

1216
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)
1428
class SqgraphApplicationTests {
1529

1630
@Test
@@ -231,4 +245,51 @@ void populateMetricsHasSyntheticPerH() {
231245
assertEquals(synths.get("something__PER_H_otherthing").getRealMetrics().get(1), "otherthing");
232246
assertEquals(200.0, synths.get("something__PER_H_otherthing").calculate(metrics), 0);
233247
}
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+
}
234295
}

0 commit comments

Comments
 (0)