use of co.cask.cdap.proto.StreamProperties in project cdap by caskdata.
the class StreamHandlerTest method verifyUpdateOwnerFailure.
private void verifyUpdateOwnerFailure(String streamName, @Nullable String ownerPrincipal) throws IOException, URISyntaxException {
StreamProperties newProps = new StreamProperties(1L, null, null, null, ownerPrincipal);
HttpURLConnection urlConn = openURL(createPropertiesURL(streamName), HttpMethod.PUT);
urlConn.setDoOutput(true);
urlConn.getOutputStream().write(GSON.toJson(newProps).getBytes(Charsets.UTF_8));
Assert.assertEquals(HttpResponseStatus.FORBIDDEN.getCode(), urlConn.getResponseCode());
urlConn.disconnect();
}
use of co.cask.cdap.proto.StreamProperties in project cdap by caskdata.
the class StreamHandlerTest method testUpdateDescription.
@Test
public void testUpdateDescription() throws Exception {
// Create a stream with some ttl and description
String desc = "large stream";
HttpURLConnection urlConn = openURL(createURL("streams/stream1"), HttpMethod.PUT);
urlConn.setDoOutput(true);
Schema schema = Schema.recordOf("event", Schema.Field.of("purchase", Schema.of(Schema.Type.STRING)));
FormatSpecification formatSpecification = new FormatSpecification(TextRecordFormat.class.getCanonicalName(), schema, ImmutableMap.of(TextRecordFormat.CHARSET, "utf8"));
StreamProperties properties = new StreamProperties(1L, formatSpecification, 128, desc, null);
urlConn.getOutputStream().write(GSON.toJson(properties).getBytes(Charsets.UTF_8));
Assert.assertEquals(HttpResponseStatus.OK.getCode(), urlConn.getResponseCode());
urlConn.disconnect();
// Check whether ttl and description got persisted
urlConn = openURL(createStreamInfoURL("stream1"), HttpMethod.GET);
Assert.assertEquals(HttpResponseStatus.OK.getCode(), urlConn.getResponseCode());
StreamProperties actual = GSON.fromJson(new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8), StreamProperties.class);
urlConn.disconnect();
Assert.assertEquals(properties, actual);
// Update desc and ttl and check whether the changes were persisted
StreamProperties newProps = new StreamProperties(2L, null, null, "small stream", null);
urlConn = openURL(createPropertiesURL("stream1"), HttpMethod.PUT);
urlConn.setDoOutput(true);
urlConn.getOutputStream().write(GSON.toJson(newProps).getBytes(Charsets.UTF_8));
Assert.assertEquals(HttpResponseStatus.OK.getCode(), urlConn.getResponseCode());
urlConn.disconnect();
urlConn = openURL(createStreamInfoURL("stream1"), HttpMethod.GET);
Assert.assertEquals(HttpResponseStatus.OK.getCode(), urlConn.getResponseCode());
actual = GSON.fromJson(new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8), StreamProperties.class);
urlConn.disconnect();
StreamProperties expected = new StreamProperties(newProps.getTTL(), properties.getFormat(), properties.getNotificationThresholdMB(), newProps.getDescription(), properties.getOwnerPrincipal());
Assert.assertEquals(expected, actual);
}
use of co.cask.cdap.proto.StreamProperties in project cdap by caskdata.
the class StreamHandlerTest method testPutInvalidStreamConfig.
@Test
public void testPutInvalidStreamConfig() throws Exception {
// create the new stream.
HttpURLConnection urlConn = openURL(createURL("streams/stream_badconf"), HttpMethod.PUT);
Assert.assertEquals(HttpResponseStatus.OK.getCode(), urlConn.getResponseCode());
urlConn.disconnect();
// put a config with invalid json
urlConn = openURL(createPropertiesURL("stream_badconf"), HttpMethod.PUT);
urlConn.setDoOutput(true);
urlConn.getOutputStream().write("ttl:2".getBytes(Charsets.UTF_8));
Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), urlConn.getResponseCode());
urlConn.disconnect();
// put a config with an invalid TTL
urlConn = openURL(createPropertiesURL("stream_badconf"), HttpMethod.PUT);
urlConn.setDoOutput(true);
StreamProperties streamProperties = new StreamProperties(-1L, null, 20);
urlConn.getOutputStream().write(GSON.toJson(streamProperties).getBytes(Charsets.UTF_8));
Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), urlConn.getResponseCode());
urlConn.disconnect();
// put a config with a format without a format class
urlConn = openURL(createPropertiesURL("stream_badconf"), HttpMethod.PUT);
urlConn.setDoOutput(true);
FormatSpecification formatSpec = new FormatSpecification(null, null, null);
streamProperties = new StreamProperties(2L, formatSpec, 20);
urlConn.getOutputStream().write(GSON.toJson(streamProperties).getBytes(Charsets.UTF_8));
Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), urlConn.getResponseCode());
urlConn.disconnect();
// put a config with a format with a bad format class
urlConn = openURL(createPropertiesURL("stream_badconf"), HttpMethod.PUT);
urlConn.setDoOutput(true);
formatSpec = new FormatSpecification("gibberish", null, null);
streamProperties = new StreamProperties(2L, formatSpec, 20);
urlConn.getOutputStream().write(GSON.toJson(streamProperties).getBytes(Charsets.UTF_8));
Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), urlConn.getResponseCode());
urlConn.disconnect();
// put a config with an incompatible format and schema
urlConn = openURL(createPropertiesURL("stream_badconf"), HttpMethod.PUT);
urlConn.setDoOutput(true);
Schema schema = Schema.recordOf("event", Schema.Field.of("col", Schema.of(Schema.Type.DOUBLE)));
formatSpec = new FormatSpecification(TextRecordFormat.class.getCanonicalName(), schema, null);
streamProperties = new StreamProperties(2L, formatSpec, 20);
urlConn.getOutputStream().write(GSON.toJson(streamProperties).getBytes(Charsets.UTF_8));
Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), urlConn.getResponseCode());
urlConn.disconnect();
// put a config with a bad threshold
urlConn = openURL(createPropertiesURL("stream_badconf"), HttpMethod.PUT);
urlConn.setDoOutput(true);
streamProperties = new StreamProperties(2L, null, -20);
urlConn.getOutputStream().write(GSON.toJson(streamProperties).getBytes(Charsets.UTF_8));
Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), urlConn.getResponseCode());
urlConn.disconnect();
}
use of co.cask.cdap.proto.StreamProperties in project cdap by caskdata.
the class StreamHandlerTest method testStreamInfo.
@Test
public void testStreamInfo() throws Exception {
// Now, create the new stream.
HttpURLConnection urlConn = openURL(createURL("streams/stream_info"), HttpMethod.PUT);
Assert.assertEquals(HttpResponseStatus.OK.getCode(), urlConn.getResponseCode());
urlConn.disconnect();
// put a new config
urlConn = openURL(createPropertiesURL("stream_info"), HttpMethod.PUT);
urlConn.setDoOutput(true);
Schema schema = Schema.recordOf("event", Schema.Field.of("purchase", Schema.of(Schema.Type.STRING)));
FormatSpecification formatSpecification;
formatSpecification = new FormatSpecification(TextRecordFormat.class.getCanonicalName(), schema, ImmutableMap.of(TextRecordFormat.CHARSET, "utf8"));
StreamProperties streamProperties = new StreamProperties(2L, formatSpecification, 20);
urlConn.getOutputStream().write(GSON.toJson(streamProperties).getBytes(Charsets.UTF_8));
Assert.assertEquals(HttpResponseStatus.OK.getCode(), urlConn.getResponseCode());
urlConn.disconnect();
// test the config ttl by calling info
urlConn = openURL(createStreamInfoURL("stream_info"), HttpMethod.GET);
Assert.assertEquals(HttpResponseStatus.OK.getCode(), urlConn.getResponseCode());
StreamProperties actual = GSON.fromJson(new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8), StreamProperties.class);
urlConn.disconnect();
Assert.assertEquals(streamProperties, actual);
}
use of co.cask.cdap.proto.StreamProperties in project cdap by caskdata.
the class StreamAdminTest method testConfigAndTruncate.
@Test
public void testConfigAndTruncate() throws Exception {
StreamAdmin streamAdmin = getStreamAdmin();
grantAndAssertSuccess(FOO_NAMESPACE, USER, ImmutableSet.of(Action.WRITE));
StreamId stream = FOO_NAMESPACE.stream("stream");
streamAdmin.create(stream);
Assert.assertTrue(streamAdmin.exists(stream));
writeEvent(stream);
// Getting config / properties should work
streamAdmin.getConfig(stream);
streamAdmin.getProperties(stream);
// Now revoke access to the user to the stream and to the namespace
revokeAndAssertSuccess(FOO_NAMESPACE, USER, ImmutableSet.of(Action.WRITE));
revokeAndAssertSuccess(stream, USER, EnumSet.allOf(Action.class));
streamAdmin.getConfig(stream);
try {
streamAdmin.getProperties(stream);
Assert.fail("User should not be able to get the properties.");
} catch (UnauthorizedException e) {
// expected
}
// read action should be enough to get the stream config
grantAndAssertSuccess(stream, USER, ImmutableSet.of(Action.READ));
streamAdmin.getConfig(stream);
StreamProperties properties = streamAdmin.getProperties(stream);
try {
streamAdmin.updateConfig(stream, properties);
Assert.fail("User should not be able to update the config with just READ permissions.");
} catch (UnauthorizedException e) {
// expected
}
// This call bypasses the stream handler and thus authorization is not checked for this call and so write
// to stream will succeed. It is done so that we can check and perform truncate call.
writeEvent(stream);
grantAndAssertSuccess(stream, USER, ImmutableSet.of(Action.WRITE));
writeEvent(stream);
try {
streamAdmin.updateConfig(stream, properties);
Assert.fail("User should not be able to update the config with just READ and WRITE permissions.");
} catch (UnauthorizedException e) {
// expected
}
try {
streamAdmin.truncate(stream);
Assert.fail("User should not be able to truncate the stream without ADMIN permission.");
} catch (UnauthorizedException e) {
// expected
}
try {
streamAdmin.drop(stream);
Assert.fail("User should not be able to drop the stream without ADMIN permission.");
} catch (UnauthorizedException e) {
// expdcted
}
grantAndAssertSuccess(stream, USER, ImmutableSet.of(Action.ADMIN));
streamAdmin.updateConfig(stream, properties);
streamAdmin.truncate(stream);
Assert.assertEquals(0, getStreamSize(stream));
streamAdmin.drop(stream);
}
Aggregations