1+ using System ;
2+ using System . Threading ;
3+ using System . Threading . Tasks ;
4+ using Amazon . DynamoDBv2 . DataModel ;
5+ using Amazon . StepFunctions ;
6+ using Amazon . StepFunctions . Model ;
7+ using NSubstitute ;
8+ using Xunit ;
9+ using Xunit . Abstractions ;
10+
11+ namespace Unicorn . Approvals . ApprovalsService . Tests ;
12+
13+ [ Collection ( "Sequential" ) ]
14+ public class PropertiesApprovalSyncFunctionTest
15+ {
16+ private readonly ITestOutputHelper _testOutputHelper ;
17+
18+ private const string Token = "AQDIAAAAKgAAAAMAAAAAAAAAAapnwdMR3Z7RAg3IavSq2hbHt+CZPIQYaakFO6Em9Ik00VsGcaznxo" +
19+ "tIUGB2t7kihvuu/ffeoF+Z4yg4dggTxtzVwvRJwUDQr33/s/LhJyvEfNS57PXCv/CYFssJZ+28FRCAYb" +
20+ "GekKaopYhaUlvq0taLGMaEfIbRBeLUmLHInDkPPDbwTA==N0LRrCP3bIFW1MPkMQC3kd35p8yflvpThH" +
21+ "Ceviqe3qeyKBX03+ziocuyvNHVpktMuECnHL3MN9a6BfpSM1KItYI+qdIC74ls83ALSjjOs8G8pOz4Ou" +
22+ "dcliLYAvmZPRQXvFaw6aSMLfJJ7xRpEFSBwj1zDzbadMLtfudG78pmZX1m75/idMU5gz0UPkC87bVQN6" +
23+ "Kyjl7obxAeUO4aoqGJeNz6WbJQtrsUhiQWVEH/AwjUAj9Q0DwRqRPqeWyrv4MIMKea/Xu9vhXbcS+zPWP" +
24+ "q8onN9fyAqoMNh64K6wSGWxtAbaaByKxNpQu7o9ho/Iu/ME0KOAqyUK6vcnXOpIwIoMAZiG34KF4UnQsD0" +
25+ "gIokQcGLbehMGRixvEJMDZIloLxkuH0jvpIvD5xokGxpHwiVMISi2XRJ92nnGmWTCLWqsqJlsg4We6snJp" +
26+ "0Akw2w1Nt41lgY8kWkjxHNWEHDIMjzx1zeWiVa9b9aDDcckb71ouknJCN4gbxVs+yP30M97qnCEmMrc25" +
27+ "Yq7cEXLhu5Dh" ;
28+
29+ public PropertiesApprovalSyncFunctionTest ( ITestOutputHelper testOutputHelper )
30+ {
31+ _testOutputHelper = testOutputHelper ;
32+ // Set env variable for Powertools Metrics
33+ Environment . SetEnvironmentVariable ( "POWERTOOLS_METRICS_NAMESPACE" , "ContractService" ) ;
34+ }
35+
36+ [ Fact ]
37+ public void StatusIsDraftSyncShouldNotSendTaskSuccess ( )
38+ {
39+ // Setup
40+ var ddbEvent = TestHelpers . LoadDynamoDbEventSource ( "./events/StreamEvents/contract_status_changed_draft.json" ) ;
41+
42+ var mockDynamoDbContext = Substitute . For < IDynamoDBContext > ( ) ;
43+
44+ mockDynamoDbContext . LoadAsync < ContractStatusItem > ( Arg . Any < string > ( ) , Arg . Is ( CancellationToken . None ) )
45+ . Returns ( new ContractStatusItem
46+ {
47+ PropertyId = "usa/anytown/main-street/999" ,
48+ ContractId = Guid . NewGuid ( ) ,
49+ ContractStatus = "DRAFT" ,
50+ ContractLastModifiedOn = DateTime . Today ,
51+ SfnWaitApprovedTaskToken = null
52+ } ) ;
53+
54+ var mockStepFunctionsClient = Substitute . ForPartsOf < AmazonStepFunctionsClient > ( ) ;
55+ mockStepFunctionsClient . Received ( 0 )
56+ . SendTaskSuccessAsync ( Arg . Any < SendTaskSuccessRequest > ( ) ,
57+ Arg . Any < CancellationToken > ( ) ) ;
58+
59+ var context = TestHelpers . NewLambdaContext ( ) ;
60+
61+ var function =
62+ new PropertiesApprovalSyncFunction ( mockStepFunctionsClient , mockDynamoDbContext ) ;
63+
64+ var handler = function . FunctionHandler ( ddbEvent , context ) ;
65+
66+
67+ }
68+
69+
70+ [ Fact ]
71+ public Task StatusIsApprovedNoTokenSyncShouldNotSendTaskSuccess ( )
72+ {
73+ var ddbEvent =
74+ TestHelpers . LoadDynamoDbEventSource ( "./events/StreamEvents/contract_status_changed_approved.json" ) ;
75+
76+ var mockDynamoDbContext = Substitute . For < IDynamoDBContext > ( ) ;
77+ mockDynamoDbContext . LoadAsync < ContractStatusItem > ( Arg . Any < string > ( ) , Arg . Any < CancellationToken > ( ) )
78+ . Returns ( new ContractStatusItem
79+ {
80+ PropertyId = "usa/anytown/main-street/999" ,
81+ ContractId = Guid . NewGuid ( ) ,
82+ ContractStatus = "APPROVED" ,
83+ ContractLastModifiedOn = DateTime . Today ,
84+ SfnWaitApprovedTaskToken = null
85+ } ) ;
86+
87+ var mockStepFunctionsClient = Substitute . ForPartsOf < AmazonStepFunctionsClient > ( ) ;
88+ mockStepFunctionsClient . Received ( 0 ) .
89+ SendTaskSuccessAsync ( Arg . Any < SendTaskSuccessRequest > ( ) ,
90+ Arg . Any < CancellationToken > ( ) ) ;
91+
92+ var context = TestHelpers . NewLambdaContext ( ) ;
93+
94+ var function =
95+ new PropertiesApprovalSyncFunction ( mockStepFunctionsClient , mockDynamoDbContext ) ;
96+
97+ function . FunctionHandler ( ddbEvent , context ) ;
98+
99+ mockStepFunctionsClient . Received ( 0 ) .
100+ SendTaskSuccessAsync ( Arg . Any < SendTaskSuccessRequest > ( ) ,
101+ Arg . Any < CancellationToken > ( ) ) ;
102+
103+ return Task . CompletedTask ;
104+ }
105+
106+
107+ [ Fact ]
108+ public Task StatusIsDraftWithTokenSyncShouldNotSendTaskSuccess ( )
109+ {
110+ var ddbEvent =
111+ TestHelpers . LoadDynamoDbEventSource (
112+ "./events/StreamEvents/contract_status_draft_waiting_for_approval.json" ) ;
113+
114+ var mockDynamoDbContext = Substitute . For < IDynamoDBContext > ( ) ;
115+
116+ mockDynamoDbContext . LoadAsync < ContractStatusItem > ( Arg . Any < string > ( ) , CancellationToken . None )
117+ . Returns ( new ContractStatusItem
118+ {
119+ PropertyId = "usa/anytown/main-street/999" ,
120+ ContractId = Guid . NewGuid ( ) ,
121+ ContractStatus = "DRAFT" ,
122+ ContractLastModifiedOn = DateTime . Today ,
123+ SfnWaitApprovedTaskToken = Token
124+ } ) ;
125+
126+ var mockStepFunctionsClient = Substitute . ForPartsOf < AmazonStepFunctionsClient > ( ) ;
127+
128+ var context = TestHelpers . NewLambdaContext ( ) ;
129+
130+ var function =
131+ new PropertiesApprovalSyncFunction ( mockStepFunctionsClient , mockDynamoDbContext ) ;
132+
133+ var handler = function . FunctionHandler ( ddbEvent , context ) ;
134+
135+ mockStepFunctionsClient . Received ( 0 ) .
136+ SendTaskSuccessAsync ( Arg . Any < SendTaskSuccessRequest > ( ) ,
137+ Arg . Any < CancellationToken > ( ) ) ;
138+
139+ return Task . CompletedTask ;
140+ }
141+
142+
143+ [ Fact ]
144+ public Task StatusIsApprovedWithTokenSyncShouldSendTaskSuccess ( )
145+ {
146+ var ddbEvent =
147+ TestHelpers . LoadDynamoDbEventSource (
148+ "./events/StreamEvents/contract_status_changed_approved_waiting_for_approval.json" ) ;
149+
150+ var mockStepFunctionsClient = Substitute . ForPartsOf < AmazonStepFunctionsClient > ( ) ;
151+ var mockDynamoDbContext = Substitute . For < IDynamoDBContext > ( ) ;
152+
153+ mockDynamoDbContext . LoadAsync < ContractStatusItem > ( Arg . Any < string > ( ) , Arg . Is ( CancellationToken . None ) )
154+ . Returns ( new ContractStatusItem
155+ {
156+ PropertyId = "usa/anytown/main-street/999" ,
157+ ContractId = Guid . NewGuid ( ) ,
158+ ContractStatus = "APPROVED" ,
159+ ContractLastModifiedOn = DateTime . Today ,
160+ SfnWaitApprovedTaskToken = Token
161+ } ) ;
162+ var context = TestHelpers . NewLambdaContext ( ) ;
163+
164+ var function =
165+ new PropertiesApprovalSyncFunction ( mockStepFunctionsClient , mockDynamoDbContext ) ;
166+ var handler = function . FunctionHandler ( ddbEvent , context ) ;
167+
168+ mockStepFunctionsClient . Received ( 1 ) .
169+ SendTaskSuccessAsync ( Arg . Any < SendTaskSuccessRequest > ( ) ,
170+ Arg . Any < CancellationToken > ( ) ) ;
171+
172+ return Task . CompletedTask ;
173+ }
174+ }
0 commit comments