Skip to content

Commit

Permalink
SONAR-10881 Log flooding when a WS param is not an integer as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Brandhof authored and SonarTech committed Jun 14, 2018
1 parent a2f45d5 commit 11b5185
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
Expand Up @@ -216,7 +216,7 @@ private static int validateAsNumeric(String key, String value) {
try { try {
return Integer.parseInt(value); return Integer.parseInt(value);
} catch (NumberFormatException exception) { } catch (NumberFormatException exception) {
throw new IllegalStateException(format("'%s' value '%s' cannot be parsed as an integer", key, value), exception); throw new IllegalArgumentException(format("'%s' value '%s' cannot be parsed as an integer", key, value), exception);
} }
} }


Expand Down
Expand Up @@ -60,7 +60,7 @@ public class RequestTest {
@Rule @Rule
public ExpectedException expectedException = ExpectedException.none(); public ExpectedException expectedException = ExpectedException.none();


FakeRequest underTest = new FakeRequest(); private FakeRequest underTest = new FakeRequest();


@Before @Before
public void before() { public void before() {
Expand Down Expand Up @@ -144,6 +144,17 @@ public void maximum_value_not_ok() {
underTest.setParam(param, "11").param(param); underTest.setParam(param, "11").param(param);
} }


@Test
public void paramAsInt_throws_IAE_if_maximum_defined_and_value_not_a_number() {
String param = "maximum_value_param";
defineParameterTestAction(newParam -> newParam.setMaximumValue(10), param);

expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("'maximum_value_param' value 'foo' cannot be parsed as an integer");

underTest.setParam(param, "foo").paramAsInt(param);
}

@Test @Test
public void required_param_as_strings() { public void required_param_as_strings() {
underTest.setParam("a_required_string", "foo,bar"); underTest.setParam("a_required_string", "foo,bar");
Expand Down Expand Up @@ -214,7 +225,7 @@ public void null_param() {
} }


@Test @Test
public void param_as_int() { public void paramAsInt() {
assertThat(underTest.setParam("a_number", "123").paramAsInt("a_number")).isEqualTo(123); assertThat(underTest.setParam("a_number", "123").paramAsInt("a_number")).isEqualTo(123);
assertThat(underTest.setParam("a_number", "123").paramAsInt("a_number", 42)).isEqualTo(123); assertThat(underTest.setParam("a_number", "123").paramAsInt("a_number", 42)).isEqualTo(123);
assertThat(underTest.setParam("a_number", null).paramAsInt("a_number", 42)).isEqualTo(123); assertThat(underTest.setParam("a_number", null).paramAsInt("a_number", 42)).isEqualTo(123);
Expand Down Expand Up @@ -609,7 +620,7 @@ public void param_as_input_stream() throws Exception {
} }


@Test @Test
public void param_as_part() throws Exception { public void param_as_part() {
InputStream inputStream = mock(InputStream.class); InputStream inputStream = mock(InputStream.class);
underTest.setPart("key", inputStream, "filename"); underTest.setPart("key", inputStream, "filename");


Expand All @@ -621,7 +632,7 @@ public void param_as_part() throws Exception {
} }


@Test @Test
public void mandatory_param_as_part() throws Exception { public void mandatory_param_as_part() {
expectedException.expect(IllegalArgumentException.class); expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("The 'required_param' parameter is missing"); expectedException.expectMessage("The 'required_param' parameter is missing");


Expand Down
Expand Up @@ -50,7 +50,7 @@ public void has_param() {
} }


@Test @Test
public void get_part() throws Exception { public void get_part() {
InputStream inputStream = mock(InputStream.class); InputStream inputStream = mock(InputStream.class);
underTest.setPart("key", inputStream, "filename"); underTest.setPart("key", inputStream, "filename");


Expand Down

0 comments on commit 11b5185

Please sign in to comment.