/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.master.config.impl;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.AssertJUnit.assertEquals;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.core.config.impl.ConfigItem;
import com.opengamma.id.ExternalId;
import com.opengamma.master.config.ConfigDocument;
import com.opengamma.master.config.ConfigMaster;
import com.opengamma.master.config.ConfigSearchRequest;
import com.opengamma.master.config.ConfigSearchSortOrder;

/**
 * Test {@link ConfigSearchIterator}.
 */
@Test
public class ConfigMasterIteratorTest {

  private ConfigItem<ExternalId> _item1;
  private ConfigItem<ExternalId> _item2;
  private ConfigItem<ExternalId> _item3;
  private ConfigItem<ExternalId> _item4;

  private ConfigMaster _configMaster;

  @SuppressWarnings("unchecked")
  @BeforeMethod
  public void setUp() throws Exception {
    _item1 = ConfigItem.of(ExternalId.of("A", "B"), "Test1");
    _item2 = ConfigItem.of(ExternalId.of("C", "D"), "Test2");
    _item3 = ConfigItem.of(ExternalId.of("E", "F"), "Test3");
    _item4 = ConfigItem.of(ExternalId.of("E", "F"), "Test3");
    
    InMemoryConfigMaster configMaster = new InMemoryConfigMaster();
    _item1 = (ConfigItem<ExternalId>) configMaster.add(new ConfigDocument(_item1)).getConfig();
    _item2 = (ConfigItem<ExternalId>) configMaster.add(new ConfigDocument(_item2)).getConfig();
    _item3 = (ConfigItem<ExternalId>) configMaster.add(new ConfigDocument(_item3)).getConfig();
    _item4 = (ConfigItem<ExternalId>) configMaster.add(new ConfigDocument(_item4)).getConfig();
    _configMaster = configMaster;
  }

  @AfterMethod
  public void tearDown() throws Exception {
    _configMaster = null;
  }

  //-------------------------------------------------------------------------
  @Test(expectedExceptions = IllegalArgumentException.class)
  public void test_constructor_2arg_nullMaster() throws Exception {
    new ConfigSearchIterator<ExternalId>(null, new ConfigSearchRequest<ExternalId>());
  }

  @Test(expectedExceptions = IllegalArgumentException.class)
  public void test_constructor_2arg_nullRequest() throws Exception {
    new ConfigSearchIterator<ExternalId>(_configMaster, null);
  }

  public void iterate() throws Exception {
    ConfigSearchRequest<ExternalId> request = new ConfigSearchRequest<ExternalId>();
    request.setType(ExternalId.class);
    request.setSortOrder(ConfigSearchSortOrder.NAME_ASC);
    
    ConfigSearchIterator<ExternalId> iterator = new ConfigSearchIterator<ExternalId>(_configMaster, request);
    assertEquals(true, iterator.hasNext());
    assertEquals(0, iterator.nextIndex());
    assertEquals(_item1, iterator.next());
    assertEquals(1, iterator.nextIndex());
    
    assertEquals(true, iterator.hasNext());
    assertEquals(1, iterator.nextIndex());
    assertEquals(_item2, iterator.next());
    assertEquals(2, iterator.nextIndex());
    
    assertEquals(true, iterator.hasNext());
    assertEquals(2, iterator.nextIndex());
    assertEquals(_item3, iterator.next());
    assertEquals(3, iterator.nextIndex());
    
    assertEquals(true, iterator.hasNext());
    assertEquals(3, iterator.nextIndex());
    assertEquals(_item4, iterator.next());
    assertEquals(4, iterator.nextIndex());
    
    assertEquals(false, iterator.hasNext());
    assertEquals(4, iterator.nextIndex());
  }

  @SuppressWarnings("unchecked")
  @Test(expectedExceptions = OpenGammaRuntimeException.class)
  public void iterateError() throws Exception {
    ConfigSearchRequest<ExternalId> request = new ConfigSearchRequest<ExternalId>();
    request.setType(ExternalId.class);
    request.setSortOrder(ConfigSearchSortOrder.NAME_ASC);
    
    ConfigMaster mockMaster = mock(ConfigMaster.class);
    when(mockMaster.search(any(ConfigSearchRequest.class))).thenThrow(new IllegalStateException());
    
    ConfigSearchIterator<ExternalId> iterator = new ConfigSearchIterator<ExternalId>(mockMaster, request);
    iterator.hasNext();
  }

}

@@ -932,153 +932,117 @@
 nfig
-Item%3CExternalId%3E _item1;%0A  private ConfigItem%3CExternalId%3E _item2;%0A  private ConfigItem%3CExternalId%3E _item3;%0A  private ConfigItem%3CExternalId%3E _item
+Document _doc1;%0A  private ConfigDocument _doc2;%0A  private ConfigDocument _doc3;%0A  private ConfigDocument _doc
 4;%0A%0A
@@ -1084,41 +1084,8 @@
 r;%0A%0A
-  @SuppressWarnings(%22unchecked%22)%0A
   @B
@@ -1137,25 +1137,47 @@
 ption %7B%0A    
-_
+ConfigItem%3CExternalId%3E 
 item1 = Conf
@@ -1221,25 +1221,47 @@
 est1%22);%0A    
-_
+ConfigItem%3CExternalId%3E 
 item2 = Conf
@@ -1305,25 +1305,47 @@
 est2%22);%0A    
-_
+ConfigItem%3CExternalId%3E 
 item3 = Conf
@@ -1389,25 +1389,47 @@
 est3%22);%0A    
-_
+ConfigItem%3CExternalId%3E 
 item4 = Conf
@@ -1555,40 +1555,14 @@
    _
-item1 = (ConfigItem%3CExternalId%3E)
+doc1 =
  con
@@ -1598,67 +1598,28 @@
 ent(
-_
 item1))
-.getConfig();%0A    _item2 = (ConfigItem%3CExternalId%3E)
+;%0A    _doc2 =
  con
@@ -1655,67 +1655,28 @@
 ent(
-_
 item2))
-.getConfig();%0A    _item3 = (ConfigItem%3CExternalId%3E)
+;%0A    _doc3 =
  con
@@ -1712,67 +1712,28 @@
 ent(
-_
 item3))
-.getConfig();%0A    _item4 = (ConfigItem%3CExternalId%3E)
+;%0A    _doc4 =
  con
@@ -1769,28 +1769,15 @@
 ent(
-_
 item4))
-.getConfig()
 ;%0A  
@@ -2845,20 +2845,19 @@
 Equals(_
-item
+doc
 1, itera
@@ -3022,20 +3022,19 @@
 Equals(_
-item
+doc
 2, itera
@@ -3199,20 +3199,19 @@
 Equals(_
-item
+doc
 3, itera
@@ -3376,20 +3376,19 @@
 Equals(_
-item
+doc
 4, itera

Fix test