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,95 @@ 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 cs , apiKey , petstoreAuth ;
62
+
63
+ // security of "getPetById": api_key, petstore_auth
64
+ CodegenOperation getPetById = findCodegenOperationByOperationId (paths , "getPetById" );
65
+ assertEquals (getPetById .authMethods .size (), 2 );
66
+ cs = getPetById .authMethods .get (0 );
67
+ if ("api_key" .equals (cs .name )) {
68
+ apiKey = cs ;
69
+ petstoreAuth = getPetById .authMethods .get (1 );
70
+ } else {
71
+ petstoreAuth = cs ;
72
+ apiKey = getPetById .authMethods .get (1 );
73
+ }
74
+ assertEquals (apiKey .name , "api_key" );
75
+ assertEquals (apiKey .type , "apiKey" );
76
+ assertEquals (petstoreAuth .name , "petstore_auth" );
77
+ assertEquals (petstoreAuth .type , "oauth2" );
78
+
79
+ // security of "updatePetWithForm": petstore_auth
80
+ CodegenOperation updatePetWithForm = findCodegenOperationByOperationId (paths , "updatePetWithForm" );
81
+ assertEquals (updatePetWithForm .authMethods .size (), 1 );
82
+ petstoreAuth = updatePetWithForm .authMethods .iterator ().next ();
83
+ assertEquals (petstoreAuth .name , "petstore_auth" );
84
+ assertEquals (petstoreAuth .type , "oauth2" );
85
+
86
+ // security of "loginUser": null (no global security either)
87
+ CodegenOperation loginUser = findCodegenOperationByOperationId (paths , "loginUser" );
88
+ assertNull (loginUser .authMethods );
89
+ }
90
+
91
+ @ Test
92
+ public void testSecurityWithGlobal () throws Exception {
93
+ final Swagger swagger = new SwaggerParser ().read ("src/test/resources/2_0/globalSecurity.json" );
94
+ CodegenConfig codegenConfig = new JavaClientCodegen ();
95
+
96
+ ClientOptInput clientOptInput = new ClientOptInput ().opts (new ClientOpts ()).swagger (swagger ).config (codegenConfig );
97
+
98
+ DefaultGenerator gen = new DefaultGenerator ();
99
+ gen .opts (clientOptInput );
100
+ Map <String , List <CodegenOperation >> paths = gen .processPaths (swagger .getPaths ());
101
+
102
+ CodegenSecurity cs , apiKey , petstoreAuth ;
103
+
104
+ // security of "getPetById": api_key, petstore_auth
105
+ CodegenOperation getPetById = findCodegenOperationByOperationId (paths , "getPetById" );
106
+ assertEquals (getPetById .authMethods .size (), 2 );
107
+ cs = getPetById .authMethods .get (0 );
108
+ if ("api_key" .equals (cs .name )) {
109
+ apiKey = cs ;
110
+ petstoreAuth = getPetById .authMethods .get (1 );
111
+ } else {
112
+ petstoreAuth = cs ;
113
+ apiKey = getPetById .authMethods .get (1 );
114
+ }
115
+ assertEquals (apiKey .name , "api_key" );
116
+ assertEquals (apiKey .type , "apiKey" );
117
+ assertEquals (petstoreAuth .name , "petstore_auth" );
118
+ assertEquals (petstoreAuth .type , "oauth2" );
119
+
120
+ // security of "updatePetWithForm": petstore_auth
121
+ CodegenOperation updatePetWithForm = findCodegenOperationByOperationId (paths , "updatePetWithForm" );
122
+ assertEquals (updatePetWithForm .authMethods .size (), 1 );
123
+ petstoreAuth = updatePetWithForm .authMethods .iterator ().next ();
124
+ assertEquals (petstoreAuth .name , "petstore_auth" );
125
+ assertEquals (petstoreAuth .type , "oauth2" );
126
+
127
+ // security of "loginUser": api_key (from global security)
128
+ CodegenOperation loginUser = findCodegenOperationByOperationId (paths , "loginUser" );
129
+ assertEquals (loginUser .authMethods .size (), 1 );
130
+ apiKey = loginUser .authMethods .iterator ().next ();
131
+ assertEquals (apiKey .name , "api_key" );
132
+ assertEquals (apiKey .type , "apiKey" );
133
+
134
+ // security of "logoutUser": null (override global security)
135
+ CodegenOperation logoutUser = findCodegenOperationByOperationId (paths , "logoutUser" );
136
+ assertNull (logoutUser .authMethods );
137
+ }
138
+
47
139
@ Test
48
140
public void testSkipOverwrite () throws Exception {
49
141
final File output = folder .getRoot ();
@@ -89,4 +181,15 @@ private void changeContent(File file) throws IOException {
89
181
out .close ();
90
182
}
91
183
184
+ private CodegenOperation findCodegenOperationByOperationId (Map <String , List <CodegenOperation >> paths , String operationId ) {
185
+ for (List <CodegenOperation > ops : paths .values ()) {
186
+ for (CodegenOperation co : ops ) {
187
+ if (operationId .equals (co .operationId )) {
188
+ return co ;
189
+ }
190
+ }
191
+ }
192
+ return null ;
193
+ }
194
+
92
195
}
0 commit comments