/**
 * Copyright (C) 2012 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.web.analytics.blotter;

import static org.testng.AssertJUnit.assertEquals;

import java.util.Map;

import org.joda.beans.Bean;
import org.joda.beans.BeanBuilder;
import org.joda.beans.MetaBean;
import org.joda.beans.MetaProperty;
import org.testng.annotations.Test;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.opengamma.financial.security.fx.FXForwardSecurity;
import com.opengamma.financial.security.swap.FixedInterestRateLeg;
import com.opengamma.financial.security.swap.FloatingInterestRateLeg;
import com.opengamma.financial.security.swap.InterestRateNotional;
import com.opengamma.financial.security.swap.SwapLeg;
import com.opengamma.financial.security.swap.SwapSecurity;
import com.opengamma.master.security.ManageableSecurity;
import com.opengamma.util.test.TestGroup;

/**
 * Test.
 */
@Test(groups = TestGroup.UNIT)
@SuppressWarnings("unchecked")
public class BeanBuildingVisitorTest {

  private static final BeanVisitorDecorator s_securityTypeFilter = new PropertyFilter(ManageableSecurity.meta().securityType());

  private static final MetaBeanFactory META_BEAN_FACTORY = new MapMetaBeanFactory(ImmutableSet.<MetaBean>of(
      FXForwardSecurity.meta(),
      SwapSecurity.meta(),
      FixedInterestRateLeg.meta(),
      FloatingInterestRateLeg.meta(),
      InterestRateNotional.meta()));

  private static Map<MetaProperty<?>, Converter<?, ?>> s_regionConverters =
      ImmutableMap.<MetaProperty<?>, Converter<?, ?>>of(
          FXForwardSecurity.meta().regionId(), new FXRegionConverter(),
          SwapLeg.meta().regionId(), new StringToRegionIdConverter());

  private static final Converters CONVERTERS = new Converters(s_regionConverters,
                                                              BlotterUtils.getStringConvert());

  /**
   * test building a nice simple security
   */
  @Test
  public void buildFXForwardSecurity() {
    BeanVisitor<BeanBuilder<Bean>> visitor = new BeanBuildingVisitor<>(BlotterTestUtils.FX_FORWARD_DATA_SOURCE,
                                                                       META_BEAN_FACTORY,
                                                                       CONVERTERS);
    MetaBean metaBean = META_BEAN_FACTORY.beanFor(BlotterTestUtils.FX_FORWARD_DATA_SOURCE);
    BeanBuilder<ManageableSecurity> beanBuilder =
        (BeanBuilder<ManageableSecurity>) new BeanTraverser(s_securityTypeFilter).traverse(metaBean, visitor);
    ManageableSecurity security = beanBuilder.build();
    assertEquals(BlotterTestUtils.FX_FORWARD, security);
  }

  /**
   * test building a security with fields that are also beans and have bean fields themsevles (legs, notionals)
   */
  @Test
  public void buildSwapSecurity() {
    BeanVisitor<BeanBuilder<Bean>> visitor = new BeanBuildingVisitor<>(BlotterTestUtils.SWAP_DATA_SOURCE,
                                                                       META_BEAN_FACTORY,
                                                                       CONVERTERS);
    MetaBean metaBean = META_BEAN_FACTORY.beanFor(BlotterTestUtils.SWAP_DATA_SOURCE);
    BeanBuilder<ManageableSecurity> beanBuilder =
        (BeanBuilder<ManageableSecurity>) new BeanTraverser(s_securityTypeFilter).traverse(metaBean, visitor);
    ManageableSecurity security = beanBuilder.build();
    assertEquals(BlotterTestUtils.SWAP, security);
  }
}

@@ -212,26 +212,19 @@
 sert
-JUnit
 .assert
-Equals
+True
 ;%0A%0Ai
@@ -413,16 +413,17 @@
 .Test;%0A%0A
+%0A
 import c
@@ -985,16 +985,67 @@
 stGroup;
+%0Aimport com.opengamma.util.beancompare.BeanCompare;
 %0A%0A/**%0A *
@@ -2721,38 +2721,62 @@
 ld();%0A    assert
-Equals
+True(BeanCompare.equalIgnoring
 (BlotterTestUtil
@@ -2793,24 +2793,63 @@
 RD, security
+, ManageableSecurity.meta().uniqueId())
 );%0A  %7D%0A%0A  /*
@@ -3608,14 +3608,38 @@
 sert
-Equals
+True(BeanCompare.equalIgnoring
 (Blo
@@ -3666,16 +3666,55 @@
 security
+, ManageableSecurity.meta().uniqueId())
 );%0A  %7D%0A%7D

Use BeanCompare in BeanBuildingVisitorTest.