Repository navigation
fix(cloud-sql): postgres servlet reported a failed vote as success #10382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f2f95b2
02fce1e
445978c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Copyright 2024 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.cloudsql; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static org.mockito.ArgumentMatchers.anyInt; | ||
| import static org.mockito.Mockito.doAnswer; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.io.PrintWriter; | ||
| import java.io.StringWriter; | ||
| import java.sql.SQLException; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import javax.servlet.ServletContext; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import javax.sql.DataSource; | ||
| import org.junit.Test; | ||
|
|
||
| /** Covers the doPost error path, which does not need a database. */ | ||
| public class TestIndexServletMysqlErrorResponse { | ||
|
|
||
| @Test | ||
| public void testFailedVoteIsNotReportedAsSuccess() throws Exception { | ||
| DataSource pool = mock(DataSource.class); | ||
| when(pool.getConnection()).thenThrow(new SQLException("connection refused")); | ||
|
|
||
| ServletContext context = mock(ServletContext.class); | ||
| when(context.getAttribute("my-pool")).thenReturn(pool); | ||
|
|
||
| HttpServletRequest request = mock(HttpServletRequest.class); | ||
| when(request.getServletContext()).thenReturn(context); | ||
| when(request.getParameter("team")).thenReturn("TABS"); | ||
|
|
||
| StringWriter body = new StringWriter(); | ||
| PrintWriter writer = new PrintWriter(body); | ||
| AtomicInteger status = new AtomicInteger(); | ||
| HttpServletResponse response = mock(HttpServletResponse.class); | ||
| when(response.getWriter()).thenReturn(writer); | ||
| doAnswer( | ||
| invocation -> { | ||
| status.set(invocation.getArgument(0)); | ||
| return null; | ||
| }) | ||
| .when(response) | ||
| .setStatus(anyInt()); | ||
|
|
||
| new IndexServlet().doPost(request, response); | ||
| writer.flush(); | ||
|
|
||
| assertThat(status.get()).isEqualTo(500); | ||
| assertThat(body.toString()).contains("Unable to successfully cast vote"); | ||
| assertThat(body.toString()).doesNotContain("Vote successfully cast"); | ||
|
Comment on lines
+52
to
+68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplify the verification of the HTTP status code by using Mockito's HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getWriter()).thenReturn(writer);
new IndexServlet().doPost(request, response);
writer.flush();
verify(response).setStatus(500);
assertThat(body.toString()).contains("Unable to successfully cast vote");
assertThat(body.toString()).doesNotContain("Vote successfully cast"); |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,70 @@ | ||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||
| * Copyright 2024 Google LLC | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||||||||
| * you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||||||||
| * You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||||||||
| * See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||||||
| * limitations under the License. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| package com.example.cloudsql; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import static com.google.common.truth.Truth.assertThat; | ||||||||||||||||||||||||||||||||||
| import static org.mockito.ArgumentMatchers.anyInt; | ||||||||||||||||||||||||||||||||||
| import static org.mockito.Mockito.doAnswer; | ||||||||||||||||||||||||||||||||||
| import static org.mockito.Mockito.mock; | ||||||||||||||||||||||||||||||||||
| import static org.mockito.Mockito.when; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import java.io.PrintWriter; | ||||||||||||||||||||||||||||||||||
| import java.io.StringWriter; | ||||||||||||||||||||||||||||||||||
| import java.sql.SQLException; | ||||||||||||||||||||||||||||||||||
| import java.util.concurrent.atomic.AtomicInteger; | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+20
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unused imports (
Suggested change
|
||||||||||||||||||||||||||||||||||
| import javax.servlet.ServletContext; | ||||||||||||||||||||||||||||||||||
| import javax.servlet.http.HttpServletRequest; | ||||||||||||||||||||||||||||||||||
| import javax.servlet.http.HttpServletResponse; | ||||||||||||||||||||||||||||||||||
| import javax.sql.DataSource; | ||||||||||||||||||||||||||||||||||
| import org.junit.Test; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** Covers the doPost error path, which does not need a database. */ | ||||||||||||||||||||||||||||||||||
| public class TestIndexServletPostgresErrorResponse { | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @Test | ||||||||||||||||||||||||||||||||||
| public void testFailedVoteIsNotReportedAsSuccess() throws Exception { | ||||||||||||||||||||||||||||||||||
| DataSource pool = mock(DataSource.class); | ||||||||||||||||||||||||||||||||||
| when(pool.getConnection()).thenThrow(new SQLException("connection refused")); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ServletContext context = mock(ServletContext.class); | ||||||||||||||||||||||||||||||||||
| when(context.getAttribute("my-pool")).thenReturn(pool); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| HttpServletRequest request = mock(HttpServletRequest.class); | ||||||||||||||||||||||||||||||||||
| when(request.getServletContext()).thenReturn(context); | ||||||||||||||||||||||||||||||||||
| when(request.getParameter("team")).thenReturn("TABS"); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| StringWriter body = new StringWriter(); | ||||||||||||||||||||||||||||||||||
| PrintWriter writer = new PrintWriter(body); | ||||||||||||||||||||||||||||||||||
| AtomicInteger status = new AtomicInteger(); | ||||||||||||||||||||||||||||||||||
| HttpServletResponse response = mock(HttpServletResponse.class); | ||||||||||||||||||||||||||||||||||
| when(response.getWriter()).thenReturn(writer); | ||||||||||||||||||||||||||||||||||
| doAnswer( | ||||||||||||||||||||||||||||||||||
| invocation -> { | ||||||||||||||||||||||||||||||||||
| status.set(invocation.getArgument(0)); | ||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
| .when(response) | ||||||||||||||||||||||||||||||||||
| .setStatus(anyInt()); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| new IndexServlet().doPost(request, response); | ||||||||||||||||||||||||||||||||||
| writer.flush(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| assertThat(status.get()).isEqualTo(500); | ||||||||||||||||||||||||||||||||||
| assertThat(body.toString()).contains("Unable to successfully cast vote"); | ||||||||||||||||||||||||||||||||||
| assertThat(body.toString()).doesNotContain("Vote successfully cast"); | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+52
to
+68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplify the verification of the HTTP status code by using Mockito's HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getWriter()).thenReturn(writer);
new IndexServlet().doPost(request, response);
writer.flush();
verify(response).setStatus(500);
assertThat(body.toString()).contains("Unable to successfully cast vote");
assertThat(body.toString()).doesNotContain("Vote successfully cast"); |
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,70 @@ | ||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||
| * Copyright 2024 Google LLC | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||||||||
| * you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||||||||
| * You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||||||||
| * See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||||||
| * limitations under the License. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| package com.example.cloudsql; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import static com.google.common.truth.Truth.assertThat; | ||||||||||||||||||||||||||||||||||
| import static org.mockito.ArgumentMatchers.anyInt; | ||||||||||||||||||||||||||||||||||
| import static org.mockito.Mockito.doAnswer; | ||||||||||||||||||||||||||||||||||
| import static org.mockito.Mockito.mock; | ||||||||||||||||||||||||||||||||||
| import static org.mockito.Mockito.when; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import java.io.PrintWriter; | ||||||||||||||||||||||||||||||||||
| import java.io.StringWriter; | ||||||||||||||||||||||||||||||||||
| import java.sql.SQLException; | ||||||||||||||||||||||||||||||||||
| import java.util.concurrent.atomic.AtomicInteger; | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+20
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unused imports (
Suggested change
|
||||||||||||||||||||||||||||||||||
| import javax.servlet.ServletContext; | ||||||||||||||||||||||||||||||||||
| import javax.servlet.http.HttpServletRequest; | ||||||||||||||||||||||||||||||||||
| import javax.servlet.http.HttpServletResponse; | ||||||||||||||||||||||||||||||||||
| import javax.sql.DataSource; | ||||||||||||||||||||||||||||||||||
| import org.junit.Test; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** Covers the doPost error path, which does not need a database. */ | ||||||||||||||||||||||||||||||||||
| public class TestIndexServletSqlServerErrorResponse { | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @Test | ||||||||||||||||||||||||||||||||||
| public void testFailedVoteIsNotReportedAsSuccess() throws Exception { | ||||||||||||||||||||||||||||||||||
| DataSource pool = mock(DataSource.class); | ||||||||||||||||||||||||||||||||||
| when(pool.getConnection()).thenThrow(new SQLException("connection refused")); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ServletContext context = mock(ServletContext.class); | ||||||||||||||||||||||||||||||||||
| when(context.getAttribute("my-pool")).thenReturn(pool); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| HttpServletRequest request = mock(HttpServletRequest.class); | ||||||||||||||||||||||||||||||||||
| when(request.getServletContext()).thenReturn(context); | ||||||||||||||||||||||||||||||||||
| when(request.getParameter("team")).thenReturn("TABS"); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| StringWriter body = new StringWriter(); | ||||||||||||||||||||||||||||||||||
| PrintWriter writer = new PrintWriter(body); | ||||||||||||||||||||||||||||||||||
| AtomicInteger status = new AtomicInteger(); | ||||||||||||||||||||||||||||||||||
| HttpServletResponse response = mock(HttpServletResponse.class); | ||||||||||||||||||||||||||||||||||
| when(response.getWriter()).thenReturn(writer); | ||||||||||||||||||||||||||||||||||
| doAnswer( | ||||||||||||||||||||||||||||||||||
| invocation -> { | ||||||||||||||||||||||||||||||||||
| status.set(invocation.getArgument(0)); | ||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
| .when(response) | ||||||||||||||||||||||||||||||||||
| .setStatus(anyInt()); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| new IndexServlet().doPost(request, response); | ||||||||||||||||||||||||||||||||||
| writer.flush(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| assertThat(status.get()).isEqualTo(500); | ||||||||||||||||||||||||||||||||||
| assertThat(body.toString()).contains("Unable to successfully cast vote"); | ||||||||||||||||||||||||||||||||||
| assertThat(body.toString()).doesNotContain("Vote successfully cast"); | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+52
to
+68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplify the verification of the HTTP status code by using Mockito's HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getWriter()).thenReturn(writer);
new IndexServlet().doPost(request, response);
writer.flush();
verify(response).setStatus(500);
assertThat(body.toString()).contains("Unable to successfully cast vote");
assertThat(body.toString()).doesNotContain("Vote successfully cast"); |
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unused imports (
anyInt,doAnswer,AtomicInteger) and statically importverifyto simplify the Mockito verification in the test.