|
| 1 | +/* |
| 2 | + * Copyright (C) 2017-2026 HERE Europe B.V. |
| 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 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +package com.here.xyz.jobs.service; |
| 21 | + |
| 22 | +import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 26 | + |
| 27 | +import com.here.xyz.jobs.steps.JobCompiler.CompilationError; |
| 28 | +import com.here.xyz.util.service.BaseHttpServerVerticle.ValidationException; |
| 29 | +import com.here.xyz.util.service.HttpException; |
| 30 | +import com.here.xyz.util.service.errors.DetailedHttpException; |
| 31 | +import com.here.xyz.util.service.errors.ErrorManager; |
| 32 | +import java.util.Map; |
| 33 | +import org.junit.jupiter.api.BeforeAll; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +class JobApiErrorNormalizationTest { |
| 37 | + |
| 38 | + private final JobApi api = new JobApi(); |
| 39 | + |
| 40 | + @BeforeAll |
| 41 | + static void loadErrorDefinitions() { |
| 42 | + try { |
| 43 | + ErrorManager.loadErrors("job-errors.json"); |
| 44 | + } |
| 45 | + catch (RuntimeException e) { |
| 46 | + Throwable cause = e.getCause(); |
| 47 | + if (!(cause instanceof IllegalStateException) || !cause.getMessage().contains("already registered")) |
| 48 | + throw e; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void mapsCompilationErrorsToBadRequestWithJobId() { |
| 54 | + Throwable normalized = api.normalizeJobCreationError(new CompilationError("broken job"), "job-123"); |
| 55 | + |
| 56 | + DetailedHttpException detailed = assertInstanceOf(DetailedHttpException.class, normalized); |
| 57 | + assertEquals(BAD_REQUEST.code(), detailed.status.code()); |
| 58 | + assertEquals("E319002", detailed.errorDefinition.getCode()); |
| 59 | + assertNotNull(detailed.errorDetails); |
| 60 | + assertEquals("job-123", detailed.errorDetails.get("jobId")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void mapsValidationErrorsToBadRequestWithJobId() { |
| 65 | + Throwable normalized = api.normalizeJobCreationError(new ValidationException("invalid payload"), "job-456"); |
| 66 | + |
| 67 | + DetailedHttpException detailed = assertInstanceOf(DetailedHttpException.class, normalized); |
| 68 | + assertEquals(BAD_REQUEST.code(), detailed.status.code()); |
| 69 | + assertEquals("E319003", detailed.errorDefinition.getCode()); |
| 70 | + assertNotNull(detailed.errorDetails); |
| 71 | + assertEquals("job-456", detailed.errorDetails.get("jobId")); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void mapsNestedClassCastErrorsToBadRequestWithJobId() { |
| 76 | + Throwable error = new RuntimeException(new ClassCastException("layerIds must be array")); |
| 77 | + |
| 78 | + Throwable normalized = api.normalizeJobCreationError(error, "job-789"); |
| 79 | + |
| 80 | + DetailedHttpException detailed = assertInstanceOf(DetailedHttpException.class, normalized); |
| 81 | + assertEquals(BAD_REQUEST.code(), detailed.status.code()); |
| 82 | + assertEquals("E319003", detailed.errorDefinition.getCode()); |
| 83 | + assertNotNull(detailed.errorDetails); |
| 84 | + assertEquals("job-789", detailed.errorDetails.get("jobId")); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void mapsIllegalArgumentErrorsToBadRequestWithJobId() { |
| 89 | + Throwable normalized = api.normalizeJobCreationError(new IllegalArgumentException("bad argument"), "job-abc"); |
| 90 | + |
| 91 | + HttpException http = assertInstanceOf(HttpException.class, normalized); |
| 92 | + assertEquals(BAD_REQUEST.code(), http.status.code()); |
| 93 | + assertEquals(Map.of("jobId", "job-abc"), http.errorDetails); |
| 94 | + } |
| 95 | +} |
| 96 | + |
0 commit comments