Skip to content

Commit

Permalink
Make HumanReadableByteCount serialize to a JSON string instead of a J…
Browse files Browse the repository at this point in the history
…SON object. (#105)
  • Loading branch information
GrahamDennis authored and carterkozak committed Nov 25, 2019
1 parent 35533b4 commit 011fb38
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.palantir.humanreadabletypes;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Locale;
Expand Down Expand Up @@ -247,6 +248,7 @@ public int hashCode() {
* @return a human-readable string representation of this byte string, not null
*/
@Override
@JsonValue
public String toString() {
String units = unit.toString().toLowerCase(Locale.ENGLISH);
if (size == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,30 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.TextNode;
import java.util.Arrays;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.junit.Test;

public final class HumanReadableByteCountTests {
private static final ObjectMapper objectMapper = new ObjectMapper();

@Test
public void testParseByte() {
assetStringsEqualToBytes(10L, "10", "10b", "10 byte", "10 bytes");
assertStringsEqualToBytes(10L, "10", "10b", "10 byte", "10 bytes");
}

@Test
public void testParseKibiBytes() {
assetStringsEqualToBytes(1024L * 10L, "10k", "10kb", "10 kibibyte", "10 kibibytes");
assertStringsEqualToBytes(1024L * 10L, "10k", "10kb", "10 kibibyte", "10 kibibytes");
}

@Test
public void testParseMibiBytes() {
assetStringsEqualToBytes((long) Math.pow(1024L, 2L) * 10L, "10m", "10mb", "10 mibibyte", "10 mibibytes");
assertStringsEqualToBytes((long) Math.pow(1024L, 2L) * 10L, "10m", "10mb", "10 mibibyte", "10 mibibytes");
}

@Test
Expand All @@ -48,17 +52,17 @@ public void testParseMebiBytes() {

@Test
public void testParseGibiBytes() {
assetStringsEqualToBytes((long) Math.pow(1024L, 3L) * 10L, "10g", "10gb", "10 gibibyte", "10 gibibytes");
assertStringsEqualToBytes((long) Math.pow(1024L, 3L) * 10L, "10g", "10gb", "10 gibibyte", "10 gibibytes");
}

@Test
public void testParseTebiBytes() {
assetStringsEqualToBytes((long) Math.pow(1024L, 4L) * 10L, "10t", "10tb", "10 tebibyte", "10 tebibytes");
assertStringsEqualToBytes((long) Math.pow(1024L, 4L) * 10L, "10t", "10tb", "10 tebibyte", "10 tebibytes");
}

@Test
public void testParsePebiBytes() {
assetStringsEqualToBytes((long) Math.pow(1024L, 5L) * 10L, "10p", "10pb", "10 pebibyte", "10 pebibytes");
assertStringsEqualToBytes((long) Math.pow(1024L, 5L) * 10L, "10p", "10pb", "10 pebibyte", "10 pebibytes");
}

@Test
Expand Down Expand Up @@ -99,11 +103,31 @@ public void testToString() {
assertThat(HumanReadableByteCount.valueOf("2 bytes").toString()).isEqualTo("2 bytes");
}

private static void assetStringsEqualToBytes(long expectedBytes, String... byteCounts) {
@Test
public void testToJsonString() throws Exception {
assertThat(toJsonString(HumanReadableByteCount.valueOf("1 byte"))).isEqualTo("\"1 byte\"");
assertThat(toJsonString(HumanReadableByteCount.valueOf("1 bytes"))).isEqualTo("\"1 byte\"");
assertThat(toJsonString(HumanReadableByteCount.valueOf("2 byte"))).isEqualTo("\"2 bytes\"");
assertThat(toJsonString(HumanReadableByteCount.valueOf("2 bytes"))).isEqualTo("\"2 bytes\"");
}

private static void assertStringsEqualToBytes(long expectedBytes, String... byteCounts) {
assertThat(Arrays.stream(byteCounts)
.map(HumanReadableByteCount::valueOf)
.map(HumanReadableByteCountTests::parseFromString)
.map(HumanReadableByteCount::toBytes)
.collect(Collectors.toList())
).allMatch(Predicate.isEqual(expectedBytes));
}

private static HumanReadableByteCount parseFromString(String durationString) {
try {
return objectMapper.treeToValue(TextNode.valueOf(durationString), HumanReadableByteCount.class);
} catch (JsonProcessingException e) {
throw new RuntimeException("Failed to parse duration from string", e);
}
}

private String toJsonString(HumanReadableByteCount humanReadableByteCount) throws JsonProcessingException {
return objectMapper.writeValueAsString(humanReadableByteCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,50 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.TextNode;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.junit.Test;

public final class HumanReadableDurationTests {
private static final ObjectMapper objectMapper = new ObjectMapper();

@Test
public void testParseNanoseconds() {
assetStringsEqualToDuration(10, TimeUnit.NANOSECONDS, "10ns", "10 nanosecond", "10 nanoseconds");
assertStringsEqualToDuration(10, TimeUnit.NANOSECONDS, "10ns", "10 nanosecond", "10 nanoseconds");
}

@Test
public void testParseMicroseconds() {
assetStringsEqualToDuration(12, TimeUnit.MICROSECONDS, "12us", "12 microsecond", "12 microseconds");
assertStringsEqualToDuration(12, TimeUnit.MICROSECONDS, "12us", "12 microsecond", "12 microseconds");
}

@Test
public void testParseMilliseconds() {
assetStringsEqualToDuration(1, TimeUnit.MILLISECONDS, "1ms", "1 millisecond", "1 milliseconds");
assertStringsEqualToDuration(1, TimeUnit.MILLISECONDS, "1ms", "1 millisecond", "1 milliseconds");
}

@Test
public void testParseSeconds() {
assetStringsEqualToDuration(15, TimeUnit.SECONDS, "15s", "15 second", "15 seconds");
assertStringsEqualToDuration(15, TimeUnit.SECONDS, "15s", "15 second", "15 seconds");
}

@Test
public void testParseMinutes() {
assetStringsEqualToDuration(8, TimeUnit.MINUTES, "8m", "8 minute", "8 minutes");
assertStringsEqualToDuration(8, TimeUnit.MINUTES, "8m", "8 minute", "8 minutes");
}

@Test
public void testParseHours() {
assetStringsEqualToDuration(7, TimeUnit.HOURS, "7h", "7 hour", "7 hours");
assertStringsEqualToDuration(7, TimeUnit.HOURS, "7h", "7 hour", "7 hours");
}

@Test
public void testParseDays() {
assetStringsEqualToDuration(14, TimeUnit.DAYS, "14d", "14 day", "14 days");
assertStringsEqualToDuration(14, TimeUnit.DAYS, "14d", "14 day", "14 days");
}

@Test
Expand Down Expand Up @@ -100,11 +104,31 @@ public void testToString() {
assertThat(HumanReadableDuration.valueOf("2 seconds").toString()).isEqualTo("2 seconds");
}

private static void assetStringsEqualToDuration(long expectedQuantity, TimeUnit expectedTimeUnit,
@Test
public void testToJson() throws Exception {
assertThat(toJsonString(HumanReadableDuration.valueOf("1 second"))).isEqualTo("\"1 second\"");
assertThat(toJsonString(HumanReadableDuration.valueOf("1 seconds"))).isEqualTo("\"1 second\"");
assertThat(toJsonString(HumanReadableDuration.valueOf("2 second"))).isEqualTo("\"2 seconds\"");
assertThat(toJsonString(HumanReadableDuration.valueOf("2 seconds"))).isEqualTo("\"2 seconds\"");
}

private static void assertStringsEqualToDuration(long expectedQuantity, TimeUnit expectedTimeUnit,
String... durationStrings) {
assertThat(Arrays.stream(durationStrings)
.map(HumanReadableDuration::valueOf)
.map(HumanReadableDurationTests::parseFromString)
.collect(Collectors.toList())
).allMatch(duration -> duration.getQuantity() == expectedQuantity && duration.getUnit() == expectedTimeUnit);
}

private static HumanReadableDuration parseFromString(String durationString) {
try {
return objectMapper.treeToValue(TextNode.valueOf(durationString), HumanReadableDuration.class);
} catch (JsonProcessingException e) {
throw new RuntimeException("Failed to parse duration from string", e);
}
}

private String toJsonString(HumanReadableDuration humanReadableDuration) throws JsonProcessingException {
return objectMapper.writeValueAsString(humanReadableDuration);
}
}

0 comments on commit 011fb38

Please sign in to comment.