Java Code Examples for play.db.jpa.JPA#withTransaction()

The following examples show how to use play.db.jpa.JPA#withTransaction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NotificationTest.java    From htwplus with MIT License 6 votes vote down vote up
/**
 * Tests, if no notification is created, when no recipient is set.
 */
@Test
public void testNoNotification() throws Throwable {
    final MockNotifiable notifiable = new MockNotifiable();
    notifiable.sender = this.getTestAccount(1);
    notifiable.rendered = "MOCK NO NOTIFICATION";
    NotificationService.getInstance().createNotification(notifiable);
    this.sleep(5); // sleep to ensure, that Akka would be done with notification creation

    JPA.withTransaction(new F.Callback0() {
        @Override
        @SuppressWarnings("unused")
        public void invoke() throws Throwable {
            List<Notification> notifications = Notification.findByRenderedContent(notifiable.rendered);
            assertThat(notifications.size()).isEqualTo(0);
        }
    });
}
 
Example 2
Source File: SecurityTest.java    From htwplus with MIT License 6 votes vote down vote up
/**
 * Tests, if the method "isMemberOfGroup()" works as expected.
 */
@Test
public void testIsMemberOfGroup() {
    final Account testAccount1 = this.getTestAccount(1);
    final Account testAccount2 = this.getTestAccount(2);
    final Account testAccount3 = this.getTestAccount(3);
    final Group testGroup = this.getTestGroup(1, testAccount1);
    this.establishGroupMembership(testAccount2, testGroup);
    this.removeGroupMembership(testAccount3, testGroup);

    // test, that we have exactly one notification
    JPA.withTransaction(new F.Callback0() {
        @Override
        public void invoke() throws Throwable {
            assertThat(Secured.isMemberOfGroup(testGroup, testAccount1)).isTrue();
            assertThat(Secured.isMemberOfGroup(testGroup, testAccount2)).isTrue();
            assertThat(Secured.isMemberOfGroup(testGroup, testAccount3)).isFalse();
        }
    });
}
 
Example 3
Source File: MessageProcessor.java    From judgels with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void run() {
    JPA.withTransaction(() -> {
            try {
                GradingResponse response = MAPPER.readValue(message.getContent(), GradingResponse.class);

                boolean gradingExists = false;

                // temporary solution
                // problem is: grading response arrives before the grading model persistance has been flushed

                for (int i = 0; i < 3; i++) {
                    if (submissionService.gradingExists(response.getGradingJid())) {
                        gradingExists = true;
                        break;
                    }

                    Thread.sleep(TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS));
                }

                if (gradingExists) {
                    submissionService.grade(response.getGradingJid(), response.getResult(), message.getSourceJid(), "localhost");
                } else {
                    System.out.println("Grading JID " + response.getGradingJid() + " not found!");
                }
                messageService.confirmMessage(sealtielClientAuthHeader, message.getId());
            } catch (RemoteException | IOException e) {
                System.out.println("Bad grading response!");
                e.printStackTrace();
            }
        });
}
 
Example 4
Source File: AbstractJudgelsDataMigrator.java    From judgels with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void migrate() throws SQLException {
    checkTable();

    long currentDataVersion = dataVersionDao.getVersion();
    long latestDataVersion = getLatestDataVersion();
    if (currentDataVersion != latestDataVersion) {
        migrate(currentDataVersion);

        JPA.withTransaction(() -> dataVersionDao.update(latestDataVersion));
    }
}
 
Example 5
Source File: FakeApplicationTest.java    From htwplus with MIT License 5 votes vote down vote up
/**
 * Returns an account by email.
 *
 * @param email E-Mail address of account to fetch
 * @return Account instance
 */
public Account getAccountByEmail(final String email) {
    try {
        return JPA.withTransaction(new F.Function0<Account>() {
            @Override
            public Account apply() throws Throwable {
                return Account.findByEmail(email);
            }
        });
    } catch (Throwable throwable) {
        throwable.printStackTrace();
        return null;
    }
}
 
