-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#189 - Add blackbox tests for Json.Creator
- Loading branch information
Showing
8 changed files
with
173 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
blackbox-test/src/main/java/org/example/customer/creator/Kingfisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
blackbox-test/src/main/java/org/example/customer/creator/KingfisherMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
blackbox-test/src/main/java/org/example/customer/creator/StudentViaConstructor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
blackbox-test/src/main/java/org/example/customer/creator/StudentViaStaticMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
blackbox-test/src/test/java/org/example/customer/creator/KingFisherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
blackbox-test/src/test/java/org/example/customer/creator/StudentViaConstructorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
blackbox-test/src/test/java/org/example/customer/creator/StudentViaStaticMethodTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |