-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
255 lines (189 loc) · 7.36 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"context"
"crypto/rand"
"fmt"
"log"
"math/big"
"os"
"path/filepath"
"time"
"github.com/senzing-garage/go-helpers/fileutil"
"github.com/senzing-garage/go-helpers/settings"
"github.com/senzing-garage/go-helpers/truthset"
"github.com/senzing-garage/go-logging/logging"
"github.com/senzing-garage/sz-sdk-go-core/szabstractfactory"
"github.com/senzing-garage/sz-sdk-go/senzing"
)
// ----------------------------------------------------------------------------
// Variables
// ----------------------------------------------------------------------------
var Messages = map[int]string{
1: "%s",
2: "WithInfo: %s",
2001: "Testing %s.",
2002: "Physical cores: %d.",
2003: "withInfo",
2004: "License",
2999: "Cannot retrieve last error message.",
}
var logger logging.Logging
// ----------------------------------------------------------------------------
// Main
// ----------------------------------------------------------------------------
func main() {
var err error
ctx := context.TODO()
// Create a directory for temporary files.
testDirectoryPath := getTestDirectoryPath()
err = os.RemoveAll(filepath.Clean(testDirectoryPath)) // Cleanup any previous test run.
failOnError(5001, err)
err = os.MkdirAll(filepath.Clean(testDirectoryPath), 0750) // Recreate the test target directory.
failOnError(5002, err)
// Setup dependencies.
databaseURL, err := setupDatabase()
failOnError(5003, err)
log.SetFlags(0)
logger, err = getLogger(ctx)
failOnError(5004, err)
// Create a SzAbstractFactory.
settings, err := getSettings(databaseURL)
failOnError(5005, err)
szAbstractFactory := &szabstractfactory.Szabstractfactory{
ConfigID: senzing.SzInitializeWithDefaultConfiguration,
InstanceName: "Example instance",
Settings: settings,
VerboseLogging: senzing.SzNoLogging,
}
// Demonstrate persisting a Senzing configuration to the Senzing repository.
err = demonstrateConfigFunctions(ctx, szAbstractFactory)
failOnError(5006, err)
// Demonstrate tests.
err = demonstrateSenzingFunctions(ctx, szAbstractFactory)
failOnError(5007, err)
err = szAbstractFactory.Destroy(ctx)
failOnError(5008, err)
fmt.Printf("\n-------------------------------------------------------------------------------\n\n")
}
// ----------------------------------------------------------------------------
// Demonstrations
// ----------------------------------------------------------------------------
func demonstrateAddRecord(ctx context.Context, szEngine senzing.SzEngine) (string, error) {
dataSourceCode := "TEST"
randomNumber, err := rand.Int(rand.Reader, big.NewInt(1000000000))
failOnError(5010, err)
recordID := randomNumber.String()
jsonData := fmt.Sprintf(
"%s%s%s",
`{"SOCIAL_HANDLE": "flavorh", "DATE_OF_BIRTH": "4/8/1983", "ADDR_STATE": "LA", "ADDR_POSTAL_CODE": "71232", "SSN_NUMBER": "053-39-3251", "ENTITY_TYPE": "TEST", "GENDER": "F", "srccode": "MDMPER", "CC_ACCOUNT_NUMBER": "5534202208773608", "RECORD_ID": "`,
recordID,
`", "DSRC_ACTION": "A", "ADDR_CITY": "Delhi", "DRIVERS_LICENSE_STATE": "DE", "PHONE_NUMBER": "225-671-0796", "NAME_LAST": "SEAMAN", "entityid": "284430058", "ADDR_LINE1": "772 Armstrong RD"}`)
var flags int64 = senzing.SzWithInfo
// Using SzEngine: Add record and return "withInfo".
return szEngine.AddRecord(ctx, dataSourceCode, recordID, jsonData, flags)
}
func demonstrateConfigFunctions(ctx context.Context, szAbstractFactory senzing.SzAbstractFactory) error {
now := time.Now()
// Create Senzing objects.
szConfig, err := szAbstractFactory.CreateConfig(ctx)
failOnError(5101, err)
szConfigManager, err := szAbstractFactory.CreateConfigManager(ctx)
failOnError(5102, err)
// Using SzConfig: Create a default configuration in memory.
configHandle, err := szConfig.CreateConfig(ctx)
failOnError(5103, err)
// Using SzConfig: Add data source to in-memory configuration.
for testDataSourceCode := range truthset.TruthsetDataSources {
_, err := szConfig.AddDataSource(ctx, configHandle, testDataSourceCode)
failOnError(5104, err)
}
// Using SzConfig: Persist configuration to a string.
configStr, err := szConfig.ExportConfig(ctx, configHandle)
failOnError(5105, err)
// Using SzConfigManager: Persist configuration string to database.
configComment := fmt.Sprintf("Created by main.go at %s", now.UTC())
configID, err := szConfigManager.AddConfig(ctx, configStr, configComment)
failOnError(5106, err)
// Using SzConfigManager: Set new configuration as the default.
err = szConfigManager.SetDefaultConfigID(ctx, configID)
failOnError(5107, err)
return nil
}
func demonstrateSenzingFunctions(ctx context.Context, szAbstractFactory senzing.SzAbstractFactory) error {
// Create Senzing objects.
szDiagnostic, err := szAbstractFactory.CreateDiagnostic(ctx)
failOnError(5201, err)
szEngine, err := szAbstractFactory.CreateEngine(ctx)
failOnError(5202, err)
szProduct, err := szAbstractFactory.CreateProduct(ctx)
failOnError(5203, err)
// Clean the repository.
err = szDiagnostic.PurgeRepository(ctx)
failOnError(5204, err)
// Using SzEngine: Add records with information returned.
withInfo, err := demonstrateAddRecord(ctx, szEngine)
failOnError(5205, err)
logger.Log(2003, withInfo)
// Using SzProduct: Show license metadata.
license, err := szProduct.GetLicense(ctx)
failOnError(5206, err)
logger.Log(2004, license)
// Using SzEngine: Purge repository again.
err = szDiagnostic.PurgeRepository(ctx)
failOnError(5207, err)
return nil
}
// ----------------------------------------------------------------------------
// Internal methods
// ----------------------------------------------------------------------------
func copyDatabase() (string, error) {
var result string
// Construct SQLite database URL.
testDirectoryPath := getTestDirectoryPath()
dbTargetPath, err := filepath.Abs(filepath.Join(testDirectoryPath, "G2C.db"))
if err != nil {
return result, fmt.Errorf("failed to make target database path (%s) absolute. Error: %w", dbTargetPath, err)
}
result = fmt.Sprintf("sqlite3://na:na@nowhere/%s", dbTargetPath)
// Copy template file to test directory.
databaseTemplatePath, err := filepath.Abs(getDatabaseTemplatePath())
if err != nil {
return result, fmt.Errorf("failed to obtain absolute path to database file (%s): %s", databaseTemplatePath, err.Error())
}
_, _, err = fileutil.CopyFile(databaseTemplatePath, testDirectoryPath, true) // Copy the SQLite database file.
if err != nil {
return result, fmt.Errorf("setup failed to copy template database (%v) to target path (%v): %w", databaseTemplatePath, testDirectoryPath, err)
}
return result, nil
}
func failOnError(msgID int, err error) {
if err != nil {
logger.Log(msgID, err)
panic(err.Error())
}
}
func getDatabaseTemplatePath() string {
return filepath.FromSlash("testdata/sqlite/G2C.db")
}
func getLogger(ctx context.Context) (logging.Logging, error) {
_ = ctx
return logging.NewSenzingLogger(9999, Messages)
}
func getSettings(databaseURL string) (string, error) {
configAttrMap := map[string]string{"databaseUrl": databaseURL}
result, err := settings.BuildSimpleSettingsUsingMap(configAttrMap)
if err != nil {
return result, err
}
return result, err
}
func getTestDirectoryPath() string {
return filepath.FromSlash("target/test/main")
}
func setupDatabase() (string, error) {
databaseURL, ok := os.LookupEnv("SENZING_TOOLS_DATABASE_URL")
if ok {
return databaseURL, nil
}
return copyDatabase()
}