Skip to content

Commit

Permalink
#189 - Add blackbox tests for Json.Creator
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Nov 30, 2023
1 parent 14c60f7 commit d2ed924
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 2 deletions.
4 changes: 2 additions & 2 deletions blackbox-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb</artifactId>
<version>1.10-RC1</version>
<version>1.10-RC2</version>
</dependency>

<!-- <dependency>-->
Expand All @@ -47,7 +47,7 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb-generator</artifactId>
<version>1.10-RC1</version>
<version>1.10-RC2</version>
<scope>provided</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.example.customer.creator;

public class Kingfisher {
private final String name;
private int fishCaught;

public Kingfisher(String name) {
this.name = name;
}

public String getName() {
return name;
}

public int getFishCaught() {
return fishCaught;
}

public void setFishCaught(int fishCaught) {
this.fishCaught = fishCaught;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.example.customer.creator;

import io.avaje.jsonb.Json;
import io.avaje.jsonb.Json.MixIn;

@MixIn(Kingfisher.class)
public interface KingfisherMixin {

@Json.Creator
static Kingfisher construct(String name) {
var kf = new Kingfisher(name);
kf.setFishCaught(42);
return kf;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.example.customer.creator;

import io.avaje.jsonb.Json;

@Json
public class StudentViaConstructor {
private final String name;
private int rollNo;

@Json.Creator
public StudentViaConstructor(@Json.Alias("theName") String name, long rolling) {
this.name = name;
this.rollNo = name.length();
}

public StudentViaConstructor(String name, int rollNo) {
this.name = name;
this.rollNo = rollNo;
}

public String getName() {
return name;
}

public int getRollNo() {
return rollNo;
}

public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.example.customer.creator;

import io.avaje.jsonb.Json;

@Json
public class StudentViaStaticMethod {
private final String name;
private int rollNo;

@Json.Creator
public static StudentViaStaticMethod create(@Json.Alias("theName") String name, long rolling) {
return new StudentViaStaticMethod(name, name.length());
}

public StudentViaStaticMethod(String name, int rollNo) {
this.name = name;
this.rollNo = rollNo;
}

public String getName() {
return name;
}

public int getRollNo() {
return rollNo;
}

public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.example.customer.creator;

import io.avaje.jsonb.Jsonb;
import org.junit.jupiter.api.Test;

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

class KingFisherTest {

Jsonb jsonb = Jsonb.builder().build();

@Test
void asJson() {
Kingfisher kf = new Kingfisher("hi");
kf.setFishCaught(90);

String asJson = jsonb.toJson(kf);
assertThat(asJson).isEqualTo("{\"name\":\"hi\",\"fishCaught\":90}");

Kingfisher fromJson = jsonb.type(Kingfisher.class).fromJson(asJson);
assertThat(fromJson.getName()).isEqualTo("hi");
assertThat(fromJson.getFishCaught()).isEqualTo(42);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.example.customer.creator;

import io.avaje.jsonb.Jsonb;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

class StudentViaConstructorTest {

Jsonb jsonb = Jsonb.builder().build();

@Test
void asJson() {
StudentViaConstructor stu = new StudentViaConstructor("one", 1);

String asJson = jsonb.toJson(stu);
assertThat(asJson).isEqualTo("{\"name\":\"one\",\"rollNo\":1}");

StudentViaConstructor fromJson = jsonb.type(StudentViaConstructor.class).fromJson(asJson);
assertThat(fromJson.getName()).isEqualTo("one");
assertThat(fromJson.getRollNo()).isEqualTo(3); // the length of name
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.example.customer.creator;

import io.avaje.jsonb.Jsonb;
import org.junit.jupiter.api.Test;

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

class StudentViaStaticMethodTest {

Jsonb jsonb = Jsonb.builder().build();

@Test
void asJson() {
StudentViaStaticMethod stu = new StudentViaStaticMethod("one", 1);

String asJson = jsonb.toJson(stu);
assertThat(asJson).isEqualTo("{\"name\":\"one\",\"rollNo\":1}");

StudentViaStaticMethod fromJson = jsonb.type(StudentViaStaticMethod.class).fromJson(asJson);
assertThat(fromJson.getName()).isEqualTo("one");
assertThat(fromJson.getRollNo()).isEqualTo(3); // the length of name
}
}

0 comments on commit d2ed924

Please sign in to comment.