Search in sources :

Example 16 with StreamProperties

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();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) StreamProperties(co.cask.cdap.proto.StreamProperties)

Example 17 with StreamProperties

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);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TextRecordFormat(co.cask.cdap.format.TextRecordFormat) Schema(co.cask.cdap.api.data.schema.Schema) FormatSpecification(co.cask.cdap.api.data.format.FormatSpecification) StreamProperties(co.cask.cdap.proto.StreamProperties) Test(org.junit.Test)

Example 18 with StreamProperties

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();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Schema(co.cask.cdap.api.data.schema.Schema) StreamProperties(co.cask.cdap.proto.StreamProperties) FormatSpecification(co.cask.cdap.api.data.format.FormatSpecification) Test(org.junit.Test)

Example 19 with StreamProperties

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);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Schema(co.cask.cdap.api.data.schema.Schema) FormatSpecification(co.cask.cdap.api.data.format.FormatSpecification) StreamProperties(co.cask.cdap.proto.StreamProperties) Test(org.junit.Test)

Example 20 with StreamProperties

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);
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) Action(co.cask.cdap.proto.security.Action) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) StreamProperties(co.cask.cdap.proto.StreamProperties) Test(org.junit.Test)

Aggregations

StreamProperties (co.cask.cdap.proto.StreamProperties)31 StreamId (co.cask.cdap.proto.id.StreamId)17 Test (org.junit.Test)14 FormatSpecification (co.cask.cdap.api.data.format.FormatSpecification)11 Schema (co.cask.cdap.api.data.schema.Schema)8 HttpURLConnection (java.net.HttpURLConnection)7 IOException (java.io.IOException)5 UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)4 NotFoundException (co.cask.cdap.common.NotFoundException)3 CoordinatorStreamProperties (co.cask.cdap.data.stream.CoordinatorStreamProperties)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 Reader (java.io.Reader)3 Path (javax.ws.rs.Path)3 StreamNotFoundException (co.cask.cdap.common.StreamNotFoundException)2 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)2 ExploreExecutionResult (co.cask.cdap.explore.client.ExploreExecutionResult)2 NotificationFeedException (co.cask.cdap.notifications.feeds.NotificationFeedException)2 ColumnDesc (co.cask.cdap.proto.ColumnDesc)2 ProgramId (co.cask.cdap.proto.id.ProgramId)2