package sg.ncl.service.user;

import org.apache.commons.lang3.RandomStringUtils;
import org.hamcrest.collection.IsIterableContainingInAnyOrder;
import org.junit.Assert;
import org.junit.Test;
import sg.ncl.service.user.data.jpa.entities.AddressEntity;
import sg.ncl.service.user.data.jpa.entities.UserDetailsEntity;
import sg.ncl.service.user.data.jpa.entities.UserEntity;
import sg.ncl.service.user.data.jpa.repositories.UserRepository;
import sg.ncl.service.user.domain.User;
import sg.ncl.service.user.exceptions.UserNotFoundException;

import javax.inject.Inject;
import java.time.ZonedDateTime;
import java.util.List;

/**
 * Created by Desmond
 */
public class UserServiceTest extends AbstractTest {
    @Inject
    private UserRepository userRepository;

    @Test
    public void getAllUserWithNoUserInDbTest() throws Exception {
        UserService userService = new UserService(userRepository);
        List<User> list = userService.get();
        Assert.assertTrue(list.size() == 0);
    }

    @Test
    public void getAllUserTest() throws Exception {
        UserService userService = new UserService(userRepository);
        User[] userArray = new User[3];

        for (int i = 0; i < 3; i++) {
            UserEntity[] userEntityArray = addUser();
            userArray[i] = userEntityArray[0];
        }

        List<User> userList2 = userService.get();

        Assert.assertThat(userList2, IsIterableContainingInAnyOrder.containsInAnyOrder(userArray));
    }

    @Test(expected=UserNotFoundException.class)
    public void getUserWithNoUserInDbTest() throws Exception {
        UserService userService = new UserService(userRepository);
        userService.find(RandomStringUtils.randomAlphabetic(20));
    }

    private UserEntity[] addUser() throws Exception {
        final UserEntity userEntity = new UserEntity();
        userEntity.setApplicationDate(ZonedDateTime.now());

        final UserDetailsEntity userDetailsEntity = new UserDetailsEntity();
        userDetailsEntity.setFirstName(RandomStringUtils.randomAlphabetic(20));
        userDetailsEntity.setLastName(RandomStringUtils.randomAlphabetic(20));
        userDetailsEntity.setEmail(RandomStringUtils.randomAlphabetic(20));
        userDetailsEntity.setPhone(RandomStringUtils.randomAlphabetic(20));

        final AddressEntity address = new AddressEntity();
        address.setAddress1(RandomStringUtils.randomAlphabetic(20));
        address.setAddress2(RandomStringUtils.randomAlphabetic(20));
        address.setCountry(RandomStringUtils.randomAlphabetic(20));
        address.setRegion(RandomStringUtils.randomAlphabetic(20));
        address.setZipCode(RandomStringUtils.randomAlphabetic(20));

        userDetailsEntity.setAddress(address);
        userEntity.setUserDetails(userDetailsEntity);

        UserEntity saveUser = userRepository.save(userEntity);

        final UserEntity[] userArray = new UserEntity[2];
        userArray[0] = saveUser;
        userArray[1] = userEntity;

        return userArray;
    }
}

@@ -1720,24 +1720,1451 @@
 0));%0A    %7D%0A%0A
+    @Test%0A    public void getUserTest() throws Exception %7B%0A        UserService userService = new UserService(userRepository);%0A        final UserEntity%5B%5D userArray = addUser();%0A        final String idString = userArray%5B0%5D.getId();%0A        final UserEntity originalEntity = userArray%5B1%5D;%0A%0A        UserEntity fromDbEntity = userService.find(idString);%0A%0A        UserDetailsEntity originalDetails = originalEntity.getUserDetails();%0A        UserDetailsEntity fromDbDetails = fromDbEntity.getUserDetails();%0A        Assert.assertEquals(originalDetails.getFirstName(), fromDbDetails.getFirstName());%0A        Assert.assertEquals(originalDetails.getLastName(), fromDbDetails.getLastName());%0A        Assert.assertEquals(originalDetails.getEmail(), fromDbDetails.getEmail());%0A        Assert.assertEquals(originalDetails.getPhone(), fromDbDetails.getPhone());%0A%0A        AddressEntity originalAddress = originalDetails.getAddress();%0A        AddressEntity fromDbAddress = fromDbDetails.getAddress();%0A        Assert.assertEquals(originalAddress.getAddress1(), fromDbAddress.getAddress1());%0A        Assert.assertEquals(originalAddress.getAddress2(), fromDbAddress.getAddress2());%0A        Assert.assertEquals(originalAddress.getCountry(), fromDbAddress.getCountry());%0A        Assert.assertEquals(originalAddress.getRegion(), fromDbAddress.getRegion());%0A        Assert.assertEquals(originalAddress.getZipCode(), fromDbAddress.getZipCode());%0A    %7D%0A%0A
     private 

Add getUserTest (DEV 100)