Skip to content

Commit

Permalink
feat(xjx-serdes): add enum serialization support
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-grgt committed Jan 17, 2024
1 parent 2fbf677 commit 5651b37
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ jobs:
- name: Commit bumped version
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "Bump version to ${{ github.event.inputs.tag }}"
commit_message: "Prepare release version ${{ github.event.inputs.tag }}"
publish:
name: "Publish"
needs: [bump_version]
runs-on: ubuntu-latest
steps:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package io.jonasg.xjx.serdes.serialize;

import io.jonasg.xjx.serdes.Tag;
import io.jonasg.xjx.serdes.XjxSerdes;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

public class EnumSerializationTest {


@Test
void mapToFieldOfTypeEnum_whenCharacterDataMatchesEnumConstantName() {
// given
var enumHolder = new EnumHolder(Unit.FAHRENHEIT, Unit.PERCENTAGE);

// when
String xml = new XjxSerdes().write(enumHolder);

// then
Assertions.assertThat(xml).isEqualTo("""
<WeatherData>
<Day>
<High>
<Unit>FAHRENHEIT</Unit>
</High>
<Precipitation>
<Unit>PERCENTAGE</Unit>
</Precipitation>
</Day>
</WeatherData>
""");
}

@Nested
class NullEnumFieldsShouldBeSerializedAsEmptyTag {

@Test
void withOnlyValue() {
// given
var enumHolder = new EnumHolder(null, Unit.PERCENTAGE);

// when
String xml = new XjxSerdes().write(enumHolder);

// then
Assertions.assertThat(xml).isEqualTo("""
<WeatherData>
<Day>
<High>
<Unit/>
</High>
<Precipitation>
<Unit>PERCENTAGE</Unit>
</Precipitation>
</Day>
</WeatherData>
""");
}

@Test
void withValueAndAttribute() {
// given
var enumHolder = new EnumHolderWithAttr(null, "C", Unit.PERCENTAGE);

// when
String xml = new XjxSerdes().write(enumHolder);

// then
Assertions.assertThat(xml).isEqualTo("""
<WeatherData>
<Day>
<High>
<Unit abbr="C"/>
</High>
<Precipitation>
<Unit>PERCENTAGE</Unit>
</Precipitation>
</Day>
</WeatherData>
""");
}
}

static class EnumHolder {

public EnumHolder(Unit dayHighUnit, Unit precipitationUnit) {
this.dayHighUnit = dayHighUnit;
this.precipitationUnit = precipitationUnit;
}

@Tag(path = "/WeatherData/Day/High/Unit")
Unit dayHighUnit;

@Tag(path = "/WeatherData/Day/Precipitation/Unit")
Unit precipitationUnit;
}

static class EnumHolderWithAttr {

public EnumHolderWithAttr(Unit dayHighUnit, String abbreviation, Unit precipitationUnit) {
this.dayHighUnit = dayHighUnit;
this.abbreviation = abbreviation;
this.precipitationUnit = precipitationUnit;
}

@Tag(path = "/WeatherData/Day/High/Unit")
Unit dayHighUnit;

@Tag(path = "/WeatherData/Day/High/Unit", attribute = "abbr")
String abbreviation;

@Tag(path = "/WeatherData/Day/Precipitation/Unit")
Unit precipitationUnit;
}

enum Unit {
FAHRENHEIT, PERCENTAGE
}
}

0 comments on commit 5651b37

Please sign in to comment.