@Override public Payload create() throws Exception { sftp = acquire(sftpConnection); return Payloads.newInputStreamPayload( new CloseFtpChannelOnCloseInputStream( sftp.getSFTPEngine().open(path).getInputStream(), sftp)); }
private void assertCodeMakes( String method, URI uri, int statusCode, String message, String contentType, String content, Class<? extends Exception> expected) { ElasticStackErrorHandler function = Guice.createInjector().getInstance(ElasticStackErrorHandler.class); HttpCommand command = createMock(HttpCommand.class); HttpRequest request = new HttpRequest(method, uri); HttpResponse response = new HttpResponse( statusCode, message, Payloads.newInputStreamPayload(Strings2.toInputStream(content))); response.getPayload().getContentMetadata().setContentType(contentType); expect(command.getCurrentRequest()).andReturn(request).atLeastOnce(); command.setException(classEq(expected)); replay(command); function.handleError(command, response); verify(command); }
public void testApplyInputStreamDetails() throws UnknownHostException { InputStream is = getClass().getResourceAsStream("/metadata.json"); ParseMetadataFromJsonResponse parser = i.getInstance(ParseMetadataFromJsonResponse.class); Map<String, String> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); assertEquals(response, ImmutableMap.of("MD5", "IGPBYI1uC6+AJJxC4r5YBA==", "test", "1")); }
public void testApplyInputStreamDetails() throws UnknownHostException { InputStream is = getClass().getResourceAsStream("/test_list_backupschedule.json"); UnwrapOnlyJsonValue<BackupSchedule> parser = i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<BackupSchedule>>() {})); BackupSchedule response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); assertEquals( new BackupSchedule(WeeklyBackup.THURSDAY, DailyBackup.H_0400_0600, true), response); }
public void testPrivate() throws UnknownHostException { InputStream is = getClass().getResourceAsStream("/cloudservers/test_list_addresses_private.json"); UnwrapOnlyJsonValue<List<String>> parser = i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<List<String>>>() {})); List<String> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); assertEquals(response, ImmutableList.of("10.176.42.16")); }
@Test public void testApplyInputStreamDetails() throws UnknownHostException { InputStream is = getClass().getResourceAsStream("/test_error_handler.json"); ParseErrorFromJsonResponse parser = i.getInstance(ParseErrorFromJsonResponse.class); ErrorResponse response = Iterables.getOnlyElement( parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)))); assert "No object found that matches your input criteria.".equals(response.getMessage()); assert "IllegalArgumentException".equals(response.getErrorCode()); }
public Payload get(String path) { checkNotNull(path, "path"); ChannelSftp sftp = getSftp(); try { return Payloads.newInputStreamPayload( new CloseFtpChannelOnCloseInputStream(sftp.get(path), sftp)); } catch (SftpException e) { throw new SshException( String.format("%s@%s:%d: Error getting path: %s", username, host, port, path), e); } }
public void testApplyInputStream() { InputStream is = getClass().getResourceAsStream("/test_list_servers.json"); List<Server> expects = ImmutableList.of(new Server(1234, "sample-server"), new Server(5678, "sample-server2")); UnwrapOnlyJsonValue<List<Server>> parser = i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<List<Server>>>() {})); List<Server> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); assertEquals(response, expects); }
@Test public void testApplyInputStreamDetails() throws UnknownHostException { InputStream is = getClass().getResourceAsStream("/test_get_server_list.json"); ParseServerListFromJsonResponse parser = i.getInstance(ParseServerListFromJsonResponse.class); SortedSet<Server> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); Option dc = new Option(1l, "US-West-1", "US West 1 Datacenter"); Option centOs = new Option(13L, "CentOS 5.2 (32-bit)", "CentOS 5.2 (32-bit)"); Option webServer = new Option(1L, "Web Server", "Web or Application Server"); Server server = new Server( 75245L, dc, false, "PowerServer", "server to test the api. created by Alex", ServerState.ON, webServer, new Option(1L, "512MB", "Server with 512MB RAM"), centOs, new Ip( 1313079L, "204.51.240.178", "204.51.240.176/255.255.255.240", true, IpState.ASSIGNED, dc), new ServerImage( 1946L, "GSI-f8979644-e646-4711-ad58-d98a5fa3612c", "BitNami Gallery 2.3.1-0", "http://bitnami.org/stack/gallery", centOs, null, ServerImageType.WEB_APPLICATION_SERVER, ServerImageState.AVAILABLE, 0.0, "24732/GSI-f8979644-e646-4711-ad58-d98a5fa3612c.img", true, true, new Date(1261504577971L), new Date(1262649582180L), ImmutableSortedSet.of( new BillingToken(38L, "CentOS 5.2 32bit", 0.0), new BillingToken(56L, "BitNami: Gallery", 0.0)), new Customer(24732L, "BitRock"))); assertEquals(Iterables.getOnlyElement(response), server); }
public void testApplyInputStreamDetails() throws UnknownHostException { InputStream is = getClass().getResourceAsStream("/test_list_addresses.json"); UnwrapOnlyJsonValue<Addresses> parser = i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<Addresses>>() {})); Addresses response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); List<String> publicAddresses = ImmutableList.of("67.23.10.132", "67.23.10.131"); List<String> privateAddresses = ImmutableList.of("10.176.42.16"); assertEquals(response.getPublicAddresses(), publicAddresses); assertEquals(response.getPrivateAddresses(), privateAddresses); }
@Test void testClosesInputStream() throws InterruptedException, IOException, SecurityException, NoSuchMethodException { HttpCommand command = createCommand(); HttpResponse response = new HttpResponse(400, null, null); InputStream inputStream = new InputStream() { boolean isOpen = true; @Override public void close() { this.isOpen = false; } int count = 1; @Override public int read() throws IOException { if (this.isOpen) return (count > -1) ? count-- : -1; else return -1; } @Override public int available() throws IOException { if (this.isOpen) return count; else return 0; } }; response.setPayload(Payloads.newInputStreamPayload(inputStream)); response.getPayload().getContentMetadata().setContentLength(1l); assertEquals(response.getPayload().getInput().available(), 1); assertEquals(response.getPayload().getInput().read(), 1); handler.shouldRetryRequest(command, response); assertEquals(response.getPayload().getInput().available(), 0); assertEquals(response.getPayload().getInput().read(), -1); }
public void testApplyInputStreamDetails() throws UnknownHostException { InputStream is = getClass().getResourceAsStream("/test_list_servers_detail.json"); UnwrapOnlyJsonValue<List<Server>> parser = i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<List<Server>>>() {})); List<Server> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); assertEquals(response.get(0).getId(), 1234); assertEquals(response.get(0).getName(), "sample-server"); assertEquals(response.get(0).getImageId(), new Integer(2)); assertEquals(response.get(0).getFlavorId(), new Integer(1)); assertEquals(response.get(0).getHostId(), "e4d909c290d0fb1ca068ffaddf22cbd0"); assertEquals(response.get(0).getStatus(), ServerStatus.BUILD); assertEquals(response.get(0).getProgress(), new Integer(60)); List<String> publicAddresses = Lists.newArrayList("67.23.10.132", "67.23.10.131"); List<String> privateAddresses = Lists.newArrayList("10.176.42.16"); Addresses addresses1 = new Addresses(); addresses1.getPrivateAddresses().addAll(privateAddresses); addresses1.getPublicAddresses().addAll(publicAddresses); assertEquals(response.get(0).getAddresses(), addresses1); assertEquals( response.get(0).getMetadata(), ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")); assertEquals(response.get(1).getId(), 5678); assertEquals(response.get(1).getName(), "sample-server2"); assertEquals(response.get(1).getImageId(), new Integer(2)); assertEquals(response.get(1).getFlavorId(), new Integer(1)); assertEquals(response.get(1).getHostId(), "9e107d9d372bb6826bd81d3542a419d6"); assertEquals(response.get(1).getStatus(), ServerStatus.ACTIVE); assertEquals(response.get(1).getProgress(), null); List<String> publicAddresses2 = Lists.newArrayList("67.23.10.133"); List<String> privateAddresses2 = Lists.newArrayList("10.176.42.17"); Addresses addresses2 = new Addresses(); addresses2.getPrivateAddresses().addAll(privateAddresses2); addresses2.getPublicAddresses().addAll(publicAddresses2); assertEquals(response.get(1).getAddresses(), addresses2); assertEquals(response.get(1).getMetadata(), ImmutableMap.of("Server Label", "DB 1")); }
public HttpResponse apply(Response nativeResponse) { InputStream in = null; try { in = BaseHttpCommandExecutorService.consumeOnClose(nativeResponse.getResponseBodyAsStream()); } catch (IOException e) { Closeables.closeQuietly(in); propagate(e); assert false : "should have propagated exception"; } Payload payload = in != null ? Payloads.newInputStreamPayload(in) : null; HttpResponse response = new HttpResponse(nativeResponse.getStatusCode(), nativeResponse.getStatusText(), payload); Multimap<String, String> headers = LinkedHashMultimap.create(); for (Entry<String, List<String>> header : nativeResponse.getHeaders()) { headers.putAll(header.getKey(), header.getValue()); } utils.setPayloadPropertiesFromHeaders(headers, response); return response; }
private void assertCodeMakes( String method, URI uri, int statusCode, String message, String contentType, String content, Class<? extends Exception> expected) { ParseAzureBlobErrorFromXmlContent function = Guice.createInjector( new SaxParserModule(), new AbstractModule() { @Override protected void configure() { bind(SharedKeyLiteAuthentication.class) .toInstance(createMock(SharedKeyLiteAuthentication.class)); } }) .getInstance(ParseAzureBlobErrorFromXmlContent.class); HttpCommand command = createMock(HttpCommand.class); HttpRequest request = new HttpRequest(method, uri); HttpResponse response = new HttpResponse( statusCode, message, Payloads.newInputStreamPayload(Strings2.toInputStream(content))); response.getPayload().getContentMetadata().setContentType(contentType); expect(command.getCurrentRequest()).andReturn(request).atLeastOnce(); command.setException(classEq(expected)); replay(command); function.handleError(command, response); verify(command); }