diff options
author | Thomas <> | 2021-04-21 13:49:36 +0200 |
---|---|---|
committer | Thomas <> | 2021-04-21 13:49:36 +0200 |
commit | cc8c96de6330f9186ea271fe9e120e2fef0b5375 (patch) | |
tree | cb0436a47570ff1609a38755b960a64ecc21489b /eaaf_core/src/test/java | |
parent | 3cbba3fcda614fa37357822d0eeb543c3e19276e (diff) | |
download | EAAF-Components-cc8c96de6330f9186ea271fe9e120e2fef0b5375.tar.gz EAAF-Components-cc8c96de6330f9186ea271fe9e120e2fef0b5375.tar.bz2 EAAF-Components-cc8c96de6330f9186ea271fe9e120e2fef0b5375.zip |
add missing error-handling and add a few more tests
Diffstat (limited to 'eaaf_core/src/test/java')
3 files changed, 456 insertions, 0 deletions
diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/controller/ProtocolFinalizationControllerTest.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/controller/ProtocolFinalizationControllerTest.java new file mode 100644 index 00000000..8aa3e8a9 --- /dev/null +++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/controller/ProtocolFinalizationControllerTest.java @@ -0,0 +1,304 @@ +package at.gv.egiz.eaaf.core.impl.idp.auth.controller; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.util.SerializationUtils; + +import at.gv.egiz.eaaf.core.api.data.EaafConfigConstants; +import at.gv.egiz.eaaf.core.api.data.EaafConstants; +import at.gv.egiz.eaaf.core.api.data.ExceptionContainer; +import at.gv.egiz.eaaf.core.api.gui.GroupDefinition; +import at.gv.egiz.eaaf.core.api.gui.ModifyableGuiBuilderConfiguration; +import at.gv.egiz.eaaf.core.api.idp.IConfiguration; +import at.gv.egiz.eaaf.core.api.storage.ITransactionStorage; +import at.gv.egiz.eaaf.core.api.utils.IPendingRequestIdGenerationStrategy; +import at.gv.egiz.eaaf.core.exceptions.EaafException; +import at.gv.egiz.eaaf.core.impl.gui.AbstractGuiFormBuilderConfiguration; +import at.gv.egiz.eaaf.core.impl.idp.auth.dummy.DummyDefaultErrorService; +import at.gv.egiz.eaaf.core.impl.idp.auth.services.IErrorService.ActionType; +import at.gv.egiz.eaaf.core.impl.idp.auth.services.IErrorService.LogLevel; +import at.gv.egiz.eaaf.core.impl.idp.controller.ProtocolFinalizationController; +import at.gv.egiz.eaaf.core.impl.idp.module.gui.DummyGuiBuilderConfigurationFactory; +import at.gv.egiz.eaaf.core.impl.idp.module.gui.DummyGuiFormBuilder; +import at.gv.egiz.eaaf.core.impl.idp.module.test.DummySpConfiguration; +import at.gv.egiz.eaaf.core.impl.idp.module.test.TestRequestImpl; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration({"/SpringTest-context_eaaf_core.xml", "/SpringTest-context_eaaf_auth.xml"}) +public class ProtocolFinalizationControllerTest { + + @Autowired IConfiguration config; + @Autowired ProtocolFinalizationController controller; + @Autowired DummyGuiBuilderConfigurationFactory guiConfigFactory; + @Autowired DummyGuiFormBuilder guiBuilder; + @Autowired IPendingRequestIdGenerationStrategy requestIdValidationStragegy; + @Autowired ITransactionStorage storage; + @Autowired DummyDefaultErrorService errorService; + + /** + * jUnit test initializer. + */ + @Before + public void initialize() { + errorService.setErrorIdTokenForRedirect(null); + errorService.setLogLevel(LogLevel.WARN); + errorService.setTicketType(ActionType.NO_TICKET); + + } + + @Test + public void performErrorRedirectNoToken() throws EaafException, IOException { + + MockHttpServletRequest httpReq = new MockHttpServletRequest("POST", "https://localhost/authhandler"); + httpReq.addHeader("Accept", "application/json"); + MockHttpServletResponse httpResp = new MockHttpServletResponse(); + guiConfigFactory.setErrorGuiConfig( + new DummyGuiBuilderConfig("https://localhost/authhandler", "jUnitView", "/junitSubmit")); + + // perform test + controller.errorRedirect(httpReq, httpResp); + + //validate state + assertNotNull("No gui builder request", guiBuilder.getConfig()); + assertFalse("No GUI form infos", guiBuilder.getConfig().getViewParameters().isEmpty()); + assertTrue("No GUI form infos", guiBuilder.getConfig().getViewParameters().containsKey("msg")); + Map<String, Object> params = ((Map<String, Object>) guiBuilder.getConfig().getViewParameters().get("msg")); + assertFalse("No GUI form infos", params.isEmpty()); + assertEquals("wrong intErrorCode", "auth.26", params.get("errorCode")); + assertTrue("wrong extErrorCode", ((String) params.get("extErrorCode")).contains("auth.26")); + + assertFalse("GUI sp redirect", guiBuilder.getConfig().getViewParameters() + .containsKey(DummyDefaultErrorService.JUNIT_EL_SPREDIRECT)); + + } + + @Test + public void performErrorRedirect() throws EaafException, IOException { + MockHttpServletRequest httpReq = new MockHttpServletRequest("POST", "https://localhost/authhandler"); + httpReq.addHeader("Accept", "application/json"); + MockHttpServletResponse httpResp = new MockHttpServletResponse(); + guiConfigFactory.setErrorGuiConfig( + new DummyGuiBuilderConfig("https://localhost/authhandler", "jUnitView", "/junitSubmit")); + + String token = requestIdValidationStragegy.generateExternalPendingRequestId(); + httpReq.setParameter(EaafConstants.PARAM_HTTP_ERROR_CODE, token); + + TestRequestImpl protocolRequest = new TestRequestImpl(); + Map<String, String> spConfig = new HashMap<>(); + spConfig.put(EaafConfigConstants.SERVICE_UNIQUEIDENTIFIER, RandomStringUtils.randomAlphabetic(10)); + + protocolRequest.setSpConfig(new DummySpConfiguration(spConfig, config)); + Throwable throwable = new EaafException("internal.00"); + final ExceptionContainer exceptionContainer = new ExceptionContainer(protocolRequest, throwable); + final byte[] serialized = SerializationUtils.serialize(exceptionContainer); + storage.put(token, serialized, -1); + + // perform test + controller.errorRedirect(httpReq, httpResp); + + //validate state + assertNull("Exception not removed from cache", storage.get( + requestIdValidationStragegy.getPendingRequestIdWithOutChecks(token))); + + assertNotNull("No gui builder request", guiBuilder.getConfig()); + assertFalse("No GUI form infos", guiBuilder.getConfig().getViewParameters().isEmpty()); + assertTrue("No GUI form infos", guiBuilder.getConfig().getViewParameters().containsKey("msg")); + Map<String, Object> params = ((Map<String, Object>) guiBuilder.getConfig().getViewParameters().get("msg")); + assertFalse("No GUI form infos", params.isEmpty()); + assertEquals("wrong intErrorCode", "internal.00", params.get("errorCode")); + assertTrue("wrong extErrorCode", ((String) params.get("extErrorCode")).contains("internal.00")); + + assertFalse("GUI sp redirect", guiBuilder.getConfig().getViewParameters() + .containsKey(DummyDefaultErrorService.JUNIT_EL_SPREDIRECT)); + + } + + @Test + public void performErrorHandlingNoToken() throws EaafException, IOException { + + MockHttpServletRequest httpReq = new MockHttpServletRequest("POST", "https://localhost/authhandler"); + httpReq.addHeader("Accept", "application/json"); + MockHttpServletResponse httpResp = new MockHttpServletResponse(); + guiConfigFactory.setErrorGuiConfig( + new DummyGuiBuilderConfig("https://localhost/authhandler", "jUnitView", "/junitSubmit")); + + // perform test + controller.errorHandling(httpReq, httpResp); + + //validate state + assertNotNull("No gui builder request", guiBuilder.getConfig()); + assertFalse("No GUI form infos", guiBuilder.getConfig().getViewParameters().isEmpty()); + assertTrue("No GUI form infos", guiBuilder.getConfig().getViewParameters().containsKey("msg")); + Map<String, Object> params = ((Map<String, Object>) guiBuilder.getConfig().getViewParameters().get("msg")); + assertFalse("No GUI form infos", params.isEmpty()); + assertEquals("wrong intErrorCode", "auth.26", params.get("errorCode")); + assertTrue("wrong extErrorCode", ((String) params.get("extErrorCode")).contains("auth.26")); + + assertFalse("GUI sp redirect", guiBuilder.getConfig().getViewParameters() + .containsKey(DummyDefaultErrorService.JUNIT_EL_SPREDIRECT)); + + } + + @Test + public void performErrorHandlingWithToken() throws EaafException, IOException { + MockHttpServletRequest httpReq = new MockHttpServletRequest("POST", "https://localhost/authhandler"); + httpReq.addHeader("Accept", "application/json"); + MockHttpServletResponse httpResp = new MockHttpServletResponse(); + guiConfigFactory.setErrorGuiConfig( + new DummyGuiBuilderConfig("https://localhost/authhandler", "jUnitView", "/junitSubmit")); + + String token = requestIdValidationStragegy.generateExternalPendingRequestId(); + httpReq.setParameter(EaafConstants.PARAM_HTTP_ERROR_CODE, token); + + TestRequestImpl protocolRequest = new TestRequestImpl(); + Map<String, String> spConfig = new HashMap<>(); + spConfig.put(EaafConfigConstants.SERVICE_UNIQUEIDENTIFIER, RandomStringUtils.randomAlphabetic(10)); + + protocolRequest.setSpConfig(new DummySpConfiguration(spConfig, config)); + Throwable throwable = new EaafException("internal.00"); + final ExceptionContainer exceptionContainer = new ExceptionContainer(protocolRequest, throwable); + final byte[] serialized = SerializationUtils.serialize(exceptionContainer); + storage.put(token, serialized, -1); + + // perform test + controller.errorHandling(httpReq, httpResp); + + //validate state + + assertNull("Exception not removed from cache", storage.get( + requestIdValidationStragegy.getPendingRequestIdWithOutChecks(token))); + + assertNotNull("No gui builder request", guiBuilder.getConfig()); + assertFalse("No GUI form infos", guiBuilder.getConfig().getViewParameters().isEmpty()); + assertTrue("No GUI form infos", guiBuilder.getConfig().getViewParameters().containsKey("msg")); + Map<String, Object> params = ((Map<String, Object>) guiBuilder.getConfig().getViewParameters().get("msg")); + assertFalse("No GUI form infos", params.isEmpty()); + assertEquals("wrong intErrorCode", "internal.00", params.get("errorCode")); + assertTrue("wrong extErrorCode", ((String) params.get("extErrorCode")).contains("internal.00")); + + + assertFalse("GUI sp redirect", guiBuilder.getConfig().getViewParameters() + .containsKey(DummyDefaultErrorService.JUNIT_EL_SPREDIRECT)); + } + + @Test + public void performErrorHandlingWithTokenAndRedirect() throws EaafException, IOException { + MockHttpServletRequest httpReq = new MockHttpServletRequest("POST", "https://localhost/authhandler"); + httpReq.addHeader("Accept", "application/json"); + MockHttpServletResponse httpResp = new MockHttpServletResponse(); + guiConfigFactory.setErrorGuiConfig( + new DummyGuiBuilderConfig("https://localhost/authhandler", "jUnitView", "/junitSubmit")); + + String token = requestIdValidationStragegy.generateExternalPendingRequestId(); + httpReq.setParameter(EaafConstants.PARAM_HTTP_ERROR_CODE, token); + + TestRequestImpl protocolRequest = new TestRequestImpl(); + Map<String, String> spConfig = new HashMap<>(); + spConfig.put(EaafConfigConstants.SERVICE_UNIQUEIDENTIFIER, RandomStringUtils.randomAlphabetic(10)); + + protocolRequest.setSpConfig(new DummySpConfiguration(spConfig, config)); + protocolRequest.setTransactionId(RandomStringUtils.randomAlphanumeric(10)); + + Throwable throwable = new EaafException("internal.00"); + final ExceptionContainer exceptionContainer = new ExceptionContainer(protocolRequest, throwable); + final byte[] serialized = SerializationUtils.serialize(exceptionContainer); + storage.put(token, serialized, -1); + + String secondErrorTicket = requestIdValidationStragegy.generateExternalPendingRequestId(); + errorService.setErrorIdTokenForRedirect(secondErrorTicket); + errorService.setTicketType(ActionType.TICKET); + + + // perform test + controller.errorHandling(httpReq, httpResp); + + //validate state + assertNull("Exception not removed from cache", storage.get( + requestIdValidationStragegy.getPendingRequestIdWithOutChecks(token))); + + assertNotNull("No gui builder request", guiBuilder.getConfig()); + assertFalse("No GUI form infos", guiBuilder.getConfig().getViewParameters().isEmpty()); + assertTrue("No GUI form infos", guiBuilder.getConfig().getViewParameters().containsKey("msg")); + Map<String, Object> params = ((Map<String, Object>) guiBuilder.getConfig().getViewParameters().get("msg")); + assertFalse("No GUI form infos", params.isEmpty()); + assertEquals("wrong intErrorCode", "internal.00", params.get("errorCode")); + assertTrue("wrong extErrorCode", ((String) params.get("extErrorCode")).contains("internal.00")); + + byte[] secondErrorSerialized = storage.get( + requestIdValidationStragegy.getPendingRequestIdWithOutChecks(secondErrorTicket), byte[].class); + assertNotNull("Exception not removed from cache", secondErrorSerialized); + ExceptionContainer secondError = (ExceptionContainer) SerializationUtils.deserialize(secondErrorSerialized); + assertEquals("wrong pengingReq", protocolRequest.getUniqueTransactionIdentifier(), + secondError.getPendingRequest().getUniqueTransactionIdentifier()); + assertEquals("wrong exception", throwable.getMessage(), secondError.getExceptionThrown().getMessage()); + + assertTrue("GUI sp redirect", guiBuilder.getConfig().getViewParameters() + .containsKey(DummyDefaultErrorService.JUNIT_EL_SPREDIRECT)); + + } + + + private class DummyGuiBuilderConfig extends AbstractGuiFormBuilderConfiguration + implements ModifyableGuiBuilderConfiguration { + + /** + * DummyGuiBuilderConfiguration. + * + * @param authUrl AuthUrl + * @param viewName viewName + * @param formSubmitEndpoint submit endpoint + */ + DummyGuiBuilderConfig(String authUrl, String viewName, String formSubmitEndpoint) { + super(authUrl, viewName, formSubmitEndpoint); + + } + + @Override + public String getDefaultContentType() { + return null; + + } + + @Override + protected void putSpecificViewParameters() { + + + } + + @Override + protected GroupDefinition getFromGroup() { + return null; + + } + + @Override + public void putCustomParameterWithOutEscaption(GroupDefinition group, String key, Object value) { + setViewParameter(group, key, value); + + } + + @Override + public void putCustomParameter(GroupDefinition group, String key, String value) { + setViewParameter(group, key, value); + + } + }; + +} diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/dummy/DummyDefaultErrorService.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/dummy/DummyDefaultErrorService.java new file mode 100644 index 00000000..347f9b5c --- /dev/null +++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/dummy/DummyDefaultErrorService.java @@ -0,0 +1,115 @@ +package at.gv.egiz.eaaf.core.impl.idp.auth.dummy; + +import java.text.MessageFormat; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.beans.factory.annotation.Autowired; + +import at.gv.egiz.eaaf.core.api.IStatusMessenger; +import at.gv.egiz.eaaf.core.api.gui.ModifyableGuiBuilderConfiguration; +import at.gv.egiz.eaaf.core.api.idp.IConfiguration; +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.services.IErrorService; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +public class DummyDefaultErrorService implements IErrorService { + private static final String TECH_LOG_MSG = "errorCode={0} Message={1}"; + + public static final String JUNIT_EL_SPREDIRECT = "spRedirect"; + + @Autowired IConfiguration basicConfig; + @Autowired IStatusMessenger statusMessager; + + @Setter + private ActionType ticketType = ActionType.NO_TICKET; + + @Setter + private LogLevel logLevel = LogLevel.WARN; + + @Setter + private String errorIdTokenForRedirect; + + @Override + public String getExternalCodeFromInternal(String internalCode) { + return statusMessager.mapInternalErrorToExternalError(internalCode); + + } + + @Override + public IHandleData createHandleData(Throwable throwable, boolean supportRedirctToSp) throws EaafException { + String internalErrorId = extractInternalErrorCode(throwable); + + return HandleData.builder() + .throwable(throwable) + .internalErrorCode(internalErrorId) + .actionType(ticketType) + .logLevel(logLevel) + .errorIdTokenForRedirect(errorIdTokenForRedirect) + .allowSpRedirct(supportRedirctToSp) + .build(); + + } + + @Override + public void displayErrorData(ModifyableGuiBuilderConfiguration c, IHandleData errorData, + HttpServletRequest httpReq) throws EaafException { + if (((HandleData)errorData).isAllowSpRedirct()) { + c.putCustomParameter(null, JUNIT_EL_SPREDIRECT, "toSpWithToken:" + errorData.getErrorIdTokenForRedirect()); + + } + + + } + + private String extractInternalErrorCode(Throwable throwable) { + Throwable originalException; + if (throwable instanceof TaskExecutionException + && ((TaskExecutionException) throwable).getOriginalException() != null) { + originalException = ((TaskExecutionException) throwable).getOriginalException(); + + } else { + originalException = throwable; + + } + + if (!(originalException instanceof EaafException)) { + return IStatusMessenger.CODES_INTERNAL_ERROR_GENERIC; + + } else { + return ((EaafException) originalException).getErrorId(); + + } + } + + @Builder + static class HandleData implements IHandleData { + + @Getter + private boolean allowSpRedirct; + + @Getter + private String errorIdTokenForRedirect; + + @Getter + private final Throwable throwable; + + @Getter + private String internalErrorCode; + + @Getter + private ActionType actionType; + + @Getter + private LogLevel logLevel; + + public String getPreFormatedErrorMessage() { + return MessageFormat.format(TECH_LOG_MSG, internalErrorCode, throwable.getMessage()); + + } + + } +} diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/module/gui/DummyGuiFormBuilder.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/module/gui/DummyGuiFormBuilder.java new file mode 100644 index 00000000..bd81169d --- /dev/null +++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/module/gui/DummyGuiFormBuilder.java @@ -0,0 +1,37 @@ +package at.gv.egiz.eaaf.core.impl.idp.module.gui; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import at.gv.egiz.eaaf.core.api.gui.IGuiBuilderConfiguration; +import at.gv.egiz.eaaf.core.api.gui.IGuiFormBuilder; +import at.gv.egiz.eaaf.core.exceptions.GuiBuildException; +import lombok.Getter; + +@Getter +public class DummyGuiFormBuilder implements IGuiFormBuilder { + + private String loggerName; + + private IGuiBuilderConfiguration config; + + private String contentType; + + @Override + public void build(HttpServletRequest httpReq, HttpServletResponse httpResp, IGuiBuilderConfiguration config, + String loggerName) throws GuiBuildException { + this.loggerName = loggerName; + this.config = config; + + } + + @Override + public void build(HttpServletRequest httpReq, HttpServletResponse httpResp, IGuiBuilderConfiguration config, + String contentType, String loggerName) throws GuiBuildException { + this.loggerName = loggerName; + this.config = config; + this.contentType = contentType; + + } + +} |