You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by "wzhallright (via GitHub)" <gi...@apache.org> on 2023/12/25 02:33:47 UTC
[PR] HDDS-9982. Improve assertTrue assertions in hdds-server-framework [ozone]
wzhallright opened a new pull request, #5862:
URL: https://github.com/apache/ozone/pull/5862
## What changes were proposed in this pull request?
Improve assertTrue assertions in hadoop-hdds/framework
Assertions in the form:
```
assertTrue(<string>.contains(...))
```
do not provide any information about the actual value, if the assertion fails.
```
AssertionFailedError: expected: <true> but was: <false>
```
This can be improved by replacing them with:
```
import static org.assertj.core.api.Assertions.assertThat;
assertThat(<string>).contains(...)
```
which gives us more info in the form:
```
AssertionError:
Expecting:
<"actual string">
to contain:
<"part">
```
Similarly, assertions about inequality relations, e.g.:
```
assertTrue(<x> > <y>)
```
can be replaced with:
```
assertThat(<x>).isGreaterThan(<y>);
```
The goal of this task is to find and replace such assertTrue assertions.
## What is the link to the Apache JIRA
[HDDS-9982](https://issues.apache.org/jira/browse/HDDS-9982?filter=-1)
## How was this patch tested?
## CI
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-9982. Improve assertTrue assertions in hdds-server-framework [ozone]
Posted by "adoroszlai (via GitHub)" <gi...@apache.org>.
adoroszlai commented on code in PR #5862:
URL: https://github.com/apache/ozone/pull/5862#discussion_r1436157576
##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/TestJsonUtils.java:
##########
@@ -46,7 +46,8 @@ public void printObjectAsJson() throws IOException {
}
private static void assertContains(String str, String part) {
- assertTrue(str.contains(part),
- "Expected JSON to contain '" + part + "', but didn't: " + str);
+ assertThat(str)
+ .withFailMessage("Expected JSON to contain '" + part + "', but didn't: " + str)
Review Comment:
I think `withFailMessage` can be omitted here, too.
##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/authority/TestDefaultCAServer.java:
##########
@@ -296,8 +297,7 @@ public void testRevokeCertificates() throws Exception {
CRLReason.lookup(CRLReason.keyCompromise), now);
result.get();
});
- assertTrue(execution.getCause().getMessage()
- .contains("Certificates cannot be null"));
+ assertThat(execution.getCause().getMessage()).contains("Certificates cannot be null");
Review Comment:
nit: keeping `.contains...` on new line makes the assertion more readable, because `assertThat`'s argument is also a chained method call
##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/symmetric/TestSecretKeyManager.java:
##########
@@ -110,16 +110,15 @@ public void testLoadSecretKeys(List<ManagedSecretKey> savedSecretKey,
// expect the current key is newly generated.
assertFalse(savedSecretKey.contains(state.getCurrentKey()));
Review Comment:
This one can also be converted to `assertThat`.
##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/ozone/audit/TestOzoneAuditLogger.java:
##########
@@ -183,11 +183,11 @@ public void verifyDefaultLogLevelForAuthFailure() throws IOException {
@Test
public void messageIncludesAllParts() {
String message = WRITE_FAIL_MSG.getFormattedMessage();
- assertTrue(message.contains(USER), message);
- assertTrue(message.contains(IP_ADDRESS), message);
- assertTrue(message.contains(DummyAction.CREATE_VOLUME.name()), message);
- assertTrue(message.contains(PARAMS.toString()), message);
- assertTrue(message.contains(FAILURE.getStatus()), message);
+ assertThat(message).withFailMessage(message).contains(USER);
+ assertThat(message).withFailMessage(message).contains(IP_ADDRESS);
+ assertThat(message).withFailMessage(message).contains(DummyAction.CREATE_VOLUME.name());
+ assertThat(message).withFailMessage(message).contains(PARAMS.toString());
+ assertThat(message).withFailMessage(message).contains(FAILURE.getStatus());
Review Comment:
With `assertTrue(..., message)`, the second parameter was only passed to get some useful hint in case of failure. When using `assertThat`, we get such hint automatically, so we can omit `withFailMessage(message)`.
##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/TestRDBTableStore.java:
##########
@@ -692,7 +694,7 @@ public void testDumpAndLoadBasic() throws Exception {
// check dump file exist
assertTrue(dumpFile.exists());
- assertTrue(dumpFile.length() != 0);
+ assertNotEquals(1, dumpFile.length());
Review Comment:
Here the "not equal" value is changed from 0 to 1. Why?
##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/conf/TestHddsConfServlet.java:
##########
@@ -254,13 +255,12 @@ private void verifyGetProperty(OzoneConfiguration conf, String format,
// in the response
if (Strings.isNullOrEmpty(propertyName)) {
for (Map.Entry<String, String> entry : TEST_PROPERTIES.entrySet()) {
- assertTrue(result.contains(entry.getKey()) &&
- result.contains(entry.getValue()));
+ assertThat(result).contains(entry.getKey(), entry.getValue());
}
} else {
if (conf.get(propertyName) != null) {
// if property name is not empty and property is found
- assertTrue(result.contains(propertyName));
+ assertThat(result).contains(propertyName);
for (Map.Entry<String, String> entry : TEST_PROPERTIES.entrySet()) {
if (!entry.getKey().equals(propertyName)) {
assertFalse(result.contains(entry.getKey()));
Review Comment:
`assertFalse` assertions can be changed along the same lines:
```
assertThat(result).doesNotContain(entry.getKey());
```
Sorry if that wasn't clear from the task description. I have now updated the parent issue.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-9982. Improve assertTrue assertions in hdds-server-framework [ozone]
Posted by "wzhallright (via GitHub)" <gi...@apache.org>.
wzhallright commented on code in PR #5862:
URL: https://github.com/apache/ozone/pull/5862#discussion_r1436185154
##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/conf/TestHddsConfServlet.java:
##########
@@ -254,13 +255,12 @@ private void verifyGetProperty(OzoneConfiguration conf, String format,
// in the response
if (Strings.isNullOrEmpty(propertyName)) {
for (Map.Entry<String, String> entry : TEST_PROPERTIES.entrySet()) {
- assertTrue(result.contains(entry.getKey()) &&
- result.contains(entry.getValue()));
+ assertThat(result).contains(entry.getKey(), entry.getValue());
}
} else {
if (conf.get(propertyName) != null) {
// if property name is not empty and property is found
- assertTrue(result.contains(propertyName));
+ assertThat(result).contains(propertyName);
for (Map.Entry<String, String> entry : TEST_PROPERTIES.entrySet()) {
if (!entry.getKey().equals(propertyName)) {
assertFalse(result.contains(entry.getKey()));
Review Comment:
fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-9982. Improve assertTrue assertions in hdds-server-framework [ozone]
Posted by "adoroszlai (via GitHub)" <gi...@apache.org>.
adoroszlai merged PR #5862:
URL: https://github.com/apache/ozone/pull/5862
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-9982. Improve assertTrue assertions in hdds-server-framework [ozone]
Posted by "wzhallright (via GitHub)" <gi...@apache.org>.
wzhallright commented on code in PR #5862:
URL: https://github.com/apache/ozone/pull/5862#discussion_r1436186153
##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/ozone/audit/TestOzoneAuditLogger.java:
##########
@@ -183,11 +183,11 @@ public void verifyDefaultLogLevelForAuthFailure() throws IOException {
@Test
public void messageIncludesAllParts() {
String message = WRITE_FAIL_MSG.getFormattedMessage();
- assertTrue(message.contains(USER), message);
- assertTrue(message.contains(IP_ADDRESS), message);
- assertTrue(message.contains(DummyAction.CREATE_VOLUME.name()), message);
- assertTrue(message.contains(PARAMS.toString()), message);
- assertTrue(message.contains(FAILURE.getStatus()), message);
+ assertThat(message).withFailMessage(message).contains(USER);
+ assertThat(message).withFailMessage(message).contains(IP_ADDRESS);
+ assertThat(message).withFailMessage(message).contains(DummyAction.CREATE_VOLUME.name());
+ assertThat(message).withFailMessage(message).contains(PARAMS.toString());
+ assertThat(message).withFailMessage(message).contains(FAILURE.getStatus());
Review Comment:
Do we need to omit all waitFailMessage or allow the user to partially customize useful messages?
Because there is still some user-defined information in the current PR
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-9982. Improve assertTrue assertions in hdds-server-framework [ozone]
Posted by "wzhallright (via GitHub)" <gi...@apache.org>.
wzhallright commented on code in PR #5862:
URL: https://github.com/apache/ozone/pull/5862#discussion_r1436185306
##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/authority/TestDefaultCAServer.java:
##########
@@ -296,8 +297,7 @@ public void testRevokeCertificates() throws Exception {
CRLReason.lookup(CRLReason.keyCompromise), now);
result.get();
});
- assertTrue(execution.getCause().getMessage()
- .contains("Certificates cannot be null"));
+ assertThat(execution.getCause().getMessage()).contains("Certificates cannot be null");
Review Comment:
fixed
##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/symmetric/TestSecretKeyManager.java:
##########
@@ -110,16 +110,15 @@ public void testLoadSecretKeys(List<ManagedSecretKey> savedSecretKey,
// expect the current key is newly generated.
assertFalse(savedSecretKey.contains(state.getCurrentKey()));
Review Comment:
fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-9982. Improve assertTrue assertions in hdds-server-framework [ozone]
Posted by "wzhallright (via GitHub)" <gi...@apache.org>.
wzhallright commented on code in PR #5862:
URL: https://github.com/apache/ozone/pull/5862#discussion_r1436185214
##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/TestRDBTableStore.java:
##########
@@ -692,7 +694,7 @@ public void testDumpAndLoadBasic() throws Exception {
// check dump file exist
assertTrue(dumpFile.exists());
- assertTrue(dumpFile.length() != 0);
+ assertNotEquals(1, dumpFile.length());
Review Comment:
fixed
##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/TestJsonUtils.java:
##########
@@ -46,7 +46,8 @@ public void printObjectAsJson() throws IOException {
}
private static void assertContains(String str, String part) {
- assertTrue(str.contains(part),
- "Expected JSON to contain '" + part + "', but didn't: " + str);
+ assertThat(str)
+ .withFailMessage("Expected JSON to contain '" + part + "', but didn't: " + str)
Review Comment:
fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-9982. Improve assertTrue assertions in hdds-server-framework [ozone]
Posted by "adoroszlai (via GitHub)" <gi...@apache.org>.
adoroszlai commented on code in PR #5862:
URL: https://github.com/apache/ozone/pull/5862#discussion_r1436371115
##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/ozone/audit/TestOzoneAuditLogger.java:
##########
@@ -183,11 +183,11 @@ public void verifyDefaultLogLevelForAuthFailure() throws IOException {
@Test
public void messageIncludesAllParts() {
String message = WRITE_FAIL_MSG.getFormattedMessage();
- assertTrue(message.contains(USER), message);
- assertTrue(message.contains(IP_ADDRESS), message);
- assertTrue(message.contains(DummyAction.CREATE_VOLUME.name()), message);
- assertTrue(message.contains(PARAMS.toString()), message);
- assertTrue(message.contains(FAILURE.getStatus()), message);
+ assertThat(message).withFailMessage(message).contains(USER);
+ assertThat(message).withFailMessage(message).contains(IP_ADDRESS);
+ assertThat(message).withFailMessage(message).contains(DummyAction.CREATE_VOLUME.name());
+ assertThat(message).withFailMessage(message).contains(PARAMS.toString());
+ assertThat(message).withFailMessage(message).contains(FAILURE.getStatus());
Review Comment:
I'd remove two kinds:
* where the string itself is passed (like here)
* message similar to "expected: ..., got: ..."
We can keep the rest.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-9982. Improve assertTrue assertions in hdds-server-framework [ozone]
Posted by "adoroszlai (via GitHub)" <gi...@apache.org>.
adoroszlai commented on PR #5862:
URL: https://github.com/apache/ozone/pull/5862#issuecomment-1869473663
Thanks @wzhallright for the patch.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org