package com.epam.ta.reportportal.database.dao;

import com.epam.ta.reportportal.BaseDaoTest;
import com.epam.ta.reportportal.database.BinaryData;
import com.epam.ta.reportportal.database.DataStorage;
import com.epam.ta.reportportal.database.entity.user.User;
import com.epam.ta.reportportal.database.entity.user.UserType;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;

import java.io.IOException;

import static com.epam.ta.reportportal.database.dao.UserUtils.photoFilename;

public class UserRepositoryTest extends BaseDaoTest {

	@Autowired
	private UserRepository userRepository;

	@Autowired
	private DataStorage dataStorage;

	@Autowired
	@Value("classpath:meh.jpg")
	private Resource photoResource;

	@Test
	public void replaceUserTest() throws IOException {
		String username = RandomStringUtils.randomAlphabetic(5);
		User user = new User();
		user.setLogin(username);
		user.setEmail(username + "@epam.com");
		user.setType(UserType.INTERNAL);

		userRepository.save(user);
		BinaryData binaryData = new BinaryData(MediaType.IMAGE_JPEG_VALUE, photoResource.getFile().length(),
				photoResource.getInputStream());
		userRepository.replaceUserPhoto(user.getLogin(), binaryData);

		/* add one more data with such filename */
		dataStorage.saveData(binaryData, photoFilename(username));

		Assert.assertEquals("Incorrect count of files", 2, dataStorage.findByFilename(photoFilename(username)).size());

		/* replace photo again */
		userRepository.replaceUserPhoto(user.getLogin(), binaryData);

		/* make sure duplicates are removed */
		Assert.assertEquals("Incorrect count of files", 1, dataStorage.findByFilename(photoFilename(username)).size());
	}

}
@@ -1,8 +1,727 @@
+/*%0A * This file is part of Report Portal.%0A *%0A * Report Portal is free software: you can redistribute it and/or modify%0A * it under the terms of the GNU Lesser General Public License as published by%0A * the Free Software Foundation, either version 3 of the License, or%0A * (at your option) any later version.%0A *%0A * Report Portal is distributed in the hope that it will be useful,%0A * but WITHOUT ANY WARRANTY; without even the implied warranty of%0A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the%0A * GNU Lesser General Public License for more details.%0A *%0A * You should have received a copy of the GNU Lesser General Public License%0A * along with Report Portal.  If not, see %3Chttp://www.gnu.org/licenses/%3E.%0A */%0A
 package 
@@ -1451,16 +1451,51 @@
 ename;%0A%0A
+/**%0A * @author Andrei Varabyeu%0A */%0A
 public c

refactor user photos