@@ -15,27 +15,47 @@ import (
1515 "go.uber.org/mock/gomock"
1616)
1717
18- func TestCreateAppCommand_Run (t * testing.T ) {
18+ func TestCreateAppCommand_Run_Flags (t * testing.T ) {
1919 ctrl := gomock .NewController (t )
2020 defer ctrl .Finish ()
2121
22- serverDetails := & config.ServerDetails {Url : "https://example.com" }
22+ ctx := & components.Context {
23+ Arguments : []string {"app-key" },
24+ }
25+ ctx .AddStringFlag ("application-name" , "test-app" )
26+ ctx .AddStringFlag ("project" , "test-project" )
27+ ctx .AddStringFlag ("desc" , "Test application" )
28+ ctx .AddStringFlag ("business-criticality" , "high" )
29+ ctx .AddStringFlag ("maturity-level" , "production" )
30+ ctx .AddStringFlag ("labels" , "env=prod;region=us-east" )
31+ ctx .AddStringFlag ("user-owners" , "john.doe;jane.smith" )
32+ ctx .AddStringFlag ("group-owners" , "devops;security" )
33+ ctx .AddStringFlag ("url" , "https://example.com" )
34+
2335 requestPayload := & model.AppDescriptor {
24- ApplicationKey : "app-key" ,
25- ApplicationName : "app-name" ,
26- ProjectKey : "proj-key" ,
36+ ApplicationKey : "app-key" ,
37+ ApplicationName : "test-app" ,
38+ ProjectKey : "test-project" ,
39+ Description : "Test application" ,
40+ BusinessCriticality : "high" ,
41+ MaturityLevel : "production" ,
42+ Labels : map [string ]string {
43+ "env" : "prod" ,
44+ "region" : "us-east" ,
45+ },
46+ UserOwners : []string {"john.doe" , "jane.smith" },
47+ GroupOwners : []string {"devops" , "security" },
2748 }
2849
2950 mockAppService := mockapps .NewMockApplicationService (ctrl )
3051 mockAppService .EXPECT ().CreateApplication (gomock .Any (), requestPayload ).Return (nil ).Times (1 )
3152
3253 cmd := & createAppCommand {
3354 applicationService : mockAppService ,
34- serverDetails : serverDetails ,
3555 requestBody : requestPayload ,
3656 }
3757
38- err := cmd .Run ( )
58+ err := cmd .prepareAndRunCommand ( ctx )
3959 assert .NoError (t , err )
4060}
4161
@@ -84,3 +104,199 @@ func TestCreateAppCommand_WrongNumberOfArguments(t *testing.T) {
84104 assert .Error (t , err )
85105 assert .Contains (t , err .Error (), "Wrong number of arguments" )
86106}
107+
108+ func TestCreateAppCommand_MissingProjectFlag (t * testing.T ) {
109+ ctrl := gomock .NewController (t )
110+ defer ctrl .Finish ()
111+
112+ ctx := & components.Context {
113+ Arguments : []string {"app-key" },
114+ }
115+ ctx .AddStringFlag ("application-name" , "test-app" )
116+ ctx .AddStringFlag ("url" , "https://example.com" )
117+ mockAppService := mockapps .NewMockApplicationService (ctrl )
118+
119+ cmd := & createAppCommand {
120+ applicationService : mockAppService ,
121+ }
122+
123+ err := cmd .prepareAndRunCommand (ctx )
124+ assert .Error (t , err )
125+ assert .Contains (t , err .Error (), "--project is mandatory" )
126+ }
127+
128+ func TestCreateAppCommand_Run_SpecFile (t * testing.T ) {
129+ tests := []struct {
130+ name string
131+ specPath string
132+ args []string
133+ expectsError bool
134+ errorContains string
135+ expectsPayload * model.AppDescriptor
136+ }{
137+ {
138+ name : "minimal spec file" ,
139+ specPath : "./testfiles/minimal-spec.json" ,
140+ args : []string {"app-min" },
141+ expectsPayload : & model.AppDescriptor {
142+ ApplicationKey : "app-min" ,
143+ ApplicationName : "app-min" ,
144+ ProjectKey : "test-project" ,
145+ },
146+ },
147+ {
148+ name : "full spec file" ,
149+ specPath : "./testfiles/full-spec.json" ,
150+ args : []string {"app-full" },
151+ expectsPayload : & model.AppDescriptor {
152+ ApplicationKey : "app-full" ,
153+ ApplicationName : "test-app-full" ,
154+ ProjectKey : "test-project" ,
155+ Description : "A comprehensive test application" ,
156+ MaturityLevel : "production" ,
157+ BusinessCriticality : "high" ,
158+ Labels : map [string ]string {
159+ "environment" : "production" ,
160+ "region" : "us-east-1" ,
161+ "team" : "devops" ,
162+ },
163+ UserOwners : []string {"john.doe" , "jane.smith" },
164+ GroupOwners : []string {"devops-team" , "security-team" },
165+ },
166+ },
167+ {
168+ name : "invalid spec file" ,
169+ specPath : "./testfiles/invalid-spec.json" ,
170+ args : []string {"app-invalid" },
171+ expectsError : true ,
172+ errorContains : "unexpected end of JSON input" ,
173+ },
174+ {
175+ name : "missing project key" ,
176+ specPath : "./testfiles/missing-project-spec.json" ,
177+ args : []string {"app-no-project" },
178+ expectsError : true ,
179+ errorContains : "project_key is mandatory in spec file" ,
180+ },
181+ {
182+ name : "non-existent spec file" ,
183+ specPath : "./testfiles/non-existent.json" ,
184+ args : []string {"app-nonexistent" },
185+ expectsError : true ,
186+ errorContains : "no such file or directory" ,
187+ },
188+ {
189+ name : "spec with application_key that should be ignored" ,
190+ specPath : "./testfiles/spec-with-app-key.json" ,
191+ args : []string {"command-line-app-key" },
192+ expectsPayload : & model.AppDescriptor {
193+ ApplicationKey : "command-line-app-key" ,
194+ ApplicationName : "test-app" ,
195+ ProjectKey : "test-project" ,
196+ Description : "A test application with application_key that should be ignored" ,
197+ },
198+ },
199+ }
200+
201+ for _ , tt := range tests {
202+ t .Run (tt .name , func (t * testing.T ) {
203+ ctrl := gomock .NewController (t )
204+ defer ctrl .Finish ()
205+
206+ ctx := & components.Context {
207+ Arguments : tt .args ,
208+ }
209+ ctx .AddStringFlag ("url" , "https://example.com" )
210+ ctx .AddStringFlag ("spec" , tt .specPath )
211+
212+ var actualPayload * model.AppDescriptor
213+ mockAppService := mockapps .NewMockApplicationService (ctrl )
214+ if ! tt .expectsError {
215+ mockAppService .EXPECT ().CreateApplication (gomock .Any (), gomock .Any ()).
216+ DoAndReturn (func (_ interface {}, req * model.AppDescriptor ) error {
217+ actualPayload = req
218+ return nil
219+ }).Times (1 )
220+ }
221+
222+ cmd := & createAppCommand {
223+ applicationService : mockAppService ,
224+ }
225+
226+ err := cmd .prepareAndRunCommand (ctx )
227+ if tt .expectsError {
228+ assert .Error (t , err )
229+ if tt .errorContains != "" {
230+ assert .Contains (t , err .Error (), tt .errorContains )
231+ }
232+ } else {
233+ assert .NoError (t , err )
234+ assert .Equal (t , tt .expectsPayload , actualPayload )
235+ }
236+ })
237+ }
238+ }
239+
240+ func TestCreateAppCommand_Run_SpecVars (t * testing.T ) {
241+ ctrl := gomock .NewController (t )
242+ defer ctrl .Finish ()
243+
244+ expectedPayload := & model.AppDescriptor {
245+ ApplicationKey : "app-with-vars" ,
246+ ApplicationName : "test-app" ,
247+ ProjectKey : "test-project" ,
248+ Description : "A test application for production" ,
249+ MaturityLevel : "production" ,
250+ BusinessCriticality : "high" ,
251+ Labels : map [string ]string {
252+ "environment" : "production" ,
253+ "region" : "us-east-1" ,
254+ },
255+ }
256+
257+ ctx := & components.Context {
258+ Arguments : []string {"app-with-vars" },
259+ }
260+ ctx .AddStringFlag ("spec" , "./testfiles/with-vars-spec.json" )
261+ ctx .AddStringFlag ("spec-vars" , "PROJECT_KEY=test-project;APP_NAME=test-app;ENVIRONMENT=production;MATURITY_LEVEL=production;CRITICALITY=high;REGION=us-east-1" )
262+ ctx .AddStringFlag ("url" , "https://example.com" )
263+
264+ var actualPayload * model.AppDescriptor
265+ mockAppService := mockapps .NewMockApplicationService (ctrl )
266+ mockAppService .EXPECT ().CreateApplication (gomock .Any (), gomock .Any ()).
267+ DoAndReturn (func (_ interface {}, req * model.AppDescriptor ) error {
268+ actualPayload = req
269+ return nil
270+ }).Times (1 )
271+
272+ cmd := & createAppCommand {
273+ applicationService : mockAppService ,
274+ }
275+
276+ err := cmd .prepareAndRunCommand (ctx )
277+ assert .NoError (t , err )
278+ assert .Equal (t , expectedPayload , actualPayload )
279+ }
280+
281+ func TestCreateAppCommand_Error_SpecAndFlags (t * testing.T ) {
282+ ctrl := gomock .NewController (t )
283+ defer ctrl .Finish ()
284+
285+ testSpecPath := "./testfiles/minimal-spec.json"
286+ ctx := & components.Context {
287+ Arguments : []string {"app-key" },
288+ }
289+ ctx .AddStringFlag ("spec" , testSpecPath )
290+ ctx .AddStringFlag ("project" , "test-project" )
291+ ctx .AddStringFlag ("url" , "https://example.com" )
292+
293+ mockAppService := mockapps .NewMockApplicationService (ctrl )
294+
295+ cmd := & createAppCommand {
296+ applicationService : mockAppService ,
297+ }
298+
299+ err := cmd .prepareAndRunCommand (ctx )
300+ assert .Error (t , err )
301+ assert .Contains (t , err .Error (), "the flag --project is not allowed when --spec is provided" )
302+ }
0 commit comments