-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (171 loc) · 5.58 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"context"
"form3-accountapi-client/account_type"
"form3-accountapi-client/country"
"form3-accountapi-client/currency"
"form3-accountapi-client/uuid"
"log"
"os"
"strconv"
"strings"
"time"
)
const (
envProtcol string = "API_PROTOCOL"
envHost string = "API_HOST"
envPort string = "API_PORT"
envVersion string = "API_VERSION"
fmtLineBreak string = "-------------------------------------------------------------"
)
func main() {
log.Println("Stefano Mantini 06/08/2020 Form3tech-oss/interview-accountapi")
log.Println(fmtLineBreak)
for {
runTest()
log.Print("\n\n\n")
time.Sleep(10 * time.Second)
}
}
func getEnv(key string) string {
value := strings.TrimSpace(os.Getenv(key))
if value == "" {
log.Fatalf("key %s not found", key)
}
return value
}
func getEnvInt(key string) int {
value := strings.TrimSpace(os.Getenv(key))
if value == "" {
log.Fatalf("key %s not found", key)
}
i, err := strconv.Atoi(value)
if err != nil {
log.Fatalf("key %s not integer", key)
}
return i
}
// Exemplary code demonstrating the client library for the AccountClient
func runTest() {
// Initialise an instance of the account account_client
accountClient, err := NewAccountClient(getEnv(envProtcol), getEnv(envHost), getEnvInt(envPort), getEnvInt(envVersion))
if err != nil {
log.Fatal(err)
}
// context passed for trace id headers, and timeouts etc TODO
ctx := context.Background()
// health check
log.Println("attempting health check")
err = accountClient.Health(ctx)
if err != nil {
log.Fatalf("have you started the backing services? %v", err)
}
log.Println("Healthcheck completed successfully")
log.Println(fmtLineBreak)
// create account setup data
attr := AccountAttributes{}
attr.
WithAlternativeBankAccountNames([]string{"some bank"}).
WithCountry(country.UnitedKingdomofGreatBritainandNorthernIrelandthe).
WithBankId("1234").
WithBic("GEBABEBB").
WithBankIdCode("BARC").
WithBaseCurrency(currency.PoundSterling).
WithCountry(country.UnitedKingdomofGreatBritainandNorthernIrelandthe)
a := Account{}
account := a.
WithAccountType(account_type.TypeAccount).
WithId(uuid.MustUUID(uuid.NewV4())).
WithCreatedOn(time.Now().AddDate(0, 0, -10)).
WithModifiedOn(time.Now()).
WithOrganisationId(uuid.MustUUID(uuid.NewV4())).
WithVersion(1).
WithAttributes(attr)
// create accounts
log.Printf("attempting to create account %s", account.Id)
accountCreated, err := accountClient.Create(ctx, account)
if err != nil {
log.Fatal(err)
}
log.Printf("account created with id %s as expected \n", accountCreated.Id)
log.Println(fmtLineBreak)
account.Id = uuid.MustUUID(uuid.NewV4())
log.Printf("attempting to create account 2 %s", account.Id)
acct2, err := accountClient.Create(ctx, account)
if err != nil {
log.Fatal(err)
}
log.Printf("account created with id %s as expected \n", acct2.Id)
log.Println(fmtLineBreak)
// duplicate account created
log.Printf("attempting to create duplicate account")
_, err = accountClient.Create(ctx, account)
if _, ok := err.(*ErrDuplicateAccount); ok {
log.Printf("duplicate account not created with id %s as expected\n", account.Id)
log.Println(fmtLineBreak)
}
// invalid/nil account passed
invalidAcct := Account{}
invalidAcct.WithId(uuid.MustUUID(uuid.NewV4()))
invalidAcct.WithAttributes(AccountAttributes{Bic: "123"})
log.Printf("attempting to create with an invalid account payload")
_, err = accountClient.Create(ctx, &invalidAcct)
if _, ok := err.(*ErrInvalidRequest); ok {
log.Printf("invalid account not created with id %s as expected\n", invalidAcct.Id)
log.Println(fmtLineBreak)
}
// fetch Account for non-existent
nonExistentAccount, err := uuid.FromStringV4("ad27e265-9605-4b4b-a0e5-3003ea9cc4de")
log.Printf("attempting to fetch with an inexistent account %s", nonExistentAccount)
if err != nil {
log.Fatalf("account-id is not valid")
}
_, err = accountClient.Fetch(ctx, nonExistentAccount)
if notFoundErr, ok := err.(*ErrAccountNotFound); ok {
log.Println(notFoundErr, "as expected")
log.Println(fmtLineBreak)
}
// Get Account for existent
log.Printf("attempting to fetch with an existing account %s", accountCreated.Id)
accountRetrieved, err := accountClient.Fetch(ctx, accountCreated.Id)
if err != nil {
log.Fatal(err)
}
log.Printf("account retrieved with id %s as expected \n", accountRetrieved.Id)
log.Println(fmtLineBreak)
// List all Accounts
log.Printf("attempting to list all accounts")
accountsRetrieved, err := accountClient.List(ctx, 100, 0)
if err != nil {
log.Fatal(err)
}
log.Printf("list retrieved %d accounts \n", len(accountsRetrieved))
log.Println(fmtLineBreak)
// simple limit/offset for paginated accounts
log.Printf("attempting to list account subset of all %d accounts, with limit %d and offset %d", len(accountsRetrieved), 1, 1)
paginated, err := accountClient.List(ctx, 1, 1)
if err != nil {
log.Fatal(err)
}
if len(paginated) == 1 {
log.Printf("paginated %d accounts to single record successfully", len(accountsRetrieved))
log.Println(fmtLineBreak)
}
// delete account that doesn't exist
err = accountClient.Delete(ctx, uuid.MustUUID(uuid.NewV4()), 1)
if _, ok := err.(*ErrAccountNotFound); ok {
log.Printf("account not deleted successfully %s", err)
log.Println(fmtLineBreak)
}
// delete found accounts
log.Printf("Deleting all returned accounts %d", len(accountsRetrieved))
for _, acct := range accountsRetrieved {
// delete account that exists
err = accountClient.Delete(ctx, acct.Id, acct.Version)
if err != nil {
log.Fatal(err)
}
log.Printf("account deleted successfully %s", acct.Id)
log.Println(fmtLineBreak)
}
}