diff options
author | Thomas <> | 2022-11-29 09:07:30 +0100 |
---|---|---|
committer | Thomas <> | 2022-11-29 09:07:30 +0100 |
commit | 20f485434680151111cf7cc7eaf33ca3b92221cb (patch) | |
tree | 0bb3460ecdf55324ff2c4c7dc8a84325c6f716ee /eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/service | |
parent | 49d02d8c7c19ab9bb763d3f7fe862273706cc73a (diff) | |
download | EAAF-Components-20f485434680151111cf7cc7eaf33ca3b92221cb.tar.gz EAAF-Components-20f485434680151111cf7cc7eaf33ca3b92221cb.tar.bz2 EAAF-Components-20f485434680151111cf7cc7eaf33ca3b92221cb.zip |
feat(core): add ticket-based error-handling service as EAAF core functionality
Diffstat (limited to 'eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/service')
2 files changed, 293 insertions, 0 deletions
diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/service/JunitTicketErrorService.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/service/JunitTicketErrorService.java new file mode 100644 index 00000000..a8ccc54a --- /dev/null +++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/service/JunitTicketErrorService.java @@ -0,0 +1,21 @@ +package at.gv.egiz.eaaf.core.impl.idp.auth.service; + +import org.apache.commons.lang3.RandomStringUtils; + +import at.gv.egiz.eaaf.core.impl.idp.auth.services.TicketErrorService; + +/** + * Ticket based error-handling service for jUnit tests. + * + * @author tlenz + * + */ +public class JunitTicketErrorService extends TicketErrorService { + + @Override + protected String generateSupportTicket() { + return RandomStringUtils.randomAlphabetic(10); + + } + +} diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/service/TicketErrorServiceTest.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/service/TicketErrorServiceTest.java new file mode 100644 index 00000000..2c89e49f --- /dev/null +++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/service/TicketErrorServiceTest.java @@ -0,0 +1,272 @@ +package at.gv.egiz.eaaf.core.impl.idp.auth.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +import java.util.regex.Pattern; + +import javax.servlet.http.HttpServletRequest; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import at.gv.egiz.eaaf.core.api.gui.ModifyableGuiBuilderConfiguration; +import at.gv.egiz.eaaf.core.exceptions.EaafException; +import at.gv.egiz.eaaf.core.exceptions.TaskExecutionException; +import at.gv.egiz.eaaf.core.impl.idp.auth.dummy.DummyPendingRequest; +import at.gv.egiz.eaaf.core.impl.idp.auth.services.IErrorService; +import at.gv.egiz.eaaf.core.impl.idp.auth.services.IErrorService.LogLevel; +import ch.qos.logback.classic.spi.ILoggingEvent; +import lombok.val; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("/SpringTest-authcommon-errorService.xml") +@TestPropertySource( + properties = {"core.error.handling.config=src/test/resources/config/error_conf.yaml"}) +public class TicketErrorServiceTest { + @Autowired + private IErrorService ticketErrorService; + + @Mock + ch.qos.logback.core.AppenderBase<ch.qos.logback.classic.spi.ILoggingEvent> appender; + @Captor + ArgumentCaptor<ILoggingEvent> logCaptor; + + + /** + * Setup jUnit test. + */ + @Before + public void setUp() { + MockitoAnnotations.openMocks(this); + + } + + @Test + public void cofigurationLodingAndExternalCodeMapping() { + String external = ticketErrorService.getExternalCodeFromInternal("auth.01"); + + Assert.assertNotNull(external); + Assert.assertEquals(external, "9199"); + } + + @Test + public void coverDifferentExceptions() throws EaafException { + + val pendingReq = new DummyPendingRequest(); + pendingReq.setPendingRequestId("324"); + + val errorData = ticketErrorService.createHandleData(new Exception("sl20.07.02"), true); + val errorData1 = ticketErrorService.createHandleData( + new EaafException("sl20.07.02", new Object[]{"dummy"}), false); + val errorData2 = ticketErrorService.createHandleData( + new TaskExecutionException(pendingReq, "dummy", new EaafException("auth.00", new Object[]{"dummy"})), false); + val errorData3 = ticketErrorService.createHandleData(new EaafException("auth.21", null), false); + val errorData4 = ticketErrorService.createHandleData(new EaafException("internal.pendingreqid.01", null), false); + val errorData5 = ticketErrorService.createHandleData(new EaafException("junit.01", null), false); + + Assert.assertNotNull(errorData); + Assert.assertEquals(errorData.getActionType(), IErrorService.ActionType.TICKET); + Assert.assertEquals(errorData.getInternalErrorCode(), "internal.00"); + Assert.assertEquals(errorData.getLogLevel(), IErrorService.LogLevel.ERROR); + Assert.assertEquals(ticketErrorService.getExternalCodeFromInternal(errorData.getInternalErrorCode()), "9199"); + Assert.assertNotNull(errorData.getErrorIdTokenForRedirect()); + + Assert.assertNotNull(errorData1); + Assert.assertEquals(errorData1.getActionType(), IErrorService.ActionType.NO_TICKET); + Assert.assertEquals(errorData1.getInternalErrorCode(), "sl20.07.02"); + Assert.assertEquals(errorData1.getLogLevel(), IErrorService.LogLevel.WARN); + Assert.assertEquals(ticketErrorService.getExternalCodeFromInternal(errorData1.getInternalErrorCode()), "1003"); + Assert.assertNull(errorData1.getErrorIdTokenForRedirect()); + + Assert.assertNotNull(errorData2); + Assert.assertEquals(errorData2.getActionType(), IErrorService.ActionType.TICKET); + Assert.assertEquals(errorData2.getInternalErrorCode(), "auth.00"); + Assert.assertEquals(errorData2.getLogLevel(), IErrorService.LogLevel.ERROR); + Assert.assertEquals(ticketErrorService.getExternalCodeFromInternal(errorData2.getInternalErrorCode()), "9199"); + + Assert.assertNotNull(errorData3); + Assert.assertEquals(errorData3.getActionType(), IErrorService.ActionType.NO_TICKET); + Assert.assertEquals(errorData3.getInternalErrorCode(), "auth.21"); + Assert.assertEquals(errorData3.getLogLevel(), IErrorService.LogLevel.INFO); + Assert.assertEquals(ticketErrorService.getExternalCodeFromInternal(errorData3.getInternalErrorCode()), "1005"); + + Assert.assertNotNull(errorData4); + Assert.assertEquals(errorData4.getActionType(), IErrorService.ActionType.TICKET); + Assert.assertEquals(errorData4.getInternalErrorCode(), "internal.pendingreqid.01"); + Assert.assertEquals(errorData4.getLogLevel(), IErrorService.LogLevel.WARN); + Assert.assertEquals(ticketErrorService.getExternalCodeFromInternal(errorData4.getInternalErrorCode()), + "9199"); + + Assert.assertNotNull(errorData5); + Assert.assertEquals(errorData5.getActionType(), IErrorService.ActionType.ERRORPAGE); + Assert.assertEquals(errorData5.getInternalErrorCode(), "junit.01"); + Assert.assertEquals(errorData5.getLogLevel(), IErrorService.LogLevel.ERROR); + Assert.assertEquals(ticketErrorService.getExternalCodeFromInternal(errorData5.getInternalErrorCode()), + "junit.01"); + + } + + @Test + public void handleDataTicketNoRedirect() { + val logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("at.gv.egiz.eaaf.core"); + logger.addAppender(appender); + + IErrorService.IHandleData errorData = null; + try { + errorData = ticketErrorService.createHandleData(new EaafException("auth.00", new Object[]{"dummy"}), false); + } catch (EaafException e) { + e.printStackTrace(); + } + + Assert.assertNotNull(errorData); + Assert.assertEquals(errorData.getActionType(), IErrorService.ActionType.TICKET); + // Assert.assertEquals(errorData.getThrowable(), new EaafException("auth.00", new Object[] {"dummy"})); //TODO + // wrong excepiton + Assert.assertEquals(errorData.getInternalErrorCode(), "auth.00"); + + assertEquals("wrong errorLevel", LogLevel.ERROR, errorData.getLogLevel()); + assertTrue("ticket", errorData.getPreFormatedErrorMessage().contains("Ticket=")); + + } + + @Test + public void handleDataNoTicketNoRedirect() { + val logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("at.gv.egiz.eaaf.core"); + logger.addAppender(appender); + + + IErrorService.IHandleData errorData = null; + try { + errorData = ticketErrorService.createHandleData( + new EaafException("internal.pendingreqid.00", new Object[]{"dummy"}), false); + } catch (EaafException e) { + e.printStackTrace(); + } + + Assert.assertNotNull(errorData); + Assert.assertEquals(errorData.getActionType(), IErrorService.ActionType.NO_TICKET); + // Assert.assertEquals(errorData.getThrowable(), new EaafException("auth.00", new Object[] {"dummy"})); //TODO + // wrong excepiton + Assert.assertEquals(errorData.getInternalErrorCode(), "internal.pendingreqid.00"); + + assertEquals("wrong errorLevel", LogLevel.WARN, errorData.getLogLevel()); + assertFalse("ticket", errorData.getPreFormatedErrorMessage().contains("Ticket=")); + } + + @Test + public void handleDataTicketRedirect() { + val logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("at.gv.egiz.eaaf.core"); + logger.addAppender(appender); + + IErrorService.IHandleData errorData = null; + try { + errorData = ticketErrorService.createHandleData(new EaafException("auth.01", new Object[]{"dummy"}), true); + } catch (EaafException e) { + e.printStackTrace(); + } + + Assert.assertNotNull(errorData); + Assert.assertEquals(IErrorService.ActionType.TICKET, errorData.getActionType()); + // Assert.assertEquals(errorData.getThrowable(), new EaafException("auth.00", new Object[] {"dummy"})); //TODO + // wrong excepiton + Assert.assertEquals(errorData.getInternalErrorCode(), "auth.01"); + + assertEquals("wrong errorLevel", LogLevel.ERROR, errorData.getLogLevel()); + assertTrue("ticket", errorData.getPreFormatedErrorMessage().contains("Ticket=")); + } + + @Test + public void handleDataNoTicketRedirect() { + val logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("at.gv.egiz.eaaf.core"); + logger.addAppender(appender); + + IErrorService.IHandleData errorData = null; + try { + errorData = ticketErrorService + .createHandleData(new EaafException("module.binding.14", new Object[]{"dummy"}), false); + } catch (EaafException e) { + e.printStackTrace(); + } + + Assert.assertNotNull(errorData); + Assert.assertEquals(IErrorService.ActionType.NO_TICKET, errorData.getActionType()); + // Assert.assertEquals(errorData.getThrowable(), new EaafException("auth.00", new Object[] {"dummy"})); //TODO + // wrong excepiton + Assert.assertEquals(errorData.getInternalErrorCode(), "module.binding.14"); + + assertEquals("wrong errorLevel", LogLevel.WARN, errorData.getLogLevel()); + assertFalse("ticket", errorData.getPreFormatedErrorMessage().contains("Ticket=")); + } + + @Test + public void handleDataNoTicketAutoRedirect() { + val logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("at.gv.egiz.eaaf.core"); + logger.addAppender(appender); + + IErrorService.IHandleData errorData = null; + try { + errorData = ticketErrorService.createHandleData(new EaafException("auth.21", new Object[]{"dummy"}), true); + } catch (EaafException e) { + e.printStackTrace(); + } + + Assert.assertNotNull(errorData); + Assert.assertEquals(IErrorService.ActionType.NO_TICKET, errorData.getActionType()); + // Assert.assertEquals(errorData.getThrowable(), new EaafException("auth.00", new Object[] {"dummy"})); //TODO + // wrong excepiton + Assert.assertEquals(errorData.getInternalErrorCode(), "auth.21"); + + assertEquals("wrong errorLevel", LogLevel.INFO, errorData.getLogLevel()); + assertFalse("ticket", errorData.getPreFormatedErrorMessage().contains("Ticket=")); + } + + @Test + public void testErrorDataDisplay() throws EaafException { + + IErrorService.IHandleData errorData = null; + errorData = ticketErrorService.createHandleData(new EaafException("auth.01", new Object[]{"dummy"}), true); + Assert.assertNotNull(errorData); + + val guiBuilder = Mockito.mock(ModifyableGuiBuilderConfiguration.class); + ArgumentCaptor<String> valueCapture = ArgumentCaptor.forClass(String.class); + doNothing().when(guiBuilder).putCustomParameter(any(), any(), valueCapture.capture()); + + ArgumentCaptor<String> valueCapture2 = ArgumentCaptor.forClass(String.class); + doNothing().when(guiBuilder).putCustomParameterWithOutEscaption(any(), any(), valueCapture2.capture()); + + ticketErrorService.displayErrorData(guiBuilder, errorData, getMocReq()); + Assert.assertTrue("Incorrect Ticketvalue", + Pattern.matches("[A-Za-z0-9]{10}", valueCapture.getAllValues().get(0))); + Assert.assertEquals( + "http://iaik.dummy/blub/public/secure/errorRedirect?errorid=" + errorData.getErrorIdTokenForRedirect(), + valueCapture2.getAllValues().get(0)); + } + + private HttpServletRequest getMocReq() { + val req = Mockito.mock(HttpServletRequest.class); + when(req.getServerPort()).thenReturn(80); + when(req.getScheme()).thenReturn("http"); + when(req.getServerName()).thenReturn("iaik.dummy"); + when(req.getContextPath()).thenReturn("/blub"); + return req; + + } + +} |