<commit_before>package com.google.step.coffee.data;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.Calendar.Events;
import com.google.api.services.calendar.Calendar.Events.Insert;
import com.google.api.services.calendar.model.Event;
import com.google.appengine.api.users.User;
import com.google.step.coffee.TestHelper;
import com.google.step.coffee.entity.TimeSlot;
import java.io.IOException;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;

public class CalendarUtilsTest extends TestHelper {

  private final String testUser1Id = "1111";
  private final String testUser2Id = "2222";
  private final String testUser3Id = "3333";

  @Test
  public void eventDescriptionContainsCommonTags() {
    final TimeSlot timeslot = new TimeSlot(ZonedDateTime.now(), Duration.ofMinutes(30));
    final List<String> participantIds = new ArrayList<>();
    final List<String> commonTags = new ArrayList<>();

    setUpEventInfo(participantIds, commonTags);
    addTestUsersInfo();

    Event event = CalendarUtils.createEvent(timeslot, participantIds, commonTags);

    assertThat(event.getDescription(), containsString("Football"));
    assertThat(event.getDescription(), containsString("Photography"));
  }

  @Test
  public void addingEventAddsOnceOnSuccess() throws IOException {
    Event event = mock(Event.class);
    Calendar service = mock(Calendar.class);
    Events events = mock(Events.class);
    Insert insertReturn = mock(Insert.class);

    when(service.events()).thenReturn(events);

    when(events.insert("primary", event)).thenReturn(insertReturn);

    when(insertReturn.setConferenceDataVersion(1)).thenReturn(insertReturn);

    CalendarUtils.addEvent(service, testUser1Id, event, true);

    verify(events, times(1)).insert("primary", event);
    verify(insertReturn, times(1)).execute();
  }

  @Test
  public void addingAnEventRetriesIfFails() throws IOException {
    Event event = mock(Event.class);
    Calendar service = mock(Calendar.class);
    Events events = mock(Events.class);
    Insert insertReturn = mock(Insert.class);

    when(service.events()).thenReturn(events);

    when(events.insert("primary", event))
        .thenThrow(new IOException("failed"))
        .thenReturn(insertReturn);

    when(insertReturn.setConferenceDataVersion(1)).thenReturn(insertReturn);

    CalendarUtils.addEvent(service, testUser1Id, event, true);

    verify(events, times(2)).insert("primary", event);
    verify(insertReturn, times(1)).execute();
  }

  private void setUpEventInfo(List<String> participantIds, List<String> commonTags) {
    participantIds.add(testUser1Id);
    participantIds.add(testUser2Id);
    participantIds.add(testUser3Id);

    commonTags.add("Football");
    commonTags.add("Photography");
  }

  private void addTestUsersInfo() {
    User testUser1 = mock(User.class);
    User testUser2 = mock(User.class);
    User testUser3 = mock(User.class);

    when(testUser1.getUserId()).thenReturn(testUser1Id);
    when(testUser1.getEmail()).thenReturn("<EMAIL>");
    when(testUser2.getUserId()).thenReturn(testUser2Id);
    when(testUser2.getEmail()).thenReturn("<EMAIL>");
    when(testUser3.getUserId()).thenReturn(testUser3Id);
    when(testUser3.getEmail()).thenReturn("<EMAIL>");

    UserStore userStore = new UserStore();
    userStore.addNewUser(testUser1);
    userStore.addNewUser(testUser2);
    userStore.addNewUser(testUser3);
  }
}<commit_msg>Move test setup to inside method to follow arrange, act, assert principle<commit_after>package com.google.step.coffee.data;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.Calendar.Events;
import com.google.api.services.calendar.Calendar.Events.Insert;
import com.google.api.services.calendar.model.Event;
import com.google.appengine.api.users.User;
import com.google.step.coffee.TestHelper;
import com.google.step.coffee.entity.TimeSlot;
import java.io.IOException;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;

public class CalendarUtilsTest extends TestHelper {

  private final String testUser1Id = "1111";

  @Test
  public void eventDescriptionContainsCommonTags() {
    final TimeSlot timeslot = new TimeSlot(ZonedDateTime.now(), Duration.ofMinutes(30));
    final List<String> participantIds = new ArrayList<>();
    final List<String> commonTags = new ArrayList<>();

    final String testUser2Id = "2222";
    final String testUser3Id = "3333";

    participantIds.add(testUser1Id);
    participantIds.add(testUser2Id);
    participantIds.add(testUser3Id);

    commonTags.add("Football");
    commonTags.add("Photography");

    User testUser1 = mock(User.class);
    User testUser2 = mock(User.class);
    User testUser3 = mock(User.class);

    when(testUser1.getUserId()).thenReturn(testUser1Id);
    when(testUser1.getEmail()).thenReturn("<EMAIL>");
    when(testUser2.getUserId()).thenReturn(testUser2Id);
    when(testUser2.getEmail()).thenReturn("<EMAIL>");
    when(testUser3.getUserId()).thenReturn(testUser3Id);
    when(testUser3.getEmail()).thenReturn("<EMAIL>");

    // UserStore entities required as creating event also creates attendants list using emails
    // retrieved via datastore using userId.
    UserStore userStore = new UserStore();
    userStore.addNewUser(testUser1);
    userStore.addNewUser(testUser2);
    userStore.addNewUser(testUser3);

    Event event = CalendarUtils.createEvent(timeslot, participantIds, commonTags);

    assertThat(event.getDescription(), containsString("Football"));
    assertThat(event.getDescription(), containsString("Photography"));
  }

  @Test
  public void addingEventAddsOnceOnSuccess() throws IOException {
    Event event = mock(Event.class);
    Calendar service = mock(Calendar.class);
    Events events = mock(Events.class);
    Insert insertReturn = mock(Insert.class);

    when(service.events()).thenReturn(events);

    when(events.insert("primary", event)).thenReturn(insertReturn);

    when(insertReturn.setConferenceDataVersion(1)).thenReturn(insertReturn);

    CalendarUtils.addEvent(service, testUser1Id, event, true);

    verify(events, times(1)).insert("primary", event);
    verify(insertReturn, times(1)).execute();
  }

  @Test
  public void addingAnEventRetriesIfFails() throws IOException {
    Event event = mock(Event.class);
    Calendar service = mock(Calendar.class);
    Events events = mock(Events.class);
    Insert insertReturn = mock(Insert.class);

    when(service.events()).thenReturn(events);

    when(events.insert("primary", event))
        .thenThrow(new IOException("failed"))
        .thenReturn(insertReturn);

    when(insertReturn.setConferenceDataVersion(1)).thenReturn(insertReturn);

    CalendarUtils.addEvent(service, testUser1Id, event, true);

    verify(events, times(2)).insert("primary", event);
    verify(insertReturn, times(1)).execute();
  }
}
{'fraction_non_alphanumeric': 0.1260559662090813, 'fraction_numerical': 0.011351636747624076, 'mean_word_length': 5.110483870967742, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5530290', 'n_tokens_mistral': 2271, 'n_tokens_neox': 2192, 'n_words': 412}