<commit_before>/*
 * OpenWMS, the open Warehouse Management System
 * 
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.openwms.common.domain.system.usermanagement;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.fail;

import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.junit.Test;
import org.openwms.common.domain.helper.AbstractPDOTestCase;

/**
 * 
 * A RoleTest.
 * 
 * @author <a href="<EMAIL>"><NAME></a>
 * @version $Revision$
 */
public final class RoleTest extends AbstractPDOTestCase {

	private static final String TEST_ROLE = "ROLE_TEST";
	private static final String KNOWN_USER = "KNOWN_USER";
	private static final String UNKNOWN_USER = "UNKNOWN_USER";

	@Test
	public final void testRoleConstraint() {
		EntityTransaction entityTransaction = em.getTransaction();
		Role role = new Role(TEST_ROLE);
		Role role2 = new Role(TEST_ROLE);

		try {
			entityTransaction.begin();
			em.persist(role);
			em.persist(role2);
			entityTransaction.commit();
			fail("No unique constraint on rolename");
		}
		catch (PersistenceException pe) {
			LOG.debug("OK:Tested unique constraint on rolename.");
		}
	}

	@Test
	public final void testRoleLifecycle() {
		EntityTransaction entityTransaction = em.getTransaction();
		Role role = new Role(TEST_ROLE);
		User unknownUser = new User(UNKNOWN_USER);
		User knownUser = new User(KNOWN_USER);

		entityTransaction.begin();
		em.persist(knownUser);
		entityTransaction.commit();

		role.addUser(knownUser);
		role.addUser(unknownUser);

		entityTransaction.begin();
		em.merge(role);
		entityTransaction.commit();

		Query query = em.createQuery("select count(u) from User u where u.username = :username");
		query.setParameter("username", unknownUser.getUsername());
		Long cnt = (Long) query.getSingleResult();
		assertEquals("User must be persisted with Role", 1, cnt.intValue());

		query = em.createQuery("select r from Role r where r.rolename = :rolename");
		query.setParameter("rolename", role.getRolename());
		role = (Role) query.getSingleResult();
		assertNotSame("Role must be persisted", 0, role.getId());<commit_msg>Improved test case<commit_after>/*
 * OpenWMS, the open Warehouse Management System
 * 
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.openwms.common.domain.system.usermanagement;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.fail;

import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.junit.Test;
import org.openwms.common.domain.helper.AbstractPDOTestCase;

/**
 * 
 * A RoleTest.
 * 
 * @author <a href="<EMAIL>"><NAME></a>
 * @version $Revision$
 */
public final class RoleTest extends AbstractPDOTestCase {

	private static final String TEST_ROLE = "ROLE_TEST";
	private static final String KNOWN_USER = "KNOWN_USER";
	private static final String UNKNOWN_USER = "UNKNOWN_USER";

	@Test
	public final void testRoleConstraint() {
		EntityTransaction entityTransaction = em.getTransaction();
		Role role = new Role(TEST_ROLE);
		Role role2 = new Role(TEST_ROLE);

		try {
			entityTransaction.begin();
			em.persist(role);
			em.persist(role2);
			entityTransaction.commit();
			fail("No unique constraint on rolename");
		}
		catch (PersistenceException pe) {
			LOG.debug("OK:Tested unique constraint on rolename.");
		}
	}
	
	@Test
	public final void testRoleInstanciation() {
		Role role = new Role("Rolename","Description");
		assertEquals("Rolename doesnt match", "Rolename", role.getRolename());
		assertEquals("Description doesnt match", "Description", role.getDescription());
	}

	@Test
	public final void testAddingUserToRole() {
		Role role = new Role("Rolename");
		try {
			role.addUser(null);
			fail("Not allowed to call addUser() with null");
		}
		catch (Exception e) {
			LOG.debug("OK:Adding null user not allowed");
		}
	}

	@Test
	public final void testAddingUsersToRole() {
		Role role = new Role("Rolename");
		try {
			role.setUsers(null);
			fail("Not allowed to call setUsers() with null");
		}
		catch (Exception e) {
			LOG.debug("OK:Setting null to Set of users not allowed");
		}
	}

	
	@Test
	public final void testRoleLifecycle() {
		EntityTransaction entityTransaction = em.getTransaction();
		Role role = new Role(TEST_ROLE);
		User unknownUser = new User(UNKNOWN_USER);
		User knownUser = new User(KNOWN_USER);

		entityTransaction.begin();
		em.persist(knownUser);
		entityTransaction.commit();

		role.addUser(knownUser);
		role.addUser(unknownUser);

		entityTransaction.begin();
		em.merge(role);
		entityTransaction.commit();

		Query query = em.createQuery("select count(u) from User u where u.username = :username");
		query.setParameter("username", unknownUser.getUsername());
		Long cnt = (Long) query.getSingleResult();
		assertEquals("User must be persisted with Role", 1, cnt.intValue());

		query = em.createQuery("select r from Role r where r.rolename = :rolename");
		query.setParameter("rolename", role.getRolename());
		role = (Role) query.getSingleResult();
		assertNotSame("Role must be persisted", 0, role.getId());
{'fraction_non_alphanumeric': 0.12219730941704036, 'fraction_numerical': 0.0014947683109118087, 'mean_word_length': 4.812160694896852, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '144215', 'n_tokens_mistral': 1749, 'n_tokens_neox': 1606, 'n_words': 425}