use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.
the class NonEntityQueryTest method setUp.
@Before
public void setUp() throws Exception {
dataManager = AppBeans.get(DataManager.class);
passwordEncryption = AppBeans.get(PasswordEncryption.class);
QueryRunner runner = new QueryRunner(cont.persistence().getDataSource());
runner.update("delete from SYS_SERVER");
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
UserSessionSource uss = AppBeans.get(UserSessionSource.class);
UserSession userSession = uss.getUserSession();
Server server = new Server();
server.setName("someServer");
server.setRunning(false);
serverId = server.getId();
em.persist(server);
EntitySnapshot entitySnapshot = cont.metadata().create(EntitySnapshot.class);
entitySnapshot.setSnapshotDate(new Date());
entitySnapshot.setAuthor(userSession.getCurrentOrSubstitutedUser());
entitySnapshotId = entitySnapshot.getId();
em.persist(entitySnapshot);
Role role1 = new Role();
role1Id = role1.getId();
role1.setName("testRole1");
em.persist(role1);
Permission permission1 = new Permission();
permission1Id = permission1.getId();
permission1.setRole(role1);
permission1.setType(PermissionType.ENTITY_ATTR);
permission1.setTarget("sys$Server:name");
permission1.setValue(0);
em.persist(permission1);
Permission permission2 = new Permission();
permission2Id = permission2.getId();
permission2.setRole(role1);
permission2.setType(PermissionType.ENTITY_OP);
permission2.setTarget("sys$EntitySnapshot:read");
permission2.setValue(0);
em.persist(permission2);
Group group1 = new Group();
group1Id = group1.getId();
group1.setName("testGroup1");
em.persist(group1);
User user1 = new User();
user1Id = user1.getId();
user1.setName(USER_NAME_1);
user1.setLogin(USER_NAME_1);
user1.setPassword(passwordEncryption.getPasswordHash(user1Id, USER_PASSWORD));
user1.setGroup(group1);
em.persist(user1);
UserRole userRole1 = new UserRole();
userRole1Id = userRole1.getId();
userRole1.setUser(user1);
userRole1.setRole(role1);
em.persist(userRole1);
Group group2 = new Group();
group2Id = group2.getId();
group2.setName("testGroup2");
em.persist(group2);
Constraint constraint1 = new Constraint();
constraint1Id = constraint1.getId();
constraint1.setEntityName("sys$Server");
constraint1.setWhereClause("{E}.running = true");
constraint1.setGroup(group2);
em.persist(constraint1);
Constraint constraint2 = new Constraint();
constraint2Id = constraint2.getId();
constraint2.setEntityName("sys$EntitySnapshot");
constraint2.setCheckType(ConstraintCheckType.MEMORY);
constraint2.setGroovyScript("{E}.viewXml = 'xml'");
constraint2.setGroup(group2);
em.persist(constraint2);
User user2 = new User();
user2Id = user2.getId();
user2.setName(USER_NAME_2);
user2.setLogin(USER_NAME_2);
user2.setPassword(passwordEncryption.getPasswordHash(user2Id, USER_PASSWORD));
user2.setGroup(group2);
em.persist(user2);
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.
the class PersistenceHelperTest method testEntityStates.
@Test
public void testEntityStates() {
try {
PersistenceHelper.isNew(null);
fail("isNew() should not accept null");
} catch (Exception e) {
//
}
try {
PersistenceHelper.isManaged(null);
fail("isManaged() should not accept null");
} catch (Exception e) {
//
}
try {
PersistenceHelper.isDetached(null);
fail("isDetached() should not accept null");
} catch (Exception e) {
//
}
assertTrue(PersistenceHelper.isNew(new EntityDiff(null)));
UUID id;
Server server;
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
assertNotNull(em);
server = new Server();
assertTrue(PersistenceHelper.isNew(server));
assertFalse(PersistenceHelper.isManaged(server));
assertFalse(PersistenceHelper.isDetached(server));
id = server.getId();
server.setName("localhost");
server.setRunning(true);
em.persist(server);
assertTrue(PersistenceHelper.isNew(server));
assertTrue(PersistenceHelper.isManaged(server));
assertFalse(PersistenceHelper.isDetached(server));
tx.commit();
} finally {
tx.end();
}
assertFalse(PersistenceHelper.isNew(server));
assertFalse(PersistenceHelper.isManaged(server));
assertTrue(PersistenceHelper.isDetached(server));
tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
server = em.find(Server.class, id);
assertNotNull(server);
assertEquals(id, server.getId());
assertFalse(PersistenceHelper.isNew(server));
assertTrue(PersistenceHelper.isManaged(server));
assertFalse(PersistenceHelper.isDetached(server));
server.setRunning(false);
tx.commit();
} finally {
tx.end();
}
assertFalse(PersistenceHelper.isNew(server));
assertFalse(PersistenceHelper.isManaged(server));
assertTrue(PersistenceHelper.isDetached(server));
tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
server = em.merge(server);
assertFalse(PersistenceHelper.isNew(server));
assertTrue(PersistenceHelper.isManaged(server));
assertFalse(PersistenceHelper.isDetached(server));
tx.commit();
} finally {
tx.end();
}
tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
server = em.find(Server.class, id);
assertNotNull(server);
assertEquals(id, server.getId());
em.remove(server);
assertFalse(PersistenceHelper.isNew(server));
// is it correct?
assertTrue(PersistenceHelper.isManaged(server));
assertFalse(PersistenceHelper.isDetached(server));
tx.commit();
} finally {
tx.end();
}
assertFalse(PersistenceHelper.isNew(server));
assertFalse(PersistenceHelper.isManaged(server));
// is it correct?
assertTrue(PersistenceHelper.isDetached(server));
}
use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.
the class PersistenceHelperTest method testCheckLoaded.
@Test
public void testCheckLoaded() {
Server server = new Server();
cont.persistence().runInTransaction((em) -> {
em.persist(server);
});
View view = new View(Server.class).addProperty("name").addProperty("data").setLoadPartialEntities(true);
Server reloadedServer = cont.persistence().callInTransaction((em) -> {
return em.find(Server.class, server.getId(), view);
});
// fine
PersistenceHelper.checkLoaded(reloadedServer, "name");
try {
PersistenceHelper.checkLoaded(reloadedServer, "data", "running");
Assert.fail("Must throw exception");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("Server.running"));
}
}
use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.
the class PersistenceTest method testDirtyFields.
@Test
public void testDirtyFields() throws Exception {
PersistenceTools persistenceTools = cont.persistence().getTools();
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
assertNotNull(em);
Server server = new Server();
UUID id = server.getId();
server.setName("localhost");
server.setRunning(true);
em.persist(server);
assertTrue(persistenceTools.isDirty(server, "name", "running"));
assertNull(persistenceTools.getOldValue(server, "data"));
tx.commitRetaining();
em = cont.persistence().getEntityManager();
server = em.find(Server.class, id);
assertNotNull(server);
server.setData("testData");
assertTrue(persistenceTools.isDirty(server));
Set<String> dirtyFields = persistenceTools.getDirtyFields(server);
assertTrue(dirtyFields.contains("data"));
assertTrue(persistenceTools.isDirty(server, "data"));
assertNull(persistenceTools.getOldValue(server, "data"));
tx.commitRetaining();
em = cont.persistence().getEntityManager();
server = em.find(Server.class, id);
assertNotNull(server);
server.setData("testData1");
assertEquals("testData", persistenceTools.getOldValue(server, "data"));
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.
the class TransactionTest method testCommitRetaining.
@Test
public void testCommitRetaining() {
UUID id;
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
assertNotNull(em);
Server server = new Server();
id = server.getId();
server.setName("localhost");
server.setRunning(true);
em.persist(server);
tx.commitRetaining();
em = cont.persistence().getEntityManager();
server = em.find(Server.class, id);
assertEquals(id, server.getId());
server.setRunning(false);
tx.commit();
} finally {
tx.end();
}
}
Aggregations