/*
 * 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
 *
 *      http://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 org.ngrinder.user.model;

import org.junit.Before;
import org.junit.Test;
import org.ngrinder.AbstractNGrinderTransactionalTest;
import org.ngrinder.model.PerfTest;
import org.ngrinder.model.Role;
import org.ngrinder.model.User;
import org.ngrinder.perftest.repository.PerfTestRepository;
import org.ngrinder.perftest.repository.TagRepository;
import org.ngrinder.user.repository.UserRepository;
import org.ngrinder.user.repository.UserSpecification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

import static java.time.Instant.now;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class UserTest extends AbstractNGrinderTransactionalTest {

	@Autowired
	public UserRepository userRepository;

	@Autowired
	private PerfTestRepository perfTestRepository;

	@Autowired
	private TagRepository tagRepository;


	@Before
	public void before() {
		List<PerfTest> findAll = perfTestRepository.findAll();
		for (PerfTest perfTest : findAll) {
			perfTest.getTags().clear();
		}
		perfTestRepository.saveAll(findAll);
		perfTestRepository.deleteAll();
		perfTestRepository.flush();
		tagRepository.deleteAll();
		tagRepository.flush();
		userRepository.deleteAll();
		userRepository.flush();
	}

	@Test
	public void testShareUser() {
		List<User> sharedUsers = new ArrayList<>();
		List<User> shareUsers = new ArrayList<>();

		Instant now = now();
		User user = new User();
		user.setUserName("MyName1");
		user.setEmail("junoyoon@gmail.com");
		user.setCreatedBy(getUser("user"));
		user.setCreatedAt(now);
		user.setUserId("hello");
		user.setRole(Role.USER);
		user = userRepository.save(user);
		User user2 = new User();
		user2.setUserId("hello2");
		user2.setUserName("MyName2");
		user2.setEmail("junoyoon2@paran.com");
		user2.setCreatedBy(getUser("user"));
		user2.setCreatedAt(now);
		user2.setRole(Role.USER);
		userRepository.save(user2);

		User user3 = new User();
		user3.setUserId("hello3");
		user3.setUserName("MyName3");
		user3.setEmail("junoyoon3@paran.com");
		user3.setCreatedBy(getUser("user"));
		user3.setCreatedAt(now);
		user3.setRole(Role.USER);
		userRepository.save(user3);

		User user4 = new User();
		user4.setUserId("hello4");
		user4.setUserName("MyName4");
		user4.setEmail("junoyoon4@paran.com");
		user4.setCreatedBy(getUser("user"));
		user4.setCreatedAt(now);
		user4.setRole(Role.USER);
		sharedUsers.add(user3);
		sharedUsers.add(user2);

		user4.setFollowers(sharedUsers);
		shareUsers.add(user);
		user4.setOwners(shareUsers);
		userRepository.save(user4);

	    User sharedUser = userRepository.findOneByUserId(user4.getUserId());
		List<User> sh = sharedUser.getFollowers();
		LOG.debug("sharedUser.getFollowers:{}", sh);
	}


	@Test
	public void testUser() {
		Instant now = now();
		User user = new User();
		user.setUserName("MyName1");
		user.setEmail("junoyoon@gmail.com");
		user.setCreatedBy(getUser("user"));
		user.setCreatedAt(now);
		user.setUserId("hello");
		user.setRole(Role.USER);
		user = userRepository.save(user);
		User user2 = new User();
		user2.setUserId("hello2");
		user2.setUserName("MyName2");
		user2.setEmail("junoyoon@paran.com");
		user2.setCreatedBy(getUser("user"));
		user2.setCreatedAt(now);
		user2.setRole(Role.USER);
		userRepository.save(user2);

		assertThat(userRepository.count(), is(2L));

		assertThat(userRepository.findAll(UserSpecification.emailLike("gmail")).size(), is(1));

		assertThat(userRepository.findAll(
						Specification.where(UserSpecification.emailLike("@paran")).and(
										UserSpecification.nameLike("MyName2"))).size(), is(1));

	}
}
