diff --git a/src/main/java/com/avioconsulting/mule/logger/internal/utils/PayloadTransformer.java b/src/main/java/com/avioconsulting/mule/logger/internal/utils/PayloadTransformer.java index 7940b45..cc064f0 100644 --- a/src/main/java/com/avioconsulting/mule/logger/internal/utils/PayloadTransformer.java +++ b/src/main/java/com/avioconsulting/mule/logger/internal/utils/PayloadTransformer.java @@ -52,22 +52,25 @@ public String transformPayload(CustomLoggerConfiguration loggerConfig, if (payloadString == null) return payloadString; Compressor compressor = loggerConfig.getCompressor(); - Result executeCompress = null; + Result executionResult = null; if (compressor != null) { - executeCompress = compressPayload(loggerConfig, payloadString); - try { - payloadString = Base64.encodeBytes(convertToByteArray(executeCompress.getOutput(), streamingHelper)); - } catch (IOException e) { - throw new RuntimeException("Exception while transforming payload", e); - } + executionResult = compressPayload(loggerConfig, payloadString); } EncryptionAlgorithm encryptionAlgorithm = loggerConfig.getEncryptionAlgorithm(); if (encryptionAlgorithm != null) { - payloadString = encryptPayload(loggerConfig, streamingHelper, executeCompress, encryptionAlgorithm, + executionResult = encryptPayload(loggerConfig, streamingHelper, executionResult, encryptionAlgorithm, payloadString); } + if (executionResult != null) { + try { + payloadString = Base64.encodeBytes(convertToByteArray(executionResult.getOutput(), streamingHelper)); + } catch (IOException e) { + throw new RuntimeException("Exception while transforming payload", e); + } + } + return payloadString; } @@ -88,12 +91,11 @@ public Result compressPayload(CustomLoggerConfiguration logge .addParameter("content", new ByteArrayInputStream(payload.getBytes())) .addParameter("compressor", compressorStrategy); try { - executeCompress = loggerConfig.getExtensionsClient().execute("Compression", "compress", + return loggerConfig.getExtensionsClient().execute("Compression", "compress", parametersBuilder.build()); } catch (Exception e) { throw new RuntimeException("Compression Exception", e); } - return executeCompress; } /** @@ -106,7 +108,7 @@ public Result compressPayload(CustomLoggerConfiguration logge * @since 2.1.0 * @return encrypted string */ - public String encryptPayload(CustomLoggerConfiguration loggerConfig, + public Result encryptPayload(CustomLoggerConfiguration loggerConfig, StreamingHelper streamingHelper, Result executeCompress, EncryptionAlgorithm encryptionAlgorithm, String payload) { /* @@ -127,11 +129,9 @@ public String encryptPayload(CustomLoggerConfiguration loggerConfig, .addParameter("algorithm", jceEncryptionPbeAlgorithm) .addParameter("password", loggerConfig.getEncryptionPassword()); try { - Result executeEncrypt = loggerConfig.getExtensionsClient().execute("Crypto", + return loggerConfig.getExtensionsClient().execute("Crypto", "jceEncryptPbe", encryptionParametersBuilder.build()); - return Base64 - .encodeBytes(convertToByteArray(executeEncrypt.getOutput(), streamingHelper)); } catch (Exception e) { throw new RuntimeException("Encryption Error", e); } diff --git a/src/test/java/com/avioconsulting/mule/logger/internal/CustomLoggerOperationTest.java b/src/test/java/com/avioconsulting/mule/logger/internal/CustomLoggerOperationTest.java index afb51f1..dadece3 100644 --- a/src/test/java/com/avioconsulting/mule/logger/internal/CustomLoggerOperationTest.java +++ b/src/test/java/com/avioconsulting/mule/logger/internal/CustomLoggerOperationTest.java @@ -118,13 +118,12 @@ public void log_encryptedPayloadIsSet_compressionNull_test() throws MuleExceptio PayloadTransformer payloadTransformer = new PayloadTransformer(); PayloadTransformer spyTransformer = spy(payloadTransformer); - // mock response from convertToByteArray doReturn(examplePayload.getBytes()).when(spyTransformer) .convertToByteArray(any(), any()); // call method - String resultPayload = spyTransformer.encryptPayload( + Result executionResult = spyTransformer.encryptPayload( loggerConfig, mock(StreamingHelper.class), executeCompress, @@ -133,7 +132,7 @@ public void log_encryptedPayloadIsSet_compressionNull_test() throws MuleExceptio // assertions verify(extensionsClient, atMost(1)).execute(any(), any(), any()); - Assert.assertEquals("RXhhbXBsZSBQYXlsb2FkIFRleHQ=", resultPayload); + Assert.assertNotNull(executionResult); } @Test