Create and test a builder for a custom entity

About this task

It is possible to create a builder for a custom entity. For example, suppose that you want each PolicyCenter user to have an array of external credentials for automatic sign-on to linked external systems. To implement this requirement, you can create an array of ExtCredential on User, with each ExtCredential having the following parameters.

Parameter

Type

ExtSystem

Typekey

UserName

String

Password

String

After creating your custom entity and its builder class, you need to test it. To accomplish this, you need to do the following:

Task

Affected files

1. Create a custom ExtCredential array entity and extend the User entity to include it.

ExtCredential.eti

User.etx

2. Create an ExtCrendentialBuilder by extending the DataBuilder class and adding withXXX methods to it.

ExtCredentialBuilder.gs

3. Create a test class to exercise and test your new builder.

ExtCredentialBuilderTest.gs

To create a new array ExtCredential custom entity, do the following tasks, as shown in the procedure.

  • Add the ExtSystem typelist by using the Typelist editor in Guidewire Studio.
  • Define the ExtCredential array entity in the ExtCredential, by using the editor in Guidewire Studio.
  • Modify the array entity definition to include a foreign key to User in ExtCredential.
  • Add an array field to the User entity in User.etx.

Procedure

  1. Add the ExtSystem typelist.
    1. In Guidewire Studio, navigate to configuration > config > Extensions > Typelist.
    2. Right-click Typelist, and then click New > Typelist.
    3. In Name, type ExtSystem. Then, click OK.
    4. Add a few external system typecodes.
      Add SystemOne, SystemTwo, or similar items.
  2. Create the ExtCredential entity type.
    1. In Guidewire Studio, navigate to configuration > config > Extensions > Entity.
    2. Right-click Entity, and then click New > Entity.
    3. In Entity, type ExtCredential. Then, click OK.
    4. Set the exportable attribute to true.
    5. Set the platform attribute to true.
    6. Add a typekey with the following attributes:
      name
      ExtSystem
      typelist
      ExtSystem
      nullok
      false
      desc
      Type of external system
    7. Add a column with the following attributes:
      name
      UserName
      type
      shorttext
      nullok
      false
    8. Add a column with the following attributes:
      name
      Password
      type
      shorttext
      nullok
      false
    9. Add a foreignkey with the following attributes:
      name
      UserID
      fkentity
      User
      nullok
      false
      desc
      FK back to User
  3. Modify the User entity.
    1. In Guidewire Studio, in configuration > config > Extensions > Entity, double-click User.etx.
    2. Add an array with the following attributes:
      name
      ExtCredentialRetirable
      type
      ExtCredential
      desc
      An array of ExtCredential objects
      arrayfield
      UserID
      exportable
      false
  4. Create an ExtCredentialBuilder class that extends the base DataBuilder class. Place this class in its own package under configuration and in the gsrc folder.
    1. In Guidewire Studio, navigate to configuration > config > gsrc.
    2. Right-click gsrc, and then click New > Package.
    3. In Enter new package name, type a name for the new package. Then, click OK.
      Type AllMyClasses.
    4. Right-click AllMyClasses, and then click New > Gosu Class.
    5. In Name, type ExtCredentialBuilder. Then, click OK.
    6. In the editor, enter the following code:
      package AllMyClasses
      
      uses gw.api.databuilder.DataBuilder
      
      class ExtCredentialBuilder extends DataBuilder<ExtCredential, ExtCredentialBuilder>  {
      
        construct() {
          super(ExtCredential)
        }
        
        function withType (  type: typekey.ExtSystem ) : ExtCredentialBuilder {
          set(ExtCredential.TypeInfo.getProperty( "ExtSystem" ), type)
          return this
        }
      
        function withUserName( somename : String ) : ExtCredentialBuilder {
          set(ExtCredential.TypeInfo.getProperty( "UserName" ), somename)
          return this
        }
      
        function withPassword( password : String ) : ExtCredentialBuilder {
           set(ExtCredential.TypeInfo.getProperty( "Password" ), password)
           return this
        }
      }

      Notice the following about this code sample.

      • It includes a uses ... DataBuilder statement.
      • It extends the Databuilder class, setting the BuilderType parameter to ExtCredential and the BuilderEntity parameter to ExtCredentialBuilder.
      • It uses a constructor for the super class—DataBuilder—that requires the entity type to create.
      • It implements multiple withXXX methods that populate an ExtCredential array object with the passed in values.
  5. Create an ExtCredentialBuilderTest class that is a GUnit test that uses the ExtCredentialBuilder class to create test data. Place this class in its own package in the Tests folder.
    package MyTests
    
    uses AllMyClasses.ExtCredentialBuilder
    uses gw.transaction.Transaction
    
    @gw.testharness.ServerTest
    class ExtCredentialBuilderTest extends gw.testharness.TestBase {
      
      static var credential : ExtCredential
      construct() {
      }
      
      function beforeClass () {
        Transaction.runWithNewBundle( \ bundle -> { 
          credential = new ExtCredentialBuilder()
                  .withType(  "SystemOne" )
                  .withUserName( "Peter Rabbit" )
                  .withPassword( "carrots" ) 
                  .create( bundle ) 
          }
        )
      }
    
      function testUsername() {    
        assertEquals("User names do not match.", credential.UserName, "Peter Rabbit")
      }
    
      function testPassword() {    
        assertEquals("Passwords do not match.", credential.Password, "carrots")
      }
    }

    Notice the following about this code sample:

    • It includes the uses statements for both ExtCredentialBuilder and gw.transaction.Transaction.
    • It creates a static credential variable. As the code declares this variable outside of a method—as a class variable—it is available to all methods within the class. (GUnit maintains a single copy of this variable.) As you run a test, GUnit creates a single instance of the test class that each test method uses. Therefore, to preserve a variable value across multiple test methods, you must declare it as a static variable.
    • It uses a beforeClass method to create the ExtCredential test data. This method calls ExtCredentialBuilder as part of a transaction block, which creates and commits the bundle automatically. GUnit calls the beforeClass method before it instantiates the test class for the first time. Thereafter, the test class uses the test data created by the beforeClass method. It is important to understand that GUnit does not drop the database between execution of each test method within a test class. However, if you run multiple test classes together (for example, by running all the test classes in a package), GUnit resets the database between execution of each test class.
    • It defines several test methods, each of which starts with test, with each method including an assertXXX method to test the data.

Results

If you run the ExtCredentialBuilderTest class as defined, the GUnit tester displays green icons, indicating that the tests were successful.