-
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.
* fix JsonCreator interaction with Property * Remove duplicate f.fieldName().equals(name) * Add test and add Adapter for Duration --------- Co-authored-by: Rob Bygrave <[email protected]>
- Loading branch information
Showing
6 changed files
with
183 additions
and
3 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
blackbox-test/src/main/java/org/example/customer/creator/PropertyCreator.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,43 @@ | ||
package org.example.customer.creator; | ||
|
||
import io.avaje.jsonb.Json; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.UUID; | ||
|
||
@Json | ||
public final class PropertyCreator { | ||
|
||
@Json.Property("uid") | ||
private final String identifier; | ||
|
||
@Json.Property("stp") | ||
private final Instant startup; | ||
|
||
@Json.Creator | ||
public PropertyCreator( | ||
String identifier, | ||
Instant startup) { | ||
this.identifier = identifier; | ||
this.startup = startup; | ||
} | ||
|
||
public PropertyCreator(Instant startup) { | ||
this.identifier = Instant.now().toEpochMilli() + "-" + UUID.randomUUID(); | ||
this.startup = startup; | ||
} | ||
|
||
public String identifier() { | ||
return this.identifier; | ||
} | ||
|
||
public Instant startup() { | ||
return this.startup; | ||
} | ||
|
||
@Json.Property("upt") | ||
public Duration uptime() { | ||
return Duration.ofMillis(1); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
blackbox-test/src/test/java/org/example/customer/creator/PropertyCreatorTest.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,27 @@ | ||
package org.example.customer.creator; | ||
|
||
import io.avaje.jsonb.Jsonb; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Instant; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class PropertyCreatorTest { | ||
|
||
final Jsonb jsonb = Jsonb.builder().build(); | ||
|
||
@Test | ||
void asJson() { | ||
var instant = Instant.ofEpochMilli(1); | ||
var pc = new PropertyCreator("strVal", instant); | ||
|
||
String json = jsonb.toJson(pc); | ||
PropertyCreator fromJson = jsonb.type(PropertyCreator.class).fromJson(json); | ||
|
||
assertThat(fromJson.identifier()).isEqualTo("strVal"); | ||
assertThat(fromJson.identifier()).isEqualTo(pc.identifier()); | ||
assertThat(fromJson.startup()).isEqualTo(pc.startup()); | ||
assertThat(fromJson.uptime()).isEqualTo(pc.uptime()); | ||
} | ||
} |
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
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
90 changes: 90 additions & 0 deletions
90
jsonb-generator/src/test/java/io/avaje/jsonb/generator/models/valid/PropertyCreator.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,90 @@ | ||
package io.avaje.jsonb.generator.models.valid; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.time.temporal.Temporal; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.function.Consumer; | ||
|
||
import io.avaje.jsonb.Json; | ||
|
||
@Json | ||
public final class PropertyCreator { | ||
|
||
private final transient String[] arguments; | ||
|
||
private final transient List<Consumer<?>> hooks; | ||
|
||
@Json.Property("uid") | ||
private final String identifier; | ||
|
||
@Json.Property("stp") | ||
private final Instant startup; | ||
|
||
@Json.Property("cfg") | ||
private final Properties configuration; | ||
|
||
@Json.Property("ins") | ||
private final Set<PropertyCreator> instances; | ||
|
||
@Json.Creator | ||
public PropertyCreator( | ||
String identifier, | ||
Instant startup, | ||
Properties configuration, | ||
Set<PropertyCreator> instances) { | ||
this.arguments = null; | ||
this.hooks = null; | ||
|
||
this.identifier = identifier; | ||
this.startup = startup; | ||
this.configuration = configuration; | ||
this.instances = null; | ||
} | ||
|
||
public PropertyCreator(String[] arguments, Instant startup) throws IOException { | ||
this.arguments = arguments != null ? arguments : new String[0]; | ||
this.hooks = new ArrayList<>(); | ||
|
||
this.identifier = Instant.now().toEpochMilli() + "-" + UUID.randomUUID(); | ||
this.startup = startup; | ||
|
||
this.configuration = new Properties(); | ||
this.configuration.putAll(System.getenv()); | ||
this.configuration.putAll(System.getProperties()); | ||
|
||
this.instances = new HashSet<>(); | ||
} | ||
|
||
public String[] arguments() { | ||
return arguments; | ||
} | ||
|
||
public String identifier() { | ||
return this.identifier; | ||
} | ||
|
||
public Instant startup() { | ||
return this.startup; | ||
} | ||
|
||
public Properties configuration() { | ||
return this.configuration; | ||
} | ||
|
||
public Set<PropertyCreator> instances() { | ||
return this.instances; | ||
} | ||
|
||
@Json.Property("upt") | ||
public Duration uptime() { | ||
return Duration.ofMillis(1); | ||
} | ||
} |
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