Skip to content

Commit 96e9abb

Browse files
committed
Some more checks on ID
1 parent 35f0c2a commit 96e9abb

2 files changed

Lines changed: 61 additions & 5 deletions

File tree

temporal-sdk/src/main/java/io/temporal/client/StartNexusOperationOptions.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ private Builder(StartNexusOperationOptions options) {
5151
}
5252

5353
/**
54-
* Required. Unique identifier for this operation within its namespace. Callers must supply this
55-
* explicitly; the SDK does not invent one on the caller's behalf.
54+
* Required. Unique identifier for this operation within its namespace.
5655
*/
5756
public Builder setId(@Nonnull String id) {
58-
this.id = Objects.requireNonNull(id, "id");
57+
Objects.requireNonNull(id, "id");
58+
if (id.isEmpty()) {
59+
throw new IllegalArgumentException("id must not be empty");
60+
}
61+
this.id = id;
5962
return this;
6063
}
6164

@@ -102,9 +105,9 @@ public Builder setIdConflictPolicy(@Nullable NexusOperationIdConflictPolicy idCo
102105
}
103106

104107
public StartNexusOperationOptions build() {
105-
if (id == null) {
108+
if (id == null || id.isEmpty()) {
106109
throw new IllegalStateException(
107-
"StartNexusOperationOptions.Builder.setId(...) must be called with a non-null id "
110+
"StartNexusOperationOptions.Builder.setId(...) must be called with a non-empty id "
108111
+ "before build(); the SDK does not generate operation IDs.");
109112
}
110113
return new StartNexusOperationOptions(this);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.temporal.client;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
/**
7+
* Pure unit tests for {@link StartNexusOperationOptions.Builder} input validation. ID is required —
8+
* callers must supply a non-empty value via {@link StartNexusOperationOptions.Builder#setId} and
9+
* the SDK does not invent one on their behalf.
10+
*/
11+
public class StartNexusOperationOptionsTest {
12+
13+
@Test
14+
public void buildThrowsWhenIdNotSet() {
15+
try {
16+
StartNexusOperationOptions.newBuilder().build();
17+
Assert.fail("expected IllegalStateException when id is unset");
18+
} catch (IllegalStateException expected) {
19+
Assert.assertTrue(
20+
"error message should mention setId, got: " + expected.getMessage(),
21+
expected.getMessage() != null && expected.getMessage().contains("setId"));
22+
}
23+
}
24+
25+
@Test
26+
public void setIdRejectsNull() {
27+
try {
28+
StartNexusOperationOptions.newBuilder().setId(null);
29+
Assert.fail("expected NullPointerException when setId is called with null");
30+
} catch (NullPointerException expected) {
31+
// expected
32+
}
33+
}
34+
35+
@Test
36+
public void setIdRejectsEmpty() {
37+
try {
38+
StartNexusOperationOptions.newBuilder().setId("");
39+
Assert.fail("expected IllegalArgumentException when setId is called with an empty string");
40+
} catch (IllegalArgumentException expected) {
41+
Assert.assertTrue(
42+
"error message should mention empty, got: " + expected.getMessage(),
43+
expected.getMessage() != null && expected.getMessage().contains("empty"));
44+
}
45+
}
46+
47+
@Test
48+
public void buildSucceedsWithNonEmptyId() {
49+
StartNexusOperationOptions options =
50+
StartNexusOperationOptions.newBuilder().setId("my-id").build();
51+
Assert.assertEquals("my-id", options.getId());
52+
}
53+
}

0 commit comments

Comments
 (0)