From 6eb1a89205b0ca495ce81bda098cd8e46585b88d Mon Sep 17 00:00:00 2001 From: xhh Date: Fri, 29 May 2015 11:28:03 +0800 Subject: [PATCH] Add authentication helper methods and test cases --- .../resources/Java/configuration.mustache | 44 +++++++++++++++ .../java/io/swagger/client/Configuration.java | 44 +++++++++++++++ .../io/swagger/client/ConfigurationTest.java | 56 +++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 samples/client/petstore/java/src/test/java/io/swagger/client/ConfigurationTest.java diff --git a/modules/swagger-codegen/src/main/resources/Java/configuration.mustache b/modules/swagger-codegen/src/main/resources/Java/configuration.mustache index aaeb532829b..cd367c65704 100644 --- a/modules/swagger-codegen/src/main/resources/Java/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/configuration.mustache @@ -24,4 +24,48 @@ public class Configuration { public static Authentication getAuthentication(String authName) { return AUTH.get(authName); } + + /** Set username for the first HTTP basic authentication. */ + public static void setUsername(String username) { + for (Authentication auth : AUTH.values()) { + if (auth instanceof HttpBasicAuth) { + ((HttpBasicAuth) auth).setUsername(username); + return; + } + } + throw new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Set password for the first HTTP basic authentication. */ + public static void setPassword(String password) { + for (Authentication auth : AUTH.values()) { + if (auth instanceof HttpBasicAuth) { + ((HttpBasicAuth) auth).setPassword(password); + return; + } + } + throw new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Set API key value for the first API key authentication. */ + public static void setApiKey(String apiKey) { + for (Authentication auth : AUTH.values()) { + if (auth instanceof ApiKeyAuth) { + ((ApiKeyAuth) auth).setApiKey(apiKey); + return; + } + } + throw new RuntimeException("No API key authentication configured!"); + } + + /** Set API key prefix for the first API key authentication. */ + public static void setApiKeyPrefix(String apiKeyPrefix) { + for (Authentication auth : AUTH.values()) { + if (auth instanceof ApiKeyAuth) { + ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix); + return; + } + } + throw new RuntimeException("No API key authentication configured!"); + } } diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/Configuration.java b/samples/client/petstore/java/src/main/java/io/swagger/client/Configuration.java index 753a3f215cc..6fb5fb56e7c 100644 --- a/samples/client/petstore/java/src/main/java/io/swagger/client/Configuration.java +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/Configuration.java @@ -28,4 +28,48 @@ public class Configuration { public static Authentication getAuthentication(String authName) { return AUTH.get(authName); } + + /** Set username for the first HTTP basic authentication. */ + public static void setUsername(String username) { + for (Authentication auth : AUTH.values()) { + if (auth instanceof HttpBasicAuth) { + ((HttpBasicAuth) auth).setUsername(username); + return; + } + } + throw new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Set password for the first HTTP basic authentication. */ + public static void setPassword(String password) { + for (Authentication auth : AUTH.values()) { + if (auth instanceof HttpBasicAuth) { + ((HttpBasicAuth) auth).setPassword(password); + return; + } + } + throw new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Set API key value for the first API key authentication. */ + public static void setApiKey(String apiKey) { + for (Authentication auth : AUTH.values()) { + if (auth instanceof ApiKeyAuth) { + ((ApiKeyAuth) auth).setApiKey(apiKey); + return; + } + } + throw new RuntimeException("No API key authentication configured!"); + } + + /** Set API key prefix for the first API key authentication. */ + public static void setApiKeyPrefix(String apiKeyPrefix) { + for (Authentication auth : AUTH.values()) { + if (auth instanceof ApiKeyAuth) { + ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix); + return; + } + } + throw new RuntimeException("No API key authentication configured!"); + } } diff --git a/samples/client/petstore/java/src/test/java/io/swagger/client/ConfigurationTest.java b/samples/client/petstore/java/src/test/java/io/swagger/client/ConfigurationTest.java new file mode 100644 index 00000000000..b7c51739359 --- /dev/null +++ b/samples/client/petstore/java/src/test/java/io/swagger/client/ConfigurationTest.java @@ -0,0 +1,56 @@ +package io.swagger.client; + +import io.swagger.client.auth.*; + +import static org.junit.Assert.*; +import org.junit.*; + +public class ConfigurationTest { + @Test + public void testGetAuthentication() { + Authentication auth = Configuration.getAuthentication("api_key"); + assertNotNull(auth); + assertTrue(auth instanceof ApiKeyAuth); + ApiKeyAuth apiKeyAuth = (ApiKeyAuth) auth; + assertEquals("header", apiKeyAuth.getLocation()); + assertEquals("api_key", apiKeyAuth.getParamName()); + + auth = Configuration.getAuthentication("petstore_auth"); + assertTrue(auth instanceof OAuth); + + assertNull(Configuration.getAuthentication("unknown")); + } + + @Test + public void testSetUsername() { + try { + Configuration.setUsername("my-username"); + fail("should throw RuntimeException"); + } catch (RuntimeException e) { + } + } + + @Test + public void testSetPassword() { + try { + Configuration.setPassword("my-password"); + fail("should throw RuntimeException"); + } catch (RuntimeException e) { + } + } + + @Test + public void testSetApiKeyAndPrefix() { + ApiKeyAuth auth = (ApiKeyAuth) Configuration.getAuthentication("api_key"); + auth.setApiKey(null); + auth.setApiKeyPrefix(null); + + Configuration.setApiKey("my-api-key"); + Configuration.setApiKeyPrefix("Token"); + assertEquals("my-api-key", auth.getApiKey()); + assertEquals("Token", auth.getApiKeyPrefix()); + + auth.setApiKey(null); + auth.setApiKeyPrefix(null); + } +}