11package com .aerospike .mapper .example ;
22
3- import java .util .Date ;
4- import java .util .concurrent .TimeUnit ;
3+ import static org .junit .Assert .assertEquals ;
54
65import org .junit .Test ;
76
1211import com .aerospike .client .policy .Policy ;
1312import com .aerospike .client .policy .Replica ;
1413import com .aerospike .client .policy .WritePolicy ;
15- import com .aerospike .mapper .example .model .Account ;
1614import com .aerospike .mapper .example .model .Address ;
1715import com .aerospike .mapper .example .model .Branch ;
1816import com .aerospike .mapper .example .model .Checkbook ;
1917import com .aerospike .mapper .example .model .Customer ;
18+ import com .aerospike .mapper .example .model .Property ;
2019import com .aerospike .mapper .example .model .Transaction ;
20+ import com .aerospike .mapper .example .model .accounts .Account ;
21+ import com .aerospike .mapper .example .model .accounts .LoanAccount ;
22+ import com .aerospike .mapper .example .model .accounts .PortfolioAccount ;
2123import com .aerospike .mapper .tools .AeroMapper ;
24+ import com .fasterxml .jackson .core .JsonProcessingException ;
25+ import com .fasterxml .jackson .databind .ObjectMapper ;
26+ import com .fasterxml .jackson .databind .ObjectWriter ;
2227
23- public class Application {
28+ public class Application extends ApplicationBase {
2429
2530 @ Test
26- public void run () {
31+ public void run () throws JsonProcessingException {
2732 Policy readPolicy = new Policy ();
2833 readPolicy .maxRetries = 4 ;
2934 readPolicy .replica = Replica .SEQUENCE ;
@@ -46,56 +51,89 @@ public void run() {
4651
4752 AeroMapper mapper = new AeroMapper .Builder (client )
4853 .withWritePolicy (writePolicy ).forAll ()
49- .withReadPolicy (txnReadPolicy ).forAll ()
54+ .withReadPolicy (readPolicy ).forAll ()
5055 .withReadPolicy (txnReadPolicy ).forClasses (Transaction .class )
5156 .build ();
5257
53- Address mailingAddress = new Address ("773 Elm St" , "Apt 2" , "Grand Junction" , new char [] { 'C' , 'O' } , "83451" );
54- Address billingAddress = new Address ("123 Main St" , null , "Denver" , new char [] { 'C' , 'O' } , "80001" );
55- Address alternateAddress = new Address ("1 Park Road" , null , "Miami" , new char [] { 'F' , 'L' } , "98531" );
58+ Address mailingAddress = new Address ("773 Elm St" , "Apt 2" , "Grand Junction" , "CO" , "83451" );
59+ Address billingAddress = new Address ("123 Main St" , null , "Denver" , "CO" , "80001" );
60+ Address alternateAddress = new Address ("1 Park Road" , null , "Miami" , "FL" , "98531" );
5661
57- Customer customer = new Customer ("cust1" , "Bob" , "Smith" );
58- customer .setDateOfBirth (new Date (new Date ().getTime () - TimeUnit .MILLISECONDS .convert (30 *365 , TimeUnit .DAYS )));
59- customer .setPhone ("(555)555-1234" );
62+ Customer customer = createAndPopulateCustomer ();
6063 customer .setMailingAddress (mailingAddress );
6164
62- Account testAccount = new Account ("ACC-1234" , customer .getCustomerId (), "Test Account" );
63- testAccount .setBalance (100000 );
64- testAccount .setCard (true );
65- testAccount .setBillingAddress (billingAddress );
66- testAccount .getAlternateAddresses ().add (mailingAddress );
67- testAccount .getAlternateAddresses ().add (alternateAddress );
68- testAccount .setRouting ("123456789" );
69- testAccount .setPaperless (true );
70- testAccount .setOverdraftProtection (false );
71- testAccount .setOnlineUserName ("beesmith" );
72- testAccount .setLastLogin (new Date ());
65+ /* Create a checking account for this customer */
66+ Account checkingAccount = createAndPopulateChecking (customer .getCustomerId ());
67+ checkingAccount .setBillingAddress (billingAddress );
68+ checkingAccount .getAlternateAddresses ().add (mailingAddress );
69+ checkingAccount .getAlternateAddresses ().add (alternateAddress );
7370
74- Branch issuingBranch = new Branch ("Br123" , new Address ("77 Park Road" , null , "Miami" , new char [] {'F' , 'L' }, "98531" ), "Miami Central" );
75- Checkbook checkbook = new Checkbook (testAccount .getAccountId (), 1 , 100 , new Date ());
76- checkbook .setIssuer (issuingBranch );
77- checkbook .setRecalled (false );
78- testAccount .getCheckbooks ().put (1 , checkbook );
71+ Branch issuingBranch = new Branch ("Br123" , new Address ("77 Park Road" , null , "Miami" , "FL" , "98531" ), "Miami Central" );
72+ Checkbook checkbook = createAndPopulateCheckbook1 (checkingAccount .getAccountId (), issuingBranch );
73+ checkingAccount .getCheckbooks ().put (1 , checkbook );
7974
80- Branch otherIssuingBranch = new Branch ("Br567" , new Address ("129 Bump Drive" , null , "New York" , new char [] {'N' , 'Y' }, "77777" ), "New York City Office" );
81- Checkbook checkbook2 = new Checkbook (testAccount .getAccountId (), 101 , 600 , new Date ());
82- checkbook2 .setIssuer (otherIssuingBranch );
83- checkbook2 .setRecalled (false );
84- testAccount .getCheckbooks ().put (2 , checkbook2 );
75+ Branch otherIssuingBranch = new Branch ("Br567" , new Address ("129 Bump Drive" , null , "New York" , "NY" , "77777" ), "New York City Office" );
76+ Checkbook checkbook2 = createAndPopulateCheckbook2 (checkingAccount .getAccountId (), otherIssuingBranch );
77+ checkingAccount .getCheckbooks ().put (2 , checkbook2 );
8578
86- customer .getAccounts ().add (testAccount );
79+ mapper .save (checkingAccount );
80+ customer .getAccounts ().add (checkingAccount );
81+
82+ /* Create a savings account for this customer */
83+ Account savingsAccount = createAndPopulateSavingsAccount (customer .getCustomerId ());
84+ savingsAccount .setBillingAddress (billingAddress );
85+ savingsAccount .getAlternateAddresses ().add (mailingAddress );
86+
87+ mapper .save (savingsAccount );
88+ customer .getAccounts ().add (savingsAccount );
89+
90+ /* Create a porfolio account for this customer */
91+ Property property1 = createAndPopulateProperty1 ();
92+ Property property2 = createAndPopulateProperty2 ();
93+ Property property3 = createAndPopulateProperty3 ();
94+ mapper .save (property1 );
95+ mapper .save (property2 );
96+ mapper .save (property3 );
97+
98+ PortfolioAccount portfolioAccount = createAndPopulatePortfolioAccount (customer .getCustomerId ());
99+ portfolioAccount .setBillingAddress (billingAddress );
100+ portfolioAccount .getAlternateAddresses ().add (mailingAddress );
101+ portfolioAccount .addPropertyToPortfolio (property1 , 0.0239f );
102+ portfolioAccount .addPropertyToPortfolio (property2 , 0.0299f );
103+ portfolioAccount .addPropertyToPortfolio (property3 , 0.0319f );
104+
105+ mapper .save (portfolioAccount );
106+ customer .getAccounts ().add (portfolioAccount );
107+
108+ /* Create a loan account for this customer */
87109
110+ LoanAccount loanAccount = createAndPopulateLoanAccount (customer .getCustomerId ());
111+ loanAccount .setBillingAddress (billingAddress );
112+ loanAccount .getAlternateAddresses ().add (mailingAddress );
113+ customer .getAccounts ().add (loanAccount );
114+
115+ Property securityProperty = createAndPopulateProperty4 ();
116+ loanAccount .setSecurityProperty (securityProperty );
117+
118+ mapper .save (securityProperty );
119+ mapper .save (loanAccount );
120+
88121 mapper .save (issuingBranch );
89122 mapper .save (otherIssuingBranch );
90- mapper .save (testAccount );
91123 mapper .save (customer );
92124 mapper .save (checkbook );
93125 mapper .save (checkbook2 );
94126
127+ Customer readCustomer = null ;
95128 for (int i = 0 ; i < 100 ; i ++) {
96- long now =System .nanoTime ();
97- Customer readCustomer = mapper .read (Customer .class , customer .getCustomerId ());
98- System .out .println (( System .nanoTime () - now )/1000 );
129+ long now = System .nanoTime ();
130+ readCustomer = mapper .read (Customer .class , customer .getCustomerId ());
131+ System .out .println (String . format ( "Customer graph read time: %.3fms" , ( System .nanoTime () - now )/1000000f ) );
99132 }
133+ ObjectWriter objectWriter = new ObjectMapper ().writerWithDefaultPrettyPrinter ();
134+ String readString = objectWriter .writeValueAsString (readCustomer );
135+ System .out .println (readString );
136+ String originalObject = objectWriter .writeValueAsString (customer );
137+ assertEquals (originalObject , readString );
100138 }
101139}
0 commit comments