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 2023/03/06 22:33:44 UTC

[sling-org-apache-sling-servlet-helpers] 01/01: SLING-11745 - Sling Mocks: allow setting parts for a request

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

rombert pushed a commit to branch feature/SLING-11745
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlet-helpers.git

commit bfde2fac784a0b10efee415065a5da565e8fc118
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Mon Mar 6 23:32:36 2023 +0100

    SLING-11745 - Sling Mocks: allow setting parts for a request
    
    Allow setting parts on a MockSlingHttpServletRequest and provide a simple ByteArrayPart
    implementation.
---
 pom.xml                                            |   6 +
 .../apache/sling/servlethelpers/ByteArrayPart.java | 128 +++++++++++++++++++++
 .../MockSlingHttpServletRequest.java               |  30 +++--
 .../apache/sling/servlethelpers/package-info.java  |   2 +-
 .../MockSlingHttpServletRequestTest.java           |  30 +++++
 5 files changed, 185 insertions(+), 11 deletions(-)

diff --git a/pom.xml b/pom.xml
index 2d81cbd..254a567 100644
--- a/pom.xml
+++ b/pom.xml
@@ -171,6 +171,12 @@
             <version>3.1.0</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <version>3.11.1</version>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.ops4j.pax.exam</groupId>
             <artifactId>pax-exam</artifactId>
diff --git a/src/main/java/org/apache/sling/servlethelpers/ByteArrayPart.java b/src/main/java/org/apache/sling/servlethelpers/ByteArrayPart.java
new file mode 100644
index 0000000..83535d4
--- /dev/null
+++ b/src/main/java/org/apache/sling/servlethelpers/ByteArrayPart.java
@@ -0,0 +1,128 @@
+/*
+ * 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.servlethelpers;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collection;
+
+import javax.servlet.http.Part;
+
+/**
+ * Simple <tt>Part</tt> implementation backed by an in-memory byte array
+ *
+ */
+public class ByteArrayPart implements Part {
+	
+	/**
+	 * Returns a <tt>Builder</tt> instance used to create a <tt>ByteArrayPart</tt> 
+	 * 
+	 * @return a new builder instance
+	 */
+	public static Builder builder() {
+		return new Builder();
+	}
+	
+	private final byte[] content;
+	private final String name;
+	
+	private ByteArrayPart(byte[] content, String name) {
+		if ( content == null )
+			throw new IllegalArgumentException("content may not be null");
+		
+		if ( name == null || name.isEmpty() )
+			throw new IllegalArgumentException("name may not be null or empty");
+		
+		this.content = content;
+		this.name = name;
+	}
+
+	@Override
+	public InputStream getInputStream() throws IOException {
+		return new ByteArrayInputStream(content);
+	}
+
+	@Override
+	public String getContentType() {
+		return null;
+	}
+
+	@Override
+	public String getName() {
+		return name;
+	}
+
+	@Override
+	public String getSubmittedFileName() {
+		return getName();
+	}
+
+	@Override
+	public long getSize() {
+		return content.length;
+	}
+
+	@Override
+	public void write(String fileName) throws IOException {
+		throw new UnsupportedOperationException();
+
+	}
+
+	@Override
+	public void delete() throws IOException {
+		throw new UnsupportedOperationException();
+	}
+
+	@Override
+	public String getHeader(String name) {
+		throw new UnsupportedOperationException();
+	}
+
+	@Override
+	public Collection<String> getHeaders(String name) {
+		throw new UnsupportedOperationException();
+	}
+
+	@Override
+	public Collection<String> getHeaderNames() {
+		throw new UnsupportedOperationException();
+	}
+	
+	public static class Builder {
+
+		private byte[] content;
+		private String name;
+		
+		public Builder withContent(byte[] content) {
+			this.content = content;
+			return this;
+		}
+		
+		public Builder withName(String name) {
+			this.name = name;
+			return this;
+		}
+		
+		public ByteArrayPart build() {
+			return new ByteArrayPart(content, name);
+		}
+	}
+
+}
diff --git a/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequest.java b/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequest.java
index 45e0a8e..bc1a3d2 100644
--- a/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequest.java
+++ b/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequest.java
@@ -107,6 +107,7 @@ public class MockSlingHttpServletRequest extends SlingAdaptable implements Sling
     private Locale locale = Locale.US;
     private boolean getInputStreamCalled;
     private boolean getReaderCalled;
