Skip to content

Commit

Permalink
Fixed PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bullet-tooth committed Dec 11, 2018
1 parent d6a9cd1 commit 28c1e72
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 25 deletions.
1 change: 0 additions & 1 deletion LICENSES-THIRD-PARTY.TXT
Expand Up @@ -12,7 +12,6 @@ The following third party software components are governed under Apache 2.0 lice
- Vert.x Web, (c) 2018 https://vertx.io/
- Apache Log4j, (c) 1999-2018 The Apache Software Foundation https://logging.apache.org/log4j/2.x/
- Lazysodium, (c) 2018 Terl https://github.com/terl/lazysodium-java
- JsonPathAssert, (c) 2018 json-path https://github.com/json-path/JsonPath/tree/master/json-path-assert

The following third party software components are governed under both Apache 2.0 and MIT licenses:
- jni, (c) 2018 Josh Chase https://github.com/prevoty/jni-rs
Expand Down
Expand Up @@ -24,10 +24,14 @@
import com.google.gson.LongSerializationPolicy;

/**
* Provides {@link Gson} serializer for converting java objects to Json and vice versa.
* Provides {@link Gson} serializer for converting Java objects to Json and vice versa.
* It is configured to serialize Exonum objects in a format, compatible with the core framework
* and light clients (e.g., {@link HashCode} as a hex string).
*/
public final class JsonSerializer {

private static final Gson INSTANCE = builder().create();

/**
* Returns preconfigured {@link Gson} builder instance. Can be useful in cases when
* some customization is required. For example, type adapters should be extended or replaced.
Expand All @@ -45,7 +49,7 @@ public static GsonBuilder builder() {
* of the Json serializer is required.
*/
public static Gson json() {
return builder().create();
return INSTANCE;
}

private JsonSerializer() {
Expand Down
Expand Up @@ -36,16 +36,16 @@ void longSerializesAsText() {

String json = json().toJson(new Wrapper<>(value));

assertJson(json, String.valueOf(value));
assertJsonValue(json, "10");
}

@Test
void publicKeySerializesAsValue() {
void publicKeySerializesAsHexValue() {
PublicKey value = PublicKey.fromBytes(bytes(0x00, 0x01, 0x02));

String json = json().toJson(new Wrapper<>(value));

assertJson(json, value.toString());
assertJsonValue(json, "000102");
}

@Test
Expand All @@ -54,10 +54,10 @@ void hashCodeSerializesAsValue() {

String json = json().toJson(new Wrapper<>(value));

assertJson(json, value.toString());
assertJsonValue(json, "000102");
}

private static void assertJson(String json, Object expectedValue) {
private static void assertJsonValue(String json, Object expectedValue) {
assertThat(json, isJson(withJsonPath("$.value", equalTo(expectedValue))));
}

Expand Down
Expand Up @@ -16,12 +16,12 @@

package com.exonum.binding.common.serialization.json;

import static com.exonum.binding.common.serialization.json.JsonSerializer.json;
import static java.util.Collections.singletonList;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.exonum.binding.common.configuration.ConsensusConfiguration;
import com.exonum.binding.common.configuration.StoredConfiguration;
Expand Down Expand Up @@ -65,28 +65,16 @@ class StoredConfigurationGsonSerializerTest {
@Test
void roundTripTest() {
StoredConfiguration configuration = createConfiguration();
StoredConfiguration restoredConfiguration = StoredConfigurationGsonSerializer
.fromJson(StoredConfigurationGsonSerializer.toJson(configuration));
StoredConfiguration restoredConfiguration = json().fromJson(
json().toJson(configuration), StoredConfiguration.class);

assertThat(restoredConfiguration, equalTo(configuration));
}

@Test
void nullPointerException() {
StoredConfiguration configuration = null;
String jsonConfiguration = null;

assertThrows(NullPointerException.class,
() -> StoredConfigurationGsonSerializer.toJson(configuration));

assertThrows(NullPointerException.class,
() -> StoredConfigurationGsonSerializer.fromJson(jsonConfiguration));
}

@Test
void readConfiguration() {
StoredConfiguration configuration = StoredConfigurationGsonSerializer
.fromJson(CONFIG_EXAMPLE);
StoredConfiguration configuration = json()
.fromJson(CONFIG_EXAMPLE, StoredConfiguration.class);

assertThat(configuration, notNullValue());
assertThat(configuration.previousCfgHash(),
Expand Down

0 comments on commit 28c1e72

Please sign in to comment.