From cd61bfbb1f865456ca609b807aaba40d6d1e13b2 Mon Sep 17 00:00:00 2001 From: Christian Kollmann Date: Tue, 12 Jan 2021 15:00:43 +0100 Subject: Improve readability of test cases by using modern syntax --- .../v2/test/tasks/InitialSearchTaskFirstTest.java | 151 +++++++++------------ 1 file changed, 67 insertions(+), 84 deletions(-) (limited to 'eidas_modules/authmodule-eIDAS-v2') diff --git a/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/tasks/InitialSearchTaskFirstTest.java b/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/tasks/InitialSearchTaskFirstTest.java index 12a0969d..35e2e56e 100644 --- a/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/tasks/InitialSearchTaskFirstTest.java +++ b/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/tasks/InitialSearchTaskFirstTest.java @@ -70,6 +70,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Random; +import static org.junit.Assert.assertThrows; + @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/SpringTest-context_tasks_test.xml") @DirtiesContext(classMode = ClassMode.BEFORE_CLASS) @@ -82,17 +84,15 @@ public class InitialSearchTaskFirstTest { private IErnpClient ernpClient; final ExecutionContext executionContext = new ExecutionContextImpl(); - private MockHttpServletRequest httpReq; - private MockHttpServletResponse httpResp; private TestRequestImpl pendingReq; - private String randomIdentifier = RandomStringUtils.randomNumeric(10); - private String randomFamilyName = RandomStringUtils.randomNumeric(11); - private String randomGivenName = RandomStringUtils.randomNumeric(12); - private String randomPlaceOfBirth = RandomStringUtils.randomNumeric(12); - private String randomBirthName = RandomStringUtils.randomNumeric(12); - private String randomDate = "2011-01-"+ (10 + new Random().nextInt(18)); - private String DE_ST = "de/st/"; - private String IT_ST = "it/st/"; + private final String randomIdentifier = RandomStringUtils.randomNumeric(10); + private final String randomFamilyName = RandomStringUtils.randomNumeric(11); + private final String randomGivenName = RandomStringUtils.randomNumeric(12); + private final String randomPlaceOfBirth = RandomStringUtils.randomNumeric(12); + private final String randomBirthName = RandomStringUtils.randomNumeric(12); + private final String randomDate = "2011-01-"+ (10 + new Random().nextInt(18)); + private final String DE_ST = "de/st/"; + private final String IT_ST = "it/st/"; /** * jUnit class initializer. @@ -112,10 +112,10 @@ public class InitialSearchTaskFirstTest { @Before public void setUp() throws URISyntaxException, EaafStorageException { MockitoAnnotations.initMocks(this); - task = new InitialSearchTask(new ArrayList(), ernpClient, zmrClient); + task = new InitialSearchTask(new ArrayList<>(), ernpClient, zmrClient); - httpReq = new MockHttpServletRequest("POST", "https://localhost/authhandler"); - httpResp = new MockHttpServletResponse(); + MockHttpServletRequest httpReq = new MockHttpServletRequest("POST", "https://localhost/authhandler"); + MockHttpServletResponse httpResp = new MockHttpServletResponse(); RequestContextHolder.resetRequestAttributes(); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(httpReq, httpResp)); @@ -125,11 +125,11 @@ public class InitialSearchTaskFirstTest { .setGenericDataToSession(Constants.DATA_FULL_EIDAS_RESPONSE, response); } - @Test - @DirtiesContext /** * One match, but register update needed */ + @Test + @DirtiesContext public void testNode100_UserIdentifiedUpdateNecessary_a() throws Exception { ArrayList zmrResult = new ArrayList<>(); String randomBpk = RandomStringUtils.randomNumeric(6); @@ -143,14 +143,14 @@ public class InitialSearchTaskFirstTest { task.execute(pendingReq, executionContext); String bPk = (String) pendingReq.getSessionData(AuthProcessDataWrapper.class).getGenericDataFromSession(Constants.DATA_RESULT_MATCHING_BPK); - Assert.assertTrue("Wrong bpk", bPk.equals(randomBpk)); + Assert.assertEquals("Wrong bpk", bPk, randomBpk); } - @Test - @DirtiesContext /** * One match, but register update needed */ + @Test + @DirtiesContext public void testNode100_UserIdentifiedUpdateNecessary_b() throws TaskExecutionException { ArrayList zmrResult = new ArrayList<>(); Mockito.when(zmrClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(zmrResult); @@ -163,17 +163,16 @@ public class InitialSearchTaskFirstTest { task.execute(pendingReq, executionContext); String bPk = (String) pendingReq.getSessionData(AuthProcessDataWrapper.class).getGenericDataFromSession(Constants.DATA_RESULT_MATCHING_BPK); - Assert.assertTrue("Wrong bpk", bPk.equals(randomBpk)); - + Assert.assertEquals("Wrong bpk", bPk, randomBpk); } - @Test - @DirtiesContext /** * Two matches found in ZMR */ - public void testNode101_ManualFixNecessary_a() throws Exception { + @Test + @DirtiesContext + public void testNode101_ManualFixNecessary_a() { ArrayList zmrResult = new ArrayList<>(); zmrResult.add(new RegisterResult("bpkMax", DE_ST+randomIdentifier, randomGivenName, randomFamilyName, randomDate)); zmrResult.add(new RegisterResult("bpkMax", DE_ST+randomIdentifier, "Maximilian", randomFamilyName, randomDate)); @@ -182,22 +181,19 @@ public class InitialSearchTaskFirstTest { ArrayList ernpResult = new ArrayList<>(); Mockito.when(ernpClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(ernpResult); - try { - task.execute(pendingReq, executionContext); - Assert.assertTrue("Wrong workflow, should not reach this point/ get a bpk", false); - } catch (final TaskExecutionException e) { - Throwable origE = e.getOriginalException(); - Assert.assertTrue("Wrong exception", (origE.getCause() instanceof ManualFixNecessaryException)); - } + TaskExecutionException exception = assertThrows(TaskExecutionException.class, + () -> task.execute(pendingReq, executionContext)); + Throwable origE = exception.getOriginalException(); + Assert.assertTrue("Wrong exception", (origE.getCause() instanceof ManualFixNecessaryException)); } - @Test - @DirtiesContext /** * Two matches found in ErnP */ - public void testNode101_ManualFixNecessary_b() throws Exception { + @Test + @DirtiesContext + public void testNode101_ManualFixNecessary_b() { String randombpk = RandomStringUtils.random(5); ArrayList zmrResult = new ArrayList<>(); Mockito.when(zmrClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(zmrResult); @@ -209,20 +205,17 @@ public class InitialSearchTaskFirstTest { randomDate)); Mockito.when(ernpClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(ernpResult); - try { - task.execute(pendingReq, executionContext); - Assert.assertTrue("Wrong workflow, should not reach this point/ get a bpk", false); - } catch (final TaskExecutionException e) { - Throwable origE = e.getOriginalException(); - Assert.assertTrue("Wrong exception", (origE.getCause() instanceof ManualFixNecessaryException)); - } + TaskExecutionException exception = assertThrows(TaskExecutionException.class, + () -> task.execute(pendingReq, executionContext)); + Throwable origE = exception.getOriginalException(); + Assert.assertTrue("Wrong exception", (origE.getCause() instanceof ManualFixNecessaryException)); } - @Test - @DirtiesContext /** * One match, no register update needed */ + @Test + @DirtiesContext public void testNode102_UserIdentified_a() throws Exception { String randomBpk = RandomStringUtils.randomNumeric(12); ArrayList zmrResult = new ArrayList<>(); @@ -235,14 +228,14 @@ public class InitialSearchTaskFirstTest { task.execute(pendingReq, executionContext); String bPk = (String) pendingReq.getSessionData(AuthProcessDataWrapper.class).getGenericDataFromSession(Constants.DATA_RESULT_MATCHING_BPK); - Assert.assertTrue("Wrong bpk", bPk.equals(randomBpk)); + Assert.assertEquals("Wrong bpk", bPk, randomBpk); } - @Test - @DirtiesContext /** * One match, no register update needed */ + @Test + @DirtiesContext public void testNode102_UserIdentified_b() throws Exception { String randomBpk = RandomStringUtils.randomNumeric(14); @@ -256,14 +249,14 @@ public class InitialSearchTaskFirstTest { task.execute(pendingReq, executionContext); String bPk = (String) pendingReq.getSessionData(AuthProcessDataWrapper.class).getGenericDataFromSession(Constants.DATA_RESULT_MATCHING_BPK); - Assert.assertTrue("Wrong bpk", bPk.equals(randomBpk)); + Assert.assertEquals("Wrong bpk", bPk, randomBpk); } - @Test - @DirtiesContext /** * One match found in ZMR and ErnP with detail search */ + @Test + @DirtiesContext public void testNode103_UserIdentified_IT() throws Exception { String bpkRegister = RandomStringUtils.randomNumeric(14); String taxNumber = RandomStringUtils.randomNumeric(14); @@ -291,22 +284,18 @@ public class InitialSearchTaskFirstTest { handlers.add(it); task = new InitialSearchTask(handlers, ernpClient, zmrClient); - try { - task.execute(pendingReq1, executionContext); + task.execute(pendingReq1, executionContext); - String bPk = (String) - pendingReq1.getSessionData(AuthProcessDataWrapper.class).getGenericDataFromSession(Constants.DATA_RESULT_MATCHING_BPK); - Assert.assertTrue("Wrong bpk", bPk.equals(bpkRegister)); - } catch (final TaskExecutionException e) { - Assert.assertTrue("Wrong workflow, should not reach this point", false); - } + String bPk = (String) + pendingReq1.getSessionData(AuthProcessDataWrapper.class).getGenericDataFromSession(Constants.DATA_RESULT_MATCHING_BPK); + Assert.assertEquals("Wrong bpk", bPk, bpkRegister); } - @Test - @DirtiesContext /** * Multiple matches found in ZMR and ErnP with detail search */ + @Test + @DirtiesContext public void testNode103_UserIdentified_DE() throws Exception { String givenName = randomGivenName; String familyName = randomFamilyName; @@ -339,22 +328,18 @@ public class InitialSearchTaskFirstTest { handlers.add(de); task = new InitialSearchTask(handlers, ernpClient, zmrClient); - try { - task.execute(pendingReq1, executionContext); + task.execute(pendingReq1, executionContext); - String bPk = (String) - pendingReq1.getSessionData(AuthProcessDataWrapper.class).getGenericDataFromSession(Constants.DATA_RESULT_MATCHING_BPK); - Assert.assertTrue("Wrong bpk", bPk.equals(bpk)); - } catch (final TaskExecutionException e) { - Assert.assertTrue("Wrong workflow, should not reach this point", false); - } + String bPk = (String) + pendingReq1.getSessionData(AuthProcessDataWrapper.class).getGenericDataFromSession(Constants.DATA_RESULT_MATCHING_BPK); + Assert.assertEquals("Wrong bpk", bPk, bpk); } - @Test - @DirtiesContext /** * Multiple matches found in ZMR and ErnP with detail search */ + @Test + @DirtiesContext public void testNode104_ManualFixNecessary_DE() throws Exception { String givenName = randomGivenName; String familyName = randomFamilyName; @@ -393,20 +378,20 @@ public class InitialSearchTaskFirstTest { try { task.execute(pendingReq1, executionContext); - Assert.assertTrue("Wrong workflow, should not reach this point/ get a bpk", false); + Assert.fail("Wrong workflow, should not reach this point/ get a bpk"); } catch (final TaskExecutionException e) { Throwable origE = e.getOriginalException(); Assert.assertTrue("Wrong exception", (origE.getCause() instanceof ManualFixNecessaryException)); } } - @Test - @DirtiesContext /** * Multiple matches found in ZMR and ErnP with detail search */ + @Test + @DirtiesContext public void testNode104_ManualFixNecessary_IT() throws Exception { - String fakeTaxNumber = RandomStringUtils.randomNumeric(14);; + String fakeTaxNumber = RandomStringUtils.randomNumeric(14); final AuthenticationResponse response = buildDummyAuthResponseRandomPersonIT_Tax(fakeTaxNumber); TestRequestImpl pendingReq1 = new TestRequestImpl(); pendingReq1.getSessionData(AuthProcessDataWrapper.class) @@ -432,20 +417,18 @@ public class InitialSearchTaskFirstTest { handlers.add(it); task = new InitialSearchTask(handlers, ernpClient, zmrClient); - try { - task.execute(pendingReq1, executionContext); - Assert.assertTrue("Wrong workflow, should not reach this point/ get a bpk", false); - } catch (final TaskExecutionException e) { - Throwable origE = e.getOriginalException(); - Assert.assertTrue("Wrong exception", (origE.getCause() instanceof ManualFixNecessaryException)); - } + + TaskExecutionException exception = assertThrows(TaskExecutionException.class, + () -> task.execute(pendingReq1, executionContext)); + Throwable origE = exception.getOriginalException(); + Assert.assertTrue("Wrong exception", (origE.getCause() instanceof ManualFixNecessaryException)); } - @Test - @DirtiesContext /** * NO match found in ZMR and ErnP with Initial search */ + @Test + @DirtiesContext public void testNode105_TemporaryEnd() throws TaskExecutionException { ArrayList zmrResult = new ArrayList<>(); Mockito.when(zmrClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(zmrResult); @@ -509,17 +492,17 @@ public class InitialSearchTaskFirstTest { "afaf").attributes(attributeMap).build(); } - private AttributeDefinition generateStringAttribute(String friendlyName, String fragment, String prefix) throws URISyntaxException { + private AttributeDefinition generateStringAttribute(String friendlyName, String fragment, String prefix) throws URISyntaxException { return generateAttribute(friendlyName, fragment, prefix, "eu.eidas.auth.commons.attribute.impl" + ".LiteralStringAttributeValueMarshaller"); } - private AttributeDefinition generateDateTimeAttribute(String friendlyName, String fragment, String prefix) throws URISyntaxException { + private AttributeDefinition generateDateTimeAttribute(String friendlyName, String fragment, String prefix) throws URISyntaxException { return generateAttribute(friendlyName, fragment, prefix, "eu.eidas.auth.commons.attribute.impl" + ".DateTimeAttributeValueMarshaller"); } - private AttributeDefinition generateAttribute(String friendlyName, String fragment, String prefix, + private AttributeDefinition generateAttribute(String friendlyName, String fragment, String prefix, String marshaller) throws URISyntaxException { return AttributeDefinition.builder() .friendlyName(friendlyName).nameUri(new URI("ad", "sd", fragment)) -- cgit v1.2.3