|
26 | 26 |
|
27 | 27 | class ApitallyClientTest { |
28 | 28 |
|
29 | | - private ApitallyClient client; |
30 | | - private ApitallyClient clientSpy; |
| 29 | + private ApitallyClient client; |
| 30 | + private ApitallyClient clientSpy; |
31 | 31 |
|
32 | | - @BeforeEach |
33 | | - void setUp() { |
34 | | - RequestLoggingConfig requestLoggingConfig = new RequestLoggingConfig(); |
35 | | - requestLoggingConfig.setEnabled(true); |
36 | | - client = new ApitallyClient("00000000-0000-0000-0000-000000000000", "test", requestLoggingConfig); |
37 | | - clientSpy = spy(client); |
38 | | - } |
39 | | - |
40 | | - @AfterEach |
41 | | - void tearDown() { |
42 | | - client.shutdown(); |
43 | | - } |
| 32 | + @BeforeEach |
| 33 | + void setUp() { |
| 34 | + RequestLoggingConfig requestLoggingConfig = new RequestLoggingConfig(); |
| 35 | + requestLoggingConfig.setEnabled(true); |
| 36 | + client = new ApitallyClient("00000000-0000-0000-0000-000000000000", "test", requestLoggingConfig); |
| 37 | + clientSpy = spy(client); |
| 38 | + when(clientSpy.sendHubRequest(any(HttpRequest.class))) |
| 39 | + .thenReturn(CompletableFuture.completedFuture(ApitallyClient.HubRequestStatus.OK)); |
| 40 | + } |
44 | 41 |
|
45 | | - @Test |
46 | | - void testSync() { |
47 | | - when(clientSpy.sendHubRequest(any(HttpRequest.class))) |
48 | | - .thenReturn(CompletableFuture.completedFuture(ApitallyClient.HubRequestStatus.OK)); |
| 42 | + @AfterEach |
| 43 | + void tearDown() { |
| 44 | + client.shutdown(); |
| 45 | + } |
49 | 46 |
|
50 | | - Header[] requestHeaders = new Header[] { |
51 | | - new Header("Content-Type", "application/json"), |
52 | | - new Header("User-Agent", "test-client"), |
53 | | - }; |
54 | | - Header[] responseHeaders = new Header[] { |
55 | | - new Header("Content-Type", "application/json"), |
56 | | - }; |
57 | | - Request request = new Request( |
58 | | - System.currentTimeMillis() / 1000.0, |
59 | | - "tester", |
60 | | - "GET", |
61 | | - "/items", |
62 | | - "http://test/items", |
63 | | - requestHeaders, |
64 | | - 0L, |
65 | | - new byte[0]); |
66 | | - Response response = new Response( |
67 | | - 200, |
68 | | - 0.123, |
69 | | - responseHeaders, |
70 | | - 13L, |
71 | | - "{\"items\": []}".getBytes()); |
72 | | - client.requestLogger.logRequest(request, response); |
73 | | - client.requestLogger.maintain(); |
| 47 | + @Test |
| 48 | + void testSync() { |
| 49 | + Header[] requestHeaders = new Header[] { |
| 50 | + new Header("Content-Type", "application/json"), |
| 51 | + new Header("User-Agent", "test-client"), |
| 52 | + }; |
| 53 | + Header[] responseHeaders = new Header[] { |
| 54 | + new Header("Content-Type", "application/json"), |
| 55 | + }; |
| 56 | + Request request = new Request( |
| 57 | + System.currentTimeMillis() / 1000.0, |
| 58 | + "tester", |
| 59 | + "GET", |
| 60 | + "/items", |
| 61 | + "http://test/items", |
| 62 | + requestHeaders, |
| 63 | + 0L, |
| 64 | + new byte[0]); |
| 65 | + Response response = new Response( |
| 66 | + 200, |
| 67 | + 0.123, |
| 68 | + responseHeaders, |
| 69 | + 13L, |
| 70 | + "{\"items\": []}".getBytes()); |
| 71 | + client.requestLogger.logRequest(request, response); |
| 72 | + client.requestLogger.maintain(); |
74 | 73 |
|
75 | | - List<Path> paths = Arrays.asList(new Path("GET", "/items")); |
76 | | - Map<String, String> versions = new HashMap<>(); |
77 | | - versions.put("package", "1.0.0"); |
78 | | - clientSpy.setStartupData(paths, versions, "java:test"); |
79 | | - clientSpy.startSync(); |
| 74 | + List<Path> paths = Arrays.asList(new Path("GET", "/items")); |
| 75 | + Map<String, String> versions = new HashMap<>(); |
| 76 | + versions.put("package", "1.0.0"); |
| 77 | + clientSpy.setStartupData(paths, versions, "java:test"); |
| 78 | + clientSpy.startSync(); |
80 | 79 |
|
81 | | - delay(100); |
| 80 | + delay(100); |
82 | 81 |
|
83 | | - ArgumentCaptor<HttpRequest> requestCaptor = ArgumentCaptor.forClass(HttpRequest.class); |
84 | | - verify(clientSpy, times(3)).sendHubRequest(requestCaptor.capture()); |
85 | | - List<HttpRequest> capturedRequests = requestCaptor.getAllValues(); |
86 | | - assertTrue(capturedRequests.stream().anyMatch( |
87 | | - r -> r.uri().toString().contains("/startup"))); |
88 | | - assertTrue(capturedRequests.stream().anyMatch( |
89 | | - r -> r.uri().toString().contains("/sync"))); |
90 | | - assertTrue(capturedRequests.stream().anyMatch( |
91 | | - r -> r.uri().toString().contains("/log"))); |
| 82 | + ArgumentCaptor<HttpRequest> requestCaptor = ArgumentCaptor.forClass(HttpRequest.class); |
| 83 | + verify(clientSpy, times(3)).sendHubRequest(requestCaptor.capture()); |
| 84 | + List<HttpRequest> capturedRequests = requestCaptor.getAllValues(); |
| 85 | + assertTrue(capturedRequests.stream().anyMatch( |
| 86 | + r -> r.uri().toString().contains("/startup"))); |
| 87 | + assertTrue(capturedRequests.stream().anyMatch( |
| 88 | + r -> r.uri().toString().contains("/sync"))); |
| 89 | + assertTrue(capturedRequests.stream().anyMatch( |
| 90 | + r -> r.uri().toString().contains("/log"))); |
92 | 91 |
|
93 | | - clientSpy.stopSync(); |
94 | | - } |
| 92 | + clientSpy.stopSync(); |
| 93 | + } |
95 | 94 |
|
96 | | - private void delay(long millis) { |
97 | | - try { |
98 | | - Thread.sleep(millis); |
99 | | - } catch (InterruptedException e) { |
100 | | - Thread.currentThread().interrupt(); |
101 | | - } |
| 95 | + private void delay(long millis) { |
| 96 | + try { |
| 97 | + Thread.sleep(millis); |
| 98 | + } catch (InterruptedException e) { |
| 99 | + Thread.currentThread().interrupt(); |
102 | 100 | } |
| 101 | + } |
103 | 102 | } |
0 commit comments