Search in sources :

Example 1 with WireMockRuntimeInfo

use of com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo in project sandbox by backpaper0.

the class HelloWireMockTest method test.

@Test
void test(WireMockRuntimeInfo wireMockRuntimeInfo) throws Exception {
    WireMock wireMock = wireMockRuntimeInfo.getWireMock();
    wireMock.register(get("/hello").willReturn(ok().withBody("Hello, world!")));
    int port = wireMockRuntimeInfo.getHttpPort();
    HttpClient client = HttpClient.newHttpClient();
    HttpResponse<String> response = client.send(HttpRequest.newBuilder(URI.create("http://localhost:" + port + "/hello")).build(), BodyHandlers.ofString());
    assertEquals(200, response.statusCode());
    assertEquals("Hello, world!", response.body());
}
Also used : HttpClient(java.net.http.HttpClient) WireMock(com.github.tomakehurst.wiremock.client.WireMock) Test(org.junit.jupiter.api.Test) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest)

Example 2 with WireMockRuntimeInfo

use of com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo in project concord by walmartlabs.

the class SecretClientTest method testInvalidSecretType.

@Test
public void testInvalidSecretType(WireMockRuntimeInfo wmRuntimeInfo) throws Exception {
    String orgName = "org_" + System.currentTimeMillis();
    String secretName = "secret_" + System.currentTimeMillis();
    stubFor(post(urlEqualTo("/api/v1/org/" + orgName + "/secret/" + secretName + "/data")).willReturn(aResponse().withStatus(200).withHeader(Constants.Headers.SECRET_TYPE, SecretEntry.TypeEnum.DATA.name()).withBody("Hello!")));
    ApiClient apiClient = new ConcordApiClient("http://localhost:" + wmRuntimeInfo.getHttpPort());
    SecretClient secretClient = new SecretClient(apiClient);
    try {
        secretClient.getData(orgName, secretName, null, SecretEntry.TypeEnum.KEY_PAIR);
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().contains("Unexpected type of " + orgName + "/" + secretName));
    }
}
Also used : ApiClient(com.walmartlabs.concord.ApiClient) Test(org.junit.jupiter.api.Test) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest)

Example 3 with WireMockRuntimeInfo

use of com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo in project comet-java-sdk by comet-ml.

the class ConnectionTest method testDownloadAsync_onCometApiException.

@Test
public void testDownloadAsync_onCometApiException(@NonNull WireMockRuntimeInfo wmRuntimeInfo) throws IOException {
    // create test error response
    // 
    stubFor(get(urlPathEqualTo(SOME_ENDPOINT)).willReturn(aResponse().withStatus(404)));
    // execute request and check results
    // 
    File downloadFile = Files.createTempFile(null, null).toFile();
    downloadFile.deleteOnExit();
    Connection connection = new Connection(wmRuntimeInfo.getHttpBaseUrl(), TEST_API_KEY, MAX_AUTH_RETRIES_DEFAULT, logger);
    ListenableFuture<Response> responseListenableFuture = connection.downloadAsync(downloadFile, SOME_ENDPOINT, SOME_PARAMS);
    assertNotNull(responseListenableFuture, "future expected");
    // wait for result
    CompletableFuture<Response> completableFuture = responseListenableFuture.toCompletableFuture();
    Exception exception = assertThrows(ExecutionException.class, () -> completableFuture.get(5, TimeUnit.SECONDS));
    // check that inventory was fully processed
    assertEquals(0, connection.getRequestsInventory().get(), "inventory must be empty");
    // check that correct exception returned
    assertTrue(exception.getCause() instanceof CometApiException, "wrong exception returned");
}
Also used : WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) Response(org.asynchttpclient.Response) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) CometWebJavaSdkException(ml.comet.experiment.impl.rest.CometWebJavaSdkException) ExecutionException(java.util.concurrent.ExecutionException) CometApiException(ml.comet.experiment.exception.CometApiException) CometApiException(ml.comet.experiment.exception.CometApiException) Test(org.junit.jupiter.api.Test) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest)

Example 4 with WireMockRuntimeInfo

use of com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo in project comet-java-sdk by comet-ml.

the class ConnectionTest method testSendPostWithRetries_throwOnFailure_onCometWebException.

/**
 * Tests that correct exception raised when Comet web error code received and max retry attempts exceeded
 * with throwOnFailure set to false.
 */
@Test
public void testSendPostWithRetries_throwOnFailure_onCometWebException(@NonNull WireMockRuntimeInfo wmRuntimeInfo) {
    // create test error response
    // 
    stubFor(post(urlPathEqualTo(SOME_ENDPOINT)).willReturn(aResponse().withBody(JsonUtils.toJson(COMET_WEB_JAVA_SDK_EXCEPTION)).withStatus(BAD_REQUEST_ERROR_CODE).withHeader(CONTENT_TYPE.toString(), APPLICATION_JSON.toString())));
    // execute request and check results
    // 
    String baseUrl = wmRuntimeInfo.getHttpBaseUrl();
    Connection connection = new Connection(baseUrl, TEST_API_KEY, MAX_AUTH_RETRIES_DEFAULT, logger);
    CometApiException cometApiException = assertThrows(CometApiException.class, () -> connection.sendPostWithRetries(SOME_REQUEST_STRING, SOME_ENDPOINT, true));
    // check that inventory was fully processed
    assertEquals(0, connection.getRequestsInventory().get(), "inventory must be empty");
    // check that correct exception returned
    checkWebJavaSdkException(cometApiException);
}
Also used : CometApiException(ml.comet.experiment.exception.CometApiException) Test(org.junit.jupiter.api.Test) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest)

