Skip to content

Commit

Permalink
Merge pull request #8 from danieladams456/objectmapper
Browse files Browse the repository at this point in the history
Objectmapper
  • Loading branch information
danieladams456 authored Jan 3, 2025
2 parents 4e07e7b + c95d4b0 commit 4889cac
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 8 deletions.
19 changes: 15 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = 'com.github.danieladams456'
version = '0.0.1-SNAPSHOT'
version = '0.0.2'

repositories {
mavenCentral()
Expand All @@ -14,7 +14,18 @@ test {
}

dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation group: 'org.assertj', name: 'assertj-core', version: '3.27.1'
// test platform
testImplementation libs.assertj.core
testImplementation libs.junit.jupiter
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// testing integrations, but don't force consumers to use
compileOnly libs.jackson.databind
testImplementation libs.jackson.databind

// lombok
compileOnly libs.lombok
annotationProcessor libs.lombok
testCompileOnly libs.lombok
testAnnotationProcessor libs.lombok
}
11 changes: 11 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[versions]
assertj = '3.27.1'
jackson = '2.18.2'
junit = '5.10.0'
lombok = '1.18.36'

[libraries]
assertj-core = { group = 'org.assertj', name = 'assertj-core', version.ref = 'assertj' }
jackson-databind = { group = 'com.fasterxml.jackson.core', name = 'jackson-databind', version.ref = 'jackson' }
junit-jupiter = { group = 'org.junit.jupiter', name = 'junit-jupiter', version.ref = 'junit' }
lombok = { group = 'org.projectlombok', name = 'lombok', version.ref = 'lombok' }
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.github.danieladams456.statusvoter;

import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

@Getter
public class StatusVoter {
private StatusClassification classification;

public StatusVoter() {
classification = StatusClassification.INITIAL;
}

public StatusClassification getClassification() {
return classification;
}

/**
* Updates the current status classification if the incoming classification
* has a higher score than the existing classification.
Expand Down Expand Up @@ -41,6 +41,12 @@ public void update(String newClassification) {
}
}

/**
* toString() is also used for JSON serialization
*
* @return the string representation of the current StatusClassification
*/
@JsonValue
@Override
public String toString() {
return classification.name();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.danieladams456.statusvoter;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

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

public class IntegrationTest {
@Test
void testSerializeObject() throws IOException {
TestObject data = new TestObject();
data.setA("test1");
data.setB("test2");
ObjectMapper mapper = new ObjectMapper();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
mapper.writeValue(outputStream, data);
assertThat(outputStream.toString()).isEqualTo("""
{"a":"test1","b":"test2","voter":"INITIAL"}""");
}

@Test
void testSerializeRecord() throws IOException {
TestRecord record = new TestRecord("test1", "test2", new StatusVoter());
ObjectMapper mapper = new ObjectMapper();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
mapper.writeValue(outputStream, record);
assertThat(outputStream.toString()).isEqualTo("""
{"a":"test1","b":"test2","voter":"INITIAL"}""");
}

@Data
private static class TestObject {
private String a;
private String b;
private StatusVoter voter = new StatusVoter();
}

private record TestRecord(
String a,
String b,
StatusVoter voter
) {
}
}

0 comments on commit 4889cac

Please sign in to comment.