Skip to content

Commit

Permalink
#72 First version for PlainByteArraySerializer/Deserializer. Modulbin…
Browse files Browse the repository at this point in the history
…ding not yet implemented
  • Loading branch information
chb-ppi committed Aug 20, 2021
1 parent 920df5e commit 952d80c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import de.ppi.deepsampler.persistence.json.extension.PlainByteArrayDeserializer;
import de.ppi.deepsampler.persistence.json.extension.PlainByteArraySerializer;
import de.ppi.deepsampler.persistence.json.extension.DeserializationExtension;
import de.ppi.deepsampler.persistence.json.extension.SerializationExtension;

Expand Down Expand Up @@ -50,6 +52,9 @@ protected ObjectMapper createObjectMapper() {
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
objectMapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));
objectMapper.registerModule(new JavaTimeModule());

//Hier muss StandardModule für unsere eigenen Serializer

objectMapper.setDefaultTyping(new CustomTypeResolverBuilder(ObjectMapper.DefaultTyping.NON_CONCRETE_AND_ARRAYS, objectMapper.getPolymorphicTypeValidator()));

applyCustomExtensions(objectMapper);
Expand All @@ -70,7 +75,8 @@ private void applyCustomExtensions(final ObjectMapper objectMapper) {
final SerializationExtension<Object> serializationObjExtension = (SerializationExtension<Object>) serializationExtension;
simpleModule.addSerializer(serializationObjExtension.getTypeToSerialize(), serializationObjExtension.getJsonSerializer());
}

simpleModule.addSerializer(byte[].class, new PlainByteArraySerializer());
simpleModule.addDeserializer(byte[].class,new PlainByteArrayDeserializer());
objectMapper.registerModule(simpleModule);

for (final Module module : moduleList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.ppi.deepsampler.persistence.json.extension;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.IOException;

public class PlainByteArrayDeserializer extends JsonDeserializer<byte[]> {
@Override
public byte[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {

ObjectCodec oc = p.getCodec();
JsonNode node = oc.readTree(p);

if (node.isArray() && node.isEmpty()) {
return null;
}

byte[] returnValue = new byte[node.size()];
int i =0;
for (JsonNode value : node) {
returnValue[i]= (byte) value.asInt();
i++;
}

return returnValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package de.ppi.deepsampler.persistence.json.extension;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.type.WritableTypeId;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

public class PlainByteArraySerializer extends StdSerializer<byte[]> {


public PlainByteArraySerializer() {
super(byte[].class);
}

@Override
public void serialize(byte[] value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartArray();
for (byte b: value) {
gen.writeNumber(b );
}
gen.writeEndArray();
}

@Override
public void serializeWithType(byte[] value, JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
// most likely scalar
WritableTypeId typeIdDef = typeSer.writeTypePrefix(gen,
typeSer.typeId(value, JsonToken.VALUE_EMBEDDED_OBJECT));
serialize(value,gen,serializers);
typeSer.writeTypeSuffix(gen, typeIdDef);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import de.ppi.deepsampler.persistence.api.PersistentSampler;
import de.ppi.deepsampler.persistence.error.PersistenceException;
import de.ppi.deepsampler.persistence.json.JsonSourceManager;
import de.ppi.deepsampler.persistence.json.extension.PlainByteArraySerializer;
import de.ppi.deepsampler.provider.testservices.DecoupledTestService;
import de.ppi.deepsampler.provider.testservices.DecoupledTestServiceImpl;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -50,7 +51,7 @@ public abstract class SamplerAspectTest {
private static final TestBean TEST_BEAN_B = new TestBean();
public static final String MY_ECHO_PARAMS = "MY ECHO PARAMS";
public static final String NO_RETURN_VALUE_SAMPLE_ID = "NoReturnValue";
public static final byte[] SOME_BYTES = {1, 2, 3, 4};
public static final byte[] SOME_BYTES = {1, 2, 33, 4};


/**
Expand Down Expand Up @@ -534,9 +535,9 @@ public void byteArrayCanBeRecordedAndLoaded() throws IOException {
Sampler.clear();

final TestService testServiceSampler = Sampler.prepare(TestService.class);
Sample.of(testServiceSampler.echoParameter(any(TestBeanWithBytes.class)));
Sample.of(testServiceSampler.getRandomByteArray(anyInt()));

getTestService().echoParameter(new TestBeanWithBytes(SOME_BYTES));
byte[] expectedArray = getTestService().getRandomByteArray(42);
final String pathToFile = "./record/byteArrayCanBeRecordedAndLoaded.json";
final PersistentSampleManager source = PersistentSampler.source(JsonSourceManager.builder().buildWithFile(pathToFile));
source.record();
Expand All @@ -545,11 +546,14 @@ public void byteArrayCanBeRecordedAndLoaded() throws IOException {
Sampler.clear();
assertTrue(SampleRepository.getInstance().isEmpty());

Sample.of(testServiceSampler.echoParameter(any(TestBeanWithBytes.class)));

Sample.of(testServiceSampler.getRandomByteArray(anyInt()));

source.load();

assertFalse(SampleRepository.getInstance().isEmpty());
assertArrayEquals(SOME_BYTES , getTestService().echoParameter(new TestBeanWithBytes(SOME_BYTES)).getSomeBytes());
byte[] valueStubbedMethod = getTestService().getRandomByteArray(42);
assertArrayEquals(expectedArray ,valueStubbedMethod);
Files.delete(Paths.get(pathToFile));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,12 @@ public Map<Integer, Integer> getMapOfIntegers() {
}


public byte[] getRandomByteArray(int size){

byte[] b = new byte[size];
new Random().nextBytes(b);
return b;

}

}

0 comments on commit 952d80c

Please sign in to comment.