Skip to content

Commit

Permalink
Verify that REPO_TAGS can be processed as a JsonArray (#1355)
Browse files Browse the repository at this point in the history
REPO_TAGS member may be null (JsonNull), so we need to make sure it is an instance of JsonArray

Signed-off-by: Robin Raju <[email protected]>

Co-authored-by: Roland Huß <[email protected]>
  • Loading branch information
robinr618 and rhuss authored Aug 30, 2020
1 parent 7a74393 commit b94598f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/main/java/io/fabric8/maven/docker/model/ImageDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Map;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

Expand Down Expand Up @@ -62,8 +63,8 @@ public Map<String, String> getLabels() {
public List<String> getRepoTags() {
List<String> repoTags = new ArrayList<>();

if (json.has(REPO_TAGS)) {
for(JsonElement item : json.getAsJsonArray(REPO_TAGS)) {
if (json.has(REPO_TAGS) && json.get(REPO_TAGS) instanceof JsonArray) {
for (JsonElement item : json.getAsJsonArray(REPO_TAGS)) {
repoTags.add(item.getAsString());
}
}
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/io/fabric8/maven/docker/model/ImageDetailsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.gson.JsonNull;
import org.junit.Before;
import org.junit.Test;
import com.google.gson.JsonArray;
Expand Down Expand Up @@ -42,6 +43,41 @@ private void givenAnImageWithLabels() {
json.add(ImageDetails.LABELS, labels);
}

@Test
public void testImageWithRepoTags() {
givenImageData();
whenCreateImage();
thenRepoTagsSizeIs(2);
}

@Test
public void testImageWithNullRepoTags() {
givenAnImageWithNullRepoTags();
whenCreateImage();
thenRepoTagsSizeIs(0);
}

@Test
public void testImageWitEmptyRepoTags() {
givenAnImageWithEmptyRepoTags();
whenCreateImage();
thenRepoTagsSizeIs(0);
}

@Test
public void testImageWitNoRepoTags() {
whenCreateImage();
thenRepoTagsSizeIs(0);
}

private void givenAnImageWithNullRepoTags() {
json.add(ImageDetails.REPO_TAGS, JsonNull.INSTANCE);
}

private void givenAnImageWithEmptyRepoTags() {
json.add(ImageDetails.REPO_TAGS, new JsonArray());
}

@Test
public void testCreateImage() throws Exception {
givenImageData();
Expand All @@ -68,6 +104,10 @@ private void thenLabelsSizeIs(int size) {
assertEquals(size, image.getLabels().size());
}

private void thenRepoTagsSizeIs(int size) {
assertEquals(size, image.getRepoTags().size());
}

private void thenValidateImage() {
assertEquals("b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc", image.getId());
assertEquals("27cf784147099545", image.getParentId());
Expand Down

0 comments on commit b94598f

Please sign in to comment.