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 |
|---|---|
|
Typekey |
|
String |
|
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.eti User.etx |
|
2. Create an |
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
ExtSystemtypelist by using the Typelist editor in Guidewire Studio. - Define the
ExtCredentialarray entity in theExtCredential, by using the editor in Guidewire Studio. - Modify the array entity definition to include a foreign key to
UserinExtCredential. - Add an array field to the User entity in User.etx.
Procedure
-
Add the
ExtSystemtypelist.- In Guidewire Studio, navigate to .
- Right-click Typelist, and then click .
- In Name, type ExtSystem. Then, click OK.
-
Add a few external system typecodes.
Add SystemOne, SystemTwo, or similar items.
-
Create the
ExtCredentialentity type.- In Guidewire Studio, navigate to .
- Right-click Entity, and then click .
- In Entity, type ExtCredential. Then, click OK.
-
Set the
exportableattribute to true. -
Set the
platformattribute to true. -
Add a typekey with the following attributes:
- name
- ExtSystem
- typelist
- ExtSystem
- nullok
- false
- desc
- Type of external system
-
Add a column with the following attributes:
- name
- UserName
- type
- shorttext
- nullok
- false
-
Add a column with the following attributes:
- name
- Password
- type
- shorttext
- nullok
- false
-
Add a
foreignkeywith the following attributes:- name
- UserID
- fkentity
- User
- nullok
- false
- desc
- FK back to User
-
Modify the User entity.
- In Guidewire Studio, in , double-click User.etx.
-
Add an array with the following attributes:
- name
- ExtCredentialRetirable
- type
- ExtCredential
- desc
- An array of ExtCredential objects
- arrayfield
- UserID
- exportable
- false
-
Create an ExtCredentialBuilder class that extends the base
DataBuilder class. Place this class in its own package under configuration and in the gsrc folder.
- In Guidewire Studio, navigate to .
- Right-click gsrc, and then click .
-
In Enter new package name, type a name for the new package. Then, click
OK.
Type AllMyClasses.
- Right-click AllMyClasses, and then click .
- In Name, type ExtCredentialBuilder. Then, click OK.
-
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 ... DataBuilderstatement. - It extends the Databuilder class, setting the BuilderType
parameter to
ExtCredentialand the BuilderEntity parameter toExtCredentialBuilder. - It uses a constructor for the super class—
DataBuilder—that requires the entity type to create. - It implements multiple withXXX methods that populate an
ExtCredentialarray object with the passed in values.
- It includes a
-
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
usesstatements for bothExtCredentialBuilderandgw.transaction.Transaction. - It creates a static
credentialvariable. 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
ExtCredentialtest data. This method callsExtCredentialBuilderas 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.
- It includes the
Results
If you run the ExtCredentialBuilderTest class as defined, the GUnit tester displays green icons, indicating that the tests were successful.
