From 7b249d48d1aa6b26655c453537385968163df4bf Mon Sep 17 00:00:00 2001 From: kcichonczyk Date: Wed, 8 May 2024 10:15:04 +0200 Subject: [PATCH] [ACS-5648] handling NPE when T-Engines are unavailable --- .../transform/base/TransformController.java | 5 +++++ .../transform/base/TransformControllerTest.java | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java b/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java index 1bd86ad88..41df5f37e 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java @@ -68,6 +68,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.concurrent.atomic.AtomicReference; import static java.text.MessageFormat.format; @@ -88,6 +89,7 @@ import static org.alfresco.transform.common.RequestParamMap.TARGET_MIMETYPE; import static org.alfresco.transform.config.CoreVersionDecorator.setOrClearCoreVersion; import static org.springframework.http.HttpStatus.BAD_REQUEST; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; import static org.springframework.http.HttpStatus.OK; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE; @@ -268,6 +270,9 @@ public ResponseEntity transformConfig( { logger.info("GET Transform Config version: " + configVersion); TransformConfig transformConfig = ((TransformRegistry) transformRegistry).getTransformConfig(); + if(Objects.isNull(transformConfig)) { + throw new TransformException(INTERNAL_SERVER_ERROR, "Transform Config unavailable."); + } transformConfig = setOrClearCoreVersion(transformConfig, configVersion); return new ResponseEntity<>(transformConfig, OK); } diff --git a/engines/base/src/test/java/org/alfresco/transform/base/TransformControllerTest.java b/engines/base/src/test/java/org/alfresco/transform/base/TransformControllerTest.java index b1ae9e434..cc92c5f5b 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/TransformControllerTest.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/TransformControllerTest.java @@ -78,6 +78,7 @@ import ch.qos.logback.core.AppenderBase; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableMap; +import org.alfresco.transform.base.registry.TransformRegistry; import org.codehaus.plexus.util.FileUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -87,6 +88,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; import org.springframework.http.ResponseEntity; @@ -133,6 +135,8 @@ public class TransformControllerTest public File tempDir; @MockBean protected SharedFileStoreClient fakeSfsClient; + @SpyBean + private TransformRegistry transformRegistry; static void resetProbeForTesting(TransformController transformController) { @@ -495,4 +499,13 @@ public void testInterceptOfTransformException_noTransformers() throws Exception .andExpect(content().string(containsString("TwoCustomTransformers Error Page"))) .andExpect(content().string(containsString("No transforms for: text/plain (5 bytes) -> application/pdf unknown=1"))); } + + @Test + public void testUnavailableTransformConfig() throws Exception { + when(transformRegistry.getTransformConfig()).thenReturn(null); + + mockMvc.perform(MockMvcRequestBuilders.get(ENDPOINT_TRANSFORM_CONFIG)) + .andExpect(status().isInternalServerError()) + .andExpect(content().string(containsString("Transform Config unavailable."))); + } }