/**
 * Copyright (C) 2013 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.masterdb.legalentity;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;

import java.util.LinkedList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

import com.opengamma.id.ExternalIdBundle;
import com.opengamma.id.UniqueId;
import com.opengamma.master.legalentity.LegalEntityDocument;
import com.opengamma.util.money.Currency;
import com.opengamma.util.test.AbstractDbTest;
import com.opengamma.util.test.DbTest;
import com.opengamma.util.test.TestGroup;

/**
 * Test DbLegalEntityBeanMaster.
 */
@Test(groups = TestGroup.UNIT_DB)
public class DbLegalEntityBeanMasterTest extends AbstractDbTest {

  private static final Logger s_logger = LoggerFactory.getLogger(DbLegalEntityBeanMasterTest.class);

  private DbLegalEntityBeanMaster _lenMaster;

  @Factory(dataProvider = "databases", dataProviderClass = DbTest.class)
  public DbLegalEntityBeanMasterTest(String databaseType, String databaseVersion) {
    super(databaseType, databaseVersion);
    s_logger.info("running testcases for {}", databaseType);
  }

  //-------------------------------------------------------------------------
  @Override
  protected void doSetUp() {
    _lenMaster = new DbLegalEntityBeanMaster(getDbConnector());
  }

  @Override
  protected void doTearDown() {
    _lenMaster = null;
  }

  //-------------------------------------------------------------------------
  @Test
  public void test_basics() throws Exception {
    assertNotNull(_lenMaster);
    assertEquals(true, _lenMaster.getUniqueIdScheme().equals("DbLen"));
    assertNotNull(_lenMaster.getDbConnector());
    assertNotNull(_lenMaster.getClock());
  }

  //-------------------------------------------------------------------------
  @Test
  public void test_addAndGet() throws Exception {
    MockLegalEntity len = new MockLegalEntity("London", ExternalIdBundle.of("Test", "OG"), Currency.GBP);
    LegalEntityDocument addDoc = new LegalEntityDocument(len);
    LegalEntityDocument added = _lenMaster.add(addDoc);

    LegalEntityDocument loaded = _lenMaster.get(added.getUniqueId());
    assertEquals(added, loaded);
  }

  //-------------------------------------------------------------------------
  @Test
  public void test_get_not_existent() throws Exception {
    LegalEntityDocument loaded = _lenMaster.get(UniqueId.of("Len", "b"));
    assertEquals(null, loaded);
  }

  //-------------------------------------------------------------------------
  @Test(enabled = false)
  public void test_concurrentModification() {
    final AtomicReference<Throwable> exceptionOccurred = new AtomicReference<Throwable>();
    Runnable task = new Runnable() {
      @Override
      public void run() {
        try {
          test_addAndGet();
        } catch (Throwable th) {
          exceptionOccurred.compareAndSet(null, th);
        }
      }
    };

    // 5 threads for plenty of concurrent activity
    ExecutorService executor = Executors.newFixedThreadPool(5);

    // 10 security inserts is always enough to produce a duplicate key exception
    LinkedList<Future<?>> futures = new LinkedList<Future<?>>();
    for (int i = 0; i < 10; i++) {
      futures.add(executor.submit(task));
    }

    while (!futures.isEmpty()) {
      Future<?> future = futures.poll();
      try {
        future.get();
      } catch (Throwable t) {
        s_logger.error("Exception waiting for task to complete", t);
      }
    }

    assertEquals(null, exceptionOccurred.get());
  }

  //-------------------------------------------------------------------------
  @Test
  public void test_toString() {
    assertEquals("DbLegalEntityBeanMaster[DbLen]", _lenMaster.toString());
  }

}

@@ -2617,32 +2617,96 @@
 --------%0A  @Test
+(expectedExceptions = com.opengamma.DataNotFoundException.class)
 %0A  public void t
@@ -2754,37 +2754,8 @@
 %0A   
- LegalEntityDocument loaded =
  _le
@@ -2783,50 +2783,24 @@
 of(%22
+Db
 Len%22, %22
-b%22));%0A    assertEquals(null, loaded
+12331%22)
 );%0A 

Change in legal entity master's contract to communicate no data found - reviewed by Yomi Ayodele <yomi@opengamma.com>