Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(4.x) Migrate to JUnit5 #451

Open
wants to merge 2 commits into
base: dgtkit-4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices;
import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices;
import org.springframework.security.web.context.SecurityContextHolderFilter;

import static org.devgateway.toolkit.web.WebConstants.FORMS_BASE_PATH;

Expand All @@ -45,7 +46,7 @@ public class FormsSecurityConfig extends WebSecurityConfig {
* resources
*/
@Override
public void configure(final WebSecurity web) throws Exception {
public void configure(final WebSecurity web) {
super.configure(web);
web.ignoring().antMatchers("/img/**", "/css*/**", "/js*/**", "/assets*/**", "/favicon.ico", "/resources/**",
"/resources/public/**");
Expand Down Expand Up @@ -87,8 +88,9 @@ public AbstractRememberMeServices rememberMeServices() {
}

@Override
protected void configure(final HttpSecurity http) throws Exception {
super.configure(http);
protected void configure(final HttpSecurity http, final SecurityContextHolderFilter securityContextHolderFilter)
throws Exception {
super.configure(http, securityContextHolderFilter);

// we do not allow anyonymous token. When
// enabled this basically means any guest
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.devgateway.toolkit.forms.util;

import org.apache.wicket.request.cycle.RequestCycle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author Nadejda Mandrescu
*/
public final class PageUtil {
private PageUtil() {
}

public static HttpServletRequest getHttpServletRequest() {
return (HttpServletRequest) RequestCycle.get().getRequest().getContainerRequest();
}

public static HttpServletResponse getHttpServletResponse() {
return (HttpServletResponse) RequestCycle.get().getResponse().getContainerResponse();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.wicket.request.Request;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.devgateway.toolkit.forms.util.PageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
Expand All @@ -31,6 +32,7 @@
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;

import java.util.Collection;

Expand All @@ -56,6 +58,9 @@ public class SSAuthenticatedWebSession extends AuthenticatedWebSession {
@SpringBean
private AuthenticationManager authenticationManager;

@SpringBean
private HttpSessionSecurityContextRepository securityContextRepository;

@SpringBean
private RoleHierarchy roleHierarchy;

Expand All @@ -64,7 +69,7 @@ public class SSAuthenticatedWebSession extends AuthenticatedWebSession {

/*
* (non-Javadoc)
*
*
* @see org.apache.wicket.Session#replaceSession()
*/
@Override
Expand Down Expand Up @@ -99,6 +104,11 @@ public boolean authenticate(final String username, final String password) {
Authentication authentication =
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
SecurityContextHolder.getContext().setAuthentication(authentication);

// (since Spring Security 5.7) explicitly save context, no longer done by security filter
securityContextRepository.saveContext(SecurityContextHolder.getContext(), PageUtil.getHttpServletRequest(),
PageUtil.getHttpServletResponse());

// httpSession.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
// SecurityContextHolder.getContext());
authenticated = authentication.isAuthenticated();
Expand Down Expand Up @@ -129,7 +139,7 @@ public Roles getRoles() {
/**
* Gets the Spring roles and dumps them into Wicket's {@link Roles} object,
* only if the user is signed in
*
*
* @see {@link #isSignedIn()}
* @see #addRolesFromAuthentication(Roles, Authentication)
* @param roles
Expand Down
5 changes: 0 additions & 5 deletions persistence-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.devgateway.toolkit.persistence.mongo;

import org.devgateway.toolkit.persistence.mongo.spring.MongoPersistenceApplication;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@ActiveProfiles("integration")
@SpringBootTest(classes = { MongoPersistenceApplication.class })
@TestPropertySource("classpath:test.properties")
Expand Down
6 changes: 4 additions & 2 deletions persistence/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<!-- matching the version of junit.jupiter provided by spring-boot-test-starter -->
<version>1.8.2</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.List;

Expand Down Expand Up @@ -58,9 +58,9 @@ public void writeCell() throws Exception {
excelSheet.writeCell("text", row, 2);
excelSheet.writeCell(1, row, 3);

Assert.assertEquals(CellType.BLANK, row.getCell(0).getCellType());
Assert.assertEquals("Yes", row.getCell(1).getStringCellValue());
Assert.assertEquals(CellType.STRING, row.getCell(2).getCellType());
Assert.assertEquals(CellType.NUMERIC, row.getCell(3).getCellType());
Assertions.assertEquals(CellType.BLANK, row.getCell(0).getCellType());
Assertions.assertEquals("Yes", row.getCell(1).getStringCellValue());
Assertions.assertEquals(CellType.STRING, row.getCell(2).getCellType());
Assertions.assertEquals(CellType.NUMERIC, row.getCell(3).getCellType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import org.devgateway.toolkit.persistence.excel.annotation.ExcelImport;
import org.devgateway.toolkit.persistence.excel.info.ClassFields;
import org.devgateway.toolkit.persistence.excel.info.ClassFieldsDefault;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.util.ArrayList;
Expand Down Expand Up @@ -45,13 +45,13 @@ public void getFieldType() throws Exception {
final Iterator<Field> fields = classFields.getFields();

final Field firstField = fields.next(); // get first element
Assert.assertEquals("Check basic field type", FieldTypeImport.basic, ExcelFieldImportService.getFieldType(firstField));
Assertions.assertEquals(FieldTypeImport.basic, ExcelFieldImportService.getFieldType(firstField), "Check basic field type");

final Field thirdField = fields.next(); // get second element
Assert.assertEquals("Check basic field type for Object", FieldTypeImport.object, ExcelFieldImportService.getFieldType(thirdField));
Assertions.assertEquals(FieldTypeImport.object, ExcelFieldImportService.getFieldType(thirdField), "Check basic field type for Object");

final Field fourthField = fields.next(); // get third element
Assert.assertEquals("Check importByProp field type Object", FieldTypeImport.objectImportByProp, ExcelFieldImportService.getFieldType(fourthField));
Assertions.assertEquals(FieldTypeImport.objectImportByProp, ExcelFieldImportService.getFieldType(fourthField), "Check importByProp field type Object");
}

@Test
Expand All @@ -66,6 +66,6 @@ public void getFields() throws Exception {
actualFields.add(f.getName());
}

Assert.assertArrayEquals("Check get fields", expectedFields, actualFields.toArray());
Assertions.assertArrayEquals(expectedFields, actualFields.toArray(), "Check get fields");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import org.devgateway.toolkit.persistence.excel.annotation.ExcelExport;
import org.devgateway.toolkit.persistence.excel.info.ClassFields;
import org.devgateway.toolkit.persistence.excel.info.ClassFieldsDefault;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.Persistable;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -58,16 +58,16 @@ public void getFieldType() throws Exception {
final Iterator<Field> fields = classFields.getFields();

final Field firstField = fields.next(); // get first element
Assert.assertEquals("Check basic field type", FieldType.basic, ExcelFieldService.getFieldType(firstField));
Assertions.assertEquals(FieldType.basic, ExcelFieldService.getFieldType(firstField), "Check basic field type");

final Field secondField = fields.next(); // get second element
Assert.assertEquals("Check type class for a List", FieldType.basic, ExcelFieldService.getFieldType(secondField));
Assertions.assertEquals(FieldType.basic, ExcelFieldService.getFieldType(secondField), "Check type class for a List");

final Field thirdField = fields.next(); // get third element
Assert.assertEquals("Check basic field type for Object", FieldType.basic, ExcelFieldService.getFieldType(thirdField));
Assertions.assertEquals(FieldType.basic, ExcelFieldService.getFieldType(thirdField), "Check basic field type for Object");

final Field fourthField = fields.next(); // get fourth element
Assert.assertEquals("Check objectSeparateSheet field type Object", FieldType.objectSeparateSheet, ExcelFieldService.getFieldType(fourthField));
Assertions.assertEquals(FieldType.objectSeparateSheet, ExcelFieldService.getFieldType(fourthField), "Check objectSeparateSheet field type Object");
}

@Test
Expand All @@ -76,11 +76,11 @@ public void getFieldClass() throws Exception {
final Iterator<Field> fields = classFields.getFields();

final Field firstField = fields.next(); // get first element
Assert.assertEquals("Check basic field class", Long.class, ExcelFieldService.getFieldClass(firstField));
Assertions.assertEquals(Long.class, ExcelFieldService.getFieldClass(firstField), "Check basic field class");


final Field secondField = fields.next(); // get second element
Assert.assertEquals("Check field class for a List", String.class, ExcelFieldService.getFieldClass(secondField));
Assertions.assertEquals(String.class, ExcelFieldService.getFieldClass(secondField), "Check field class for a List");
}

@Test
Expand All @@ -95,16 +95,16 @@ public void getFields() throws Exception {
actualFields.add(f.getName());
}

Assert.assertArrayEquals("Check get fields", expectedFields, actualFields.toArray());
Assertions.assertArrayEquals(expectedFields, actualFields.toArray(), "Check get fields");
}

@Test
public void getObjectID() throws Exception {
final OtherClass otherClass = new OtherClass();
Assert.assertEquals("Check object ID", Long.valueOf(-1), ExcelFieldService.getObjectID(otherClass));
Assertions.assertEquals(Long.valueOf(-1), ExcelFieldService.getObjectID(otherClass), "Check object ID");

final TestClass testclass = new TestClass((long) 10);
Assert.assertEquals("Check object ID", Long.valueOf(10), ExcelFieldService.getObjectID(testclass));
Assertions.assertEquals(Long.valueOf(10), ExcelFieldService.getObjectID(testclass), "Check object ID");
}

@Test
Expand All @@ -113,9 +113,9 @@ public void getFieldName() throws Exception {
final Iterator<Field> fields = classFields.getFields();

final Field firstField = fields.next(); // get first element
Assert.assertEquals("Check field name", "valid", ExcelFieldService.getFieldName(null, firstField, null));
Assertions.assertEquals("valid", ExcelFieldService.getFieldName(null, firstField, null), "Check field name");

final Field secondField = fields.next(); // get second element
Assert.assertEquals("Check field name", "money", ExcelFieldService.getFieldName(null, secondField, null));
Assertions.assertEquals("money", ExcelFieldService.getFieldName(null, secondField, null), "Check field name");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import org.apache.poi.ss.usermodel.Workbook;
import org.devgateway.toolkit.persistence.excel.annotation.ExcelExport;
import org.devgateway.toolkit.persistence.excel.service.TranslateService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Persistable;
Expand Down Expand Up @@ -36,27 +36,27 @@ public void createWorkbook() throws Exception {
// workbook.write(outputStream);
// }

Assert.assertEquals("Number of sheets", 3, workbook.getNumberOfSheets());
Assertions.assertEquals(3, workbook.getNumberOfSheets(), "Number of sheets");

Assert.assertNotNull("buyer sheet", workbook.getSheet("testbuyer"));
Assert.assertNotNull("contract sheet", workbook.getSheet("testcontract"));
Assert.assertNotNull("document sheet", workbook.getSheet("testdocument"));
Assertions.assertNotNull(workbook.getSheet("testbuyer"), "buyer sheet");
Assertions.assertNotNull(workbook.getSheet("testcontract"), "contract sheet");
Assertions.assertNotNull(workbook.getSheet("testdocument"), "document sheet");

Assert.assertEquals("buyer name", "buyer 1",
workbook.getSheet("testbuyer").getRow(1).getCell(0).toString());
Assert.assertEquals("buyer classification", "TP-1 - alii aliquam",
workbook.getSheet("testbuyer").getRow(2).getCell(8).toString());
Assertions.assertEquals("buyer 1", workbook.getSheet("testbuyer").getRow(1).getCell(0).toString(),
"buyer name");
Assertions.assertEquals("TP-1 - alii aliquam", workbook.getSheet("testbuyer").getRow(2).getCell(8).toString(),
"buyer classification");

Assert.assertEquals("contract parent", "testbuyer - 1",
workbook.getSheet("testcontract").getRow(1).getCell(0).toString());
Assert.assertEquals("contract amount", 1000,
workbook.getSheet("testcontract").getRow(1).getCell(2).getNumericCellValue(), 0.0);
Assertions.assertEquals("testbuyer - 1", workbook.getSheet("testcontract").getRow(1).getCell(0).toString(),
"contract parent");
Assertions.assertEquals(1000, workbook.getSheet("testcontract").getRow(1).getCell(2).getNumericCellValue(), 0.0,
"contract amount");


Assert.assertEquals("document number of rows", 2, workbook.getSheet("testdocument").getLastRowNum());
Assertions.assertEquals(2, workbook.getSheet("testdocument").getLastRowNum(), "document number of rows");

Assert.assertEquals("buyer address flatten", "Street 1 | Street 2",
workbook.getSheet("testbuyer").getRow(1).getCell(4).toString());
Assertions.assertEquals("Street 1 | Street 2", workbook.getSheet("testbuyer").getRow(1).getCell(4).toString(),
"buyer address flatten");
}

private List createObjects() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import org.devgateway.toolkit.persistence.excel.info.ImportBean;
import org.devgateway.toolkit.persistence.excel.test.TestAddressRepository;
import org.devgateway.toolkit.persistence.excel.test.TestBuyer;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
Expand Down Expand Up @@ -35,7 +35,7 @@
* @since 21/03/2018
*/
public class ExcelFileImportDefaultTest {
@BeforeClass
@BeforeAll
public static void setErrorLogging() {
LoggingSystem.get(ClassLoader.getSystemClassLoader()).setLogLevel(ROOT_LOGGER_NAME, LogLevel.WARN);
}
Expand All @@ -49,11 +49,11 @@ public void readWorkbook() throws Exception {
final ImportResponse importResponse = excelFileImport.readWorkbook();

TestBuyer testBuyer = (TestBuyer) importResponse.getObjects().get(0);
Assert.assertEquals("check number of objects", 2, importResponse.getObjects().size());
Assert.assertEquals("check buyer name", "buyer 1", testBuyer.getName());
Assert.assertEquals("check organization name", "organization 1", testBuyer.getOrg().getName());
Assert.assertEquals("check address", "Street 1", testBuyer.getOrg().getAddress().getStreet());
Assert.assertEquals("check address", "Romania", testBuyer.getOrg().getAddress().getCountry());
Assertions.assertEquals(2, importResponse.getObjects().size(), "check number of objects");
Assertions.assertEquals("buyer 1", testBuyer.getName(), "check buyer name");
Assertions.assertEquals("organization 1", testBuyer.getOrg().getName(), "check organization name");
Assertions.assertEquals("Street 1", testBuyer.getOrg().getAddress().getStreet(), "check address");
Assertions.assertEquals("Romania", testBuyer.getOrg().getAddress().getCountry(), "check address");
}

/**
Expand Down
Loading