<commit_before>// Copyright 2019 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
//
//     https://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.google.sps;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.sps.servlets.SurveyServlet;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

/** Tests for the SurveyServlet.java class */
@RunWith(PowerMockRunner.class)
@PrepareForTest(UserServiceFactory.class)
public final class SurveyServletTest {
  private final LocalServiceTestHelper helper =
      new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
  private SurveyServlet testSurveyServlet;

  @Before
  public void setUp() throws IOException {
    helper.setUp();
    testSurveyServlet = new SurveyServlet();
    TestingUtil.setUp();
  }

  @After
  public void tearDown() {
    helper.tearDown();
    TestingUtil.tearDown();
  }

  @Test
  public void submitSurvey() throws IOException {
    // basic test to make sure all parameters have been posted to datastore

    String email = "<EMAIL>";
    TestingUtil.toggleLogin(email);

    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getParameter("environment")).thenReturn("3");
    when(request.getParameter("blm")).thenReturn("4");
    when(request.getParameter("volunteer")).thenReturn("3");
    when(request.getParameter("education")).thenReturn("2");
    when(request.getParameter("LGBTQ+")).thenReturn("4");

    testSurveyServlet.doPost(request, response);

    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Entity userEntity = ds.prepare(new Query("User")).asSingleEntity();
    assertEquals("3", userEntity.getProperty("environment"));
    assertEquals("4", userEntity.getProperty("blm"));
    assertEquals("3", userEntity.getProperty("volunteer"));
    assertEquals("2", userEntity.getProperty("education"));
    assertEquals("4", userEntity.getProperty("LGBTQ+"));
  }

  @Test
  public void submitIncomplete() throws IOException {
    // try to submit a survey with incomplete fields
    String email = "<EMAIL>";
    TestingUtil.toggleLogin(email);

    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getParameter("environment")).thenReturn("3");
    when(request.getParameter("blm")).thenReturn("4");
    when(request.getParameter("volunteer")).thenReturn("3");
    when(request.getParameter("education")).thenReturn("2");

    try {
      testSurveyServlet.doPost(request, response);

      fail();
    } catch (IOException expected) {

      // make sure nothing has been posted
      DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
      Entity userEntity = ds.prepare(new Query("User")).asSingleEntity();

      assertEquals(false, userEntity.hasProperty("environment"));
      assertEquals(false, userEntity.hasProperty("blm"));
      assertEquals(false, userEntity.hasProperty("volunteer"));
      assertEquals(false, userEntity.hasProperty("education"));
      assertEquals(false, userEntity.hasProperty("LGBTQ+"));
    }
  }

  @Test
  public void submitWithoutLogin() throws IOException {
    // try to submit a survey without being logged in

    String email = "<EMAIL>";
    // login then log back out
    TestingUtil.toggleLogin(email);
    TestingUtil.toggleLogin(email);

    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getParameter("environment")).thenReturn("3");
    when(request.getParameter("blm")).thenReturn("4");
    when(request.getParameter("volunteer")).thenReturn("3");
    when(request.getParameter("education")).thenReturn("2");
    when(request.getParameter("LGBTQ+")).thenReturn("4");

    try {
      testSurveyServlet.doPost(request, response);

      fail();
    } catch (IOException expected) {

      // make sure nothing has been posted
      DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
      Entity userEntity = ds.prepare(new Query("User")).asSingleEntity();

      assertEquals(false, userEntity.hasProperty("environment"));
      assertEquals(false, userEntity.hasProperty("blm"));
      assertEquals(false, userEntity.hasProperty("volunteer"));
      assertEquals(false, userEntity.hasProperty("education"));
      assertEquals(false, userEntity.hasProperty("LGBTQ+"));
    }
  }
}<commit_msg>Make sure build fails if test fails<commit_after>// Copyright 2019 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
//
//     https://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.google.sps;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.sps.servlets.SurveyServlet;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

