Skip to content

Commit

Permalink
fix: allow deserializing tag with value and multiple attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-grgt committed Mar 4, 2024
1 parent 2b57b70 commit d497011
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 28 deletions.
6 changes: 4 additions & 2 deletions xjx-serdes/src/main/java/io/jonasg/xjx/serdes/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ public static Path parse(String path) {
}

public Path appendAttribute(String attribute) {
this.attribute = attribute;
return this;
var copiedSections = new LinkedList<>(this.sections);
var path = new Path(copiedSections);
path.attribute = attribute;
return path;
}

public Path append(String section) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package io.jonasg.xjx.serdes.deserialize;

import static org.assertj.core.api.Assertions.assertThat;

import java.math.BigDecimal;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -25,25 +26,43 @@ void deserialize_StringField() {
DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class);

// then
Assertions.assertThat(dataTypes.String).isEqualTo("11");
assertThat(dataTypes.String).isEqualTo("11");
}

@Test
void deserialize_IntegerField() {
// given
String data = """
@Test
void deserialize_IntegerField() {
// given
String data = """
<?xml version="1.0" encoding="UTF-8"?>
<DataTypes>
<Integer value="11"/>
</DataTypes>
""";

// when
DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class);
// when
DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class);

// then
Assertions.assertThat(dataTypes.Integer).isEqualTo(11);
}
// then
assertThat(dataTypes.Integer).isEqualTo(11);
}


@Test
void deserialize_primitiveIntField() {
// given
String data = """
<?xml version="1.0" encoding="UTF-8"?>
<DataTypes>
<primitiveInt value="11"/>
</DataTypes>
""";

// when
DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class);

// then
assertThat(dataTypes.primitiveInt).isEqualTo(11);
}

@Test
void deserialize_LongField() {
Expand All @@ -59,7 +78,7 @@ void deserialize_LongField() {
DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class);

// then
Assertions.assertThat(dataTypes.Long).isEqualTo(11L);
assertThat(dataTypes.Long).isEqualTo(11L);
}

@Test
Expand All @@ -76,7 +95,7 @@ void deserialize_primitiveLongField() {
DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class);

// then
Assertions.assertThat(dataTypes.primitiveLong).isEqualTo(11L);
assertThat(dataTypes.primitiveLong).isEqualTo(11L);
}

@Test
Expand All @@ -93,7 +112,7 @@ void deserialize_BigDecimalField() {
DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class);

// then
Assertions.assertThat(dataTypes.BigDecimal).isEqualTo(BigDecimal.valueOf(11));
assertThat(dataTypes.BigDecimal).isEqualTo(BigDecimal.valueOf(11));
}

@Test
Expand All @@ -110,7 +129,7 @@ void deserialize_DoubleField() {
DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class);

// then
Assertions.assertThat(dataTypes.Double).isEqualTo(Double.valueOf(11));
assertThat(dataTypes.Double).isEqualTo(Double.valueOf(11));
}

@Test
Expand All @@ -127,7 +146,7 @@ void deserialize_primitiveDoubleField() {
DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class);

// then
Assertions.assertThat(dataTypes.primitiveDouble).isEqualTo(Double.valueOf(11));
assertThat(dataTypes.primitiveDouble).isEqualTo(Double.valueOf(11));
}

@Test
Expand All @@ -144,7 +163,7 @@ void deserialize_multiCharString_toPrimitiveCharField() {
DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class);

// then
Assertions.assertThat(dataTypes.multipleCharacters).isEqualTo('A');
assertThat(dataTypes.multipleCharacters).isEqualTo('A');
}

@Test
Expand All @@ -161,7 +180,7 @@ void deserialize_primitiveCharField() {
DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class);

// then
Assertions.assertThat(dataTypes.primitiveChar).isEqualTo('A');
assertThat(dataTypes.primitiveChar).isEqualTo('A');
}

@Test
Expand All @@ -178,7 +197,7 @@ void deserialize_CharacterField() {
DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class);

// then
Assertions.assertThat(dataTypes.Character).isEqualTo(Character.valueOf('A'));
assertThat(dataTypes.Character).isEqualTo(Character.valueOf('A'));
}

@ParameterizedTest
Expand All @@ -195,8 +214,8 @@ void deserializeTrueValuesFor_booleanField(String value) {
DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class);

// then
Assertions.assertThat(dataTypes.BooleanTrue).isTrue();
Assertions.assertThat(dataTypes.booleanTrue).isTrue();
assertThat(dataTypes.BooleanTrue).isTrue();
assertThat(dataTypes.booleanTrue).isTrue();
}

@ParameterizedTest
Expand All @@ -213,8 +232,8 @@ void deserializeFalseValuesFor_booleanField(String value) {
DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class);

// then
Assertions.assertThat(dataTypes.BooleanFalse).isFalse();
Assertions.assertThat(dataTypes.booleanFalse).isFalse();
assertThat(dataTypes.BooleanFalse).isFalse();
assertThat(dataTypes.booleanFalse).isFalse();
}

@Test
Expand All @@ -231,18 +250,52 @@ void deserialize_StringField_mappedUsingRelativePath() {
ParentHolder parentHolder = new XjxSerdes().read(data, ParentHolder.class);

// then
Assertions.assertThat(parentHolder.nestedField.String).isEqualTo("11");
assertThat(parentHolder.nestedField.String).isEqualTo("11");
}

@Test
void deserialize_mapValueAndAtTheSameTimeAttributeValuesOfOneTag() {
// given
String data = """
<?xml version="1.0" encoding="UTF-8"?>
<Person>
<Name age="18" sex="MALE">John</Name>
</Person>
""";

// when
var person = new XjxSerdes().read(data, Person.class);

// then
assertThat(person.name).isEqualTo("John");
assertThat(person.sex).isEqualTo("MALE");
assertThat(person.age).isEqualTo(18);
}

public static class Person {
@Tag(path = "/Person/Name")
String name;

@Tag(path = "/Person/Name", attribute = "sex")
String sex;

@Tag(path = "/Person/Name", attribute = "age")
int age;
}


public static class DataTypeHolder {
public DataTypeHolder() {
}

@Tag(path = "/DataTypes/String", attribute = "value")
String String;

@Tag(path = "/DataTypes/Integer", attribute = "value")
Integer Integer;
@Tag(path = "/DataTypes/Integer", attribute = "value")
Integer Integer;

@Tag(path = "/DataTypes/primitiveInt", attribute = "value")
int primitiveInt;

@Tag(path = "/DataTypes/Long", attribute = "value")
Long Long;
Expand Down

0 comments on commit d497011

Please sign in to comment.