Fix some checkstyle issues. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/4f8c9c9b Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4f8c9c9b Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4f8c9c9b
Branch: refs/heads/master Commit: 4f8c9c9bec14a7d3cd4902b60e2959dd9b1f1a10 Parents: 2e43972 Author: oalles <oal...@neivi.es> Authored: Mon Mar 21 13:36:08 2016 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Mar 23 18:46:10 2016 +0100 ---------------------------------------------------------------------- .../apache/camel/component/cm/CMComponent.java | 20 +- .../component/cm/CMSenderOneMessageImpl.java | 526 +++++++-------- .../camel/component/cm/test/CMMessageTest.java | 646 +++++++++---------- .../apache/camel/component/cm/test/CMTest.java | 5 +- .../cm/test/CamelTestConfiguration.java | 212 +++--- .../camel/component/cm/test/SMSMessageTest.java | 366 +++++------ 6 files changed, 889 insertions(+), 886 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/4f8c9c9b/components/camel-cm/src/main/java/org/apache/camel/component/cm/CMComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-cm/src/main/java/org/apache/camel/component/cm/CMComponent.java b/components/camel-cm/src/main/java/org/apache/camel/component/cm/CMComponent.java index 3239af2..fc7b9b5 100644 --- a/components/camel-cm/src/main/java/org/apache/camel/component/cm/CMComponent.java +++ b/components/camel-cm/src/main/java/org/apache/camel/component/cm/CMComponent.java @@ -38,8 +38,7 @@ import org.slf4j.LoggerFactory; */ public class CMComponent extends UriEndpointComponent { - private static final Logger LOG = LoggerFactory - .getLogger(CMComponent.class); + private static final Logger LOG = LoggerFactory.getLogger(CMComponent.class); @BeanInject private Validator validator; @@ -56,21 +55,16 @@ public class CMComponent extends UriEndpointComponent { * Endpoints factory */ @Override - protected Endpoint createEndpoint(final String uri, final String remaining, - final Map<String, Object> parameters) throws Exception { + protected Endpoint createEndpoint(final String uri, final String remaining, final Map<String, Object> parameters) throws Exception { LOG.debug("Creating CM Endpoint ... "); final String url = CMConstants.DEFAULT_SCHEME + remaining; if (!UrlValidator.getInstance().isValid(url)) { - throw new InvalidURLException(String.format( - "HOST provided: %s seem to be invalid. Remember SCHEME has to be excluded.", - url)); + throw new InvalidURLException(String.format("HOST provided: %s seem to be invalid. Remember SCHEME has to be excluded.", url)); } - LOG.debug("Uri=[{}], path=[{}], parameters=[{}]", - new Object[] { URISupport.sanitizeUri(uri), - URISupport.sanitizePath(remaining), parameters }); + LOG.debug("Uri=[{}], path=[{}], parameters=[{}]", new Object[] {URISupport.sanitizeUri(uri), URISupport.sanitizePath(remaining), parameters }); // Set configuration based on uri parameters final CMConfiguration config = new CMConfiguration(); @@ -78,13 +72,11 @@ public class CMComponent extends UriEndpointComponent { // Validate configuration LOG.debug("Validating uri based configuration"); - final Set<ConstraintViolation<CMConfiguration>> constraintViolations = validator - .validate(config); + final Set<ConstraintViolation<CMConfiguration>> constraintViolations = validator.validate(config); if (constraintViolations.size() > 0) { final StringBuffer msg = new StringBuffer(); for (final ConstraintViolation<CMConfiguration> cv : constraintViolations) { - msg.append(String.format("- Invalid value for %s: %s", - cv.getPropertyPath().toString(), cv.getMessage())); + msg.append(String.format("- Invalid value for %s: %s", cv.getPropertyPath().toString(), cv.getMessage())); } throw new InvalidUriEndpointException(msg.toString()); } http://git-wip-us.apache.org/repos/asf/camel/blob/4f8c9c9b/components/camel-cm/src/main/java/org/apache/camel/component/cm/CMSenderOneMessageImpl.java ---------------------------------------------------------------------- diff --git a/components/camel-cm/src/main/java/org/apache/camel/component/cm/CMSenderOneMessageImpl.java b/components/camel-cm/src/main/java/org/apache/camel/component/cm/CMSenderOneMessageImpl.java index fc69fc5..e1d78c1 100644 --- a/components/camel-cm/src/main/java/org/apache/camel/component/cm/CMSenderOneMessageImpl.java +++ b/components/camel-cm/src/main/java/org/apache/camel/component/cm/CMSenderOneMessageImpl.java @@ -1,263 +1,263 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.cm; - -import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.Charset; -import java.util.UUID; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Result; -import javax.xml.transform.Source; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -import org.w3c.dom.DOMImplementation; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Text; - -import org.apache.camel.component.cm.exceptions.MessagingException; -import org.apache.camel.component.cm.exceptions.ProviderException; -import org.apache.camel.component.cm.exceptions.XMLConstructionException; -import org.apache.camel.component.cm.exceptions.cmresponse.CMResponseException; -import org.apache.camel.component.cm.exceptions.cmresponse.InsufficientBalanceException; -import org.apache.camel.component.cm.exceptions.cmresponse.InvalidProductTokenException; -import org.apache.camel.component.cm.exceptions.cmresponse.NoAccountFoundForProductTokenException; -import org.apache.camel.component.cm.exceptions.cmresponse.UnknownErrorException; -import org.apache.camel.component.cm.exceptions.cmresponse.UnroutableMessageException; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.HttpClientBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class CMSenderOneMessageImpl implements CMSender { - - private static final Logger LOG = LoggerFactory.getLogger(CMSenderOneMessageImpl.class); - - private final String url; - private final UUID productToken; - - public CMSenderOneMessageImpl(final String url, final UUID productToken) { - - this.url = url; - this.productToken = productToken; - } - - /** - * Sends a previously validated SMSMessage to CM endpoint - */ - @Override - public void send(final CMMessage cmMessage) throws MessagingException { - - // See: Check https://dashboard.onlinesmsgateway.com/docs for responses - - try { - - // 1.Construct XML. Throws XMLConstructionException - final String xml = createXml(cmMessage); - - // 2. Try to send to CM SMS Provider ...Throws CMResponse - doHttpPost(url, xml); - } catch (final RuntimeException e) { - LOG.error("Failed to send SMS: {}", cmMessage, e); - throw e; // XMLConstrucion o - } - } - - private String createXml(final CMMessage message) { - - try { - - final ByteArrayOutputStream xml = new ByteArrayOutputStream(); - final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); - - // Get the DocumentBuilder - final DocumentBuilder docBuilder = factory.newDocumentBuilder(); - - // Create blank DOM Document - final DOMImplementation impl = docBuilder.getDOMImplementation(); - final Document doc = impl.createDocument(null, "MESSAGES", null); - - // ROOT Element es MESSAGES - final Element root = doc.getDocumentElement(); - - // AUTHENTICATION element - final Element authenticationElement = doc.createElement("AUTHENTICATION"); - final Element productTokenElement = doc.createElement("PRODUCTTOKEN"); - authenticationElement.appendChild(productTokenElement); - final Text productTokenValue = doc.createTextNode("" + productToken); - productTokenElement.appendChild(productTokenValue); - root.appendChild(authenticationElement); - - // MSG Element - final Element msgElement = doc.createElement("MSG"); - root.appendChild(msgElement); - - // <FROM>VALUE</FROM> - final Element fromElement = doc.createElement("FROM"); - fromElement.appendChild(doc.createTextNode(message.getSender())); - msgElement.appendChild(fromElement); - - // <BODY>VALUE</BODY> - final Element bodyElement = doc.createElement("BODY"); - bodyElement.appendChild(doc.createTextNode(message.getMessage())); - msgElement.appendChild(bodyElement); - - // <TO>VALUE</TO> - final Element toElement = doc.createElement("TO"); - toElement.appendChild(doc.createTextNode(message.getPhoneNumber())); - msgElement.appendChild(toElement); - - // <DCS>VALUE</DCS> - if UNICODE - messageOut.isGSM338Enc - // false - if (message.isUnicode()) { - final Element dcsElement = doc.createElement("DCS"); - dcsElement.appendChild(doc.createTextNode("8")); - msgElement.appendChild(dcsElement); - } - - // <REFERENCE>VALUE</REFERENCE> -Alfanum - final String id = message.getIdAsString(); - if (id != null && !id.isEmpty()) { - final Element refElement = doc.createElement("REFERENCE"); - refElement.appendChild(doc.createTextNode("" + message.getIdAsString())); - msgElement.appendChild(refElement); - } - - // <MINIMUMNUMBEROFMESSAGEPARTS>1</MINIMUMNUMBEROFMESSAGEPARTS> - // <MAXIMUMNUMBEROFMESSAGEPARTS>8</MAXIMUMNUMBEROFMESSAGEPARTS> - if (message.isMultipart()) { - final Element minMessagePartsElement = doc.createElement("MINIMUMNUMBEROFMESSAGEPARTS"); - minMessagePartsElement.appendChild(doc.createTextNode("1")); - msgElement.appendChild(minMessagePartsElement); - - final Element maxMessagePartsElement = doc.createElement("MAXIMUMNUMBEROFMESSAGEPARTS"); - maxMessagePartsElement.appendChild(doc.createTextNode(Integer.toString(message.getMultiparts()))); - msgElement.appendChild(maxMessagePartsElement); - } - - // Creatate XML as String - final Transformer aTransformer = TransformerFactory.newInstance().newTransformer(); - aTransformer.setOutputProperty(OutputKeys.INDENT, "yes"); - final Source src = new DOMSource(doc); - final Result dest = new StreamResult(xml); - aTransformer.transform(src, dest); - return xml.toString(); - } catch (final TransformerException e) { - LOG.error("Cant serialize CMMessage {}: ", message, e); - throw new XMLConstructionException(e); - } catch (final ParserConfigurationException e) { - LOG.error("Cant serialize CMMessage {}: ", message, e); - throw new XMLConstructionException(e); - } - } - - private void doHttpPost(final String urlString, final String requestString) { - - final HttpClient client = HttpClientBuilder.create().build(); - final HttpPost post = new HttpPost(urlString); - post.setEntity(new StringEntity(requestString, Charset.forName("UTF-8"))); - - try { - - final HttpResponse response = client.execute(post); - - final int statusCode = response.getStatusLine().getStatusCode(); - - LOG.debug("Response Code : {}", statusCode); - - if (statusCode == 400) { - throw new ProviderException("CM Component and CM API show some kind of inconsistency. " - + "CM is complaining about not using a post method for the request. And this component only uses POST requests. What happens?"); - } - - if (statusCode != 200) { - throw new ProviderException("CM Component and CM API show some kind of inconsistency. The component expects the status code to be 200 or 400. New api released? "); - } - - // So we have 200 status code... - - // The response type is 'text/plain' and contains the actual - // result of the request processing. - - // We obtaing the result text - final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); - - final StringBuffer result = new StringBuffer(); - String line = null; - while ((line = rd.readLine()) != null) { - result.append(line); - } - - // ... and process it - - line = result.toString(); - if (!line.isEmpty()) { - - // Line is not empty = error - LOG.debug("Result of the request processing: FAILED\n{}", line); - - // The response text contains the error description. We will - // throw a custom exception for each. - - if (line.contains(CMConstants.ERROR_UNKNOWN)) { - throw new UnknownErrorException(); - } else if (line.contains(CMConstants.ERROR_NO_ACCOUNT)) { - throw new NoAccountFoundForProductTokenException(); - } else if (line.contains(CMConstants.ERROR_INSUFICIENT_BALANCE)) { - throw new InsufficientBalanceException(); - } else if (line.contains(CMConstants.ERROR_UNROUTABLE_MESSAGE)) { - throw new UnroutableMessageException(); - } else if (line.contains(CMConstants.ERROR_INVALID_PRODUCT_TOKEN)) { - throw new InvalidProductTokenException(); - } else { - - // SO FAR i would expect other kind of ERROR. - - // MSISDN correctness and message validity is client - // responsibility - throw new CMResponseException("CHECK ME. I am not expecting this. "); - } - } - - // Ok. Line is EMPTY - successfully submitted - LOG.debug("Result of the request processing: Successfully submited"); - } catch (final IOException io) { - throw new ProviderException(io); - } catch (Throwable t) { - if (!(t instanceof MessagingException)) { - // Chain it - t = new MessagingException(t); - } - throw (MessagingException) t; - } - } -} +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.cm; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.util.UUID; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Text; + +import org.apache.camel.component.cm.exceptions.MessagingException; +import org.apache.camel.component.cm.exceptions.ProviderException; +import org.apache.camel.component.cm.exceptions.XMLConstructionException; +import org.apache.camel.component.cm.exceptions.cmresponse.CMResponseException; +import org.apache.camel.component.cm.exceptions.cmresponse.InsufficientBalanceException; +import org.apache.camel.component.cm.exceptions.cmresponse.InvalidProductTokenException; +import org.apache.camel.component.cm.exceptions.cmresponse.NoAccountFoundForProductTokenException; +import org.apache.camel.component.cm.exceptions.cmresponse.UnknownErrorException; +import org.apache.camel.component.cm.exceptions.cmresponse.UnroutableMessageException; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.HttpClientBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CMSenderOneMessageImpl implements CMSender { + + private static final Logger LOG = LoggerFactory.getLogger(CMSenderOneMessageImpl.class); + + private final String url; + private final UUID productToken; + + public CMSenderOneMessageImpl(final String url, final UUID productToken) { + + this.url = url; + this.productToken = productToken; + } + + /** + * Sends a previously validated SMSMessage to CM endpoint + */ + @Override + public void send(final CMMessage cmMessage) throws MessagingException { + + // See: Check https://dashboard.onlinesmsgateway.com/docs for responses + + try { + + // 1.Construct XML. Throws XMLConstructionException + final String xml = createXml(cmMessage); + + // 2. Try to send to CM SMS Provider ...Throws CMResponse + doHttpPost(url, xml); + } catch (final RuntimeException e) { + LOG.error("Failed to send SMS: {}", cmMessage, e); + throw e; // XMLConstrucion o + } + } + + private String createXml(final CMMessage message) { + + try { + + final ByteArrayOutputStream xml = new ByteArrayOutputStream(); + final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + + // Get the DocumentBuilder + final DocumentBuilder docBuilder = factory.newDocumentBuilder(); + + // Create blank DOM Document + final DOMImplementation impl = docBuilder.getDOMImplementation(); + final Document doc = impl.createDocument(null, "MESSAGES", null); + + // ROOT Element es MESSAGES + final Element root = doc.getDocumentElement(); + + // AUTHENTICATION element + final Element authenticationElement = doc.createElement("AUTHENTICATION"); + final Element productTokenElement = doc.createElement("PRODUCTTOKEN"); + authenticationElement.appendChild(productTokenElement); + final Text productTokenValue = doc.createTextNode("" + productToken); + productTokenElement.appendChild(productTokenValue); + root.appendChild(authenticationElement); + + // MSG Element + final Element msgElement = doc.createElement("MSG"); + root.appendChild(msgElement); + + // <FROM>VALUE</FROM> + final Element fromElement = doc.createElement("FROM"); + fromElement.appendChild(doc.createTextNode(message.getSender())); + msgElement.appendChild(fromElement); + + // <BODY>VALUE</BODY> + final Element bodyElement = doc.createElement("BODY"); + bodyElement.appendChild(doc.createTextNode(message.getMessage())); + msgElement.appendChild(bodyElement); + + // <TO>VALUE</TO> + final Element toElement = doc.createElement("TO"); + toElement.appendChild(doc.createTextNode(message.getPhoneNumber())); + msgElement.appendChild(toElement); + + // <DCS>VALUE</DCS> - if UNICODE - messageOut.isGSM338Enc + // false + if (message.isUnicode()) { + final Element dcsElement = doc.createElement("DCS"); + dcsElement.appendChild(doc.createTextNode("8")); + msgElement.appendChild(dcsElement); + } + + // <REFERENCE>VALUE</REFERENCE> -Alfanum + final String id = message.getIdAsString(); + if (id != null && !id.isEmpty()) { + final Element refElement = doc.createElement("REFERENCE"); + refElement.appendChild(doc.createTextNode("" + message.getIdAsString())); + msgElement.appendChild(refElement); + } + + // <MINIMUMNUMBEROFMESSAGEPARTS>1</MINIMUMNUMBEROFMESSAGEPARTS> + // <MAXIMUMNUMBEROFMESSAGEPARTS>8</MAXIMUMNUMBEROFMESSAGEPARTS> + if (message.isMultipart()) { + final Element minMessagePartsElement = doc.createElement("MINIMUMNUMBEROFMESSAGEPARTS"); + minMessagePartsElement.appendChild(doc.createTextNode("1")); + msgElement.appendChild(minMessagePartsElement); + + final Element maxMessagePartsElement = doc.createElement("MAXIMUMNUMBEROFMESSAGEPARTS"); + maxMessagePartsElement.appendChild(doc.createTextNode(Integer.toString(message.getMultiparts()))); + msgElement.appendChild(maxMessagePartsElement); + } + + // Creatate XML as String + final Transformer aTransformer = TransformerFactory.newInstance().newTransformer(); + aTransformer.setOutputProperty(OutputKeys.INDENT, "yes"); + final Source src = new DOMSource(doc); + final Result dest = new StreamResult(xml); + aTransformer.transform(src, dest); + return xml.toString(); + } catch (final TransformerException e) { + LOG.error("Cant serialize CMMessage {}: ", message, e); + throw new XMLConstructionException(e); + } catch (final ParserConfigurationException e) { + LOG.error("Cant serialize CMMessage {}: ", message, e); + throw new XMLConstructionException(e); + } + } + + private void doHttpPost(final String urlString, final String requestString) { + + final HttpClient client = HttpClientBuilder.create().build(); + final HttpPost post = new HttpPost(urlString); + post.setEntity(new StringEntity(requestString, Charset.forName("UTF-8"))); + + try { + + final HttpResponse response = client.execute(post); + + final int statusCode = response.getStatusLine().getStatusCode(); + + LOG.debug("Response Code : {}", statusCode); + + if (statusCode == 400) { + throw new ProviderException("CM Component and CM API show some kind of inconsistency. " + + "CM is complaining about not using a post method for the request. And this component only uses POST requests. What happens?"); + } + + if (statusCode != 200) { + throw new ProviderException("CM Component and CM API show some kind of inconsistency. The component expects the status code to be 200 or 400. New api released? "); + } + + // So we have 200 status code... + + // The response type is 'text/plain' and contains the actual + // result of the request processing. + + // We obtaing the result text + final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); + + final StringBuffer result = new StringBuffer(); + String line = null; + while ((line = rd.readLine()) != null) { + result.append(line); + } + + // ... and process it + + line = result.toString(); + if (!line.isEmpty()) { + + // Line is not empty = error + LOG.debug("Result of the request processing: FAILED\n{}", line); + + // The response text contains the error description. We will + // throw a custom exception for each. + + if (line.contains(CMConstants.ERROR_UNKNOWN)) { + throw new UnknownErrorException(); + } else if (line.contains(CMConstants.ERROR_NO_ACCOUNT)) { + throw new NoAccountFoundForProductTokenException(); + } else if (line.contains(CMConstants.ERROR_INSUFICIENT_BALANCE)) { + throw new InsufficientBalanceException(); + } else if (line.contains(CMConstants.ERROR_UNROUTABLE_MESSAGE)) { + throw new UnroutableMessageException(); + } else if (line.contains(CMConstants.ERROR_INVALID_PRODUCT_TOKEN)) { + throw new InvalidProductTokenException(); + } else { + + // SO FAR i would expect other kind of ERROR. + + // MSISDN correctness and message validity is client + // responsibility + throw new CMResponseException("CHECK ME. I am not expecting this. "); + } + } + + // Ok. Line is EMPTY - successfully submitted + LOG.debug("Result of the request processing: Successfully submited"); + } catch (final IOException io) { + throw new ProviderException(io); + } catch (Throwable t) { + if (!(t instanceof MessagingException)) { + // Chain it + t = new MessagingException(t); + } + throw (MessagingException) t; + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4f8c9c9b/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/CMMessageTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/CMMessageTest.java b/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/CMMessageTest.java index 9eacecb..d796eb9 100644 --- a/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/CMMessageTest.java +++ b/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/CMMessageTest.java @@ -1,323 +1,323 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.cm.test; - -import com.google.i18n.phonenumbers.PhoneNumberUtil; -import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; -import org.apache.camel.component.cm.CMConstants; -import org.apache.camel.component.cm.CMMessage; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.util.Assert; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { ValidatorConfiguration.class }) -// @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) -// @DisableJmx(false) -// @FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class CMMessageTest extends AbstractJUnit4SpringContextTests { - - private final PhoneNumberUtil pnu = PhoneNumberUtil.getInstance(); - private String validNumber; - - @Before - public void beforeTest() throws Exception { - validNumber = pnu.format(pnu.getExampleNumber("ES"), PhoneNumberFormat.E164); - } - - // @After - // public void afterTest() { - - /* - * GSM0338 - */ - - @Test - public void testGSM338AndLTMAXGSMMESSAGELENGTH() throws Exception { - - // 0338 and less than 160 char -> 1 part - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < CMConstants.MAX_GSM_MESSAGE_LENGTH; index++) { - message.append("a"); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 1); - Assert.isTrue(!cmMessage.isUnicode()); - } - - @Test - public void testGSM338AndEQMAXGSMMESSAGELENGTH() throws Exception { - // 0338 and length is exactly 160 -> 1 part - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < CMConstants.MAX_GSM_MESSAGE_LENGTH; index++) { - message.append("a"); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 1); - Assert.isTrue(!cmMessage.isUnicode()); - } - - @Test - public void testGSM338AndGTMAXGSMMESSAGELENGTH() throws Exception { - - // 0338 and length is exactly 161 -> 2 part - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < CMConstants.MAX_GSM_MESSAGE_LENGTH + 1; index++) { - message.append("a"); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 2); - Assert.isTrue(!cmMessage.isUnicode()); - } - - @Test - public void testGSM338AndLT2MAXGSMMESSAGELENGTH() throws Exception { - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < (2 * CMConstants.MAX_GSM_MESSAGE_LENGTH_PER_PART_IF_MULTIPART - 1); index++) { - message.append("a"); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 2); - Assert.isTrue(!cmMessage.isUnicode()); - } - - @Test - public void testGSM338AndEQ2MAXGSMMESSAGELENGTH() throws Exception { - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < (2 * CMConstants.MAX_GSM_MESSAGE_LENGTH_PER_PART_IF_MULTIPART); index++) { - message.append("a"); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 2); - Assert.isTrue(!cmMessage.isUnicode()); - } - - @Test - public void testGSM338AndGT2MAXGSMMESSAGELENGTH() throws Exception { - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < (2 * CMConstants.MAX_GSM_MESSAGE_LENGTH_PER_PART_IF_MULTIPART + 1); index++) { - message.append("a"); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 3); - Assert.isTrue(!cmMessage.isUnicode()); - } - - @Test - public void testGSM338AndEQ8MAXGSMMESSAGELENGTH() throws Exception { - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < (8 * CMConstants.MAX_GSM_MESSAGE_LENGTH_PER_PART_IF_MULTIPART); index++) { - message.append("a"); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 8); - Assert.isTrue(!cmMessage.isUnicode()); - } - - @Test - public void testGSM338AndGT8MAXGSMMESSAGELENGTH() throws Exception { - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < (8 * CMConstants.MAX_GSM_MESSAGE_LENGTH_PER_PART_IF_MULTIPART + 1); index++) { - message.append("a"); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 8); - Assert.isTrue(!cmMessage.isUnicode()); - } - - /* - * Unicode Messages - */ - - @Test - public void testUnicodeAndLTMAXGSMMESSAGELENGTH() throws Exception { - - String ch = "\uF400"; - - // 0338 and less than 160 char -> 1 part - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < CMConstants.MAX_UNICODE_MESSAGE_LENGTH; index++) { - message.append(ch); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 1); - Assert.isTrue(cmMessage.isUnicode()); - } - - @Test - public void testUnicodeAndEQMAXGSMMESSAGELENGTH() throws Exception { - // 0338 and length is exactly 160 -> 1 part - - String ch = "\uF400"; - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < CMConstants.MAX_UNICODE_MESSAGE_LENGTH; index++) { - message.append(ch); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 1); - Assert.isTrue(cmMessage.isUnicode()); - } - - @Test - public void testUnicodeAndGTMAXGSMMESSAGELENGTH() throws Exception { - - // 0338 and length is exactly 161 -> 2 part - - String ch = "\uF400"; - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < CMConstants.MAX_UNICODE_MESSAGE_LENGTH + 1; index++) { - message.append(ch); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 2); - Assert.isTrue(cmMessage.isUnicode()); - } - - @Test - public void testUnicodeAndLT2MAXGSMMESSAGELENGTH() throws Exception { - - String ch = "\uF400"; - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < (2 * CMConstants.MAX_UNICODE_MESSAGE_LENGTH_PER_PART_IF_MULTIPART - 1); index++) { - message.append(ch); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 2); - Assert.isTrue(cmMessage.isUnicode()); - } - - @Test - public void testUnicodeAndEQ2MAXGSMMESSAGELENGTH() throws Exception { - - String ch = "\uF400"; - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < (2 * CMConstants.MAX_UNICODE_MESSAGE_LENGTH_PER_PART_IF_MULTIPART); index++) { - message.append(ch); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 2); - Assert.isTrue(cmMessage.isUnicode()); - } - - @Test - public void testUnicodeAndGT2MAXGSMMESSAGELENGTH() throws Exception { - - String ch = "\uF400"; - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < (2 * CMConstants.MAX_UNICODE_MESSAGE_LENGTH_PER_PART_IF_MULTIPART + 1); index++) { - message.append(ch); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 3); - Assert.isTrue(cmMessage.isUnicode()); - } - - @Test - public void testUnicodeAndEQ8MAXGSMMESSAGELENGTH() throws Exception { - - String ch = "\uF400"; - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < (8 * CMConstants.MAX_UNICODE_MESSAGE_LENGTH_PER_PART_IF_MULTIPART); index++) { - message.append(ch); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 8); - Assert.isTrue(cmMessage.isUnicode()); - } - - @Test - public void testUnicodeAndGT8MAXGSMMESSAGELENGTH() throws Exception { - - String ch = "\uF400"; - - StringBuffer message = new StringBuffer(); - for (int index = 0; index < (8 * CMConstants.MAX_GSM_MESSAGE_LENGTH_PER_PART_IF_MULTIPART + 1); index++) { - message.append(ch); - } - - final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); - cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); - - Assert.isTrue(cmMessage.getMultiparts() == 8); - Assert.isTrue(cmMessage.isUnicode()); - } - -} +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.cm.test; + +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; +import org.apache.camel.component.cm.CMConstants; +import org.apache.camel.component.cm.CMMessage; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.util.Assert; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { ValidatorConfiguration.class }) +// @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) +// @DisableJmx(false) +// @FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class CMMessageTest extends AbstractJUnit4SpringContextTests { + + private final PhoneNumberUtil pnu = PhoneNumberUtil.getInstance(); + private String validNumber; + + @Before + public void beforeTest() throws Exception { + validNumber = pnu.format(pnu.getExampleNumber("ES"), PhoneNumberFormat.E164); + } + + // @After + // public void afterTest() { + + /* + * GSM0338 + */ + + @Test + public void testGSM338AndLTMAXGSMMESSAGELENGTH() throws Exception { + + // 0338 and less than 160 char -> 1 part + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < CMConstants.MAX_GSM_MESSAGE_LENGTH; index++) { + message.append("a"); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 1); + Assert.isTrue(!cmMessage.isUnicode()); + } + + @Test + public void testGSM338AndEQMAXGSMMESSAGELENGTH() throws Exception { + // 0338 and length is exactly 160 -> 1 part + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < CMConstants.MAX_GSM_MESSAGE_LENGTH; index++) { + message.append("a"); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 1); + Assert.isTrue(!cmMessage.isUnicode()); + } + + @Test + public void testGSM338AndGTMAXGSMMESSAGELENGTH() throws Exception { + + // 0338 and length is exactly 161 -> 2 part + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < CMConstants.MAX_GSM_MESSAGE_LENGTH + 1; index++) { + message.append("a"); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 2); + Assert.isTrue(!cmMessage.isUnicode()); + } + + @Test + public void testGSM338AndLT2MAXGSMMESSAGELENGTH() throws Exception { + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < (2 * CMConstants.MAX_GSM_MESSAGE_LENGTH_PER_PART_IF_MULTIPART - 1); index++) { + message.append("a"); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 2); + Assert.isTrue(!cmMessage.isUnicode()); + } + + @Test + public void testGSM338AndEQ2MAXGSMMESSAGELENGTH() throws Exception { + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < (2 * CMConstants.MAX_GSM_MESSAGE_LENGTH_PER_PART_IF_MULTIPART); index++) { + message.append("a"); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 2); + Assert.isTrue(!cmMessage.isUnicode()); + } + + @Test + public void testGSM338AndGT2MAXGSMMESSAGELENGTH() throws Exception { + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < (2 * CMConstants.MAX_GSM_MESSAGE_LENGTH_PER_PART_IF_MULTIPART + 1); index++) { + message.append("a"); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 3); + Assert.isTrue(!cmMessage.isUnicode()); + } + + @Test + public void testGSM338AndEQ8MAXGSMMESSAGELENGTH() throws Exception { + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < (8 * CMConstants.MAX_GSM_MESSAGE_LENGTH_PER_PART_IF_MULTIPART); index++) { + message.append("a"); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 8); + Assert.isTrue(!cmMessage.isUnicode()); + } + + @Test + public void testGSM338AndGT8MAXGSMMESSAGELENGTH() throws Exception { + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < (8 * CMConstants.MAX_GSM_MESSAGE_LENGTH_PER_PART_IF_MULTIPART + 1); index++) { + message.append("a"); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 8); + Assert.isTrue(!cmMessage.isUnicode()); + } + + /* + * Unicode Messages + */ + + @Test + public void testUnicodeAndLTMAXGSMMESSAGELENGTH() throws Exception { + + String ch = "\uF400"; + + // 0338 and less than 160 char -> 1 part + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < CMConstants.MAX_UNICODE_MESSAGE_LENGTH; index++) { + message.append(ch); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 1); + Assert.isTrue(cmMessage.isUnicode()); + } + + @Test + public void testUnicodeAndEQMAXGSMMESSAGELENGTH() throws Exception { + // 0338 and length is exactly 160 -> 1 part + + String ch = "\uF400"; + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < CMConstants.MAX_UNICODE_MESSAGE_LENGTH; index++) { + message.append(ch); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 1); + Assert.isTrue(cmMessage.isUnicode()); + } + + @Test + public void testUnicodeAndGTMAXGSMMESSAGELENGTH() throws Exception { + + // 0338 and length is exactly 161 -> 2 part + + String ch = "\uF400"; + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < CMConstants.MAX_UNICODE_MESSAGE_LENGTH + 1; index++) { + message.append(ch); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 2); + Assert.isTrue(cmMessage.isUnicode()); + } + + @Test + public void testUnicodeAndLT2MAXGSMMESSAGELENGTH() throws Exception { + + String ch = "\uF400"; + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < (2 * CMConstants.MAX_UNICODE_MESSAGE_LENGTH_PER_PART_IF_MULTIPART - 1); index++) { + message.append(ch); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 2); + Assert.isTrue(cmMessage.isUnicode()); + } + + @Test + public void testUnicodeAndEQ2MAXGSMMESSAGELENGTH() throws Exception { + + String ch = "\uF400"; + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < (2 * CMConstants.MAX_UNICODE_MESSAGE_LENGTH_PER_PART_IF_MULTIPART); index++) { + message.append(ch); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 2); + Assert.isTrue(cmMessage.isUnicode()); + } + + @Test + public void testUnicodeAndGT2MAXGSMMESSAGELENGTH() throws Exception { + + String ch = "\uF400"; + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < (2 * CMConstants.MAX_UNICODE_MESSAGE_LENGTH_PER_PART_IF_MULTIPART + 1); index++) { + message.append(ch); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 3); + Assert.isTrue(cmMessage.isUnicode()); + } + + @Test + public void testUnicodeAndEQ8MAXGSMMESSAGELENGTH() throws Exception { + + String ch = "\uF400"; + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < (8 * CMConstants.MAX_UNICODE_MESSAGE_LENGTH_PER_PART_IF_MULTIPART); index++) { + message.append(ch); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 8); + Assert.isTrue(cmMessage.isUnicode()); + } + + @Test + public void testUnicodeAndGT8MAXGSMMESSAGELENGTH() throws Exception { + + String ch = "\uF400"; + + StringBuffer message = new StringBuffer(); + for (int index = 0; index < (8 * CMConstants.MAX_GSM_MESSAGE_LENGTH_PER_PART_IF_MULTIPART + 1); index++) { + message.append(ch); + } + + final CMMessage cmMessage = new CMMessage(validNumber, message.toString()); + cmMessage.setUnicodeAndMultipart(CMConstants.DEFAULT_MULTIPARTS); + + Assert.isTrue(cmMessage.getMultiparts() == 8); + Assert.isTrue(cmMessage.isUnicode()); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/4f8c9c9b/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/CMTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/CMTest.java b/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/CMTest.java index bb4ed83..9fac037 100644 --- a/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/CMTest.java +++ b/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/CMTest.java @@ -19,6 +19,8 @@ package org.apache.camel.component.cm.test; import java.math.BigInteger; import java.security.SecureRandom; +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; import org.apache.camel.CamelContext; import org.apache.camel.EndpointInject; import org.apache.camel.InvalidPayloadRuntimeException; @@ -61,9 +63,6 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import org.springframework.util.Assert; -import com.google.i18n.phonenumbers.PhoneNumberUtil; -import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; - @RunWith(CamelSpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { CamelTestConfiguration.class }, loader = CamelSpringDelegatingTestContextLoader.class) http://git-wip-us.apache.org/repos/asf/camel/blob/4f8c9c9b/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/CamelTestConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/CamelTestConfiguration.java b/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/CamelTestConfiguration.java index b6a136d..54a3356 100644 --- a/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/CamelTestConfiguration.java +++ b/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/CamelTestConfiguration.java @@ -1,100 +1,112 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.cm.test; - -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.core.env.Environment; -import org.springframework.util.Assert; -import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; - -/** - * Builds a SimpleRoute to send a message to CM GW and CM Uri is built based on properties in a file. - */ -@Configuration("cmConfig") -@PropertySource("classpath:/cm-smsgw.properties") -public class CamelTestConfiguration extends SingleRouteCamelConfiguration { - - public static final String SIMPLE_ROUTE_ID = "simple-route"; - - private String uri; - - @Override - public RouteBuilder route() { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - - Assert.hasLength(uri); - - log.info("\nCM Component is an URI based component\nCM URI: {}", uri); - - // Route definition - from("direct:sms").to(uri).to("mock:test").routeId(SIMPLE_ROUTE_ID).autoStartup(true); - - } - }; - } - - @Bean - public LocalValidatorFactoryBean getValidatorFactory() { - final LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean(); - localValidatorFactoryBean.getValidationPropertyMap().put("hibernate.validator.fail_fast", "true"); - return localValidatorFactoryBean; - } - - /** - * Build the URI of the CM Component based on Environmental properties - */ - @Override - public final void setApplicationContext(final ApplicationContext applicationContext) { - - super.setApplicationContext(applicationContext); - - final Environment env = applicationContext.getEnvironment(); - - final String host = env.getRequiredProperty("cm.url"); - final String productTokenString = env.getRequiredProperty("cm.product-token"); - final String sender = env.getRequiredProperty("cm.default-sender"); - - final StringBuffer cmUri = new StringBuffer("cm:" + host).append("?productToken=").append(productTokenString); - if (sender != null && !sender.isEmpty()) { - cmUri.append("&defaultFrom=").append(sender); - } - - // Defaults to false - final Boolean testConnectionOnStartup = Boolean.parseBoolean(env.getProperty("cm.testConnectionOnStartup", "false")); - if (testConnectionOnStartup) { - cmUri.append("&testConnectionOnStartup=").append(testConnectionOnStartup.toString()); - } - - // Defaults to 8 - final Integer defaultMaxNumberOfParts = Integer.parseInt(env.getProperty("defaultMaxNumberOfParts", "8")); - cmUri.append("&defaultMaxNumberOfParts=").append(defaultMaxNumberOfParts.toString()); - - uri = cmUri.toString(); - } - - public String getUri() { - return uri; - } -} +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.cm.test; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.util.Assert; +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; + +/** + * Builds a SimpleRoute to send a message to CM GW and CM Uri is built based on + * properties in a file. + */ +@Configuration("cmConfig") +@PropertySource("classpath:/cm-smsgw.properties") +public class CamelTestConfiguration extends SingleRouteCamelConfiguration { + + public static final String SIMPLE_ROUTE_ID = "simple-route"; + + private String uri; + + @Override + public RouteBuilder route() { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + + Assert.hasLength(uri); + + log.debug( + "\nCM Component is an URI based component\nCM URI: {}", + uri); + + // Route definition + from("direct:sms").to(uri).to("mock:test") + .routeId(SIMPLE_ROUTE_ID).autoStartup(true); + + } + }; + } + + @Bean + public LocalValidatorFactoryBean getValidatorFactory() { + final LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean(); + localValidatorFactoryBean.getValidationPropertyMap() + .put("hibernate.validator.fail_fast", "true"); + return localValidatorFactoryBean; + } + + /** + * Build the URI of the CM Component based on Environmental properties + */ + @Override + public final void setApplicationContext( + final ApplicationContext applicationContext) { + + super.setApplicationContext(applicationContext); + + final Environment env = applicationContext.getEnvironment(); + + final String host = env.getRequiredProperty("cm.url"); + final String productTokenString = env + .getRequiredProperty("cm.product-token"); + final String sender = env.getRequiredProperty("cm.default-sender"); + + final StringBuffer cmUri = new StringBuffer("cm:" + host) + .append("?productToken=").append(productTokenString); + if (sender != null && !sender.isEmpty()) { + cmUri.append("&defaultFrom=").append(sender); + } + + // Defaults to false + final Boolean testConnectionOnStartup = Boolean.parseBoolean( + env.getProperty("cm.testConnectionOnStartup", "false")); + if (testConnectionOnStartup) { + cmUri.append("&testConnectionOnStartup=") + .append(testConnectionOnStartup.toString()); + } + + // Defaults to 8 + final Integer defaultMaxNumberOfParts = Integer + .parseInt(env.getProperty("defaultMaxNumberOfParts", "8")); + cmUri.append("&defaultMaxNumberOfParts=") + .append(defaultMaxNumberOfParts.toString()); + + uri = cmUri.toString(); + } + + public String getUri() { + return uri; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4f8c9c9b/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/SMSMessageTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/SMSMessageTest.java b/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/SMSMessageTest.java index 63bfced..8d653d3 100644 --- a/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/SMSMessageTest.java +++ b/components/camel-cm/src/test/java/org/apache/camel/component/cm/test/SMSMessageTest.java @@ -1,183 +1,183 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.cm.test; - -import java.util.Set; - -import javax.validation.ConstraintViolation; -import javax.validation.Validator; - -import com.google.i18n.phonenumbers.PhoneNumberUtil; -import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; -import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; -import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource; -import org.apache.camel.component.cm.client.SMSMessage; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.util.Assert; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = {ValidatorConfiguration.class }) -// @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) -// @DisableJmx(false) -// @FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class SMSMessageTest extends AbstractJUnit4SpringContextTests { - - @Autowired - private Validator validator; - - private final PhoneNumberUtil pnu = PhoneNumberUtil.getInstance(); - private String validNumber; - - @Before - public void beforeTest() throws Exception { - - validNumber = pnu.format(pnu.getExampleNumber("ES"), PhoneNumberFormat.E164); - } - - // @After - // public void afterTest() { - - @Test - public void testSMSMessageConstructor() throws Throwable { - - // Coverage ;) - SMSMessage message = new SMSMessage(null, null); - Assert.isNull(message.getMessage()); - Assert.isNull(message.getPhoneNumber()); - - message = new SMSMessage("idAsString", null, null, "MySelf"); - Assert.isTrue(message.getId().equals("idAsString")); - Assert.isTrue(message.getFrom().equals("MySelf")); - } - - @Test - public void testNullMessageField() throws Exception { - - final SMSMessage m = new SMSMessage(null, validNumber); - - final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); - Assert.isTrue(1 == constraintViolations.size()); - } - - @Test - public void testNullPhoneNumber() throws Exception { - - final SMSMessage m = new SMSMessage("Hello world!", null); - - final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); - Assert.isTrue(1 == constraintViolations.size()); - } - - @Test - public void testDynamicFromFieldMaxLength() throws Exception { - - String dynamicFrom = "messagelengthgreaterthan12"; - - final SMSMessage m = new SMSMessage("idAsString", "Hello World", validNumber, dynamicFrom); - - final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); - Assert.isTrue(1 == constraintViolations.size()); - } - - @Test - public void testDynamicFromFieldZeroLength() throws Exception { - - String zeroLengthDynamicFrom = ""; - - final SMSMessage m = new SMSMessage("idAsString", "Hello World", validNumber, zeroLengthDynamicFrom); - - final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); - Assert.isTrue(1 == constraintViolations.size()); - } - - @Test - public void testIdAsStringMaxLength() throws Exception { - - String idAsString = "thisistheidastringlengthgreaterthan32"; - - final SMSMessage m = new SMSMessage(idAsString, "Hello World", validNumber, "MySelf"); - - final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); - Assert.isTrue(1 == constraintViolations.size()); - } - - @Test - public void testIdAsStringFieldZeroLength() throws Exception { - - String zeroLengthIdAsString = ""; - - final SMSMessage m = new SMSMessage(zeroLengthIdAsString, "Hello World", validNumber, "MySelf"); - - final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); - Assert.isTrue(1 == constraintViolations.size()); - } - - @Test - public void testE164NullNumberIsInValid() throws Exception { - - final String phoneNumber = null; - final SMSMessage m = new SMSMessage("Hello world!", phoneNumber); - - final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); - Assert.isTrue(1 == constraintViolations.size()); - } - - @Test - public void testE164IsValid() throws Exception { - - final SMSMessage m = new SMSMessage("Hello world!", validNumber); - - final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); - Assert.isTrue(0 == constraintViolations.size()); - } - - @Test - public void testE164NoPlusSignedNumberIsInvalid() throws Exception { - - final String phoneNumber = "34600000000"; - final SMSMessage m = new SMSMessage("Hello world!", phoneNumber); - - final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); - Assert.isTrue(1 == constraintViolations.size()); - } - - @Test - public void testE164NoPlusSignedNumberBut00IsInvalid() throws Exception { - - final String phoneNumber = new PhoneNumber().setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN).setNationalNumber(0034600000000).toString(); - final SMSMessage m = new SMSMessage("Hello world!", phoneNumber); - - final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); - Assert.isTrue(1 == constraintViolations.size()); - } - - @Test - public void testE164NumberWithPlusSignIsInvalid() throws Exception { - - final String phoneNumber = "+34 600 00 00 00"; - final SMSMessage m = new SMSMessage("Hello world!", phoneNumber); - - final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); - Assert.isTrue(1 == constraintViolations.size()); - } -} +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.cm.test; + +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.Validator; + +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; +import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; +import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource; +import org.apache.camel.component.cm.client.SMSMessage; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.util.Assert; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = {ValidatorConfiguration.class }) +// @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) +// @DisableJmx(false) +// @FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class SMSMessageTest extends AbstractJUnit4SpringContextTests { + + @Autowired + private Validator validator; + + private final PhoneNumberUtil pnu = PhoneNumberUtil.getInstance(); + private String validNumber; + + @Before + public void beforeTest() throws Exception { + + validNumber = pnu.format(pnu.getExampleNumber("ES"), PhoneNumberFormat.E164); + } + + // @After + // public void afterTest() { + + @Test + public void testSMSMessageConstructor() throws Throwable { + + // Coverage ;) + SMSMessage message = new SMSMessage(null, null); + Assert.isNull(message.getMessage()); + Assert.isNull(message.getPhoneNumber()); + + message = new SMSMessage("idAsString", null, null, "MySelf"); + Assert.isTrue(message.getId().equals("idAsString")); + Assert.isTrue(message.getFrom().equals("MySelf")); + } + + @Test + public void testNullMessageField() throws Exception { + + final SMSMessage m = new SMSMessage(null, validNumber); + + final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); + Assert.isTrue(1 == constraintViolations.size()); + } + + @Test + public void testNullPhoneNumber() throws Exception { + + final SMSMessage m = new SMSMessage("Hello world!", null); + + final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); + Assert.isTrue(1 == constraintViolations.size()); + } + + @Test + public void testDynamicFromFieldMaxLength() throws Exception { + + String dynamicFrom = "messagelengthgreaterthan12"; + + final SMSMessage m = new SMSMessage("idAsString", "Hello World", validNumber, dynamicFrom); + + final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); + Assert.isTrue(1 == constraintViolations.size()); + } + + @Test + public void testDynamicFromFieldZeroLength() throws Exception { + + String zeroLengthDynamicFrom = ""; + + final SMSMessage m = new SMSMessage("idAsString", "Hello World", validNumber, zeroLengthDynamicFrom); + + final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); + Assert.isTrue(1 == constraintViolations.size()); + } + + @Test + public void testIdAsStringMaxLength() throws Exception { + + String idAsString = "thisistheidastringlengthgreaterthan32"; + + final SMSMessage m = new SMSMessage(idAsString, "Hello World", validNumber, "MySelf"); + + final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); + Assert.isTrue(1 == constraintViolations.size()); + } + + @Test + public void testIdAsStringFieldZeroLength() throws Exception { + + String zeroLengthIdAsString = ""; + + final SMSMessage m = new SMSMessage(zeroLengthIdAsString, "Hello World", validNumber, "MySelf"); + + final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); + Assert.isTrue(1 == constraintViolations.size()); + } + + @Test + public void testE164NullNumberIsInValid() throws Exception { + + final String phoneNumber = null; + final SMSMessage m = new SMSMessage("Hello world!", phoneNumber); + + final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); + Assert.isTrue(1 == constraintViolations.size()); + } + + @Test + public void testE164IsValid() throws Exception { + + final SMSMessage m = new SMSMessage("Hello world!", validNumber); + + final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); + Assert.isTrue(0 == constraintViolations.size()); + } + + @Test + public void testE164NoPlusSignedNumberIsInvalid() throws Exception { + + final String phoneNumber = "34600000000"; + final SMSMessage m = new SMSMessage("Hello world!", phoneNumber); + + final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); + Assert.isTrue(1 == constraintViolations.size()); + } + + @Test + public void testE164NoPlusSignedNumberBut00IsInvalid() throws Exception { + + final String phoneNumber = new PhoneNumber().setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN).setNationalNumber(0034600000000).toString(); + final SMSMessage m = new SMSMessage("Hello world!", phoneNumber); + + final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); + Assert.isTrue(1 == constraintViolations.size()); + } + + @Test + public void testE164NumberWithPlusSignIsInvalid() throws Exception { + + final String phoneNumber = "+34 600 00 00 00"; + final SMSMessage m = new SMSMessage("Hello world!", phoneNumber); + + final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m); + Assert.isTrue(1 == constraintViolations.size()); + } +}