john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

AppProperties TEST AuthgatewayServiceDAOTest

//2012-06-22 johnpfeiffer requires AuthgatewayService , it will overwrite the current AuthgatewayServiceDAO.AUTHGATEWAYAPPPROPERTIES
// TODO: javascript html form validation tests

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import org.junit.Before;
import org.junit.Test;


public class AuthgatewayServiceDAOTest
{
    private AuthgatewayServiceDAO testerDefault;

    private static final String DEFAULTSHAREDSECRET = "testsharedSECRET";
    private static final int DEFAULTEXPIRATION = 60;
    private static final String DEFAULTURL = "https://wgw.test/webauth/Servlet";

    private static final String MAPACTIVE = "false" ;
    private static final String MAPHOST = "maphost" ;
    private static final String MAPPORT =   "636" ;
    private static final String MAPBASEDN = "dc\\=test,dc\\=map" ;
    private static final String MAPUSERDN = "cn\\=admin,ou\\=service,dc\\=test,dc\\=map" ;
    private static final String MAPPASSWORD = "passwordMap" ;
    private static final String MAPSEARCHBASE = "ou\\=searchBaseMap" ;
    private static final String MAPLDAPATTRIBUTE = "uid" ;
    private static final String MAPSEARCHSUBTREE = "false" ;


    @Before public void setUp() throws InstantiationException, IOException
    {
        writeConfiguration( DEFAULTSHAREDSECRET );
        testerDefault = new AuthgatewayServiceDAO();
    }

    @Test public void testAuthgatewayServiceDAO() throws InstantiationException, IOException
    {
        writeConfiguration( "constructor" );
        AuthgatewayServiceDAO constructor = new AuthgatewayServiceDAO();
        assertEquals( "constructor"  , constructor.getPropertyValue( AuthgatewayServiceDAO.SHAREDSECRETIDENTIFIER + "=" ) );
    }



    @Test   public void testGetPropertyValue()
    {
        assertEquals( DEFAULTSHAREDSECRET , testerDefault.getPropertyValue( AuthgatewayServiceDAO.SHAREDSECRETIDENTIFIER + "=" ) ); //it should skip the extraneous # comment
        String expirationString = testerDefault.getPropertyValue( AuthgatewayServiceDAO.LOGINEXPIRATIONIDENTIFIER + "=" );
        assertEquals( DEFAULTEXPIRATION , Integer.parseInt( expirationString ) );
        assertEquals( DEFAULTURL , testerDefault.getPropertyValue( AuthgatewayServiceDAO.NEWTORKIDAUTHENTICATIONURLIDENTIFIER + "=" ) );
    }

    //TODO: test more of the config is written?
    @Test public void testWriteConfigurationToFile() throws InstantiationException , IOException
    {
        writeConfiguration( "testWrite" );
        AuthgatewayServiceDAO testWrite = new AuthgatewayServiceDAO();
        assertEquals( "testWrite"  , testWrite.getPropertyValue( AuthgatewayServiceDAO.SHAREDSECRETIDENTIFIER + "=" ) );
        testWrite.writeConfigurationToFile();
        assertEquals( "testWrite"  , testWrite.getPropertyValue( AuthgatewayServiceDAO.SHAREDSECRETIDENTIFIER + "=" ) );
    }

    //TODO: test more of the config is written?
    @Test public void testGetHTMLForm()
    {
        StringBuilder expected = new StringBuilder();
        String newline = System.getProperty( "line.separator" );
        expected.append( "<h2>Service 1</h2>" + newline );
//      expected.append( expectedAuthgatewayServiceDAOHTML(  ) );
//      assertEquals( expected.toString() , testerDefault.getHTMLForm() );
    }

    //TODO: how to test what's in the authgatewayservice except getHTML?
    @Test public void testSaveService() throws IOException
    {
        assertEquals( DEFAULTSHAREDSECRET , testerDefault.getPropertyValue( AuthgatewayServiceDAO.SHAREDSECRETIDENTIFIER + "=" ) );
        AuthgatewayServiceDAO testSaveService = new AuthgatewayServiceDAO();
        testSaveService.saveService( expectedAuthgatewayServiceDAOMap() );
    }

