<commit_before>package za.org.grassroot.core.repository;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import za.org.grassroot.TestContextConfiguration;
import za.org.grassroot.core.GrassrootApplicationProfiles;
import za.org.grassroot.core.domain.*;
import za.org.grassroot.core.enums.EventLogType;
import za.org.grassroot.core.enums.EventRSVPResponse;

import javax.transaction.Transactional;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;

/**
 * @author <NAME>
 */

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = TestContextConfiguration.class)
@Transactional
@ActiveProfiles(GrassrootApplicationProfiles.INMEMORY)
public class UserRepositoryTest {

    private static final Logger log = LoggerFactory.getLogger(UserRepositoryTest.class);

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private GroupRepository groupRepository;

    @Autowired
    private EventRepository eventRepository;

    @Autowired
    private EventLogRepository eventLogRepository;

    @Autowired
    private RoleRepository roleRepository;

    private static final String number = "0821234560";

    /* Gaven below     */



    @Test
    public void shouldCheckAndroidProfile() {
        assertThat(userRepository.count(), is(0L));
        User userProfile = new User("12345");
        assertNull(userProfile.getId());
        assertNotNull(userProfile.getUid());
        userProfile.setHasAndroidProfile(true);
        userRepository.save(userProfile);

        User fromDb = userRepository.findAll().iterator().next();
        assertNotNull(fromDb.getUid());
        assertTrue(fromDb.hasAndroidProfile());
    }


    @Test
    public void checkTrialStatus() throws Exception {
        assertThat(userRepository.count(), is(0L));

        User entity = new User("07515757537");
        assertNull(entity.getId());
        assertNotNull(entity.getUid());
        userRepository.save(entity);

        assertThat(userRepository.count(), is(1L));
        User db = userRepository.findAll().iterator().next();
        assertNotNull(db.getId());
        assertThat(db.getPhoneNumber(), is("07515757537"));
        assertNotNull(db.getCreatedDateTime());
        assertFalse(db.isHasUsedFreeTrial());

    }

    @Test
    public void changeTrialStatus() throws Exception {
        assertThat(userRepository.count(), is(0L));

        User ToCreate = new User("07515757537");
        userRepository.save(ToCreate);

        User retrieve = userRepository.findAll().iterator().next();
        assertNotNull(retrieve.getId());
        assertThat(retrieve.getPhoneNumber(), is("07515757537"));
        assertFalse(retrieve.isHasUsedFreeTrial());
        retrieve.setHasUsedFreeTrial(true);
        userRepository.save(retrieve);

    }

    @Test
    public void checkOriginalTrialStatus() throws Exception {
        assertThat(userRepository.count(), is(0L));

        User dbz = new User("07515757537");
        userRepository.save(dbz);

        User check = userRepository.findAll().iterator().next();
        assertNotNull(check.getId());
        assertThat(check.getPhoneNumber(), is("07515757537"));
        assertFalse(check.isHasUsedFreeTrial());
<commit_msg>test cases for UserRepo mostly setters,getters and some collection methods<commit_after>package za.org.grassroot.core.repository;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import za.org.grassroot.TestContextConfiguration;
import za.org.grassroot.core.GrassrootApplicationProfiles;
import za.org.grassroot.core.domain.*;
import za.org.grassroot.core.enums.EventLogType;
import za.org.grassroot.core.enums.EventRSVPResponse;
import za.org.grassroot.core.enums.UserMessagingPreference;
import za.org.grassroot.core.util.PhoneNumberUtil;

import javax.transaction.Transactional;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;

/**
 * @author <NAME>
 */

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = TestContextConfiguration.class)
@Transactional
@ActiveProfiles(GrassrootApplicationProfiles.INMEMORY)
public class UserRepositoryTest {

    private static final Logger log = LoggerFactory.getLogger(UserRepositoryTest.class);

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private GroupRepository groupRepository;

    @Autowired
    private EventRepository eventRepository;

    @Autowired
    private EventLogRepository eventLogRepository;

    @Autowired
    private RoleRepository roleRepository;

    private static final String number = "0821234560";




    @Test
    public void shouldCheckAndroidProfile() {
        assertThat(userRepository.count(), is(0L));
        User userProfile = new User("12345");
        assertNull(userProfile.getId());
        assertNotNull(userProfile.getUid());
        userProfile.setHasAndroidProfile(true);
        userRepository.save(userProfile);

        User fromDb = userRepository.findAll().iterator().next();
        assertNotNull(fromDb.getUid());
        assertTrue(fromDb.hasAndroidProfile());
    }

