1+ // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License"). You may
4+ // not use this file except in compliance with the License. A copy of the
5+ // License is located at
6+ //
7+ // http://aws.amazon.com/apache2.0/
8+ //
9+ // or in the "license" file accompanying this file. This file is distributed
10+ // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+ // express or implied. See the License for the specific language governing
12+ // permissions and limitations under the License.
13+
14+ package code_test
15+
16+ import (
17+ "testing"
18+
19+ "github.com/aws-controllers-k8s/code-generator/pkg/generate/code"
20+ "github.com/aws-controllers-k8s/code-generator/pkg/testutil"
21+ "github.com/stretchr/testify/assert"
22+ "github.com/stretchr/testify/require"
23+ )
24+
25+ func Test_ReferenceFieldsValidation_NoReferenceConfig (t * testing.T ) {
26+ assert := assert .New (t )
27+ require := require .New (t )
28+
29+ g := testutil .NewModelForServiceWithOptions (t , "apigatewayv2" ,
30+ & testutil.TestingModelOptions {
31+ GeneratorConfigFile : "generator-with-reference.yaml" ,
32+ })
33+
34+ crd := testutil .GetCRDByName (t , g , "Api" )
35+ require .NotNil (crd )
36+ expected := ""
37+ assert .Equal (expected , code .ReferenceFieldsValidation (crd , "ko" , "Ref" , 1 ))
38+ }
39+
40+ func Test_ReferenceFieldsValidation_SingleReference (t * testing.T ) {
41+ assert := assert .New (t )
42+ require := require .New (t )
43+
44+ g := testutil .NewModelForServiceWithOptions (t , "apigatewayv2" ,
45+ & testutil.TestingModelOptions {
46+ GeneratorConfigFile : "generator-with-reference.yaml" ,
47+ })
48+
49+ crd := testutil .GetCRDByName (t , g , "Integration" )
50+ require .NotNil (crd )
51+ expected :=
52+ ` if ko.Spec.APIIDRef != nil && ko.Spec.APIID != nil {
53+ return ackerr.ResourceReferenceAndIDNotSupportedFor("APIID", "APIIDRef")
54+ }
55+ if ko.Spec.APIIDRef == nil && ko.Spec.APIID == nil {
56+ return ackerr.ResourceReferenceOrIDRequiredFor("APIID", "APIIDRef")
57+ }
58+ `
59+ assert .Equal (expected , code .ReferenceFieldsValidation (crd , "ko" , "Ref" , 1 ))
60+ }
61+
62+ func Test_ReferenceFieldsValidation_SliceOfReferences (t * testing.T ) {
63+ assert := assert .New (t )
64+ require := require .New (t )
65+
66+ g := testutil .NewModelForServiceWithOptions (t , "apigatewayv2" ,
67+ & testutil.TestingModelOptions {
68+ GeneratorConfigFile : "generator-with-reference.yaml" ,
69+ })
70+
71+ //NOTE: For the moment, we are substituting SecurityGroupId with ApiId
72+ // just to test code generation for slices of reference
73+ crd := testutil .GetCRDByName (t , g , "VpcLink" )
74+ require .NotNil (crd )
75+ expected :=
76+ ` if ko.Spec.SecurityGroupIDsRef != nil && ko.Spec.SecurityGroupIDs != nil {
77+ return ackerr.ResourceReferenceAndIDNotSupportedFor("SecurityGroupIDs", "SecurityGroupIDsRef")
78+ }
79+ `
80+ assert .Equal (expected , code .ReferenceFieldsValidation (crd , "ko" , "Ref" , 1 ))
81+ }
82+
83+ func Test_ReferenceFieldsPresent_NoReferenceConfig (t * testing.T ) {
84+ assert := assert .New (t )
85+ require := require .New (t )
86+
87+ g := testutil .NewModelForServiceWithOptions (t , "apigatewayv2" ,
88+ & testutil.TestingModelOptions {
89+ GeneratorConfigFile : "generator-with-reference.yaml" ,
90+ })
91+
92+ crd := testutil .GetCRDByName (t , g , "Api" )
93+ require .NotNil (crd )
94+ expected := "false"
95+ assert .Equal (expected , code .ReferenceFieldsPresent (crd , "ko" ))
96+ }
97+
98+ func Test_ReferenceFieldsPresent_SingleReference (t * testing.T ) {
99+ assert := assert .New (t )
100+ require := require .New (t )
101+
102+ g := testutil .NewModelForServiceWithOptions (t , "apigatewayv2" ,
103+ & testutil.TestingModelOptions {
104+ GeneratorConfigFile : "generator-with-reference.yaml" ,
105+ })
106+
107+ crd := testutil .GetCRDByName (t , g , "Integration" )
108+ require .NotNil (crd )
109+ expected := "false || ko.Spec.APIIDRef != nil"
110+ assert .Equal (expected , code .ReferenceFieldsPresent (crd , "ko" ))
111+ }
112+
113+ func Test_ReferenceFieldsPresent_SliceOfReferences (t * testing.T ) {
114+ assert := assert .New (t )
115+ require := require .New (t )
116+
117+ g := testutil .NewModelForServiceWithOptions (t , "apigatewayv2" ,
118+ & testutil.TestingModelOptions {
119+ GeneratorConfigFile : "generator-with-reference.yaml" ,
120+ })
121+
122+ //NOTE: For the moment, we are substituting SecurityGroupId with ApiId
123+ // just to test code generation for slices of reference
124+ crd := testutil .GetCRDByName (t , g , "VpcLink" )
125+ require .NotNil (crd )
126+ expected := "false || ko.Spec.SecurityGroupIDsRef != nil"
127+ assert .Equal (expected , code .ReferenceFieldsPresent (crd , "ko" ))
128+ }
0 commit comments