From 48d0b548492a3fc0b072543be81b5e1b0ea1f278 Mon Sep 17 00:00:00 2001 From: Anu Engineer Date: Thu, 10 May 2018 12:43:13 -0700 Subject: [PATCH] HDDS-31. Fix TestSCMCli. Contributed by Lokesh Jain. --- .../common/helpers/ContainerInfo.java | 26 ++++++ .../cli/container/CreateContainerHandler.java | 8 +- .../cli/container/ListContainerHandler.java | 8 +- .../apache/hadoop/ozone/scm/TestSCMCli.java | 80 +++++++++---------- 4 files changed, 74 insertions(+), 48 deletions(-) diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/common/helpers/ContainerInfo.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/common/helpers/ContainerInfo.java index 0bd4c26d42a..10fd96c175a 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/common/helpers/ContainerInfo.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/common/helpers/ContainerInfo.java @@ -18,6 +18,10 @@ package org.apache.hadoop.hdds.scm.container.common.helpers; +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; import com.google.common.base.Preconditions; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; @@ -25,6 +29,7 @@ import org.apache.hadoop.hdds.scm.container.ContainerID; import org.apache.hadoop.hdds.protocol.proto.HddsProtos; import org.apache.hadoop.util.Time; +import java.io.IOException; import java.util.Comparator; /** @@ -32,6 +37,17 @@ import java.util.Comparator; */ public class ContainerInfo implements Comparator, Comparable { + + private static final ObjectWriter WRITER; + + static { + ObjectMapper mapper = new ObjectMapper(); + mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); + mapper + .setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE); + WRITER = mapper.writer(); + } + private HddsProtos.LifeCycleState state; private Pipeline pipeline; // Bytes allocated by SCM for clients. @@ -254,6 +270,16 @@ public class ContainerInfo return this.compare(this, o); } + /** + * Returns a JSON string of this object. + * + * @return String - json string + * @throws IOException + */ + public String toJsonString() throws IOException { + return WRITER.writeValueAsString(this); + } + /** * Builder class for ContainerInfo. */ diff --git a/hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/CreateContainerHandler.java b/hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/CreateContainerHandler.java index c0ff1f7a6d6..278ee30842f 100644 --- a/hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/CreateContainerHandler.java +++ b/hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/CreateContainerHandler.java @@ -44,6 +44,10 @@ public class CreateContainerHandler extends OzoneCommandHandler { @Override public void execute(CommandLine cmd) throws IOException { + if (cmd.hasOption(HELP_OP)) { + displayHelp(); + } + if (!cmd.hasOption(CONTAINER_CREATE)) { throw new IOException("Expecting container create"); } @@ -57,7 +61,7 @@ public class CreateContainerHandler extends OzoneCommandHandler { public void displayHelp() { Options options = new Options(); HelpFormatter helpFormatter = new HelpFormatter(); - helpFormatter.printHelp(CMD_WIDTH, "hdfs scm -container -create