Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx
.write(
"Unable to successfully cast vote! Please check the application "
+ "logs for more details.");
// Without this return the success response below overwrites the 500 and the error text.
return;
// [END_EXCLUDE]
}
// [END cloud_sql_mysql_servlet_connection]
Expand Down
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove unused imports (anyInt, doAnswer, AtomicInteger) and statically import verify to simplify the Mockito verification in the test.

Suggested change
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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.SQLException;

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Simplify the verification of the HTTP status code by using Mockito's verify instead of stubbing setStatus with doAnswer and tracking it with an AtomicInteger. This makes the test cleaner and more idiomatic.

    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
Expand Up @@ -123,6 +123,8 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx
.write(
"Unable to successfully cast vote! Please check the application "
+ "logs for more details.");
// Without this return the success response below overwrites the 500 and the error text.
return;
// [END_EXCLUDE]
}
// [END cloud_sql_postgres_servlet_connection]
Expand Down
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove unused imports (anyInt, doAnswer, AtomicInteger) and statically import verify to simplify the Mockito verification in the test.

Suggested change
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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.SQLException;

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Simplify the verification of the HTTP status code by using Mockito's verify instead of stubbing setStatus with doAnswer and tracking it with an AtomicInteger. This makes the test cleaner and more idiomatic.

    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
Expand Up @@ -97,6 +97,8 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp)
resp.setStatus(500);
resp.getWriter().write("Unable to successfully cast vote! Please check the application "
+ "logs for more details.");
// Without this return the success response below overwrites the 500 and the error text.
return;
// [END_EXCLUDE]
}
// [END cloud_sql_sqlserver_servlet_connection]
Expand Down
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove unused imports (anyInt, doAnswer, AtomicInteger) and statically import verify to simplify the Mockito verification in the test.

Suggested change
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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.SQLException;

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Simplify the verification of the HTTP status code by using Mockito's verify instead of stubbing setStatus with doAnswer and tracking it with an AtomicInteger. This makes the test cleaner and more idiomatic.

    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");

}
}
Loading