package coyote.loader.cfg;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

//import static org.junit.Assert.*;
import org.junit.Test;

import coyote.commons.GUID;
import coyote.dataframe.marshal.JSONMarshaler;


public class ConfigTest {

  @Test
  public void test() {
    Config config = new Config();
    config.setName( "Loader" );
    config.setClassName( coyote.loader.DefaultLoader.class.getName() );
    config.setId( GUID.randomGUID().toString() );

    // The configuration section of all the components this loader is to load
    Config componentCfg = new Config();

    // Create a configuration for component 1
    Config cfg = new Config();
    cfg.setName( "Component1" );
    cfg.setClassName( coyote.loader.SimpleComponent.class.getName() );
    cfg.setId( "F56A" );
    componentCfg.add( cfg );

    cfg = new Config();
    cfg.setName( "Component2" );
    cfg.setClassName( coyote.loader.SimpleComponent.class.getName() );
    cfg.setId( "DB46" );
    componentCfg.add( cfg );

    cfg = new Config();
    cfg.setName( "Component3" );
    cfg.setClassName( coyote.loader.SimpleComponent.class.getName() );
    cfg.setId( "8A0A" );
    componentCfg.add( cfg );

    // Add the component configuration to the main Loader config
    config.add( "Components", componentCfg );

    // This is a standard configuration for a loader
    System.out.println( JSONMarshaler.toFormattedString( config ) );

  }




  @Test
  public void testGetInt() {

    Config cfg = new Config();
    cfg.set( "port", "123" );

    try {
      int value = cfg.getInt( "PORT" );
      assertTrue( value == 123 );
    } catch ( NumberFormatException e ) {
      fail( "Could not retrieve as an integer" );
    }

    cfg.set( "fail", null );
    try {
      int value = cfg.getInt( "FAIL" );
      fail( "Should have thrown an exception" );

      value = cfg.getInt( "NotThere" );
      fail( "Should have thrown an exception - null value" );
    } catch ( NumberFormatException e ) {}

    try {
      int value = cfg.getInt( "NotThere" );
      fail( "Should have thrown an exception - not found" );
    } catch ( NumberFormatException e ) {}

  }
}

@@ -62,49 +62,181 @@
 sert
-True;%0Aimport static org.junit.Assert.fail
+NotNull;%0Aimport static org.junit.Assert.assertTrue;%0Aimport static org.junit.Assert.fail;%0A%0Aimport java.io.IOException;%0Aimport java.net.URI;%0Aimport java.net.URISyntaxException
 ;%0A%0A/
@@ -612,24 +612,53 @@
 oString() );
+%0A    config.setName( %22Bob%22 );
 %0A%0A    // The
@@ -1459,17 +1459,16 @@
 omponent
-s
 %22, compo
@@ -2331,10 +2331,334 @@
 %7B%7D%0A%0A  %7D%0A
+%0A%0A%0A%0A  @Test%0A  public void readFromNetwork() throws URISyntaxException, IOException, ConfigurationException %7B%0A    URI cfgUri = new URI( %22https://coyote.systems/device/ou812%22 );%0A    Config config = Config.read( cfgUri );%0A    assertNotNull( config );%0A    //System.out.println( JSONMarshaler.toFormattedString( config ) );%0A  %7D%0A%0A
 %7D%0A

Make sure network loads work