Commit 8df80480 authored by Paul-Christian Volkmer's avatar Paul-Christian Volkmer
Browse files

Minor code cleanup und security test

parent 63962361
......@@ -23,16 +23,16 @@ public class UniRepServiceImpl implements UniRepService {
private UniRepDao uniRepDao;
@Override
public IliasCategoryNode getTreeObjects(int refId)
public IliasCategoryNode getTreeObjects(final int refId)
throws ServiceUnavailableException, NotFoundException {
if (uniRepDao == null) {
throw new ServiceUnavailableException();
}
List<IliasCategoryNode> result = uniRepDao.getTreeObjects(refId);
result = this.removeNotMarkedNodes(result, false);
result = removeNotMarkedNodes(result, false);
while(removeBranchesWithoutQuestionPools(result));
while(removeBranchesWithoutQuestionPools(result)) {};
if (result.size() == 1) {
return result.get(0);
......@@ -41,48 +41,53 @@ public class UniRepServiceImpl implements UniRepService {
}
@Override
public List<IliasQuestion> getQuestions(int refId, boolean noRandomQuestions) throws ServiceUnavailableException {
public List<IliasQuestion> getQuestions(
final int refId,
final boolean noRandomQuestions
) throws ServiceUnavailableException {
if (uniRepDao == null) {
throw new ServiceUnavailableException();
}
if(uniRepDao.isRandomQuestionSet(refId) && !noRandomQuestions) {
return this.getRandomQuestions(refId);
return getRandomQuestions(refId);
} else {
return uniRepDao.getQuestion(refId);
}
}
@Override
public List<IliasQuestion> getRandomQuestions(int refId) {
int amountOfQuestions = uniRepDao.getQuestionAmountPerTest(refId);
List<IliasQuestion> allQuestions = uniRepDao.getRandomTestQuestions(refId);
public List<IliasQuestion> getRandomQuestions(final int refId) {
final int amountOfQuestions = uniRepDao.getQuestionAmountPerTest(refId);
final List<IliasQuestion> allQuestions = uniRepDao.getRandomTestQuestions(refId);
Set<Integer> randomIds = new HashSet<>();
final Set<Integer> randomIds = new HashSet<>();
while (randomIds.size() < amountOfQuestions) {
randomIds.add((int)Math.floor(Math.random() * allQuestions.size()));
}
List<IliasQuestion> result = new ArrayList<>();
for (int id : randomIds) {
final List<IliasQuestion> result = new ArrayList<>();
for (final int id : randomIds) {
result.add(allQuestions.get(id));
}
return result;
}
private boolean removeBranchesWithoutQuestionPools( List<IliasCategoryNode> nodes ) throws ServiceUnavailableException {
private boolean removeBranchesWithoutQuestionPools(
final List<IliasCategoryNode> nodes
) throws ServiceUnavailableException {
if (nodes == null) {
return false;
}
Iterator<IliasCategoryNode> it = nodes.iterator();
final Iterator<IliasCategoryNode> it = nodes.iterator();
boolean hasRemovedNodes = false;
while (it.hasNext()) {
IliasCategoryNode node = it.next();
final IliasCategoryNode node = it.next();
if (node == null) {
continue;
......@@ -106,19 +111,22 @@ public class UniRepServiceImpl implements UniRepService {
return hasRemovedNodes;
}
private List<IliasCategoryNode> removeNotMarkedNodes(List<IliasCategoryNode> nodes, boolean isParentMarked) {
Map<String, String> categoryMetaTagRefIds = uniRepDao.getReferenceIdsWithMetaDataFlag("UniRepApp");
Map<String, String> courseMetaTagRefIds = uniRepDao.getReferenceIdsWithMetaDataFlag("UniRepCourse");
private List<IliasCategoryNode> removeNotMarkedNodes(
final List<IliasCategoryNode> nodes,
final boolean isParentMarked
) {
final Map<String, String> categoryMetaTagRefIds = uniRepDao.getReferenceIdsWithMetaDataFlag("UniRepApp");
final Map<String, String> courseMetaTagRefIds = uniRepDao.getReferenceIdsWithMetaDataFlag("UniRepCourse");
if (nodes == null) {
return null;
}
Iterator<IliasCategoryNode> it = nodes.iterator();
final Iterator<IliasCategoryNode> it = nodes.iterator();
while (it.hasNext()) {
IliasCategoryNode node = it.next();
String nodeId = String.valueOf(node.getId());
final IliasCategoryNode node = it.next();
final String nodeId = String.valueOf(node.getId());
if("crs".equals(node.getType())) {
if("yes".equals(courseMetaTagRefIds.get(nodeId))) {
......
......@@ -40,7 +40,7 @@ public class RepositoryTestConfig {
@Bean(name = "configDataSource")
public DriverManagerDataSource configDataSource() throws SQLException {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
dataSource.setUrl("jdbc:hsqldb:mem:testdb");
dataSource.setUsername("test");
......@@ -50,11 +50,11 @@ public class RepositoryTestConfig {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws SQLException {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
final LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(configDataSource());
lef.setJpaVendorAdapter(jpaVendorAdapter());
lef.setPackagesToScan("de.thm.arsnova.connector.persistence.domain");
Properties jpaProperties = new Properties();
final Properties jpaProperties = new Properties();
jpaProperties.put("openjpa.RuntimeUnenhancedClasses", "supported");
lef.setJpaProperties(jpaProperties);
lef.afterPropertiesSet();
......@@ -63,7 +63,7 @@ public class RepositoryTestConfig {
@Bean
public OpenJpaVendorAdapter jpaVendorAdapter() {
OpenJpaVendorAdapter jpaVendorAdapter = new OpenJpaVendorAdapter();
final OpenJpaVendorAdapter jpaVendorAdapter = new OpenJpaVendorAdapter();
jpaVendorAdapter.setShowSql(true);
jpaVendorAdapter.setGenerateDdl(true);
return jpaVendorAdapter;
......@@ -71,7 +71,7 @@ public class RepositoryTestConfig {
@Bean
public PlatformTransactionManager transactionManager() throws SQLException {
JpaTransactionManager txManager = new JpaTransactionManager();
final JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory().getObject());
return txManager;
}
......
......@@ -19,7 +19,7 @@ import de.thm.arsnova.connector.core.RepoPermissionEvaluator;
public class SecurityTestConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("admin")
.password("secret").authorities("ADMIN")
.and().withUser("user").password("secret").authorities("USER");
......@@ -37,7 +37,7 @@ public class SecurityTestConfig extends WebSecurityConfigurerAdapter {
}
@Override
protected void configure(HttpSecurity http) throws Exception {
protected void configure(final HttpSecurity http) throws Exception {
http.csrf().disable();
http.httpBasic();
}
......
......@@ -37,10 +37,10 @@ public class ConfigurationServiceTest {
@Before
public void initDatabase() {
try {
Connection con = dataSource.getConnection();
IDatabaseConnection connection = new DatabaseConnection(con);
final Connection con = dataSource.getConnection();
final IDatabaseConnection connection = new DatabaseConnection(con);
DatabaseOperation.CLEAN_INSERT.execute(connection, getDataSet());
} catch (Exception e) {
} catch (final Exception e) {
e.printStackTrace();
}
}
......@@ -50,26 +50,26 @@ public class ConfigurationServiceTest {
}
private IDataSet getDataSet() throws Exception {
FileInputStream fis = new FileInputStream(new File(
final FileInputStream fis = new FileInputStream(new File(
"src/test/resources/dbunit/internaldb.xml"));
return new XmlDataSet(fis);
}
@Test
public void testShouldReturnConfiguration() {
Configuration actual = configurationService.getConfigurationElement("key2");
final Configuration actual = configurationService.getConfigurationElement("key2");
assertEquals("value2", actual.getValue());
}
@Test
public void testShouldAddAndReadConfiguration() {
Configuration config = new Configuration();
final Configuration config = new Configuration();
config.setKey("Testkey");
config.setValue("Testvalue");
configurationService.setConfigurationElement(config);
Configuration actual = configurationService.getConfigurationElement("Testkey");
final Configuration actual = configurationService.getConfigurationElement("Testkey");
assertEquals(config, actual);
}
}
\ No newline at end of file
......@@ -24,15 +24,15 @@ public class ConnectorServiceDummyTest {
@Test
public void testShouldReturnCourseForEnroledUser() {
Courses courses = connectorService.getCourses("ptsr00");
int actual = courses.getCourse().size();
int expected = 1;
final Courses courses = connectorService.getCourses("ptsr00");
final int actual = courses.getCourse().size();
final int expected = 1;
assertEquals(expected, actual);
}
@Test
public void testShouldReturnMembership() {
Membership membership = connectorService.getMembership("ptsr01", "1");
final Membership membership = connectorService.getMembership("ptsr01", "1");
assertTrue(membership.isMember());
assertEquals(UserRole.MEMBER, membership.getUserrole());
}
......
......@@ -44,18 +44,18 @@ public class ConnectorServiceMoodleTest {
@Before
public void initDatabase() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
createTables(jdbcTemplate);
try {
Connection con = dataSource.getConnection();
IDatabaseConnection connection = new DatabaseConnection(con);
final Connection con = dataSource.getConnection();
final IDatabaseConnection connection = new DatabaseConnection(con);
DatabaseOperation.CLEAN_INSERT.execute(connection, getDataSet());
} catch (Exception e) {
} catch (final Exception e) {
e.printStackTrace();
}
}
private void createTables(JdbcTemplate jdbcTemplate) {
private void createTables(final JdbcTemplate jdbcTemplate) {
jdbcTemplate.execute(
"CREATE TABLE mdl_course ("
+ "id bigint NOT NULL,"
......@@ -108,13 +108,13 @@ public class ConnectorServiceMoodleTest {
}
private IDataSet getDataSet() throws Exception {
FileInputStream fis = new FileInputStream(new File("src/test/resources/dbunit/moodle-datasource.xml"));
final FileInputStream fis = new FileInputStream(new File("src/test/resources/dbunit/moodle-datasource.xml"));
return new XmlDataSet(fis);
}
@After
public void cleanupDatabase() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("DROP TABLE mdl_course");
jdbcTemplate.execute("DROP TABLE mdl_user");
......@@ -126,25 +126,25 @@ public class ConnectorServiceMoodleTest {
@Test
public void testShouldNotReturnCourseForNotEnroledUser() {
Courses courses = connectorService.getCourses("admin");
int actual = courses.getCourse().size();
int expected = 0;
final Courses courses = connectorService.getCourses("admin");
final int actual = courses.getCourse().size();
final int expected = 0;
assertEquals(expected, actual);
}
@Test
public void testShouldReturnCoursesForEnroledUser() {
Courses courses = connectorService.getCourses("ptsr00");
int actual = courses.getCourse().size();
int expected = 1;
final Courses courses = connectorService.getCourses("ptsr00");
final int actual = courses.getCourse().size();
final int expected = 1;
assertEquals(expected, actual);
}
@Test
public void testShouldReturnMembershipForEnroledUser() {
Membership membership = connectorService.getMembership("ptsr00", "1");
final Membership membership = connectorService.getMembership("ptsr00", "1");
assertTrue(membership.isMember());
assertEquals(UserRole.TEACHER, membership.getUserrole());
......@@ -152,51 +152,51 @@ public class ConnectorServiceMoodleTest {
@Test
public void testShouldNotReturnCoursesForNonexistantUsers() {
Courses courses = connectorService.getCourses("iamnothere");
int actual = courses.getCourse().size();
int expected = 0;
final Courses courses = connectorService.getCourses("iamnothere");
final int actual = courses.getCourse().size();
final int expected = 0;
assertEquals(expected, actual);
}
@Test
public void testShouldIndicateAnEnroledUser() {
Membership membership = connectorService.getMembership("ptsr00", "1");
final Membership membership = connectorService.getMembership("ptsr00", "1");
assertTrue(membership.isMember());
assertNotNull(membership.getUserrole());
}
@Test
public void testShouldNotIndicateUnenroledUsers() {
Membership membership = connectorService.getMembership("ptsr01", "1");
final Membership membership = connectorService.getMembership("ptsr01", "1");
assertFalse(membership.isMember());
assertNull(membership.getUserrole());
}
@Test
public void testShouldReturnFalseOnUnknownCourse() {
Membership membership = connectorService.getMembership("ptsr00", "12345678");
final Membership membership = connectorService.getMembership("ptsr00", "12345678");
assertFalse(membership.isMember());
}
@Test
public void testShouldReturnFalseOnUnknownUser() {
Membership membership = connectorService.getMembership("iamnothere", "1");
final Membership membership = connectorService.getMembership("iamnothere", "1");
assertFalse(membership.isMember());
}
@Test
public void testShouldReturnCorrectCourseType() {
Courses courses = connectorService.getCourses("ptsr00");
String actual = courses.getCourse().get(0).getType();
String expected = "moodle";
final Courses courses = connectorService.getCourses("ptsr00");
final String actual = courses.getCourse().get(0).getType();
final String expected = "moodle";
assertEquals(expected, actual);
}
@Test
public void testShouldReturnCorrectCourseData() {
Courses courses = connectorService.getCourses("ptsr00");
final Courses courses = connectorService.getCourses("ptsr00");
String actual = courses.getCourse().get(0).getShortname();
String expected = "TK1";
assertEquals(expected, actual);
......@@ -208,8 +208,8 @@ public class ConnectorServiceMoodleTest {
@Test
public void testShouldReturnCorrectCourseMembershipForTeacher() {
Courses courses = connectorService.getCourses("ptsr00");
Membership actual = courses.getCourse().get(0).getMembership();
final Courses courses = connectorService.getCourses("ptsr00");
final Membership actual = courses.getCourse().get(0).getMembership();
assertTrue(actual.isMember());
assertEquals(UserRole.TEACHER, actual.getUserrole());
......@@ -217,8 +217,8 @@ public class ConnectorServiceMoodleTest {
@Test
public void testShouldReturnCorrectCourseMembershipForMember() {
Courses courses = connectorService.getCourses("ptsr02");
Membership actual = courses.getCourse().get(0).getMembership();
final Courses courses = connectorService.getCourses("ptsr02");
final Membership actual = courses.getCourse().get(0).getMembership();
assertTrue(actual.isMember());
assertEquals(UserRole.MEMBER, actual.getUserrole());
......@@ -226,7 +226,7 @@ public class ConnectorServiceMoodleTest {
@Test
public void testShouldNotIndicateEnroledUserButInvisibleCourse() {
Membership membership = connectorService.getMembership("ptsr02", "4");
final Membership membership = connectorService.getMembership("ptsr02", "4");
assertFalse(membership.isMember());
assertNull(membership.getUserrole());
}
......
......@@ -44,18 +44,18 @@ public class ConnectorServiceStudipTest {
@Before
public void initDatabase() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
createTables(jdbcTemplate);
try {
Connection con = dataSource.getConnection();
IDatabaseConnection connection = new DatabaseConnection(con);
final Connection con = dataSource.getConnection();
final IDatabaseConnection connection = new DatabaseConnection(con);
DatabaseOperation.CLEAN_INSERT.execute(connection, getDataSet());
} catch (Exception e) {
} catch (final Exception e) {
e.printStackTrace();
}
}
private void createTables(JdbcTemplate jdbcTemplate) {
private void createTables(final JdbcTemplate jdbcTemplate) {
jdbcTemplate.execute(
"CREATE TABLE seminare ("
+ "Seminar_ID varchar(254) NOT NULL,"
......@@ -81,13 +81,13 @@ public class ConnectorServiceStudipTest {
}
private IDataSet getDataSet() throws Exception {
FileInputStream fis = new FileInputStream(new File("src/test/resources/dbunit/studip-datasource.xml"));
final FileInputStream fis = new FileInputStream(new File("src/test/resources/dbunit/studip-datasource.xml"));
return new XmlDataSet(fis);
}
@After
public void cleanupDatabase() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("DROP TABLE seminare");
jdbcTemplate.execute("DROP TABLE auth_user_md5");
......@@ -96,25 +96,25 @@ public class ConnectorServiceStudipTest {
@Test
public void testShouldNotReturnCourseForNotEnroledUser() {
Courses courses = connectorService.getCourses("ptsr01");
int actual = courses.getCourse().size();
int expected = 0;
final Courses courses = connectorService.getCourses("ptsr01");
final int actual = courses.getCourse().size();
final int expected = 0;
assertEquals(expected, actual);
}
@Test
public void testShouldReturnCourseForEnroledUser() {
Courses courses = connectorService.getCourses("ptsr00");
int actual = courses.getCourse().size();
int expected = 1;
final Courses courses = connectorService.getCourses("ptsr00");
final int actual = courses.getCourse().size();
final int expected = 1;
assertEquals(expected, actual);
}
@Test
public void testShouldReturnMembershipForEnroledUser() {
Membership membership = connectorService.getMembership("ptsr00", "2");
final Membership membership = connectorService.getMembership("ptsr00", "2");
assertTrue(membership.isMember());
assertEquals(UserRole.MEMBER, membership.getUserrole());
......@@ -122,51 +122,51 @@ public class ConnectorServiceStudipTest {
@Test
public void testShouldNotReturnCoursesForNonexistantUsers() {
Courses courses = connectorService.getCourses("iamnothere");
int actual = courses.getCourse().size();
int expected = 0;
final Courses courses = connectorService.getCourses("iamnothere");
final int actual = courses.getCourse().size();
final int expected = 0;
assertEquals(expected, actual);
}
@Test
public void testShouldIndicateAnEnroledUser() {
Membership membership = connectorService.getMembership("admin", "1");
final Membership membership = connectorService.getMembership("admin", "1");
assertTrue(membership.isMember());
assertNotNull(membership.getUserrole());
}
@Test
public void testShouldNotIndicateUnenroledUsers() {
Membership membership = connectorService.getMembership("ptsr01", "1");
final Membership membership = connectorService.getMembership("ptsr01", "1");
assertFalse(membership.isMember());
assertNull(membership.getUserrole());
}
@Test
public void testShouldReturnFalseOnUnknownCourse() {
Membership membership = connectorService.getMembership("ptsr00", "12345678");
final Membership membership = connectorService.getMembership("ptsr00", "12345678");
assertFalse(membership.isMember());
}
@Test
public void testShouldReturnFalseOnUnknownUser() {
Membership membership = connectorService.getMembership("iamnothere", "1");
final Membership membership = connectorService.getMembership("iamnothere", "1");
assertFalse(membership.isMember());
}
@Test
public void testShouldReturnCorrectCourseType() {
Courses courses = connectorService.getCourses("ptsr00");
String actual = courses.getCourse().get(0).getType();
String expected = "studip";
final Courses courses = connectorService.getCourses("ptsr00");
final String actual = courses.getCourse().get(0).getType();
final String expected = "studip";
assertEquals(expected, actual);
}
@Test
public void testShouldReturnCorrectCourseData() {
Courses courses = connectorService.getCourses("ptsr00");
final Courses courses = connectorService.getCourses("ptsr00");
String actual = courses.getCourse().get(0).getShortname();
String expected = "Testkurs 2";
assertEquals(expected, actual);
......@@ -178,8 +178,8 @@ public class ConnectorServiceStudipTest {
@Test
public void testShouldReturnCorrectCourseMembership() {
Courses courses = connectorService.getCourses("admin");
Membership actual = courses.getCourse().get(0).getMembership();
final Courses courses = connectorService.getCourses("admin");
final Membership actual = courses.getCourse().get(0).getMembership();
assertTrue(actual.isMember());
assertEquals(UserRole.TEACHER, actual.getUserrole());
......
......@@ -37,10 +37,10 @@ public class EnabledCategoryServiceTest {
@Before
public void initDatabase() {
try {
Connection con = dataSource.getConnection();
IDatabaseConnection connection = new DatabaseConnection(con);
final Connection con = dataSource.getConnection();
final IDatabaseConnection connection = new DatabaseConnection(con);
DatabaseOperation.CLEAN_INSERT.execute(connection, getDataSet());
} catch (Exception e) {
} catch (final Exception e) {
e.printStackTrace();
}
}
...