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);
}
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());
}
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);
}
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();
}
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());
}
Aggregations