SxmpSession { public Response process(InputStream is) throws IOException, SAXException, ParserConfigurationException { SxmpParser parser = new SxmpParser(version); Operation operation = null; try { operation = parser.parse(is); } catch (SxmpParsingException e) { if (e.getOperation() != null && e.getOperation().getType() != null) { logger.warn("Unable to fully parse XML into a request, returning ErrorResponse; error: "+e.getMessage()+", parsed: "+e.getOperation()); return new ErrorResponse(e.getOperation().getType(), e.getErrorCode().getIntValue(), e.getErrorMessage()); } else { throw new SAXException(e.getMessage(), e); } } try { if (!(operation instanceof Request)) { throw new SxmpErrorException(SxmpErrorCode.UNSUPPORTED_OPERATION, "A session can only process requests"); } Request req = (Request)operation; if (req.getAccount() == null) { throw new SxmpErrorException(SxmpErrorCode.MISSING_REQUIRED_ELEMENT, "A request must include account credentials"); } if (!processor.authenticate(req.getAccount())) { throw new SxmpErrorException(SxmpErrorCode.AUTHENTICATION_FAILURE, "Authentication failure"); } if (operation instanceof SubmitRequest) { return processor.submit(req.getAccount(), (SubmitRequest)operation); } else if (operation instanceof DeliverRequest) { return processor.deliver(req.getAccount(), (DeliverRequest)operation); } else if (operation instanceof DeliveryReportRequest) { return processor.deliveryReport(req.getAccount(), (DeliveryReportRequest)operation); } else { throw new SxmpErrorException(SxmpErrorCode.UNSUPPORTED_OPERATION, "Unsupported operation request type"); } } catch (SxmpErrorException e) { logger.warn(e.getMessage()); return new ErrorResponse(operation.getType(), e.getErrorCode().getIntValue(), e.getErrorMessage()); } catch (Throwable t) { logger.error("Major uncaught throwable while processing request, generating an ErrorResponse", t); return new ErrorResponse(operation.getType(), SxmpErrorCode.GENERIC.getIntValue(), "Generic error while processing request"); } } SxmpSession(final SxmpProcessor processor, final String version); Response process(InputStream is);  }
@Test public void processThrowsSAXParsingException() throws Exception { StringBuilder string0 = new StringBuilder(200) .append("<?xml version=\"1.0\"?>\n") .append("<operation type=\"submit\">\n") .append("</operation2>\n") .append(""); ByteArrayInputStream is = new ByteArrayInputStream(string0.toString().getBytes()); MockSxmpRequestProcessor processor = new MockSxmpRequestProcessor(); SxmpSession session = new SxmpSession(processor, SxmpParser.VERSION_1_0); Response response = null; try { response = session.process(is); Assert.fail(); } catch (SAXParseException e) { } }
@Test public void processThrowsSxmpParsingExceptionWithNoOperationType() throws Exception { StringBuilder string0 = new StringBuilder(200) .append("<?xml version=\"1.0\"?>\n") .append("<operation2 type=\"submit\">\n") .append("</operation2>\n") .append(""); ByteArrayInputStream is = new ByteArrayInputStream(string0.toString().getBytes()); MockSxmpRequestProcessor processor = new MockSxmpRequestProcessor(); SxmpSession session = new SxmpSession(processor, SxmpParser.VERSION_1_0); Response response = null; try { response = session.process(is); Assert.fail(); } catch (SAXException e) { Assert.assertThat(e.getMessage(), CoreMatchers.containsString("Root element must be an [operation]")); } }
@Test public void processResponseThrowsUnsupportedOperation() throws Exception { StringBuilder string0 = new StringBuilder(200) .append("<?xml version=\"1.0\"?>\n") .append("<operation type=\"submit\">\n") .append(" <submitResponse>\n") .append(" <error code=\"0\" message=\"OK\"/>\n") .append(" <ticketId>000:20090118002220948:000</ticketId>\n") .append(" </submitResponse>\n") .append("</operation>\n") .append(""); ByteArrayInputStream is = new ByteArrayInputStream(string0.toString().getBytes()); MockSxmpRequestProcessor processor = new MockSxmpRequestProcessor(); SxmpSession session = new SxmpSession(processor, SxmpParser.VERSION_1_0); ErrorResponse response = (ErrorResponse)session.process(is); Assert.assertEquals(Operation.Type.SUBMIT, response.getType()); Assert.assertEquals(SxmpErrorCode.UNSUPPORTED_OPERATION.getIntValue(), response.getErrorCode().intValue()); }
@Test public void processResponseAuthenticationReturnsFalse() throws Exception { StringBuilder string0 = new StringBuilder(200) .append("<?xml version=\"1.0\"?>\n") .append("<operation type=\"submit\">\n") .append(" <account username=\"customer1\" password=\"test1\"/>\n") .append(" <submitRequest referenceId=\"MYREF102020022\">\n") .append(" <operatorId>10</operatorId>\n") .append(" <deliveryReport>true</deliveryReport>\n") .append(" <sourceAddress type=\"network\">40404</sourceAddress>\n") .append(" <destinationAddress type=\"international\">+12065551212</destinationAddress>\n") .append(" <text encoding=\"ISO-8859-1\">48656c6c6f20576f726c64</text>\n") .append(" </submitRequest>\n") .append("</operation>\n") .append(""); ByteArrayInputStream is = new ByteArrayInputStream(string0.toString().getBytes()); SxmpProcessor processor = new SxmpProcessor() { public boolean authenticate(Account account) throws SxmpErrorException { return false; } public SubmitResponse submit(Account account, SubmitRequest submitRequest) throws SxmpErrorException { return null; } public DeliverResponse deliver(Account account, DeliverRequest deliverRequest) throws SxmpErrorException { return null; } public DeliveryReportResponse deliveryReport(Account account, DeliveryReportRequest deliveryRequest) throws SxmpErrorException { return null; } }; SxmpSession session = new SxmpSession(processor, SxmpParser.VERSION_1_0); ErrorResponse response = (ErrorResponse)session.process(is); Assert.assertEquals(Operation.Type.SUBMIT, response.getType()); Assert.assertEquals(SxmpErrorCode.AUTHENTICATION_FAILURE.getIntValue(), response.getErrorCode().intValue()); }
@Test public void processResponseAuthenticationThrowsException() throws Exception { StringBuilder string0 = new StringBuilder(200) .append("<?xml version=\"1.0\"?>\n") .append("<operation type=\"submit\">\n") .append(" <account username=\"customer1\" password=\"test1\"/>\n") .append(" <submitRequest referenceId=\"MYREF102020022\">\n") .append(" <operatorId>10</operatorId>\n") .append(" <deliveryReport>true</deliveryReport>\n") .append(" <sourceAddress type=\"network\">40404</sourceAddress>\n") .append(" <destinationAddress type=\"international\">+12065551212</destinationAddress>\n") .append(" <text encoding=\"ISO-8859-1\">48656c6c6f20576f726c64</text>\n") .append(" </submitRequest>\n") .append("</operation>\n") .append(""); ByteArrayInputStream is = new ByteArrayInputStream(string0.toString().getBytes()); SxmpProcessor processor = new SxmpProcessor() { public boolean authenticate(Account account) throws SxmpErrorException { throw new SxmpErrorException(SxmpErrorCode.EMPTY_VALUE, "Unable to correctly get database connection"); } public SubmitResponse submit(Account account, SubmitRequest submitRequest) throws SxmpErrorException { return null; } public DeliverResponse deliver(Account account, DeliverRequest deliverRequest) throws SxmpErrorException { return null; } public DeliveryReportResponse deliveryReport(Account account, DeliveryReportRequest deliveryRequest) throws SxmpErrorException { return null; } }; SxmpSession session = new SxmpSession(processor, SxmpParser.VERSION_1_0); ErrorResponse response = (ErrorResponse)session.process(is); Assert.assertEquals(Operation.Type.SUBMIT, response.getType()); Assert.assertEquals(SxmpErrorCode.EMPTY_VALUE.getIntValue(), response.getErrorCode().intValue()); }
@Test public void processSubmitThrowsUncaughtException() throws Exception { StringBuilder string0 = new StringBuilder(200) .append("<?xml version=\"1.0\"?>\n") .append("<operation type=\"submit\">\n") .append(" <account username=\"customer1\" password=\"test1\"/>\n") .append(" <submitRequest referenceId=\"MYREF102020022\">\n") .append(" <operatorId>10</operatorId>\n") .append(" <deliveryReport>true</deliveryReport>\n") .append(" <sourceAddress type=\"network\">40404</sourceAddress>\n") .append(" <destinationAddress type=\"international\">+12065551212</destinationAddress>\n") .append(" <text encoding=\"ISO-8859-1\">48656c6c6f20576f726c64</text>\n") .append(" </submitRequest>\n") .append("</operation>\n") .append(""); ByteArrayInputStream is = new ByteArrayInputStream(string0.toString().getBytes()); SxmpProcessor processor = new SxmpProcessor() { public boolean authenticate(Account account) throws SxmpErrorException { return true; } public SubmitResponse submit(Account account, SubmitRequest submitRequest) throws SxmpErrorException { throw new UnsupportedOperationException("Not supported yet."); } public DeliverResponse deliver(Account account, DeliverRequest deliverRequest) throws SxmpErrorException { return null; } public DeliveryReportResponse deliveryReport(Account account, DeliveryReportRequest deliveryRequest) throws SxmpErrorException { return null; } }; SxmpSession session = new SxmpSession(processor, SxmpParser.VERSION_1_0); ErrorResponse response = (ErrorResponse)session.process(is); Assert.assertEquals(Operation.Type.SUBMIT, response.getType()); Assert.assertEquals(SxmpErrorCode.GENERIC.getIntValue(), response.getErrorCode().intValue()); }
@Test public void processSubmitOK() throws Exception { StringBuilder string0 = new StringBuilder(200) .append("<?xml version=\"1.0\"?>\n") .append("<operation type=\"submit\">\n") .append(" <account username=\"customer1\" password=\"test1\"/>\n") .append(" <submitRequest referenceId=\"MYREF102020022\">\n") .append(" <operatorId>10</operatorId>\n") .append(" <deliveryReport>true</deliveryReport>\n") .append(" <sourceAddress type=\"network\">40404</sourceAddress>\n") .append(" <destinationAddress type=\"international\">+12065551212</destinationAddress>\n") .append(" <text encoding=\"ISO-8859-1\">48656c6c6f20576f726c64</text>\n") .append(" </submitRequest>\n") .append("</operation>\n") .append(""); ByteArrayInputStream is = new ByteArrayInputStream(string0.toString().getBytes()); MockSxmpRequestProcessor processor = new MockSxmpRequestProcessor(); SxmpSession session = new SxmpSession(processor, SxmpParser.VERSION_1_0); SubmitResponse response = (SubmitResponse)session.process(is); Assert.assertEquals(Operation.Type.SUBMIT, response.getType()); Assert.assertEquals(0, response.getErrorCode().intValue()); Assert.assertEquals("OK", response.getErrorMessage()); Assert.assertEquals("THISISATICKET", response.getTicketId()); }