@Test
  public void testUndefinedTypeEmptyConverter() {
    TypeConverter converter = getConverter(new ModelNode());
    Assert.assertEquals(SimpleType.STRING, converter.getOpenType());

    ModelNode node = new ModelNode();
    node.get("abc");
    node.get("def");
    node.protect();

    String json = assertCast(String.class, converter.fromModelNode(node));
    Assert.assertEquals(node.resolve(), ModelNode.fromJSONString(json));
    Assert.assertEquals(json, assertCast(String.class, converter.fromModelNode(node)));
    assertToArray(converter, json);
  }
  @Test
  public void testPropertyValueTypeConverter() {
    ModelNode description = createDescription(ModelType.PROPERTY, ModelType.INT);
    TypeConverter converter = getConverter(description);

    ModelNode node = new ModelNode();
    node.set("name", 1);
    node.protect();

    Assert.assertEquals(SimpleType.STRING, converter.getOpenType());
    String dmr = assertCast(String.class, converter.fromModelNode(node));
    Assert.assertEquals(node, ModelNode.fromString(dmr));
    Assert.assertEquals(dmr, assertCast(String.class, converter.fromModelNode(node)));
    assertToArray(converter, dmr);
  }
  @Test
  public void testProperPropertyValueTypeExpressionConverter() throws Exception {
    ModelNode description = createDescription(ModelType.PROPERTY, ModelType.INT);
    TypeConverter converter =
        TypeConverters.createLegacyTypeConverters(true).getConverter(description);

    CompositeType type = assertCast(CompositeType.class, converter.getOpenType());
    Set<String> keys = type.keySet();
    Assert.assertEquals(2, keys.size());
    assertCompositeType(type, "name", String.class.getName(), "The property name");
    assertCompositeType(type, "value", Integer.class.getName(), "The property value");

    CompositeData data =
        assertCast(
            CompositeData.class,
            converter.fromModelNode(
                new ModelNode().setExpression("one", "${this.should.not.exist.!!!!!:1}")));
    Assert.assertEquals(type, data.getCompositeType());
    Assert.assertEquals("one", data.get("name"));
    Assert.assertEquals(1, data.get("value"));

    data =
        new CompositeDataSupport(
            type, new String[] {"name", "value"}, new Object[] {"two", Integer.valueOf(2)});
    ModelNode newNode = converter.toModelNode(data);
    Assert.assertEquals(ModelType.PROPERTY, newNode.getType());
    Assert.assertEquals(new ModelNode().set("two", 2), newNode);

    assertToArray(converter, data);
  }
  @Test
  public void testByteArrayObject() throws Exception {
    ModelNode description = createDescription(ModelType.OBJECT, ModelType.BYTES);
    TypeConverter converter = getConverter(description);

    assertMapType(
        assertCast(TabularType.class, converter.getOpenType()),
        SimpleType.STRING,
        ArrayType.getPrimitiveArrayType(byte[].class));

    ModelNode node = new ModelNode();
    node.get("one").set(new byte[] {1, 2});
    node.get("two").set(new byte[] {3, 4});

    TabularData tabularData = assertCast(TabularData.class, converter.fromModelNode(node));
    Assert.assertEquals(2, tabularData.size());
    Assert.assertTrue(
        Arrays.equals(
            new byte[] {1, 2}, (byte[]) tabularData.get(new Object[] {"one"}).get("value")));
    Assert.assertTrue(
        Arrays.equals(
            new byte[] {3, 4}, (byte[]) tabularData.get(new Object[] {"two"}).get("value")));

    // Allow plain map as well? Yeah why not!
    Map<String, byte[]> map = new HashMap<String, byte[]>();
    map.put("one", new byte[] {1, 2});
    map.put("two", new byte[] {3, 4});
    Assert.assertEquals(node, converter.toModelNode(map));
  }
  @Test
  public void testSimpleTypeObject() throws Exception {
    ModelNode description = createDescription(ModelType.OBJECT, ModelType.LONG);
    TypeConverter converter = getConverter(description);

    assertMapType(
        assertCast(TabularType.class, converter.getOpenType()), SimpleType.STRING, SimpleType.LONG);

    ModelNode node = new ModelNode();
    node.get("one").set(1L);
    node.get("two").set(2L);

    TabularData tabularData = assertCast(TabularData.class, converter.fromModelNode(node));
    Assert.assertEquals(2, tabularData.size());
    Assert.assertEquals(Long.valueOf(1), tabularData.get(new Object[] {"one"}).get("value"));
    Assert.assertEquals(Long.valueOf(2), tabularData.get(new Object[] {"two"}).get("value"));

    Assert.assertEquals(node, converter.toModelNode(tabularData));

    // Allow plain map as well? Yeah why not!
    Map<String, Long> map = new HashMap<String, Long>();
    map.put("one", 1L);
    map.put("two", 2L);
    Assert.assertEquals(node, converter.toModelNode(map));
  }
  @Test
  public void testProperPropertyTypeConverter() throws Exception {
    ModelNode description = createDescription(ModelType.PROPERTY);
    TypeConverter converter =
        TypeConverters.createLegacyTypeConverters(true).getConverter(description);

    CompositeType type = assertCast(CompositeType.class, converter.getOpenType());
    Set<String> keys = type.keySet();
    Assert.assertEquals(2, keys.size());
    assertCompositeType(type, "name", String.class.getName(), "The property name");
    assertCompositeType(type, "value", String.class.getName(), "The property value");

    CompositeData data =
        assertCast(CompositeData.class, converter.fromModelNode(new ModelNode().set("one", "uno")));
    Assert.assertEquals(type, data.getCompositeType());
    Assert.assertEquals("one", data.get("name"));
    Assert.assertEquals("uno", data.get("value"));

    data =
        new CompositeDataSupport(type, new String[] {"name", "value"}, new String[] {"two", "dos"});
    ModelNode newNode = converter.toModelNode(data);
    Assert.assertEquals(ModelType.PROPERTY, newNode.getType());
    Assert.assertEquals(new ModelNode().set("two", "dos"), newNode);

    assertToArray(converter, data);
  }
 @Test
 public void testTypeEmptyConverter() {
   ModelNode description = createDescription(ModelType.TYPE);
   TypeConverter converter = getConverter(description);
   Assert.assertEquals(SimpleType.STRING, converter.getOpenType());
   Assert.assertNull(converter.fromModelNode(new ModelNode().set("")));
 }
 @Test
 public void testBigDecimalEmptyConverter() {
   ModelNode description = createDescription(ModelType.BIG_DECIMAL);
   TypeConverter converter = getConverter(description);
   Assert.assertEquals(SimpleType.BIGDECIMAL, converter.getOpenType());
   Assert.assertNull(converter.fromModelNode(new ModelNode().set("")));
 }
 @Test
 public void testBigIntegerEmptyConverter() {
   ModelNode description = createDescription(ModelType.BIG_INTEGER);
   TypeConverter converter = getConverter(description);
   Assert.assertEquals(SimpleType.BIGINTEGER, converter.getOpenType());
   Assert.assertNull(converter.fromModelNode(new ModelNode().set("")));
 }
  @Test
  public void testJsonObjectInComplexValue() throws Exception {
    ModelNode description = createDescription(ModelType.OBJECT);
    ModelNode complexValueType = new ModelNode();
    complexValueType.get("value", DESCRIPTION).set("A  value");
    complexValueType.get("value", TYPE).set(ModelType.OBJECT);
    description.get(VALUE_TYPE).set(complexValueType);

    TypeConverter converter = getConverter(description);

    CompositeType type = assertCast(CompositeType.class, converter.getOpenType());
    Set<String> keys = type.keySet();
    Assert.assertEquals(1, keys.size());

    Assert.assertEquals(SimpleType.STRING, type.getType("value"));

    ModelNode node = new ModelNode();
    node.get("value", "long").set(1L);
    node.get("value", "string").set("test");

    CompositeData data = assertCast(CompositeData.class, converter.fromModelNode(node));
    Assert.assertEquals(type, data.getCompositeType());
    Assert.assertEquals(
        ModelNode.fromJSONString(node.toJSONString(false)), converter.toModelNode(data));
  }
  @Test
  public void testJsonObjectInList() throws Exception {
    ModelNode description = createDescription(ModelType.LIST, ModelType.OBJECT);

    TypeConverter converter = getConverter(description);

    ArrayType<String> arrayType = assertCast(ArrayType.class, converter.getOpenType());
    Assert.assertEquals(
        SimpleType.STRING, assertCast(SimpleType.class, arrayType.getElementOpenType()));

    ModelNode list = new ModelNode();
    ModelNode value1 = new ModelNode();
    value1.get("long").set(5L);
    value1.get("string").set("Value");
    value1.get("a", "b").set(true);
    value1.get("c", "d").set(40);
    list.add(value1);
    ModelNode value2 = new ModelNode();
    value2.get("long").set(10L);
    list.add(value2);

    String json1 = value1.toJSONString(false);
    String json2 = value2.toJSONString(false);
    String[] data = assertCast(String[].class, converter.fromModelNode(list));
    Assert.assertEquals(2, data.length);
    Assert.assertEquals(json1, data[0]);
    Assert.assertEquals(json2, data[1]);

    Assert.assertEquals(
        ModelNode.fromJSONString(list.toJSONString(false)), converter.toModelNode(data));
  }
  @Test
  public void testPropertyValueTypeExpressionConverter() throws Exception {
    ModelNode description = createDescription(ModelType.PROPERTY, ModelType.INT);
    TypeConverter converter = getConverter(description);

    ModelNode node = new ModelNode();
    node.set("name", "${this.should.not.exist.!!!!!:1}");
    node.protect();

    ModelNode expected = node.clone().resolve();

    Assert.assertEquals(SimpleType.STRING, converter.getOpenType());
    String dmr = assertCast(String.class, converter.fromModelNode(node));
    Assert.assertEquals(expected, ModelNode.fromString(dmr));
    Assert.assertEquals(dmr, assertCast(String.class, converter.fromModelNode(expected)));
    assertToArray(converter, dmr);
  }
 public void testStringConverter() {
   ModelNode description = createDescription(ModelType.STRING);
   TypeConverter converter = getConverter(description);
   Assert.assertEquals(SimpleType.STRING, converter.getOpenType());
   Assert.assertEquals(
       "A", assertCast(String.class, converter.fromModelNode(new ModelNode().set("A"))));
   Assert.assertEquals("B", converter.toModelNode("B").asString());
   assertToArray(converter, "A", "B");
 }
 @Test
 public void testLongConverter() {
   ModelNode description = createDescription(ModelType.LONG);
   TypeConverter converter = getConverter(description);
   Assert.assertEquals(SimpleType.LONG, converter.getOpenType());
   Assert.assertEquals(
       Long.valueOf(1), assertCast(Long.class, converter.fromModelNode(new ModelNode().set(1L))));
   Assert.assertEquals(2L, converter.toModelNode(Long.valueOf(2)).asLong());
   assertToArray(converter, Long.valueOf(1), Long.valueOf(2));
 }
  @Test
  public void testUndefinedTypeConverter() {
    TypeConverter converter = getConverter(new ModelNode());
    Assert.assertEquals(SimpleType.STRING, converter.getOpenType());

    ModelNode node = new ModelNode();
    // BES 2013/01/10 This uses BigInteger; I'm not sure why. But use a value > Long.MAX_VALUE
    // so the json parser won't convert it down to a long or int resulting in a different value
    // See AS7-4913
    // Likely BigInteger was used *because of* the problem discussed in AS7-4913
    node.get("abc").set(new BigInteger(String.valueOf(Long.MAX_VALUE) + "0"));
    node.get("def").set(false);
    node.protect();

    String json = assertCast(String.class, converter.fromModelNode(node));
    Assert.assertEquals(node, ModelNode.fromJSONString(json));
    Assert.assertEquals(json, assertCast(String.class, converter.fromModelNode(node)));
    assertToArray(converter, json);
  }
 @Test
 public void testDoubleConverter() {
   ModelNode description = createDescription(ModelType.DOUBLE);
   TypeConverter converter = getConverter(description);
   Assert.assertEquals(SimpleType.DOUBLE, converter.getOpenType());
   Assert.assertEquals(
       Double.valueOf(1),
       assertCast(Double.class, converter.fromModelNode(new ModelNode().set(1))));
   Assert.assertEquals(Double.valueOf(2), converter.toModelNode(Double.valueOf(2)).asDouble());
   assertToArray(converter, Double.valueOf(1), Double.valueOf(2));
 }
 @Test
 public void testIntConverter() {
   ModelNode description = createDescription(ModelType.INT);
   TypeConverter converter = getConverter(description);
   Assert.assertEquals(SimpleType.INTEGER, converter.getOpenType());
   Assert.assertEquals(
       Integer.valueOf(1),
       assertCast(Integer.class, converter.fromModelNode(new ModelNode().set(1))));
   Assert.assertEquals(2, converter.toModelNode(Integer.valueOf(2)).asInt());
   assertToArray(converter, Integer.valueOf(1), Integer.valueOf(2));
 }
 @Test
 public void testTypeConverter() {
   ModelNode description = createDescription(ModelType.TYPE);
   TypeConverter converter = getConverter(description);
   Assert.assertEquals(SimpleType.STRING, converter.getOpenType());
   Assert.assertEquals(
       "OBJECT",
       assertCast(String.class, converter.fromModelNode(new ModelNode().set(ModelType.OBJECT))));
   Assert.assertEquals(ModelType.LONG, converter.toModelNode("LONG").asType());
   assertToArray(converter, "LONG", "INT");
 }
 @Test
 public void testBooleanConverter() {
   ModelNode description = createDescription(ModelType.BOOLEAN);
   TypeConverter converter = getConverter(description);
   Assert.assertEquals(SimpleType.BOOLEAN, converter.getOpenType());
   Assert.assertEquals(
       Boolean.FALSE,
       assertCast(Boolean.class, converter.fromModelNode(new ModelNode().set(false))));
   Assert.assertEquals(true, converter.toModelNode(Boolean.TRUE).asBoolean());
   assertToArray(converter, Boolean.TRUE, Boolean.FALSE);
 }
 @Test
 public void testBigDecimalConverter() {
   ModelNode description = createDescription(ModelType.BIG_DECIMAL);
   TypeConverter converter = getConverter(description);
   Assert.assertEquals(SimpleType.BIGDECIMAL, converter.getOpenType());
   Assert.assertEquals(
       BigDecimal.valueOf(1),
       assertCast(
           BigDecimal.class, converter.fromModelNode(new ModelNode().set(BigDecimal.valueOf(1)))));
   Assert.assertEquals(2, converter.toModelNode(BigDecimal.valueOf(2)).asInt());
   assertToArray(converter, BigDecimal.valueOf(1), BigDecimal.valueOf(2));
 }
 @Test
 public void testSimpleTypeEmptyList() throws Exception {
   ModelNode description = createDescription(ModelType.LIST, ModelType.INT);
   TypeConverter converter = getConverter(description);
   Assert.assertEquals(ArrayType.getArrayType(SimpleType.INTEGER), converter.getOpenType());
   ModelNode node = new ModelNode();
   node.add("");
   node.add("");
   Assert.assertTrue(
       Arrays.equals(
           new Integer[] {null, null},
           assertCast(Integer[].class, converter.fromModelNode(node))));
 }
 @Test
 public void testStringExpressionConverter() throws Exception {
   ModelNode description = createDescription(ModelType.STRING);
   TypeConverter converter = getConverter(description);
   String data =
       assertCast(
           String.class,
           converter.fromModelNode(
               new ModelNode().setExpression("${this.should.not.exist.!!!!!:A}")));
   Assert.assertEquals("A", data);
   ModelNode newNode = converter.toModelNode("${this.should.not.exist.!!!!!:B}");
   Assert.assertEquals(ModelType.EXPRESSION, newNode.getType());
   Assert.assertEquals("${this.should.not.exist.!!!!!:B}", newNode.asString());
 }
 @Test
 public void testStringVaultExpressionConverter() throws Exception {
   ModelNode description = createDescription(ModelType.STRING);
   TypeConverter converter = getConverter(description);
   String data =
       assertCast(
           String.class,
           converter.fromModelNode(
               new ModelNode().setExpression("${VAULT::keystore_pass::password::xxx}")));
   Assert.assertEquals("${VAULT::keystore_pass::password::xxx}", data);
   ModelNode newNode = converter.toModelNode("${VAULT::keystore_pass::password::yyyy}");
   Assert.assertEquals(ModelType.EXPRESSION, newNode.getType());
   Assert.assertEquals("${VAULT::keystore_pass::password::yyyy}", newNode.asString());
 }
  @Test
  public void testUndefinedTypeExpressionConverter() throws Exception {
    ModelNode description = new ModelNode();
    description.get(EXPRESSIONS_ALLOWED).set(true);
    TypeConverter converter = getConverter(description);

    ModelNode node = new ModelNode();
    node.get("abc").setExpression("${this.should.not.exist.!!!!!:10}");
    node.get("def").setExpression("${this.should.not.exist.!!!!!:false}");
    node.protect();

    String json = assertCast(String.class, converter.fromModelNode(node));
    Assert.assertEquals(node, ModelNode.fromJSONString(json));
    assertToArray(converter, json, null);
  }
 @Test
 public void testByteArrayList() throws Exception {
   ModelNode description = createDescription(ModelType.LIST, ModelType.BYTES);
   TypeConverter converter = getConverter(description);
   Assert.assertEquals(
       ArrayType.getArrayType(ArrayType.getPrimitiveArrayType(byte[].class)),
       converter.getOpenType());
   ModelNode node = new ModelNode();
   node.add(new byte[] {1, 2});
   node.add(new byte[] {3, 4});
   byte[][] bytes = assertCast(byte[][].class, converter.fromModelNode(node));
   Assert.assertEquals(2, bytes.length);
   Assert.assertTrue(Arrays.equals(new byte[] {1, 2}, bytes[0]));
   Assert.assertTrue(Arrays.equals(new byte[] {3, 4}, bytes[1]));
   Assert.assertEquals(node, converter.toModelNode(new byte[][] {{1, 2}, {3, 4}}));
 }
 @Test
 public void testBooleanExpressionConverter() throws Exception {
   ModelNode description = createDescription(ModelType.BOOLEAN);
   TypeConverter converter = getConverter(description);
   Boolean data =
       assertCast(
           Boolean.class,
           converter.fromModelNode(
               new ModelNode().setExpression("${this.should.not.exist.!!!!!:false}")));
   Assert.assertEquals(Boolean.FALSE, data);
   try {
     converter.toModelNode("${this.should.not.exist.!!!!!:2}");
     Assert.fail("Should not have been able to convert to a model node");
   } catch (IllegalArgumentException expexted) {
   }
 }
 @Test
 public void testDoubleExpressionConverter() throws Exception {
   ModelNode description = createDescription(ModelType.DOUBLE);
   TypeConverter converter = getConverter(description);
   Double data =
       assertCast(
           Double.class,
           converter.fromModelNode(
               new ModelNode().setExpression("${this.should.not.exist.!!!!!:1}")));
   Assert.assertEquals(Double.valueOf(1), data);
   try {
     converter.toModelNode("${this.should.not.exist.!!!!!:2}");
     Assert.fail("Should not have been able to convert to a model node");
   } catch (IllegalArgumentException expexted) {
   }
 }
 @Test
 public void testTypeExpressionConverter() throws Exception {
   ModelNode description = createDescription(ModelType.TYPE);
   TypeConverter converter = getConverter(description);
   String data =
       assertCast(
           String.class,
           converter.fromModelNode(
               new ModelNode().setExpression("${this.should.not.exist.!!!!!:OBJECT}")));
   Assert.assertEquals(ModelType.OBJECT, ModelType.valueOf(data));
   try {
     converter.toModelNode("${this.should.not.exist.!!!!!:LONG}");
     Assert.fail("Should not have been able to convert to a model node");
   } catch (IllegalArgumentException expexted) {
   }
 }
  @Test
  public void testSimpleTypeExpressionList() throws Exception {
    ModelNode description = createDescription(ModelType.LIST, ModelType.INT);
    description.get(EXPRESSIONS_ALLOWED).set(true);
    TypeConverter converter = getConverter(description);
    ArrayType<?> arrayType = assertCast(ArrayType.class, converter.getOpenType());
    Assert.assertEquals(SimpleType.INTEGER, arrayType.getElementOpenType());

    ModelNode node = new ModelNode();
    node.addExpression("${this.should.not.exist.!!!!!:1}");
    node.addExpression("${this.should.not.exist.!!!!!:2}");

    Integer[] data = assertCast(Integer[].class, converter.fromModelNode(node));
    Assert.assertEquals(Integer.valueOf(1), data[0]);
    Assert.assertEquals(Integer.valueOf(2), data[1]);
  }
  @Test
  public void testSimpleTypeObjectExpressions() throws Exception {
    ModelNode description = createDescription(ModelType.OBJECT, ModelType.LONG);
    description.get(EXPRESSIONS_ALLOWED).set(true);
    TypeConverter converter = getConverter(description);

    assertMapType(
        assertCast(TabularType.class, converter.getOpenType()), SimpleType.STRING, SimpleType.LONG);

    ModelNode node = new ModelNode();
    node.get("one").setExpression("${this.should.not.exist.!!!!!:1}");
    node.get("two").setExpression("${this.should.not.exist.!!!!!:2}");

    TabularData tabularData = assertCast(TabularData.class, converter.fromModelNode(node));
    Assert.assertEquals(2, tabularData.size());
    Assert.assertEquals(Long.valueOf(1), tabularData.get(new Object[] {"one"}).get("value"));
    Assert.assertEquals(Long.valueOf(2), tabularData.get(new Object[] {"two"}).get("value"));
  }