package com.celements.structEditor;

import static com.celements.common.test.CelementsTestUtils.*;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

import java.util.Collections;

import org.junit.Before;
import org.junit.Test;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;

import com.celements.common.test.AbstractComponentTest;
import com.celements.model.access.IModelAccessFacade;
import com.celements.pagetype.PageTypeReference;
import com.celements.pagetype.service.IPageTypeResolverRole;
import com.celements.structEditor.classes.FormFieldEditorClass;
import com.celements.structEditor.fields.FormFieldPageType;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.web.Utils;

public class DefaultStructuredDataEditorServiceTest extends AbstractComponentTest {

  private DefaultStructuredDataEditorService service;
  private XWikiDocument cellDoc;

  @Before
  public void prepareTest() throws Exception {
    registerComponentMocks(IModelAccessFacade.class, IPageTypeResolverRole.class);
    service = (DefaultStructuredDataEditorService) Utils.getComponent(
        StructuredDataEditorService.class);
    cellDoc = new XWikiDocument(new DocumentReference("wiki", "layout", "cell"));
  }

  @Test
  public void test_resolveFormPrefix_null() throws Exception {
    String prefix = null;
    replayDefault();
    String ret = service.resolveFormPrefix(cellDoc);
    verifyDefault();
    assertEquals(prefix, ret);
  }

  @Test
  public void test_resolveFormPrefix() throws Exception {
    String prefix = "prefix";
    XWikiDocument parentDoc = new XWikiDocument(new DocumentReference("wiki", "layout", "parent"));
    cellDoc.setParentReference((EntityReference) parentDoc.getDocumentReference());
    expect(getMock(IModelAccessFacade.class).getDocument(eq(
        parentDoc.getDocumentReference()))).andReturn(parentDoc).once();
    expect(getMock(IPageTypeResolverRole.class).getPageTypeRefForDoc(same(parentDoc))).andReturn(
        new PageTypeReference(FormFieldPageType.PAGETYPE_NAME, "",
            Collections.<String>emptyList())).once();
    expect(getMock(IModelAccessFacade.class).getProperty(same(parentDoc), same(
        FormFieldEditorClass.FIELD_PREFIX))).andReturn(prefix).once();
    replayDefault();
    String ret = service.resolveFormPrefix(cellDoc);
    verifyDefault();
    assertEquals(prefix, ret);
  }

  @Test
  public void test_getXClassPrettyName() throws Exception {
    String prettyName = "";
    replayDefault();
    String ret = service.getXClassPrettyName(cellDoc);
    verifyDefault();
    assertEquals(prettyName, ret);
  }

}

@@ -670,72 +670,224 @@
 tor.
-fields.FormFieldPageType;%0Aimport com.xpn.xwiki.doc.XWikiDocument
+classes.StructuredDataEditorClass;%0Aimport com.celements.structEditor.fields.FormFieldPageType;%0Aimport com.xpn.xwiki.XWikiContext;%0Aimport com.xpn.xwiki.doc.XWikiDocument;%0Aimport com.xpn.xwiki.objects.classes.BaseClass
 ;%0Aim
@@ -1088,16 +1088,121 @@
 cellDoc;
+%0A  private IModelAccessFacade modelAccessMock;%0A  private XWikiContext context;%0A  private String wikiName;
 %0A%0A  @Bef
@@ -1446,24 +1446,147 @@
 ice.class);%0A
+    modelAccessMock = getMock(IModelAccessFacade.class);%0A    context = getContext();%0A    wikiName = context.getDatabase();%0A
     cellDoc 
@@ -1619,38 +1619,40 @@
 cumentReference(
-%22
 wiki
-%22
+Name
 , %22layout%22, %22cel
@@ -2060,14 +2060,16 @@
 nce(
-%22
 wiki
-%22
+Name
 , %22l
@@ -2075,16 +2075,24 @@
 layout%22,
+%0A       
  %22parent
@@ -2195,41 +2195,23 @@
 ect(
-getMock(IModelAccessFacade.class)
+modelAccessMock
 .get
@@ -2222,25 +2222,16 @@
 ment(eq(
-%0A        
 parentDo
@@ -2268,16 +2268,25 @@
 dReturn(
+%0A        
 parentDo
@@ -2530,41 +2530,23 @@
 ect(
-getMock(IModelAccessFacade.class)
+modelAccessMock
 .get
@@ -2862,23 +2862,826 @@
 ing 
-prettyName = %22%22
+fieldXClassName = %22Celements.TestXClassName%22;%0A    expect(modelAccessMock.getProperty(same(cellDoc), same(%0A        StructuredDataEditorClass.FIELD_EDIT_FIELD_CLASS_NAME))).andReturn(fieldXClassName).once();%0A    DocumentReference xClassDocRef = new DocumentReference(wikiName, %22Celements%22, %22TestXClassName%22);%0A    XWikiDocument xClassDoc = new XWikiDocument(xClassDocRef);%0A    expect(modelAccessMock.getDocument(xClassDocRef)).andReturn(xClassDoc).atLeastOnce();%0A    String editFieldName = %22edit_field%22;%0A    expect(modelAccessMock.getProperty(same(cellDoc), same(%0A        StructuredDataEditorClass.FIELD_EDIT_FIELD_NAME))).andReturn(editFieldName).once();%0A    BaseClass xClass = xClassDoc.getXClass();%0A    String thePrettyFieldName = %22the Pretty Field Name%22;%0A    xClass.addTextField(editFieldName, thePrettyFieldName, 30)
 ;%0A  
@@ -3796,14 +3796,22 @@
 als(
-p
+theP
 retty
+Field
 Name

fix test_getXClassPrettyName unit test