// 2012-06-19 johnpfeiffer requires hibernate-validator-4.3 , jboss-logging, validation-api , outbound internet connection for InetAddress.getByName(String)
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
public class AtmosServiceTest
{
private AtmosService defaultTester;
private static final String constructorHostname = "storage.synaptic.att.com";
private static final int constructorPort = 80;
private static final String constructorUid = "constructor/UID";
private static final String constructorSharedSecret = "constructorSecret=";
@Before
public void setUp() throws Exception
{
defaultTester = new AtmosService.Builder().build();
}
@Test
public void testAtmosService()
{
String expected = expectedAtmosServiceString( "" , AtmosService.DEFAULTEMCIPADDRESS , AtmosService.DEFAULTEMCPORT , AtmosService.DEFAULTUID ,
AtmosService.DEFAULTSHAREDSECRET );
assertEquals( expected , defaultTester.getString( "" ) );
String expectedHTML = expectedAtmosHTMLForm( AtmosService.DEFAULTEMCIPADDRESS , AtmosService.DEFAULTEMCPORT , AtmosService.DEFAULTUID ,
AtmosService.DEFAULTSHAREDSECRET );
assertEquals( expectedHTML , defaultTester.getHTMLForm() );
}
@Test
public void testAtmosServiceStringIntStringString()
{
AtmosService constructor = new AtmosService.Builder().hostname( constructorHostname ).port( constructorPort ).uid( constructorUid )
.sharedSecret( constructorSharedSecret ).build();
assertEquals( expectedAtmosServiceString( "" , constructorHostname , constructorPort , constructorUid , constructorSharedSecret ) ,
constructor.getString( "" ) );
assertEquals( expectedAtmosHTMLForm( constructorHostname , constructorPort , constructorUid , constructorSharedSecret ) , constructor.getHTMLForm() );
}
@Test
public void testAtmosServiceHostnameNull()
{
try
{
new AtmosService.Builder().hostname( null ).build();
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testAtmosServiceHostnameEmpty()
{
try
{
new AtmosService.Builder().hostname( "" ).build();
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testAtmosServicePortNumberNegative()
{
try
{
new AtmosService.Builder().port( -1 ).build();
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testAtmosServicePortNumberOutOfRange()
{
try
{
new AtmosService.Builder().port( 65536 ).build();
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testAtmosServiceUIDNull()
{
try
{
new AtmosService.Builder().uid( null ).build();
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testAtmosServiceSharedSecretNull()
{
try
{
new AtmosService.Builder().sharedSecret( null ).build();
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testAtmosServiceHostnameInvalid()
{
try
{
new AtmosService.Builder().hostname( "not a dns name" ).build();
}catch( IllegalArgumentException iae )
{
return;
}
fail( "url Expected IllegalArgumentException" );
}
@Test
public void testAtmosServiceSharedSecretEndsWithEqualsSymbol()
{
try
{
new AtmosService.Builder().sharedSecret( "sharedsecretShouldEndWithEqualsSymbol" ).build();
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testGetString()
{
assertEquals(
expectedAtmosServiceString( "" , AtmosService.DEFAULTEMCIPADDRESS , AtmosService.DEFAULTEMCPORT , AtmosService.DEFAULTUID ,
AtmosService.DEFAULTSHAREDSECRET ) , defaultTester.getString( "" ) );
}
@Test
public void testGetHTMLForm()
{
assertEquals(
expectedAtmosHTMLForm( AtmosService.DEFAULTEMCIPADDRESS , AtmosService.DEFAULTEMCPORT , AtmosService.DEFAULTUID , AtmosService.DEFAULTSHAREDSECRET ) ,
defaultTester.getHTMLForm() );
}
private String expectedAtmosServiceString( String header , String emcIpAddress , int emcPortNumber , String emcUid , String emcSharedSecret )
{
StringBuilder strb = new StringBuilder();
String newline = System.getProperty( "line.separator" );
strb.append( header + AtmosService.PARAMEMCIPADDRESS + "=" + emcIpAddress + newline );
strb.append( header + AtmosService.PARAMEMCPORT + "=" + emcPortNumber + newline );
strb.append( header + AtmosService.PARAMEMCUID + "=" + emcUid + newline );
strb.append( header + AtmosService.PARAMEMCSHAREDSECRET + "=" + emcSharedSecret + newline );
strb.append( header + AtmosService.PARAMEMCSTORAGEFOLDER + "=" + AtmosService.DEFAULTEMCFOLDER + newline );
return strb.toString();
}
private String expectedAtmosHTMLForm( String emcIpAddress , int emcPortNumber , String emcUid , String emcSharedSecret )
{
StringBuilder strb = new StringBuilder();
String newline = "<br /><br />" + System.getProperty( "line.separator" );
String space = " ";
strb.append( "<label>Atmos Hostname or IP Address:</label>" + space + space + "<input type='text' name='" + AtmosService.PARAMEMCIPADDRESS
+ "' size='40' value='" + StringUtils.safeHTML( emcIpAddress ) + "' /> " + newline );
strb.append( "<label>Port Number: </label> " );
strb.append( "<input type='radio' name='" + AtmosService.PARAMEMCPORT + "' value='80' " );
if( emcPortNumber == 80 )
{
strb.append( "checked='checked'" );
}
strb.append( " /> <label>80</label>" );
strb.append( space + space );
strb.append( "<input type='radio' name='" + AtmosService.PARAMEMCPORT + "' value='443' " );
if( emcPortNumber == 443 )
{
strb.append( "checked='checked'" );
}
strb.append( " /> <label> 443 </label> " + newline );
strb.append( "<label>Atmos Subtenant/UID:</label>" + space + space + "<input type='text' name='" + AtmosService.PARAMEMCUID + "' size='60' value='"
+ StringUtils.safeHTML( emcUid ) + "' /> " + newline );
strb.append( "<label>Atmos Shared Secret:</label>" + space + space + "<input type='text' name='" + AtmosService.PARAMEMCSHAREDSECRET
+ "' size='40' value='" + StringUtils.safeHTML( emcSharedSecret ) + "' /> " + newline );
return strb.toString();
}
} // end class