Skip to content

Commit

Permalink
fix: null-checks and empty-strings (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
eledhwen authored Jan 28, 2025
1 parent e7178de commit 9ed77d5
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public Pipeline<I> build()
throw new IllegalStateException("A payload-based pipeline cannot be built without an initializer");
if (this.indexers().isEmpty())
this.indexers.add(new SingleAutoIndexer<>());
if (this.id == null)
throw new IllegalStateException("The pipeline id cannot be null");

return new CompositePipeline<>(
this.id(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,18 @@ protected InitializerDescriptor<I> build()
config.ifPresent(this::fillFromAnnotation);

this.fillDefaults();
this.validate();

return new InitializerDescriptor<>(
this.id,
this.initializer,
this.errorHandler
);
}

private void validate()
{
if (this.id == null)
throw new IllegalStateException("An initializer cannot have a null id, make sure the defaultId() return value is not null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import tech.illuin.pipeline.output.PipelineTag;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static java.util.Collections.emptyMap;
Expand Down Expand Up @@ -91,12 +92,13 @@ public static Tag[] computeDiscriminants(String identifier)
@Override
public Map<String, String> compileMarkers()
{
Map<String, String> markers = new HashMap<>();
markers.put("pipeline", this.tag.pipeline());
/* The author value can be null, thus Map.of cannot be used here */
markers.put("author", this.tag.author());
return MetricFunctions.compileMarkers(
this.metricTags,
Map.of(
"pipeline", this.tag.pipeline(),
"author", this.tag.author()
),
markers,
emptyMap()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public Map<String, String> asMap()

public Collection<Tag> asTags()
{
return this.data.entrySet().stream().map(e -> Tag.of(e.getKey(), e.getValue())).toList();
return this.data.entrySet().stream()
.map(e -> Tag.of(e.getKey(), e.getValue() == null ? "" : e.getValue()))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ protected SinkDescriptor build()
config.ifPresent(this::fillFromAnnotation);

this.fillDefaults();
this.validate();

return new SinkDescriptor(
this.id,
Expand All @@ -120,4 +121,10 @@ protected SinkDescriptor build()
this.errorHandler
);
}

private void validate()
{
if (this.id == null)
throw new IllegalStateException("A sink cannot have a null id, make sure the defaultId() return value is not null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ protected StepDescriptor<T, I> build()
config.ifPresent(this::fillFromAnnotation);

this.fillDefaults();
this.validate();

return new StepDescriptor<>(
this.id,
Expand All @@ -188,4 +189,10 @@ protected StepDescriptor<T, I> build()
this.errorHandler
);
}

private void validate()
{
if (this.id == null)
throw new IllegalStateException("A step cannot have a null id, make sure the defaultId() return value is not null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ public void testMDC()
Assertions.assertFalse(ctx1.containsKey("test"));
}

@Test
public void testMDC_shouldHandleNulls()
{
MDCAdapter adapter = new BasicMDCAdapter();
PipelineMetrics metrics = Assertions.assertDoesNotThrow(() -> new PipelineMetrics(
new SimpleMeterRegistry(),
new PipelineTag(TSIDGenerator.INSTANCE.generate(), "test-mdc-nullable", null),
new MetricTags().put("test", "true"),
new DebugMDCManager(adapter)
));

metrics.setMDC();
Map<String, String> ctx0 = adapter.getCopyOfContextMap();

Assertions.assertTrue(ctx0.containsKey("pipeline"));
Assertions.assertTrue(ctx0.containsKey("author"));
Assertions.assertTrue(ctx0.containsKey("test"));
Assertions.assertEquals("test-mdc-nullable", ctx0.get("pipeline"));
Assertions.assertNull(ctx0.get("author"));
Assertions.assertEquals("true", ctx0.get("test"));
}

@Test
public void testMDCException()
{
Expand Down
16 changes: 16 additions & 0 deletions core/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) %green(%mdc) - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT" />
</root>

<logger name="tech.illuin" level="DEBUG" />
<logger name="tech.illuin.pipeline" level="TRACE" />
<logger name="dev.illuin" level="INFO" />
<logger name="io.micrometer.core" level="INFO" />
</configuration>
6 changes: 0 additions & 6 deletions core/src/test/resources/simplelogger.properties

This file was deleted.

9 changes: 5 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>1.7.36</slf4j.version>
<slf4j.version>2.0.16</slf4j.version>
<logback.version>1.5.16</logback.version>
<junit.version>5.11.4</junit.version>
<mockito.version>5.15.2</mockito.version>
<jacoco.version>0.8.12</jacoco.version>
Expand All @@ -85,9 +86,9 @@
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>test</scope>
</dependency>

Expand Down
16 changes: 16 additions & 0 deletions resilience4j/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) %green(%mdc) - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT" />
</root>

<logger name="tech.illuin" level="DEBUG" />
<logger name="tech.illuin.pipeline" level="TRACE" />
<logger name="dev.illuin" level="INFO" />
<logger name="io.micrometer.core" level="INFO" />
</configuration>
6 changes: 0 additions & 6 deletions resilience4j/src/test/resources/simplelogger.properties

This file was deleted.

0 comments on commit 9ed77d5

Please sign in to comment.