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; } }