    private String expectedAuthgatewayServiceDAOString( String sharedSecret )
    {
        StringBuilder expected = new StringBuilder();
        String newline = System.getProperty( "line.separator" );
        String header = "authgateway.";
//      expected.append( "#" + header + AuthgatewayServiceDAO.SHAREDSECRETIDENTIFIER + "=" + sharedSecret + newline );      //to test commented out line
        expected.append( header + AuthgatewayServiceDAO.SHAREDSECRETIDENTIFIER + "=" + sharedSecret + newline );
        expected.append( header + AuthgatewayServiceDAO.LOGINEXPIRATIONIDENTIFIER + "=" + DEFAULTEXPIRATION + newline );
        expected.append( header + AuthgatewayServiceDAO.NEWTORKIDAUTHENTICATIONURLIDENTIFIER + "=" + DEFAULTURL + newline );
        expected.append( newline );

        for( int i=1; i<4; i++ )
        {
            header = "authgateway.service" + i + ".";
            expected.append( header + AuthgatewayService.PARAMACTIVE + "=" + AuthgatewayServiceTest.DEFAULTACTIVE + newline );
            expected.append( header + AuthgatewayService.PARAMHOST + "=" + AuthgatewayServiceTest.DEFAULTHOST + newline );
            expected.append( header + AuthgatewayService.PARAMPORT + "=" + AuthgatewayServiceTest.DEFAULTPORT + newline );
            expected.append( header + AuthgatewayService.PARAMSSL + "=" + AuthgatewayServiceTest.DEFAULTSSL + newline );
            expected.append( header + AuthgatewayService.PARAMBASEDN + "=" + AuthgatewayServiceTest.DEFAULTBASEDN + newline );
            expected.append( header + AuthgatewayService.PARAMUSERDN + "=" + AuthgatewayServiceTest.DEFAULTUSERDN + newline );
            expected.append( header + AuthgatewayService.PARAMUSERDNPASSWORD + "=" + AuthgatewayServiceTest.DEFAULTUSERPASSWORD + newline );
            expected.append( header + AuthgatewayService.PARAMSEARCHBASE + "=" + AuthgatewayServiceTest.DEFAULTSEARCHBASE + newline );
            expected.append( header + AuthgatewayService.PARAMSEARCHBASE + "=" + AuthgatewayServiceTest.DEFAULTLDAPATTRIBUTE + newline );
            expected.append( header + AuthgatewayService.PARAMSEARCHSUBTREE + "=" + AuthgatewayServiceTest.DEFAULTSEARCHSUBTREE + newline );
            expected.append( newline );
        }

        return expected.toString();
    }

    private HashMap <String , String> expectedAuthgatewayServiceDAOMap()
    {
        HashMap <String , String> parameterMap = new HashMap <String , String> ();

        parameterMap.put( AuthgatewayService.PARAMACTIVE , MAPACTIVE );
        parameterMap.put( AuthgatewayService.PARAMHOST , MAPHOST );
        parameterMap.put( AuthgatewayService.PARAMPORT , MAPPORT );
        parameterMap.put( AuthgatewayService.PARAMBASEDN , MAPBASEDN );
        parameterMap.put( AuthgatewayService.PARAMUSERDN , MAPUSERDN );
        parameterMap.put( AuthgatewayService.PARAMUSERDNPASSWORD , MAPPASSWORD );
        parameterMap.put( AuthgatewayService.PARAMSEARCHBASE , MAPSEARCHBASE );
        parameterMap.put( AuthgatewayService.PARAMLDAPUSERATTRIBUTE , MAPLDAPATTRIBUTE );
        parameterMap.put( AuthgatewayService.PARAMSEARCHSUBTREE , MAPSEARCHSUBTREE );
        return parameterMap;
    }

    private void writeConfiguration( String sharedSecret ) throws InstantiationException , IOException
    {
        File f = new File( AuthgatewayServiceDAO.AUTHGATEWAYAPPPROPERTIES );
        if( !f.exists() || !f.canWrite() )
        {       throw new InstantiationException( "ERROR: Test prerequisite (exists and writable) is missing: " + AuthgatewayServiceDAO.AUTHGATEWAYAPPPROPERTIES );
        }
        PrintWriter out;
        out = new PrintWriter( AuthgatewayServiceDAO.AUTHGATEWAYAPPPROPERTIES );
        out.print( expectedAuthgatewayServiceDAOString( sharedSecret ) );
        out.close();
    }

} //end class

  • « AppProperties TEST CIFSServiceDAOTest
  • javascript validate html form »

Published

Jun 26, 2012

Category

java-servlet

~436 words

Tags

  • appproperties 18
  • authgatewayservicedaotest 1
  • java-servlet 61
  • test 29