HDFS-8741. Proper error msg to be printed when invalid operation type is given to WebHDFS operations. Contributed by Surendra Singh Lilhore.

This commit is contained in:
Yiqun Lin 2017-03-06 19:04:03 +08:00 committed by Anu Engineer
parent 31f306c7d1
commit b13878cf50
5 changed files with 81 additions and 4 deletions

View File

@ -72,7 +72,16 @@ public class DeleteOpParam extends HttpOpParam<DeleteOpParam.Op> {
* @param str a string representation of the parameter value.
*/
public DeleteOpParam(final String str) {
super(DOMAIN, DOMAIN.parse(str));
super(DOMAIN, getOp(str));
}
private static Op getOp(String str) {
try {
return DOMAIN.parse(str);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(str + " is not a valid " + Type.DELETE
+ " operation.");
}
}
@Override

View File

@ -111,7 +111,16 @@ public class GetOpParam extends HttpOpParam<GetOpParam.Op> {
* @param str a string representation of the parameter value.
*/
public GetOpParam(final String str) {
super(DOMAIN, DOMAIN.parse(str));
super(DOMAIN, getOp(str));
}
private static Op getOp(String str) {
try {
return DOMAIN.parse(str);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(str + " is not a valid " + Type.GET
+ " operation.");
}
}
@Override

View File

@ -80,7 +80,16 @@ public class PostOpParam extends HttpOpParam<PostOpParam.Op> {
* @param str a string representation of the parameter value.
*/
public PostOpParam(final String str) {
super(DOMAIN, DOMAIN.parse(str));
super(DOMAIN, getOp(str));
}
private static Op getOp(String str) {
try {
return DOMAIN.parse(str);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(str + " is not a valid " + Type.POST
+ " operation.");
}
}
@Override

View File

@ -107,7 +107,16 @@ public class PutOpParam extends HttpOpParam<PutOpParam.Op> {
* @param str a string representation of the parameter value.
*/
public PutOpParam(final String str) {
super(DOMAIN, DOMAIN.parse(str));
super(DOMAIN, getOp(str));
}
private static Op getOp(String str) {
try {
return DOMAIN.parse(str);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(str + " is not a valid " + Type.PUT
+ " operation.");
}
}
@Override

View File

@ -36,6 +36,7 @@ import org.apache.hadoop.fs.XAttrSetFlag;
import org.apache.hadoop.fs.permission.AclEntry;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.hdfs.DFSConfigKeys;
import org.apache.hadoop.test.GenericTestUtils;
import org.apache.hadoop.util.StringUtils;
import org.junit.Assert;
import org.junit.Test;
@ -503,4 +504,44 @@ public class TestParam {
p = new StoragePolicyParam("COLD");
Assert.assertEquals("COLD", p.getValue());
}
@Test
public void testHttpOpParams() {
try {
new PostOpParam("TEST");
Assert
.fail("Construct the PostOpParam with param value 'TEST' should be"
+ " failed.");
} catch (IllegalArgumentException e) {
GenericTestUtils.assertExceptionContains(
"TEST is not a valid POST operation.", e);
}
try {
new PutOpParam("TEST");
Assert
.fail("Construct the PutOpParam with param value 'TEST' should be"
+ " failed.");
} catch (IllegalArgumentException e) {
GenericTestUtils.assertExceptionContains(
"TEST is not a valid PUT operation.", e);
}
try {
new DeleteOpParam("TEST");
Assert
.fail("Construct the DeleteOpParam with param value 'TEST' should be"
+ " failed.");
} catch (IllegalArgumentException e) {
GenericTestUtils.assertExceptionContains(
"TEST is not a valid DELETE operation.", e);
}
try {
new GetOpParam("TEST");
Assert
.fail("Construct the GetOpParam with param value 'TEST' should be"
+ " failed.");
} catch (IllegalArgumentException e) {
GenericTestUtils.assertExceptionContains(
"TEST is not a valid GET operation.", e);
}
}
}