    @Test
    public void shouldHaveSafetyGroup() {
        User newUser = new User("0111222");
        Group newGroup = new Group("test Group", newUser);
        assertNotNull(newUser.getUid());
        newUser.setSafetyGroup(newGroup);
        assertTrue(newUser.hasSafetyGroup());
        assertThat(newUser.getSafetyGroup(), is(newGroup));
        userRepository.save(newUser);
        newUser.setSafetyGroup(null);
        assertFalse(newUser.hasSafetyGroup());

    }

    @Test
    public void shouldUpdateTimeStamps() {
        User userStamp = new User("2567");
        assertNotNull(userStamp.getUid());
        Instant createdTime = Instant.now();
        userStamp.updateTimeStamps();
        assertTrue(userStamp.getCreatedDateTime().equals(createdTime));

    }

    @Test
    public void shouldSetUserName() {
        User user = new User("9087687");
        assertNotNull(user.getUid());
        user.setUsername("john");
        assertThat(user.getUsername(), is("john"));
        userRepository.save(user);

        User userDb = userRepository.findOneByUid(user.getUid());
        assertThat(userDb.getUsername(), is("john"));
    }

    @Test
    public void shouldSavePassword() {
        User userPass = new User("<PASSWORD>");
        assertNotNull(userPass.getUid());
        userPass.setPassword("password");
        assertThat(userPass.getPassword(), is("password"));
        userRepository.save(userPass);

        User userDb = userRepository.findOneByUid(userPass.getUid());
        assertThat(userDb.getPassword(), is("password"));
    }


    @Test
    public void shouldFetchNameToDisplay() {
        User user = new User("0987");
        assertNotNull(user.getUid());
        user.setDisplayName("john");
        user.setHasSetOwnName(true);
        assertTrue(user.isHasSetOwnName());
        assertThat(user.getDisplayName(), is("john"));
        userRepository.save(user);

        User db = userRepository.findOneByUid(user.getUid());
        assertTrue(user.isHasSetOwnName());
        assertThat(db.getDisplayName(), is("john"));
    }

    @Test
    public void shouldSetFirstNameAndLastName() {
        User userName = new User("09876");
        assertNotNull(userName.getUid());
        userName.setFirstName("john");
        userName.setLastName("doe");
        assertThat(userName.getFirstName(), is("john"));
        assertThat(userName.getLastName(), is("doe"));
        userRepository.save(userName);

        User userDb = userRepository.findOneByUid(userName.getUid());
        assertThat(userDb.getFirstName(), is("john"));
        assertThat(userDb.getLastName(), is("doe"));

    }

    @Test
    public void shouldSetLanguageCode() {
        User userLanguage = new User("12345");
        assertNotNull(userLanguage.getUid());
        userLanguage.setLanguageCode("EN");
        assertThat(userLanguage.getLanguageCode(), is("EN"));
        userRepository.save(userLanguage);

        User userDb = userRepository.findOneByUid(userLanguage.getUid());
        assertThat(userDb.getLanguageCode(), is("EN"));

    }

    @Test
    public void shouldSavePhoneNumber() {
        User userNumber = new User("907856");
        assertNotNull(userNumber.getUid());
        userNumber.setPhoneNumber("07890");
        assertThat(userNumber.getPhoneNumber(), is("07890"));
        assertThat(userNumber.getNationalNumber(), is(PhoneNumberUtil.formattedNumber("07890")));
        userRepository.save(userNumber);

        User userDb = userRepository.findByPhoneNumber("07890");
        assertThat(userDb.getPhoneNumber(), is("07890"));
        assertThat(userDb.getNationalNumber(), is(PhoneNumberUtil.formattedNumber("07890")));

    }

    @Test
    public void shouldSetEmailAddress() {
        User userEmail = new User("09090");
        assertNotNull(userEmail.getUid());
        userEmail.setEmailAddress("<EMAIL>");
        assertTrue(userEmail.hasEmailAddress());
        assertThat(userEmail.getEmailAddress(), is("<EMAIL>"));
        userRepository.save(userEmail);

        User userDb = userRepository.findOneByUid(userEmail.getUid());
        assertTrue(userDb.hasEmailAddress());
        assertThat(userDb.getEmailAddress(), is("<EMAIL>"));
    }

    @Test
    public void shouldAddAndRemoveMappedMembership() {

        User user = new User("099654");
        Role role = new Role("", null);
        Group group = new Group("", user);
        Membership newMember = new Membership(group, user, role, Instant.now());
        user.addMappedByMembership(newMember);
        assertThat(user.getMemberships().size(), is(1));
        userRepository.save(user);
        user.removeMappedByMembership(newMember);
        assertThat(user.getMemberships().size(), is(0));
        userRepository.save(user);
    }

