|
| 1 | +/* |
| 2 | + * Copyright 2019 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.mantisrx.runtime; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | + |
| 22 | +import io.mantisrx.shaded.com.fasterxml.jackson.databind.DeserializationFeature; |
| 23 | +import io.mantisrx.shaded.com.fasterxml.jackson.databind.ObjectMapper; |
| 24 | +import io.mantisrx.shaded.com.fasterxml.jackson.datatype.jdk8.Jdk8Module; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +public class NamedJobDefinitionDeserializationTest { |
| 28 | + |
| 29 | + private static final ObjectMapper objectMapper = new ObjectMapper() |
| 30 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) |
| 31 | + .registerModule(new Jdk8Module()); |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testDeserializeNamedJobDefinitionWithJobPrincipal() throws Exception { |
| 35 | + String json = "{\n" + |
| 36 | + " \"jobDefinition\": {\n" + |
| 37 | + " \"name\": \"TestGigi2\",\n" + |
| 38 | + " \"user\": \"ggao\",\n" + |
| 39 | + " \"jobJarFileLocation\": \"https://mantis.staging.us-east-1.prod.netflix.net/mantis-artifacts/titus-migration-thin-1.2.3-snapshot.202511202319+aa2e763.zip\",\n" + |
| 40 | + " \"version\": \"1.2.3-snapshot.202511202319+aa2e763\",\n" + |
| 41 | + " \"schedulingInfo\": {\n" + |
| 42 | + " \"stages\": {\n" + |
| 43 | + " \"1\": {\n" + |
| 44 | + " \"numberOfInstances\": \"1\",\n" + |
| 45 | + " \"machineDefinition\": {\n" + |
| 46 | + " \"cpuCores\": 2,\n" + |
| 47 | + " \"memoryMB\": 8192,\n" + |
| 48 | + " \"diskMB\": 8192,\n" + |
| 49 | + " \"networkMbps\": 700,\n" + |
| 50 | + " \"numPorts\": \"1\"\n" + |
| 51 | + " },\n" + |
| 52 | + " \"scalable\": false,\n" + |
| 53 | + " \"softConstraints\": [],\n" + |
| 54 | + " \"hardConstraints\": [],\n" + |
| 55 | + " \"containerAttributes\": {\n" + |
| 56 | + " \"containerSkuID\": \"small\"\n" + |
| 57 | + " }\n" + |
| 58 | + " }\n" + |
| 59 | + " }\n" + |
| 60 | + " },\n" + |
| 61 | + " \"parameters\": [],\n" + |
| 62 | + " \"labels\": [\n" + |
| 63 | + " {\n" + |
| 64 | + " \"name\": \"_mantis.user\",\n" + |
| 65 | + " \"value\": \"ggao\"\n" + |
| 66 | + " },\n" + |
| 67 | + " {\n" + |
| 68 | + " \"name\": \"_mantis.ownerEmail\",\n" + |
| 69 | + " \"value\": \"ggao@netflix.com\"\n" + |
| 70 | + " },\n" + |
| 71 | + " {\n" + |
| 72 | + " \"name\": \"_mantis.artifact\",\n" + |
| 73 | + " \"value\": \"titus-migration-thin-1.2.3\"\n" + |
| 74 | + " },\n" + |
| 75 | + " {\n" + |
| 76 | + " \"name\": \"_mantis.artifact.version\",\n" + |
| 77 | + " \"value\": \"snapshot\"\n" + |
| 78 | + " }\n" + |
| 79 | + " ],\n" + |
| 80 | + " \"migrationConfig\": {\n" + |
| 81 | + " \"strategy\": \"PERCENTAGE\",\n" + |
| 82 | + " \"configString\": \"{\\\"percentToMove\\\":25,\\\"intervalMs\\\":60000}\"\n" + |
| 83 | + " },\n" + |
| 84 | + " \"slaMin\": \"0\",\n" + |
| 85 | + " \"slaMax\": \"0\",\n" + |
| 86 | + " \"deploymentStrategy\": {\n" + |
| 87 | + " \"resourceClusterId\": \"mantisResourceClusterStagingDefault3\"\n" + |
| 88 | + " },\n" + |
| 89 | + " \"cronSpec\": null,\n" + |
| 90 | + " \"cronPolicy\": \"KEEP_EXISTING\"\n" + |
| 91 | + " },\n" + |
| 92 | + " \"owner\": {\n" + |
| 93 | + " \"contactEmail\": \"ggao@netflix.com\",\n" + |
| 94 | + " \"description\": \"\",\n" + |
| 95 | + " \"name\": \"Gigi Gao\",\n" + |
| 96 | + " \"repo\": \"\",\n" + |
| 97 | + " \"teamName\": \"\"\n" + |
| 98 | + " },\n" + |
| 99 | + " \"jobPrincipal\": {\n" + |
| 100 | + " \"email\": \"ggao@netflix.com\"\n" + |
| 101 | + " }\n" + |
| 102 | + "}"; |
| 103 | + |
| 104 | + NamedJobDefinition njd = objectMapper.readValue(json, NamedJobDefinition.class); |
| 105 | + |
| 106 | + // Verify the object was deserialized |
| 107 | + assertNotNull("NamedJobDefinition should not be null", njd); |
| 108 | + assertNotNull("JobDefinition should not be null", njd.getJobDefinition()); |
| 109 | + assertNotNull("Owner should not be null", njd.getOwner()); |
| 110 | + |
| 111 | + // Verify jobPrincipal was deserialized correctly |
| 112 | + assertNotNull("JobPrincipal should not be null", njd.getJobPrincipal()); |
| 113 | + assertEquals("JobPrincipal email should match", "ggao@netflix.com", njd.getJobPrincipal().getEmail()); |
| 114 | + assertEquals("JobPrincipal type should default to USER", JobPrincipal.PrincipalType.USER, njd.getJobPrincipal().getType()); |
| 115 | + |
| 116 | + // Verify job definition fields |
| 117 | + assertEquals("Job name should match", "TestGigi2", njd.getJobDefinition().getName()); |
| 118 | + assertEquals("User should match", "ggao", njd.getJobDefinition().getUser()); |
| 119 | + |
| 120 | + // Verify owner fields |
| 121 | + assertEquals("Owner email should match", "ggao@netflix.com", njd.getOwner().getContactEmail()); |
| 122 | + assertEquals("Owner name should match", "Gigi Gao", njd.getOwner().getName()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testDeserializeNamedJobDefinitionWithoutJobPrincipal() throws Exception { |
| 127 | + // Test that jobPrincipal can be null for backward compatibility |
| 128 | + String json = "{\n" + |
| 129 | + " \"jobDefinition\": {\n" + |
| 130 | + " \"name\": \"TestJob\",\n" + |
| 131 | + " \"user\": \"testuser\",\n" + |
| 132 | + " \"jobJarFileLocation\": \"https://example.com/test.zip\",\n" + |
| 133 | + " \"version\": \"1.0.0\",\n" + |
| 134 | + " \"schedulingInfo\": {\n" + |
| 135 | + " \"stages\": {\n" + |
| 136 | + " \"1\": {\n" + |
| 137 | + " \"numberOfInstances\": \"1\",\n" + |
| 138 | + " \"machineDefinition\": {\n" + |
| 139 | + " \"cpuCores\": 1,\n" + |
| 140 | + " \"memoryMB\": 1024,\n" + |
| 141 | + " \"diskMB\": 1024,\n" + |
| 142 | + " \"networkMbps\": 128,\n" + |
| 143 | + " \"numPorts\": \"1\"\n" + |
| 144 | + " },\n" + |
| 145 | + " \"scalable\": false\n" + |
| 146 | + " }\n" + |
| 147 | + " }\n" + |
| 148 | + " },\n" + |
| 149 | + " \"parameters\": [],\n" + |
| 150 | + " \"labels\": []\n" + |
| 151 | + " },\n" + |
| 152 | + " \"owner\": {\n" + |
| 153 | + " \"contactEmail\": \"test@example.com\",\n" + |
| 154 | + " \"description\": \"\",\n" + |
| 155 | + " \"name\": \"Test User\",\n" + |
| 156 | + " \"repo\": \"\",\n" + |
| 157 | + " \"teamName\": \"\"\n" + |
| 158 | + " }\n" + |
| 159 | + "}"; |
| 160 | + |
| 161 | + NamedJobDefinition njd = objectMapper.readValue(json, NamedJobDefinition.class); |
| 162 | + |
| 163 | + // Verify the object was deserialized |
| 164 | + assertNotNull("NamedJobDefinition should not be null", njd); |
| 165 | + assertNotNull("JobDefinition should not be null", njd.getJobDefinition()); |
| 166 | + assertNotNull("Owner should not be null", njd.getOwner()); |
| 167 | + |
| 168 | + // JobPrincipal can be null for backward compatibility |
| 169 | + // This ensures old payloads without jobPrincipal still work |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments