Skip to content

Commit

Permalink
#12 - EmptyJson and MissingJson.
Browse files Browse the repository at this point in the history
  • Loading branch information
vzurauskas committed Nov 2, 2019
1 parent 3ab95bb commit bcc2b76
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 5 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/vzurauskas/nereides/javax/EmptyJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.vzurauskas.nereides.javax;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
* Empty {@link Json}. It is equivalent to {@code new Json.Of("{}")}.
*/
public final class EmptyJson implements Json {
@Override
public InputStream bytes() {
return new ByteArrayInputStream("{}".getBytes());
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/vzurauskas/nereides/javax/MissingJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.vzurauskas.nereides.javax;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
* {@link Json}, which is missing. It is equivalent to
* {@code new Json.Of(new byte[0])}.
*/
public final class MissingJson implements Json {

private static final byte[] MISSING = new byte[0];

@Override
public InputStream bytes() {
return new ByteArrayInputStream(MISSING);
}
}
22 changes: 17 additions & 5 deletions src/main/java/com/vzurauskas/nereides/javax/SmartJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.Writer;
import java.util.Collections;
import java.util.Optional;
import javax.json.JsonException;
import javax.json.JsonObject;
import javax.json.JsonStructure;
import javax.json.JsonValue;
Expand Down Expand Up @@ -176,10 +177,21 @@ public JsonStructure jsonStructure() {
* @return The nested JSON, which could be missing.
*/
public SmartJson at(String path) {
return new SmartJson(
new Of(
structure.value().asJsonObject().getValue(path).asJsonObject()
)
);
JsonObject whole = structure.value().asJsonObject();
JsonValue target;
try {
target = whole.getValue(path);
} catch (JsonException e) {
return new SmartJson(new MissingJson());
}
return new SmartJson(new Json.Of(target.asJsonObject()));
}

/**
* Method which tells if this JSON is missing.
* @return true if this JSON is missing; otherwise false.
*/
public boolean isMissing() {
return byteArray().length == 0;
}
}
13 changes: 13 additions & 0 deletions src/test/java/com/vzurauskas/nereides/javax/EmptyJsonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.vzurauskas.nereides.javax;

import org.junit.jupiter.api.Test;

final class EmptyJsonTest {
@Test
void creates() {
new EqualityAssertion(
new Json.Of("{}"),
new EmptyJson()
).affirm();
}
}
20 changes: 20 additions & 0 deletions src/test/java/com/vzurauskas/nereides/javax/EqualityAssertion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.vzurauskas.nereides.javax;

import static org.junit.jupiter.api.Assertions.assertEquals;

public final class EqualityAssertion {
private final Json first;
private final Json second;

public EqualityAssertion(Json first, Json second) {
this.first = first;
this.second = second;
}

public void affirm() {
assertEquals(
new SmartJson(first).pretty(),
new SmartJson(second).pretty()
);
}
}
15 changes: 15 additions & 0 deletions src/test/java/com/vzurauskas/nereides/javax/MissingJsonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.vzurauskas.nereides.javax;

import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import org.junit.jupiter.api.Test;

final class MissingJsonTest {
@Test
void creates() throws IOException {
assertEquals(
0,
new MissingJson().bytes().available()
);
}
}
19 changes: 19 additions & 0 deletions src/test/java/com/vzurauskas/nereides/javax/SmartJsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,15 @@ void findsPathInArray() {
);
}

@Test
void handlesNonExistentPaths() {
assertTrue(
new SmartJson(
new Json.Of(deep)
).at("/ocean/nothing").isMissing()
);
}

@Disabled("https://github.com/vzurauskas/nereides-javax/issues/26")
@Test
void understandsArrays() {
Expand All @@ -335,4 +344,14 @@ void reallyUnderstandsArrays() {
).at("/ocean/rock1/nereid1/associates").at("/0").leaf("name").get()
);
}

@Test
void knowsIfMissing() {
assertTrue(new SmartJson(new MissingJson()).isMissing());
}

@Test
void knowsIfNotMissing() {
assertFalse(new SmartJson(new Json.Of("{}")).isMissing());
}
}

0 comments on commit bcc2b76

Please sign in to comment.