17
17
import java .io .Writer ;
18
18
import java .nio .charset .StandardCharsets ;
19
19
import java .util .Arrays ;
20
+ import java .util .List ;
21
+ import java .util .Map ;
20
22
21
23
import static java .nio .charset .StandardCharsets .UTF_8 ;
22
24
import static org .junit .Assert .fail ;
23
25
import static org .testng .Assert .assertEquals ;
24
26
import static org .testng .Assert .assertTrue ;
27
+ import static org .testng .Assert .assertNull ;
25
28
26
29
/**
27
30
* Tests for DefaultGenerator logic
@@ -44,6 +47,102 @@ public void tearDown() throws Exception {
44
47
folder .delete ();
45
48
}
46
49
50
+ @ Test
51
+ public void testSecurityWithoutGlobal () throws Exception {
52
+ final Swagger swagger = new SwaggerParser ().read ("src/test/resources/2_0/petstore.json" );
53
+ CodegenConfig codegenConfig = new JavaClientCodegen ();
54
+
55
+ ClientOptInput clientOptInput = new ClientOptInput ().opts (new ClientOpts ()).swagger (swagger ).config (codegenConfig );
56
+
57
+ DefaultGenerator gen = new DefaultGenerator ();
58
+ gen .opts (clientOptInput );
59
+ Map <String , List <CodegenOperation >> paths = gen .processPaths (swagger .getPaths ());
60
+
61
+ CodegenSecurity apiKey , petstoreAuth ;
62
+
63
+ // security of "getPetById": api_key
64
+ CodegenOperation getPetById = findCodegenOperationByOperationId (paths , "getPetById" );
65
+ assertEquals (getPetById .authMethods .size (), 1 );
66
+ apiKey = getPetById .authMethods .iterator ().next ();
67
+ assertEquals (apiKey .name , "api_key" );
68
+ assertEquals (apiKey .type , "apiKey" );
69
+
70
+ // security of "updatePetWithForm": petstore_auth
71
+ CodegenOperation updatePetWithForm = findCodegenOperationByOperationId (paths , "updatePetWithForm" );
72
+ assertEquals (updatePetWithForm .authMethods .size (), 1 );
73
+ petstoreAuth = updatePetWithForm .authMethods .iterator ().next ();
74
+ assertEquals (petstoreAuth .name , "petstore_auth" );
75
+ assertEquals (petstoreAuth .type , "oauth2" );
76
+
77
+ // security of "loginUser": null (no global security either)
78
+ CodegenOperation loginUser = findCodegenOperationByOperationId (paths , "loginUser" );
79
+ assertNull (loginUser .authMethods );
80
+ }
81
+
82
+ @ Test
83
+ public void testSecurityWithGlobal () throws Exception {
84
+ final Swagger swagger = new SwaggerParser ().read ("src/test/resources/2_0/globalSecurity.json" );
85
+ CodegenConfig codegenConfig = new JavaClientCodegen ();
86
+
87
+ ClientOptInput clientOptInput = new ClientOptInput ().opts (new ClientOpts ()).swagger (swagger ).config (codegenConfig );
88
+
89
+ DefaultGenerator gen = new DefaultGenerator ();
90
+ gen .opts (clientOptInput );
91
+ Map <String , List <CodegenOperation >> paths = gen .processPaths (swagger .getPaths ());
92
+
93
+ CodegenSecurity cs , apiKey , apiKey2 , petstoreAuth ;
94
+
95
+ // security of "getPetById": api_key
96
+ CodegenOperation getPetById = findCodegenOperationByOperationId (paths , "getPetById" );
97
+ assertEquals (getPetById .authMethods .size (), 1 );
98
+ apiKey = getPetById .authMethods .iterator ().next ();
99
+ assertEquals (apiKey .name , "api_key" );
100
+ assertEquals (apiKey .type , "apiKey" );
101
+
102
+ // security of "updatePetWithForm": petstore_auth
103
+ CodegenOperation updatePetWithForm = findCodegenOperationByOperationId (paths , "updatePetWithForm" );
104
+ assertEquals (updatePetWithForm .authMethods .size (), 1 );
105
+ petstoreAuth = updatePetWithForm .authMethods .iterator ().next ();
106
+ assertEquals (petstoreAuth .name , "petstore_auth" );
107
+ assertEquals (petstoreAuth .type , "oauth2" );
108
+
109
+ // security of "loginUser": api_key, petstore_auth (from global security)
110
+ CodegenOperation loginUser = findCodegenOperationByOperationId (paths , "loginUser" );
111
+ assertEquals (loginUser .authMethods .size (), 2 );
112
+ cs = loginUser .authMethods .get (0 );
113
+ if ("api_key" .equals (cs .name )) {
114
+ apiKey = cs ;
115
+ petstoreAuth = loginUser .authMethods .get (1 );
116
+ } else {
117
+ petstoreAuth = cs ;
118
+ apiKey = loginUser .authMethods .get (1 );
119
+ }
120
+ assertEquals (apiKey .name , "api_key" );
121
+ assertEquals (apiKey .type , "apiKey" );
122
+ assertEquals (petstoreAuth .name , "petstore_auth" );
123
+ assertEquals (petstoreAuth .type , "oauth2" );
124
+
125
+ // security of "logoutUser": null (override global security)
126
+ CodegenOperation logoutUser = findCodegenOperationByOperationId (paths , "logoutUser" );
127
+ assertNull (logoutUser .authMethods );
128
+
129
+ // security of "getUserByName": api_key, api_key2 (override global security)
130
+ CodegenOperation getUserByName = findCodegenOperationByOperationId (paths , "getUserByName" );
131
+ assertEquals (getUserByName .authMethods .size (), 2 );
132
+ cs = getUserByName .authMethods .get (0 );
133
+ if ("api_key" .equals (cs .name )) {
134
+ apiKey = cs ;
135
+ apiKey2 = getUserByName .authMethods .get (1 );
136
+ } else {
137
+ apiKey2 = cs ;
138
+ apiKey = getUserByName .authMethods .get (1 );
139
+ }
140
+ assertEquals (apiKey .name , "api_key" );
141
+ assertEquals (apiKey .type , "apiKey" );
142
+ assertEquals (apiKey2 .name , "api_key2" );
143
+ assertEquals (apiKey2 .type , "apiKey" );
144
+ }
145
+
47
146
@ Test
48
147
public void testSkipOverwrite () throws Exception {
49
148
final File output = folder .getRoot ();
@@ -89,4 +188,15 @@ private void changeContent(File file) throws IOException {
89
188
out .close ();
90
189
}
91
190
191
+ private CodegenOperation findCodegenOperationByOperationId (Map <String , List <CodegenOperation >> paths , String operationId ) {
192
+ for (List <CodegenOperation > ops : paths .values ()) {
193
+ for (CodegenOperation co : ops ) {
194
+ if (operationId .equals (co .operationId )) {
195
+ return co ;
196
+ }
197
+ }
198
+ }
199
+ return null ;
200
+ }
201
+
92
202
}
0 commit comments