This commit is contained in:
Tony Tam
2015-10-20 10:57:36 -07:00
107 changed files with 2619 additions and 403 deletions

View File

@@ -17,11 +17,14 @@ import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.fail;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertNull;
/**
* Tests for DefaultGenerator logic
@@ -44,6 +47,102 @@ public class DefaultGeneratorTest {
folder.delete();
}
@Test
public void testSecurityWithoutGlobal() throws Exception {
final Swagger swagger = new SwaggerParser().read("src/test/resources/2_0/petstore.json");
CodegenConfig codegenConfig = new JavaClientCodegen();
ClientOptInput clientOptInput = new ClientOptInput().opts(new ClientOpts()).swagger(swagger).config(codegenConfig);
DefaultGenerator gen = new DefaultGenerator();
gen.opts(clientOptInput);
Map<String, List<CodegenOperation>> paths = gen.processPaths(swagger.getPaths());
CodegenSecurity apiKey, petstoreAuth;
// security of "getPetById": api_key
CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById");
assertEquals(getPetById.authMethods.size(), 1);
apiKey = getPetById.authMethods.iterator().next();
assertEquals(apiKey.name, "api_key");
assertEquals(apiKey.type, "apiKey");
// security of "updatePetWithForm": petstore_auth
CodegenOperation updatePetWithForm = findCodegenOperationByOperationId(paths, "updatePetWithForm");
assertEquals(updatePetWithForm.authMethods.size(), 1);
petstoreAuth = updatePetWithForm.authMethods.iterator().next();
assertEquals(petstoreAuth.name, "petstore_auth");
assertEquals(petstoreAuth.type, "oauth2");
// security of "loginUser": null (no global security either)
CodegenOperation loginUser = findCodegenOperationByOperationId(paths, "loginUser");
assertNull(loginUser.authMethods);
}
@Test
public void testSecurityWithGlobal() throws Exception {
final Swagger swagger = new SwaggerParser().read("src/test/resources/2_0/globalSecurity.json");
CodegenConfig codegenConfig = new JavaClientCodegen();
ClientOptInput clientOptInput = new ClientOptInput().opts(new ClientOpts()).swagger(swagger).config(codegenConfig);
DefaultGenerator gen = new DefaultGenerator();
gen.opts(clientOptInput);
Map<String, List<CodegenOperation>> paths = gen.processPaths(swagger.getPaths());
CodegenSecurity cs, apiKey, apiKey2, petstoreAuth;
// security of "getPetById": api_key
CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById");
assertEquals(getPetById.authMethods.size(), 1);
apiKey = getPetById.authMethods.iterator().next();
assertEquals(apiKey.name, "api_key");
assertEquals(apiKey.type, "apiKey");
// security of "updatePetWithForm": petstore_auth
CodegenOperation updatePetWithForm = findCodegenOperationByOperationId(paths, "updatePetWithForm");
assertEquals(updatePetWithForm.authMethods.size(), 1);
petstoreAuth = updatePetWithForm.authMethods.iterator().next();
assertEquals(petstoreAuth.name, "petstore_auth");
assertEquals(petstoreAuth.type, "oauth2");
// security of "loginUser": api_key, petstore_auth (from global security)
CodegenOperation loginUser = findCodegenOperationByOperationId(paths, "loginUser");
assertEquals(loginUser.authMethods.size(), 2);
cs = loginUser.authMethods.get(0);
if ("api_key".equals(cs.name)) {
apiKey = cs;
petstoreAuth = loginUser.authMethods.get(1);
} else {
petstoreAuth = cs;
apiKey = loginUser.authMethods.get(1);
}
assertEquals(apiKey.name, "api_key");
assertEquals(apiKey.type, "apiKey");
assertEquals(petstoreAuth.name, "petstore_auth");
assertEquals(petstoreAuth.type, "oauth2");
// security of "logoutUser": null (override global security)
CodegenOperation logoutUser = findCodegenOperationByOperationId(paths, "logoutUser");
assertNull(logoutUser.authMethods);
// security of "getUserByName": api_key, api_key2 (override global security)
CodegenOperation getUserByName = findCodegenOperationByOperationId(paths, "getUserByName");
assertEquals(getUserByName.authMethods.size(), 2);
cs = getUserByName.authMethods.get(0);
if ("api_key".equals(cs.name)) {
apiKey = cs;
apiKey2 = getUserByName.authMethods.get(1);
} else {
apiKey2 = cs;
apiKey = getUserByName.authMethods.get(1);
}
assertEquals(apiKey.name, "api_key");
assertEquals(apiKey.type, "apiKey");
assertEquals(apiKey2.name, "api_key2");
assertEquals(apiKey2.type, "apiKey");
}
@Test
public void testSkipOverwrite() throws Exception {
final File output = folder.getRoot();
@@ -89,4 +188,15 @@ public class DefaultGeneratorTest {
out.close();
}
private CodegenOperation findCodegenOperationByOperationId(Map<String, List<CodegenOperation>> paths, String operationId) {
for (List<CodegenOperation> ops : paths.values()) {
for (CodegenOperation co : ops) {
if (operationId.equals(co.operationId)) {
return co;
}
}
}
return null;
}
}