package org.nuxeo.webengine.sites.listeners;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.IdRef;
import org.nuxeo.ecm.core.storage.sql.SQLRepositoryTestCase;
import org.nuxeo.webengine.sites.utils.SiteConstants;

public class TestSiteActionListener extends SQLRepositoryTestCase {

    protected Log log = LogFactory.getLog(TestSiteActionListener.class);

    @Override
    public void setUp() throws Exception {
        super.setUp();

        deployContrib("org.nuxeo.ecm.platform.webengine.sites.core.contrib",
                "OSGI-INF/core-types-contrib.xml");
        deployContrib("org.nuxeo.ecm.platform.webengine.sites.core.contrib",
                "OSGI-INF/webengine-sites-listener-contrib.xml");
        openSession();
    }

    public void testTestSiteAction() throws ClientException {
        DocumentModel website1 = session.createDocumentModel("WebSite");
        website1.setPathInfo("/", "website");
        website1 = session.createDocument(website1);
        session.save();

        DocumentModel website2 = session.createDocumentModel("WebSite");
        website2.setPathInfo("/", "website");
        website2 = session.createDocument(website2);
        session.save();

        String website1URL = (String) session.getDocument(new IdRef(website1.getId())).getPropertyValue(
                SiteConstants.WEBCONTAINER_URL);
        String website2URL = (String) session.getDocument(new IdRef(website2.getId())).getPropertyValue(
                SiteConstants.WEBCONTAINER_URL);
        log.info("webSite1 URL : " + website1URL);
        log.info("webSite2 URL : " + website2URL);
        assertTrue(!(website1URL.equals(website2URL)));
    }

    public void testTestSiteActionWithWebSiteNotInTheSameContainer()
            throws ClientException {
        DocumentModel folder1 = session.createDocumentModel("Folder");
        folder1.setPathInfo("/", "folder1");
        session.createDocument(folder1);
        session.save();

        DocumentModel folder2 = session.createDocumentModel("Folder");
        folder2.setPathInfo("/", "folder2");
        session.createDocument(folder2);
        session.save();

        DocumentModel website1 = session.createDocumentModel("WebSite");
        website1.setPathInfo("/folder1", "website");
        website1 = session.createDocument(website1);
        session.save();

        DocumentModel website2 = session.createDocumentModel("WebSite");
        website2.setPathInfo("/folder2", "website");
        website2 = session.createDocument(website2);
        session.save();

        String website1URL = (String) session.getDocument(new IdRef(website1.getId())).getPropertyValue(
                SiteConstants.WEBCONTAINER_URL);
        String website2URL = (String) session.getDocument(new IdRef(website2.getId())).getPropertyValue(
                SiteConstants.WEBCONTAINER_URL);
        log.info("webSite1 path : " + (String) session.getDocument(website1.getRef()).getPathAsString());
        log.info("webSite2 path : " + (String) session.getDocument(website2.getRef()).getPathAsString());
        log.info("webSite1 URL : " + website1URL);
        log.info("webSite2 URL : " + website2URL);
        assertTrue(!(website1URL.equals(website2URL)));
    }
}

@@ -1497,32 +1497,79 @@
 CONTAINER_URL);%0A
+        assertEquals(%22website%22, website1URL);%0A%0A
         String w
@@ -1718,122 +1718,19 @@
     
-log.info(%22webSite1 URL : %22 + website1URL);%0A        log.info(%22webSite2 URL : %22 + website2URL);%0A        assertTrue(!
+assertFalse
 (web
@@ -1750,33 +1750,32 @@
 ls(website2URL))
-)
 ;%0A    %7D%0A%0A    pub
@@ -2791,32 +2791,87 @@
 CONTAINER_URL);%0A
+        assertEquals(%22website%22, website1URL);%0A        %0A
         String w
@@ -3004,32 +3004,33 @@
 AINER_URL);%0A
+%0A
         
 log.info(%22we
@@ -3021,37 +3021,22 @@
     
-log.info(%22webSite1 path : %22 +
+String path1 =
  (St
@@ -3089,33 +3089,32 @@
 etPathAsString()
-)
 ;%0A        log.in
@@ -3111,37 +3111,22 @@
     
-log.info(%22webSite2 path : %22 +
+String path2 =
  (St
@@ -3187,17 +3187,16 @@
 String()
-)
 ;%0A      
@@ -3201,122 +3201,126 @@
     
-log.info(%22webSite1 URL : %22 + website1URL);%0A        log.info(%22webSite2 URL : %22 + website2URL);%0A        assertTrue(!
+assertEquals(%22/folder1/website%22, path1);%0A        assertEquals(%22/folder2/website%22, path2);%0A        %0A        assertFalse
 (web
@@ -3348,17 +3348,16 @@
 te2URL))
-)
 ;%0A    %7D%0A

Test instead of logging.