-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ryan Rishi <ryan@ryanrishi.com>
- Loading branch information
1 parent
6a8fffe
commit bb612ed
Showing
4 changed files
with
122 additions
and
17 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
95 changes: 95 additions & 0 deletions
95
src/test/java/io/fabric8/maven/docker/wait/HttpPingCheckerTest.java
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,95 @@ | ||
package io.fabric8.maven.docker.wait; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.RETURNS_SELF; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import io.fabric8.maven.docker.util.Logger; | ||
import org.apache.http.StatusLine; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class HttpPingCheckerTest { | ||
@Mock | ||
private Logger logger; | ||
|
||
private HttpPingChecker httpPingChecker; | ||
|
||
@BeforeEach | ||
void setup() { | ||
httpPingChecker = new HttpPingChecker("https://example.com", logger); | ||
} | ||
|
||
@Test | ||
void checkingAfterSuccessfulResponseSucceeds() throws IOException { | ||
try (MockedStatic<HttpClientBuilder> mockedStatic = mockStatic(HttpClientBuilder.class)) { | ||
final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); | ||
final HttpClientBuilder httpClientBuilder = mock(HttpClientBuilder.class, RETURNS_SELF); | ||
when(httpClientBuilder.build()).thenReturn(httpClient); | ||
mockedStatic.when(HttpClientBuilder::create).thenReturn(httpClientBuilder); | ||
|
||
final CloseableHttpResponse response = mock(CloseableHttpResponse.class); | ||
when(httpClient.execute(any())).thenReturn(response); | ||
|
||
final StatusLine statusLine = mock(StatusLine.class); | ||
when(statusLine.getStatusCode()).thenReturn(200); | ||
when(response.getStatusLine()).thenReturn(statusLine); | ||
|
||
assertTrue(httpPingChecker.check()); | ||
} | ||
} | ||
|
||
@Test | ||
void checkingAfterUnsuccessfulResponseFails() throws IOException { | ||
try (MockedStatic<HttpClientBuilder> mockedStatic = mockStatic(HttpClientBuilder.class)) { | ||
final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); | ||
final HttpClientBuilder httpClientBuilder = mock(HttpClientBuilder.class, RETURNS_SELF); | ||
when(httpClientBuilder.build()).thenReturn(httpClient); | ||
mockedStatic.when(HttpClientBuilder::create).thenReturn(httpClientBuilder); | ||
|
||
final CloseableHttpResponse response = mock(CloseableHttpResponse.class); | ||
when(httpClient.execute(any())).thenReturn(response); | ||
|
||
final StatusLine statusLine = mock(StatusLine.class); | ||
when(statusLine.getStatusCode()).thenReturn(500); | ||
when(response.getStatusLine()).thenReturn(statusLine); | ||
|
||
assertFalse(httpPingChecker.check()); | ||
} | ||
} | ||
|
||
@Test | ||
void checkingAfterIOExceptionFails() throws IOException { | ||
try (MockedStatic<HttpClientBuilder> mockedStatic = mockStatic(HttpClientBuilder.class)) { | ||
final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); | ||
final HttpClientBuilder httpClientBuilder = mock(HttpClientBuilder.class, RETURNS_SELF); | ||
when(httpClientBuilder.build()).thenReturn(httpClient); | ||
mockedStatic.when(HttpClientBuilder::create).thenReturn(httpClientBuilder); | ||
|
||
final IOException ioException = mock(IOException.class); | ||
final String message = "Error " + UUID.randomUUID(); | ||
when(ioException.getMessage()).thenReturn(message); | ||
when(httpClient.execute(any())).thenThrow(ioException); | ||
|
||
assertFalse(httpPingChecker.check()); | ||
verify(logger).debug(any(), eq(message), any()); | ||
} | ||
} | ||
} |