package com.celements.navigation.service;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.xwiki.context.Execution;

import com.celements.common.test.AbstractBridgedComponentTestCase;
import com.celements.navigation.cmd.GetMappedMenuItemsForParentCommand;
import com.celements.navigation.cmd.GetNotMappedMenuItemsForParentCommand;
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.store.XWikiStoreInterface;
import com.xpn.xwiki.web.Utils;

public class TreeNodeCacheTest extends AbstractBridgedComponentTestCase {

  private XWikiContext context;
  private XWiki wiki;
  private XWikiStoreInterface mockStore;
  private TreeNodeCache treeNodeCache;

  @Before
  public void setUp_TreeNodeCacheTest() throws Exception {
    context = getContext();
    wiki = createMock(XWiki.class);
    context.setWiki(wiki);
    mockStore = createMock(XWikiStoreInterface.class);
    expect(wiki.getStore()).andReturn(mockStore).anyTimes();
    treeNodeCache = new TreeNodeCache();
    treeNodeCache.execution = Utils.getComponent(Execution.class);
  }

  @Test
  public void testGetMappedMenuItemsForParentCmd() {
    assertFalse(treeNodeCache.getMappedMenuItemsForParentCmd().isActive());
  }

  @Test
  public void testGetMappedMenuItemsForParentCmd_injected() {
    GetMappedMenuItemsForParentCommand testGetMenuItemCommand = new GetMappedMenuItemsForParentCommand();
    testGetMenuItemCommand.setIsActive(true);
    treeNodeCache.inject_GetMappedMenuItemsForParentCmd(testGetMenuItemCommand);
    assertEquals(testGetMenuItemCommand, treeNodeCache.getMappedMenuItemsForParentCmd());
    assertTrue(treeNodeCache.getMappedMenuItemsForParentCmd().isActive());
  }

  @Test
  public void testGetNotMappedMenuItemsForParentCmd() {
    assertNotNull(treeNodeCache.getNotMappedMenuItemsForParentCmd());
  }

  @Test
  public void testGetNotMappedMenuItemsForParentCmd_injected() {
    GetNotMappedMenuItemsForParentCommand testGetMenuItemCommand = new GetNotMappedMenuItemsForParentCommand();
    treeNodeCache.inject_GetNotMappedMenuItemsForParentCmd(testGetMenuItemCommand);
    assertNotNull(treeNodeCache.getNotMappedMenuItemsForParentCmd());
    assertSame("Expecting injected cmd object", testGetMenuItemCommand,
        treeNodeCache.getNotMappedMenuItemsForParentCmd());
  }

  @Test
  public void testGetNotMappedMenuItemsForParentCmd_singleton() {
    GetNotMappedMenuItemsForParentCommand testGetMenuItemCommand = treeNodeCache.getNotMappedMenuItemsForParentCmd();
    assertNotNull(testGetMenuItemCommand);
    assertSame("Expecting injected cmd object", testGetMenuItemCommand,
        treeNodeCache.getNotMappedMenuItemsForParentCmd());
  }

  @Test
  public void testGetMappedMenuItemsForParentCmd_injectedByContext() {
    GetMappedMenuItemsForParentCommand testGetMenuItemCommand = new GetMappedMenuItemsForParentCommand();
    testGetMenuItemCommand.setIsActive(true);
    context.put(GetMappedMenuItemsForParentCommand.CELEMENTS_MAPPED_MENU_ITEMS_KEY,
        testGetMenuItemCommand);
    assertTrue(treeNodeCache.getMappedMenuItemsForParentCmd().isActive());

    testGetMenuItemCommand.setIsActive(false);
    context.put(GetMappedMenuItemsForParentCommand.CELEMENTS_MAPPED_MENU_ITEMS_KEY,
        testGetMenuItemCommand);
    assertFalse(treeNodeCache.getMappedMenuItemsForParentCmd().isActive());
  }

  @Test
  public void testQueryCount() {
    GetNotMappedMenuItemsForParentCommand testGetMenuItemCommand = createMock(
        GetNotMappedMenuItemsForParentCommand.class);
    treeNodeCache.inject_GetNotMappedMenuItemsForParentCmd(testGetMenuItemCommand);
    expect(testGetMenuItemCommand.queryCount()).andReturn(15);
    replayAll(testGetMenuItemCommand);
    assertEquals(15, treeNodeCache.queryCount());
    verifyAll(testGetMenuItemCommand);
  }

  @Test
  public void testFlushMenuItemCache() {
    GetNotMappedMenuItemsForParentCommand testGetMenuItemCommand = createMock(
        GetNotMappedMenuItemsForParentCommand.class);
    treeNodeCache.inject_GetNotMappedMenuItemsForParentCmd(testGetMenuItemCommand);
    testGetMenuItemCommand.flushMenuItemCache(same(context));
    expectLastCall().once();
    replayAll(testGetMenuItemCommand);
    treeNodeCache.flushMenuItemCache();
    verifyAll(testGetMenuItemCommand);
  }

  private void replayAll(Object... mocks) {
    replay(mockStore, wiki);
    replay(mocks);
  }

  private void verifyAll(Object... mocks) {
    verify(mockStore, wiki);
    verify(mocks);
  }
}

@@ -36,16 +36,78 @@
 rvice;%0A%0A
+import static com.celements.common.test.CelementsTestUtils.*;%0A
 import s
@@ -223,44 +223,8 @@
 est;
-%0Aimport org.xwiki.context.Execution;
 %0A%0Aim
@@ -254,39 +254,32 @@
 on.test.Abstract
-Bridged
 ComponentTestCas
@@ -275,20 +275,16 @@
 nentTest
-Case
 ;%0Aimport
@@ -619,15 +619,8 @@
 ract
-Bridged
 Comp
@@ -632,12 +632,8 @@
 Test
-Case
  %7B%0A%0A
@@ -668,30 +668,8 @@
 xt;%0A
-  private XWiki wiki;%0A
   pr
@@ -850,64 +850,33 @@
     
+XWiki 
 wiki = 
-createMock(XWiki.class);%0A    context.setWiki(wiki
+getWikiMock(
 );%0A 
@@ -1010,20 +1010,17 @@
 Cache = 
-new 
+(
 TreeNode
@@ -1028,41 +1028,9 @@
 ache
-();%0A    treeNodeCache.execution =
+)
  Uti
@@ -1049,17 +1049,22 @@
 ent(
-Execution
+ITreeNodeCache
 .cla
@@ -3454,32 +3454,47 @@
 and = createMock
+AndAddToDefault
 (%0A        GetNot
@@ -3694,34 +3694,16 @@
 play
-All(testGetMenuItemCommand
+Default(
 );%0A 
@@ -3765,34 +3765,16 @@
 rify
-All(testGetMenuItemCommand
+Default(
 );%0A 
@@ -3903,16 +3903,31 @@
 eateMock
+AndAddToDefault
 (%0A      
@@ -4163,34 +4163,16 @@
 play
-All(testGetMenuItemCommand
+Default(
 );%0A 
@@ -4224,233 +4224,22 @@
 rify
-All(testGetMenuItemCommand);%0A  %7D%0A%0A  private void replayAll(Object... mocks) %7B%0A    replay(mockStore, wiki);%0A    replay(mocks);%0A  %7D%0A%0A  private void verifyAll(Object... mocks) %7B%0A    verify(mockStore, wiki);%0A    verify(mocks);%0A  %7D
+Default();%0A  %7D%0A
 %0A%7D%0A

refactor TreeNodeCacheTest