// 2012-08-29 johnpfeiffer requires outbound internet connection for InetAddress.getByName(String)
// TODO: javascript html form validation tests
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 EXPECTEDEFAULTHEADER = "";
@Before
public void setUp() throws Exception
{
defaultTester = new AtmosService.Builder().build();
}
@Test
public void testAtmosService()
{
final String expected = expectedAtmosServiceString( EXPECTEDEFAULTHEADER , AtmosService.DEFAULTATMOSISAVAILABLE , AtmosService.DEFAULTATMOSIPADDRESS ,
AtmosService.DEFAULTATMOSPORT , AtmosService.DEFAULTATMOSUID , AtmosService.DEFAULTATMOSSHAREDSECRET );
assertEquals( expected , defaultTester.getString( "" ) );
String expectedHTML = expectedAtmosHTMLForm( AtmosService.DEFAULTATMOSIPADDRESS , AtmosService.DEFAULTATMOSPORT , AtmosService.DEFAULTATMOSUID ,
AtmosService.DEFAULTATMOSSHAREDSECRET );
assertEquals( expectedHTML , defaultTester.getHTMLForm() );
}
@Test
public void testAtmosServiceIsAvailableTrue()
{
final boolean constructorIsAvailable = true;
AtmosService isAvailableTrueTest = new AtmosService.Builder().isAvailable( constructorIsAvailable ).build();
final String expected = expectedAtmosServiceString( EXPECTEDEFAULTHEADER , constructorIsAvailable , AtmosService.DEFAULTATMOSIPADDRESS ,
AtmosService.DEFAULTATMOSPORT , AtmosService.DEFAULTATMOSUID , AtmosService.DEFAULTATMOSSHAREDSECRET );
assertEquals( expected , isAvailableTrueTest.getString( EXPECTEDEFAULTHEADER ) );
}
@Test
public void testAtmosServiceIsAvailableFalse()
{
final boolean constructorIsAvailable = false;
AtmosService isAvailableFalseTest = new AtmosService.Builder().isAvailable( constructorIsAvailable ).build();
final String expected = expectedAtmosServiceString( EXPECTEDEFAULTHEADER , constructorIsAvailable , AtmosService.DEFAULTATMOSIPADDRESS ,
AtmosService.DEFAULTATMOSPORT , AtmosService.DEFAULTATMOSUID , AtmosService.DEFAULTATMOSSHAREDSECRET );
assertEquals( expected , isAvailableFalseTest.getString( EXPECTEDEFAULTHEADER ) );
}
@Test
public void testAtmosServiceHostname()
{
final String constructorHostname = "storage.synaptic.att.com";
AtmosService hostnameTest = new AtmosService.Builder().hostname( constructorHostname ).build();
final String expected = expectedAtmosServiceString( EXPECTEDEFAULTHEADER , AtmosService.DEFAULTATMOSISAVAILABLE , constructorHostname ,
AtmosService.DEFAULTATMOSPORT , AtmosService.DEFAULTATMOSUID , AtmosService.DEFAULTATMOSSHAREDSECRET );
assertEquals( expected , hostnameTest.getString( EXPECTEDEFAULTHEADER ) );
}
@Test
public void testAtmosServiceHostnameNull()
{
try
{
new AtmosService.Builder().hostname( null ).build();
}catch( IllegalArgumentException iae )
{
assertEquals( "ERROR: Atmos hostname cannot be null" , iae.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testAtmosServiceHostnameEmpty()
{
try
{
new AtmosService.Builder().hostname( "" ).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 testAtmosServicePort()
{
final int constructorPort = 80;
AtmosService portTest = new AtmosService.Builder().port( constructorPort ).build();
final String expected = expectedAtmosServiceString( EXPECTEDEFAULTHEADER , AtmosService.DEFAULTATMOSISAVAILABLE , AtmosService.DEFAULTATMOSIPADDRESS ,
constructorPort , AtmosService.DEFAULTATMOSUID , AtmosService.DEFAULTATMOSSHAREDSECRET );
assertEquals( expected , portTest.getString( EXPECTEDEFAULTHEADER ) );
}
@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 testAtmosServiceUID()
{
final String constructorUid = "constructor/UID";
AtmosService uidTest = new AtmosService.Builder().uid( constructorUid ).build();
final String expected = expectedAtmosServiceString( EXPECTEDEFAULTHEADER , AtmosService.DEFAULTATMOSISAVAILABLE , AtmosService.DEFAULTATMOSIPADDRESS ,
AtmosService.DEFAULTATMOSPORT , constructorUid , AtmosService.DEFAULTATMOSSHAREDSECRET );
assertEquals( expected , uidTest.getString( EXPECTEDEFAULTHEADER ) );
}
@Test
public void testAtmosServiceUIDNull()
{
try
{
new AtmosService.Builder().uid( null ).build();
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testAtmosServiceSharedSecret()
{
final String constructorSharedSecret = "constructorSecret=";
AtmosService sharedSecretTest = new AtmosService.Builder().sharedSecret( constructorSharedSecret ).build();
final String expected = expectedAtmosServiceString( EXPECTEDEFAULTHEADER , AtmosService.DEFAULTATMOSISAVAILABLE , AtmosService.DEFAULTATMOSIPADDRESS , AtmosService.DEFAULTATMOSPORT ,
AtmosService.DEFAULTATMOSUID , constructorSharedSecret );
assertEquals( expected , sharedSecretTest.getString( EXPECTEDEFAULTHEADER ) );
}
@Test
public void testAtmosServiceSharedSecretNull()
{
try
{
new AtmosService.Builder().sharedSecret( null ).build();
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testAtmosServiceSharedSecretEndsWithEqualsSymbol()
{
try
{
new AtmosService.Builder().sharedSecret( "sharedsecretShouldEndWithEqualsSymbol" ).build();
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testGetString()
{
final String expected = expectedAtmosServiceString( EXPECTEDEFAULTHEADER , AtmosService.DEFAULTATMOSISAVAILABLE , AtmosService.DEFAULTATMOSIPADDRESS , AtmosService.DEFAULTATMOSPORT ,
AtmosService.DEFAULTATMOSUID , AtmosService.DEFAULTATMOSSHAREDSECRET );
assertEquals( expected , defaultTester.getString( "" ) );
}
@Test
public void testGetHTMLForm()
{
final String expectedHTML = expectedAtmosHTMLForm( AtmosService.DEFAULTATMOSIPADDRESS , AtmosService.DEFAULTATMOSPORT , AtmosService.DEFAULTATMOSUID ,
AtmosService.DEFAULTATMOSSHAREDSECRET );
assertEquals( expectedHTML , defaultTester.getHTMLForm() );
}
@Test
public void getValidateFormOnSubmit()
{
String expectedJavascript = expectedValidateFormOnSubmit();
assertEquals( expectedJavascript , AtmosService.getValidateFormOnSubmit() );
}
private String expectedAtmosServiceString( String header , boolean isAvailable , String emcIpAddress , int emcPortNumber , String emcUid ,
String emcSharedSecret )
{
StringBuilder strb = new StringBuilder();
String newline = System.getProperty( "line.separator" );
strb.append( header + AtmosService.PARAMATMOSISAVALAIBLE + "=" + isAvailable + newline );
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.DEFAULTATMOSFOLDER + newline );
return strb.toString();
}
protected 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' id='" + AtmosService.PARAMEMCIPADDRESS + "' name='"
+ AtmosService.PARAMEMCIPADDRESS + "' size='40' value='" + JohnStringUtils.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' id='" + AtmosService.PARAMEMCUID + "' name='"
+ AtmosService.PARAMEMCUID + "' size='60' value='" + JohnStringUtils.safeHTML( emcUid ) + "' /> " + newline );
strb.append( "<label>Atmos Shared Secret:</label>" + space + space + "<input type='text' id='" + AtmosService.PARAMEMCSHAREDSECRET + "' name='"
+ AtmosService.PARAMEMCSHAREDSECRET + "' size='40' value='" + JohnStringUtils.safeHTML( emcSharedSecret ) + "' /> " + newline );
return strb.toString();
}
// TODO: more generic validation from the Data Model
protected static String expectedValidateFormOnSubmit()
{
StringBuilder strb = new StringBuilder();
String newline = System.getProperty( "line.separator" );
strb.append( newline + "<script type='text/javascript'>" + newline );
strb.append( "function validateFormOnSubmit() {" + newline );
strb.append( "var userinput=document.getElementById( '" + AtmosService.PARAMEMCIPADDRESS + "' ).value;" + newline );
strb.append( "if( userinput==null || userinput=='' )" + newline );
strb.append( "{ alert('Error: " + AtmosService.PARAMEMCIPADDRESS + " cannot be blank');" + newline );
strb.append( " return false; }" + newline );
strb.append( "var userinput=document.getElementById( '" + AtmosService.PARAMEMCUID + "' ).value;" + newline );
strb.append( "if( userinput==null || userinput=='' )" + newline );
strb.append( "{ alert('Error: " + AtmosService.PARAMEMCUID + " cannot be blank');" + newline );
strb.append( " return false; }" + newline );
strb.append( "var userinput=document.getElementById( '" + AtmosService.PARAMEMCSHAREDSECRET + "' ).value;" + newline );
strb.append( "if( userinput==null || userinput=='' )" + newline );
strb.append( "{ alert('Error: " + AtmosService.PARAMEMCSHAREDSECRET + " cannot be blank');" + newline );
strb.append( " return false; }" + newline );
strb.append( "}" + newline );
strb.append( "</script>" + newline );
return strb.toString();
}
} // end class