    @Test
    public void shouldFetchMemberships() {
        User userMember = new User("1234");
        Group group = new Group("Group", userMember);
        Role role = new Role("", null);
        assertNotNull(userMember.getUid());
        assertTrue(userMember.getMemberships().isEmpty());
        Membership newMember = new Membership(group, userMember, role, Instant.now());
        assertThat(newMember.getGroup().getGroupName(), is("Group"));
        assertThat(newMember.getUser().getPhoneNumber(), is("1234"));
        //assertThat(userMember.getMemberships().);

    }

    @Test
    public void shouldSaveMessagePreference() {
        User userPrefer = new User("0908");
        assertNotNull(userPrefer.getUid());
        userPrefer.setMessagingPreference(UserMessagingPreference.SMS);
        assertThat(userPrefer.getMessagingPreference(), is(UserMessagingPreference.SMS));
        userRepository.save(userPrefer);
        User userDb = userRepository.findOneByUid(userPrefer.getUid());
        assertNotNull(userDb.getUid());
        assertThat(userDb.getMessagingPreference(), is(UserMessagingPreference.SMS));

    }

    @Test
    public void shouldSaveNotificationPriority() {
        User userPriority = new User("56790");
        assertNotNull(userPriority.getUid());
        userPriority.setNotificationPriority(2);
        assertThat(userPriority.getNotificationPriority(), is(2));
        userPriority.setNotificationPriority(null);
        assertThat(userPriority.getNotificationPriority(), is(1));
        userRepository.save(userPriority);
    }



    @Test
    public void checkTrialStatus() throws Exception {
        assertThat(userRepository.count(), is(0L));

        User entity = new User("07515757537");
        assertNull(entity.getId());
        assertNotNull(entity.getUid());
        userRepository.save(entity);

        assertThat(userRepository.count(), is(1L));
        User db = userRepository.findAll().iterator().next();
        assertNotNull(db.getId());
        assertThat(db.getPhoneNumber(), is("07515757537"));
        assertNotNull(db.getCreatedDateTime());
        assertFalse(db.isHasUsedFreeTrial());

    }

    @Test
    public void changeTrialStatus() throws Exception {
        assertThat(userRepository.count(), is(0L));

        User ToCreate = new User("07515757537");
        userRepository.save(ToCreate);

        User retrieve = userRepository.findAll().iterator().next();
        assertNotNull(retrieve.getId());
        assertThat(retrieve.getPhoneNumber(), is("07515757537"));
        assertFalse(retrieve.isHasUsedFreeTrial());
        retrieve.setHasUsedFreeTrial(true);
        userRepository.save(retrieve);

    }

    @Test
    public void shouldSaveAndFetchName() {

        User entity = new User("232331");
        assertNotNull(entity.getUid());
        entity.setDisplayName("john");
        assertTrue(entity.hasName());
        assertThat(entity.getName(), is("john"));
        userRepository.save(entity);

        User fromDb = userRepository.findAll().iterator().next();
        assertNotNull(fromDb.getUid());
        assertThat(entity.getName(), is("john"));

    }

    @Test
    public void shouldSetDisplayName() {
        User entity = new User("2435", "");
        assertNotNull(entity.getUid());
        assertFalse(entity.hasName());
        assertThat(entity.getName(""), is("035"));
        assertThat(entity.getName("name"), is("name (035)"));
        assertThat(entity.getPhoneNumber(), is("2435"));
        userRepository.save(entity);
        User userDb = userRepository.findOneByUid(entity.getUid());
        assertNotNull(userDb.getUid());
        assertThat(entity.getPhoneNumber(), is("2435"));

    }

    @Test
    public void checkOriginalTrialStatus() throws Exception {
        assertThat(userRepository.count(), is(0L));

        User dbz = new User("07515757537");
        userRepository.save(dbz);

        User check = userRepository.findAll().iterator().next();
        assertNotNull(check.getId());
        assertThat(check.getPhoneNumber(), is("07515757537"));
        assertFalse(check.isHasUsedFreeTrial());

{'fraction_non_alphanumeric': 0.12536992198009148, 'fraction_numerical': 0.020379338175948346, 'mean_word_length': 3.603405572755418, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '263178', 'n_tokens_mistral': 4300, 'n_tokens_neox': 4090, 'n_words': 683}