[ 
https://issues.apache.org/jira/browse/GEODE-3872?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16248152#comment-16248152
 ] 

ASF GitHub Bot commented on GEODE-3872:
---------------------------------------

jinmeiliao closed pull request #1043: GEODE-3872: simplify the Command result 
Assert
URL: https://github.com/apache/geode/pull/1043
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/DestroyRegionCommandTest.java
 
b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/DestroyRegionCommandTest.java
index b519c8d423..7cfe1263a5 100644
--- 
a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/DestroyRegionCommandTest.java
+++ 
b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/DestroyRegionCommandTest.java
@@ -31,6 +31,7 @@
 import org.apache.geode.internal.cache.InternalCache;
 import org.apache.geode.management.cli.Result;
 import org.apache.geode.management.internal.cli.result.CommandResult;
+import org.apache.geode.test.junit.assertions.CommandResultAssert;
 import org.apache.geode.test.junit.categories.UnitTest;
 import org.apache.geode.test.junit.rules.GfshParserRule;
 
@@ -68,9 +69,10 @@ public void invalidRegion() throws Exception {
   public void whenNoRegionIsFoundOnAnyMembers() throws Exception {
     doReturn(Collections.emptySet()).when(command).findMembersForRegion(any(), 
any());
     result = parser.executeCommandWithInstance(command, "destroy region 
--name=test");
-    assertThat(result.getStatus()).isEqualTo(Result.Status.ERROR);
+    new CommandResultAssert(result).statusIsError()
+        .containsOutput("Could not find a Region with Region path");
 
     result = parser.executeCommandWithInstance(command, "destroy region 
--name=test --if-exists");
-    assertThat(result.getStatus()).isEqualTo(Result.Status.OK);
+    new CommandResultAssert(result).statusIsSuccess();
   }
 }
diff --git 
a/geode-core/src/test/java/org/apache/geode/test/junit/assertions/GfshShellConnectionRuleAssert.java
 
b/geode-core/src/test/java/org/apache/geode/test/junit/assertions/CommandResultAssert.java
similarity index 80%
rename from 
geode-core/src/test/java/org/apache/geode/test/junit/assertions/GfshShellConnectionRuleAssert.java
rename to 
geode-core/src/test/java/org/apache/geode/test/junit/assertions/CommandResultAssert.java
index 79d54d9c86..f23326ba67 100644
--- 
a/geode-core/src/test/java/org/apache/geode/test/junit/assertions/GfshShellConnectionRuleAssert.java
+++ 
b/geode-core/src/test/java/org/apache/geode/test/junit/assertions/CommandResultAssert.java
@@ -17,8 +17,6 @@
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.util.Arrays;
-import java.util.List;
-import java.util.Optional;
 
 import org.assertj.core.api.AbstractAssert;
 import org.assertj.core.api.Assertions;
@@ -27,14 +25,18 @@
 import org.apache.geode.management.cli.Result;
 import org.apache.geode.management.internal.cli.json.GfJsonObject;
 import org.apache.geode.management.internal.cli.result.CommandResult;
-import org.apache.geode.test.junit.rules.GfshShellConnectionRule;
 
 
-public class GfshShellConnectionRuleAssert
-    extends AbstractAssert<GfshShellConnectionRuleAssert, 
GfshShellConnectionRuleExecution> {
-  public GfshShellConnectionRuleAssert(GfshShellConnectionRule gfsh, 
CommandResult commandResult) {
-    super(new GfshShellConnectionRuleExecution(gfsh, commandResult),
-        GfshShellConnectionRuleAssert.class);
+public class CommandResultAssert
+    extends AbstractAssert<CommandResultAssert, CommandResultExecution> {
+
+  public CommandResultAssert(CommandResult commandResult) {
+    super(new CommandResultExecution(commandResult.toJson(), commandResult),
+        CommandResultAssert.class);
+  }
+
+  public CommandResultAssert(String output, CommandResult commandResult) {
+    super(new CommandResultExecution(output, commandResult), 
CommandResultAssert.class);
   }
 
   /**
@@ -57,8 +59,8 @@ public GfshShellConnectionRuleAssert(GfshShellConnectionRule 
gfsh, CommandResult
    * <code> containsKeyValuePair("Key Class", "java.lang.String"); </code>
    * </pre>
    */
-  public GfshShellConnectionRuleAssert containsKeyValuePair(String key, String 
value) {
-    assertThat(actual.getGfshOutput()).containsPattern(key + "\\s+: " + value);
+  public CommandResultAssert containsKeyValuePair(String key, String value) {
+    assertThat(actual.getOutput()).containsPattern(key + "\\s+: " + value);
 
     return this;
   }
@@ -66,9 +68,9 @@ public GfshShellConnectionRuleAssert 
containsKeyValuePair(String key, String val
   /**
    * Verifies the gfsh output contains the given output
    */
-  public GfshShellConnectionRuleAssert containsOutput(String... 
expectedOutputs) {
+  public CommandResultAssert containsOutput(String... expectedOutputs) {
     for (String expectedOutput : expectedOutputs) {
-      assertThat(actual.getGfshOutput()).contains(expectedOutput);
+      assertThat(actual.getOutput()).contains(expectedOutput);
     }
 
     return this;
@@ -77,9 +79,9 @@ public GfshShellConnectionRuleAssert containsOutput(String... 
expectedOutputs) {
   /**
    * Verifies the gfsh output does not contain the given output
    */
-  public GfshShellConnectionRuleAssert doesNotContainOutput(String... 
expectedOutputs) {
+  public CommandResultAssert doesNotContainOutput(String... expectedOutputs) {
     for (String expectedOutput : expectedOutputs) {
-      assertThat(actual.getGfshOutput()).doesNotContain(expectedOutput);
+      assertThat(actual.getOutput()).doesNotContain(expectedOutput);
     }
 
     return this;
@@ -88,9 +90,9 @@ public GfshShellConnectionRuleAssert 
doesNotContainOutput(String... expectedOutp
   /**
    * Verifies that gfsh executed with status OK
    */
-  public GfshShellConnectionRuleAssert statusIsSuccess() {
+  public CommandResultAssert statusIsSuccess() {
     CommandResult result = actual.getCommandResult();
-    
Assertions.assertThat(result.getStatus()).describedAs(actual.getGfsh().getGfshOutput())
+    Assertions.assertThat(result.getStatus()).describedAs(actual.getOutput())
         .isEqualTo(Result.Status.OK);
 
     return this;
@@ -99,9 +101,9 @@ public GfshShellConnectionRuleAssert statusIsSuccess() {
   /**
    * Verifies that gfsh executed with status ERROR
    */
-  public GfshShellConnectionRuleAssert statusIsError() {
+  public CommandResultAssert statusIsError() {
     CommandResult result = actual.getCommandResult();
-    
Assertions.assertThat(result.getStatus()).describedAs(actual.getGfsh().getGfshOutput())
+    Assertions.assertThat(result.getStatus()).describedAs(actual.getOutput())
         .isEqualTo(Result.Status.ERROR);
 
     return this;
@@ -127,7 +129,7 @@ public GfshShellConnectionRuleAssert statusIsError() {
    * </code>
    * </pre>
    */
-  public GfshShellConnectionRuleAssert 
tableHasColumnWithExactValuesInExactOrder(String header,
+  public CommandResultAssert tableHasColumnWithExactValuesInExactOrder(String 
header,
       Object... expectedValues) {
     GfJsonObject resultContentJSON = actual.getCommandResult().getContent();
     Object content = resultContentJSON.get(header);
@@ -163,7 +165,7 @@ public GfshShellConnectionRuleAssert 
tableHasColumnWithExactValuesInExactOrder(S
    * <code> tableHasColumnWithExactValuesInAnyOrder("Region Path", "/region2", 
"/region1"); </code>
    * </pre>
    */
-  public GfshShellConnectionRuleAssert 
tableHasColumnWithExactValuesInAnyOrder(String header,
+  public CommandResultAssert tableHasColumnWithExactValuesInAnyOrder(String 
header,
       Object... expectedValues) {
     GfJsonObject resultContentJSON = actual.getCommandResult().getContent();
     Object content = resultContentJSON.get(header);
@@ -183,7 +185,7 @@ public GfshShellConnectionRuleAssert 
tableHasColumnWithExactValuesInAnyOrder(Str
    * Verifies that each of the actual values in the column with the given 
header contains at least
    * one of the expectedValues.
    */
-  public GfshShellConnectionRuleAssert 
tableHasColumnWithValuesContaining(String header,
+  public CommandResultAssert tableHasColumnWithValuesContaining(String header,
       String... expectedValues) {
     GfJsonObject resultContentJSON = actual.getCommandResult().getContent();
     Object content = resultContentJSON.get(header);
@@ -208,13 +210,13 @@ public GfshShellConnectionRuleAssert 
tableHasColumnWithValuesContaining(String h
     return this;
   }
 
-  public GfshShellConnectionRuleAssert hasResult() {
+  public CommandResultAssert hasResult() {
     containsKeyValuePair("Result", "true");
 
     return this;
   }
 
-  public GfshShellConnectionRuleAssert hasNoResult() {
+  public CommandResultAssert hasNoResult() {
     containsKeyValuePair("Result", "false");
 
     return this;
diff --git 
a/geode-core/src/test/java/org/apache/geode/test/junit/assertions/GfshShellConnectionRuleExecution.java
 
b/geode-core/src/test/java/org/apache/geode/test/junit/assertions/CommandResultExecution.java
similarity index 71%
rename from 
geode-core/src/test/java/org/apache/geode/test/junit/assertions/GfshShellConnectionRuleExecution.java
rename to 
geode-core/src/test/java/org/apache/geode/test/junit/assertions/CommandResultExecution.java
index b4c3003e94..ec79c18ce6 100644
--- 
a/geode-core/src/test/java/org/apache/geode/test/junit/assertions/GfshShellConnectionRuleExecution.java
+++ 
b/geode-core/src/test/java/org/apache/geode/test/junit/assertions/CommandResultExecution.java
@@ -15,28 +15,21 @@
 package org.apache.geode.test.junit.assertions;
 
 import org.apache.geode.management.internal.cli.result.CommandResult;
-import org.apache.geode.test.junit.rules.GfshShellConnectionRule;
 
-public class GfshShellConnectionRuleExecution {
-  private GfshShellConnectionRule gfsh;
+public class CommandResultExecution {
   private CommandResult commandResult;
+  private String output;
 
-  public GfshShellConnectionRuleExecution(GfshShellConnectionRule gfsh,
-      CommandResult commandResult) {
-
-    this.gfsh = gfsh;
+  public CommandResultExecution(String output, CommandResult commandResult) {
+    this.output = output;
     this.commandResult = commandResult;
   }
 
-  public GfshShellConnectionRule getGfsh() {
-    return gfsh;
-  }
-
   public CommandResult getCommandResult() {
     return commandResult;
   }
 
-  public String getGfshOutput() {
-    return gfsh.getGfshOutput();
+  public String getOutput() {
+    return output;
   }
 }
diff --git 
a/geode-core/src/test/java/org/apache/geode/test/junit/rules/GfshParserRule.java
 
b/geode-core/src/test/java/org/apache/geode/test/junit/rules/GfshParserRule.java
index 9e1b220cf0..20592be04b 100644
--- 
a/geode-core/src/test/java/org/apache/geode/test/junit/rules/GfshParserRule.java
+++ 
b/geode-core/src/test/java/org/apache/geode/test/junit/rules/GfshParserRule.java
@@ -34,6 +34,7 @@
 import org.apache.geode.management.internal.cli.GfshParser;
 import org.apache.geode.management.internal.cli.result.CommandResult;
 import org.apache.geode.management.internal.cli.result.ResultBuilder;
+import org.apache.geode.test.junit.assertions.CommandResultAssert;
 
 public class GfshParserRule extends ExternalResource {
 
@@ -81,6 +82,11 @@ public GfshParseResult parse(String command) {
         parseResult.getArguments());
   }
 
+  public <T> CommandResultAssert executeAndAssertThat(T instance, String 
command) {
+    CommandResult result = executeCommandWithInstance(instance, command);
+    return new CommandResultAssert(result);
+  }
+
   public CommandCandidate complete(String command) {
     List<Completion> candidates = new ArrayList<>();
     int cursor = parser.completeAdvanced(command, command.length(), 
candidates);
diff --git 
a/geode-core/src/test/java/org/apache/geode/test/junit/rules/GfshShellConnectionRule.java
 
b/geode-core/src/test/java/org/apache/geode/test/junit/rules/GfshShellConnectionRule.java
index 6f8de9ba2a..1b09cb9c09 100644
--- 
a/geode-core/src/test/java/org/apache/geode/test/junit/rules/GfshShellConnectionRule.java
+++ 
b/geode-core/src/test/java/org/apache/geode/test/junit/rules/GfshShellConnectionRule.java
@@ -34,7 +34,7 @@
 import org.apache.geode.management.internal.cli.shell.Gfsh;
 import org.apache.geode.management.internal.cli.util.CommandStringBuilder;
 import org.apache.geode.test.dunit.IgnoredException;
-import org.apache.geode.test.junit.assertions.GfshShellConnectionRuleAssert;
+import org.apache.geode.test.junit.assertions.CommandResultAssert;
 
 /**
  * Class which eases the connection to the locator/jmxManager in Gfsh shell 
and execute gfsh
@@ -238,9 +238,9 @@ public CommandResult executeCommand(String command) {
     return result;
   }
 
-  public GfshShellConnectionRuleAssert executeAndAssertThat(String command) {
+  public CommandResultAssert executeAndAssertThat(String command) {
     CommandResult commandResult = executeCommand(command);
-    return new GfshShellConnectionRuleAssert(this, commandResult);
+    return new CommandResultAssert(gfsh.outputString, commandResult);
   }
 
 
diff --git 
a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/cli/LuceneIndexCommandsDUnitTest.java
 
b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/cli/LuceneIndexCommandsDUnitTest.java
index 2aa38041fe..e87ed02a1d 100755
--- 
a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/cli/LuceneIndexCommandsDUnitTest.java
+++ 
b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/cli/LuceneIndexCommandsDUnitTest.java
@@ -59,7 +59,7 @@
 import org.apache.geode.management.internal.cli.util.CommandStringBuilder;
 import org.apache.geode.test.dunit.rules.LocatorServerStartupRule;
 import org.apache.geode.test.dunit.rules.MemberVM;
-import org.apache.geode.test.junit.assertions.GfshShellConnectionRuleAssert;
+import org.apache.geode.test.junit.assertions.CommandResultAssert;
 import org.apache.geode.test.junit.categories.DistributedTest;
 import org.apache.geode.test.junit.rules.GfshShellConnectionRule;
 import org.apache.geode.test.junit.rules.serializable.SerializableTestName;
@@ -461,8 +461,7 @@ public void 
searchShouldReturnResultsInCorrectOrderOfScore() throws Exception {
     csb.addOption(LuceneCliStrings.LUCENE_SEARCH_INDEX__QUERY_STRING, 
"field1:jon~");
     csb.addOption(LuceneCliStrings.LUCENE_SEARCH_INDEX__DEFAULT_FIELD, 
"field1");
 
-    GfshShellConnectionRuleAssert assertion =
-        gfsh.executeAndAssertThat(csb.toString()).statusIsSuccess();
+    CommandResultAssert assertion = 
gfsh.executeAndAssertThat(csb.toString()).statusIsSuccess();
 
     try {
       assertion.tableHasColumnWithExactValuesInExactOrder("key", "A", "B", 
"C", "D");


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> Consolidate GfshShellConnectionRule assertions
> ----------------------------------------------
>
>                 Key: GEODE-3872
>                 URL: https://issues.apache.org/jira/browse/GEODE-3872
>             Project: Geode
>          Issue Type: Improvement
>          Components: gfsh, tests
>            Reporter: Jared Stewart
>            Assignee: Jared Stewart
>             Fix For: 1.4.0
>
>
> We have started to accumulate several flavors of _executeCommand_ methods on 
> GfshShellConnectionRule that implicitly make certain kinds of assertions.  
> (For example, assertions on the command result status or on the output text.) 
>  It would make the code more maintainable to separate the various assertion 
> methods into a separate class and to have a single executeCommand method.
> (See e.g. 
> http://joel-costigliola.github.io/assertj/assertj-core-custom-assertions.html#custom-assertion-class)



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to