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;

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 = getComponentManager().lookup(Execution.class);
  }

  @Test
  public void testGetMappedMenuItemsForParentCmd() {
    assertFalse(treeNodeCache.getMappedMenuItemsForParentCmd().is_isActive());
  }
  
  @Test
  public void testGetMappedMenuItemsForParentCmd_injected() {
    GetMappedMenuItemsForParentCommand testGetMenuItemCommand =
      new GetMappedMenuItemsForParentCommand();
    testGetMenuItemCommand.set_isActive(true);
    treeNodeCache.inject_GetMappedMenuItemsForParentCmd(testGetMenuItemCommand);
    assertEquals(testGetMenuItemCommand, treeNodeCache.getMappedMenuItemsForParentCmd());
    assertTrue(treeNodeCache.getMappedMenuItemsForParentCmd().is_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.set_isActive(true);
    context.put(GetMappedMenuItemsForParentCommand.CELEMENTS_MAPPED_MENU_ITEMS_KEY, 
        testGetMenuItemCommand);
    assertTrue(treeNodeCache.getMappedMenuItemsForParentCmd().is_isActive());
    
    testGetMenuItemCommand.set_isActive(false);
    context.put(GetMappedMenuItemsForParentCommand.CELEMENTS_MAPPED_MENU_ITEMS_KEY, 
        testGetMenuItemCommand);
    assertFalse(treeNodeCache.getMappedMenuItemsForParentCmd().is_isActive());
  }

  @Test
  public void testQueryCount() {
    fail("Not yet implemented");
  }

  @Test
  public void testFlushMenuItemCache() {
    fail("Not yet implemented");
  }

}

@@ -36,16 +36,56 @@
 rvice;%0A%0A
+import static org.easymock.EasyMock.*;%0A%0A
 import s
@@ -3539,127 +3539,1085 @@
     
-fail(%22Not yet implemented%22);%0A  %7D%0A%0A  @Test%0A  public void testFlushMenuItemCache() %7B%0A    fail(%22Not yet implemented%22);%0A  %7D%0A
+GetNotMappedMenuItemsForParentCommand testGetMenuItemCommand =%0A      createMock(GetNotMappedMenuItemsForParentCommand.class);%0A    treeNodeCache.inject_GetNotMappedMenuItemsForParentCmd(testGetMenuItemCommand);%0A    expect(testGetMenuItemCommand.queryCount()).andReturn(15);%0A    replayAll(testGetMenuItemCommand);%0A    assertEquals(15, treeNodeCache.queryCount());%0A    verifyAll(testGetMenuItemCommand);%0A  %7D%0A%0A  @Test%0A  public void testFlushMenuItemCache() %7B%0A    GetNotMappedMenuItemsForParentCommand testGetMenuItemCommand =%0A      createMock(GetNotMappedMenuItemsForParentCommand.class);%0A    treeNodeCache.inject_GetNotMappedMenuItemsForParentCmd(testGetMenuItemCommand);%0A    testGetMenuItemCommand.flushMenuItemCache(same(context));%0A    expectLastCall().once();%0A    replayAll(testGetMenuItemCommand);%0A    treeNodeCache.flushMenuItemCache();%0A    verifyAll(testGetMenuItemCommand);%0A  %7D%0A%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
 %0A%7D%0A

add missing tests for queryCount and flushMenuItemCache