Example 6
Source File: FakeApplicationTest.java    From htwplus with MIT License 5 votes vote down vote up
/**
 * Returns a test account, creates one before if not exists.
 *
 * @param number Number of test account
 * @return Account instance
 */
public Account getTestAccount(final int number) {
    final String testAccountEmail = "test" + String.valueOf(number) + "@htwplus.de";
    Account storedTestAccount = this.getAccountByEmail(testAccountEmail);

    // if there is this test account, return
    if (storedTestAccount != null) {
        return storedTestAccount;
    }

    // there is no test account with that number right now, create a persistent one
    try {
        return JPA.withTransaction(new F.Function0<Account>() {
            @Override
            public Account apply() throws Throwable {
                Account testAccount = new Account();
                testAccount.firstname = "Test";
                testAccount.lastname = "User " + String.valueOf(number);
                testAccount.email = testAccountEmail;
                testAccount.avatar = "a1";
                testAccount.role = AccountRole.STUDENT;
                testAccount.password = Component.md5(FakeApplicationTest.TEST_ACCOUNT_PASSWORD);
                testAccount.create();

                return testAccount;
            }
        });
    } catch (Throwable throwable) {
        throwable.printStackTrace();
        return null;
    }
}
 
Example 7
Source File: FakeApplicationTest.java    From htwplus with MIT License 5 votes vote down vote up
/**
 * Removes a friendship between test accounts.
 *
 * @param testAccountA First test account
 * @param testAccountB Second test account
 */
public void removeFriendshipTestAccounts(final Account testAccountA, final Account testAccountB) {
    JPA.withTransaction(new F.Callback0() {
        @Override
        public void invoke() throws Throwable {
            if (Friendship.alreadyFriendly(testAccountA, testAccountB)) {
                Friendship.findFriendLink(testAccountA, testAccountB).delete();
            }
            if (Friendship.alreadyFriendly(testAccountB, testAccountA)) {
                Friendship.findFriendLink(testAccountB, testAccountA).delete();
            }
        }
    });
}
 
Example 8
Source File: FakeApplicationTest.java    From htwplus with MIT License 5 votes vote down vote up
/**
 * Returns a group by title.
 *
 * @param title Title of group to fetch
 * @return Group instance
 */
public Group getGroupByTitle(final String title) {
    try {
        return JPA.withTransaction(new F.Function0<Group>() {
            @Override
            public Group apply() throws Throwable {
                return Group.findByTitle(title);
            }
        });
    } catch (Throwable throwable) {
        throwable.printStackTrace();
        return null;
    }
}
 
Example 9
Source File: FakeApplicationTest.java    From htwplus with MIT License 5 votes vote down vote up
/**
 * Returns a test group, creates one before if not exists.
 *
 * @param number Number of test group
 * @param groupOwner the optional group owner
 * @return Group instance
 */
public Group getTestGroup(final int number, final Account groupOwner) {
    final String testGroupTitle = "Test Group " + String.valueOf(number);
    Group storedTestGroup = this.getGroupByTitle(testGroupTitle);

    // if there is this test group, return
    if (storedTestGroup != null) {
        return storedTestGroup;
    }

    // there is no test account with that number right now, create a persistent one
    try {
        return JPA.withTransaction(new F.Function0<Group>() {
            @Override
            public Group apply() throws Throwable {
                Group testGroup = new Group();
                testGroup.groupType = GroupType.close;
                testGroup.setTitle(testGroupTitle);

                if (groupOwner != null) {
                    testGroup.createWithGroupAccount(groupOwner);
                } else {
                    testGroup.create();
                }

                return testGroup;
            }
        });
    } catch (Throwable throwable) {
        throwable.printStackTrace();
        return null;
    }
}
 
Example 10
Source File: FakeApplicationTest.java    From htwplus with MIT License 5 votes vote down vote up
/**
 * Returns a group account for an account to a group.
 *
 * @param account Account
 * @param group Group
 * @return GroupAccount instance if found, otherwise null
 */