+    private List<Part> parts = new ArrayList<Part>();
 
     private MockRequestDispatcherFactory requestDispatcherFactory;
     private String responseContentType;
@@ -860,6 +861,25 @@ public class MockSlingHttpServletRequest extends SlingAdaptable implements Sling
     public RequestProgressTracker getRequestProgressTracker() {
         return new MockRequestProgressTracker();
     }
+    
+    public void addPart(Part part) {
+    	if ( part == null )
+    		throw new IllegalArgumentException("part may not be null");
+    	this.parts.add(part);
+    }
+    
+    @Override
+    public Collection<Part> getParts() {
+        return parts;
+    }
+
+    @Override
+    public Part getPart(String name) {
+        return parts.stream()
+			.filter( p -> p.getName().equals(name))
+			.findFirst()
+			.orElse(null);
+    }
 
     // --- unsupported operations ---
 
@@ -948,16 +968,6 @@ public class MockSlingHttpServletRequest extends SlingAdaptable implements Sling
         throw new UnsupportedOperationException();
     }
 
-    @Override
-    public Collection<Part> getParts() {
-        throw new UnsupportedOperationException();
-    }
-
-    @Override
-    public Part getPart(String name) {
-        throw new UnsupportedOperationException();
-    }
-
     @Override
     public ServletContext getServletContext() {
         throw new UnsupportedOperationException();
diff --git a/src/main/java/org/apache/sling/servlethelpers/package-info.java b/src/main/java/org/apache/sling/servlethelpers/package-info.java
index 1a22083..2bcdb37 100644
--- a/src/main/java/org/apache/sling/servlethelpers/package-info.java
+++ b/src/main/java/org/apache/sling/servlethelpers/package-info.java
@@ -19,5 +19,5 @@
 /**
  * Mock implementation of selected Servlet-related Sling APIs.
  */
-@org.osgi.annotation.versioning.Version("1.8")
+@org.osgi.annotation.versioning.Version("1.9")
 package org.apache.sling.servlethelpers;
diff --git a/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java b/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java
index aa87e05..f69fe73 100644
--- a/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java
+++ b/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java
@@ -18,6 +18,8 @@
  */
 package org.apache.sling.servlethelpers;
 
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -34,6 +36,7 @@ import java.io.BufferedReader;
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.Calendar;
 import java.util.Enumeration;
 import java.util.LinkedHashMap;
@@ -539,5 +542,32 @@ public class MockSlingHttpServletRequestTest {
         String result2 = request.adaptTo(String.class);
         assertNotEquals(result1,  result2);
     }
+    
+    @Test
+    public void testNoParts() {
+    	assertThat(request.getParts()).as("request parts").isEmpty();
+    	assertThat(request.getPart("some-part")).as("missing part").isNull();
+    }
+    
+    @Test
+    public void testExistingParts() {
+    	ByteArrayPart part = ByteArrayPart.builder()
+    			.withName("log.txt")
+    			.withContent("hello, world".getBytes(UTF_8))
+    			.build();
 
+    	request.addPart(part);
+    	
+    	assertThat(request.getParts()).as("request parts")
+    		.hasSize(1)
+    		.extracting( p -> p.getName())
+    		.containsExactly("log.txt");
+    	
+    	assertThat(request.getPart("log.txt")).as("part looked up by name").isNotNull();
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testInvalidPart() {
+    	request.addPart(null);
+    }
 }