/** Tests for the SurveyServlet.java class */
@RunWith(PowerMockRunner.class)
@PrepareForTest(UserServiceFactory.class)
public final class SurveyServletTest {
  private final LocalServiceTestHelper helper =
      new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
  private SurveyServlet testSurveyServlet;

  @Before
  public void setUp() throws IOException {
    helper.setUp();
    testSurveyServlet = new SurveyServlet();
    TestingUtil.setUp();
  }

  @After
  public void tearDown() {
    helper.tearDown();
    TestingUtil.tearDown();
  }

  @Test
  public void submitSurvey() throws IOException {
    // basic test to make sure all parameters have been posted to datastore

    String email = "<EMAIL>";
    TestingUtil.toggleLogin(email);

    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getParameter("environment")).thenReturn("3");
    when(request.getParameter("blm")).thenReturn("4");
    when(request.getParameter("volunteer")).thenReturn("3");
    when(request.getParameter("education")).thenReturn("2");
    when(request.getParameter("LGBTQ+")).thenReturn("4");

    testSurveyServlet.doPost(request, response);

    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Entity userEntity = ds.prepare(new Query("User")).asSingleEntity();
    assertEquals("3", userEntity.getProperty("environment"));
    assertEquals("4", userEntity.getProperty("blm"));
    assertEquals("3", userEntity.getProperty("volunteer"));
    assertEquals("2", userEntity.getProperty("education"));
    assertEquals("4", userEntity.getProperty("LGBTQ+"));
  }

  @Test
  public void submitIncomplete() throws IOException {
    // try to submit a survey with incomplete fields
    String email = "<EMAIL>";
    TestingUtil.toggleLogin(email);

    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getParameter("environment")).thenReturn("3");
    when(request.getParameter("blm")).thenReturn("4");
    when(request.getParameter("volunteer")).thenReturn("3");
    when(request.getParameter("education")).thenReturn("2");

    try {
      testSurveyServlet.doPost(request, response);

      fail();
    } catch (IOException expected) {

      // make sure nothing has been posted
      DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
      Entity userEntity = ds.prepare(new Query("User")).asSingleEntity();

      assertEquals(false, userEntity.hasProperty("environment"));
      assertEquals(false, userEntity.hasProperty("blm"));
      assertEquals(false, userEntity.hasProperty("volunteer"));
      assertEquals(false, userEntity.hasProperty("education"));
      assertEquals(false, userEntity.hasProperty("LGBTQ+"));
    }
  }

  @Test
  public void submitWithoutLogin() throws IOException {
    // try to submit a survey without being logged in

    String email = "<EMAIL>";
    // login then log back out
    TestingUtil.toggleLogin(email);
    TestingUtil.toggleLogin(email);

    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getParameter("environment")).thenReturn("3");
    when(request.getParameter("blm")).thenReturn("4");
    when(request.getParameter("volunteer")).thenReturn("3");
    when(request.getParameter("education")).thenReturn("2");
    when(request.getParameter("LGBTQ+")).thenReturn("4");

    try {
      testSurveyServlet.doPost(request, response);

      fail();
    } catch (IOException expected) {

      // make sure nothing has been posted
      DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
      Entity userEntity = ds.prepare(new Query("User")).asSingleEntity();

      assertEquals(false, userEntity.hasProperty("environment"));
      assertEquals(false, userEntity.hasProperty("blm"));
      assertEquals(false, userEntity.hasProperty("volunteer"));
      assertEquals(false, userEntity.hasProperty("education"));
      assertEquals(false, userEntity.hasProperty("LGBTQ+"));
    }
  }

  @Test
  public void wantToFail() {
    assertEquals(1, 0);
  }
}
{'fraction_non_alphanumeric': 0.1228595890410959, 'fraction_numerical': 0.004965753424657535, 'mean_word_length': 4.834665334665335, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6032344', 'n_tokens_mistral': 3211, 'n_tokens_neox': 3153, 'n_words': 696}