Skip to content

Commit

Permalink
Add tests for UserAccountDao, fix issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Oct 19, 2023
1 parent b111a7e commit b916abf
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
12 changes: 10 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,21 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private List<Statement> generateUserMetadataStatements(KodiUserAccount userAccou
vf.createLiteral(userAccount.getUsername()))
));
if (vocabulary.getEmail() != null) {
statements.add(vf.createStatement(subject, vf.createIRI(vocabulary.getEmail()), vf.createIRI(userAccount.getEmail())));
statements.add(vf.createStatement(subject, vf.createIRI(vocabulary.getEmail()), vf.createLiteral(userAccount.getEmail())));
}
return statements;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package cz.cvut.kbss.keycloak.provider.dao;

import cz.cvut.kbss.keycloak.provider.model.KodiUserAccount;
import cz.cvut.kbss.keycloak.provider.model.Vocabulary;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.model.vocabulary.FOAF;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.net.URI;
import java.util.UUID;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class UserAccountDaoTest {

private static final ValueFactory vf = SimpleValueFactory.getInstance();

private final Vocabulary vocabulary = new Vocabulary();

@Mock
private RepositoryConnection connection;

private UserAccountDao sut;

@BeforeEach
void setUp() {
when(connection.getValueFactory()).thenReturn(vf);
this.sut = new UserAccountDao(connection, vocabulary);
}

@AfterEach
void tearDown() {
KodiUserAccount.setContext(null);
}

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

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())));
}

private KodiUserAccount initUserAccount() {
final KodiUserAccount user = new KodiUserAccount();
user.setUri(URI.create(vocabulary.getType() + "/" + UUID.randomUUID()));
user.setFirstName("First");
user.setLastName("Last");
user.setUsername("username");
user.setEmail("[email protected]");
return user;
}

@Test
void persistGeneratesUserMetadataStatementsToContextWhenItIsConfigured() {
final String context = cz.cvut.kbss.keycloak.provider.Vocabulary.s_i_uzivatel;
final KodiUserAccount user = initUserAccount();
KodiUserAccount.setContext(context);

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

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

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())));
verify(connection).add(vf.createStatement(subj, vf.createIRI(vocabulary.getEmail()), vf.createLiteral(user.getEmail())));
}
}

0 comments on commit b916abf

Please sign in to comment.