RED-4824: Added annotations to correctly handle json deserialization
This commit is contained in:
parent
4c06ab958c
commit
b6a95244d8
@ -2,6 +2,9 @@ package com.iqser.red.service.redaction.v1.server.parsing.model;
|
|||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonValue;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ -12,6 +15,8 @@ public enum TextDirection {
|
|||||||
THREE_QUARTER_CIRCLE(270f);
|
THREE_QUARTER_CIRCLE(270f);
|
||||||
|
|
||||||
public static final String VALUE_STRING_SUFFIX = "°";
|
public static final String VALUE_STRING_SUFFIX = "°";
|
||||||
|
|
||||||
|
@JsonValue
|
||||||
private final float degrees;
|
private final float degrees;
|
||||||
private final float radians;
|
private final float radians;
|
||||||
|
|
||||||
@ -25,6 +30,12 @@ public enum TextDirection {
|
|||||||
return degrees + VALUE_STRING_SUFFIX;
|
return degrees + VALUE_STRING_SUFFIX;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@com.dslplatform.json.JsonValue
|
||||||
|
public float jsonValue() {
|
||||||
|
return getDegrees();
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonCreator
|
||||||
public static TextDirection fromDegrees(float degrees) {
|
public static TextDirection fromDegrees(float degrees) {
|
||||||
|
|
||||||
for (var dir : TextDirection.values()) {
|
for (var dir : TextDirection.values()) {
|
||||||
|
|||||||
@ -1,53 +1,65 @@
|
|||||||
package com.iqser.red.service.redaction.v1.server.parsing.model;
|
package com.iqser.red.service.redaction.v1.server.parsing.model;
|
||||||
|
|
||||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.InputStream;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import org.apache.fontbox.util.BoundingBox;
|
|
||||||
import org.apache.pdfbox.cos.COSDictionary;
|
|
||||||
import org.apache.pdfbox.pdmodel.font.PDFont;
|
|
||||||
import org.apache.pdfbox.text.TextPosition;
|
|
||||||
import org.apache.pdfbox.util.Matrix;
|
import org.apache.pdfbox.util.Matrix;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.iqser.red.storage.commons.properties.StorageProperties;
|
||||||
|
import com.iqser.red.storage.commons.service.ObjectSerializer;
|
||||||
|
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
|
|
||||||
public class TextPositionSequenceTest {
|
public class TextPositionSequenceTest {
|
||||||
|
|
||||||
public static final float UNROTAED_RECTANGLE_X_OFFSET = 0.f;
|
private static final String TEXT_POSITION_SEQUENCE_AS_JSON = "{\n" //
|
||||||
public static final float UNROTATED_RECTANGLE_Y_OFFSET = -2.f;
|
+ " \"page\": 1,\n" //
|
||||||
public static final float UNROTATED_RECTANGLE_WIDTH_OFFSET = 1.f;
|
+ " \"textPositions\": [],\n" //
|
||||||
public static final float UNROTATED_RECTANGLE_HEIGHT_OFFSET = 6.f;
|
+ " \"dir\": 180.0,\n" //
|
||||||
|
+ " \"rotation\": 0,\n" //
|
||||||
|
+ " \"pageHeight\": 800,\n" //
|
||||||
|
+ " \"pageWidth\": 600\n" //
|
||||||
|
+ "}";
|
||||||
|
|
||||||
|
private final ObjectSerializer objectSerializer = new ObjectSerializer(new ObjectMapper(), new StorageProperties());
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public void testSinglePosNoRotationRectangle() {
|
public void testDeserializationWithJackson() {
|
||||||
|
|
||||||
int page = 2;
|
TextPositionSequence textPositionSequence = objectSerializer.deserializeWithJackson(
|
||||||
var testSequence = new TextPositionSequence(page);
|
new ByteArrayInputStream(TEXT_POSITION_SEQUENCE_AS_JSON.getBytes(StandardCharsets.UTF_8)),
|
||||||
|
TextPositionSequence.class);
|
||||||
|
|
||||||
float endX = 4.f;
|
assertPropertiesAfterJsonDeserialization(textPositionSequence);
|
||||||
float endY = 5.f;
|
|
||||||
float maxHeight = 20.f;
|
|
||||||
var redTextPosition = createTextPosition(0, endX, endY, maxHeight);
|
|
||||||
|
|
||||||
testSequence.add(redTextPosition);
|
|
||||||
|
|
||||||
var rectangle = testSequence.getRectangle();
|
|
||||||
assertThat(rectangle.getPage()).isEqualTo(page);
|
|
||||||
assertThat(rectangle.getTopLeft().getX()).isEqualTo(UNROTAED_RECTANGLE_X_OFFSET);
|
|
||||||
assertThat(rectangle.getTopLeft().getY()).isEqualTo(UNROTATED_RECTANGLE_Y_OFFSET);
|
|
||||||
assertThat(rectangle.getWidth()).isEqualTo(endX + UNROTATED_RECTANGLE_WIDTH_OFFSET);
|
|
||||||
assertThat(rectangle.getHeight()).isEqualTo(maxHeight + UNROTATED_RECTANGLE_HEIGHT_OFFSET);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private TextPosition createTextPosition(int pageRotation, float endX, float endY, float maxHeight) throws IOException {
|
private void assertPropertiesAfterJsonDeserialization(TextPositionSequence textPositionSequence) {
|
||||||
|
|
||||||
return new TextPosition(pageRotation, 100, 200, createIdentityMatrix(), endX, endY, maxHeight, 4, 2, "a", null, new DummyFont(), 14, 42);
|
assertThat(textPositionSequence.getPage()).isEqualTo(1);
|
||||||
|
assertThat(textPositionSequence.getTextPositions()).hasSize(0);
|
||||||
|
assertThat(textPositionSequence.getDir()).isEqualTo(TextDirection.HALF_CIRCLE);
|
||||||
|
assertThat(textPositionSequence.getRotation()).isEqualTo(0);
|
||||||
|
assertThat(textPositionSequence.getPageHeight()).isEqualTo(800f);
|
||||||
|
assertThat(textPositionSequence.getPageWidth()).isEqualTo(600f);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SneakyThrows
|
||||||
|
public void testDeserializationWithDslJson() {
|
||||||
|
|
||||||
|
TextPositionSequence textPositionSequence = objectSerializer.deserializeWithDslJson(
|
||||||
|
new ByteArrayInputStream(TEXT_POSITION_SEQUENCE_AS_JSON.getBytes(StandardCharsets.UTF_8)),
|
||||||
|
TextPositionSequence.class);
|
||||||
|
|
||||||
|
assertPropertiesAfterJsonDeserialization(textPositionSequence);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -56,112 +68,4 @@ public class TextPositionSequenceTest {
|
|||||||
return new Matrix();
|
return new Matrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class DummyFont extends PDFont {
|
|
||||||
|
|
||||||
public DummyFont() throws IOException {
|
|
||||||
|
|
||||||
super(new COSDictionary());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected float getStandard14Width(int i) {
|
|
||||||
|
|
||||||
throw new RuntimeException("not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected byte[] encode(int i) {
|
|
||||||
|
|
||||||
throw new RuntimeException("not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int readCode(InputStream inputStream) {
|
|
||||||
|
|
||||||
throw new RuntimeException("not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isVertical() {
|
|
||||||
|
|
||||||
throw new RuntimeException("not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addToSubset(int i) {
|
|
||||||
|
|
||||||
throw new RuntimeException("not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void subset() {
|
|
||||||
|
|
||||||
throw new RuntimeException("not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean willBeSubset() {
|
|
||||||
|
|
||||||
throw new RuntimeException("not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
|
|
||||||
return "DummyFont";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BoundingBox getBoundingBox() {
|
|
||||||
|
|
||||||
throw new RuntimeException("not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public float getHeight(int i) {
|
|
||||||
|
|
||||||
throw new RuntimeException("not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasExplicitWidth(int i) {
|
|
||||||
|
|
||||||
throw new RuntimeException("not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public float getWidthFromFont(int i) {
|
|
||||||
|
|
||||||
throw new RuntimeException("not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isEmbedded() {
|
|
||||||
|
|
||||||
throw new RuntimeException("not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isDamaged() {
|
|
||||||
|
|
||||||
throw new RuntimeException("not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user