Browse Source

Integration tests for new repository methods

pull/1378/head
Sauli Ketola 7 years ago
parent
commit
e6a8e0c17d
  1. 21
      openid-connect-server/pom.xml
  2. 96
      openid-connect-server/src/test/java/org/mitre/oauth2/repository/impl/TestDatabaseConfiguration.java
  3. 107
      openid-connect-server/src/test/java/org/mitre/oauth2/repository/impl/TestJpaOAuth2TokenRepository.java

21
openid-connect-server/pom.xml

@ -47,10 +47,31 @@
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.core</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>

96
openid-connect-server/src/test/java/org/mitre/oauth2/repository/impl/TestDatabaseConfiguration.java

@ -0,0 +1,96 @@
package org.mitre.oauth2.repository.impl;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
public class TestDatabaseConfiguration {
@Autowired
private JpaVendorAdapter jpaAdapter;
@Autowired
private DataSource dataSource;
@Autowired
private EntityManagerFactory entityManagerFactory;
@Bean
public JpaOAuth2TokenRepository repository() {
return new JpaOAuth2TokenRepository();
}
@Bean(name = "defaultPersistenceUnit")
public FactoryBean<EntityManagerFactory> entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setPackagesToScan("org.mitre", "org.mitre");
factory.setPersistenceProviderClass(org.eclipse.persistence.jpa.PersistenceProvider.class);
factory.setPersistenceUnitName("test" + System.currentTimeMillis());
factory.setDataSource(dataSource);
factory.setJpaVendorAdapter(jpaAdapter);
Map<String, Object> jpaProperties = new HashMap<String, Object>();
jpaProperties.put("eclipselink.weaving", "false");
jpaProperties.put("eclipselink.logging.level", "INFO");
jpaProperties.put("eclipselink.logging.level.sql", "INFO");
jpaProperties.put("eclipselink.cache.shared.default", "false");
factory.setJpaPropertyMap(jpaProperties);
return factory;
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder(new DefaultResourceLoader() {
@Override
public Resource getResource(String location) {
String sql;
try {
sql = new String(Files.readAllBytes(Paths.get("..", "openid-connect-server-webapp", "src", "main",
"resources", "db", "hsql", location)), UTF_8);
} catch (IOException e) {
throw new RuntimeException("Failed to read sql-script " + location, e);
}
return new ByteArrayResource(sql.getBytes(UTF_8));
}
}).generateUniqueName(true).setScriptEncoding(UTF_8.name()).setType(EmbeddedDatabaseType.HSQL)
.addScripts("hsql_database_tables.sql").build();
}
@Bean
public JpaVendorAdapter jpaAdapter() {
EclipseLinkJpaVendorAdapter adapter = new EclipseLinkJpaVendorAdapter();
adapter.setDatabase(Database.HSQL);
adapter.setShowSql(true);
return adapter;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager platformTransactionManager = new JpaTransactionManager();
platformTransactionManager.setEntityManagerFactory(entityManagerFactory);
return platformTransactionManager;
}
}

107
openid-connect-server/src/test/java/org/mitre/oauth2/repository/impl/TestJpaOAuth2TokenRepository.java

@ -0,0 +1,107 @@
package org.mitre.oauth2.repository.impl;
import static org.junit.Assert.assertEquals;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mitre.oauth2.model.AuthenticationHolderEntity;
import org.mitre.oauth2.model.OAuth2AccessTokenEntity;
import org.mitre.oauth2.model.OAuth2RefreshTokenEntity;
import org.mitre.oauth2.model.SavedUserAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { TestDatabaseConfiguration.class })
@Transactional
public class TestJpaOAuth2TokenRepository {
@Autowired
private JpaOAuth2TokenRepository repository;
@PersistenceContext
private EntityManager entityManager;
@Before
public void setUp(){
createAccessToken("user1");
createAccessToken("user1");
createAccessToken("user2");
createAccessToken("user2");
createRefreshToken("user1");
createRefreshToken("user1");
createRefreshToken("user2");
createRefreshToken("user2");
createRefreshToken("user2");
}
@Test
public void testGetAccessTokensByUserName() {
Set<OAuth2AccessTokenEntity> tokens = repository.getAccessTokensByUserName("user1");
assertEquals(2, tokens.size());
assertEquals("user1", tokens.iterator().next().getAuthenticationHolder().getUserAuth().getName());
}
@Test
public void testGetRefreshTokensByUserName() {
Set<OAuth2RefreshTokenEntity> tokens = repository.getRefreshTokensByUserName("user2");
assertEquals(3, tokens.size());
assertEquals("user2", tokens.iterator().next().getAuthenticationHolder().getUserAuth().getName());
}
@Test
public void testGetAllAccessTokens(){
Set<OAuth2AccessTokenEntity> tokens = repository.getAllAccessTokens();
assertEquals(4, tokens.size());
}
@Test
public void testGetAllRefreshTokens(){
Set<OAuth2RefreshTokenEntity> tokens = repository.getAllRefreshTokens();
assertEquals(5, tokens.size());
}
private OAuth2AccessTokenEntity createAccessToken(String name) {
SavedUserAuthentication userAuth = new SavedUserAuthentication();
userAuth.setName(name);
userAuth = entityManager.merge(userAuth);
AuthenticationHolderEntity authHolder = new AuthenticationHolderEntity();
authHolder.setUserAuth(userAuth);
authHolder = entityManager.merge(authHolder);
OAuth2AccessTokenEntity accessToken = new OAuth2AccessTokenEntity();
accessToken.setAuthenticationHolder(authHolder);
accessToken = entityManager.merge(accessToken);
return accessToken;
}
private OAuth2RefreshTokenEntity createRefreshToken(String name) {
SavedUserAuthentication userAuth = new SavedUserAuthentication();
userAuth.setName(name);
userAuth = entityManager.merge(userAuth);
AuthenticationHolderEntity authHolder = new AuthenticationHolderEntity();
authHolder.setUserAuth(userAuth);
authHolder = entityManager.merge(authHolder);
OAuth2RefreshTokenEntity refreshToken = new OAuth2RefreshTokenEntity();
refreshToken.setAuthenticationHolder(authHolder);
refreshToken = entityManager.merge(refreshToken);
return refreshToken;
}
}
Loading…
Cancel
Save