From b0f2b00b460e1c8419ea631bdbd1da1c33150bce Mon Sep 17 00:00:00 2001 From: Christian Kollmann Date: Tue, 12 Jan 2021 14:17:52 +0100 Subject: Streamline mock creation in tests --- .../v2/test/tasks/InitialSearchTaskFirstTest.java | 53 +++------------------- 1 file changed, 7 insertions(+), 46 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 a1dce0f2..12a0969d 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 @@ -50,7 +50,9 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.annotation.DirtiesContext; @@ -69,13 +71,14 @@ import java.util.List; import java.util.Random; @RunWith(SpringJUnit4ClassRunner.class) - @ContextConfiguration("/SpringTest-context_tasks_test.xml") @DirtiesContext(classMode = ClassMode.BEFORE_CLASS) public class InitialSearchTaskFirstTest { private InitialSearchTask task; + @Mock private IZmrClient zmrClient; + @Mock private IErnpClient ernpClient; final ExecutionContext executionContext = new ExecutionContextImpl(); @@ -108,6 +111,9 @@ public class InitialSearchTaskFirstTest { */ @Before public void setUp() throws URISyntaxException, EaafStorageException { + MockitoAnnotations.initMocks(this); + task = new InitialSearchTask(new ArrayList(), ernpClient, zmrClient); + httpReq = new MockHttpServletRequest("POST", "https://localhost/authhandler"); httpResp = new MockHttpServletResponse(); RequestContextHolder.resetRequestAttributes(); @@ -125,20 +131,15 @@ public class InitialSearchTaskFirstTest { * One match, but register update needed */ public void testNode100_UserIdentifiedUpdateNecessary_a() throws Exception { - ArrayList zmrResult = new ArrayList<>(); String randomBpk = RandomStringUtils.randomNumeric(6); String newFirstName = RandomStringUtils.randomAlphabetic(5); zmrResult.add(new RegisterResult(randomBpk, DE_ST+randomIdentifier, newFirstName, randomFamilyName, randomDate)); - - zmrClient = Mockito.mock(IZmrClient.class); Mockito.when(zmrClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(zmrResult); ArrayList ernpResult = new ArrayList<>(); - ernpClient = Mockito.mock(IErnpClient.class); Mockito.when(ernpClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(ernpResult); - task = new InitialSearchTask(emptyHandlers(), ernpClient, zmrClient); task.execute(pendingReq, executionContext); String bPk = (String) pendingReq.getSessionData(AuthProcessDataWrapper.class).getGenericDataFromSession(Constants.DATA_RESULT_MATCHING_BPK); @@ -151,20 +152,14 @@ public class InitialSearchTaskFirstTest { * One match, but register update needed */ public void testNode100_UserIdentifiedUpdateNecessary_b() throws TaskExecutionException { - ArrayList zmrResult = new ArrayList<>(); - - zmrClient = Mockito.mock(IZmrClient.class); Mockito.when(zmrClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(zmrResult); ArrayList ernpResult = new ArrayList<>(); String randomBpk = RandomStringUtils.randomNumeric(6); ernpResult.add(new RegisterResult(randomBpk, DE_ST+randomIdentifier, "Max_new", randomFamilyName, randomDate)); - - ernpClient = Mockito.mock(IErnpClient.class); Mockito.when(ernpClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(ernpResult); - task = new InitialSearchTask(emptyHandlers(), ernpClient, zmrClient); task.execute(pendingReq, executionContext); String bPk = (String) pendingReq.getSessionData(AuthProcessDataWrapper.class).getGenericDataFromSession(Constants.DATA_RESULT_MATCHING_BPK); @@ -179,18 +174,14 @@ public class InitialSearchTaskFirstTest { * Two matches found in ZMR */ public void testNode101_ManualFixNecessary_a() throws Exception { - 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)); - zmrClient = Mockito.mock(IZmrClient.class); Mockito.when(zmrClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(zmrResult); ArrayList ernpResult = new ArrayList<>(); - ernpClient = Mockito.mock(IErnpClient.class); Mockito.when(ernpClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(ernpResult); - task = new InitialSearchTask(emptyHandlers(), ernpClient, zmrClient); try { task.execute(pendingReq, executionContext); Assert.assertTrue("Wrong workflow, should not reach this point/ get a bpk", false); @@ -209,7 +200,6 @@ public class InitialSearchTaskFirstTest { public void testNode101_ManualFixNecessary_b() throws Exception { String randombpk = RandomStringUtils.random(5); ArrayList zmrResult = new ArrayList<>(); - zmrClient = Mockito.mock(IZmrClient.class); Mockito.when(zmrClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(zmrResult); ArrayList ernpResult = new ArrayList<>(); @@ -217,11 +207,8 @@ public class InitialSearchTaskFirstTest { ernpResult.add(new RegisterResult(randombpk, DE_ST+randomIdentifier, randomGivenName+RandomStringUtils.random(2), randomFamilyName, randomDate)); - - ernpClient = Mockito.mock(IErnpClient.class); Mockito.when(ernpClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(ernpResult); - task = new InitialSearchTask(emptyHandlers(), ernpClient, zmrClient); try { task.execute(pendingReq, executionContext); Assert.assertTrue("Wrong workflow, should not reach this point/ get a bpk", false); @@ -237,19 +224,14 @@ public class InitialSearchTaskFirstTest { * One match, no register update needed */ public void testNode102_UserIdentified_a() throws Exception { - String randomBpk = RandomStringUtils.randomNumeric(12); ArrayList zmrResult = new ArrayList<>(); - zmrClient = Mockito.mock(IZmrClient.class); Mockito.when(zmrClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(zmrResult); ArrayList ernpResult = new ArrayList<>(); ernpResult.add(new RegisterResult(randomBpk, DE_ST+randomIdentifier, randomGivenName, randomFamilyName, randomDate)); - - ernpClient = Mockito.mock(IErnpClient.class); Mockito.when(ernpClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(ernpResult); - task = new InitialSearchTask(emptyHandlers(), ernpClient, zmrClient); task.execute(pendingReq, executionContext); String bPk = (String) pendingReq.getSessionData(AuthProcessDataWrapper.class).getGenericDataFromSession(Constants.DATA_RESULT_MATCHING_BPK); @@ -266,16 +248,11 @@ public class InitialSearchTaskFirstTest { ArrayList zmrResult = new ArrayList<>(); zmrResult.add(new RegisterResult(randomBpk, DE_ST+randomIdentifier, randomGivenName, randomFamilyName, randomDate)); - - zmrClient = Mockito.mock(IZmrClient.class); Mockito.when(zmrClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(zmrResult); ArrayList ernpResult = new ArrayList<>(); - - ernpClient = Mockito.mock(IErnpClient.class); Mockito.when(ernpClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(ernpResult); - task = new InitialSearchTask(emptyHandlers(), ernpClient, zmrClient); task.execute(pendingReq, executionContext); String bPk = (String) pendingReq.getSessionData(AuthProcessDataWrapper.class).getGenericDataFromSession(Constants.DATA_RESULT_MATCHING_BPK); @@ -296,7 +273,6 @@ public class InitialSearchTaskFirstTest { .setGenericDataToSession(Constants.DATA_FULL_EIDAS_RESPONSE, response); ArrayList zmrResultInitial = new ArrayList<>(); - zmrClient = Mockito.mock(IZmrClient.class); Mockito.when(zmrClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(zmrResultInitial); ArrayList zmrResultSpecific = new ArrayList<>(); @@ -308,7 +284,6 @@ public class InitialSearchTaskFirstTest { Mockito.when(zmrClient.searchItSpecific(taxNumber)).thenReturn(zmrResultSpecific); ArrayList ernpResultInitial = new ArrayList<>(); - ernpClient = Mockito.mock(IErnpClient.class); Mockito.when(ernpClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(ernpResultInitial); List handlers = new ArrayList<>(); @@ -347,7 +322,6 @@ public class InitialSearchTaskFirstTest { .setGenericDataToSession(Constants.DATA_FULL_EIDAS_RESPONSE, response); ArrayList zmrResultInitial = new ArrayList<>(); - zmrClient = Mockito.mock(IZmrClient.class); Mockito.when(zmrClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(zmrResultInitial); ArrayList zmrResultSpecific = new ArrayList<>(); @@ -358,7 +332,6 @@ public class InitialSearchTaskFirstTest { Mockito.when(zmrClient.searchDeSpecific(givenName, familyName, dateOfBirth, placeOfBirth, birthName)).thenReturn(zmrResultSpecific); ArrayList ernpResultInitial = new ArrayList<>(); - ernpClient = Mockito.mock(IErnpClient.class); Mockito.when(ernpClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(ernpResultInitial); List handlers = new ArrayList<>(); @@ -399,7 +372,6 @@ public class InitialSearchTaskFirstTest { .setGenericDataToSession(Constants.DATA_FULL_EIDAS_RESPONSE, response); ArrayList zmrResultInitial = new ArrayList<>(); - zmrClient = Mockito.mock(IZmrClient.class); Mockito.when(zmrClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(zmrResultInitial); ArrayList zmrResultSpecific = new ArrayList<>(); @@ -412,7 +384,6 @@ public class InitialSearchTaskFirstTest { Mockito.when(zmrClient.searchDeSpecific(givenName, familyName, dateOfBirth, placeOfBirth, birthName)).thenReturn(zmrResultSpecific); ArrayList ernpResultInitial = new ArrayList<>(); - ernpClient = Mockito.mock(IErnpClient.class); Mockito.when(ernpClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(ernpResultInitial); List handlers = new ArrayList<>(); @@ -442,7 +413,6 @@ public class InitialSearchTaskFirstTest { .setGenericDataToSession(Constants.DATA_FULL_EIDAS_RESPONSE, response); ArrayList zmrResultInitial = new ArrayList<>(); - zmrClient = Mockito.mock(IZmrClient.class); Mockito.when(zmrClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(zmrResultInitial); ArrayList zmrResultSpecific = new ArrayList<>(); @@ -455,7 +425,6 @@ public class InitialSearchTaskFirstTest { Mockito.when(zmrClient.searchItSpecific(fakeTaxNumber)).thenReturn(zmrResultSpecific); ArrayList ernpResultInitial = new ArrayList<>(); - ernpClient = Mockito.mock(IErnpClient.class); Mockito.when(ernpClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(ernpResultInitial); List handlers = new ArrayList<>(); @@ -478,16 +447,12 @@ public class InitialSearchTaskFirstTest { * NO match found in ZMR and ErnP with Initial search */ public void testNode105_TemporaryEnd() throws TaskExecutionException { - ArrayList zmrResult = new ArrayList<>(); - zmrClient = Mockito.mock(IZmrClient.class); Mockito.when(zmrClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(zmrResult); ArrayList ernpResult = new ArrayList<>(); - ernpClient = Mockito.mock(IErnpClient.class); Mockito.when(ernpClient.searchWithPersonIdentifier(randomIdentifier)).thenReturn(ernpResult); - task = new InitialSearchTask(emptyHandlers(), ernpClient, zmrClient); task.execute(pendingReq, executionContext); String bPk = (String) pendingReq.getSessionData(AuthProcessDataWrapper.class).getGenericDataFromSession(Constants.DATA_RESULT_MATCHING_BPK); @@ -562,8 +527,4 @@ public class InitialSearchTaskFirstTest { .attributeValueMarshaller(marshaller).build(); } - private List emptyHandlers() { - return new ArrayList<>(); - } - } -- cgit v1.2.3