/*
 * 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.launchpad.webapp.integrationtest;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.sling.commons.testing.integration.HttpTestBase;

/** Test the SLING-340 way of deriving the resource type from
 *  the content path.
 */

public class PathBasedResourceTypeTest extends HttpTestBase {

    public static final String testPath = "sling-integration-test-" + System.currentTimeMillis();
    public static final String contentPath = "/sling-test-pbrt/" + testPath;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        urlsToDelete.add(WEBDAV_BASE_URL + contentPath);
    }

    public void testDefaultResourceType() throws IOException {
        final TestNode tn = new TestNode(HTTP_BASE_URL + contentPath, null);

        // without script -> default rendering
        String content = getContent(tn.nodeUrl + ".html", CONTENT_TYPE_HTML);
        assertTrue("Content contains default rendering",content.contains("Resource dumped by HtmlRendererServlet"));

        // check default resource type
        final String scriptPath = "/apps/" + testPath;
        testClient.mkdirs(WEBDAV_BASE_URL, scriptPath);
        urlsToDelete.add(WEBDAV_BASE_URL + scriptPath);
        final String urlsToDelete = uploadTestScript(scriptPath, "rendering-test.esp", "html.esp");
        try {
            content = getContent(tn.nodeUrl + ".html", CONTENT_TYPE_HTML);
            assertTrue("Test script marker found in content (" + content + ")",content.contains("ESP template"));
        } finally {
            testClient.delete(urlsToDelete);
        }
    }

    public void testExplicitResourceType() throws IOException {

        final String resourceType = getClass().getSimpleName();
        final Map<String, String> properties = new HashMap<String, String>();
        properties.put(SLING_RESOURCE_TYPE, resourceType);
        final TestNode tn = new TestNode(HTTP_BASE_URL + contentPath, properties);

        urlsToDelete.add(uploadTestScript(tn.scriptPath, "rendering-test-2.esp", "html.esp"));
        final String content = getContent(tn.nodeUrl + ".html", CONTENT_TYPE_HTML);
        assertTrue("Test script marker found in content (" + content + ")",content.contains("Template #2 for ESP tests"));
    }
}

@@ -937,75 +937,8 @@
 p;%0A%0A
-import org.apache.sling.commons.testing.integration.HttpTestBase;%0A%0A
 /** 
@@ -1073,12 +1073,17 @@
 nds 
-Http
+Rendering
 Test
@@ -1697,77 +1697,27 @@
 sert
-True(%22Content contains default rendering%22,content.contains(%22Resource 
+Contains(content, %22
 dump
@@ -1743,17 +1743,16 @@
 ervlet%22)
-)
 ;%0A%0A     
@@ -2163,86 +2163,26 @@
 sert
-True(%22Test script marker found in content (%22 + content + %22)%22,content.contains(
+Contains(content, 
 %22ESP
@@ -2192,17 +2192,16 @@
 mplate%22)
-)
 ;%0A      
@@ -2823,86 +2823,26 @@
 sert
-True(%22Test script marker found in content (%22 + content + %22)%22,content.contains(
+Contains(content, 
 %22Tem
@@ -2865,17 +2865,16 @@
  tests%22)
-)
 ;%0A    %7D%0A

simplify tests using assertContains()