package com.xpn.xwiki.it.xmlrpc;

import java.util.HashMap;
import java.util.Map;

import com.xpn.xwiki.it.xmlrpc.framework.AbstractXmlRpcTestCase;
import com.xpn.xwiki.xmlrpc.Page;
import com.xpn.xwiki.xmlrpc.PageSummary;

public class SpacesTest extends AbstractXmlRpcTestCase
{
    public void testAddNewSpace() throws Exception
    {
        Map spaceProperties = new HashMap();
        // Stupid: property needs to be set even if IGNORED (otherwise null pointer exception)
        spaceProperties.put("name", "Test Space");
        spaceProperties.put("key", "TestSpace");
        // Stupid: property needs to be set even if IGNORED (otherwise null pointer exception)
        spaceProperties.put("description", "A test space");
        getXWikiRpc().addSpace(getToken(), spaceProperties);
        
        Map spaceSummary = getXWikiRpc().getSpace(getToken(), "TestSpace");

        // TODO The space name(!) is not saved and the key is used instead when querying
        // Is this behaviour acceptable ? 
        // assertEquals(spaceProperties.get("name"), spaceSummary.get("name"));
        
        assertEquals(spaceProperties.get("key"), spaceSummary.get("key"));
        
        // TODO The space description is not saved and the key is used instead when querying
        // Is this behaviour acceptable ? 
        // assertEquals(spaceProperties.get("description"), spaceSummary.get("description"));        
    }

    public void testAddSpaceAndPage2() throws Exception
    {
        String spaceKey = "ContainerSpace";
        Map spaceProperties = new HashMap();
        spaceProperties.put("key", spaceKey);
        // Stupid: property needs to be set even if IGNORED (otherwise null pointer exception)
        spaceProperties.put("name", "Stupid");
        // Stupid: property needs to be set even if IGNORED (otherwise null pointer exception)
        spaceProperties.put("description", "Stupid");        
        getXWikiRpc().addSpace(getToken(), spaceProperties);
        
        String title = "SomeNewPage";
        Map pageProperties = new HashMap();
        pageProperties.put("space", spaceKey);
        // Stupid: This title is also ignored, id used instead 
        pageProperties.put("title", title);
        pageProperties.put("content", "Some Content");
        Page resultPage = new Page(getXWikiRpc().storePage(getToken(), pageProperties));
        
        // TODO: This is plain wrong (returns Main.WebHome)
        // assertEquals(title, resultPage.getTitle());
        
        Object[] pageObjs = getXWikiRpc().getPages(getToken(), spaceKey);
        
        boolean found = false;
        for (int i = 0; i < pageObjs.length && !found; i++) {
            PageSummary summary = new PageSummary((Map)pageObjs[i]);
            if (summary.getTitle().equals(title)) {
                found = true;
                assertEquals(spaceKey, summary.getSpace());
            }
            
            System.out.println(summary.getTitle());
        }
        assertTrue("Adding page failed. There should be a page entitled \""+ title + "\" in this space", found); 
    }
}

@@ -215,16 +215,58 @@
 Summary;
+%0Aimport com.xpn.xwiki.xmlrpc.SpaceSummary;
 %0A%0Apublic
@@ -410,32 +410,81 @@
  new HashMap();%0A
+        spaceProperties.put(%22key%22, %22TestSpace%22);%0A
         // Stupi
@@ -554,32 +554,32 @@
 nter exception)%0A
+
         spacePro
@@ -617,57 +617,8 @@
 %22);%0A
-        spaceProperties.put(%22key%22, %22TestSpace%22);%0A
     
@@ -2958,177 +2958,1252 @@
     
+%7D%0A
     
+    assertTrue(%22Adding page failed. There should be a page entitled %5C%22%22+ title + %22%5C%22 in this space%22, found);
 %0A    
+%7D%0A
     
+%0A
     
-Sy
+public void te
 st
+R
 em
-.out.println(summary.getTitle());%0A        %7D%0A        assertTrue(%22Adding page failed. There should be a page entitled %5C%22%22+ title + %22%5C%22 in this space
+oveSpace() throws Exception %7B%0A    %09String spaceKey = %22ContainerSpace%22;%0A    %09%0A        Map spaceProperties = new HashMap();%0A        spaceProperties.put(%22key%22, spaceKey);%0A        // Stupid: property needs to be set even if IGNORED (otherwise null pointer exception)%0A        spaceProperties.put(%22name%22, %22Stupid%22);%0A        // Stupid: property needs to be set even if IGNORED (otherwise null pointer exception)%0A        spaceProperties.put(%22description%22, %22Stupid%22);%0A        getXWikiRpc().addSpace(getToken(), spaceProperties);%0A        %0A        assertNotNull(getXWikiRpc().getSpace(getToken(), spaceKey)); %0A        %0A        getXWikiRpc().removeSpace(getToken(), spaceKey);%0A        %0A        Object%5B%5D spaceObjs = getXWikiRpc().getSpaces(getToken());%0A        boolean found = false;%0A        for (int i = 0; i %3C spaceObjs.length && !found; i++) %7B%0A        %09SpaceSummary summary = new SpaceSummary((Map)spaceObjs%5Bi%5D);%0A            if (summary.getKey().equals(spaceKey)) %7B%0A                found = true;%0A            %7D            %0A        %7D%0A        assertFalse(%22Remove space failed (%22 + spaceKey + %22 still present)
 %22, f
@@ -4209,17 +4209,22 @@
 found); 
-%0A
+    
     
+%0A%09
 %7D%0A%7D%0A

Test for checking Asiri's patch attached to XWIKI-1520.