Search in sources :

Example 21 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class TetheringClientHandlerTest method testTetherStatus.

@Test
public void testTetherStatus() throws IOException, InterruptedException {
    // Tethering does not exist, should return 404.
    HttpRequest request = HttpRequest.builder(HttpMethod.GET, clientConfig.resolveURL("tethering/connections/" + SERVER_INSTANCE)).build();
    HttpResponse response = HttpRequests.execute(request);
    Assert.assertEquals(HttpResponseStatus.NOT_FOUND.code(), response.getResponseCode());
    // Server returns 404 before tethering has been accepted or rejected by admin on server side.
    serverHandler.setResponseStatus(HttpResponseStatus.NOT_FOUND);
    // Client initiate tethering with the server
    createTethering(SERVER_INSTANCE, PROJECT, LOCATION, NAMESPACES, REQUEST_TIME, DESCRIPTION);
    // Tethering status for the peer should be returned.
    request = HttpRequest.builder(HttpMethod.GET, clientConfig.resolveURL("tethering/connections/" + SERVER_INSTANCE)).build();
    response = HttpRequests.execute(request);
    Assert.assertEquals(HttpResponseStatus.OK.code(), response.getResponseCode());
    PeerState peerState = GSON.fromJson(response.getResponseBodyAsString(), PeerState.class);
    Assert.assertEquals(SERVER_INSTANCE, peerState.getName());
    Assert.assertEquals(TetheringStatus.PENDING, peerState.getTetheringStatus());
    Assert.assertTrue(peerState.isActive());
    Assert.assertEquals(serverConfig.getConnectionConfig().getURI().toString(), peerState.getEndpoint());
    Assert.assertEquals(PROJECT, peerState.getMetadata().getMetadata().get("project"));
    Assert.assertEquals(LOCATION, peerState.getMetadata().getMetadata().get("location"));
    Assert.assertEquals(NAMESPACES, peerState.getMetadata().getNamespaceAllocations());
    // cleanup
    deleteTethering(SERVER_INSTANCE);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) Test(org.junit.Test)

Example 22 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class TetheringClientHandlerTest method waitForTetheringStatus.

private void waitForTetheringStatus(TetheringStatus tetheringStatus, String instanceName, String endpoint, String project, String location, List<NamespaceAllocation> namespaces, @Nullable String description, boolean expectActive) throws IOException, InterruptedException {
    List<PeerState> peers = new ArrayList<>();
    for (int retry = 0; retry < 5; ++retry) {
        HttpRequest request = HttpRequest.builder(HttpMethod.GET, clientConfig.resolveURL("tethering/connections")).build();
        HttpResponse response = HttpRequests.execute(request);
        Assert.assertEquals(HttpResponseStatus.OK.code(), response.getResponseCode());
        Type type = new TypeToken<List<PeerState>>() {
        }.getType();
        peers = GSON.fromJson(response.getResponseBodyAsString(), type);
        Assert.assertEquals(1, peers.size());
        if (peers.get(0).getTetheringStatus() == tetheringStatus && peers.get(0).isActive() == expectActive) {
            break;
        }
        Thread.sleep(500);
    }
    Assert.assertEquals(1, peers.size());
    PeerState peer = peers.get(0);
    Assert.assertEquals(tetheringStatus, peer.getTetheringStatus());
    Assert.assertEquals(instanceName, peer.getName());
    Assert.assertEquals(endpoint, peer.getEndpoint());
    Assert.assertEquals(project, peer.getMetadata().getMetadata().get("project"));
    Assert.assertEquals(location, peer.getMetadata().getMetadata().get("location"));
    Assert.assertEquals(namespaces, peer.getMetadata().getNamespaceAllocations());
    Assert.assertEquals(description, peer.getMetadata().getDescription());
    Assert.assertEquals(expectActive, peer.isActive());
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) Type(java.lang.reflect.Type) ArrayList(java.util.ArrayList) HttpResponse(io.cdap.common.http.HttpResponse) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList)

Example 23 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class TetheringClientHandlerTest method createTethering.

private void createTethering(String instance, String project, String location, List<NamespaceAllocation> namespaceAllocations, long requestTime, @Nullable String description, TetheringStatus expectedTetheringStatus, HttpResponseStatus expectedResponseStatus) throws IOException, InterruptedException {
    // Send tethering request
    Map<String, String> metadata = ImmutableMap.of("project", project, "location", location);
    TetheringCreationRequest tetheringRequest = new TetheringCreationRequest(instance, serverConfig.getConnectionConfig().getURI().toString(), namespaceAllocations, metadata, description);
    HttpRequest request = HttpRequest.builder(HttpMethod.PUT, clientConfig.resolveURL("tethering/create")).withBody(GSON.toJson(tetheringRequest)).build();
    HttpResponse response = HttpRequests.execute(request);
    Assert.assertEquals(expectedResponseStatus.code(), response.getResponseCode());
    waitForTetheringStatus(expectedTetheringStatus, tetheringRequest.getPeer(), tetheringRequest.getEndpoint(), project, location, namespaceAllocations, tetheringRequest.getDescription(), true);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse)

Example 24 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class ArtifactCacheServiceTest method getArtifactBytes.

private byte[] getArtifactBytes(String peerName) throws IOException, URISyntaxException {
    URL url = getURL(peerName);
    HttpRequest httpRequest = HttpRequest.builder(HttpMethod.GET, url).build();
    HttpResponse httpResponse = HttpRequests.execute(httpRequest);
    Assert.assertEquals(HttpURLConnection.HTTP_OK, httpResponse.getResponseCode());
    return httpResponse.getResponseBody();
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Example 25 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class ArtifactCacheServiceTest method testFetchArtifactUnknownPeer.

@Test
public void testFetchArtifactUnknownPeer() throws Exception {
    URL url = getURL("unknownpeer");
    HttpRequest httpRequest = HttpRequest.builder(HttpMethod.GET, url).build();
    HttpResponse httpResponse = HttpRequests.execute(httpRequest);
    Assert.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, httpResponse.getResponseCode());
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL) Test(org.junit.Test) TaskWorkerServiceTest(io.cdap.cdap.internal.app.worker.TaskWorkerServiceTest)

Aggregations

HttpRequest (io.cdap.common.http.HttpRequest)124 HttpResponse (io.cdap.common.http.HttpResponse)92 URL (java.net.URL)81 Test (org.junit.Test)33 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)14 IOException (java.io.IOException)14 AccessToken (io.cdap.cdap.security.authentication.client.AccessToken)13 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)10 BadRequestException (io.cdap.cdap.common.BadRequestException)9 NotFoundException (io.cdap.cdap.common.NotFoundException)8 TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)5 ServiceManager (io.cdap.cdap.test.ServiceManager)5 TypeToken (com.google.gson.reflect.TypeToken)4 KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)4 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)4 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)4 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)4 TopicId (io.cdap.cdap.proto.id.TopicId)4 ApplicationManager (io.cdap.cdap.test.ApplicationManager)4 ByteBuffer (java.nio.ByteBuffer)4