-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
195 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.coveo.pushapiclient; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.net.http.HttpResponse.BodyHandler; | ||
import org.apache.logging.log4j.Logger; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
public class ApiCoreTest { | ||
|
||
@Mock private HttpClient httpClient; | ||
@Mock private HttpRequest httpRequest; | ||
@Mock private Logger logger; | ||
@Mock private HttpResponse<String> httpResponse; | ||
|
||
@InjectMocks private ApiCore api; | ||
|
||
private AutoCloseable closeable; | ||
private static final String[] headers = { | ||
"Content-Type", "application/json", "Accept", "application/json" | ||
}; | ||
|
||
private void mockSuccessResponse() { | ||
when(httpResponse.statusCode()).thenReturn(200); | ||
when(httpResponse.body()).thenReturn("All good!"); | ||
when(httpRequest.method()).thenReturn("POST"); | ||
} | ||
|
||
private void mockErrorResponse() { | ||
when(httpResponse.statusCode()).thenReturn(412); | ||
when(httpResponse.body()).thenReturn("BAD_REQUEST"); | ||
when(httpRequest.method()).thenReturn("DELETE"); | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
closeable = MockitoAnnotations.openMocks(this); | ||
|
||
when(httpClient.send(any(HttpRequest.class), any(BodyHandler.class))).thenReturn(httpResponse); | ||
when(httpResponse.request()).thenReturn(httpRequest); | ||
} | ||
|
||
@After | ||
public void closeService() throws Exception { | ||
closeable.close(); | ||
} | ||
|
||
@Test | ||
public void testShouldLogRequestAndResonse() | ||
throws IOException, InterruptedException, URISyntaxException { | ||
this.mockSuccessResponse(); | ||
this.api.post(new URI("https://perdu.com/"), headers); | ||
|
||
verify(logger, times(1)).debug("POST https://perdu.com/"); | ||
verify(logger, times(1)).debug("POST status: 200"); | ||
verify(logger, times(1)).debug("POST response: All good!"); | ||
} | ||
|
||
@Test | ||
public void testShouldLogResponse() throws IOException, InterruptedException, URISyntaxException { | ||
this.mockErrorResponse(); | ||
this.api.delete(new URI("https://perdu.com/"), headers); | ||
|
||
verify(logger, times(1)).debug("DELETE https://perdu.com/"); | ||
verify(logger, times(1)).error("DELETE status: 412"); | ||
verify(logger, times(1)).error("DELETE response: BAD_REQUEST"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters