Author: enorman
Date: Thu Aug  5 02:24:48 2010
New Revision: 982454

URL: http://svn.apache.org/viewvc?rev=982454&view=rev
Log:
SLING-1091 POST to URL ending in /* with :name and no :nameHint should fail if 
named node exists

Modified:
    
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java
    
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java
    
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java
    
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletCreateTest.java
    
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletImportTest.java

Modified: 
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java?rev=982454&r1=982453&r2=982454&view=diff
==============================================================================
--- 
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java
 (original)
+++ 
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java
 Thu Aug  5 02:24:48 2010
@@ -17,16 +17,6 @@
 
 package org.apache.sling.servlets.post.impl.operations;
 
-import org.apache.sling.api.SlingHttpServletRequest;
-import org.apache.sling.api.request.RequestParameter;
-import org.apache.sling.api.resource.ResourceUtil;
-import org.apache.sling.api.servlets.HtmlResponse;
-import org.apache.sling.servlets.post.AbstractSlingPostOperation;
-import org.apache.sling.servlets.post.Modification;
-import org.apache.sling.servlets.post.SlingPostConstants;
-import org.apache.sling.servlets.post.VersioningConfiguration;
-import org.apache.sling.servlets.post.impl.helper.RequestProperty;
-
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
@@ -40,8 +30,38 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
 import javax.jcr.version.VersionException;
+import javax.servlet.ServletException;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.request.RequestParameter;
+import org.apache.sling.api.request.RequestParameterMap;
+import org.apache.sling.api.resource.ResourceUtil;
+import org.apache.sling.api.servlets.HtmlResponse;
+import org.apache.sling.servlets.post.AbstractSlingPostOperation;
+import org.apache.sling.servlets.post.Modification;
+import org.apache.sling.servlets.post.NodeNameGenerator;
+import org.apache.sling.servlets.post.SlingPostConstants;
+import org.apache.sling.servlets.post.VersioningConfiguration;
+import org.apache.sling.servlets.post.impl.helper.RequestProperty;
 
 abstract class AbstractCreateOperation extends AbstractSlingPostOperation {
+    /**
+     * The default node name generator
+     */
+    private final NodeNameGenerator defaultNodeNameGenerator;
+
+    /**
+     * utility class for generating node names
+     */
+    private NodeNameGenerator[] extraNodeNameGenerators;
+
+    public AbstractCreateOperation(NodeNameGenerator defaultNodeNameGenerator) 
{
+               this.defaultNodeNameGenerator = defaultNodeNameGenerator;
+       }
+
+       public void setExtraNodeNameGenerators(NodeNameGenerator[] 
extraNodeNameGenerators) {
+        this.extraNodeNameGenerators = extraNodeNameGenerators;
+    }
 
     /**
      * Create node(s) according to current request
@@ -430,4 +450,85 @@ abstract class AbstractCreateOperation e
     }
 
 
+    protected String generateName(SlingHttpServletRequest request, String 
basePath)
+       throws RepositoryException {
+
+               // SLING-1091: If a :name parameter is supplied, the (first) 
value of this parameter is used unmodified as the name 
+               //    for the new node. If the name is illegally formed with 
respect to JCR name requirements, an exception will be 
+               //    thrown when trying to create the node. The assumption 
with the :name parameter is, that the caller knows what 
+               //    he (or she) is supplying and should get the exact result 
if possible.        
+               RequestParameterMap parameters = 
request.getRequestParameterMap();
+               RequestParameter specialParam = 
parameters.getValue(SlingPostConstants.RP_NODE_NAME);
+               if ( specialParam != null ) {
+                   if ( specialParam.getString() != null && 
specialParam.getString().length() > 0 ) {
+                       // If the path ends with a *, create a node under its 
parent, with
+                       // a generated node name
+                       basePath = basePath += "/" + specialParam.getString();
+               
+                       // if the resulting path already exists then report an 
error
+                       Session session = 
request.getResourceResolver().adaptTo(Session.class);
+                   String jcrPath = removeAndValidateWorkspace(basePath, 
session);
+                   if (session.itemExists(jcrPath)) {
+                           throw new RepositoryException(
+                                       "Collision in node names for path=" + 
basePath);
+                   }
+               
+                       return basePath;
+                   }
+               }
+
+               // no :name value was supplied, so generate a name
+               boolean requirePrefix = requireItemPathPrefix(request);
+               
+               String generatedName = null;
+               if (extraNodeNameGenerators != null) {
+                   for (NodeNameGenerator generator : extraNodeNameGenerators) 
{
+                       generatedName = generator.getNodeName(request, 
basePath, requirePrefix, defaultNodeNameGenerator);
+                       if (generatedName != null) {
+                           break;
+                       }
+                   }
+               }
+               if (generatedName == null) {
+                   generatedName = 
defaultNodeNameGenerator.getNodeName(request, basePath, requirePrefix, 
defaultNodeNameGenerator);
+               }
+               
+               // If the path ends with a *, create a node under its parent, 
with
+               // a generated node name
+               basePath += "/" + generatedName;
+               
+               basePath = ensureUniquePath(request, basePath);
+               
+               return basePath;
+    }
+
+    private String ensureUniquePath(SlingHttpServletRequest request, String 
basePath) throws RepositoryException {
+               // if resulting path exists, add a suffix until it's not the 
case
+               // anymore
+               Session session = 
request.getResourceResolver().adaptTo(Session.class);
+               
+               String jcrPath = removeAndValidateWorkspace(basePath, session);
+               
+               // if resulting path exists, add a suffix until it's not the 
case
+               // anymore
+               if (session.itemExists(jcrPath)) {
+                   for (int idx = 0; idx < 1000; idx++) {
+                       String newPath = jcrPath + "_" + idx;
+                       if (!session.itemExists(newPath)) {
+                           basePath = basePath + "_" + idx;
+                           jcrPath = newPath;
+                           break;
+                       }
+                   }
+               }
+               
+               // if it still exists there are more than 1000 nodes ?
+               if (session.itemExists(jcrPath)) {
+                   throw new RepositoryException(
+                       "Collision in generated node names for path=" + 
basePath);
+               }
+               
+               return basePath;
+    }
+    
 }

Modified: 
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java?rev=982454&r1=982453&r2=982454&view=diff
==============================================================================
--- 
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java
 (original)
+++ 
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java
 Thu Aug  5 02:24:48 2010
@@ -48,34 +48,20 @@ import org.apache.sling.servlets.post.im
 public class ImportOperation extends AbstractCreateOperation {
 
     /**
-     * The default node name generator
-     */
-    private final NodeNameGenerator defaultNodeNameGenerator;
-
-    /**
-     * utility class for generating node names
-     */
-    private NodeNameGenerator[] extraNodeNameGenerators;
-
-    /**
      * Reference to the content importer service
      */
        private ContentImporter contentImporter;
 
     public ImportOperation(NodeNameGenerator defaultNodeNameGenerator,
             ContentImporter contentImporter) {
-        this.defaultNodeNameGenerator = defaultNodeNameGenerator;
+       super(defaultNodeNameGenerator);
         this.contentImporter = contentImporter;
     }
-    
-       public void setContentImporter(ContentImporter importer) {
+
+    public void setContentImporter(ContentImporter importer) {
                this.contentImporter = importer;
        }
-
-    public void setExtraNodeNameGenerators(NodeNameGenerator[] 
extraNodeNameGenerators) {
-        this.extraNodeNameGenerators = extraNodeNameGenerators;
-    }
-       
+    
     @Override
     protected void doRun(SlingHttpServletRequest request, HtmlResponse 
response, final List<Modification> changes)
                throws RepositoryException {
@@ -85,7 +71,7 @@ public class ImportOperation extends Abs
                     "Missing content importer for import");
             return;
        }
-       Map<String, RequestProperty> reqProperties = collectContent(request,
+       Map<String, RequestProperty> reqProperties = collectContent(request,
              response);
      
         VersioningConfiguration versioningConfiguration = 
getVersioningConfiguration(request);
@@ -93,7 +79,6 @@ public class ImportOperation extends Abs
         // do not change order unless you have a very good reason.
         Session session = request.getResourceResolver().adaptTo(Session.class);
 
-       
         processCreate(session, reqProperties, response, changes, 
versioningConfiguration);
         String path = response.getPath();
         Node node = null;
@@ -135,7 +120,8 @@ public class ImportOperation extends Abs
         //check if a name was posted to use as the name of the imported root 
node
         if (request.getParameter(SlingPostConstants.RP_NODE_NAME) != null || 
                        
request.getParameter(SlingPostConstants.RP_NODE_NAME_HINT) != null) {
-                       String name = generateName(request, basePath);
+                       String nodePath = generateName(request, basePath);
+                       String name = 
nodePath.substring(nodePath.lastIndexOf('/') + 1);
                contentRootName = name + "." + contentType;
         } else {
                //no name posted, so the import won't create a root node
@@ -226,54 +212,4 @@ public class ImportOperation extends Abs
                throw new RepositoryException(e);
         }
     }
-
-    
-    private String generateName(SlingHttpServletRequest request, String 
basePath)
-               throws RepositoryException {
-       boolean requirePrefix = requireItemPathPrefix(request);
-
-       String generatedName = null;
-       if (extraNodeNameGenerators != null) {
-               for (NodeNameGenerator generator : extraNodeNameGenerators) {
-                       generatedName = generator.getNodeName(request, 
basePath, requirePrefix, defaultNodeNameGenerator);
-                       if (generatedName != null) {
-                               break;
-                       }
-               }
-       }
-       if (generatedName == null) {
-               generatedName = defaultNodeNameGenerator.getNodeName(request, 
basePath, requirePrefix, defaultNodeNameGenerator);
-       }
-
-       // If the path ends with a *, create a node under its parent, with
-       // a generated node name
-       basePath += "/" + generatedName;
-
-       // if resulting path exists, add a suffix until it's not the case
-       // anymore
-       Session session = request.getResourceResolver().adaptTo(Session.class);
-
-       // if resulting path exists, add a suffix until it's not the case
-       // anymore
-       if (session.itemExists(basePath)) {
-               for (int idx = 0; idx < 1000; idx++) {
-                       String newPath = basePath + "_" + idx;
-                       if (!session.itemExists(newPath)) {
-                               basePath = newPath;
-                               break;
-                       }
-               }
-       }
-
-       // if it still exists there are more than 1000 nodes ?
-       if (session.itemExists(basePath)) {
-               throw new RepositoryException(
-                               "Collision in generated node names for path=" + 
basePath);
-       }
-
-       //the last segment is the name.
-       String name = basePath.substring(basePath.lastIndexOf('/') + 1);
-               return name;
-    }
-
 }
\ No newline at end of file

Modified: 
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java?rev=982454&r1=982453&r2=982454&view=diff
==============================================================================
--- 
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java
 (original)
+++ 
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java
 Thu Aug  5 02:24:48 2010
@@ -28,6 +28,7 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
 import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
 
 import org.apache.sling.api.SlingException;
 import org.apache.sling.api.SlingHttpServletRequest;
@@ -36,10 +37,10 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.servlets.HtmlResponse;
 import org.apache.sling.servlets.post.Modification;
+import org.apache.sling.servlets.post.NodeNameGenerator;
 import org.apache.sling.servlets.post.SlingPostConstants;
 import org.apache.sling.servlets.post.VersioningConfiguration;
 import org.apache.sling.servlets.post.impl.helper.DateParser;
-import org.apache.sling.servlets.post.NodeNameGenerator;
 import org.apache.sling.servlets.post.impl.helper.ReferenceParser;
 import org.apache.sling.servlets.post.impl.helper.RequestProperty;
 import org.apache.sling.servlets.post.impl.helper.SlingFileUploadHandler;
@@ -52,16 +53,6 @@ import org.apache.sling.servlets.post.im
  */
 public class ModifyOperation extends AbstractCreateOperation {
 
-    /**
-     * The default node name generator
-     */
-    private final NodeNameGenerator defaultNodeNameGenerator;
-
-    /**
-     * utility class for generating node names
-     */
-    private NodeNameGenerator[] extraNodeNameGenerators;
-
     private final DateParser dateParser;
 
     /**
@@ -71,15 +62,11 @@ public class ModifyOperation extends Abs
 
     public ModifyOperation(NodeNameGenerator defaultNodeNameGenerator,
             DateParser dateParser, ServletContext servletContext) {
-        this.defaultNodeNameGenerator = defaultNodeNameGenerator;
+       super(defaultNodeNameGenerator);
         this.dateParser = dateParser;
         this.uploadHandler = new SlingFileUploadHandler(servletContext);
     }
 
-    public void setExtraNodeNameGenerators(NodeNameGenerator[] 
extraNodeNameGenerators) {
-        this.extraNodeNameGenerators = extraNodeNameGenerators;
-    }
-
     @Override
     protected void doRun(SlingHttpServletRequest request, HtmlResponse 
response, List<Modification> changes)
             throws RepositoryException {
@@ -174,62 +161,6 @@ public class ModifyOperation extends Abs
         return path;
     }
 
-    private String generateName(SlingHttpServletRequest request, String 
basePath)
-            throws RepositoryException {
-        boolean requirePrefix = requireItemPathPrefix(request);
-
-        String generatedName = null;
-        if (extraNodeNameGenerators != null) {
-            for (NodeNameGenerator generator : extraNodeNameGenerators) {
-                generatedName = generator.getNodeName(request, basePath, 
requirePrefix, defaultNodeNameGenerator);
-                if (generatedName != null) {
-                    break;
-                }
-            }
-        }
-        if (generatedName == null) {
-            generatedName = defaultNodeNameGenerator.getNodeName(request, 
basePath, requirePrefix, defaultNodeNameGenerator);
-        }
-
-        // If the path ends with a *, create a node under its parent, with
-        // a generated node name
-        basePath += "/" + generatedName;
-
-        basePath = ensureUniquePath(request, basePath);
-
-        return basePath;
-    }
-
-    private String ensureUniquePath(SlingHttpServletRequest request, String 
basePath) throws RepositoryException {
-        // if resulting path exists, add a suffix until it's not the case
-        // anymore
-        Session session = request.getResourceResolver().adaptTo(Session.class);
-
-        String jcrPath = removeAndValidateWorkspace(basePath, session);
-
-        // if resulting path exists, add a suffix until it's not the case
-        // anymore
-        if (session.itemExists(jcrPath)) {
-            for (int idx = 0; idx < 1000; idx++) {
-                String newPath = jcrPath + "_" + idx;
-                if (!session.itemExists(newPath)) {
-                    basePath = basePath + "_" + idx;
-                    jcrPath = newPath;
-                    break;
-                }
-            }
-        }
-
-        // if it still exists there are more than 1000 nodes ?
-        if (session.itemExists(jcrPath)) {
-            throw new RepositoryException(
-                "Collision in generated node names for path=" + basePath);
-        }
-
-        return basePath;
-    }
-
-
     /**
      * Moves all repository content listed as repository move source in the
      * request properties to the locations indicated by the resource 
properties.

Modified: 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletCreateTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletCreateTest.java?rev=982454&r1=982453&r2=982454&view=diff
==============================================================================
--- 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletCreateTest.java
 (original)
+++ 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletCreateTest.java
 Thu Aug  5 02:24:48 2010
@@ -17,11 +17,14 @@
 package org.apache.sling.launchpad.webapp.integrationtest.servlets.post;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.commons.httpclient.NameValuePair;
 import org.apache.sling.commons.testing.integration.HttpTestBase;
 import org.apache.sling.servlets.post.SlingPostConstants;
 
@@ -136,4 +139,60 @@ public class PostServletCreateTest exten
         assertJavascript("string", content, "out.println(typeof data.c)");
     }
 
- }
\ No newline at end of file
+    /**
+     * SLING-1091: test create node with an exact node name (no filtering) 
+     */
+    public void testCreateNodeWithExactName() throws IOException {
+       Map<String,String> nodeProperties = new HashMap<String, String>();
+       nodeProperties.put(SlingPostConstants.RP_NODE_NAME, "exactNodeName");
+        final String location = testClient.createNode(postUrl + 
SlingPostConstants.DEFAULT_CREATE_SUFFIX, nodeProperties);
+        assertHttpStatus(location + DEFAULT_EXT, HttpServletResponse.SC_OK,
+                "POST must redirect to created resource (" + location + ")");
+        assertTrue("Node (" + location + ") must have exact name",
+                !location.endsWith("/*"));
+        assertTrue("Node (" + location + ") must created be under POST URL (" 
+ postUrl + ")",
+                location.contains(postUrl + "/"));
+        assertTrue("Node (" + location + ") must have exact name 
'exactNodeName'",
+                       location.endsWith("/exactNodeName"));
+    }
+
+    /**
+     * SLING-1091: test error reporting when attempting to create a node with 
an 
+     * invalid exact node name. 
+     */
+    public void testCreateNodeWithInvalidExactName() throws IOException {
+               String location = postUrl + 
SlingPostConstants.DEFAULT_CREATE_SUFFIX;
+               List<NameValuePair> postParams = new ArrayList<NameValuePair>();
+               postParams.add(new 
NameValuePair(SlingPostConstants.RP_NODE_NAME, "exactNodeName*"));
+               //expect a 500 status since the name is invalid
+               assertPostStatus(location, 
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, postParams, null);
+    }
+
+    /**
+     * SLING-1091: test error reporting when attempting to create a node with 
an 
+     * already used node name. 
+     */
+    public void testCreateNodeWithAlreadyUsedExactName() throws IOException {
+        String testNodeName = "alreadyUsedExactNodeName";
+       
+       Map<String,String> nodeProperties = new HashMap<String, String>();
+       nodeProperties.put(SlingPostConstants.RP_NODE_NAME, testNodeName);
+        final String location = testClient.createNode(postUrl + 
SlingPostConstants.DEFAULT_CREATE_SUFFIX, nodeProperties);
+        assertHttpStatus(location + DEFAULT_EXT, HttpServletResponse.SC_OK,
+                "POST must redirect to created resource (" + location + ")");
+        assertTrue("Node (" + location + ") must have exact name",
+                !location.endsWith("/*"));
+        assertTrue("Node (" + location + ") must created be under POST URL (" 
+ postUrl + ")",
+                location.contains(postUrl + "/"));
+        assertTrue("Node (" + location + ") must have exact name '" + 
testNodeName + "'",
+                       location.endsWith("/" + testNodeName));
+
+        //try to create the same node again, since same name siblings are not 
allowed an error should be
+        // thrown
+               List<NameValuePair> postParams = new ArrayList<NameValuePair>();
+               postParams.add(new 
NameValuePair(SlingPostConstants.RP_NODE_NAME, testNodeName));
+               //expect a 500 status since the name is not unique
+               assertPostStatus(postUrl + 
SlingPostConstants.DEFAULT_CREATE_SUFFIX, 
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, postParams, null);
+    }
+
+}
\ No newline at end of file

