Skip to content

Commit aef68b2

Browse files
committed
move ConfigReaders to static method utilities
1 parent 8c563fc commit aef68b2

4 files changed

Lines changed: 20 additions & 20 deletions

File tree

dynamic-control/src/main/java/io/opentelemetry/contrib/dynamic/policy/registry/json/JsonPolicyInitConfigReader.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ public final class JsonPolicyInitConfigReader {
1919

2020
private static final ObjectMapper MAPPER = new ObjectMapper();
2121

22-
public PolicyInitConfig read(String json) throws IOException {
22+
private JsonPolicyInitConfigReader() {}
23+
24+
public static PolicyInitConfig read(String json) throws IOException {
2325
Objects.requireNonNull(json, "json cannot be null");
2426
return read(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)));
2527
}
2628

27-
public PolicyInitConfig read(InputStream in) throws IOException {
29+
public static PolicyInitConfig read(InputStream in) throws IOException {
2830
Objects.requireNonNull(in, "in cannot be null");
2931
JsonNode root = MAPPER.readTree(in);
3032
return JsonNodePolicyInitConfigParser.parse(root);

dynamic-control/src/main/java/io/opentelemetry/contrib/dynamic/policy/registry/yaml/YamlPolicyInitConfigReader.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ public final class YamlPolicyInitConfigReader {
2121

2222
private static final ObjectMapper MAPPER = new ObjectMapper(new YAMLFactory());
2323

24-
public PolicyInitConfig read(InputStream in) throws IOException {
24+
private YamlPolicyInitConfigReader() {}
25+
26+
public static PolicyInitConfig read(InputStream in) throws IOException {
2527
Objects.requireNonNull(in, "in cannot be null");
2628
JsonNode root = MAPPER.readTree(in);
2729
return JsonNodePolicyInitConfigParser.parse(root);
2830
}
2931

30-
public PolicyInitConfig read(String yaml) throws IOException {
32+
public static PolicyInitConfig read(String yaml) throws IOException {
3133
Objects.requireNonNull(yaml, "yaml cannot be null");
3234
return read(new ByteArrayInputStream(yaml.getBytes(StandardCharsets.UTF_8)));
3335
}

dynamic-control/src/test/java/io/opentelemetry/contrib/dynamic/policy/registry/json/JsonPolicyInitConfigReaderTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ class JsonPolicyInitConfigReaderTest {
2020
private static final String EXAMPLE_FIXTURE =
2121
"/io/opentelemetry/contrib/dynamic/policy/registry/json/policy-init-example.json";
2222

23-
private final JsonPolicyInitConfigReader reader = new JsonPolicyInitConfigReader();
24-
2523
@Test
2624
void readsSourceCentricFixture() throws Exception {
2725
try (InputStream in = getClass().getResourceAsStream(EXAMPLE_FIXTURE)) {
2826
assertThat(in).isNotNull();
2927

30-
PolicyInitConfig config = reader.read(in);
28+
PolicyInitConfig config = JsonPolicyInitConfigReader.read(in);
3129
assertThat(config.getSources()).hasSize(1);
3230

3331
PolicySourceConfig source = config.getSources().get(0);
@@ -43,30 +41,30 @@ void readsSourceCentricFixture() throws Exception {
4341

4442
@Test
4543
void missingSourcesThrows() {
46-
assertThatThrownBy(() -> reader.read("{}"))
44+
assertThatThrownBy(() -> JsonPolicyInitConfigReader.read("{}"))
4745
.isInstanceOf(IllegalArgumentException.class)
4846
.hasMessageContaining("sources");
4947
}
5048

5149
@Test
5250
void emptyPayloadThrows() {
53-
assertThatThrownBy(() -> reader.read(""))
51+
assertThatThrownBy(() -> JsonPolicyInitConfigReader.read(""))
5452
.isInstanceOf(IllegalArgumentException.class)
5553
.hasMessageMatching("(?s).*(empty|sources).*");
5654
}
5755

5856
@Test
5957
void missingFormatThrows() {
6058
String json = "{\"sources\":[{\"kind\":\"opamp\",\"mappings\":[]}]}";
61-
assertThatThrownBy(() -> reader.read(json))
59+
assertThatThrownBy(() -> JsonPolicyInitConfigReader.read(json))
6260
.isInstanceOf(IllegalArgumentException.class)
6361
.hasMessageContaining("format");
6462
}
6563

6664
@Test
6765
void missingMappingsThrows() {
6866
String json = "{\"sources\":[{\"kind\":\"opamp\",\"format\":\"jsonkeyvalue\"}]}";
69-
assertThatThrownBy(() -> reader.read(json))
67+
assertThatThrownBy(() -> JsonPolicyInitConfigReader.read(json))
7068
.isInstanceOf(IllegalArgumentException.class)
7169
.hasMessageContaining("mappings");
7270
}
@@ -75,7 +73,7 @@ void missingMappingsThrows() {
7573
void mappingMissingSourceKeyThrows() {
7674
String json =
7775
"{\"sources\":[{\"kind\":\"opamp\",\"format\":\"jsonkeyvalue\",\"mappings\":[{\"policyType\":\"x\"}]}]}";
78-
assertThatThrownBy(() -> reader.read(json))
76+
assertThatThrownBy(() -> JsonPolicyInitConfigReader.read(json))
7977
.isInstanceOf(IllegalArgumentException.class)
8078
.hasMessageContaining("sourceKey");
8179
}

dynamic-control/src/test/java/io/opentelemetry/contrib/dynamic/policy/registry/yaml/YamlPolicyInitConfigReaderTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ class YamlPolicyInitConfigReaderTest {
2020
private static final String EXAMPLE_FIXTURE =
2121
"/io/opentelemetry/contrib/dynamic/policy/registry/yaml/policy-init-example.yaml";
2222

23-
private final YamlPolicyInitConfigReader reader = new YamlPolicyInitConfigReader();
24-
2523
@Test
2624
void readsSourceCentricFixture() throws Exception {
2725
try (InputStream in = getClass().getResourceAsStream(EXAMPLE_FIXTURE)) {
2826
assertThat(in).isNotNull();
2927

30-
PolicyInitConfig config = reader.read(in);
28+
PolicyInitConfig config = YamlPolicyInitConfigReader.read(in);
3129
assertThat(config.getSources()).hasSize(1);
3230

3331
PolicySourceConfig source = config.getSources().get(0);
@@ -43,30 +41,30 @@ void readsSourceCentricFixture() throws Exception {
4341

4442
@Test
4543
void missingSourcesThrows() {
46-
assertThatThrownBy(() -> reader.read("{}"))
44+
assertThatThrownBy(() -> YamlPolicyInitConfigReader.read("{}"))
4745
.isInstanceOf(IllegalArgumentException.class)
4846
.hasMessageContaining("sources");
4947
}
5048

5149
@Test
5250
void emptyPayloadThrows() {
53-
assertThatThrownBy(() -> reader.read(""))
51+
assertThatThrownBy(() -> YamlPolicyInitConfigReader.read(""))
5452
.isInstanceOf(IllegalArgumentException.class)
5553
.hasMessageMatching("(?s).*(empty|sources).*");
5654
}
5755

5856
@Test
5957
void missingFormatThrows() {
6058
String yaml = "sources:\n - kind: opamp\n mappings: []\n";
61-
assertThatThrownBy(() -> reader.read(yaml))
59+
assertThatThrownBy(() -> YamlPolicyInitConfigReader.read(yaml))
6260
.isInstanceOf(IllegalArgumentException.class)
6361
.hasMessageContaining("format");
6462
}
6563

6664
@Test
6765
void missingMappingsThrows() {
6866
String yaml = "sources:\n - kind: opamp\n format: jsonkeyvalue\n";
69-
assertThatThrownBy(() -> reader.read(yaml))
67+
assertThatThrownBy(() -> YamlPolicyInitConfigReader.read(yaml))
7068
.isInstanceOf(IllegalArgumentException.class)
7169
.hasMessageContaining("mappings");
7270
}
@@ -79,7 +77,7 @@ void mappingMissingSourceKeyThrows() {
7977
+ " format: jsonkeyvalue\n"
8078
+ " mappings:\n"
8179
+ " - policyType: x\n";
82-
assertThatThrownBy(() -> reader.read(yaml))
80+
assertThatThrownBy(() -> YamlPolicyInitConfigReader.read(yaml))
8381
.isInstanceOf(IllegalArgumentException.class)
8482
.hasMessageContaining("sourceKey");
8583
}

0 commit comments

Comments
 (0)