1+ import { App , Stack } from 'aws-cdk-lib' ;
2+ import { Template } from 'aws-cdk-lib/assertions' ;
3+ import { Metric , MetricOptions } from 'aws-cdk-lib/aws-cloudwatch' ;
4+ import { Permission } from 'aws-cdk-lib/aws-lambda' ;
5+ import { Construct } from 'constructs' ;
6+ import { OpenAPI3 } from 'openapi-typescript' ;
7+ import { LambdaFunction , LambdaFunctionProps , RestApi } from '../../src/constructs' ;
8+ import { loadYaml } from '../../src/constructs/load-yaml' ;
9+
10+ jest . mock ( '../../src/constructs/load-yaml' ) ;
11+ jest . mock ( '../../src/constructs/func' ) ;
12+
13+
14+ describe ( 'A generated RestApi' , ( ) => {
15+
16+ describe ( 'when instantiated with monitoring active' , ( ) => {
17+ let stack : Stack ;
18+ beforeEach ( ( ) => {
19+ const app = new App ( ) ;
20+ stack = new Stack ( app ) ;
21+ } ) ;
22+
23+ test ( 'should create a Cloudwatch Dashboard' , ( ) => {
24+ interface Operations {
25+ testEndpoint : {
26+ responses : {
27+ 200 : { } ;
28+ } ;
29+ } ;
30+ }
31+
32+ interface Paths {
33+ '/test' : {
34+ get : Operations [ 'testEndpoint' ] ;
35+ } ;
36+ }
37+
38+ const buffer = {
39+ openapi : '3.0.1' ,
40+ paths : {
41+ '/test' : {
42+ get : {
43+ operationId : 'testEndpoint' ,
44+ responses : {
45+ 200 : {
46+ content : {
47+ 'application/json' : { } ,
48+ } ,
49+ description : '' ,
50+ summary : '' ,
51+ } ,
52+ } ,
53+ } ,
54+ } ,
55+ } ,
56+ info : {
57+ title : 'Existing API definition' ,
58+ version : '1.0' ,
59+ } ,
60+ } as OpenAPI3 ;
61+
62+ jest . mocked ( loadYaml ) . mockReturnValue ( buffer ) ;
63+ jest . mocked ( LambdaFunction ) . mockImplementation ( ( _scope : Construct , _id : string , _mockProps : LambdaFunctionProps ) => {
64+ return {
65+ metricDuration ( _props ?: MetricOptions ) : Metric {
66+ return new Metric ( {
67+ metricName : 'Duration' ,
68+ namespace : 'TestNamespace' ,
69+ } ) ;
70+ } ,
71+ metricInvocations ( _props ?: MetricOptions ) : Metric {
72+ return new Metric ( {
73+ metricName : 'Invocations' ,
74+ namespace : 'TestNamespace' ,
75+ } ) ;
76+ } ,
77+ metricErrors ( _props ?: MetricOptions ) : Metric {
78+ return new Metric ( {
79+ metricName : 'Errors' ,
80+ namespace : 'TestNamespace' ,
81+ } ) ;
82+ } ,
83+ metricThrottles ( _props ?: MetricOptions ) : Metric {
84+ return new Metric ( {
85+ metricName : 'Invocations' ,
86+ namespace : 'TestNamespace' ,
87+ } ) ;
88+ } ,
89+ addPermission ( __id : string , __permission : Permission ) {
90+ } ,
91+ } as LambdaFunction ;
92+ } ) ;
93+
94+ new RestApi < Paths , Operations > ( stack , 'TestSubject' , {
95+ monitoring : true ,
96+ apiName : 'TestSubjectApi' ,
97+ definitionFileName : 'someFile' ,
98+ cors : true ,
99+ stageName : 'test' ,
100+ } ) ;
101+
102+ const template = Template . fromStack ( stack ) ;
103+
104+ expect ( template . findResources ( 'AWS::CloudWatch::Dashboard' ) ) . toMatchSnapshot ( ) ;
105+
106+ } ) ;
107+ } ) ;
108+
109+ } ) ;
0 commit comments