Modified: 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletImportTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletImportTest.java?rev=982454&r1=982453&r2=982454&view=diff
==============================================================================
--- 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletImportTest.java
 (original)
+++ 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletImportTest.java
 Thu Aug  5 02:24:48 2010
@@ -20,13 +20,18 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.Random;
 import java.util.Set;
 
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.httpclient.NameValuePair;
 import org.apache.sling.commons.json.JSONArray;
 import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.JSONObject;
@@ -541,4 +546,94 @@ public class PostServletImportTest exten
                assertExpectedJSON(new JSONObject(expectedJsonContent), 
jsonObj);
     }
     
+    
+    
+
+    protected String importNodeWithExactName(String testNodeName) throws 
IOException, JSONException {
+        final String testPath = TEST_BASE_PATH;
+        Map<String, String> props = new HashMap<String, String>();
+        String testNode = testClient.createNode(HTTP_BASE_URL + testPath, 
props);
+        urlsToDelete.add(testNode);
+
+        props.clear();
+        props.put(SlingPostConstants.RP_OPERATION,
+                       SlingPostConstants.OPERATION_IMPORT);
+        
+        props.put(SlingPostConstants.RP_NODE_NAME, testNodeName);
+        String jsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimport.json"));
+        props.put(SlingPostConstants.RP_CONTENT, jsonContent);
+        props.put(SlingPostConstants.RP_CONTENT_TYPE, "json");
+        props.put(SlingPostConstants.RP_REDIRECT_TO, testPath + "/*");
+        String location = testClient.createNode(HTTP_BASE_URL + testPath, 
props);
+
+        // assert content at new location
+        String content = getContent(location + ".3.json", CONTENT_TYPE_JSON);
+
+               JSONObject jsonObj = new JSONObject(content);
+               assertNotNull(jsonObj);
+
+               //assert the imported content is there.
+        String expectedJsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/importresults.json"));
+               assertExpectedJSON(new JSONObject(expectedJsonContent), 
jsonObj);
+
+       assertHttpStatus(location + DEFAULT_EXT, HttpServletResponse.SC_OK,
+                "POST must redirect to created resource (" + location + ")");
+        assertTrue("Node (" + location + ") must have exact name",
+                !location.endsWith("/*"));
+        assertTrue("Node (" + location + ") must created be under POST URL (" 
+ testNode + ")",
+                location.contains(testNode + "/"));
+        assertTrue("Node (" + location + ") must have exact name '" + 
testNodeName + "'",
+                       location.endsWith("/" + testNodeName));
+
+               return location;
+    }
+    
+    /**
+     * SLING-1091: test create node with an exact node name (no filtering) 
+     */
+    public void testImportNodeWithExactName() throws IOException, 
JSONException {
+       importNodeWithExactName("exactNodeName");
+    }
+
+    /**
+     * SLING-1091: test error reporting when attempting to create a node with 
an 
+     * invalid exact node name. 
+     */
+    public void testImportNodeWithInvalidExactName() throws IOException {
+        final String testPath = TEST_BASE_PATH;
+        Map<String, String> props = new HashMap<String, String>();
+        String testNode = testClient.createNode(HTTP_BASE_URL + testPath, 
props);
+        urlsToDelete.add(testNode);
+
+               List<NameValuePair> postParams = new ArrayList<NameValuePair>();
+               postParams.add(new 
NameValuePair(SlingPostConstants.RP_OPERATION, 
SlingPostConstants.OPERATION_IMPORT));
+               postParams.add(new 
NameValuePair(SlingPostConstants.RP_NODE_NAME, "exactNodeName*"));
+        String jsonContent = 
(String)getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimport.json"));
+               postParams.add(new NameValuePair(SlingPostConstants.RP_CONTENT, 
jsonContent));
+               postParams.add(new 
NameValuePair(SlingPostConstants.RP_CONTENT_TYPE, "json"));
+               postParams.add(new 
NameValuePair(SlingPostConstants.RP_REDIRECT_TO, testPath + "/*"));
+
+        //expect a 500 status since the name is invalid
+        String location = HTTP_BASE_URL + testPath;
+               assertPostStatus(location, 
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, postParams, null);
+    }
+
+    /**
+     * SLING-1091: test error reporting when attempting to import a node with 
an 
+     * already used node name. 
+     */
+    public void testImportNodeWithAlreadyUsedExactName() throws IOException, 
JSONException {
+       String testNodeName = "alreadyUsedExactNodeName";
+       String location = importNodeWithExactName(testNodeName);
+
+       
+        //try to create the same node again, since same name siblings are not 
allowed an error should be
+        // thrown
+               List<NameValuePair> postParams = new ArrayList<NameValuePair>();
+               postParams.add(new 
NameValuePair(SlingPostConstants.RP_NODE_NAME, testNodeName));
+               //expect a 500 status since the name is not unique
+               String postUrl = location.substring(0, 
location.lastIndexOf('/'));
+               assertPostStatus(postUrl + 
SlingPostConstants.DEFAULT_CREATE_SUFFIX, 
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, postParams, null);
+    }
+    
 }


Reply via email to