Skip to content

Commit

Permalink
More tests for UserAccountDao, fix issues.
Browse files Browse the repository at this point in the history
Simplify transactional behavior of UserAccountDao.
  • Loading branch information
ledsoft committed Oct 20, 2023
1 parent b916abf commit c0c1346
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void onEvent(Event event) {

private void newUser(KodiUserAccount userAccount) {
LOG.info("Generating new user metadata into triple store for user {}", userAccount);
userAccountDao.transactional(() -> userAccountDao.persist(userAccount));
userAccountDao.persist(userAccount);
addGraphDBUser(userAccount);
}

Expand All @@ -79,7 +79,7 @@ private void addGraphDBUser(KodiUserAccount userAccount) {

private void updateUser(KodiUserAccount userAccount) {
LOG.info("Updating metadata of user {} in triple store", userAccount);
userAccountDao.transactional(() -> userAccountDao.update(userAccount));
userAccountDao.update(userAccount);
addGraphDBUser(userAccount);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public UserAccountDao(RepositoryConnection connection, Vocabulary vocabulary) {

public void persist(KodiUserAccount userAccount) {
Objects.requireNonNull(userAccount);
connection.begin();
persistInTransaction(userAccount);
connection.commit();
}

private void persistInTransaction(KodiUserAccount userAccount) {
if (Objects.isNull(KodiUserAccount.getContext()) || KodiUserAccount.getContext().isEmpty()) {
generateUserMetadataStatements(userAccount).forEach(connection::add);
} else {
Expand All @@ -49,28 +55,23 @@ private List<Statement> generateUserMetadataStatements(KodiUserAccount userAccou
vf.createLiteral(userAccount.getUsername()))
));
if (vocabulary.getEmail() != null) {
statements.add(vf.createStatement(subject, vf.createIRI(vocabulary.getEmail()), vf.createLiteral(userAccount.getEmail())));
statements.add(vf.createStatement(subject, vf.createIRI(vocabulary.getEmail()),
vf.createLiteral(userAccount.getEmail())));
}
return statements;
}

public void update(KodiUserAccount userAccount) {
Objects.requireNonNull(userAccount);
final IRI subject = vf.createIRI(userAccount.getUri().toString());
connection.remove(
connection.getStatements(subject, vf.createIRI(vocabulary.getFirstName()), null));
connection.remove(
connection.getStatements(subject, vf.createIRI(vocabulary.getLastName()), null));
connection.remove(
connection.getStatements(subject, vf.createIRI(vocabulary.getUsername()), null));
connection.remove(
connection.getStatements(subject, vf.createIRI(vocabulary.getEmail()), null));
persist(userAccount);
}

public void transactional(Runnable procedure) {
connection.begin();
procedure.run();
final IRI subject = vf.createIRI(userAccount.getUri().toString());
connection.remove(subject, vf.createIRI(vocabulary.getFirstName()), null);
connection.remove(subject, vf.createIRI(vocabulary.getLastName()), null);
connection.remove(subject, vf.createIRI(vocabulary.getUsername()), null);
if (vocabulary.getEmail() != null) {
connection.remove(subject, vf.createIRI(vocabulary.getEmail()), null);
}
persistInTransaction(userAccount);
connection.commit();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ void tearDown() {
@Test
void persistGeneratesUserMetadataStatementsToDefaultContextWhenNoneIsSpecified() {
final KodiUserAccount user = initUserAccount();

sut.persist(user);

verifyBasicUserMetadataPersist(user);
}

private void verifyBasicUserMetadataPersist(KodiUserAccount user) {
final IRI subj = vf.createIRI(user.getUri().toString());
verify(connection).add(vf.createStatement(subj, RDF.TYPE, vf.createIRI(vocabulary.getType())));
verify(connection).add(vf.createStatement(subj, vf.createIRI(vocabulary.getFirstName()), vf.createLiteral(user.getFirstName())));
Expand Down Expand Up @@ -87,10 +91,30 @@ void persistGeneratesEmailStatementWhenEmailPropertyIsConfigured() {

sut.persist(user);
final IRI subj = vf.createIRI(user.getUri().toString());
verify(connection).add(vf.createStatement(subj, RDF.TYPE, vf.createIRI(vocabulary.getType())));
verify(connection).add(vf.createStatement(subj, vf.createIRI(vocabulary.getFirstName()), vf.createLiteral(user.getFirstName())));
verify(connection).add(vf.createStatement(subj, vf.createIRI(vocabulary.getLastName()), vf.createLiteral(user.getLastName())));
verify(connection).add(vf.createStatement(subj, vf.createIRI(vocabulary.getUsername()), vf.createLiteral(user.getUsername())));
verifyBasicUserMetadataPersist(user);
verify(connection).add(vf.createStatement(subj, vf.createIRI(vocabulary.getEmail()), vf.createLiteral(user.getEmail())));
}

@Test
void updateRemovesExistingUserMetadataStatementsAndPersistsNewData() {
final KodiUserAccount user = initUserAccount();

sut.update(user);
final IRI subj = vf.createIRI(user.getUri().toString());
verify(connection).remove(subj, vf.createIRI(vocabulary.getFirstName()), null);
verify(connection).remove(subj, vf.createIRI(vocabulary.getLastName()), null);
verify(connection).remove(subj, vf.createIRI(vocabulary.getUsername()), null);
verifyBasicUserMetadataPersist(user);
}

@Test
void updateRemovesEmailWhenItsPropertyIsConfigured() {
final KodiUserAccount user = initUserAccount();
vocabulary.setEmail(FOAF.MBOX.stringValue());

sut.update(user);
final IRI subj = vf.createIRI(user.getUri().toString());
verify(connection).remove(subj, vf.createIRI(vocabulary.getEmail()), null);
verify(connection).add(vf.createStatement(subj, vf.createIRI(vocabulary.getEmail()), vf.createLiteral(user.getEmail())));
}
}

0 comments on commit c0c1346

Please sign in to comment.