<commit_before>package org.dcsc.unit.event;

import org.dcsc.event.*;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.sql.Date;
import java.sql.Time;
import java.util.List;
import java.util.Optional;

/**
 * Created by tktong on 7/7/2015.
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({EventService.class, Date.class, Time.class})
public class EventServiceTest {
    private static final long ID = 0;
    private static final String TAG = "tag";
    private static final String NAME = "name";
    private static final String DESCRIPTION = "description";
    private static final String LOCATION = "location";
    private static final boolean IS_PUBLISHED = true;
    private static final String STRING_DATE = "1111-11-11";
    private static final String STRING_START_TIME = "00:00:00";
    private static final String STRING_END_TIME = "00:00:00";

    @Mock
    private EventRepository eventRepository;

    @InjectMocks
    private EventService eventService;

    @Mock
    private List<Event> expectedEventList;
    @Mock
    private Optional<Event> expectedEventOptional;
    @Mock
    private Event event;
    @Mock
    private EventForm eventForm;
    @Mock
    private PageRequest pageRequest;
    @Mock
    private Page page;
    @Mock
    private Date date;
    @Mock
    private Time startTime;
    @Mock
    private Time endTime;

    @Test(expected = SavingUndefinedEventException.class)
    public void saveEventWithInvalidId() throws SavingUndefinedEventException {
        Mockito.when(eventRepository.findEventById(ID)).thenReturn(expectedEventOptional);
        Mockito.when(expectedEventOptional.isPresent()).thenReturn(false);

        eventService.saveEvent(ID, eventForm);
    }

    @Test
    public void saveEventWithValidId() throws Exception {
        PowerMockito.mockStatic(Date.class);
        PowerMockito.mockStatic(Time.class);

        Mockito.when(eventRepository.findEventById(ID)).thenReturn(expectedEventOptional);
        Mockito.when(expectedEventOptional.isPresent()).thenReturn(true);
        Mockito.when(expectedEventOptional.get()).thenReturn(event);

        Mockito.when(eventForm.getName()).thenReturn(NAME);
        Mockito.when(eventForm.getDescription()).thenReturn(DESCRIPTION);
        Mockito.when(eventForm.getDate()).thenReturn(STRING_DATE);
        Mockito.when(Date.valueOf(STRING_DATE)).thenReturn(date);

        Mockito.when(eventForm.getStartTime()).thenReturn(STRING_START_TIME);
        Mockito.when(Time.valueOf(Mockito.anyString())).thenReturn(startTime);

        Mockito.when(eventForm.getEndTime()).thenReturn(STRING_END_TIME);
        Mockito.when(Time.valueOf(Mockito.anyString())).thenReturn(endTime);

        Mockito.when(eventForm.getLocation()).thenReturn(LOCATION);
        Mockito.when(eventForm.isPublished()).thenReturn(IS_PUBLISHED);
        Mockito.when(eventRepository.save(event)).thenReturn(event);

        Event actualEvent = eventService.saveEvent(ID, eventForm);

        Mockito.verify(event).setName(NAME);
        Mockito.verify(event).setDescription(DESCRIPTION);
        Mockito.verify(event).setDate(date);
        //Mockito.verify(event).setStartTime(startTime);
        //Mockito.verify(event).setEndTime(endTime);
        Mockito.verify(event).setLocation(LOCATION);
        Mockito.verify(event).setPublished(IS_PUBLISHED);

        Assert.assertEquals(event, actualEvent);
    }

    @Test
    public void saveEventWithFormOnly () throws Exception {
        PowerMockito.mockStatic(Date.class);
        PowerMockito.mockStatic(Time.class);

        PowerMockito.whenNew(Event.class).withNoArguments().thenReturn(event);

        Mockito.when(eventForm.getName()).thenReturn(NAME);
        Mockito.when(eventForm.getDescription()).thenReturn(DESCRIPTION);
        Mockito.when(eventForm.getDate()).thenReturn(STRING_DATE);
        Mockito.when(Date.valueOf(STRING_DATE)).thenReturn(date);
        Mockito.when(eventForm.getStartTime()).thenReturn(STRING_START_TIME);
        Mockito.when(Time.valueOf(STRING_START_TIME)).thenReturn(startTime);
        Mockito.when(eventForm.getEndTime()).thenReturn(STRING_END_TIME);
        Mockito.when(Time.valueOf(STRING_END_TIME)).thenReturn(endTime);
        Mockito.when(eventForm.getLocation()).thenReturn(LOCATION);
        Mockito.when(eventForm.isPublished()).thenReturn(IS_PUBLISHED);
        Mockito.when(eventRepository.save(event)).thenReturn(event);

        Event actualEvent = eventService.saveEvent(eventForm);

        Mockito.verify(event).setName(NAME);
        Mockito.verify(event).setDescription(DESCRIPTION);
        Mockito.verify(event).setDate(date);
        //Mockito.verify(event).setStartTime(startTime);
        //Mockito.verify(event).setEndTime(endTime);
        Mockito.verify(event).setLocation(LOCATION);
        Mockito.verify(event).setPublished(IS_PUBLISHED);

        Assert.assertEquals(event, actualEvent);
    }

    @Test
    public void getAllEvents() {
        Mockito.when(eventRepository.findAll()).thenReturn(expectedEventList);

        Assert.assertEquals(expectedEventList, eventService.getAllEvents());
    }<commit_msg>Issue #34. Updated EventService Test. Fixing build #48.<commit_after>package org.dcsc.unit.event;

import org.dcsc.activity.Actions;
import org.dcsc.activity.ActivityService;
import org.dcsc.event.*;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.sql.Date;
import java.sql.Time;
import java.util.List;
import java.util.Optional;

/**
 * Created by tktong on 7/7/2015.
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({EventService.class, Date.class, Time.class})
public class EventServiceTest {
    private static final long ID = 0;
    private static final String TAG = "tag";
    private static final String NAME = "name";
    private static final String DESCRIPTION = "description";
    private static final String LOCATION = "location";
    private static final boolean IS_PUBLISHED = true;
    private static final String STRING_DATE = "1111-11-11";
    private static final String STRING_START_TIME = "00:00:00";
    private static final String STRING_END_TIME = "00:00:00";

    @Mock
    private EventRepository eventRepository;
    @Mock
    private ActivityService activityService;

    @InjectMocks
    private EventService eventService;

    @Mock
    private List<Event> expectedEventList;
    @Mock
    private Optional<Event> expectedEventOptional;
    @Mock
    private Event event;
    @Mock
    private EventForm eventForm;
    @Mock
    private PageRequest pageRequest;
    @Mock
    private Page page;
    @Mock
    private Date date;
    @Mock
    private Time startTime;
    @Mock
    private Time endTime;

    @Test(expected = SavingUndefinedEventException.class)
    public void saveEventWithInvalidId() throws SavingUndefinedEventException {
        Mockito.when(eventRepository.findEventById(ID)).thenReturn(expectedEventOptional);
        Mockito.when(expectedEventOptional.isPresent()).thenReturn(false);

        eventService.saveEvent(ID, eventForm);
    }

    @Test
    public void saveEventWithValidId() throws Exception {
        PowerMockito.mockStatic(Date.class);
        PowerMockito.mockStatic(Time.class);

        Mockito.when(eventRepository.findEventById(ID)).thenReturn(expectedEventOptional);
        Mockito.when(expectedEventOptional.isPresent()).thenReturn(true);
        Mockito.when(expectedEventOptional.get()).thenReturn(event);

        Mockito.when(eventForm.getName()).thenReturn(NAME);
        Mockito.when(eventForm.getDescription()).thenReturn(DESCRIPTION);
        Mockito.when(eventForm.getDate()).thenReturn(STRING_DATE);
        Mockito.when(Date.valueOf(STRING_DATE)).thenReturn(date);

        Mockito.when(eventForm.getStartTime()).thenReturn(STRING_START_TIME);
        Mockito.when(Time.valueOf(Mockito.anyString())).thenReturn(startTime);

        Mockito.when(eventForm.getEndTime()).thenReturn(STRING_END_TIME);
        Mockito.when(Time.valueOf(Mockito.anyString())).thenReturn(endTime);

        Mockito.when(eventForm.getLocation()).thenReturn(LOCATION);
        Mockito.when(eventForm.isPublished()).thenReturn(IS_PUBLISHED);
        Mockito.when(eventRepository.save(event)).thenReturn(event);

        Event actualEvent = eventService.saveEvent(ID, eventForm);

        Mockito.verify(event).setName(NAME);
        Mockito.verify(event).setDescription(DESCRIPTION);
        Mockito.verify(event).setDate(date);
        Mockito.verify(event).setStartTime(Mockito.any(Time.class));
        Mockito.verify(event).setEndTime(Mockito.any(Time.class));
        Mockito.verify(event).setLocation(LOCATION);
        Mockito.verify(event).setPublished(IS_PUBLISHED);

        Mockito.verify(activityService).save(Mockito.anyString(), Mockito.anyString(), Mockito.eq(Actions.UPDATE));

        Assert.assertEquals(event, actualEvent);
    }

    @Test
    public void saveEventWithFormOnly () throws Exception {
        PowerMockito.mockStatic(Date.class);
        PowerMockito.mockStatic(Time.class);

        PowerMockito.whenNew(Event.class).withNoArguments().thenReturn(event);

        Mockito.when(eventForm.getName()).thenReturn(NAME);
        Mockito.when(eventForm.getDescription()).thenReturn(DESCRIPTION);
        Mockito.when(eventForm.getDate()).thenReturn(STRING_DATE);
        Mockito.when(Date.valueOf(STRING_DATE)).thenReturn(date);
        Mockito.when(eventForm.getStartTime()).thenReturn(STRING_START_TIME);
        Mockito.when(Time.valueOf(STRING_START_TIME)).thenReturn(startTime);
        Mockito.when(eventForm.getEndTime()).thenReturn(STRING_END_TIME);
        Mockito.when(Time.valueOf(STRING_END_TIME)).thenReturn(endTime);
        Mockito.when(eventForm.getLocation()).thenReturn(LOCATION);
        Mockito.when(eventForm.isPublished()).thenReturn(IS_PUBLISHED);
        Mockito.when(eventRepository.save(event)).thenReturn(event);

        Event actualEvent = eventService.saveEvent(eventForm);

        Mockito.verify(event).setName(NAME);
        Mockito.verify(event).setDescription(DESCRIPTION);
        Mockito.verify(event).setDate(date);
        Mockito.verify(event).setStartTime(Mockito.any(Time.class));
        Mockito.verify(event).setEndTime(Mockito.any(Time.class));
        Mockito.verify(event).setLocation(LOCATION);
        Mockito.verify(event).setPublished(IS_PUBLISHED);

        Assert.assertEquals(event, actualEvent);
    }

    @Test
    public void getAllEvents() {
        Mockito.when(eventRepository.findAll()).thenReturn(expectedEventList);

        Assert.assertEquals(expectedEventList, eventService.getAllEvents());
    }
{'fraction_non_alphanumeric': 0.11602306482614014, 'fraction_numerical': 0.005242005940940066, 'mean_word_length': 4.163283716734326, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3398416', 'n_tokens_mistral': 3374, 'n_tokens_neox': 3248, 'n_words': 480}