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

[#1560] Feature/intermediate representation data format #1561

Expand Up @@ -148,4 +148,9 @@ public Instant getTimestamp() {
public LazyDeserializingObject<MetaData> getMetaData() {
return metaData;
}

@Override
public boolean canConvertDataTo(Class<?> requiredType) {
return serializer.getConverter().canConvert(data.getContentType(), requiredType);
}
}
Expand Up @@ -139,4 +139,12 @@ <T> IntermediateEventRepresentation upcast(SerializedType outputType, Class<T> e
* @return the MetaData of the message wrapping the object to upcast, if available
*/
LazyDeserializingObject<MetaData> getMetaData();

/**
* Checks if the data can be converted to the given {@code requiredType}.
*
* @param requiredType the type to validate if the contained data can be converted to.
* @return true, if the intermediate representation's data can be converted to desired type, false otherwise
*/
boolean canConvertDataTo(Class<?> requiredType);
}
Expand Up @@ -136,4 +136,9 @@ public LazyDeserializingObject<MetaData> getMetaData() {
}
return metaData;
}

@Override
public boolean canConvertDataTo(Class<?> requiredType) {
return converter.canConvert(source.getData().getContentType(), requiredType);
}
}
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2010-2020. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.axonframework.serialization.upcasting.event;

import org.axonframework.eventhandling.EventData;
import org.axonframework.eventhandling.GenericDomainEventMessage;
import org.axonframework.serialization.Converter;
import org.axonframework.serialization.Serializer;
import org.axonframework.serialization.TestSerializer;
import org.axonframework.utils.TestDomainEventEntry;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;

/**
* Test for intermediate representation.
*
* @author Simon Zambrovski
*/
class IntermediateRepresentationTest {

private final static Serializer serializer = TestSerializer.XSTREAM.getSerializer();

@Test
public void canConvertDataTo() {
EventData<?> eventData = new TestDomainEventEntry(
new GenericDomainEventMessage<>("test", "aggregateId", 0, "someString"), serializer
);
Serializer serializer = mock(Serializer.class);
Converter converter = mock(Converter.class);
when(serializer.getConverter()).thenReturn(converter);
when(converter.canConvert(any(), eq(String.class))).thenReturn(true);

IntermediateEventRepresentation input = new InitialEventRepresentation(eventData, serializer);
EventUpcasterChain eventUpcasterChain = new EventUpcasterChain(
new IntermediateRepresentationTest.MyEventUpcaster()
);
List<IntermediateEventRepresentation> result = eventUpcasterChain.upcast(Stream.of(input)).collect(toList());
assertEquals(1, result.size());


assertTrue(input.canConvertDataTo(String.class));
assertTrue(result.get(0).canConvertDataTo(String.class));

verify(converter).canConvert(String.class, String.class);
}

private static class MyEventUpcaster extends SingleEventUpcaster {

@Override
protected boolean canUpcast(IntermediateEventRepresentation intermediateRepresentation) {
return true;
}

@Override
protected IntermediateEventRepresentation doUpcast(IntermediateEventRepresentation intermediateRepresentation) {
return new UpcastedEventRepresentation<>(
intermediateRepresentation.getType(),
intermediateRepresentation,
Function.identity(),
Function.identity(),
Object.class,
serializer.getConverter()
);
}
}
}