You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/10/20 14:44:47 UTC

[sling-org-apache-sling-testing-clients] 28/37: SLING-6954 - Add client support for the org.apache.sling.testing.email bundle

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-clients.git

commit bddb9f839284ccad7da7f48ffc8855fca3609fb7
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Tue Jun 13 13:13:43 2017 +0000

    SLING-6954 - Add client support for the org.apache.sling.testing.email
    bundle
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1798597 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/testing/clients/email/EmailMessage.java  |  36 +++++++
 .../testing/clients/email/SlingEmailClient.java    | 113 +++++++++++++++++++++
 .../sling/testing/clients/email/package-info.java  |  23 +++++
 3 files changed, 172 insertions(+)

diff --git a/src/main/java/org/apache/sling/testing/clients/email/EmailMessage.java b/src/main/java/org/apache/sling/testing/clients/email/EmailMessage.java
new file mode 100644
index 0000000..e013e51
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/clients/email/EmailMessage.java
@@ -0,0 +1,36 @@
+/*
+ * 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.sling.testing.clients.email;
+
+/**
+ * Holds information retrieved from the mock SMTP server deployed in Sling
+ *
+ */
+public final class EmailMessage {
+	
+	private String content;
+
+	public EmailMessage(String content) {
+		this.content = content;
+	}
+	
+	public String getContent() {
+		return content;
+	}
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/testing/clients/email/SlingEmailClient.java b/src/main/java/org/apache/sling/testing/clients/email/SlingEmailClient.java
new file mode 100644
index 0000000..65bdaa1
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/clients/email/SlingEmailClient.java
@@ -0,0 +1,113 @@
+/*
+ * 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.sling.testing.clients.email;
+
+import static org.apache.http.HttpStatus.SC_NO_CONTENT;
+import static org.apache.http.HttpStatus.SC_OK;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.http.Header;
+import org.apache.http.NameValuePair;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.sling.testing.clients.ClientException;
+import org.apache.sling.testing.clients.SlingClient;
+import org.apache.sling.testing.clients.SlingClientConfig;
+import org.apache.sling.testing.clients.SlingHttpResponse;
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.map.ObjectMapper;
+
+/**
+ * Accesses email stored by a mock SMTP server deployed to Sling 
+ *
+ * <p>Requires that the <tt>org.apache.sling.testing.email</tt> bundle is deployed.</p>
+ */
+public final class SlingEmailClient extends SlingClient {
+
+	/**
+	 * The well-known path under which the EmailServlet is deployed
+	 */
+	private static final String EMAIL_SERVLET_PATH = "/system/sling/testing/email";
+	
+	/**
+	 * The well-known property name of the email body contents
+	 */
+	private static final String PN_CONTENT = "-Content-";
+	
+	
+	private final ObjectMapper mapper = new ObjectMapper();
+
+	public SlingEmailClient(CloseableHttpClient http, SlingClientConfig config) throws ClientException {
+		super(http, config);
+	}
+	
+	/**
+	 * Retrieves the actual bind port of the SMTP server
+	 * 
+	 * @return the port value
+	 * @throws ClientException in case of any errors
+	 */
+	public int getBindPort() throws ClientException {
+        try {
+			SlingHttpResponse mockEmailConfig = doGet(EMAIL_SERVLET_PATH + "/config", SC_OK);
+			
+			JsonNode configNode = mapper.readTree(mockEmailConfig.getContent());
+			return configNode.get("bindPort").getIntValue();
+		} catch (IOException e) {
+			throw new ClientException("Failed retrieving configuration", e);
+		}
+	}
+	
+	/**
+	 * Retrieves the list of mail messages currently stored
+	 * 
+	 * @return the list of messages, possibly empty
+	 * @throws ClientException in case of any errors
+	 */
+	public List<EmailMessage> getMessages() throws ClientException {
+    	List<EmailMessage> emails = new ArrayList<>();
+    	
+        try {
+			SlingHttpResponse response = doGet(EMAIL_SERVLET_PATH + "/messages", SC_OK);
+			JsonNode messages = mapper.readTree(response.getContent());
+			for ( JsonNode emailNode : messages.get("messages") ) {
+				EmailMessage msg = new EmailMessage(emailNode.get(PN_CONTENT).getTextValue());
+				emails.add(msg);
+			}
+		} catch (IOException e) {
+			throw new ClientException("Failed retrieving email messages", e);
+		}
+        
+        
+        return emails;		
+	}
+	
+	/**
+	 * Deletes all mail messages currently stored
+	 * 
+	 * @throws ClientException in case of any errors
+	 */
+	public void deleteMessages() throws ClientException {
+		doDelete(EMAIL_SERVLET_PATH, Collections.<NameValuePair>emptyList(), 
+				Collections.<Header> emptyList(), SC_NO_CONTENT);		
+	}
+}
diff --git a/src/main/java/org/apache/sling/testing/clients/email/package-info.java b/src/main/java/org/apache/sling/testing/clients/email/package-info.java
new file mode 100644
index 0000000..373bccc
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/clients/email/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+@Version("1.0.0")
+package org.apache.sling.testing.clients.email;
+
+import org.osgi.annotation.versioning.Version;

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.