From c3328e74d8d17b4bb4d28656a1e944a02ad90044 Mon Sep 17 00:00:00 2001 From: Joris Borgdorff Date: Wed, 10 Jun 2020 16:25:20 +0200 Subject: [PATCH] Removed deprecated calls --- .../java/org/radarbase/mock/MockProducer.java | 10 ++--- .../mock/data/MockRecordValidatorTest.java | 39 ++++++++++--------- .../producer/rest/RestTopicSender.java | 1 - .../producer/rest/SchemaRetrieverTest.java | 27 +++++-------- 4 files changed, 35 insertions(+), 42 deletions(-) diff --git a/radar-commons-testing/src/main/java/org/radarbase/mock/MockProducer.java b/radar-commons-testing/src/main/java/org/radarbase/mock/MockProducer.java index adf9f77f..36d1fc0f 100644 --- a/radar-commons-testing/src/main/java/org/radarbase/mock/MockProducer.java +++ b/radar-commons-testing/src/main/java/org/radarbase/mock/MockProducer.java @@ -16,7 +16,7 @@ package org.radarbase.mock; -import static io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG; +import static io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG; import static org.apache.kafka.clients.producer.ProducerConfig.BOOTSTRAP_SERVERS_CONFIG; import static org.apache.kafka.clients.producer.ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG; import static org.apache.kafka.clients.producer.ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG; @@ -193,7 +193,7 @@ private List createRestSenders(int numDevices, /** Start sending data. */ public void start() throws IOException { - for (MockDevice device : devices) { + for (MockDevice device : devices) { device.start(); } for (MockFileSender file : files) { @@ -205,11 +205,11 @@ public void start() throws IOException { public void shutdown() throws IOException, InterruptedException, SchemaValidationException { if (!devices.isEmpty()) { logger.info("Shutting down mock devices"); - for (MockDevice device : devices) { + for (MockDevice device : devices) { device.shutdown(); } logger.info("Waiting for mock devices to finish..."); - for (MockDevice device : devices) { + for (MockDevice device : devices) { device.join(5_000L); } } @@ -218,7 +218,7 @@ public void shutdown() throws IOException, InterruptedException, SchemaValidatio sender.close(); } - for (MockDevice device : devices) { + for (MockDevice device : devices) { device.checkException(); } } diff --git a/radar-commons-testing/src/test/java/org/radarbase/mock/data/MockRecordValidatorTest.java b/radar-commons-testing/src/test/java/org/radarbase/mock/data/MockRecordValidatorTest.java index 1204cf64..43308b78 100644 --- a/radar-commons-testing/src/test/java/org/radarbase/mock/data/MockRecordValidatorTest.java +++ b/radar-commons-testing/src/test/java/org/radarbase/mock/data/MockRecordValidatorTest.java @@ -16,6 +16,8 @@ package org.radarbase.mock.data; +import static org.junit.Assert.assertThrows; + import java.io.IOException; import java.io.Writer; import java.nio.file.Files; @@ -34,8 +36,6 @@ public class MockRecordValidatorTest { @Rule public TemporaryFolder folder = new TemporaryFolder(); - @Rule - public ExpectedException exception = ExpectedException.none(); private Path root; private MockDataConfig makeConfig() throws IOException { @@ -63,7 +63,6 @@ public void validate() throws Exception { MockDataConfig config = makeConfig(); generator.generate(config, 100_000L, root); - // doesn't throw new MockRecordValidator(config, 100_000L, root).validate(); } @@ -74,8 +73,7 @@ public void validateWrongDuration() throws Exception { MockDataConfig config = makeConfig(); generator.generate(config, 100_000L, root); - exception.expect(IllegalArgumentException.class); - new MockRecordValidator(config, 10_000L, root).validate(); + assertValidateThrows(IllegalArgumentException.class, config); } @Test @@ -88,7 +86,7 @@ public void validateCustom() throws Exception { writer.append("test,a,b,1,2,1\n"); } - new MockRecordValidator(config, 2_000L, root).validate(); + assertValidate(config); } @Test @@ -101,8 +99,7 @@ public void validateWrongKey() throws Exception { writer.append("test,a,c,1,2,1\n"); } - exception.expect(IllegalArgumentException.class); - new MockRecordValidator(config, 2_000L, root).validate(); + assertValidateThrows(IllegalArgumentException.class, config); } @Test @@ -115,8 +112,7 @@ public void validateWrongTime() throws Exception { writer.append("test,a,b,1,0,1\n"); } - exception.expect(IllegalArgumentException.class); - new MockRecordValidator(config, 2_000L, root).validate(); + assertValidateThrows(IllegalArgumentException.class, config); } @@ -130,8 +126,7 @@ public void validateMissingKeyField() throws Exception { writer.append("test,a,1,2,1\n"); } - exception.expect(NullPointerException.class); - new MockRecordValidator(config, 2_000L, root).validate(); + assertValidateThrows(NullPointerException.class, config); } @Test @@ -144,8 +139,7 @@ public void validateMissingValueField() throws Exception { writer.append("test,a,b,1,2\n"); } - exception.expect(NullPointerException.class); - new MockRecordValidator(config, 2_000L, root).validate(); + assertValidateThrows(NullPointerException.class, config); } @Test @@ -158,8 +152,7 @@ public void validateMissingValue() throws Exception { writer.append("test,a,b,1,2,1\n"); } - exception.expect(ArrayIndexOutOfBoundsException.class); - new MockRecordValidator(config, 2_000L, root).validate(); + assertValidateThrows(ArrayIndexOutOfBoundsException.class, config); } @Test @@ -172,8 +165,7 @@ public void validateWrongValueType() throws Exception { writer.append("test,a,b,1,2,b\n"); } - exception.expect(NumberFormatException.class); - new MockRecordValidator(config, 2_000L, root).validate(); + assertValidateThrows(NumberFormatException.class, config); } @Test @@ -188,6 +180,15 @@ public void validateMultipleFields() throws Exception { writer.append("test,a,b,1,2,1,1,1\n"); } + assertValidate(config); + } + + private void assertValidateThrows(Class ex, MockDataConfig config) { + MockRecordValidator validator = new MockRecordValidator(config, 2_000L, root); + assertThrows(ex, validator::validate); + } + + private void assertValidate(MockDataConfig config) { new MockRecordValidator(config, 2_000L, root).validate(); } -} \ No newline at end of file +} diff --git a/radar-commons/src/main/java/org/radarbase/producer/rest/RestTopicSender.java b/radar-commons/src/main/java/org/radarbase/producer/rest/RestTopicSender.java index cc7f0923..147ce268 100644 --- a/radar-commons/src/main/java/org/radarbase/producer/rest/RestTopicSender.java +++ b/radar-commons/src/main/java/org/radarbase/producer/rest/RestTopicSender.java @@ -16,7 +16,6 @@ package org.radarbase.producer.rest; -import static org.radarbase.producer.rest.RestClient.responseBody; import static org.radarbase.producer.rest.UncheckedRequestException.fail; import java.io.IOException; diff --git a/radar-commons/src/test/java/org/radarbase/producer/rest/SchemaRetrieverTest.java b/radar-commons/src/test/java/org/radarbase/producer/rest/SchemaRetrieverTest.java index 38d283ab..a56abae0 100644 --- a/radar-commons/src/test/java/org/radarbase/producer/rest/SchemaRetrieverTest.java +++ b/radar-commons/src/test/java/org/radarbase/producer/rest/SchemaRetrieverTest.java @@ -17,6 +17,7 @@ package org.radarbase.producer.rest; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import java.io.IOException; import java.util.Collections; @@ -28,23 +29,17 @@ import org.apache.avro.Schema.Field; import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.radarbase.config.ServerConfig; public class SchemaRetrieverTest { private MockWebServer server; - private ServerConfig config; private SchemaRetriever retriever; - @Rule - public ExpectedException exception = ExpectedException.none(); - @Before public void setUp() { server = new MockWebServer(); - config = new ServerConfig(); + ServerConfig config = new ServerConfig(); config.setProtocol("http"); config.setHost(server.getHostName()); config.setPort(server.getPort()); @@ -58,7 +53,7 @@ public void tearDown() throws IOException { } @Test - public void subject() throws Exception { + public void subject() { assertEquals("bla-value", SchemaRetriever.subject("bla", true)); assertEquals("bla-key", SchemaRetriever.subject("bla", false)); } @@ -95,16 +90,15 @@ public void getSchemaMetadata() throws Exception { // Already queried schema is cached and does not need another request ParsedSchemaMetadata metadata2 = retriever.getSchemaMetadata("bla", true, -1); - assertEquals(Integer.valueOf(10), metadata.getId()); - assertEquals(Integer.valueOf(2), metadata.getVersion()); - assertEquals(Schema.create(Schema.Type.STRING), metadata.getSchema()); + assertEquals(Integer.valueOf(10), metadata2.getId()); + assertEquals(Integer.valueOf(2), metadata2.getVersion()); + assertEquals(Schema.create(Schema.Type.STRING), metadata2.getSchema()); assertEquals(1, server.getRequestCount()); // Not yet queried schema needs a new request, so if the server does not respond, an // IOException is thrown. server.enqueue(new MockResponse().setResponseCode(500)); - exception.expect(IOException.class); - retriever.getSchemaMetadata("bla", false, 2); + assertThrows(IOException.class, () -> retriever.getSchemaMetadata("bla", false, 2)); } @Test @@ -122,9 +116,8 @@ public void addSchemaMetadata() throws Exception { List schemaFields = Collections.singletonList( new Field("a", Schema.create(Schema.Type.INT), "that a", 10)); - Schema record = Schema.createRecord("C", "that C", "org.radarcns", false); - metadata = new ParsedSchemaMetadata(null, null, - Schema.createRecord("C", "that C", "org.radarcns", false, schemaFields)); + Schema record = Schema.createRecord("C", "that C", "org.radarcns", false, schemaFields); + metadata = new ParsedSchemaMetadata(null, null, record); server.enqueue(new MockResponse().setBody("{\"id\":11}")); retriever.addSchemaMetadata("bla", true, metadata); assertEquals(Integer.valueOf(11), metadata.getId()); @@ -167,4 +160,4 @@ public void getOrSetSchemaMetadataGet() throws Exception { assertEquals(Integer.valueOf(10), metadata.getId()); assertEquals(Schema.create(Schema.Type.STRING), metadata.getSchema()); } -} \ No newline at end of file +}