package com.opengamma.language.object;

import com.opengamma.financial.security.fx.FXForwardSecurity;
import com.opengamma.id.ExternalId;
import com.opengamma.id.ExternalIdBundle;
import com.opengamma.id.UniqueId;
import com.opengamma.language.function.MetaFunction;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import javax.time.calendar.ZonedDateTime;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

public class CreateBeanFunctionTest {

  /**
   * This is needed to register the meta-bean for the security type so it can be looked up when the bean is created.
   */
  @BeforeClass
  public void registerMetaBean() throws ClassNotFoundException {
    Class.forName("com.opengamma.financial.security.fx.FXForwardSecurity");
  }

  @Test
  public void invoke() {
    String testFunctionName = "testFunctionName";
    CreateBeanFunction function = new CreateBeanFunction(testFunctionName, FXForwardSecurity.class);

    UniqueId uniqueId = UniqueId.of("Tst", "uid");
    ExternalId regionId = ExternalId.of("Tst", "region");
    ExternalId underlyingId = ExternalId.of("Tst", "underlying");
    ZonedDateTime forwardDate = ZonedDateTime.now();
    ExternalIdBundle idBundle = ExternalIdBundle.of(ExternalId.of("Tst", "id"));
    String name = "securityName";
    String securityType = "securityType";
    Object[] parameters = {uniqueId, idBundle, name, securityType, underlyingId, forwardDate, regionId};

    MetaFunction metaFunction = function.getMetaFunction();
    assertNotNull(metaFunction);
    assertEquals(testFunctionName, metaFunction.getName());
    CreateBeanFunction.Invoker invoker = (CreateBeanFunction.Invoker) metaFunction.getInvoker();
    Object bean = invoker.invokeImpl(null, parameters);
    assertNotNull(bean);
    assertTrue(bean instanceof FXForwardSecurity);
    FXForwardSecurity security = (FXForwardSecurity) bean;
    assertEquals(idBundle, security.getExternalIdBundle());
    assertEquals(name, security.getName());
    assertEquals(securityType, security.getSecurityType());
    assertEquals(underlyingId, security.getUnderlyingId());
    assertEquals(forwardDate, security.getForwardDate());
    assertEquals(regionId, security.getRegionId());
  }
}

@@ -44,486 +44,540 @@
 ort 
-com.opengamma.financial.security.fx.FXForwardSecurity;%0Aimport com.opengamma.id.ExternalId;%0Aimport com.opengamma.id.ExternalIdBundl
+static org.testng.Assert.assertEquals;%0Aimport static org.testng.Assert.assertNotNull;%0Aimport static org.testng.Assert.assertTru
 e;%0A
+%0A
 import 
-com.opengamma.id.UniqueId;%0Aimport com.opengamma.language.function.MetaFunction;%0Aimport org.testng.annotations.BeforeClass;%0Aimport org.testng.annotations.Test;%0A%0Aimport javax.time.calendar.ZonedDateTime;%0A%0Aimport static org.testng.Assert.assertEquals;%0Aimport static org.testng.Assert.assertNotNull;%0Aimport static org.testng.Assert.assertTrue
+java.util.Collections;%0Aimport java.util.Map;%0A%0Aimport javax.time.calendar.ZonedDateTime;%0A%0Aimport org.testng.annotations.BeforeClass;%0Aimport org.testng.annotations.Test;%0A%0Aimport com.opengamma.financial.security.fx.FXForwardSecurity;%0Aimport com.opengamma.id.ExternalId;%0Aimport com.opengamma.id.ExternalIdBundle;%0Aimport com.opengamma.id.UniqueId;%0Aimport com.opengamma.language.function.MetaFunction
 ;%0A%0Ap
@@ -1470,24 +1470,101 @@
 urityType%22;%0A
+    Map%3CString, String%3E attributes = Collections.singletonMap(%22Foo%22, %22Bar%22);%0A
     Object%5B%5D
@@ -1617,16 +1617,28 @@
 ityType,
+ attributes,
  underly
@@ -1665,16 +1665,17 @@
 regionId
+ 
 %7D;%0A%0A    
@@ -2271,24 +2271,80 @@
 ityType());%0A
+    assertEquals(attributes, security.getAttributes());%0A
     assertEq

Correct unit test to include attributes field for a security.