Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#15238] fix(api): Reject null and blank list and map values in job templates and template updates (#15239)

Jerry Shao942be7212401
diff --git a/api/src/main/java/org/apache/gravitino/job/JobTemplate.java b/api/src/main/java/org/apache/gravitino/job/JobTemplate.java
index fa6d89f211..78f5a6ab6c 100644
--- a/api/src/main/java/org/apache/gravitino/job/JobTemplate.java
+++ b/api/src/main/java/org/apache/gravitino/job/JobTemplate.java
@@ -19,8 +19,6 @@
package org.apache.gravitino.job;
import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -330,11 +328,12 @@ public abstract class JobTemplate {
Preconditions.checkArgument(
StringUtils.isNotBlank(executable), "Executable must not be null or empty");
- this.arguments = arguments != null ? ImmutableList.copyOf(arguments) : ImmutableList.of();
- this.environments =
- environments != null ? ImmutableMap.copyOf(environments) : ImmutableMap.of();
- this.customFields =
- customFields != null ? ImmutableMap.copyOf(customFields) : ImmutableMap.of();
+ JobTemplateValidations.checkNoNullElements("arguments", arguments);
+ JobTemplateValidations.checkNoBlankKeysOrNullValues("environments", environments);
+ JobTemplateValidations.checkNoBlankKeysOrNullValues("customFields", customFields);
+ this.arguments = JobTemplateValidations.immutableListCopy(arguments);
+ this.environments = JobTemplateValidations.immutableMapCopy(environments);
+ this.customFields = JobTemplateValidations.immutableMapCopy(customFields);
}
}
}
diff --git a/api/src/main/java/org/apache/gravitino/job/JobTemplateChange.java b/api/src/main/java/org/apache/gravitino/job/JobTemplateChange.java
index da9078302e..795d00d588 100644
--- a/api/src/main/java/org/apache/gravitino/job/JobTemplateChange.java
+++ b/api/src/main/java/org/apache/gravitino/job/JobTemplateChange.java
@@ -236,6 +236,11 @@ public interface JobTemplateChange {
* @param builder The builder to construct the TemplateUpdate.
*/
protected TemplateUpdate(BaseBuilder<?, ?> builder) {
+ JobTemplateValidations.checkNoNullElements("newArguments", builder.newArguments);
+ JobTemplateValidations.checkNoBlankKeysOrNullValues(
+ "newEnvironments", builder.newEnvironments);
+ JobTemplateValidations.checkNoBlankKeysOrNullValues(
+ "newCustomFields", builder.newCustomFields);
this.newExecutable = builder.newExecutable;
this.newArguments = builder.newArguments;
this.newEnvironments = builder.newEnvironments;
@@ -383,6 +388,7 @@ public interface JobTemplateChange {
private ShellTemplateUpdate(Builder builder) {
super(builder);
+ JobTemplateValidations.checkNoBlankElements("newScripts", builder.newScripts);
this.newScripts = builder.newScripts;
}
@@ -483,6 +489,10 @@ public interface JobTemplateChange {
private SparkTemplateUpdate(Builder builder) {
super(builder);
+ JobTemplateValidations.checkNoBlankElements("newJars", builder.newJars);
+ JobTemplateValidations.checkNoBlankElements("newFiles", builder.newFiles);
+ JobTemplateValidations.checkNoBlankElements("newArchives", builder.newArchives);
+ JobTemplateValidations.checkNoBlankKeysOrNullValues("newConfigs", builder.newConfigs);
this.newClassName = builder.newClassName;
this.newJars = builder.newJars;
this.newFiles = builder.newFiles;
diff --git a/api/src/main/java/org/apache/gravitino/job/JobTemplateValidations.java b/api/src/main/java/org/apache/gravitino/job/JobTemplateValidations.java
new file mode 100644
index 0000000000..945dc3d342
--- /dev/null
+++ b/api/src/main/java/org/apache/gravitino/job/JobTemplateValidations.java
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.job;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import java.util.List;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * Checks the list and map fields of job templates and job template updates, so an invalid element,
+ * key or value is rejected with an {@link IllegalArgumentException} naming the field, instead of
+ * failing later with a {@link NullPointerException}.
+ *
+ * <p>Resource URIs and map keys must not be blank. Arguments and map values may be empty, because a
+ * placeholder with an empty default value, for example {@code {{older_than:-}}}, resolves to an
+ * empty string when the job runs, and the runtime job template is built with the same builders.
+ */
+final class JobTemplateValidations {
+
+ private JobTemplateValidations() {}
+
+ /**
+ * Checks that a list field, such as the arguments, has no null element. Empty elements are
+ * allowed.
+ *
+ * @param field the name of the field, used in the error message
+ * @param values the list, may be null
+ * @throws IllegalArgumentException if the list has a null element
+ */
+ static void checkNoNullElements(String field, @Nullable List<String> values) {
+ if (values == null) {
+ return;
+ }
+ for (int i = 0; i < values.size(); i++) {
+ Preconditions.checkArgument(
+ values.get(i) != null,
+ "\"%s\" must not contain null elements, found one at index %s",
+ field,
+ i);
+ }
+ }
+
+ /**
+ * Checks that a list field of resource URIs, such as the jars, has no null or blank element.
+ *
+ * @param field the name of the field, used in the error message
+ * @param values the list, may be null
+ * @throws IllegalArgumentException if the list has a null or blank element
+ */
+ static void checkNoBlankElements(String field, @Nullable List<String> values) {
+ if (values == null) {
+ return;
+ }
+ for (int i = 0; i < values.size(); i++) {
+ Preconditions.checkArgument(
+ StringUtils.isNotBlank(values.get(i)),
+ "\"%s\" must not contain null or blank elements, found one at index %s",
+ field,
+ i);
+ }
+ }
+
+ /**
+ * Checks that a map field has no null or blank key and no null value. Empty values are allowed.
+ *
+ * @param field the name of the field, used in the error message
+ * @param entries the map, may be null
+ * @throws IllegalArgumentException if the map has a null or blank key, or a null value
+ */
+ static void checkNoBlankKeysOrNullValues(String field, @Nullable Map<String, String> entries) {
+ if (entries == null) {
+ return;
+ }
+ entries.forEach(
+ (key, value) -> {
+ Preconditions.checkArgument(
+ StringUtils.isNotBlank(key), "\"%s\" must not contain null or blank keys", field);
+ Preconditions.checkArgument(
+ value != null,
+ "\"%s\" must not contain null values, found one for key \"%s\"",
+ field,
+ key);
+ });
+ }
+
+ /**
+ * Returns an immutable copy of a list field, or an empty list if the field is null. Check the
+ * elements first, the copy rejects null elements with a {@link NullPointerException}.
+ *
+ * @param values the list, may be null
+ * @return the immutable copy
+ */
+ static List<String> immutableListCopy(@Nullable List<String> values) {
+ return values == null ? ImmutableList.of() : ImmutableList.copyOf(values);
+ }
+
+ /**
+ * Returns an immutable copy of a map field, or an empty map if the field is null. Check the
+ * entries first, the copy rejects null keys and values with a {@link NullPointerException}.
+ *
+ * @param entries the map, may be null
+ * @return the immutable copy
+ */
+ static Map<String, String> immutableMapCopy(@Nullable Map<String, String> entries) {
+ return entries == null ? ImmutableMap.of() : ImmutableMap.copyOf(entries);
+ }
+}
diff --git a/api/src/main/java/org/apache/gravitino/job/ShellJobTemplate.java b/api/src/main/java/org/apache/gravitino/job/ShellJobTemplate.java
index 69b5b30f93..4c175e9776 100644
--- a/api/src/main/java/org/apache/gravitino/job/ShellJobTemplate.java
+++ b/api/src/main/java/org/apache/gravitino/job/ShellJobTemplate.java
@@ -18,7 +18,6 @@
*/
package org.apache.gravitino.job;
-import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Objects;
@@ -141,7 +140,8 @@ public class ShellJobTemplate extends JobTemplate {
protected void validate() {
super.validate();
- this.scripts = scripts != null ? ImmutableList.copyOf(scripts) : ImmutableList.of();
+ JobTemplateValidations.checkNoBlankElements("scripts", scripts);
+ this.scripts = JobTemplateValidations.immutableListCopy(scripts);
}
}
}
diff --git a/api/src/main/java/org/apache/gravitino/job/SparkJobTemplate.java b/api/src/main/java/org/apache/gravitino/job/SparkJobTemplate.java
index 145f9cf5aa..8cbc2be6b3 100644
--- a/api/src/main/java/org/apache/gravitino/job/SparkJobTemplate.java
+++ b/api/src/main/java/org/apache/gravitino/job/SparkJobTemplate.java
@@ -18,8 +18,6 @@
*/
package org.apache.gravitino.job;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -289,10 +287,14 @@ public class SparkJobTemplate extends JobTemplate {
protected void validate() {
super.validate();
- this.jars = jars != null ? ImmutableList.copyOf(jars) : ImmutableList.of();
- this.files = files != null ? ImmutableList.copyOf(files) : ImmutableList.of();
- this.archives = archives != null ? ImmutableList.copyOf(archives) : ImmutableList.of();
- this.configs = configs != null ? ImmutableMap.copyOf(configs) : ImmutableMap.of();
+ JobTemplateValidations.checkNoBlankElements("jars", jars);
+ JobTemplateValidations.checkNoBlankElements("files", files);
+ JobTemplateValidations.checkNoBlankElements("archives", archives);
+ JobTemplateValidations.checkNoBlankKeysOrNullValues("configs", configs);
+ this.jars = JobTemplateValidations.immutableListCopy(jars);
+ this.files = JobTemplateValidations.immutableListCopy(files);
+ this.archives = JobTemplateValidations.immutableListCopy(archives);
+ this.configs = JobTemplateValidations.immutableMapCopy(configs);
}
}
}
diff --git a/api/src/test/java/org/apache/gravitino/job/TestJobTemplateValidations.java b/api/src/test/java/org/apache/gravitino/job/TestJobTemplateValidations.java
new file mode 100644
index 0000000000..41d633c883
--- /dev/null
+++ b/api/src/test/java/org/apache/gravitino/job/TestJobTemplateValidations.java
@@ -0,0 +1,237 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.job;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.function.Executable;
+
+public class TestJobTemplateValidations {
+
+ private static final List<String> NULL_ELEMENT = Arrays.asList("a", null, "b");
+
+ @Test
+ public void testShellJobTemplateRejectsNullElements() {
+ assertRejected(
+ "\"arguments\" must not contain null elements, found one at index 1",
+ () -> shellTemplate().withArguments(NULL_ELEMENT).build());
+ assertRejected(
+ "\"scripts\" must not contain null or blank elements, found one at index 1",
+ () -> shellTemplate().withScripts(NULL_ELEMENT).build());
+ }
+
+ @Test
+ public void testJobTemplateRejectsNullMapEntries() {
+ assertRejected(
+ "\"environments\" must not contain null values, found one for key \"Z\"",
+ () -> shellTemplate().withEnvironments(nullValue("Z")).build());
+ assertRejected(
+ "\"customFields\" must not contain null or blank keys",
+ () -> shellTemplate().withCustomFields(nullKey()).build());
+ }
+
+ @Test
+ public void testSparkJobTemplateRejectsNulls() {
+ assertRejected(
+ "\"jars\" must not contain null or blank elements",
+ () -> sparkTemplate().withJars(NULL_ELEMENT).build());
+ assertRejected(
+ "\"files\" must not contain null or blank elements",
+ () -> sparkTemplate().withFiles(NULL_ELEMENT).build());
+ assertRejected(
+ "\"archives\" must not contain null or blank elements",
+ () -> sparkTemplate().withArchives(NULL_ELEMENT).build());
+ assertRejected(
+ "\"configs\" must not contain null values, found one for key \"spark.master\"",
+ () -> sparkTemplate().withConfigs(nullValue("spark.master")).build());
+ }
+
+ @Test
+ public void testJobTemplateRejectsBlankResourcesAndKeys() {
+ List<String> blankElement = Arrays.asList("a", " ");
+ assertRejected(
+ "\"scripts\" must not contain null or blank elements, found one at index 1",
+ () -> shellTemplate().withScripts(blankElement).build());
+ assertRejected(
+ "\"jars\" must not contain null or blank elements, found one at index 1",
+ () -> sparkTemplate().withJars(blankElement).build());
+ assertRejected(
+ "\"files\" must not contain null or blank elements",
+ () -> sparkTemplate().withFiles(Collections.singletonList("")).build());
+ assertRejected(
+ "\"archives\" must not contain null or blank elements",
+ () -> sparkTemplate().withArchives(blankElement).build());
+ assertRejected(
+ "\"environments\" must not contain null or blank keys",
+ () -> shellTemplate().withEnvironments(Collections.singletonMap(" ", "v")).build());
+ assertRejected(
+ "\"configs\" must not contain null or blank keys",
+ () -> sparkTemplate().withConfigs(Collections.singletonMap("", "v")).build());
+ }
+
+ @Test
+ public void testTemplateUpdateRejectsBlankResourcesAndKeys() {
+ assertRejected(
+ "\"newScripts\" must not contain null or blank elements, found one at index 0",
+ () ->
+ JobTemplateChange.ShellTemplateUpdate.builder()
+ .withNewScripts(Collections.singletonList(" "))
+ .build());
+ assertRejected(
+ "\"newJars\" must not contain null or blank elements",
+ () ->
+ JobTemplateChange.SparkTemplateUpdate.builder()
+ .withNewJars(Collections.singletonList(""))
+ .build());
+ assertRejected(
+ "\"newEnvironments\" must not contain null or blank keys",
+ () ->
+ JobTemplateChange.ShellTemplateUpdate.builder()
+ .withNewEnvironments(Collections.singletonMap(" ", "v"))
+ .build());
+ assertRejected(
+ "\"newConfigs\" must not contain null or blank keys",
+ () ->
+ JobTemplateChange.SparkTemplateUpdate.builder()
+ .withNewConfigs(Collections.singletonMap("", "v"))
+ .build());
+ }
+
+ @Test
+ public void testRuntimeTemplateAllowsEmptyArgumentsAndValues() {
+ // A placeholder with an empty default, e.g. {{older_than:-}}, resolves to "" at run time.
+ SparkJobTemplate template =
+ sparkTemplate()
+ .withArguments(Arrays.asList("--older-than", "", "--location", " "))
+ .withEnvironments(Collections.singletonMap("EMPTY", ""))
+ .withConfigs(Collections.singletonMap("spark.extra", ""))
+ .build();
+ Assertions.assertEquals(
+ Arrays.asList("--older-than", "", "--location", " "), template.arguments());
+ Assertions.assertEquals("", template.environments().get("EMPTY"));
+ Assertions.assertEquals("", template.configs().get("spark.extra"));
+
+ JobTemplateChange.ShellTemplateUpdate update =
+ JobTemplateChange.ShellTemplateUpdate.builder()
+ .withNewArguments(Collections.singletonList(""))
+ .withNewEnvironments(Collections.singletonMap("EMPTY", ""))
+ .build();
+ Assertions.assertEquals(Collections.singletonList(""), update.getNewArguments());
+ }
+
+ @Test
+ public void testJobTemplateKeepsEmptyAndAbsentFields() {
+ ShellJobTemplate template =
+ shellTemplate()
+ .withArguments(Arrays.asList("a", ""))
+ .withEnvironments(Collections.singletonMap("K", ""))
+ .build();
+ Assertions.assertEquals(Arrays.asList("a", ""), template.arguments());
+ Assertions.assertEquals(Collections.singletonMap("K", ""), template.environments());
+ Assertions.assertTrue(template.customFields().isEmpty());
+ Assertions.assertTrue(template.scripts().isEmpty());
+ }
+
+ @Test
+ public void testShellTemplateUpdateRejectsNulls() {
+ assertRejected(
+ "\"newArguments\" must not contain null elements, found one at index 1",
+ () ->
+ JobTemplateChange.ShellTemplateUpdate.builder().withNewArguments(NULL_ELEMENT).build());
+ assertRejected(
+ "\"newEnvironments\" must not contain null values, found one for key \"Z\"",
+ () ->
+ JobTemplateChange.ShellTemplateUpdate.builder()
+ .withNewEnvironments(nullValue("Z"))
+ .build());
+ assertRejected(
+ "\"newCustomFields\" must not contain null or blank keys",
+ () ->
+ JobTemplateChange.ShellTemplateUpdate.builder().withNewCustomFields(nullKey()).build());
+ assertRejected(
+ "\"newScripts\" must not contain null or blank elements",
+ () -> JobTemplateChange.ShellTemplateUpdate.builder().withNewScripts(NULL_ELEMENT).build());
+ }
+
+ @Test
+ public void testSparkTemplateUpdateRejectsNulls() {
+ assertRejected(
+ "\"newJars\" must not contain null or blank elements",
+ () -> JobTemplateChange.SparkTemplateUpdate.builder().withNewJars(NULL_ELEMENT).build());
+ assertRejected(
+ "\"newFiles\" must not contain null or blank elements",
+ () -> JobTemplateChange.SparkTemplateUpdate.builder().withNewFiles(NULL_ELEMENT).build());
+ assertRejected(
+ "\"newArchives\" must not contain null or blank elements",
+ () ->
+ JobTemplateChange.SparkTemplateUpdate.builder().withNewArchives(NULL_ELEMENT).build());
+ assertRejected(
+ "\"newConfigs\" must not contain null values",
+ () ->
+ JobTemplateChange.SparkTemplateUpdate.builder()
+ .withNewConfigs(nullValue("spark.master"))
+ .build());
+ }
+
+ @Test
+ public void testTemplateUpdateKeepsAbsentFieldsAsNoChange() {
+ JobTemplateChange.ShellTemplateUpdate update =
+ JobTemplateChange.ShellTemplateUpdate.builder().withNewExecutable("/bin/echo").build();
+ Assertions.assertNull(update.getNewArguments());
+ Assertions.assertNull(update.getNewEnvironments());
+ Assertions.assertNull(update.getNewScripts());
+ }
+
+ private static ShellJobTemplate.Builder shellTemplate() {
+ return ShellJobTemplate.builder().withName("shell_template").withExecutable("/bin/echo");
+ }
+
+ private static SparkJobTemplate.Builder sparkTemplate() {
+ return SparkJobTemplate.builder()
+ .withName("spark_template")
+ .withExecutable("/path/to/app.jar")
+ .withClassName("org.example.Main");
+ }
+
+ private static Map<String, String> nullValue(String key) {
+ Map<String, String> map = new HashMap<>();
+ map.put(key, null);
+ return map;
+ }
+
+ private static Map<String, String> nullKey() {
+ Map<String, String> map = new HashMap<>();
+ map.put(null, "v");
+ return map;
+ }
+
+ private static void assertRejected(String expectedMessage, Executable executable) {
+ IllegalArgumentException e =
+ Assertions.assertThrows(IllegalArgumentException.class, executable);
+ Assertions.assertTrue(
+ e.getMessage().contains(expectedMessage),
+ () -> "Expected message to contain [" + expectedMessage + "] but was: " + e.getMessage());
+ }
+}
diff --git a/plugins/k8s-job-executor/src/test/java/org/apache/gravitino/job/k8s/spark/TestSparkApplicationUtils.java b/plugins/k8s-job-executor/src/test/java/org/apache/gravitino/job/k8s/spark/TestSparkApplicationUtils.java
index ee4a2b9d3d..f305dda6b7 100644
--- a/plugins/k8s-job-executor/src/test/java/org/apache/gravitino/job/k8s/spark/TestSparkApplicationUtils.java
+++ b/plugins/k8s-job-executor/src/test/java/org/apache/gravitino/job/k8s/spark/TestSparkApplicationUtils.java
@@ -844,7 +844,18 @@ public class TestSparkApplicationUtils {
@Test
public void testRejectInvalidEnvironmentName() {
- for (String invalid : new String[] {"", "MY VAR", "A=B", "1ST", "PATH/X", ".", "..", "..CFG"}) {
+ // A blank name is rejected when the template is built, before it reaches the executor.
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ SparkJobTemplate.builder()
+ .withName("user-job")
+ .withExecutable("https://repo/app.jar")
+ .withClassName("org.example.App")
+ .withEnvironments(ImmutableMap.of("", "secret-value"))
+ .build());
+
+ for (String invalid : new String[] {"MY VAR", "A=B", "1ST", "PATH/X", ".", "..", "..CFG"}) {
SparkJobTemplate template =
SparkJobTemplate.builder()
.withName("user-job")
diff --git a/server/src/test/java/org/apache/gravitino/server/web/rest/TestJobOperations.java b/server/src/test/java/org/apache/gravitino/server/web/rest/TestJobOperations.java
index 7df3b2205c..5f8b85878f 100644
--- a/server/src/test/java/org/apache/gravitino/server/web/rest/TestJobOperations.java
+++ b/server/src/test/java/org/apache/gravitino/server/web/rest/TestJobOperations.java
@@ -395,6 +395,78 @@ public class TestJobOperations extends JerseyTest {
verify(jobOperationDispatcher, never()).registerJobTemplate(any(), any());
}
+ @Test
+ public void testRegisterJobTemplateRejectsInvalidElementsAndKeys() {
+ String nullArgument =
+ "{\"jobTemplate\": {\"jobType\": \"shell\", \"name\": \"t1\","
+ + " \"executable\": \"/bin/echo\", \"arguments\": [\"a\", null, \"b\"]}}";
+ assertJobTemplateRequestRejected(
+ target(jobTemplatePath())
+ .request(APPLICATION_JSON_TYPE)
+ .accept("application/vnd.gravitino.v1+json")
+ .post(Entity.entity(nullArgument, APPLICATION_JSON_TYPE)),
+ "\"arguments\" must not contain null elements, found one at index 1");
+
+ String nullEnvironment =
+ "{\"jobTemplate\": {\"jobType\": \"shell\", \"name\": \"t1\","
+ + " \"executable\": \"/bin/echo\", \"environments\": {\"Z\": null}}}";
+ assertJobTemplateRequestRejected(
+ target(jobTemplatePath())
+ .request(APPLICATION_JSON_TYPE)
+ .accept("application/vnd.gravitino.v1+json")
+ .post(Entity.entity(nullEnvironment, APPLICATION_JSON_TYPE)),
+ "\"environments\" must not contain null values, found one for key \"Z\"");
+
+ String blankScript =
+ "{\"jobTemplate\": {\"jobType\": \"shell\", \"name\": \"t1\","
+ + " \"executable\": \"/bin/echo\", \"scripts\": [\" \"]}}";
+ assertJobTemplateRequestRejected(
+ target(jobTemplatePath())
+ .request(APPLICATION_JSON_TYPE)
+ .accept("application/vnd.gravitino.v1+json")
+ .post(Entity.entity(blankScript, APPLICATION_JSON_TYPE)),
+ "\"scripts\" must not contain null or blank elements, found one at index 0");
+
+ String blankEnvironmentKey =
+ "{\"jobTemplate\": {\"jobType\": \"shell\", \"name\": \"t1\","
+ + " \"executable\": \"/bin/echo\", \"environments\": {\"\": \"v\"}}}";
+ assertJobTemplateRequestRejected(
+ target(jobTemplatePath())
+ .request(APPLICATION_JSON_TYPE)
+ .accept("application/vnd.gravitino.v1+json")
+ .post(Entity.entity(blankEnvironmentKey, APPLICATION_JSON_TYPE)),
+ "\"environments\" must not contain null or blank keys");
+
+ verify(jobOperationDispatcher, never()).registerJobTemplate(any(), any());
+ }
+
+ @Test
+ public void testAlterJobTemplateRejectsNullElementsAndValues() {
+ String nullArgument =
+ "{\"updates\": [{\"@type\": \"updateTemplate\", \"newTemplate\":"
+ + " {\"@type\": \"shell\", \"newArguments\": [\"a\", null]}}]}";
+ assertJobTemplateRequestRejected(
+ target(jobTemplatePath())
+ .path("shell_template_1")
+ .request(APPLICATION_JSON_TYPE)
+ .accept("application/vnd.gravitino.v1+json")
+ .put(Entity.entity(nullArgument, APPLICATION_JSON_TYPE)),
+ "\"newArguments\" must not contain null elements, found one at index 1");
+
+ String nullConfig =
+ "{\"updates\": [{\"@type\": \"updateTemplate\", \"newTemplate\":"
+ + " {\"@type\": \"spark\", \"newConfigs\": {\"spark.master\": null}}}]}";
+ assertJobTemplateRequestRejected(
+ target(jobTemplatePath())
+ .path("spark_template_1")
+ .request(APPLICATION_JSON_TYPE)
+ .accept("application/vnd.gravitino.v1+json")
+ .put(Entity.entity(nullConfig, APPLICATION_JSON_TYPE)),
+ "\"newConfigs\" must not contain null values, found one for key \"spark.master\"");
+
+ verify(jobOperationDispatcher, never()).alterJobTemplate(any(), any(), any());
+ }
+
@Test
public void testGetJobTemplate() {
JobTemplateEntity template =
@@ -1756,4 +1828,16 @@ public class TestJobOperations extends JerseyTest {
.withFinishedAt(finishedAt)
.build();
}
+
+ private static void assertJobTemplateRequestRejected(Response resp, String expectedMessage) {
+ try (Response response = resp) {
+ Assertions.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
+ ErrorResponse error = response.readEntity(ErrorResponse.class);
+ Assertions.assertEquals(ErrorConstants.ILLEGAL_ARGUMENTS_CODE, error.getCode());
+ Assertions.assertEquals(IllegalArgumentException.class.getSimpleName(), error.getType());
+ Assertions.assertTrue(
+ error.getMessage().contains(expectedMessage),
+ () -> "Unexpected error message: " + error.getMessage());
+ }
+ }
}