public GroupAccount getGroupAccount(final Account account, final Group group) {
    try {
        return JPA.withTransaction(new F.Function0<GroupAccount>() {
            @Override
            public GroupAccount apply() throws Throwable {
                return GroupAccount.find(account, group);
            }
        });
    } catch (Throwable throwable) {
        throwable.printStackTrace();
        return null;
    }
}
 
Example 11
Source File: FakeApplicationTest.java    From htwplus with MIT License 5 votes vote down vote up
/**
 * Establishes a group membership of an account to a group.
 *
 * @param account Account
 * @param group Group
 */
public void establishGroupMembership(final Account account, final Group group) {
    JPA.withTransaction(new F.Callback0() {
        @Override
        public void invoke() throws Throwable {
            if (!Group.isMember(group, account)) {
                groupAccount.account = account;
                groupAccount.group = group;
                groupAccount.linkType = LinkType.establish;
                testGroupAccount.create();
                group.update();
            }
        }
    });
}
 
Example 12
Source File: FakeApplicationTest.java    From htwplus with MIT License 5 votes vote down vote up
/**
 * Removes a membership of an account to a group.
 *
 * @param account Account
 * @param group Group
 */
public void removeGroupMembership(final Account account, final Group group) {
    JPA.withTransaction(new F.Callback0() {
        @Override
        public void invoke() throws Throwable {
            if (Group.isMember(group, account)) {
                GroupAccount groupAccount = GroupAccount.find(account, group);
                groupAccount.delete();
            }
        }
    });
}
 
Example 13
Source File: SecurityTest.java    From htwplus with MIT License 5 votes vote down vote up
/**
 * Tests, if the method "isOwnerOfAccount()" works as expected.
 */
@Test
public void testIsOwnerOfAccount() {
    final Account testAccount1 = this.getTestAccount(1);
    final Account testAccount2 = this.getTestAccount(2);
    this.loginAccount(testAccount1);

    JPA.withTransaction(new F.Callback0() {
        @Override
        public void invoke() throws Throwable {
            assertThat(Secured.isOwnerOfAccount(testAccount1.id)).isTrue();
            assertThat(Secured.isOwnerOfAccount(testAccount2.id)).isFalse();
        }
    });
}
 
Example 14
Source File: SecurityTest.java    From htwplus with MIT License 5 votes vote down vote up
/**
 * Tests, if the method "viewGroup()" works as expected.
 */
@Test
public void testViewGroup() {
    Account testAccount1 = this.getTestAccount(1);
    final Group testGroup = this.getTestGroup(1, testAccount1);

    // test, if admin is allowed to view
    this.loginAdminAccount();
    JPA.withTransaction(new F.Callback0() {
        @Override
        public void invoke() throws Throwable {
            assertThat(Secured.viewGroup(testGroup)).isTrue();
        }
    });

    // test, if member of group is allowed to view
    this.loginAccount(testAccount1);
    JPA.withTransaction(new F.Callback0() {
        @Override
        public void invoke() throws Throwable {
            assertThat(Secured.viewGroup(testGroup)).isTrue();
        }
    });

    // test, if no member of group is disallowed to view
    Account testAccount3 = this.getTestAccount(3);
    this.removeGroupMembership(testAccount3, testGroup);
    this.loginAccount(testAccount3);
    JPA.withTransaction(new F.Callback0() {
        @Override
        public void invoke() throws Throwable {
            assertThat(Secured.viewGroup(testGroup)).isFalse();
        }
    });
}
 
Example 15
Source File: Shop.java    From pfe-samples with MIT License 5 votes vote down vote up
private <A> A withTransaction(Function<EntityManager, A> f) {
    try {
        return JPA.withTransaction(() -> f.apply(JPA.em()));
    } catch (Throwable throwable) {
        throw new RuntimeException(throwable);
    }
}