Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W-17472621: Add a registry object to query the default encoding of the deployed app #14030

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
*/
package org.mule.runtime.core.api;

import static org.mule.runtime.api.message.Message.of;

import static java.nio.charset.Charset.defaultCharset;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mule.runtime.api.message.Message.of;

import org.mule.runtime.api.exception.MuleException;
import org.mule.runtime.api.message.Message;
import org.mule.runtime.api.metadata.DataType;
import org.mule.runtime.core.api.config.MuleConfiguration;
import org.mule.runtime.core.api.transformer.Transformer;
import org.mule.runtime.core.internal.transformer.ExtendedTransformationService;
import org.mule.tck.junit4.AbstractMuleTestCase;
Expand All @@ -29,10 +32,8 @@ public class TransformationServiceNullTransformationTestCase extends AbstractMul

@Test
public void transformerIsNeverCalledWithANullValue() throws MuleException {
MuleContext muleContext = mock(MuleContext.class);
MuleConfiguration muleConfiguration = mock(MuleConfiguration.class);
when(muleContext.getConfiguration()).thenReturn(muleConfiguration);
ExtendedTransformationService transformationService = new ExtendedTransformationService(muleContext);
ExtendedTransformationService transformationService = new ExtendedTransformationService();
transformationService.setArtifactEncoding(() -> defaultCharset());

Transformer transformer1 = mock(Transformer.class);
when(transformer1.transform(any(Object.class))).thenReturn(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
import static org.mule.test.allure.AllureConstants.RegistryFeature.REGISTRY;
import static org.mule.test.allure.AllureConstants.RegistryFeature.TransfromersStory.TRANSFORMERS;

import static java.nio.charset.Charset.defaultCharset;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -26,7 +27,6 @@
import org.mule.runtime.api.exception.MuleException;
import org.mule.runtime.api.message.Message;
import org.mule.runtime.api.metadata.DataType;
import org.mule.runtime.core.api.MuleContext;
import org.mule.runtime.core.api.transformer.DataTypeConversionResolver;
import org.mule.runtime.core.api.transformer.MessageTransformerException;
import org.mule.runtime.core.api.transformer.Transformer;
Expand Down Expand Up @@ -66,15 +66,14 @@ private class D {

}

private final MuleContext muleContext = mock(MuleContext.class, RETURNS_DEEP_STUBS);
private final DataTypeConversionResolver conversionResolver = mock(DataTypeConversionResolver.class);
private ExtendedTransformationService transformationService;

@Before
public void setUp() throws Exception {
when(muleContext.getDataTypeConverterResolver()).thenReturn(conversionResolver);
this.transformationService = new ExtendedTransformationService(muleContext);
this.transformationService = new ExtendedTransformationService();
transformationService.setDataTypeConversionResolver(conversionResolver);
transformationService.setArtifactEncoding(() -> defaultCharset());
}

private static final DataType dataTypeB = DataType.fromType(B.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.mule.runtime.core.internal.context.DefaultMuleContext.currentMuleContext;
import static org.mule.test.allure.AllureConstants.MuleEvent.MULE_EVENT;

import static java.nio.charset.Charset.defaultCharset;
import static java.util.Collections.singletonMap;

import static org.hamcrest.CoreMatchers.equalTo;
Expand All @@ -37,6 +38,7 @@
import org.mule.runtime.core.api.transformer.TransformerException;
import org.mule.runtime.core.internal.context.MuleContextWithRegistry;
import org.mule.runtime.core.internal.security.DefaultSecurityContextFactory;
import org.mule.runtime.core.internal.serialization.JavaObjectSerializer;
import org.mule.runtime.core.internal.transformer.TransformersRegistry;
import org.mule.runtime.core.internal.transformer.simple.ByteArrayToObject;
import org.mule.runtime.core.internal.transformer.simple.SerializableToByteArray;
Expand All @@ -62,8 +64,11 @@
@Feature(MULE_EVENT)
public class MuleEventTestCase extends AbstractMuleContextTestCase {

private JavaObjectSerializer objectSerializer;

@Before
public void setUp() {
objectSerializer = new JavaObjectSerializer(this.getClass().getClassLoader());
currentMuleContext.set(muleContext);
}

Expand All @@ -77,7 +82,8 @@ public void serialization() throws Exception {
Serializable serialized = (Serializable) createSerializableToByteArrayTransformer().transform(testEvent());
assertNotNull(serialized);
ByteArrayToObject trans = new ByteArrayToObject();
trans.setObjectSerializer(muleContext.getObjectSerializer());
trans.setArtifactEncoding(() -> defaultCharset());
trans.setObjectSerializer(objectSerializer);
PrivilegedEvent deserialized = (PrivilegedEvent) trans.transform(serialized);

// Assert that deserialized event is not null
Expand All @@ -86,7 +92,8 @@ public void serialization() throws Exception {

private Transformer createSerializableToByteArrayTransformer() {
SerializableToByteArray transformer = new SerializableToByteArray();
transformer.setObjectSerializer(muleContext.getObjectSerializer());
transformer.setArtifactEncoding(() -> defaultCharset());
transformer.setObjectSerializer(objectSerializer);

return transformer;
}
Expand All @@ -105,7 +112,8 @@ public void testEventSerializationRestart() throws Exception {
muleContext = createMuleContext();
muleContext.start();
ByteArrayToObject trans = new ByteArrayToObject();
trans.setObjectSerializer(muleContext.getObjectSerializer());
trans.setArtifactEncoding(() -> defaultCharset());
trans.setObjectSerializer(objectSerializer);

// Recreate and register artifacts (this would happen if using any kind of static config e.g. XML)
createAndRegisterTransformersEndpointBuilderService();
Expand All @@ -131,8 +139,8 @@ public void testMuleEventSerializationWithRawPayload() throws Exception {
}
PrivilegedEvent testEvent = this.<PrivilegedEvent.Builder>getEventBuilder()
.message(of(new ByteArrayInputStream(payload.toString().getBytes()))).build();
byte[] serializedEvent = muleContext.getObjectSerializer().getExternalProtocol().serialize(testEvent);
testEvent = muleContext.getObjectSerializer().getExternalProtocol().deserialize(serializedEvent);
byte[] serializedEvent = objectSerializer.getExternalProtocol().serialize(testEvent);
testEvent = objectSerializer.getExternalProtocol().deserialize(serializedEvent);

assertArrayEquals((byte[]) testEvent.getMessage().getPayload().getValue(), payload.toString().getBytes());
}
Expand Down Expand Up @@ -392,7 +400,8 @@ public void securityContextSerialization() throws Exception {
Serializable serialized = (Serializable) createSerializableToByteArrayTransformer().transform(event);
assertNotNull(serialized);
ByteArrayToObject trans = new ByteArrayToObject();
trans.setObjectSerializer(muleContext.getObjectSerializer());
trans.setArtifactEncoding(() -> defaultCharset());
trans.setObjectSerializer(objectSerializer);
CoreEvent deserialized = (CoreEvent) trans.transform(serialized);

assertThat(deserialized.getSecurityContext().getAuthentication().getPrincipal(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.mule.runtime.core.api.transformer;

import static org.junit.Assert.fail;

import org.mule.runtime.api.metadata.DataType;
import org.mule.tck.core.transformer.AbstractTransformerTestCase;

Expand All @@ -30,7 +31,7 @@ public Object getResultData() {

@Override
public Transformer getTransformer() throws Exception {
return transformer;
return configureTransformer(transformer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,35 @@
*/
package org.mule.runtime.core.api.transformer;

import static java.nio.charset.Charset.defaultCharset;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.mule.runtime.api.metadata.DataType;
import org.mule.runtime.core.api.MuleContext;
import org.mule.runtime.core.api.config.MuleConfiguration;
import org.mule.tck.junit4.AbstractMuleTestCase;
import org.mule.tck.size.SmallTest;

import com.google.common.base.Charsets;

import java.nio.charset.Charset;

import org.junit.Before;
import org.junit.Test;

@SmallTest
public class TransformerSourceTypeEnforcementTestCase extends AbstractMuleTestCase {

private MuleContext muleContext = mock(MuleContext.class);
private MuleConfiguration muleConfiguration = mock(MuleConfiguration.class);

@Before
public void setUp() throws Exception {
when(muleConfiguration.getDefaultEncoding()).thenReturn(Charsets.UTF_8.name());
when(muleContext.getConfiguration()).thenReturn(muleConfiguration);
}

@Test
public void rejectsBadInputIfEnforcementOn() throws TransformerException {
AbstractTransformer transformer = createDummyTransformer(true);

try {
transformer.transform("TEST");
fail("Transformation should fail because source type is not supported");
} catch (TransformerException expected) {
}
assertThrows(TransformerException.class, () -> transformer.transform("TEST"));
}

@Test
public void rejectsBadInputUsingDefaultEnforcement() throws TransformerException {
AbstractTransformer transformer = createDummyTransformer(true);

try {
transformer.transform("TEST");
fail("Transformation should fail because source type is not supported");
} catch (TransformerException expected) {
}
assertThrows(TransformerException.class, () -> transformer.transform("TEST"));
}

@Test
Expand All @@ -63,8 +43,6 @@ public void transformsValidSourceTypeWithNoCheckForEnforcement() throws Transfor
transformer.sourceTypes.add(DataType.STRING);
transformer.setReturnDataType(DataType.STRING);

when(muleContext.getConfiguration()).thenReturn(muleConfiguration);

Object result = transformer.transform("TEST");
assertEquals("TRANSFORMED", result);
}
Expand All @@ -79,7 +57,7 @@ protected Object doTransform(Object src, Charset enc) throws TransformerExceptio
};

result.sourceTypes.add(DataType.BYTE_ARRAY);
result.setMuleContext(muleContext);
result.setArtifactEncoding(() -> defaultCharset());
result.setIgnoreBadInput(ignoreBadInput);

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/
package org.mule.runtime.core.internal.el.dataweave;

import static java.nio.charset.Charset.defaultCharset;
import static java.util.Optional.of;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -30,7 +32,9 @@ public void setUp() throws InitialisationException {
weaveExpressionExecutor = new WeaveDefaultExpressionLanguageFactoryService(null);
when(registry.lookupByType(DefaultExpressionLanguageFactoryService.class)).thenReturn(of(weaveExpressionExecutor));
expressionLanguage =
new DataWeaveExpressionLanguageAdaptor(muleContext, registry, weaveExpressionExecutor, getFeatureFlaggingService());
new DataWeaveExpressionLanguageAdaptor(muleContext, registry, muleContext.getConfiguration(),
() -> defaultCharset(),
weaveExpressionExecutor, getFeatureFlaggingService());
expressionLanguage.initialise();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static java.lang.String.format;
import static java.lang.System.clearProperty;
import static java.lang.System.setProperty;
import static java.nio.charset.Charset.defaultCharset;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonMap;
import static java.util.Collections.unmodifiableMap;
Expand Down Expand Up @@ -92,6 +93,7 @@
import org.mule.runtime.api.security.Authentication;
import org.mule.runtime.api.security.DefaultMuleAuthentication;
import org.mule.runtime.core.api.MuleContext;
import org.mule.runtime.core.api.config.MuleConfiguration;
import org.mule.runtime.core.api.event.CoreEvent;
import org.mule.runtime.core.api.expression.ExpressionRuntimeException;
import org.mule.runtime.core.api.security.DefaultMuleCredentials;
Expand Down Expand Up @@ -474,6 +476,8 @@ public void flowNameBinding() {
public void expressionExecutorIsNotDisposedWhenAlreadyDisposed() throws MuleException {
DataWeaveExpressionLanguageAdaptor expressionLanguageAdaptor =
new DataWeaveExpressionLanguageAdaptor(mock(MuleContext.class, RETURNS_DEEP_STUBS), registry,
mock(MuleConfiguration.class, RETURNS_DEEP_STUBS),
() -> defaultCharset(),
genericExpressionLanguageService, getFeatureFlaggingService());
ExpressionLanguage mockedExpressionLanguage = mock(ExpressionLanguage.class);
when(genericExpressionLanguageService.create(any())).thenReturn(mockedExpressionLanguage);
Expand All @@ -488,6 +492,8 @@ public void expressionExecutorIsNotDisposedWhenAlreadyDisposed() throws MuleExce
public void payloadExpressionShouldNotBeEvaluate() throws MuleException {
DataWeaveExpressionLanguageAdaptor expressionLanguageAdaptor =
new DataWeaveExpressionLanguageAdaptor(mock(MuleContext.class, RETURNS_DEEP_STUBS), registry,
mock(MuleConfiguration.class, RETURNS_DEEP_STUBS),
() -> defaultCharset(),
genericExpressionLanguageService, getFeatureFlaggingService());
expressionLanguageAdaptor.initialise();
expressionLanguageAdaptor.evaluate("#[payload]", testEvent(), bindingContext);
Expand All @@ -499,6 +505,8 @@ public void payloadExpressionShouldNotBeEvaluate() throws MuleException {
public void evaluateNoEventDoesntInstantiateExtraBindingContexts() throws InitialisationException {
DataWeaveExpressionLanguageAdaptor expressionLanguageAdaptor =
new DataWeaveExpressionLanguageAdaptor(mock(MuleContext.class, RETURNS_DEEP_STUBS), registry,
mock(MuleConfiguration.class, RETURNS_DEEP_STUBS),
() -> defaultCharset(),
genericExpressionLanguageService, getFeatureFlaggingService());

expressionLanguageAdaptor.initialise();
Expand All @@ -511,6 +519,8 @@ public void evaluateNoEventDoesntInstantiateExtraBindingContexts() throws Initia
public void evaluateWithDataTypeNoEventDoesntInstantiateExtraBindingContexts() throws InitialisationException {
DataWeaveExpressionLanguageAdaptor expressionLanguageAdaptor =
new DataWeaveExpressionLanguageAdaptor(mock(MuleContext.class, RETURNS_DEEP_STUBS), registry,
mock(MuleConfiguration.class, RETURNS_DEEP_STUBS),
() -> defaultCharset(),
genericExpressionLanguageService, getFeatureFlaggingService());

expressionLanguageAdaptor.initialise();
Expand All @@ -523,6 +533,8 @@ public void evaluateWithDataTypeNoEventDoesntInstantiateExtraBindingContexts() t
public void evaluateNoLocationNoEventWithDataTypeDoesntInstantiateExtraBindingContexts() throws InitialisationException {
DataWeaveExpressionLanguageAdaptor expressionLanguageAdaptor =
new DataWeaveExpressionLanguageAdaptor(mock(MuleContext.class, RETURNS_DEEP_STUBS), registry,
mock(MuleConfiguration.class, RETURNS_DEEP_STUBS),
() -> defaultCharset(),
genericExpressionLanguageService, getFeatureFlaggingService());

expressionLanguageAdaptor.initialise();
Expand All @@ -535,6 +547,8 @@ public void evaluateNoLocationNoEventWithDataTypeDoesntInstantiateExtraBindingCo
public void evaluateNoLocationNoEventDoesntInstantiateExtraBindingContexts() throws InitialisationException {
DataWeaveExpressionLanguageAdaptor expressionLanguageAdaptor =
new DataWeaveExpressionLanguageAdaptor(mock(MuleContext.class, RETURNS_DEEP_STUBS), registry,
mock(MuleConfiguration.class, RETURNS_DEEP_STUBS),
() -> defaultCharset(),
genericExpressionLanguageService, getFeatureFlaggingService());

expressionLanguageAdaptor.initialise();
Expand All @@ -547,6 +561,8 @@ public void evaluateNoLocationNoEventDoesntInstantiateExtraBindingContexts() thr
public void evaluateLogExpressionNoLocationNoEventDoesntInstantiateExtraBindingContexts() throws InitialisationException {
DataWeaveExpressionLanguageAdaptor expressionLanguageAdaptor =
new DataWeaveExpressionLanguageAdaptor(mock(MuleContext.class, RETURNS_DEEP_STUBS), registry,
mock(MuleConfiguration.class, RETURNS_DEEP_STUBS),
() -> defaultCharset(),
genericExpressionLanguageService, getFeatureFlaggingService());

expressionLanguageAdaptor.initialise();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static org.mule.runtime.api.metadata.MediaType.APPLICATION_XML;
import static org.mule.tck.junit4.matcher.DataTypeMatcher.like;

import static java.nio.charset.Charset.defaultCharset;
import static java.nio.charset.StandardCharsets.UTF_16;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singletonList;
Expand All @@ -20,14 +21,12 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.mule.runtime.api.message.Message;
import org.mule.runtime.api.metadata.DataType;
import org.mule.runtime.api.metadata.MediaType;
import org.mule.runtime.core.api.MuleContext;
import org.mule.runtime.core.api.event.CoreEvent;
import org.mule.runtime.core.api.transformer.Transformer;
import org.mule.runtime.core.internal.transformer.ExtendedTransformationService;
Expand All @@ -50,13 +49,12 @@ public class MuleMessageDataTypePropagationTestCase extends AbstractMuleTestCase
public static final MediaType APPLICATION_XML_DEFAULT = APPLICATION_XML.withCharset(DEFAULT_ENCODING);
public static final MediaType APPLICATION_XML_CUSTOM = APPLICATION_XML.withCharset(CUSTOM_ENCODING);

private final MuleContext muleContext = mock(MuleContext.class, RETURNS_DEEP_STUBS);
private ExtendedTransformationService transformationService;

@Before
public void setUp() throws Exception {
when(muleContext.getConfiguration().getDefaultEncoding()).thenReturn(DEFAULT_ENCODING.name());
transformationService = new ExtendedTransformationService(muleContext);
transformationService = new ExtendedTransformationService();
transformationService.setArtifactEncoding(() -> defaultCharset());
}

@Test
Expand Down
Loading