This is an automated email from the ASF dual-hosted git repository. fschumacher pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/jmeter.git
commit 47eb7df908f537b0845a2059f67bd951afd0d5e9 Author: Felix Schumacher <[email protected]> AuthorDate: Sat Oct 10 13:02:16 2020 +0200 Use assertThrows to make intent clearer and use smaller test methods --- .../http/util/TestGraphQLRequestParamUtils.java | 113 ++++++++++----------- 1 file changed, 55 insertions(+), 58 deletions(-) diff --git a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestGraphQLRequestParamUtils.java b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestGraphQLRequestParamUtils.java index 545d6be..2e5f4ec 100644 --- a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestGraphQLRequestParamUtils.java +++ b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestGraphQLRequestParamUtils.java @@ -20,9 +20,10 @@ package org.apache.jmeter.protocol.http.util; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import org.apache.jmeter.config.Arguments; @@ -30,6 +31,7 @@ import org.apache.jmeter.protocol.http.config.GraphQLRequestParams; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import com.fasterxml.jackson.core.JsonProcessingException; import com.github.jknack.handlebars.internal.lang3.StringUtils; public class TestGraphQLRequestParamUtils { @@ -116,41 +118,36 @@ public class TestGraphQLRequestParamUtils { assertEquals("op1", params.getOperationName()); assertEquals("query { droid { id }}", params.getQuery()); assertEquals("{\"id\":123}", params.getVariables()); + } + + @Test + void testInvalidJsonData() throws JsonProcessingException, UnsupportedEncodingException { + assertThrows(IllegalArgumentException.class, + () -> GraphQLRequestParamUtils.toGraphQLRequestParams("".getBytes(StandardCharsets.UTF_8), null)); + + assertThrows(IllegalArgumentException.class, + () -> GraphQLRequestParamUtils.toGraphQLRequestParams("{}".getBytes(StandardCharsets.UTF_8), null)); + } + + @Test + void testInvalidGraphQueryParam() throws JsonProcessingException, UnsupportedEncodingException { + assertThrows(IllegalArgumentException.class, () -> GraphQLRequestParamUtils + .toGraphQLRequestParams("{\"query\":\"select * from emp\"}".getBytes(StandardCharsets.UTF_8), null)); + } + + @Test + void testIvalidGraphOperationName() throws JsonProcessingException, UnsupportedEncodingException { + assertThrows(IllegalArgumentException.class, () -> GraphQLRequestParamUtils.toGraphQLRequestParams( + "{\"operationName\":{\"id\":123},\"query\":\"query { droid { id }}\"}".getBytes(StandardCharsets.UTF_8), + null)); + } - try { - params = GraphQLRequestParamUtils.toGraphQLRequestParams("".getBytes(StandardCharsets.UTF_8), null); - fail("Should have failed due to invalid json data."); - } catch (IllegalArgumentException ignore) { - } - - try { - params = GraphQLRequestParamUtils.toGraphQLRequestParams("{}".getBytes(StandardCharsets.UTF_8), null); - fail("Should have failed due to invalid json data."); - } catch (IllegalArgumentException ignore) { - } - - try { - params = GraphQLRequestParamUtils - .toGraphQLRequestParams("{\"query\":\"select * from emp\"}".getBytes(StandardCharsets.UTF_8), null); - fail("Should have failed due to invalid graph query param."); - } catch (IllegalArgumentException ignore) { - } - - try { - params = GraphQLRequestParamUtils - .toGraphQLRequestParams("{\"operationName\":{\"id\":123},\"query\":\"query { droid { id }}\"}" - .getBytes(StandardCharsets.UTF_8), null); - fail("Should have failed due to invalid graph operationName type."); - } catch (IllegalArgumentException ignore) { - } - - try { - params = GraphQLRequestParamUtils.toGraphQLRequestParams( - "{\"variables\":\"r2d2\",\"query\":\"query { droid { id }}\"}".getBytes(StandardCharsets.UTF_8), - null); - fail("Should have failed due to invalid graph variables type."); - } catch (IllegalArgumentException ignore) { - } + @Test + void testInvalidGraphVariableType() { + assertThrows(IllegalArgumentException.class, + () -> GraphQLRequestParamUtils.toGraphQLRequestParams( + "{\"variables\":\"r2d2\",\"query\":\"query { droid { id }}\"}".getBytes(StandardCharsets.UTF_8), + null)); } @Test @@ -184,29 +181,29 @@ public class TestGraphQLRequestParamUtils { assertNull(params.getOperationName()); assertEquals("query { droid { id }}", params.getQuery()); assertNull(params.getVariables()); + } + + @Test + void testMissingParams() throws UnsupportedEncodingException { + Arguments args = new Arguments(); + assertThrows(IllegalArgumentException.class, + () -> GraphQLRequestParamUtils.toGraphQLRequestParams(args, null)); + } + + @Test + void testInvalidQueryParam() throws UnsupportedEncodingException { + Arguments args = new Arguments(); + args.addArgument(new HTTPArgument("query", "select * from emp", "=", false)); + assertThrows(IllegalArgumentException.class, + () -> GraphQLRequestParamUtils.toGraphQLRequestParams(args, null)); + } - try { - args = new Arguments(); - params = GraphQLRequestParamUtils.toGraphQLRequestParams(args, null); - fail("Should have failed due to missing GraphQL parameters."); - } catch (IllegalArgumentException ignore) { - } - - try { - args = new Arguments(); - args.addArgument(new HTTPArgument("query", "select * from emp", "=", false)); - params = GraphQLRequestParamUtils.toGraphQLRequestParams(args, null); - fail("Should have failed due to invalid graph query param."); - } catch (IllegalArgumentException ignore) { - } - - try { - args = new Arguments(); - args.addArgument(new HTTPArgument("query", "query { droid { id }}", "=", false)); - args.addArgument(new HTTPArgument("variables", "r2d2", "=", false)); - params = GraphQLRequestParamUtils.toGraphQLRequestParams(args, null); - fail("Should have failed due to invalid graph query param."); - } catch (IllegalArgumentException ignore) { - } + @Test + void testInvalidQueryParamVariables() throws UnsupportedEncodingException { + Arguments args = new Arguments(); + args.addArgument(new HTTPArgument("query", "query { droid { id }}", "=", false)); + args.addArgument(new HTTPArgument("variables", "r2d2", "=", false)); + assertThrows(IllegalArgumentException.class, + () -> GraphQLRequestParamUtils.toGraphQLRequestParams(args, null)); } }