Example 5 with WireMockRuntimeInfo

use of com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo in project comet-java-sdk by comet-ml.

the class ConnectionTest method testSendPostAsync.

@Test
public void testSendPostAsync(@NonNull WireMockRuntimeInfo wmRuntimeInfo) {
    // create test HTTP stub
    // 
    stubFor(post(urlPathEqualTo(SOME_ENDPOINT)).willReturn(ok(SOME_JSON_RESPONSE).withHeader(CONTENT_TYPE.toString(), APPLICATION_JSON.toString())));
    // execute request and check results
    // 
    String baseUrl = wmRuntimeInfo.getHttpBaseUrl();
    Connection connection = new Connection(baseUrl, TEST_API_KEY, MAX_AUTH_RETRIES_DEFAULT, logger);
    ListenableFuture<Response> responseListenableFuture = connection.sendPostAsync(SOME_REQUEST_STRING, SOME_ENDPOINT);
    assertNotNull(responseListenableFuture, "future expected");
    // wait for result
    CompletableFuture<Response> completableFuture = responseListenableFuture.toCompletableFuture();
    assertDoesNotThrow(() -> {
        Response response = completableFuture.exceptionally(throwable -> fail("response failed", throwable)).get(5, TimeUnit.SECONDS);
        assertEquals(SOME_JSON_RESPONSE, response.getResponseBody(), "wrong response body");
    });
    // check that inventory was fully processed
    assertEquals(0, connection.getRequestsInventory().get(), "inventory must be empty");
}
Also used : WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) Response(org.asynchttpclient.Response) OVERWRITE(ml.comet.experiment.impl.constants.QueryParamName.OVERWRITE) LoggerFactory(org.slf4j.LoggerFactory) EXPERIMENT_KEY(ml.comet.experiment.impl.constants.QueryParamName.EXPERIMENT_KEY) WireMock.notFound(com.github.tomakehurst.wiremock.client.WireMock.notFound) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) WireMock.post(com.github.tomakehurst.wiremock.client.WireMock.post) QueryParamName(ml.comet.experiment.impl.constants.QueryParamName) NonNull(lombok.NonNull) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) WireMock.unauthorized(com.github.tomakehurst.wiremock.client.WireMock.unauthorized) UUID(java.util.UUID) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) StringValuePattern(com.github.tomakehurst.wiremock.matching.StringValuePattern) APPLICATION_JSON(io.netty.handler.codec.http.HttpHeaderValues.APPLICATION_JSON) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) WireMock.getRequestedFor(com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor) CONTENT_TYPE(io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE) Assertions.assertDoesNotThrow(org.junit.jupiter.api.Assertions.assertDoesNotThrow) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) WireMock.badRequest(com.github.tomakehurst.wiremock.client.WireMock.badRequest) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) WireMock.postRequestedFor(com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor) WireMock.ok(com.github.tomakehurst.wiremock.client.WireMock.ok) PosixFilePermissions(java.nio.file.attribute.PosixFilePermissions) ListenableFuture(org.asynchttpclient.ListenableFuture) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SdkErrorCodes(ml.comet.experiment.impl.constants.SdkErrorCodes) COMET_SDK_API_HEADER(ml.comet.experiment.impl.http.Connection.COMET_SDK_API_HEADER) WireMock.get(com.github.tomakehurst.wiremock.client.WireMock.get) WireMock.urlPathEqualTo(com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo) WireMock.equalTo(com.github.tomakehurst.wiremock.client.WireMock.equalTo) Logger(org.slf4j.Logger) Files(java.nio.file.Files) WireMockRuntimeInfo(com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Response(org.asynchttpclient.Response) WireMock.verify(com.github.tomakehurst.wiremock.client.WireMock.verify) CometWebJavaSdkException(ml.comet.experiment.impl.rest.CometWebJavaSdkException) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) JsonUtils(ml.comet.experiment.impl.utils.JsonUtils) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest) TestUtils(ml.comet.experiment.impl.TestUtils) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) APPLICATION_OCTET_STREAM(io.netty.handler.codec.http.HttpHeaderValues.APPLICATION_OCTET_STREAM) CometApiException(ml.comet.experiment.exception.CometApiException) Test(org.junit.jupiter.api.Test) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest)

Aggregations

WireMockTest (com.github.tomakehurst.wiremock.junit5.WireMockTest)23 Test (org.junit.jupiter.api.Test)23 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)16 WireMock.stubFor (com.github.tomakehurst.wiremock.client.WireMock.stubFor)11 WireMockRuntimeInfo (com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo)11 CometApiException (ml.comet.experiment.exception.CometApiException)11 WireMock.any (com.github.tomakehurst.wiremock.client.WireMock.any)8 WireMock.anyUrl (com.github.tomakehurst.wiremock.client.WireMock.anyUrl)8 BufferedReader (java.io.BufferedReader)8 FileNotFoundException (java.io.FileNotFoundException)8 IOException (java.io.IOException)8 InputStream (java.io.InputStream)8 InputStreamReader (java.io.InputStreamReader)8 URI (java.net.URI)8 StandardCharsets (java.nio.charset.StandardCharsets)8 ExecutionException (java.util.concurrent.ExecutionException)8 Function (java.util.function.Function)8 Collectors (java.util.stream.Collectors)8 CometWebJavaSdkException (ml.comet.experiment.impl.rest.CometWebJavaSdkException)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8