From 20f485434680151111cf7cc7eaf33ca3b92221cb Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Tue, 29 Nov 2022 09:07:30 +0100 Subject: feat(core): add ticket-based error-handling service as EAAF core functionality --- .../core/impl/idp/auth/dummy/DummyAuthConfig.java | 100 ++++++++ .../impl/idp/auth/dummy/DummyPendingRequest.java | 10 + .../idp/auth/service/JunitTicketErrorService.java | 21 ++ .../idp/auth/service/TicketErrorServiceTest.java | 272 +++++++++++++++++++++ 4 files changed, 403 insertions(+) create mode 100644 eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/dummy/DummyAuthConfig.java create mode 100644 eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/dummy/DummyPendingRequest.java create mode 100644 eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/service/JunitTicketErrorService.java create mode 100644 eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/service/TicketErrorServiceTest.java (limited to 'eaaf_core/src/test/java/at') diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/dummy/DummyAuthConfig.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/dummy/DummyAuthConfig.java new file mode 100644 index 00000000..03f5b759 --- /dev/null +++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/dummy/DummyAuthConfig.java @@ -0,0 +1,100 @@ +package at.gv.egiz.eaaf.core.impl.idp.auth.dummy; + +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; + +import org.apache.commons.lang3.StringUtils; + +import at.gv.egiz.eaaf.core.api.idp.IConfigurationWithSP; +import at.gv.egiz.eaaf.core.api.idp.IExtendedConfiguration; +import at.gv.egiz.eaaf.core.api.idp.ISpConfiguration; +import at.gv.egiz.eaaf.core.exceptions.EaafConfigurationException; +import at.gv.egiz.eaaf.core.exceptions.EaafException; +import at.gv.egiz.eaaf.core.impl.idp.conf.AbstractSpringBootConfigurationImpl; + + +public class DummyAuthConfig extends AbstractSpringBootConfigurationImpl + implements IConfigurationWithSP, IExtendedConfiguration { + + private ISpConfiguration spconfig = null; + + private String configPropPrefix = StringUtils.EMPTY; + + private boolean useConfigRootDirFromConfig = false; + + private URI internalConfigRootDirHolder = null; + + public void setSpConfig(final ISpConfiguration spConfig) { + this.spconfig = spConfig; + + } + + + + @Override + public URI getConfigurationRootDirectory() { + if (useConfigRootDirFromConfig) { + if (internalConfigRootDirHolder == null) { + try { + internalConfigRootDirHolder = new URI(getBasicConfiguration("core.configRootDir")); + } catch (final URISyntaxException e) { + e.printStackTrace(); + + } + } + return internalConfigRootDirHolder; + + } else { + return new java.io.File(".").toURI(); + + } + } + + @Override + public ISpConfiguration getServiceProviderConfiguration(final String arg0) + throws EaafConfigurationException { + return spconfig; + } + + @SuppressWarnings("unchecked") + @Override + public T getServiceProviderConfiguration(final String arg0, final Class arg1) + throws EaafConfigurationException { + return (T) spconfig; + + } + + @Override + public String validateIdpUrl(final URL arg0) throws EaafException { + return arg0.toString(); + } + + + @Override + protected String getBackupConfigPath() { + return null; + } + + + @Override + public String getApplicationSpecificKeyPrefix() { + return configPropPrefix; + + } + + + + public void setConfigPropPrefix(final String configPropPrefix) { + this.configPropPrefix = configPropPrefix; + } + + + + public void setUseConfigRootDirFromConfig(boolean useConfigRootDirFromConfig) { + this.useConfigRootDirFromConfig = useConfigRootDirFromConfig; + } + + + +} diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/dummy/DummyPendingRequest.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/dummy/DummyPendingRequest.java new file mode 100644 index 00000000..1a18ada2 --- /dev/null +++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/dummy/DummyPendingRequest.java @@ -0,0 +1,10 @@ +package at.gv.egiz.eaaf.core.impl.idp.auth.dummy; + +import at.gv.egiz.eaaf.core.impl.idp.controller.protocols.RequestImpl; + +public class DummyPendingRequest extends RequestImpl { + + + private static final long serialVersionUID = 8136280395622411505L; + +} 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 appender; + @Captor + ArgumentCaptor 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 valueCapture = ArgumentCaptor.forClass(String.class); + doNothing().when(guiBuilder).putCustomParameter(any(), any(), valueCapture.capture()); + + ArgumentCaptor 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; + + } + +} -- cgit v1.2.3